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(createScopedNoAliasAAWrapperPass());
53     FPM->add(createTypeBasedAAWrapperPass());
54     FPM->add(createAAResultsWrapperPass());
55 
56     // TODO: These are non-conditional passes that run between
57     // EP_ModuleOptimizerEarly and EP_VectorizerStart just to ensure we do not
58     // miss any optimization that would have run after Polly with
59     // -polly-position=early. This can probably be reduced to a more compact set
60     // of passes.
61     FPM->add(createCFGSimplificationPass());
62     FPM->add(createSROAPass());
63     FPM->add(createEarlyCSEPass());
64     FPM->add(createInstructionCombiningPass());
65     FPM->add(createJumpThreadingPass());
66     FPM->add(createCorrelatedValuePropagationPass());
67     FPM->add(createCFGSimplificationPass());
68     FPM->add(createInstructionCombiningPass());
69     FPM->add(createCFGSimplificationPass());
70     FPM->add(createReassociatePass());
71     FPM->add(createLoopRotatePass());
72     FPM->add(createLICMPass());
73     FPM->add(createLoopUnswitchPass());
74     FPM->add(createCFGSimplificationPass());
75     FPM->add(createInstructionCombiningPass());
76     FPM->add(createIndVarSimplifyPass());
77     FPM->add(createLoopIdiomPass());
78     FPM->add(createLoopDeletionPass());
79     FPM->add(createCFGSimplificationPass());
80     FPM->add(createSimpleLoopUnrollPass());
81     FPM->add(createMergedLoadStoreMotionPass());
82     FPM->add(createMemCpyOptPass());
83     FPM->add(createBitTrackingDCEPass());
84     FPM->add(createInstructionCombiningPass());
85     FPM->add(createJumpThreadingPass());
86     FPM->add(createCorrelatedValuePropagationPass());
87     FPM->add(createDeadStoreEliminationPass());
88     FPM->add(createLICMPass());
89     FPM->add(createLoopRerollPass());
90     FPM->add(createAggressiveDCEPass());
91     FPM->add(createCFGSimplificationPass());
92     FPM->add(createInstructionCombiningPass());
93 
94     return FPM->doInitialization();
95   }
96 
97   virtual bool doFinalization(Module &M) override {
98     bool Result = FPM->doFinalization();
99 
100     delete FPM;
101     FPM = nullptr;
102 
103     return Result;
104   }
105 
106   virtual bool runOnFunction(llvm::Function &F) override {
107     if (!F.hasFnAttribute("polly-optimized")) {
108       DEBUG(dbgs() << F.getName()
109                    << ": Skipping cleanup because Polly did not optimize it.");
110       return false;
111     }
112 
113     DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
114     return FPM->run(F);
115   }
116   //@}
117 };
118 
119 char CodegenCleanup::ID;
120 } // namespace
121 
122 FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
123 
124 INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
125                       "Polly - Cleanup after code generation", false, false)
126 INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
127                     "Polly - Cleanup after code generation", false, false)
128