1 //===- CodegenCleanup.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "polly/CodeGen/CodegenCleanup.h"
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"
22 #define DEBUG_TYPE "polly-cleanup"
25 using namespace polly
;
29 class CodegenCleanup
: public FunctionPass
{
31 CodegenCleanup(const CodegenCleanup
&) = delete;
32 const CodegenCleanup
&operator=(const CodegenCleanup
&) = delete;
34 llvm::legacy::FunctionPassManager
*FPM
;
38 explicit CodegenCleanup() : FunctionPass(ID
), FPM(nullptr) {}
40 /// @name FunctionPass interface
42 virtual void getAnalysisUsage(llvm::AnalysisUsage
&AU
) const override
{}
44 virtual bool doInitialization(Module
&M
) override
{
47 FPM
= new llvm::legacy::FunctionPassManager(&M
);
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());
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
61 FPM
->add(createCFGSimplificationPass());
62 FPM
->add(createSROAPass());
63 FPM
->add(createEarlyCSEPass());
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());
105 return FPM
->doInitialization();
108 virtual bool doFinalization(Module
&M
) override
{
109 bool Result
= FPM
->doFinalization();
117 virtual bool runOnFunction(llvm::Function
&F
) override
{
118 if (!F
.hasFnAttribute("polly-optimized")) {
120 dbgs() << F
.getName()
121 << ": Skipping cleanup because Polly did not optimize it.");
125 LLVM_DEBUG(dbgs() << F
.getName() << ": Running codegen cleanup...");
131 char CodegenCleanup::ID
;
134 FunctionPass
*polly::createCodegenCleanupPass() { return new CodegenCleanup(); }
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)