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(createSROAPass());
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(createCFGSimplificationPass());
72     FPM->add(createSimpleLoopUnrollPass());
73     FPM->add(createMergedLoadStoreMotionPass());
74     FPM->add(createMemCpyOptPass());
75     FPM->add(createBitTrackingDCEPass());
76     FPM->add(createInstructionCombiningPass());
77     FPM->add(createJumpThreadingPass());
78     FPM->add(createCorrelatedValuePropagationPass());
79     FPM->add(createDeadStoreEliminationPass());
80     FPM->add(createLICMPass());
81     FPM->add(createLoopRerollPass());
82     FPM->add(createAggressiveDCEPass());
83     FPM->add(createCFGSimplificationPass());
84     FPM->add(createInstructionCombiningPass());
85 
86     return FPM->doInitialization();
87   }
88 
89   virtual bool doFinalization(Module &M) override {
90     bool Result = FPM->doFinalization();
91 
92     delete FPM;
93     FPM = nullptr;
94 
95     return Result;
96   }
97 
98   virtual bool runOnFunction(llvm::Function &F) override {
99     if (!F.hasFnAttribute("polly-optimized")) {
100       DEBUG(dbgs() << F.getName()
101                    << ": Skipping cleanup because Polly did not optimize it.");
102       return false;
103     }
104 
105     DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
106     return FPM->run(F);
107   }
108   //@}
109 };
110 
111 char CodegenCleanup::ID;
112 }
113 
114 FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
115 
116 INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
117                       "Polly - Cleanup after code generation", false, false)
118 INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
119                     "Polly - Cleanup after code generation", false, false)
120