1 //===- Parsing, selection, and construction of pass pipelines -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file provides the implementation of the PassBuilder based on our
12 /// static pass registry as well as related functionality. It also provides
13 /// helpers to aid in analyzing, debugging, and testing passes and pass
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Passes/PassBuilder.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
22 #include "llvm/Analysis/AssumptionCache.h"
23 #include "llvm/Analysis/BasicAliasAnalysis.h"
24 #include "llvm/Analysis/BlockFrequencyInfo.h"
25 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
26 #include "llvm/Analysis/BranchProbabilityInfo.h"
27 #include "llvm/Analysis/CFGPrinter.h"
28 #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
29 #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
30 #include "llvm/Analysis/CGSCCPassManager.h"
31 #include "llvm/Analysis/CallGraph.h"
32 #include "llvm/Analysis/DemandedBits.h"
33 #include "llvm/Analysis/DependenceAnalysis.h"
34 #include "llvm/Analysis/DominanceFrontier.h"
35 #include "llvm/Analysis/GlobalsModRef.h"
36 #include "llvm/Analysis/IVUsers.h"
37 #include "llvm/Analysis/LazyCallGraph.h"
38 #include "llvm/Analysis/LazyValueInfo.h"
39 #include "llvm/Analysis/LoopAccessAnalysis.h"
40 #include "llvm/Analysis/LoopInfo.h"
41 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
42 #include "llvm/Analysis/MemorySSA.h"
43 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
44 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
45 #include "llvm/Analysis/PostDominators.h"
46 #include "llvm/Analysis/ProfileSummaryInfo.h"
47 #include "llvm/Analysis/RegionInfo.h"
48 #include "llvm/Analysis/ScalarEvolution.h"
49 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
50 #include "llvm/Analysis/ScopedNoAliasAA.h"
51 #include "llvm/Analysis/TargetLibraryInfo.h"
52 #include "llvm/Analysis/TargetTransformInfo.h"
53 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
54 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
55 #include "llvm/CodeGen/UnreachableBlockElim.h"
56 #include "llvm/IR/Dominators.h"
57 #include "llvm/IR/IRPrintingPasses.h"
58 #include "llvm/IR/PassManager.h"
59 #include "llvm/IR/Verifier.h"
60 #include "llvm/Support/Debug.h"
61 #include "llvm/Support/Regex.h"
62 #include "llvm/Target/TargetMachine.h"
63 #include "llvm/Transforms/GCOVProfiler.h"
64 #include "llvm/Transforms/IPO/AlwaysInliner.h"
65 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
66 #include "llvm/Transforms/IPO/ConstantMerge.h"
67 #include "llvm/Transforms/IPO/CrossDSOCFI.h"
68 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
69 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
70 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
71 #include "llvm/Transforms/IPO/FunctionAttrs.h"
72 #include "llvm/Transforms/IPO/FunctionImport.h"
73 #include "llvm/Transforms/IPO/GlobalDCE.h"
74 #include "llvm/Transforms/IPO/GlobalOpt.h"
75 #include "llvm/Transforms/IPO/GlobalSplit.h"
76 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
77 #include "llvm/Transforms/IPO/Inliner.h"
78 #include "llvm/Transforms/IPO/Internalize.h"
79 #include "llvm/Transforms/IPO/LowerTypeTests.h"
80 #include "llvm/Transforms/IPO/PartialInlining.h"
81 #include "llvm/Transforms/IPO/SCCP.h"
82 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
83 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
84 #include "llvm/Transforms/InstCombine/InstCombine.h"
85 #include "llvm/Transforms/InstrProfiling.h"
86 #include "llvm/Transforms/PGOInstrumentation.h"
87 #include "llvm/Transforms/SampleProfile.h"
88 #include "llvm/Transforms/Scalar/ADCE.h"
89 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
90 #include "llvm/Transforms/Scalar/BDCE.h"
91 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
92 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
93 #include "llvm/Transforms/Scalar/DCE.h"
94 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
95 #include "llvm/Transforms/Scalar/DivRemPairs.h"
96 #include "llvm/Transforms/Scalar/EarlyCSE.h"
97 #include "llvm/Transforms/Scalar/Float2Int.h"
98 #include "llvm/Transforms/Scalar/GVN.h"
99 #include "llvm/Transforms/Scalar/GuardWidening.h"
100 #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
101 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
102 #include "llvm/Transforms/Scalar/JumpThreading.h"
103 #include "llvm/Transforms/Scalar/LICM.h"
104 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
105 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
106 #include "llvm/Transforms/Scalar/LoopDeletion.h"
107 #include "llvm/Transforms/Scalar/LoopDistribute.h"
108 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
109 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
110 #include "llvm/Transforms/Scalar/LoopLoadElimination.h"
111 #include "llvm/Transforms/Scalar/LoopPassManager.h"
112 #include "llvm/Transforms/Scalar/LoopPredication.h"
113 #include "llvm/Transforms/Scalar/LoopRotation.h"
114 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
115 #include "llvm/Transforms/Scalar/LoopSink.h"
116 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
117 #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
118 #include "llvm/Transforms/Scalar/LowerAtomic.h"
119 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
120 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
121 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
122 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
123 #include "llvm/Transforms/Scalar/NaryReassociate.h"
124 #include "llvm/Transforms/Scalar/NewGVN.h"
125 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
126 #include "llvm/Transforms/Scalar/Reassociate.h"
127 #include "llvm/Transforms/Scalar/SCCP.h"
128 #include "llvm/Transforms/Scalar/SROA.h"
129 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
130 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
131 #include "llvm/Transforms/Scalar/Sink.h"
132 #include "llvm/Transforms/Scalar/SpeculativeExecution.h"
133 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
134 #include "llvm/Transforms/Utils/AddDiscriminators.h"
135 #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
136 #include "llvm/Transforms/Utils/LCSSA.h"
137 #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
138 #include "llvm/Transforms/Utils/LoopSimplify.h"
139 #include "llvm/Transforms/Utils/LowerInvoke.h"
140 #include "llvm/Transforms/Utils/Mem2Reg.h"
141 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
142 #include "llvm/Transforms/Utils/PredicateInfo.h"
143 #include "llvm/Transforms/Utils/SimplifyInstructions.h"
144 #include "llvm/Transforms/Utils/SymbolRewriter.h"
145 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
146 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
148 #include <type_traits>
150 using namespace llvm
;
152 static cl::opt
<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
153 cl::ReallyHidden
, cl::init(4));
155 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
156 cl::Hidden
, cl::ZeroOrMore
,
157 cl::desc("Run Partial inlinining pass"));
160 RunNewGVN("enable-npm-newgvn", cl::init(false),
161 cl::Hidden
, cl::ZeroOrMore
,
162 cl::desc("Run NewGVN instead of GVN"));
164 static cl::opt
<bool> EnableEarlyCSEMemSSA(
165 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden
,
166 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
168 static cl::opt
<bool> EnableGVNHoist(
169 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden
,
170 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
172 static cl::opt
<bool> EnableGVNSink(
173 "enable-npm-gvn-sink", cl::init(false), cl::Hidden
,
174 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
176 static Regex
DefaultAliasRegex(
177 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
179 static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level
) {
181 case PassBuilder::O0
:
182 case PassBuilder::O1
:
183 case PassBuilder::O2
:
184 case PassBuilder::O3
:
187 case PassBuilder::Os
:
188 case PassBuilder::Oz
:
191 llvm_unreachable("Invalid optimization level!");
196 /// \brief No-op module pass which does nothing.
197 struct NoOpModulePass
{
198 PreservedAnalyses
run(Module
&M
, ModuleAnalysisManager
&) {
199 return PreservedAnalyses::all();
201 static StringRef
name() { return "NoOpModulePass"; }
204 /// \brief No-op module analysis.
205 class NoOpModuleAnalysis
: public AnalysisInfoMixin
<NoOpModuleAnalysis
> {
206 friend AnalysisInfoMixin
<NoOpModuleAnalysis
>;
207 static AnalysisKey Key
;
211 Result
run(Module
&, ModuleAnalysisManager
&) { return Result(); }
212 static StringRef
name() { return "NoOpModuleAnalysis"; }
215 /// \brief No-op CGSCC pass which does nothing.
216 struct NoOpCGSCCPass
{
217 PreservedAnalyses
run(LazyCallGraph::SCC
&C
, CGSCCAnalysisManager
&,
218 LazyCallGraph
&, CGSCCUpdateResult
&UR
) {
219 return PreservedAnalyses::all();
221 static StringRef
name() { return "NoOpCGSCCPass"; }
224 /// \brief No-op CGSCC analysis.
225 class NoOpCGSCCAnalysis
: public AnalysisInfoMixin
<NoOpCGSCCAnalysis
> {
226 friend AnalysisInfoMixin
<NoOpCGSCCAnalysis
>;
227 static AnalysisKey Key
;
231 Result
run(LazyCallGraph::SCC
&, CGSCCAnalysisManager
&, LazyCallGraph
&G
) {
234 static StringRef
name() { return "NoOpCGSCCAnalysis"; }
237 /// \brief No-op function pass which does nothing.
238 struct NoOpFunctionPass
{
239 PreservedAnalyses
run(Function
&F
, FunctionAnalysisManager
&) {
240 return PreservedAnalyses::all();
242 static StringRef
name() { return "NoOpFunctionPass"; }
245 /// \brief No-op function analysis.
246 class NoOpFunctionAnalysis
: public AnalysisInfoMixin
<NoOpFunctionAnalysis
> {
247 friend AnalysisInfoMixin
<NoOpFunctionAnalysis
>;
248 static AnalysisKey Key
;
252 Result
run(Function
&, FunctionAnalysisManager
&) { return Result(); }
253 static StringRef
name() { return "NoOpFunctionAnalysis"; }
256 /// \brief No-op loop pass which does nothing.
257 struct NoOpLoopPass
{
258 PreservedAnalyses
run(Loop
&L
, LoopAnalysisManager
&,
259 LoopStandardAnalysisResults
&, LPMUpdater
&) {
260 return PreservedAnalyses::all();
262 static StringRef
name() { return "NoOpLoopPass"; }
265 /// \brief No-op loop analysis.
266 class NoOpLoopAnalysis
: public AnalysisInfoMixin
<NoOpLoopAnalysis
> {
267 friend AnalysisInfoMixin
<NoOpLoopAnalysis
>;
268 static AnalysisKey Key
;
272 Result
run(Loop
&, LoopAnalysisManager
&, LoopStandardAnalysisResults
&) {
275 static StringRef
name() { return "NoOpLoopAnalysis"; }
278 AnalysisKey
NoOpModuleAnalysis::Key
;
279 AnalysisKey
NoOpCGSCCAnalysis::Key
;
280 AnalysisKey
NoOpFunctionAnalysis::Key
;
281 AnalysisKey
NoOpLoopAnalysis::Key
;
283 } // End anonymous namespace.
285 void PassBuilder::invokePeepholeEPCallbacks(
286 FunctionPassManager
&FPM
, PassBuilder::OptimizationLevel Level
) {
287 for (auto &C
: PeepholeEPCallbacks
)
291 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager
&MAM
) {
292 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
293 MAM.registerPass([&] { return CREATE_PASS; });
294 #include "PassRegistry.def"
296 for (auto &C
: ModuleAnalysisRegistrationCallbacks
)
300 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager
&CGAM
) {
301 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
302 CGAM.registerPass([&] { return CREATE_PASS; });
303 #include "PassRegistry.def"
305 for (auto &C
: CGSCCAnalysisRegistrationCallbacks
)
309 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager
&FAM
) {
310 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
311 FAM.registerPass([&] { return CREATE_PASS; });
312 #include "PassRegistry.def"
314 for (auto &C
: FunctionAnalysisRegistrationCallbacks
)
318 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager
&LAM
) {
319 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
320 LAM.registerPass([&] { return CREATE_PASS; });
321 #include "PassRegistry.def"
323 for (auto &C
: LoopAnalysisRegistrationCallbacks
)
328 PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level
,
331 assert(Level
!= O0
&& "Must request optimizations!");
332 FunctionPassManager
FPM(DebugLogging
);
334 // Form SSA out of local memory accesses after breaking apart aggregates into
338 // Catch trivial redundancies
339 FPM
.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA
));
341 // Hoisting of scalars and load expressions.
343 FPM
.addPass(GVNHoistPass());
345 // Global value numbering based sinking.
347 FPM
.addPass(GVNSinkPass());
348 FPM
.addPass(SimplifyCFGPass());
351 // Speculative execution if the target has divergent branches; otherwise nop.
352 FPM
.addPass(SpeculativeExecutionPass());
354 // Optimize based on known information about branches, and cleanup afterward.
355 FPM
.addPass(JumpThreadingPass());
356 FPM
.addPass(CorrelatedValuePropagationPass());
357 FPM
.addPass(SimplifyCFGPass());
358 FPM
.addPass(InstCombinePass());
360 if (!isOptimizingForSize(Level
))
361 FPM
.addPass(LibCallsShrinkWrapPass());
363 invokePeepholeEPCallbacks(FPM
, Level
);
365 FPM
.addPass(TailCallElimPass());
366 FPM
.addPass(SimplifyCFGPass());
368 // Form canonically associated expression trees, and simplify the trees using
369 // basic mathematical properties. For example, this will form (nearly)
370 // minimal multiplication trees.
371 FPM
.addPass(ReassociatePass());
373 // Add the primary loop simplification pipeline.
374 // FIXME: Currently this is split into two loop pass pipelines because we run
375 // some function passes in between them. These can and should be replaced by
376 // loop pass equivalenst but those aren't ready yet. Specifically,
377 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
378 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
379 // the other we have is `LoopInstSimplify`.
380 LoopPassManager
LPM1(DebugLogging
), LPM2(DebugLogging
);
382 // Rotate Loop - disable header duplication at -Oz
383 LPM1
.addPass(LoopRotatePass(Level
!= Oz
));
384 LPM1
.addPass(LICMPass());
385 LPM1
.addPass(SimpleLoopUnswitchPass());
386 LPM2
.addPass(IndVarSimplifyPass());
387 LPM2
.addPass(LoopIdiomRecognizePass());
389 for (auto &C
: LateLoopOptimizationsEPCallbacks
)
392 LPM2
.addPass(LoopDeletionPass());
393 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
394 // because it changes IR to makes profile annotation in back compile
396 if (Phase
!= ThinLTOPhase::PreLink
||
397 !PGOOpt
|| PGOOpt
->SampleProfileFile
.empty())
398 LPM2
.addPass(LoopFullUnrollPass(Level
));
400 for (auto &C
: LoopOptimizerEndEPCallbacks
)
403 // We provide the opt remark emitter pass for LICM to use. We only need to do
404 // this once as it is immutable.
405 FPM
.addPass(RequireAnalysisPass
<OptimizationRemarkEmitterAnalysis
, Function
>());
406 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1
)));
407 FPM
.addPass(SimplifyCFGPass());
408 FPM
.addPass(InstCombinePass());
409 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2
)));
411 // Eliminate redundancies.
413 // These passes add substantial compile time so skip them at O1.
414 FPM
.addPass(MergedLoadStoreMotionPass());
416 FPM
.addPass(NewGVNPass());
421 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
422 FPM
.addPass(MemCpyOptPass());
424 // Sparse conditional constant propagation.
425 // FIXME: It isn't clear why we do this *after* loop passes rather than
427 FPM
.addPass(SCCPPass());
429 // Delete dead bit computations (instcombine runs after to fold away the dead
430 // computations, and then ADCE will run later to exploit any new DCE
431 // opportunities that creates).
432 FPM
.addPass(BDCEPass());
434 // Run instcombine after redundancy and dead bit elimination to exploit
435 // opportunities opened up by them.
436 FPM
.addPass(InstCombinePass());
437 invokePeepholeEPCallbacks(FPM
, Level
);
439 // Re-consider control flow based optimizations after redundancy elimination,
441 FPM
.addPass(JumpThreadingPass());
442 FPM
.addPass(CorrelatedValuePropagationPass());
443 FPM
.addPass(DSEPass());
444 FPM
.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
446 for (auto &C
: ScalarOptimizerLateEPCallbacks
)
449 // Finally, do an expensive DCE pass to catch all the dead code exposed by
450 // the simplifications and basic cleanup after all the simplifications.
451 FPM
.addPass(ADCEPass());
452 FPM
.addPass(SimplifyCFGPass());
453 FPM
.addPass(InstCombinePass());
454 invokePeepholeEPCallbacks(FPM
, Level
);
459 void PassBuilder::addPGOInstrPasses(ModulePassManager
&MPM
, bool DebugLogging
,
460 PassBuilder::OptimizationLevel Level
,
462 std::string ProfileGenFile
,
463 std::string ProfileUseFile
) {
464 // Generally running simplification passes and the inliner with an high
465 // threshold results in smaller executables, but there may be cases where
466 // the size grows, so let's be conservative here and skip this simplification
468 if (!isOptimizingForSize(Level
)) {
471 // In the old pass manager, this is a cl::opt. Should still this be one?
472 IP
.DefaultThreshold
= 75;
474 // FIXME: The hint threshold has the same value used by the regular inliner.
475 // This should probably be lowered after performance testing.
476 // FIXME: this comment is cargo culted from the old pass manager, revisit).
477 IP
.HintThreshold
= 325;
479 CGSCCPassManager
CGPipeline(DebugLogging
);
481 CGPipeline
.addPass(InlinerPass(IP
));
483 FunctionPassManager FPM
;
485 FPM
.addPass(EarlyCSEPass()); // Catch trivial redundancies.
486 FPM
.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
487 FPM
.addPass(InstCombinePass()); // Combine silly sequences.
488 invokePeepholeEPCallbacks(FPM
, Level
);
490 CGPipeline
.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM
)));
492 MPM
.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline
)));
495 // Delete anything that is now dead to make sure that we don't instrument
496 // dead code. Instrumentation can end up keeping dead code around and
497 // dramatically increase code size.
498 MPM
.addPass(GlobalDCEPass());
501 MPM
.addPass(PGOInstrumentationGen());
503 FunctionPassManager FPM
;
504 FPM
.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
505 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
507 // Add the profile lowering pass.
508 InstrProfOptions Options
;
509 if (!ProfileGenFile
.empty())
510 Options
.InstrProfileOutput
= ProfileGenFile
;
511 Options
.DoCounterPromotion
= true;
512 MPM
.addPass(InstrProfiling(Options
));
515 if (!ProfileUseFile
.empty())
516 MPM
.addPass(PGOInstrumentationUse(ProfileUseFile
));
520 getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level
) {
521 auto O3
= PassBuilder::O3
;
522 unsigned OptLevel
= Level
> O3
? 2 : Level
;
523 unsigned SizeLevel
= Level
> O3
? Level
- O3
: 0;
524 return getInlineParams(OptLevel
, SizeLevel
);
528 PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level
,
531 ModulePassManager
MPM(DebugLogging
);
533 // Do basic inference of function attributes from known properties of system
534 // libraries and other oracles.
535 MPM
.addPass(InferFunctionAttrsPass());
537 // Create an early function pass manager to cleanup the output of the
539 FunctionPassManager
EarlyFPM(DebugLogging
);
540 EarlyFPM
.addPass(SimplifyCFGPass());
541 EarlyFPM
.addPass(SROA());
542 EarlyFPM
.addPass(EarlyCSEPass());
543 EarlyFPM
.addPass(LowerExpectIntrinsicPass());
544 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
545 // to convert bitcast to direct calls so that they can be inlined during the
546 // profile annotation prepration step.
547 // More details about SamplePGO design can be found in:
548 // https://research.google.com/pubs/pub45290.html
549 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
550 if (PGOOpt
&& !PGOOpt
->SampleProfileFile
.empty() &&
551 Phase
== ThinLTOPhase::PostLink
)
552 EarlyFPM
.addPass(InstCombinePass());
553 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM
)));
555 if (PGOOpt
&& !PGOOpt
->SampleProfileFile
.empty()) {
556 // Annotate sample profile right after early FPM to ensure freshness of
558 MPM
.addPass(SampleProfileLoaderPass(PGOOpt
->SampleProfileFile
));
559 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
560 // for the profile annotation to be accurate in the ThinLTO backend.
561 if (Phase
!= ThinLTOPhase::PreLink
)
562 // We perform early indirect call promotion here, before globalopt.
563 // This is important for the ThinLTO backend phase because otherwise
564 // imported available_externally functions look unreferenced and are
566 MPM
.addPass(PGOIndirectCallPromotion(Phase
== ThinLTOPhase::PostLink
,
570 // Interprocedural constant propagation now that basic cleanup has occured
571 // and prior to optimizing globals.
572 // FIXME: This position in the pipeline hasn't been carefully considered in
573 // years, it should be re-analyzed.
574 MPM
.addPass(IPSCCPPass());
576 // Optimize globals to try and fold them into constants.
577 MPM
.addPass(GlobalOptPass());
579 // Promote any localized globals to SSA registers.
580 // FIXME: Should this instead by a run of SROA?
581 // FIXME: We should probably run instcombine and simplify-cfg afterward to
582 // delete control flows that are dead once globals have been folded to
584 MPM
.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
586 // Remove any dead arguments exposed by cleanups and constand folding
588 MPM
.addPass(DeadArgumentEliminationPass());
590 // Create a small function pass pipeline to cleanup after all the global
592 FunctionPassManager
GlobalCleanupPM(DebugLogging
);
593 GlobalCleanupPM
.addPass(InstCombinePass());
594 invokePeepholeEPCallbacks(GlobalCleanupPM
, Level
);
596 GlobalCleanupPM
.addPass(SimplifyCFGPass());
597 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM
)));
599 // Add all the requested passes for instrumentation PGO, if requested.
600 if (PGOOpt
&& Phase
!= ThinLTOPhase::PostLink
&&
601 (!PGOOpt
->ProfileGenFile
.empty() || !PGOOpt
->ProfileUseFile
.empty())) {
602 addPGOInstrPasses(MPM
, DebugLogging
, Level
, PGOOpt
->RunProfileGen
,
603 PGOOpt
->ProfileGenFile
, PGOOpt
->ProfileUseFile
);
604 MPM
.addPass(PGOIndirectCallPromotion(false, false));
607 // Require the GlobalsAA analysis for the module so we can query it within
608 // the CGSCC pipeline.
609 MPM
.addPass(RequireAnalysisPass
<GlobalsAA
, Module
>());
611 // Require the ProfileSummaryAnalysis for the module so we can query it within
613 MPM
.addPass(RequireAnalysisPass
<ProfileSummaryAnalysis
, Module
>());
615 // Now begin the main postorder CGSCC pipeline.
616 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
617 // manager and trying to emulate its precise behavior. Much of this doesn't
618 // make a lot of sense and we should revisit the core CGSCC structure.
619 CGSCCPassManager
MainCGPipeline(DebugLogging
);
621 // Note: historically, the PruneEH pass was run first to deduce nounwind and
622 // generally clean up exception handling overhead. It isn't clear this is
623 // valuable as the inliner doesn't currently care whether it is inlining an
626 // Run the inliner first. The theory is that we are walking bottom-up and so
627 // the callees have already been fully optimized, and we want to inline them
628 // into the callers so that our optimizations can reflect that.
629 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
630 // because it makes profile annotation in the backend inaccurate.
631 InlineParams IP
= getInlineParamsFromOptLevel(Level
);
632 if (Phase
== ThinLTOPhase::PreLink
&&
633 PGOOpt
&& !PGOOpt
->SampleProfileFile
.empty())
634 IP
.HotCallSiteThreshold
= 0;
635 MainCGPipeline
.addPass(InlinerPass(IP
));
637 // Now deduce any function attributes based in the current code.
638 MainCGPipeline
.addPass(PostOrderFunctionAttrsPass());
640 // When at O3 add argument promotion to the pass pipeline.
641 // FIXME: It isn't at all clear why this should be limited to O3.
643 MainCGPipeline
.addPass(ArgumentPromotionPass());
645 // Lastly, add the core function simplification pipeline nested inside the
647 MainCGPipeline
.addPass(createCGSCCToFunctionPassAdaptor(
648 buildFunctionSimplificationPipeline(Level
, Phase
, DebugLogging
)));
650 for (auto &C
: CGSCCOptimizerLateEPCallbacks
)
651 C(MainCGPipeline
, Level
);
653 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
654 // to detect when we devirtualize indirect calls and iterate the SCC passes
655 // in that case to try and catch knock-on inlining or function attrs
656 // opportunities. Then we add it to the module pipeline by walking the SCCs
657 // in postorder (or bottom-up).
659 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
660 std::move(MainCGPipeline
), MaxDevirtIterations
)));
666 PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level
,
668 ModulePassManager
MPM(DebugLogging
);
670 // Optimize globals now that the module is fully simplified.
671 MPM
.addPass(GlobalOptPass());
673 // Run partial inlining pass to partially inline functions that have
675 if (RunPartialInlining
)
676 MPM
.addPass(PartialInlinerPass());
678 // Remove avail extern fns and globals definitions since we aren't compiling
679 // an object file for later LTO. For LTO we want to preserve these so they
680 // are eligible for inlining at link-time. Note if they are unreferenced they
681 // will be removed by GlobalDCE later, so this only impacts referenced
682 // available externally globals. Eventually they will be suppressed during
683 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
684 // may make globals referenced by available external functions dead and saves
685 // running remaining passes on the eliminated functions.
686 MPM
.addPass(EliminateAvailableExternallyPass());
688 // Do RPO function attribute inference across the module to forward-propagate
689 // attributes where applicable.
690 // FIXME: Is this really an optimization rather than a canonicalization?
691 MPM
.addPass(ReversePostOrderFunctionAttrsPass());
693 // Re-require GloblasAA here prior to function passes. This is particularly
694 // useful as the above will have inlined, DCE'ed, and function-attr
695 // propagated everything. We should at this point have a reasonably minimal
696 // and richly annotated call graph. By computing aliasing and mod/ref
697 // information for all local globals here, the late loop passes and notably
698 // the vectorizer will be able to use them to help recognize vectorizable
699 // memory operations.
700 MPM
.addPass(RequireAnalysisPass
<GlobalsAA
, Module
>());
702 FunctionPassManager
OptimizePM(DebugLogging
);
703 OptimizePM
.addPass(Float2IntPass());
704 // FIXME: We need to run some loop optimizations to re-rotate loops after
705 // simplify-cfg and others undo their rotation.
707 // Optimize the loop execution. These passes operate on entire loop nests
708 // rather than on each loop in an inside-out manner, and so they are actually
711 for (auto &C
: VectorizerStartEPCallbacks
)
712 C(OptimizePM
, Level
);
714 // First rotate loops that may have been un-rotated by prior passes.
715 OptimizePM
.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
717 // Distribute loops to allow partial vectorization. I.e. isolate dependences
718 // into separate loop that would otherwise inhibit vectorization. This is
719 // currently only performed for loops marked with the metadata
720 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
721 OptimizePM
.addPass(LoopDistributePass());
723 // Now run the core loop vectorizer.
724 OptimizePM
.addPass(LoopVectorizePass());
726 // Eliminate loads by forwarding stores from the previous iteration to loads
727 // of the current iteration.
728 OptimizePM
.addPass(LoopLoadEliminationPass());
730 // Cleanup after the loop optimization passes.
731 OptimizePM
.addPass(InstCombinePass());
734 // Now that we've formed fast to execute loop structures, we do further
735 // optimizations. These are run afterward as they might block doing complex
736 // analyses and transforms such as what are needed for loop vectorization.
738 // Optimize parallel scalar instruction chains into SIMD instructions.
739 OptimizePM
.addPass(SLPVectorizerPass());
741 // Cleanup after all of the vectorizers.
742 OptimizePM
.addPass(SimplifyCFGPass());
743 OptimizePM
.addPass(InstCombinePass());
745 // Unroll small loops to hide loop backedge latency and saturate any parallel
746 // execution resources of an out-of-order processor. We also then need to
747 // clean up redundancies and loop invariant code.
748 // FIXME: It would be really good to use a loop-integrated instruction
749 // combiner for cleanup here so that the unrolling and LICM can be pipelined
750 // across the loop nests.
751 OptimizePM
.addPass(LoopUnrollPass(Level
));
752 OptimizePM
.addPass(InstCombinePass());
753 OptimizePM
.addPass(RequireAnalysisPass
<OptimizationRemarkEmitterAnalysis
, Function
>());
754 OptimizePM
.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
756 // Now that we've vectorized and unrolled loops, we may have more refined
757 // alignment information, try to re-derive it here.
758 OptimizePM
.addPass(AlignmentFromAssumptionsPass());
760 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
761 // canonicalization pass that enables other optimizations. As a result,
762 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
764 OptimizePM
.addPass(LoopSinkPass());
766 // And finally clean up LCSSA form before generating code.
767 OptimizePM
.addPass(InstSimplifierPass());
769 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
770 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
771 // flattening of blocks.
772 OptimizePM
.addPass(DivRemPairsPass());
774 // LoopSink (and other loop passes since the last simplifyCFG) might have
775 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
776 OptimizePM
.addPass(SimplifyCFGPass());
778 // Add the core optimizing pipeline.
779 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM
)));
781 // Now we need to do some global optimization transforms.
782 // FIXME: It would seem like these should come first in the optimization
783 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
785 MPM
.addPass(GlobalDCEPass());
786 MPM
.addPass(ConstantMergePass());
792 PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level
,
794 assert(Level
!= O0
&& "Must request optimizations for the default pipeline!");
796 ModulePassManager
MPM(DebugLogging
);
798 // Force any function attributes we want the rest of the pipeline to observe.
799 MPM
.addPass(ForceFunctionAttrsPass());
801 if (PGOOpt
&& PGOOpt
->SamplePGOSupport
)
802 MPM
.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
804 // Add the core simplification pipeline.
805 MPM
.addPass(buildModuleSimplificationPipeline(Level
, ThinLTOPhase::None
,
808 // Now add the optimization pipeline.
809 MPM
.addPass(buildModuleOptimizationPipeline(Level
, DebugLogging
));
815 PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level
,
817 assert(Level
!= O0
&& "Must request optimizations for the default pipeline!");
819 ModulePassManager
MPM(DebugLogging
);
821 // Force any function attributes we want the rest of the pipeline to observe.
822 MPM
.addPass(ForceFunctionAttrsPass());
824 if (PGOOpt
&& PGOOpt
->SamplePGOSupport
)
825 MPM
.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
827 // If we are planning to perform ThinLTO later, we don't bloat the code with
828 // unrolling/vectorization/... now. Just simplify the module as much as we
830 MPM
.addPass(buildModuleSimplificationPipeline(Level
, ThinLTOPhase::PreLink
,
833 // Run partial inlining pass to partially inline functions that have
835 // FIXME: It isn't clear whether this is really the right place to run this
836 // in ThinLTO. Because there is another canonicalization and simplification
837 // phase that will run after the thin link, running this here ends up with
838 // less information than will be available later and it may grow functions in
839 // ways that aren't beneficial.
840 if (RunPartialInlining
)
841 MPM
.addPass(PartialInlinerPass());
843 // Reduce the size of the IR as much as possible.
844 MPM
.addPass(GlobalOptPass());
850 PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level
,
852 // FIXME: The summary index is not hooked in the new pass manager yet.
853 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
856 ModulePassManager
MPM(DebugLogging
);
858 // Force any function attributes we want the rest of the pipeline to observe.
859 MPM
.addPass(ForceFunctionAttrsPass());
861 // During the ThinLTO backend phase we perform early indirect call promotion
862 // here, before globalopt. Otherwise imported available_externally functions
863 // look unreferenced and are removed.
864 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
866 if (!PGOOpt
|| PGOOpt
->SampleProfileFile
.empty())
867 MPM
.addPass(PGOIndirectCallPromotion(true /* InLTO */,
868 false /* SamplePGO */));
870 // Add the core simplification pipeline.
871 MPM
.addPass(buildModuleSimplificationPipeline(Level
, ThinLTOPhase::PostLink
,
874 // Now add the optimization pipeline.
875 MPM
.addPass(buildModuleOptimizationPipeline(Level
, DebugLogging
));
881 PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level
,
883 assert(Level
!= O0
&& "Must request optimizations for the default pipeline!");
884 // FIXME: We should use a customized pre-link pipeline!
885 return buildPerModuleDefaultPipeline(Level
, DebugLogging
);
888 ModulePassManager
PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level
,
890 assert(Level
!= O0
&& "Must request optimizations for the default pipeline!");
891 ModulePassManager
MPM(DebugLogging
);
893 // Remove unused virtual tables to improve the quality of code generated by
894 // whole-program devirtualization and bitset lowering.
895 MPM
.addPass(GlobalDCEPass());
897 // Force any function attributes we want the rest of the pipeline to observe.
898 MPM
.addPass(ForceFunctionAttrsPass());
900 // Do basic inference of function attributes from known properties of system
901 // libraries and other oracles.
902 MPM
.addPass(InferFunctionAttrsPass());
905 // Indirect call promotion. This should promote all the targets that are
906 // left by the earlier promotion pass that promotes intra-module targets.
907 // This two-step promotion is to save the compile time. For LTO, it should
908 // produce the same result as if we only do promotion here.
909 MPM
.addPass(PGOIndirectCallPromotion(
910 true /* InLTO */, PGOOpt
&& !PGOOpt
->SampleProfileFile
.empty()));
912 // Propagate constants at call sites into the functions they call. This
913 // opens opportunities for globalopt (and inlining) by substituting function
914 // pointers passed as arguments to direct uses of functions.
915 MPM
.addPass(IPSCCPPass());
918 // Now deduce any function attributes based in the current code.
919 MPM
.addPass(createModuleToPostOrderCGSCCPassAdaptor(
920 PostOrderFunctionAttrsPass()));
922 // Do RPO function attribute inference across the module to forward-propagate
923 // attributes where applicable.
924 // FIXME: Is this really an optimization rather than a canonicalization?
925 MPM
.addPass(ReversePostOrderFunctionAttrsPass());
927 // Use inragne annotations on GEP indices to split globals where beneficial.
928 MPM
.addPass(GlobalSplitPass());
930 // Run whole program optimization of virtual call when the list of callees
932 MPM
.addPass(WholeProgramDevirtPass());
938 // Optimize globals to try and fold them into constants.
939 MPM
.addPass(GlobalOptPass());
941 // Promote any localized globals to SSA registers.
942 MPM
.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
944 // Linking modules together can lead to duplicate global constant, only
945 // keep one copy of each constant.
946 MPM
.addPass(ConstantMergePass());
948 // Remove unused arguments from functions.
949 MPM
.addPass(DeadArgumentEliminationPass());
951 // Reduce the code after globalopt and ipsccp. Both can open up significant
952 // simplification opportunities, and both can propagate functions through
953 // function pointers. When this happens, we often have to resolve varargs
954 // calls, etc, so let instcombine do this.
955 FunctionPassManager
PeepholeFPM(DebugLogging
);
956 PeepholeFPM
.addPass(InstCombinePass());
957 invokePeepholeEPCallbacks(PeepholeFPM
, Level
);
959 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM
)));
961 // Note: historically, the PruneEH pass was run first to deduce nounwind and
962 // generally clean up exception handling overhead. It isn't clear this is
963 // valuable as the inliner doesn't currently care whether it is inlining an
965 // Run the inliner now.
966 MPM
.addPass(createModuleToPostOrderCGSCCPassAdaptor(
967 InlinerPass(getInlineParamsFromOptLevel(Level
))));
969 // Optimize globals again after we ran the inliner.
970 MPM
.addPass(GlobalOptPass());
972 // Garbage collect dead functions.
973 // FIXME: Add ArgumentPromotion pass after once it's ported.
974 MPM
.addPass(GlobalDCEPass());
976 FunctionPassManager
FPM(DebugLogging
);
977 // The IPO Passes may leave cruft around. Clean up after them.
978 FPM
.addPass(InstCombinePass());
979 invokePeepholeEPCallbacks(FPM
, Level
);
981 FPM
.addPass(JumpThreadingPass());
986 // Run a few AA driver optimizations here and now to cleanup the code.
987 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
989 MPM
.addPass(createModuleToPostOrderCGSCCPassAdaptor(
990 PostOrderFunctionAttrsPass()));
991 // FIXME: here we run IP alias analysis in the legacy PM.
993 FunctionPassManager MainFPM
;
995 // FIXME: once we fix LoopPass Manager, add LICM here.
996 // FIXME: once we provide support for enabling MLSM, add it here.
997 // FIXME: once we provide support for enabling NewGVN, add it here.
999 MainFPM
.addPass(NewGVNPass());
1001 MainFPM
.addPass(GVN());
1003 // Remove dead memcpy()'s.
1004 MainFPM
.addPass(MemCpyOptPass());
1006 // Nuke dead stores.
1007 MainFPM
.addPass(DSEPass());
1009 // FIXME: at this point, we run a bunch of loop passes:
1010 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1011 // loopVectorize. Enable them once the remaining issue with LPM
1014 MainFPM
.addPass(InstCombinePass());
1015 MainFPM
.addPass(SimplifyCFGPass());
1016 MainFPM
.addPass(SCCPPass());
1017 MainFPM
.addPass(InstCombinePass());
1018 MainFPM
.addPass(BDCEPass());
1020 // FIXME: We may want to run SLPVectorizer here.
1021 // After vectorization, assume intrinsics may tell us more
1022 // about pointer alignments.
1024 MainFPM
.add(AlignmentFromAssumptionsPass());
1027 // FIXME: Conditionally run LoadCombine here, after it's ported
1028 // (in case we still have this pass, given its questionable usefulness).
1030 MainFPM
.addPass(InstCombinePass());
1031 invokePeepholeEPCallbacks(MainFPM
, Level
);
1032 MainFPM
.addPass(JumpThreadingPass());
1033 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM
)));
1035 // Create a function that performs CFI checks for cross-DSO calls with
1036 // targets in the current module.
1037 MPM
.addPass(CrossDSOCFIPass());
1039 // Lower type metadata and the type.test intrinsic. This pass supports
1040 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1041 // to be run at link time if CFI is enabled. This pass does nothing if
1043 // Enable once we add support for the summary in the new PM.
1045 MPM
.addPass(LowerTypeTestsPass(Summary
? PassSummaryAction::Export
:
1046 PassSummaryAction::None
,
1050 // Add late LTO optimization passes.
1051 // Delete basic blocks, which optimization passes may have killed.
1052 MPM
.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1054 // Drop bodies of available eternally objects to improve GlobalDCE.
1055 MPM
.addPass(EliminateAvailableExternallyPass());
1057 // Now that we have optimized the program, discard unreachable functions.
1058 MPM
.addPass(GlobalDCEPass());
1060 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
1064 AAManager
PassBuilder::buildDefaultAAPipeline() {
1067 // The order in which these are registered determines their priority when
1070 // First we register the basic alias analysis that provides the majority of
1071 // per-function local AA logic. This is a stateless, on-demand local set of
1073 AA
.registerFunctionAnalysis
<BasicAA
>();
1075 // Next we query fast, specialized alias analyses that wrap IR-embedded
1076 // information about aliasing.
1077 AA
.registerFunctionAnalysis
<ScopedNoAliasAA
>();
1078 AA
.registerFunctionAnalysis
<TypeBasedAA
>();
1080 // Add support for querying global aliasing information when available.
1081 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1082 // analysis, all that the `AAManager` can do is query for any *cached*
1083 // results from `GlobalsAA` through a readonly proxy.
1084 AA
.registerModuleAnalysis
<GlobalsAA
>();
1089 static Optional
<int> parseRepeatPassName(StringRef Name
) {
1090 if (!Name
.consume_front("repeat<") || !Name
.consume_back(">"))
1093 if (Name
.getAsInteger(0, Count
) || Count
<= 0)
1098 static Optional
<int> parseDevirtPassName(StringRef Name
) {
1099 if (!Name
.consume_front("devirt<") || !Name
.consume_back(">"))
1102 if (Name
.getAsInteger(0, Count
) || Count
<= 0)
1107 /// Tests whether a pass name starts with a valid prefix for a default pipeline
1109 static bool startsWithDefaultPipelineAliasPrefix(StringRef Name
) {
1110 return Name
.startswith("default") || Name
.startswith("thinlto") ||
1111 Name
.startswith("lto");
1114 /// Tests whether registered callbacks will accept a given pass name.
1116 /// When parsing a pipeline text, the type of the outermost pipeline may be
1117 /// omitted, in which case the type is automatically determined from the first
1118 /// pass name in the text. This may be a name that is handled through one of the
1119 /// callbacks. We check this through the oridinary parsing callbacks by setting
1120 /// up a dummy PassManager in order to not force the client to also handle this
1122 template <typename PassManagerT
, typename CallbacksT
>
1123 static bool callbacksAcceptPassName(StringRef Name
, CallbacksT
&Callbacks
) {
1124 if (!Callbacks
.empty()) {
1125 PassManagerT DummyPM
;
1126 for (auto &CB
: Callbacks
)
1127 if (CB(Name
, DummyPM
, {}))
1133 template <typename CallbacksT
>
1134 static bool isModulePassName(StringRef Name
, CallbacksT
&Callbacks
) {
1135 // Manually handle aliases for pre-configured pipeline fragments.
1136 if (startsWithDefaultPipelineAliasPrefix(Name
))
1137 return DefaultAliasRegex
.match(Name
);
1139 // Explicitly handle pass manager names.
1140 if (Name
== "module")
1142 if (Name
== "cgscc")
1144 if (Name
== "function")
1147 // Explicitly handle custom-parsed pass names.
1148 if (parseRepeatPassName(Name
))
1151 #define MODULE_PASS(NAME, CREATE_PASS) \
1154 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1155 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1157 #include "PassRegistry.def"
1159 return callbacksAcceptPassName
<ModulePassManager
>(Name
, Callbacks
);
1162 template <typename CallbacksT
>
1163 static bool isCGSCCPassName(StringRef Name
, CallbacksT
&Callbacks
) {
1164 // Explicitly handle pass manager names.
1165 if (Name
== "cgscc")
1167 if (Name
== "function")
1170 // Explicitly handle custom-parsed pass names.
1171 if (parseRepeatPassName(Name
))
1173 if (parseDevirtPassName(Name
))
1176 #define CGSCC_PASS(NAME, CREATE_PASS) \
1179 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1180 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1182 #include "PassRegistry.def"
1184 return callbacksAcceptPassName
<CGSCCPassManager
>(Name
, Callbacks
);
1187 template <typename CallbacksT
>
1188 static bool isFunctionPassName(StringRef Name
, CallbacksT
&Callbacks
) {
1189 // Explicitly handle pass manager names.
1190 if (Name
== "function")
1195 // Explicitly handle custom-parsed pass names.
1196 if (parseRepeatPassName(Name
))
1199 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1202 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1203 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1205 #include "PassRegistry.def"
1207 return callbacksAcceptPassName
<FunctionPassManager
>(Name
, Callbacks
);
1210 template <typename CallbacksT
>
1211 static bool isLoopPassName(StringRef Name
, CallbacksT
&Callbacks
) {
1212 // Explicitly handle pass manager names.
1216 // Explicitly handle custom-parsed pass names.
1217 if (parseRepeatPassName(Name
))
1220 #define LOOP_PASS(NAME, CREATE_PASS) \
1223 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1224 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1226 #include "PassRegistry.def"
1228 return callbacksAcceptPassName
<LoopPassManager
>(Name
, Callbacks
);
1231 Optional
<std::vector
<PassBuilder::PipelineElement
>>
1232 PassBuilder::parsePipelineText(StringRef Text
) {
1233 std::vector
<PipelineElement
> ResultPipeline
;
1235 SmallVector
<std::vector
<PipelineElement
> *, 4> PipelineStack
= {
1238 std::vector
<PipelineElement
> &Pipeline
= *PipelineStack
.back();
1239 size_t Pos
= Text
.find_first_of(",()");
1240 Pipeline
.push_back({Text
.substr(0, Pos
), {}});
1242 // If we have a single terminating name, we're done.
1243 if (Pos
== Text
.npos
)
1246 char Sep
= Text
[Pos
];
1247 Text
= Text
.substr(Pos
+ 1);
1249 // Just a name ending in a comma, continue.
1253 // Push the inner pipeline onto the stack to continue processing.
1254 PipelineStack
.push_back(&Pipeline
.back().InnerPipeline
);
1258 assert(Sep
== ')' && "Bogus separator!");
1259 // When handling the close parenthesis, we greedily consume them to avoid
1260 // empty strings in the pipeline.
1262 // If we try to pop the outer pipeline we have unbalanced parentheses.
1263 if (PipelineStack
.size() == 1)
1266 PipelineStack
.pop_back();
1267 } while (Text
.consume_front(")"));
1269 // Check if we've finished parsing.
1273 // Otherwise, the end of an inner pipeline always has to be followed by
1274 // a comma, and then we can continue.
1275 if (!Text
.consume_front(","))
1279 if (PipelineStack
.size() > 1)
1280 // Unbalanced paretheses.
1283 assert(PipelineStack
.back() == &ResultPipeline
&&
1284 "Wrong pipeline at the bottom of the stack!");
1285 return {std::move(ResultPipeline
)};
1288 bool PassBuilder::parseModulePass(ModulePassManager
&MPM
,
1289 const PipelineElement
&E
, bool VerifyEachPass
,
1290 bool DebugLogging
) {
1291 auto &Name
= E
.Name
;
1292 auto &InnerPipeline
= E
.InnerPipeline
;
1294 // First handle complex passes like the pass managers which carry pipelines.
1295 if (!InnerPipeline
.empty()) {
1296 if (Name
== "module") {
1297 ModulePassManager
NestedMPM(DebugLogging
);
1298 if (!parseModulePassPipeline(NestedMPM
, InnerPipeline
, VerifyEachPass
,
1301 MPM
.addPass(std::move(NestedMPM
));
1304 if (Name
== "cgscc") {
1305 CGSCCPassManager
CGPM(DebugLogging
);
1306 if (!parseCGSCCPassPipeline(CGPM
, InnerPipeline
, VerifyEachPass
,
1309 MPM
.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM
)));
1312 if (Name
== "function") {
1313 FunctionPassManager
FPM(DebugLogging
);
1314 if (!parseFunctionPassPipeline(FPM
, InnerPipeline
, VerifyEachPass
,
1317 MPM
.addPass(createModuleToFunctionPassAdaptor(std::move(FPM
)));
1320 if (auto Count
= parseRepeatPassName(Name
)) {
1321 ModulePassManager
NestedMPM(DebugLogging
);
1322 if (!parseModulePassPipeline(NestedMPM
, InnerPipeline
, VerifyEachPass
,
1325 MPM
.addPass(createRepeatedPass(*Count
, std::move(NestedMPM
)));
1329 for (auto &C
: ModulePipelineParsingCallbacks
)
1330 if (C(Name
, MPM
, InnerPipeline
))
1333 // Normal passes can't have pipelines.
1337 // Manually handle aliases for pre-configured pipeline fragments.
1338 if (startsWithDefaultPipelineAliasPrefix(Name
)) {
1339 SmallVector
<StringRef
, 3> Matches
;
1340 if (!DefaultAliasRegex
.match(Name
, &Matches
))
1342 assert(Matches
.size() == 3 && "Must capture two matched strings!");
1344 OptimizationLevel L
= StringSwitch
<OptimizationLevel
>(Matches
[2])
1352 // At O0 we do nothing at all!
1355 if (Matches
[1] == "default") {
1356 MPM
.addPass(buildPerModuleDefaultPipeline(L
, DebugLogging
));
1357 } else if (Matches
[1] == "thinlto-pre-link") {
1358 MPM
.addPass(buildThinLTOPreLinkDefaultPipeline(L
, DebugLogging
));
1359 } else if (Matches
[1] == "thinlto") {
1360 MPM
.addPass(buildThinLTODefaultPipeline(L
, DebugLogging
));
1361 } else if (Matches
[1] == "lto-pre-link") {
1362 MPM
.addPass(buildLTOPreLinkDefaultPipeline(L
, DebugLogging
));
1364 assert(Matches
[1] == "lto" && "Not one of the matched options!");
1365 MPM
.addPass(buildLTODefaultPipeline(L
, DebugLogging
));
1370 // Finally expand the basic registered passes from the .inc file.
1371 #define MODULE_PASS(NAME, CREATE_PASS) \
1372 if (Name == NAME) { \
1373 MPM.addPass(CREATE_PASS); \
1376 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1377 if (Name == "require<" NAME ">") { \
1379 RequireAnalysisPass< \
1380 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
1383 if (Name == "invalidate<" NAME ">") { \
1384 MPM.addPass(InvalidateAnalysisPass< \
1385 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
1388 #include "PassRegistry.def"
1390 for (auto &C
: ModulePipelineParsingCallbacks
)
1391 if (C(Name
, MPM
, InnerPipeline
))
1396 bool PassBuilder::parseCGSCCPass(CGSCCPassManager
&CGPM
,
1397 const PipelineElement
&E
, bool VerifyEachPass
,
1398 bool DebugLogging
) {
1399 auto &Name
= E
.Name
;
1400 auto &InnerPipeline
= E
.InnerPipeline
;
1402 // First handle complex passes like the pass managers which carry pipelines.
1403 if (!InnerPipeline
.empty()) {
1404 if (Name
== "cgscc") {
1405 CGSCCPassManager
NestedCGPM(DebugLogging
);
1406 if (!parseCGSCCPassPipeline(NestedCGPM
, InnerPipeline
, VerifyEachPass
,
1409 // Add the nested pass manager with the appropriate adaptor.
1410 CGPM
.addPass(std::move(NestedCGPM
));
1413 if (Name
== "function") {
1414 FunctionPassManager
FPM(DebugLogging
);
1415 if (!parseFunctionPassPipeline(FPM
, InnerPipeline
, VerifyEachPass
,
1418 // Add the nested pass manager with the appropriate adaptor.
1419 CGPM
.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM
)));
1422 if (auto Count
= parseRepeatPassName(Name
)) {
1423 CGSCCPassManager
NestedCGPM(DebugLogging
);
1424 if (!parseCGSCCPassPipeline(NestedCGPM
, InnerPipeline
, VerifyEachPass
,
1427 CGPM
.addPass(createRepeatedPass(*Count
, std::move(NestedCGPM
)));
1430 if (auto MaxRepetitions
= parseDevirtPassName(Name
)) {
1431 CGSCCPassManager
NestedCGPM(DebugLogging
);
1432 if (!parseCGSCCPassPipeline(NestedCGPM
, InnerPipeline
, VerifyEachPass
,
1436 createDevirtSCCRepeatedPass(std::move(NestedCGPM
), *MaxRepetitions
));
1440 for (auto &C
: CGSCCPipelineParsingCallbacks
)
1441 if (C(Name
, CGPM
, InnerPipeline
))
1444 // Normal passes can't have pipelines.
1448 // Now expand the basic registered passes from the .inc file.
1449 #define CGSCC_PASS(NAME, CREATE_PASS) \
1450 if (Name == NAME) { \
1451 CGPM.addPass(CREATE_PASS); \
1454 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1455 if (Name == "require<" NAME ">") { \
1456 CGPM.addPass(RequireAnalysisPass< \
1457 std::remove_reference<decltype(CREATE_PASS)>::type, \
1458 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1459 CGSCCUpdateResult &>()); \
1462 if (Name == "invalidate<" NAME ">") { \
1463 CGPM.addPass(InvalidateAnalysisPass< \
1464 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
1467 #include "PassRegistry.def"
1469 for (auto &C
: CGSCCPipelineParsingCallbacks
)
1470 if (C(Name
, CGPM
, InnerPipeline
))
1475 bool PassBuilder::parseFunctionPass(FunctionPassManager
&FPM
,
1476 const PipelineElement
&E
,
1477 bool VerifyEachPass
, bool DebugLogging
) {
1478 auto &Name
= E
.Name
;
1479 auto &InnerPipeline
= E
.InnerPipeline
;
1481 // First handle complex passes like the pass managers which carry pipelines.
1482 if (!InnerPipeline
.empty()) {
1483 if (Name
== "function") {
1484 FunctionPassManager
NestedFPM(DebugLogging
);
1485 if (!parseFunctionPassPipeline(NestedFPM
, InnerPipeline
, VerifyEachPass
,
1488 // Add the nested pass manager with the appropriate adaptor.
1489 FPM
.addPass(std::move(NestedFPM
));
1492 if (Name
== "loop") {
1493 LoopPassManager
LPM(DebugLogging
);
1494 if (!parseLoopPassPipeline(LPM
, InnerPipeline
, VerifyEachPass
,
1497 // Add the nested pass manager with the appropriate adaptor.
1498 FPM
.addPass(createFunctionToLoopPassAdaptor(std::move(LPM
)));
1501 if (auto Count
= parseRepeatPassName(Name
)) {
1502 FunctionPassManager
NestedFPM(DebugLogging
);
1503 if (!parseFunctionPassPipeline(NestedFPM
, InnerPipeline
, VerifyEachPass
,
1506 FPM
.addPass(createRepeatedPass(*Count
, std::move(NestedFPM
)));
1510 for (auto &C
: FunctionPipelineParsingCallbacks
)
1511 if (C(Name
, FPM
, InnerPipeline
))
1514 // Normal passes can't have pipelines.
1518 // Now expand the basic registered passes from the .inc file.
1519 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1520 if (Name == NAME) { \
1521 FPM.addPass(CREATE_PASS); \
1524 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1525 if (Name == "require<" NAME ">") { \
1527 RequireAnalysisPass< \
1528 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
1531 if (Name == "invalidate<" NAME ">") { \
1532 FPM.addPass(InvalidateAnalysisPass< \
1533 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
1536 #include "PassRegistry.def"
1538 for (auto &C
: FunctionPipelineParsingCallbacks
)
1539 if (C(Name
, FPM
, InnerPipeline
))
1544 bool PassBuilder::parseLoopPass(LoopPassManager
&LPM
, const PipelineElement
&E
,
1545 bool VerifyEachPass
, bool DebugLogging
) {
1546 StringRef Name
= E
.Name
;
1547 auto &InnerPipeline
= E
.InnerPipeline
;
1549 // First handle complex passes like the pass managers which carry pipelines.
1550 if (!InnerPipeline
.empty()) {
1551 if (Name
== "loop") {
1552 LoopPassManager
NestedLPM(DebugLogging
);
1553 if (!parseLoopPassPipeline(NestedLPM
, InnerPipeline
, VerifyEachPass
,
1556 // Add the nested pass manager with the appropriate adaptor.
1557 LPM
.addPass(std::move(NestedLPM
));
1560 if (auto Count
= parseRepeatPassName(Name
)) {
1561 LoopPassManager
NestedLPM(DebugLogging
);
1562 if (!parseLoopPassPipeline(NestedLPM
, InnerPipeline
, VerifyEachPass
,
1565 LPM
.addPass(createRepeatedPass(*Count
, std::move(NestedLPM
)));
1569 for (auto &C
: LoopPipelineParsingCallbacks
)
1570 if (C(Name
, LPM
, InnerPipeline
))
1573 // Normal passes can't have pipelines.
1577 // Now expand the basic registered passes from the .inc file.
1578 #define LOOP_PASS(NAME, CREATE_PASS) \
1579 if (Name == NAME) { \
1580 LPM.addPass(CREATE_PASS); \
1583 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1584 if (Name == "require<" NAME ">") { \
1585 LPM.addPass(RequireAnalysisPass< \
1586 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1587 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1591 if (Name == "invalidate<" NAME ">") { \
1592 LPM.addPass(InvalidateAnalysisPass< \
1593 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
1596 #include "PassRegistry.def"
1598 for (auto &C
: LoopPipelineParsingCallbacks
)
1599 if (C(Name
, LPM
, InnerPipeline
))
1604 bool PassBuilder::parseAAPassName(AAManager
&AA
, StringRef Name
) {
1605 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1606 if (Name == NAME) { \
1607 AA.registerModuleAnalysis< \
1608 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1611 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1612 if (Name == NAME) { \
1613 AA.registerFunctionAnalysis< \
1614 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1617 #include "PassRegistry.def"
1619 for (auto &C
: AAParsingCallbacks
)
1625 bool PassBuilder::parseLoopPassPipeline(LoopPassManager
&LPM
,
1626 ArrayRef
<PipelineElement
> Pipeline
,
1627 bool VerifyEachPass
,
1628 bool DebugLogging
) {
1629 for (const auto &Element
: Pipeline
) {
1630 if (!parseLoopPass(LPM
, Element
, VerifyEachPass
, DebugLogging
))
1632 // FIXME: No verifier support for Loop passes!
1637 bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager
&FPM
,
1638 ArrayRef
<PipelineElement
> Pipeline
,
1639 bool VerifyEachPass
,
1640 bool DebugLogging
) {
1641 for (const auto &Element
: Pipeline
) {
1642 if (!parseFunctionPass(FPM
, Element
, VerifyEachPass
, DebugLogging
))
1645 FPM
.addPass(VerifierPass());
1650 bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager
&CGPM
,
1651 ArrayRef
<PipelineElement
> Pipeline
,
1652 bool VerifyEachPass
,
1653 bool DebugLogging
) {
1654 for (const auto &Element
: Pipeline
) {
1655 if (!parseCGSCCPass(CGPM
, Element
, VerifyEachPass
, DebugLogging
))
1657 // FIXME: No verifier support for CGSCC passes!
1662 void PassBuilder::crossRegisterProxies(LoopAnalysisManager
&LAM
,
1663 FunctionAnalysisManager
&FAM
,
1664 CGSCCAnalysisManager
&CGAM
,
1665 ModuleAnalysisManager
&MAM
) {
1666 MAM
.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM
); });
1667 MAM
.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM
); });
1668 CGAM
.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM
); });
1669 FAM
.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM
); });
1670 FAM
.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM
); });
1671 FAM
.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM
); });
1672 LAM
.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM
); });
1675 bool PassBuilder::parseModulePassPipeline(ModulePassManager
&MPM
,
1676 ArrayRef
<PipelineElement
> Pipeline
,
1677 bool VerifyEachPass
,
1678 bool DebugLogging
) {
1679 for (const auto &Element
: Pipeline
) {
1680 if (!parseModulePass(MPM
, Element
, VerifyEachPass
, DebugLogging
))
1683 MPM
.addPass(VerifierPass());
1688 // Primary pass pipeline description parsing routine for a \c ModulePassManager
1689 // FIXME: Should this routine accept a TargetMachine or require the caller to
1690 // pre-populate the analysis managers with target-specific stuff?
1691 bool PassBuilder::parsePassPipeline(ModulePassManager
&MPM
,
1692 StringRef PipelineText
, bool VerifyEachPass
,
1693 bool DebugLogging
) {
1694 auto Pipeline
= parsePipelineText(PipelineText
);
1695 if (!Pipeline
|| Pipeline
->empty())
1698 // If the first name isn't at the module layer, wrap the pipeline up
1700 StringRef FirstName
= Pipeline
->front().Name
;
1702 if (!isModulePassName(FirstName
, ModulePipelineParsingCallbacks
)) {
1703 if (isCGSCCPassName(FirstName
, CGSCCPipelineParsingCallbacks
)) {
1704 Pipeline
= {{"cgscc", std::move(*Pipeline
)}};
1705 } else if (isFunctionPassName(FirstName
,
1706 FunctionPipelineParsingCallbacks
)) {
1707 Pipeline
= {{"function", std::move(*Pipeline
)}};
1708 } else if (isLoopPassName(FirstName
, LoopPipelineParsingCallbacks
)) {
1709 Pipeline
= {{"function", {{"loop", std::move(*Pipeline
)}}}};
1711 for (auto &C
: TopLevelPipelineParsingCallbacks
)
1712 if (C(MPM
, *Pipeline
, VerifyEachPass
, DebugLogging
))
1715 // Unknown pass name!
1720 return parseModulePassPipeline(MPM
, *Pipeline
, VerifyEachPass
, DebugLogging
);
1723 // Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1724 bool PassBuilder::parsePassPipeline(CGSCCPassManager
&CGPM
,
1725 StringRef PipelineText
, bool VerifyEachPass
,
1726 bool DebugLogging
) {
1727 auto Pipeline
= parsePipelineText(PipelineText
);
1728 if (!Pipeline
|| Pipeline
->empty())
1731 StringRef FirstName
= Pipeline
->front().Name
;
1732 if (!isCGSCCPassName(FirstName
, CGSCCPipelineParsingCallbacks
))
1735 return parseCGSCCPassPipeline(CGPM
, *Pipeline
, VerifyEachPass
, DebugLogging
);
1738 // Primary pass pipeline description parsing routine for a \c
1739 // FunctionPassManager
1740 bool PassBuilder::parsePassPipeline(FunctionPassManager
&FPM
,
1741 StringRef PipelineText
, bool VerifyEachPass
,
1742 bool DebugLogging
) {
1743 auto Pipeline
= parsePipelineText(PipelineText
);
1744 if (!Pipeline
|| Pipeline
->empty())
1747 StringRef FirstName
= Pipeline
->front().Name
;
1748 if (!isFunctionPassName(FirstName
, FunctionPipelineParsingCallbacks
))
1751 return parseFunctionPassPipeline(FPM
, *Pipeline
, VerifyEachPass
,
1755 // Primary pass pipeline description parsing routine for a \c LoopPassManager
1756 bool PassBuilder::parsePassPipeline(LoopPassManager
&CGPM
,
1757 StringRef PipelineText
, bool VerifyEachPass
,
1758 bool DebugLogging
) {
1759 auto Pipeline
= parsePipelineText(PipelineText
);
1760 if (!Pipeline
|| Pipeline
->empty())
1763 return parseLoopPassPipeline(CGPM
, *Pipeline
, VerifyEachPass
, DebugLogging
);
1766 bool PassBuilder::parseAAPipeline(AAManager
&AA
, StringRef PipelineText
) {
1767 // If the pipeline just consists of the word 'default' just replace the AA
1768 // manager with our default one.
1769 if (PipelineText
== "default") {
1770 AA
= buildDefaultAAPipeline();
1774 while (!PipelineText
.empty()) {
1776 std::tie(Name
, PipelineText
) = PipelineText
.split(',');
1777 if (!parseAAPassName(AA
, Name
))