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/ModuleSummaryAnalysis.h"
43 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
44 #include "llvm/Analysis/PostDominators.h"
45 #include "llvm/Analysis/ProfileSummaryInfo.h"
46 #include "llvm/Analysis/RegionInfo.h"
47 #include "llvm/Analysis/ScalarEvolution.h"
48 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
49 #include "llvm/Analysis/ScopedNoAliasAA.h"
50 #include "llvm/Analysis/TargetLibraryInfo.h"
51 #include "llvm/Analysis/TargetTransformInfo.h"
52 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
53 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
54 #include "llvm/CodeGen/UnreachableBlockElim.h"
55 #include "llvm/IR/Dominators.h"
56 #include "llvm/IR/IRPrintingPasses.h"
57 #include "llvm/IR/PassManager.h"
58 #include "llvm/IR/Verifier.h"
59 #include "llvm/Support/Debug.h"
60 #include "llvm/Support/Regex.h"
61 #include "llvm/Target/TargetMachine.h"
62 #include "llvm/Transforms/GCOVProfiler.h"
63 #include "llvm/Transforms/IPO/AlwaysInliner.h"
64 #include "llvm/Transforms/IPO/ConstantMerge.h"
65 #include "llvm/Transforms/IPO/CrossDSOCFI.h"
66 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
67 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
68 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
69 #include "llvm/Transforms/IPO/FunctionAttrs.h"
70 #include "llvm/Transforms/IPO/FunctionImport.h"
71 #include "llvm/Transforms/IPO/GlobalDCE.h"
72 #include "llvm/Transforms/IPO/GlobalOpt.h"
73 #include "llvm/Transforms/IPO/GlobalSplit.h"
74 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
75 #include "llvm/Transforms/IPO/Inliner.h"
76 #include "llvm/Transforms/IPO/Internalize.h"
77 #include "llvm/Transforms/IPO/LowerTypeTests.h"
78 #include "llvm/Transforms/IPO/PartialInlining.h"
79 #include "llvm/Transforms/IPO/SCCP.h"
80 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
81 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
82 #include "llvm/Transforms/InstCombine/InstCombine.h"
83 #include "llvm/Transforms/InstrProfiling.h"
84 #include "llvm/Transforms/PGOInstrumentation.h"
85 #include "llvm/Transforms/SampleProfile.h"
86 #include "llvm/Transforms/Scalar/ADCE.h"
87 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
88 #include "llvm/Transforms/Scalar/BDCE.h"
89 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
90 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
91 #include "llvm/Transforms/Scalar/DCE.h"
92 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
93 #include "llvm/Transforms/Scalar/EarlyCSE.h"
94 #include "llvm/Transforms/Scalar/Float2Int.h"
95 #include "llvm/Transforms/Scalar/GVN.h"
96 #include "llvm/Transforms/Scalar/GuardWidening.h"
97 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
98 #include "llvm/Transforms/Scalar/JumpThreading.h"
99 #include "llvm/Transforms/Scalar/LICM.h"
100 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
101 #include "llvm/Transforms/Scalar/LoopDeletion.h"
102 #include "llvm/Transforms/Scalar/LoopDistribute.h"
103 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
104 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
105 #include "llvm/Transforms/Scalar/LoopRotation.h"
106 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
107 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
108 #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
109 #include "llvm/Transforms/Scalar/LowerAtomic.h"
110 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
111 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
112 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
113 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
114 #include "llvm/Transforms/Scalar/NaryReassociate.h"
115 #include "llvm/Transforms/Scalar/NewGVN.h"
116 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
117 #include "llvm/Transforms/Scalar/Reassociate.h"
118 #include "llvm/Transforms/Scalar/SCCP.h"
119 #include "llvm/Transforms/Scalar/SROA.h"
120 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
121 #include "llvm/Transforms/Scalar/Sink.h"
122 #include "llvm/Transforms/Scalar/SpeculativeExecution.h"
123 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
124 #include "llvm/Transforms/Utils/AddDiscriminators.h"
125 #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
126 #include "llvm/Transforms/Utils/LCSSA.h"
127 #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
128 #include "llvm/Transforms/Utils/LoopSimplify.h"
129 #include "llvm/Transforms/Utils/LowerInvoke.h"
130 #include "llvm/Transforms/Utils/Mem2Reg.h"
131 #include "llvm/Transforms/Utils/MemorySSA.h"
132 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
133 #include "llvm/Transforms/Utils/SimplifyInstructions.h"
134 #include "llvm/Transforms/Utils/SymbolRewriter.h"
135 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
136 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
138 #include <type_traits>
140 using namespace llvm;
142 static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
144 static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
146 case PassBuilder::O0:
147 case PassBuilder::O1:
148 case PassBuilder::O2:
149 case PassBuilder::O3:
152 case PassBuilder::Os:
153 case PassBuilder::Oz:
156 llvm_unreachable("Invalid optimization level!");
161 /// \brief No-op module pass which does nothing.
162 struct NoOpModulePass {
163 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
164 return PreservedAnalyses::all();
166 static StringRef name() { return "NoOpModulePass"; }
169 /// \brief No-op module analysis.
170 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
171 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
172 static AnalysisKey Key;
176 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
177 static StringRef name() { return "NoOpModuleAnalysis"; }
180 /// \brief No-op CGSCC pass which does nothing.
181 struct NoOpCGSCCPass {
182 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
183 LazyCallGraph &, CGSCCUpdateResult &UR) {
184 return PreservedAnalyses::all();
186 static StringRef name() { return "NoOpCGSCCPass"; }
189 /// \brief No-op CGSCC analysis.
190 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
191 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
192 static AnalysisKey Key;
196 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
199 static StringRef name() { return "NoOpCGSCCAnalysis"; }
202 /// \brief No-op function pass which does nothing.
203 struct NoOpFunctionPass {
204 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
205 return PreservedAnalyses::all();
207 static StringRef name() { return "NoOpFunctionPass"; }
210 /// \brief No-op function analysis.
211 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
212 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
213 static AnalysisKey Key;
217 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
218 static StringRef name() { return "NoOpFunctionAnalysis"; }
221 /// \brief No-op loop pass which does nothing.
222 struct NoOpLoopPass {
223 PreservedAnalyses run(Loop &L, LoopAnalysisManager &) {
224 return PreservedAnalyses::all();
226 static StringRef name() { return "NoOpLoopPass"; }
229 /// \brief No-op loop analysis.
230 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
231 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
232 static AnalysisKey Key;
236 Result run(Loop &, LoopAnalysisManager &) { return Result(); }
237 static StringRef name() { return "NoOpLoopAnalysis"; }
240 AnalysisKey NoOpModuleAnalysis::Key;
241 AnalysisKey NoOpCGSCCAnalysis::Key;
242 AnalysisKey NoOpFunctionAnalysis::Key;
243 AnalysisKey NoOpLoopAnalysis::Key;
245 } // End anonymous namespace.
247 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
248 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
249 MAM.registerPass([&] { return CREATE_PASS; });
250 #include "PassRegistry.def"
253 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
254 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
255 CGAM.registerPass([&] { return CREATE_PASS; });
256 #include "PassRegistry.def"
259 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
260 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
261 FAM.registerPass([&] { return CREATE_PASS; });
262 #include "PassRegistry.def"
265 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
266 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
267 LAM.registerPass([&] { return CREATE_PASS; });
268 #include "PassRegistry.def"
272 PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
274 assert(Level != O0 && "Must request optimizations!");
275 FunctionPassManager FPM(DebugLogging);
277 // Form SSA out of local memory accesses after breaking apart aggregates into
281 // Catch trivial redundancies
282 FPM.addPass(EarlyCSEPass());
284 // Speculative execution if the target has divergent branches; otherwise nop.
285 FPM.addPass(SpeculativeExecutionPass());
287 // Optimize based on known information about branches, and cleanup afterward.
288 FPM.addPass(JumpThreadingPass());
289 FPM.addPass(CorrelatedValuePropagationPass());
290 FPM.addPass(SimplifyCFGPass());
291 FPM.addPass(InstCombinePass());
293 if (!isOptimizingForSize(Level))
294 FPM.addPass(LibCallsShrinkWrapPass());
296 FPM.addPass(TailCallElimPass());
297 FPM.addPass(SimplifyCFGPass());
299 // Form canonically associated expression trees, and simplify the trees using
300 // basic mathematical properties. For example, this will form (nearly)
301 // minimal multiplication trees.
302 FPM.addPass(ReassociatePass());
304 // Add the primary loop simplification pipeline.
305 // FIXME: Currently this is split into two loop pass pipelines because we run
306 // some function passes in between them. These can and should be replaced by
307 // loop pass equivalenst but those aren't ready yet. Specifically,
308 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
309 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
310 // the other we have is `LoopInstSimplify`.
311 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
313 // FIXME: Enable these when the loop pass manager can support enforcing loop
314 // simplified and LCSSA form as well as updating the loop nest after
315 // transformations and we finsih porting the loop passes.
317 // Rotate Loop - disable header duplication at -Oz
318 LPM1.addPass(LoopRotatePass(Level != Oz));
319 LPM1.addPass(LICMPass());
320 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
321 LPM2.addPass(IndVarSimplifyPass());
322 LPM2.addPass(LoopIdiomPass());
323 LPM2.addPass(LoopDeletionPass());
324 LPM2.addPass(SimpleLoopUnrollPass());
326 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
327 FPM.addPass(SimplifyCFGPass());
328 FPM.addPass(InstCombinePass());
329 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
331 // Eliminate redundancies.
333 // These passes add substantial compile time so skip them at O1.
334 FPM.addPass(MergedLoadStoreMotionPass());
338 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
339 FPM.addPass(MemCpyOptPass());
341 // Sparse conditional constant propagation.
342 // FIXME: It isn't clear why we do this *after* loop passes rather than
344 FPM.addPass(SCCPPass());
346 // Delete dead bit computations (instcombine runs after to fold away the dead
347 // computations, and then ADCE will run later to exploit any new DCE
348 // opportunities that creates).
349 FPM.addPass(BDCEPass());
351 // Run instcombine after redundancy and dead bit elimination to exploit
352 // opportunities opened up by them.
353 FPM.addPass(InstCombinePass());
355 // Re-consider control flow based optimizations after redundancy elimination,
357 FPM.addPass(JumpThreadingPass());
358 FPM.addPass(CorrelatedValuePropagationPass());
359 FPM.addPass(DSEPass());
360 // FIXME: Enable this when the loop pass manager can support enforcing loop
361 // simplified and LCSSA form as well as updating the loop nest after
362 // transformations and we finsih porting the loop passes.
364 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
367 // Finally, do an expensive DCE pass to catch all the dead code exposed by
368 // the simplifications and basic cleanup after all the simplifications.
369 FPM.addPass(ADCEPass());
370 FPM.addPass(SimplifyCFGPass());
371 FPM.addPass(InstCombinePass());
377 PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
379 assert(Level != O0 && "Must request optimizations for the default pipeline!");
380 ModulePassManager MPM(DebugLogging);
382 // Force any function attributes we want the rest of the pipeline te observe.
383 MPM.addPass(ForceFunctionAttrsPass());
385 // Do basic inference of function attributes from known properties of system
386 // libraries and other oracles.
387 MPM.addPass(InferFunctionAttrsPass());
389 // Create an early function pass manager to cleanup the output of the
391 FunctionPassManager EarlyFPM(DebugLogging);
392 EarlyFPM.addPass(SimplifyCFGPass());
393 EarlyFPM.addPass(SROA());
394 EarlyFPM.addPass(EarlyCSEPass());
395 EarlyFPM.addPass(LowerExpectIntrinsicPass());
396 EarlyFPM.addPass(GVNHoistPass());
397 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
399 // Interprocedural constant propagation now that basic cleanup has occured
400 // and prior to optimizing globals.
401 // FIXME: This position in the pipeline hasn't been carefully considered in
402 // years, it should be re-analyzed.
403 MPM.addPass(IPSCCPPass());
405 // Optimize globals to try and fold them into constants.
406 MPM.addPass(GlobalOptPass());
408 // Promote any localized globals to SSA registers.
409 // FIXME: Should this instead by a run of SROA?
410 // FIXME: We should probably run instcombine and simplify-cfg afterward to
411 // delete control flows that are dead once globals have been folded to
413 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
415 // Remove any dead arguments exposed by cleanups and constand folding
417 MPM.addPass(DeadArgumentEliminationPass());
419 // Create a small function pass pipeline to cleanup after all the global
421 FunctionPassManager GlobalCleanupPM(DebugLogging);
422 GlobalCleanupPM.addPass(InstCombinePass());
423 GlobalCleanupPM.addPass(SimplifyCFGPass());
424 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
426 // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
428 MPM.addPass(RequireAnalysisPass<GlobalsAA>());
431 // Now begin the main postorder CGSCC pipeline.
432 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
433 // manager and trying to emulate its precise behavior. Much of this doesn't
434 // make a lot of sense and we should revisit the core CGSCC structure.
435 CGSCCPassManager MainCGPipeline(DebugLogging);
437 // Note: historically, the PruneEH pass was run first to deduce nounwind and
438 // generally clean up exception handling overhead. It isn't clear this is
439 // valuable as the inliner doesn't currently care whether it is inlining an
442 // Run the inliner first. The theory is that we are walking bottom-up and so
443 // the callees have already been fully optimized, and we want to inline them
444 // into the callers so that our optimizations can reflect that.
445 // FIXME; Customize the threshold based on optimization level.
446 MainCGPipeline.addPass(InlinerPass());
448 // Now deduce any function attributes based in the current code.
449 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
451 // Lastly, add the core function simplification pipeline nested inside the
453 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
454 buildFunctionSimplificationPipeline(Level, DebugLogging)));
457 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
459 // This ends the canonicalization and simplification phase of the pipeline.
460 // At this point, we expect to have canonical and simple IR which we begin
461 // *optimizing* for efficient execution going forward.
463 // Eliminate externally available functions now that inlining is over -- we
464 // won't emit these anyways.
465 MPM.addPass(EliminateAvailableExternallyPass());
467 // Do RPO function attribute inference across the module to forward-propagate
468 // attributes where applicable.
469 // FIXME: Is this really an optimization rather than a canonicalization?
470 MPM.addPass(ReversePostOrderFunctionAttrsPass());
472 // Recompute GloblasAA here prior to function passes. This is particularly
473 // useful as the above will have inlined, DCE'ed, and function-attr
474 // propagated everything. We should at this point have a reasonably minimal
475 // and richly annotated call graph. By computing aliasing and mod/ref
476 // information for all local globals here, the late loop passes and notably
477 // the vectorizer will be able to use them to help recognize vectorizable
478 // memory operations.
479 // FIXME: Enable this once analysis invalidation is fully supported.
481 MPM.addPass(Require<GlobalsAA>());
484 FunctionPassManager OptimizePM(DebugLogging);
485 OptimizePM.addPass(Float2IntPass());
486 // FIXME: We need to run some loop optimizations to re-rotate loops after
487 // simplify-cfg and others undo their rotation.
489 // Optimize the loop execution. These passes operate on entire loop nests
490 // rather than on each loop in an inside-out manner, and so they are actually
492 OptimizePM.addPass(LoopDistributePass());
494 // FIXME: LoopVectorize relies on "requiring" LCSSA which isn't supported in
496 OptimizePM.addPass(LoopVectorizePass());
498 // FIXME: Need to port Loop Load Elimination and add it here.
499 OptimizePM.addPass(InstCombinePass());
501 // Optimize parallel scalar instruction chains into SIMD instructions.
502 OptimizePM.addPass(SLPVectorizerPass());
504 // Cleanup after vectorizers.
505 OptimizePM.addPass(SimplifyCFGPass());
506 OptimizePM.addPass(InstCombinePass());
508 // Unroll small loops to hide loop backedge latency and saturate any parallel
509 // execution resources of an out-of-order processor.
510 // FIXME: Need to add once loop pass pipeline is available.
512 // FIXME: Add the loop sink pass when ported.
514 // FIXME: Add cleanup from the loop pass manager when we're forming LCSSA
517 // Now that we've vectorized and unrolled loops, we may have more refined
518 // alignment information, try to re-derive it here.
519 OptimizePM.addPass(AlignmentFromAssumptionsPass());
521 // ADd the core optimizing pipeline.
522 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
524 // Now we need to do some global optimization transforms.
525 // FIXME: It would seem like these should come first in the optimization
526 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
528 MPM.addPass(GlobalDCEPass());
529 MPM.addPass(ConstantMergePass());
535 PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
537 assert(Level != O0 && "Must request optimizations for the default pipeline!");
538 // FIXME: We should use a customized pre-link pipeline!
539 return buildPerModuleDefaultPipeline(Level, DebugLogging);
542 ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
544 assert(Level != O0 && "Must request optimizations for the default pipeline!");
545 ModulePassManager MPM(DebugLogging);
547 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
548 FunctionPassManager LateFPM(DebugLogging);
549 LateFPM.addPass(InstCombinePass());
550 LateFPM.addPass(SimplifyCFGPass());
552 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
557 AAManager PassBuilder::buildDefaultAAPipeline() {
560 // The order in which these are registered determines their priority when
563 // First we register the basic alias analysis that provides the majority of
564 // per-function local AA logic. This is a stateless, on-demand local set of
566 AA.registerFunctionAnalysis<BasicAA>();
568 // Next we query fast, specialized alias analyses that wrap IR-embedded
569 // information about aliasing.
570 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
571 AA.registerFunctionAnalysis<TypeBasedAA>();
573 // Add support for querying global aliasing information when available.
574 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
575 // analysis, all that the `AAManager` can do is query for any *cached*
576 // results from `GlobalsAA` through a readonly proxy..
578 // FIXME: Enable once the invalidation logic supports this. Currently, the
579 // `AAManager` will hold stale references to the module analyses.
580 AA.registerModuleAnalysis<GlobalsAA>();
586 static Optional<int> parseRepeatPassName(StringRef Name) {
587 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
590 if (Name.getAsInteger(0, Count) || Count <= 0)
595 static Optional<int> parseDevirtPassName(StringRef Name) {
596 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
599 if (Name.getAsInteger(0, Count) || Count <= 0)
604 static bool isModulePassName(StringRef Name) {
605 // Manually handle aliases for pre-configured pipeline fragments.
606 if (Name.startswith("default") || Name.startswith("lto"))
607 return DefaultAliasRegex.match(Name);
609 // Explicitly handle pass manager names.
610 if (Name == "module")
614 if (Name == "function")
617 // Explicitly handle custom-parsed pass names.
618 if (parseRepeatPassName(Name))
621 #define MODULE_PASS(NAME, CREATE_PASS) \
624 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
625 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
627 #include "PassRegistry.def"
632 static bool isCGSCCPassName(StringRef Name) {
633 // Explicitly handle pass manager names.
636 if (Name == "function")
639 // Explicitly handle custom-parsed pass names.
640 if (parseRepeatPassName(Name))
642 if (parseDevirtPassName(Name))
645 #define CGSCC_PASS(NAME, CREATE_PASS) \
648 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
649 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
651 #include "PassRegistry.def"
656 static bool isFunctionPassName(StringRef Name) {
657 // Explicitly handle pass manager names.
658 if (Name == "function")
663 // Explicitly handle custom-parsed pass names.
664 if (parseRepeatPassName(Name))
667 #define FUNCTION_PASS(NAME, CREATE_PASS) \
670 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
671 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
673 #include "PassRegistry.def"
678 static bool isLoopPassName(StringRef Name) {
679 // Explicitly handle pass manager names.
683 // Explicitly handle custom-parsed pass names.
684 if (parseRepeatPassName(Name))
687 #define LOOP_PASS(NAME, CREATE_PASS) \
690 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
691 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
693 #include "PassRegistry.def"
698 Optional<std::vector<PassBuilder::PipelineElement>>
699 PassBuilder::parsePipelineText(StringRef Text) {
700 std::vector<PipelineElement> ResultPipeline;
702 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
705 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
706 size_t Pos = Text.find_first_of(",()");
707 Pipeline.push_back({Text.substr(0, Pos), {}});
709 // If we have a single terminating name, we're done.
710 if (Pos == Text.npos)
713 char Sep = Text[Pos];
714 Text = Text.substr(Pos + 1);
716 // Just a name ending in a comma, continue.
720 // Push the inner pipeline onto the stack to continue processing.
721 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
725 assert(Sep == ')' && "Bogus separator!");
726 // When handling the close parenthesis, we greedily consume them to avoid
727 // empty strings in the pipeline.
729 // If we try to pop the outer pipeline we have unbalanced parentheses.
730 if (PipelineStack.size() == 1)
733 PipelineStack.pop_back();
734 } while (Text.consume_front(")"));
736 // Check if we've finished parsing.
740 // Otherwise, the end of an inner pipeline always has to be followed by
741 // a comma, and then we can continue.
742 if (!Text.consume_front(","))
746 if (PipelineStack.size() > 1)
747 // Unbalanced paretheses.
750 assert(PipelineStack.back() == &ResultPipeline &&
751 "Wrong pipeline at the bottom of the stack!");
752 return {std::move(ResultPipeline)};
755 bool PassBuilder::parseModulePass(ModulePassManager &MPM,
756 const PipelineElement &E, bool VerifyEachPass,
759 auto &InnerPipeline = E.InnerPipeline;
761 // First handle complex passes like the pass managers which carry pipelines.
762 if (!InnerPipeline.empty()) {
763 if (Name == "module") {
764 ModulePassManager NestedMPM(DebugLogging);
765 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
768 MPM.addPass(std::move(NestedMPM));
771 if (Name == "cgscc") {
772 CGSCCPassManager CGPM(DebugLogging);
773 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
776 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
780 if (Name == "function") {
781 FunctionPassManager FPM(DebugLogging);
782 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
785 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
788 if (auto Count = parseRepeatPassName(Name)) {
789 ModulePassManager NestedMPM(DebugLogging);
790 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
793 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
796 // Normal passes can't have pipelines.
800 // Manually handle aliases for pre-configured pipeline fragments.
801 if (Name.startswith("default") || Name.startswith("lto")) {
802 SmallVector<StringRef, 3> Matches;
803 if (!DefaultAliasRegex.match(Name, &Matches))
805 assert(Matches.size() == 3 && "Must capture two matched strings!");
807 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
815 // At O0 we do nothing at all!
818 if (Matches[1] == "default") {
819 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
820 } else if (Matches[1] == "lto-pre-link") {
821 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
823 assert(Matches[1] == "lto" && "Not one of the matched options!");
824 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
829 // Finally expand the basic registered passes from the .inc file.
830 #define MODULE_PASS(NAME, CREATE_PASS) \
831 if (Name == NAME) { \
832 MPM.addPass(CREATE_PASS); \
835 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
836 if (Name == "require<" NAME ">") { \
838 RequireAnalysisPass< \
839 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
842 if (Name == "invalidate<" NAME ">") { \
843 MPM.addPass(InvalidateAnalysisPass< \
844 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
847 #include "PassRegistry.def"
852 bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
853 const PipelineElement &E, bool VerifyEachPass,
856 auto &InnerPipeline = E.InnerPipeline;
858 // First handle complex passes like the pass managers which carry pipelines.
859 if (!InnerPipeline.empty()) {
860 if (Name == "cgscc") {
861 CGSCCPassManager NestedCGPM(DebugLogging);
862 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
865 // Add the nested pass manager with the appropriate adaptor.
866 CGPM.addPass(std::move(NestedCGPM));
869 if (Name == "function") {
870 FunctionPassManager FPM(DebugLogging);
871 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
874 // Add the nested pass manager with the appropriate adaptor.
876 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
879 if (auto Count = parseRepeatPassName(Name)) {
880 CGSCCPassManager NestedCGPM(DebugLogging);
881 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
884 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
887 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
888 CGSCCPassManager NestedCGPM(DebugLogging);
889 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
892 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
893 *MaxRepetitions, DebugLogging));
896 // Normal passes can't have pipelines.
900 // Now expand the basic registered passes from the .inc file.
901 #define CGSCC_PASS(NAME, CREATE_PASS) \
902 if (Name == NAME) { \
903 CGPM.addPass(CREATE_PASS); \
906 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
907 if (Name == "require<" NAME ">") { \
908 CGPM.addPass(RequireAnalysisPass< \
909 std::remove_reference<decltype(CREATE_PASS)>::type, \
910 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
911 CGSCCUpdateResult &>()); \
914 if (Name == "invalidate<" NAME ">") { \
915 CGPM.addPass(InvalidateAnalysisPass< \
916 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
919 #include "PassRegistry.def"
924 bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
925 const PipelineElement &E,
926 bool VerifyEachPass, bool DebugLogging) {
928 auto &InnerPipeline = E.InnerPipeline;
930 // First handle complex passes like the pass managers which carry pipelines.
931 if (!InnerPipeline.empty()) {
932 if (Name == "function") {
933 FunctionPassManager NestedFPM(DebugLogging);
934 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
937 // Add the nested pass manager with the appropriate adaptor.
938 FPM.addPass(std::move(NestedFPM));
941 if (Name == "loop") {
942 LoopPassManager LPM(DebugLogging);
943 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
946 // Add the nested pass manager with the appropriate adaptor.
947 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
950 if (auto Count = parseRepeatPassName(Name)) {
951 FunctionPassManager NestedFPM(DebugLogging);
952 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
955 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
958 // Normal passes can't have pipelines.
962 // Now expand the basic registered passes from the .inc file.
963 #define FUNCTION_PASS(NAME, CREATE_PASS) \
964 if (Name == NAME) { \
965 FPM.addPass(CREATE_PASS); \
968 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
969 if (Name == "require<" NAME ">") { \
971 RequireAnalysisPass< \
972 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
975 if (Name == "invalidate<" NAME ">") { \
976 FPM.addPass(InvalidateAnalysisPass< \
977 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
980 #include "PassRegistry.def"
985 bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
986 bool VerifyEachPass, bool DebugLogging) {
987 StringRef Name = E.Name;
988 auto &InnerPipeline = E.InnerPipeline;
990 // First handle complex passes like the pass managers which carry pipelines.
991 if (!InnerPipeline.empty()) {
992 if (Name == "loop") {
993 LoopPassManager NestedLPM(DebugLogging);
994 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
997 // Add the nested pass manager with the appropriate adaptor.
998 LPM.addPass(std::move(NestedLPM));
1001 if (auto Count = parseRepeatPassName(Name)) {
1002 LoopPassManager NestedLPM(DebugLogging);
1003 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1006 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
1009 // Normal passes can't have pipelines.
1013 // Now expand the basic registered passes from the .inc file.
1014 #define LOOP_PASS(NAME, CREATE_PASS) \
1015 if (Name == NAME) { \
1016 LPM.addPass(CREATE_PASS); \
1019 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1020 if (Name == "require<" NAME ">") { \
1021 LPM.addPass(RequireAnalysisPass< \
1022 std::remove_reference<decltype(CREATE_PASS)>::type, Loop>()); \
1025 if (Name == "invalidate<" NAME ">") { \
1026 LPM.addPass(InvalidateAnalysisPass< \
1027 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
1030 #include "PassRegistry.def"
1035 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
1036 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1037 if (Name == NAME) { \
1038 AA.registerModuleAnalysis< \
1039 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1042 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1043 if (Name == NAME) { \
1044 AA.registerFunctionAnalysis< \
1045 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1048 #include "PassRegistry.def"
1053 bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
1054 ArrayRef<PipelineElement> Pipeline,
1055 bool VerifyEachPass,
1056 bool DebugLogging) {
1057 for (const auto &Element : Pipeline) {
1058 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1060 // FIXME: No verifier support for Loop passes!
1065 bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
1066 ArrayRef<PipelineElement> Pipeline,
1067 bool VerifyEachPass,
1068 bool DebugLogging) {
1069 for (const auto &Element : Pipeline) {
1070 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1073 FPM.addPass(VerifierPass());
1078 bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
1079 ArrayRef<PipelineElement> Pipeline,
1080 bool VerifyEachPass,
1081 bool DebugLogging) {
1082 for (const auto &Element : Pipeline) {
1083 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1085 // FIXME: No verifier support for CGSCC passes!
1090 void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1091 FunctionAnalysisManager &FAM,
1092 CGSCCAnalysisManager &CGAM,
1093 ModuleAnalysisManager &MAM) {
1094 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1095 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
1096 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1097 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1098 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1099 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1100 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1103 bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
1104 ArrayRef<PipelineElement> Pipeline,
1105 bool VerifyEachPass,
1106 bool DebugLogging) {
1107 for (const auto &Element : Pipeline) {
1108 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1111 MPM.addPass(VerifierPass());
1116 // Primary pass pipeline description parsing routine.
1117 // FIXME: Should this routine accept a TargetMachine or require the caller to
1118 // pre-populate the analysis managers with target-specific stuff?
1119 bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1120 StringRef PipelineText, bool VerifyEachPass,
1121 bool DebugLogging) {
1122 auto Pipeline = parsePipelineText(PipelineText);
1123 if (!Pipeline || Pipeline->empty())
1126 // If the first name isn't at the module layer, wrap the pipeline up
1128 StringRef FirstName = Pipeline->front().Name;
1130 if (!isModulePassName(FirstName)) {
1131 if (isCGSCCPassName(FirstName))
1132 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1133 else if (isFunctionPassName(FirstName))
1134 Pipeline = {{"function", std::move(*Pipeline)}};
1135 else if (isLoopPassName(FirstName))
1136 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1138 // Unknown pass name!
1142 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
1145 bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
1146 // If the pipeline just consists of the word 'default' just replace the AA
1147 // manager with our default one.
1148 if (PipelineText == "default") {
1149 AA = buildDefaultAAPipeline();
1153 while (!PipelineText.empty()) {
1155 std::tie(Name, PipelineText) = PipelineText.split(',');
1156 if (!parseAAPassName(AA, Name))