1 //===---- Canonicalization.cpp - Run canonicalization passes --------------===//
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 // Run the set of default canonicalization passes.
11 // This pass is mainly used for debugging.
13 //===----------------------------------------------------------------------===//
15 #include "polly/Canonicalization.h"
16 #include "polly/LinkAllPasses.h"
17 #include "polly/Options.h"
18 #include "llvm/Analysis/GlobalsModRef.h"
19 #include "llvm/Analysis/ProfileSummaryInfo.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/Transforms/IPO.h"
22 #include "llvm/Transforms/IPO/FunctionAttrs.h"
23 #include "llvm/Transforms/InstCombine/InstCombine.h"
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Transforms/Scalar/EarlyCSE.h"
26 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
27 #include "llvm/Transforms/Scalar/LoopRotation.h"
28 #include "llvm/Transforms/Scalar/Reassociate.h"
29 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
30 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
31 #include "llvm/Transforms/Utils.h"
32 #include "llvm/Transforms/Utils/Mem2Reg.h"
35 using namespace polly
;
38 PollyInliner("polly-run-inliner",
39 cl::desc("Run an early inliner pass before Polly"), cl::Hidden
,
40 cl::cat(PollyCategory
));
42 void polly::registerCanonicalicationPasses(llvm::legacy::PassManagerBase
&PM
) {
43 bool UseMemSSA
= true;
44 PM
.add(llvm::createPromoteMemoryToRegisterPass());
45 PM
.add(llvm::createEarlyCSEPass(UseMemSSA
));
46 PM
.add(llvm::createInstructionCombiningPass());
47 PM
.add(llvm::createCFGSimplificationPass());
48 PM
.add(llvm::createTailCallEliminationPass());
49 PM
.add(llvm::createCFGSimplificationPass());
50 PM
.add(llvm::createReassociatePass());
51 PM
.add(llvm::createLoopRotatePass());
53 PM
.add(llvm::createPromoteMemoryToRegisterPass());
54 PM
.add(llvm::createCFGSimplificationPass());
55 PM
.add(llvm::createInstructionCombiningPass());
56 PM
.add(createBarrierNoopPass());
58 PM
.add(llvm::createInstructionCombiningPass());
61 /// Adapted from llvm::PassBuilder::buildInlinerPipeline
62 static ModuleInlinerWrapperPass
63 buildInlinePasses(llvm::OptimizationLevel Level
) {
64 InlineParams IP
= getInlineParams(200);
65 ModuleInlinerWrapperPass
MIWP(IP
);
67 // Require the GlobalsAA analysis for the module so we can query it within
68 // the CGSCC pipeline.
69 MIWP
.addModulePass(RequireAnalysisPass
<GlobalsAA
, Module
>());
70 // Invalidate AAManager so it can be recreated and pick up the newly available
73 createModuleToFunctionPassAdaptor(InvalidateAnalysisPass
<AAManager
>()));
75 // Require the ProfileSummaryAnalysis for the module so we can query it within
77 MIWP
.addModulePass(RequireAnalysisPass
<ProfileSummaryAnalysis
, Module
>());
79 // Now begin the main postorder CGSCC pipeline.
80 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
81 // manager and trying to emulate its precise behavior. Much of this doesn't
82 // make a lot of sense and we should revisit the core CGSCC structure.
83 CGSCCPassManager
&MainCGPipeline
= MIWP
.getPM();
85 // Now deduce any function attributes based in the current code.
86 MainCGPipeline
.addPass(PostOrderFunctionAttrsPass());
92 polly::buildCanonicalicationPassesForNPM(llvm::ModulePassManager
&MPM
,
93 llvm::OptimizationLevel Level
) {
94 FunctionPassManager FPM
;
96 bool UseMemSSA
= true;
97 FPM
.addPass(PromotePass());
98 FPM
.addPass(EarlyCSEPass(UseMemSSA
));
99 FPM
.addPass(InstCombinePass());
100 FPM
.addPass(SimplifyCFGPass());
101 FPM
.addPass(TailCallElimPass());
102 FPM
.addPass(SimplifyCFGPass());
103 FPM
.addPass(ReassociatePass());
106 LPM
.addPass(LoopRotatePass(Level
!= OptimizationLevel::Oz
));
107 FPM
.addPass(createFunctionToLoopPassAdaptor
<LoopPassManager
>(
108 std::move(LPM
), /*UseMemorySSA=*/false,
109 /*UseBlockFrequencyInfo=*/false));
112 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
113 MPM
.addPass(buildInlinePasses(Level
));
114 FPM
= FunctionPassManager();
116 FPM
.addPass(PromotePass());
117 FPM
.addPass(SimplifyCFGPass());
118 FPM
.addPass(InstCombinePass());
120 FPM
.addPass(InstCombinePass());
123 LPM
.addPass(IndVarSimplifyPass());
124 FPM
.addPass(createFunctionToLoopPassAdaptor
<LoopPassManager
>(
125 std::move(LPM
), /*UseMemorySSA=*/false,
126 /*UseBlockFrequencyInfo=*/true));
133 class PollyCanonicalize final
: public ModulePass
{
134 PollyCanonicalize(const PollyCanonicalize
&) = delete;
135 const PollyCanonicalize
&operator=(const PollyCanonicalize
&) = delete;
140 explicit PollyCanonicalize() : ModulePass(ID
) {}
141 ~PollyCanonicalize();
143 /// @name FunctionPass interface.
145 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
146 void releaseMemory() override
;
147 bool runOnModule(Module
&M
) override
;
148 void print(raw_ostream
&OS
, const Module
*) const override
;
153 PollyCanonicalize::~PollyCanonicalize() {}
155 void PollyCanonicalize::getAnalysisUsage(AnalysisUsage
&AU
) const {}
157 void PollyCanonicalize::releaseMemory() {}
159 bool PollyCanonicalize::runOnModule(Module
&M
) {
160 legacy::PassManager PM
;
161 registerCanonicalicationPasses(PM
);
167 void PollyCanonicalize::print(raw_ostream
&OS
, const Module
*) const {}
169 char PollyCanonicalize::ID
= 0;
171 Pass
*polly::createPollyCanonicalizePass() { return new PollyCanonicalize(); }
173 INITIALIZE_PASS_BEGIN(PollyCanonicalize
, "polly-canonicalize",
174 "Polly - Run canonicalization passes", false, false)
175 INITIALIZE_PASS_END(PollyCanonicalize
, "polly-canonicalize",
176 "Polly - Run canonicalization passes", false, false)