1 #include "polly/CodeGen/CodegenCleanup.h"
2 
3 #include "llvm/Analysis/CFLAliasAnalysis.h"
4 #include "llvm/Analysis/ScopedNoAliasAA.h"
5 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
6 #include "llvm/IR/Function.h"
7 #include "llvm/IR/LegacyPassManager.h"
8 #include "llvm/PassInfo.h"
9 #include "llvm/PassRegistry.h"
10 #include "llvm/PassSupport.h"
11 #include "llvm/Support/Debug.h"
12 #include "llvm/Transforms/Scalar.h"
13 #define DEBUG_TYPE "polly-cleanup"
14 
15 using namespace llvm;
16 using namespace polly;
17 
18 namespace {
19 
20 class CodegenCleanup : public FunctionPass {
21 private:
22   CodegenCleanup(const CodegenCleanup &) = delete;
23   const CodegenCleanup &operator=(const CodegenCleanup &) = delete;
24 
25   llvm::legacy::FunctionPassManager *FPM;
26 
27 public:
28   static char ID;
29   explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {}
30 
31   /// @name FunctionPass interface
32   //@{
33   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {}
34 
35   virtual bool doInitialization(Module &M) override {
36     assert(!FPM);
37 
38     FPM = new llvm::legacy::FunctionPassManager(&M);
39 
40     // TODO: How to make parent passes discoverable?
41     // TODO: Should be sensitive to compiler options in PassManagerBuilder, to
42     // which wo do not have access here.
43     FPM->add(createCFLAAWrapperPass());
44     FPM->add(createScopedNoAliasAAWrapperPass());
45     FPM->add(createTypeBasedAAWrapperPass());
46     FPM->add(createAAResultsWrapperPass());
47 
48     // TODO: These are non-conditional passes that run between
49     // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not
50     // miss any optimization that would have run after Polly with
51     // -polly-position=early. This can probably be reduced to a more compact set
52     // of passes.
53     FPM->add(createCFGSimplificationPass());
54     FPM->add(createScalarReplAggregatesPass());
55     FPM->add(createEarlyCSEPass());
56     FPM->add(createInstructionCombiningPass());
57     FPM->add(createJumpThreadingPass());
58     FPM->add(createCorrelatedValuePropagationPass());
59     FPM->add(createCFGSimplificationPass());
60     FPM->add(createInstructionCombiningPass());
61     FPM->add(createCFGSimplificationPass());
62     FPM->add(createReassociatePass());
63     FPM->add(createLoopRotatePass());
64     FPM->add(createLICMPass());
65     FPM->add(createLoopUnswitchPass());
66     FPM->add(createCFGSimplificationPass());
67     FPM->add(createInstructionCombiningPass());
68     FPM->add(createIndVarSimplifyPass());
69     FPM->add(createLoopIdiomPass());
70     FPM->add(createLoopDeletionPass());
71     FPM->add(createLoopInterchangePass());
72     FPM->add(createCFGSimplificationPass());
73     FPM->add(createSimpleLoopUnrollPass());
74     FPM->add(createMergedLoadStoreMotionPass());
75     FPM->add(createMemCpyOptPass());
76     FPM->add(createBitTrackingDCEPass());
77     FPM->add(createInstructionCombiningPass());
78     FPM->add(createJumpThreadingPass());
79     FPM->add(createCorrelatedValuePropagationPass());
80     FPM->add(createDeadStoreEliminationPass());
81     FPM->add(createLICMPass());
82     FPM->add(createLoopRerollPass());
83     FPM->add(createLoadCombinePass());
84     FPM->add(createAggressiveDCEPass());
85     FPM->add(createCFGSimplificationPass());
86     FPM->add(createInstructionCombiningPass());
87 
88     return FPM->doInitialization();
89   }
90 
91   virtual bool doFinalization(Module &M) override {
92     bool Result = FPM->doFinalization();
93 
94     delete FPM;
95     FPM = nullptr;
96 
97     return Result;
98   }
99 
100   virtual bool runOnFunction(llvm::Function &F) override {
101     if (!F.hasFnAttribute("polly-optimized")) {
102       DEBUG(dbgs() << F.getName()
103                    << ": Skipping cleanup because Polly did not optimize it.");
104       return false;
105     }
106 
107     DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
108     return FPM->run(F);
109   }
110   //@}
111 };
112 
113 char CodegenCleanup::ID;
114 }
115 
116 FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
117 
118 INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
119                       "Polly - Cleanup after code generation", false, false)
120 INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
121                     "Polly - Cleanup after code generation", false, false)
122