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/ScopedNoAliasAA.h"
13 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/LegacyPassManager.h"
16 #include "llvm/PassInfo.h"
17 #include "llvm/PassRegistry.h"
18 #include "llvm/PassSupport.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Transforms/Scalar.h"
21 #include "llvm/Transforms/Scalar/GVN.h"
22 
23 #define DEBUG_TYPE "polly-cleanup"
24 
25 using namespace llvm;
26 using namespace polly;
27 
28 namespace {
29 
30 class CodegenCleanup : public FunctionPass {
31 private:
32   CodegenCleanup(const CodegenCleanup &) = delete;
33   const CodegenCleanup &operator=(const CodegenCleanup &) = delete;
34 
35   llvm::legacy::FunctionPassManager *FPM;
36 
37 public:
38   static char ID;
39   explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {}
40 
41   /// @name FunctionPass interface
42   //@{
43   virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {}
44 
45   virtual bool doInitialization(Module &M) override {
46     assert(!FPM);
47 
48     FPM = new llvm::legacy::FunctionPassManager(&M);
49 
50     // TODO: How to make parent passes discoverable?
51     // TODO: Should be sensitive to compiler options in PassManagerBuilder, to
52     // which we do not have access here.
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(createGVNPass());
74     FPM->add(createLICMPass());
75     FPM->add(createLoopUnswitchPass());
76     FPM->add(createCFGSimplificationPass());
77     FPM->add(createInstructionCombiningPass());
78     FPM->add(createIndVarSimplifyPass());
79     FPM->add(createLoopIdiomPass());
80     FPM->add(createLoopDeletionPass());
81     FPM->add(createCFGSimplificationPass());
82     FPM->add(createSimpleLoopUnrollPass());
83     FPM->add(createMergedLoadStoreMotionPass());
84     FPM->add(createMemCpyOptPass());
85     FPM->add(createBitTrackingDCEPass());
86     FPM->add(createInstructionCombiningPass());
87     FPM->add(createJumpThreadingPass());
88     FPM->add(createCorrelatedValuePropagationPass());
89     FPM->add(createDeadStoreEliminationPass());
90     FPM->add(createLICMPass());
91     FPM->add(createLoopRerollPass());
92     FPM->add(createAggressiveDCEPass());
93     FPM->add(createCFGSimplificationPass());
94     FPM->add(createInstructionCombiningPass());
95 
96     return FPM->doInitialization();
97   }
98 
99   virtual bool doFinalization(Module &M) override {
100     bool Result = FPM->doFinalization();
101 
102     delete FPM;
103     FPM = nullptr;
104 
105     return Result;
106   }
107 
108   virtual bool runOnFunction(llvm::Function &F) override {
109     if (!F.hasFnAttribute("polly-optimized")) {
110       DEBUG(dbgs() << F.getName()
111                    << ": Skipping cleanup because Polly did not optimize it.");
112       return false;
113     }
114 
115     DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
116     return FPM->run(F);
117   }
118   //@}
119 };
120 
121 char CodegenCleanup::ID;
122 } // namespace
123 
124 FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
125 
126 INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
127                       "Polly - Cleanup after code generation", false, false)
128 INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
129                     "Polly - Cleanup after code generation", false, false)
130