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