1 //===- CodegenCleanup.cpp -------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "polly/CodeGen/CodegenCleanup.h"
10
11 #include "llvm/Analysis/ScopedNoAliasAA.h"
12 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
13 #include "llvm/IR/Function.h"
14 #include "llvm/IR/LegacyPassManager.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Transforms/InstCombine/InstCombine.h"
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Transforms/Scalar/GVN.h"
20 #include "llvm/Transforms/Utils.h"
21
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;
CodegenCleanup()38 explicit CodegenCleanup() : FunctionPass(ID), FPM(nullptr) {}
39
40 /// @name FunctionPass interface
41 //@{
getAnalysisUsage(llvm::AnalysisUsage & AU) const42 virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {}
43
doInitialization(Module & M)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 we 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
65 FPM->add(createPromoteMemoryToRegisterPass());
66 FPM->add(createInstructionCombiningPass(true));
67 FPM->add(createCFGSimplificationPass());
68 FPM->add(createSROAPass());
69 FPM->add(createEarlyCSEPass(true));
70 FPM->add(createSpeculativeExecutionIfHasBranchDivergencePass());
71 FPM->add(createJumpThreadingPass());
72 FPM->add(createCorrelatedValuePropagationPass());
73 FPM->add(createCFGSimplificationPass());
74 FPM->add(createInstructionCombiningPass(true));
75 FPM->add(createLibCallsShrinkWrapPass());
76 FPM->add(createTailCallEliminationPass());
77 FPM->add(createCFGSimplificationPass());
78 FPM->add(createReassociatePass());
79 FPM->add(createLoopRotatePass(-1));
80 FPM->add(createGVNPass());
81 FPM->add(createLICMPass());
82 FPM->add(createLoopUnswitchPass());
83 FPM->add(createCFGSimplificationPass());
84 FPM->add(createInstructionCombiningPass(true));
85 FPM->add(createIndVarSimplifyPass());
86 FPM->add(createLoopIdiomPass());
87 FPM->add(createLoopDeletionPass());
88 FPM->add(createCFGSimplificationPass());
89 FPM->add(createSimpleLoopUnrollPass(3));
90 FPM->add(createMergedLoadStoreMotionPass());
91 FPM->add(createGVNPass());
92 FPM->add(createMemCpyOptPass());
93 FPM->add(createSCCPPass());
94 FPM->add(createBitTrackingDCEPass());
95 FPM->add(createInstructionCombiningPass(true));
96 FPM->add(createJumpThreadingPass());
97 FPM->add(createCorrelatedValuePropagationPass());
98 FPM->add(createDeadStoreEliminationPass());
99 FPM->add(createLICMPass());
100 FPM->add(createAggressiveDCEPass());
101 FPM->add(createCFGSimplificationPass());
102 FPM->add(createInstructionCombiningPass(true));
103 FPM->add(createFloat2IntPass());
104
105 return FPM->doInitialization();
106 }
107
doFinalization(Module & M)108 virtual bool doFinalization(Module &M) override {
109 bool Result = FPM->doFinalization();
110
111 delete FPM;
112 FPM = nullptr;
113
114 return Result;
115 }
116
runOnFunction(llvm::Function & F)117 virtual bool runOnFunction(llvm::Function &F) override {
118 if (!F.hasFnAttribute("polly-optimized")) {
119 LLVM_DEBUG(
120 dbgs() << F.getName()
121 << ": Skipping cleanup because Polly did not optimize it.");
122 return false;
123 }
124
125 LLVM_DEBUG(dbgs() << F.getName() << ": Running codegen cleanup...");
126 return FPM->run(F);
127 }
128 //@}
129 };
130
131 char CodegenCleanup::ID;
132 } // namespace
133
createCodegenCleanupPass()134 FunctionPass *polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
135
136 INITIALIZE_PASS_BEGIN(CodegenCleanup, "polly-cleanup",
137 "Polly - Cleanup after code generation", false, false)
138 INITIALIZE_PASS_END(CodegenCleanup, "polly-cleanup",
139 "Polly - Cleanup after code generation", false, false)
140