]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Passes/PassBuilder.cpp
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Passes / PassBuilder.cpp
1 //===- Parsing, selection, and construction of pass pipelines -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
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
14 /// pipelines.
15 ///
16 //===----------------------------------------------------------------------===//
17
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"
137
138 #include <type_traits>
139
140 using namespace llvm;
141
142 static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
143
144 static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
145   switch (Level) {
146   case PassBuilder::O0:
147   case PassBuilder::O1:
148   case PassBuilder::O2:
149   case PassBuilder::O3:
150     return false;
151
152   case PassBuilder::Os:
153   case PassBuilder::Oz:
154     return true;
155   }
156   llvm_unreachable("Invalid optimization level!");
157 }
158
159 namespace {
160
161 /// \brief No-op module pass which does nothing.
162 struct NoOpModulePass {
163   PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
164     return PreservedAnalyses::all();
165   }
166   static StringRef name() { return "NoOpModulePass"; }
167 };
168
169 /// \brief No-op module analysis.
170 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
171   friend AnalysisInfoMixin<NoOpModuleAnalysis>;
172   static AnalysisKey Key;
173
174 public:
175   struct Result {};
176   Result run(Module &, ModuleAnalysisManager &) { return Result(); }
177   static StringRef name() { return "NoOpModuleAnalysis"; }
178 };
179
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();
185   }
186   static StringRef name() { return "NoOpCGSCCPass"; }
187 };
188
189 /// \brief No-op CGSCC analysis.
190 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
191   friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
192   static AnalysisKey Key;
193
194 public:
195   struct Result {};
196   Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
197     return Result();
198   }
199   static StringRef name() { return "NoOpCGSCCAnalysis"; }
200 };
201
202 /// \brief No-op function pass which does nothing.
203 struct NoOpFunctionPass {
204   PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
205     return PreservedAnalyses::all();
206   }
207   static StringRef name() { return "NoOpFunctionPass"; }
208 };
209
210 /// \brief No-op function analysis.
211 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
212   friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
213   static AnalysisKey Key;
214
215 public:
216   struct Result {};
217   Result run(Function &, FunctionAnalysisManager &) { return Result(); }
218   static StringRef name() { return "NoOpFunctionAnalysis"; }
219 };
220
221 /// \brief No-op loop pass which does nothing.
222 struct NoOpLoopPass {
223   PreservedAnalyses run(Loop &L, LoopAnalysisManager &) {
224     return PreservedAnalyses::all();
225   }
226   static StringRef name() { return "NoOpLoopPass"; }
227 };
228
229 /// \brief No-op loop analysis.
230 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
231   friend AnalysisInfoMixin<NoOpLoopAnalysis>;
232   static AnalysisKey Key;
233
234 public:
235   struct Result {};
236   Result run(Loop &, LoopAnalysisManager &) { return Result(); }
237   static StringRef name() { return "NoOpLoopAnalysis"; }
238 };
239
240 AnalysisKey NoOpModuleAnalysis::Key;
241 AnalysisKey NoOpCGSCCAnalysis::Key;
242 AnalysisKey NoOpFunctionAnalysis::Key;
243 AnalysisKey NoOpLoopAnalysis::Key;
244
245 } // End anonymous namespace.
246
247 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
248 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
249   MAM.registerPass([&] { return CREATE_PASS; });
250 #include "PassRegistry.def"
251 }
252
253 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
254 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
255   CGAM.registerPass([&] { return CREATE_PASS; });
256 #include "PassRegistry.def"
257 }
258
259 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
260 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
261   FAM.registerPass([&] { return CREATE_PASS; });
262 #include "PassRegistry.def"
263 }
264
265 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
266 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
267   LAM.registerPass([&] { return CREATE_PASS; });
268 #include "PassRegistry.def"
269 }
270
271 FunctionPassManager
272 PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
273                                                  bool DebugLogging) {
274   assert(Level != O0 && "Must request optimizations!");
275   FunctionPassManager FPM(DebugLogging);
276
277   // Form SSA out of local memory accesses after breaking apart aggregates into
278   // scalars.
279   FPM.addPass(SROA());
280
281   // Catch trivial redundancies
282   FPM.addPass(EarlyCSEPass());
283
284   // Speculative execution if the target has divergent branches; otherwise nop.
285   FPM.addPass(SpeculativeExecutionPass());
286
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());
292
293   if (!isOptimizingForSize(Level))
294     FPM.addPass(LibCallsShrinkWrapPass());
295
296   FPM.addPass(TailCallElimPass());
297   FPM.addPass(SimplifyCFGPass());
298
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());
303
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);
312
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.
316 #if 0
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());
325 #endif
326   FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
327   FPM.addPass(SimplifyCFGPass());
328   FPM.addPass(InstCombinePass());
329   FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
330
331   // Eliminate redundancies.
332   if (Level != O1) {
333     // These passes add substantial compile time so skip them at O1.
334     FPM.addPass(MergedLoadStoreMotionPass());
335     FPM.addPass(GVN());
336   }
337
338   // Specially optimize memory movement as it doesn't look like dataflow in SSA.
339   FPM.addPass(MemCpyOptPass());
340
341   // Sparse conditional constant propagation.
342   // FIXME: It isn't clear why we do this *after* loop passes rather than
343   // before...
344   FPM.addPass(SCCPPass());
345
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());
350
351   // Run instcombine after redundancy and dead bit elimination to exploit
352   // opportunities opened up by them.
353   FPM.addPass(InstCombinePass());
354
355   // Re-consider control flow based optimizations after redundancy elimination,
356   // redo DCE, etc.
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.
363 #if 0
364   FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
365 #endif
366
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());
372
373   return FPM;
374 }
375
376 ModulePassManager
377 PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
378                                            bool DebugLogging) {
379   assert(Level != O0 && "Must request optimizations for the default pipeline!");
380   ModulePassManager MPM(DebugLogging);
381
382   // Force any function attributes we want the rest of the pipeline te observe.
383   MPM.addPass(ForceFunctionAttrsPass());
384
385   // Do basic inference of function attributes from known properties of system
386   // libraries and other oracles.
387   MPM.addPass(InferFunctionAttrsPass());
388
389   // Create an early function pass manager to cleanup the output of the
390   // frontend.
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)));
398
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());
404
405   // Optimize globals to try and fold them into constants.
406   MPM.addPass(GlobalOptPass());
407
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
412   // constants.
413   MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
414
415   // Remove any dead arguments exposed by cleanups and constand folding
416   // globals.
417   MPM.addPass(DeadArgumentEliminationPass());
418
419   // Create a small function pass pipeline to cleanup after all the global
420   // optimizations.
421   FunctionPassManager GlobalCleanupPM(DebugLogging);
422   GlobalCleanupPM.addPass(InstCombinePass());
423   GlobalCleanupPM.addPass(SimplifyCFGPass());
424   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
425
426   // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
427 #if 0
428   MPM.addPass(RequireAnalysisPass<GlobalsAA>());
429 #endif
430
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);
436
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
440   // invoke or a call.
441
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());
447
448   // Now deduce any function attributes based in the current code.
449   MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
450
451   // Lastly, add the core function simplification pipeline nested inside the
452   // CGSCC walk.
453   MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
454       buildFunctionSimplificationPipeline(Level, DebugLogging)));
455
456   MPM.addPass(
457       createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
458
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.
462
463   // Eliminate externally available functions now that inlining is over -- we
464   // won't emit these anyways.
465   MPM.addPass(EliminateAvailableExternallyPass());
466
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());
471
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.
480 #if 0
481   MPM.addPass(Require<GlobalsAA>());
482 #endif
483
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.
488
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
491   // function passes.
492   OptimizePM.addPass(LoopDistributePass());
493 #if 0
494   // FIXME: LoopVectorize relies on "requiring" LCSSA which isn't supported in
495   // the new PM.
496   OptimizePM.addPass(LoopVectorizePass());
497 #endif
498   // FIXME: Need to port Loop Load Elimination and add it here.
499   OptimizePM.addPass(InstCombinePass());
500
501   // Optimize parallel scalar instruction chains into SIMD instructions.
502   OptimizePM.addPass(SLPVectorizerPass());
503
504   // Cleanup after vectorizers.
505   OptimizePM.addPass(SimplifyCFGPass());
506   OptimizePM.addPass(InstCombinePass());
507
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.
511
512   // FIXME: Add the loop sink pass when ported.
513
514   // FIXME: Add cleanup from the loop pass manager when we're forming LCSSA
515   // here.
516
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());
520
521   // ADd the core optimizing pipeline.
522   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
523
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
527   // ordering here.
528   MPM.addPass(GlobalDCEPass());
529   MPM.addPass(ConstantMergePass());
530
531   return MPM;
532 }
533
534 ModulePassManager
535 PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
536                                             bool DebugLogging) {
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);
540 }
541
542 ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
543                                                        bool DebugLogging) {
544   assert(Level != O0 && "Must request optimizations for the default pipeline!");
545   ModulePassManager MPM(DebugLogging);
546
547   // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
548   FunctionPassManager LateFPM(DebugLogging);
549   LateFPM.addPass(InstCombinePass());
550   LateFPM.addPass(SimplifyCFGPass());
551
552   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
553
554   return MPM;
555 }
556
557 AAManager PassBuilder::buildDefaultAAPipeline() {
558   AAManager AA;
559
560   // The order in which these are registered determines their priority when
561   // being queried.
562
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
565   // AA techniques.
566   AA.registerFunctionAnalysis<BasicAA>();
567
568   // Next we query fast, specialized alias analyses that wrap IR-embedded
569   // information about aliasing.
570   AA.registerFunctionAnalysis<ScopedNoAliasAA>();
571   AA.registerFunctionAnalysis<TypeBasedAA>();
572
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..
577 #if 0
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>();
581 #endif
582
583   return AA;
584 }
585
586 static Optional<int> parseRepeatPassName(StringRef Name) {
587   if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
588     return None;
589   int Count;
590   if (Name.getAsInteger(0, Count) || Count <= 0)
591     return None;
592   return Count;
593 }
594
595 static Optional<int> parseDevirtPassName(StringRef Name) {
596   if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
597     return None;
598   int Count;
599   if (Name.getAsInteger(0, Count) || Count <= 0)
600     return None;
601   return Count;
602 }
603
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);
608
609   // Explicitly handle pass manager names.
610   if (Name == "module")
611     return true;
612   if (Name == "cgscc")
613     return true;
614   if (Name == "function")
615     return true;
616
617   // Explicitly handle custom-parsed pass names.
618   if (parseRepeatPassName(Name))
619     return true;
620
621 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
622   if (Name == NAME)                                                            \
623     return true;
624 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
625   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
626     return true;
627 #include "PassRegistry.def"
628
629   return false;
630 }
631
632 static bool isCGSCCPassName(StringRef Name) {
633   // Explicitly handle pass manager names.
634   if (Name == "cgscc")
635     return true;
636   if (Name == "function")
637     return true;
638
639   // Explicitly handle custom-parsed pass names.
640   if (parseRepeatPassName(Name))
641     return true;
642   if (parseDevirtPassName(Name))
643     return true;
644
645 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
646   if (Name == NAME)                                                            \
647     return true;
648 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
649   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
650     return true;
651 #include "PassRegistry.def"
652
653   return false;
654 }
655
656 static bool isFunctionPassName(StringRef Name) {
657   // Explicitly handle pass manager names.
658   if (Name == "function")
659     return true;
660   if (Name == "loop")
661     return true;
662
663   // Explicitly handle custom-parsed pass names.
664   if (parseRepeatPassName(Name))
665     return true;
666
667 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
668   if (Name == NAME)                                                            \
669     return true;
670 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
671   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
672     return true;
673 #include "PassRegistry.def"
674
675   return false;
676 }
677
678 static bool isLoopPassName(StringRef Name) {
679   // Explicitly handle pass manager names.
680   if (Name == "loop")
681     return true;
682
683   // Explicitly handle custom-parsed pass names.
684   if (parseRepeatPassName(Name))
685     return true;
686
687 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
688   if (Name == NAME)                                                            \
689     return true;
690 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
691   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
692     return true;
693 #include "PassRegistry.def"
694
695   return false;
696 }
697
698 Optional<std::vector<PassBuilder::PipelineElement>>
699 PassBuilder::parsePipelineText(StringRef Text) {
700   std::vector<PipelineElement> ResultPipeline;
701
702   SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
703       &ResultPipeline};
704   for (;;) {
705     std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
706     size_t Pos = Text.find_first_of(",()");
707     Pipeline.push_back({Text.substr(0, Pos), {}});
708
709     // If we have a single terminating name, we're done.
710     if (Pos == Text.npos)
711       break;
712
713     char Sep = Text[Pos];
714     Text = Text.substr(Pos + 1);
715     if (Sep == ',')
716       // Just a name ending in a comma, continue.
717       continue;
718
719     if (Sep == '(') {
720       // Push the inner pipeline onto the stack to continue processing.
721       PipelineStack.push_back(&Pipeline.back().InnerPipeline);
722       continue;
723     }
724
725     assert(Sep == ')' && "Bogus separator!");
726     // When handling the close parenthesis, we greedily consume them to avoid
727     // empty strings in the pipeline.
728     do {
729       // If we try to pop the outer pipeline we have unbalanced parentheses.
730       if (PipelineStack.size() == 1)
731         return None;
732
733       PipelineStack.pop_back();
734     } while (Text.consume_front(")"));
735
736     // Check if we've finished parsing.
737     if (Text.empty())
738       break;
739
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(","))
743       return None;
744   }
745
746   if (PipelineStack.size() > 1)
747     // Unbalanced paretheses.
748     return None;
749
750   assert(PipelineStack.back() == &ResultPipeline &&
751          "Wrong pipeline at the bottom of the stack!");
752   return {std::move(ResultPipeline)};
753 }
754
755 bool PassBuilder::parseModulePass(ModulePassManager &MPM,
756                                   const PipelineElement &E, bool VerifyEachPass,
757                                   bool DebugLogging) {
758   auto &Name = E.Name;
759   auto &InnerPipeline = E.InnerPipeline;
760
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,
766                                    DebugLogging))
767         return false;
768       MPM.addPass(std::move(NestedMPM));
769       return true;
770     }
771     if (Name == "cgscc") {
772       CGSCCPassManager CGPM(DebugLogging);
773       if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
774                                   DebugLogging))
775         return false;
776       MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
777                                                           DebugLogging));
778       return true;
779     }
780     if (Name == "function") {
781       FunctionPassManager FPM(DebugLogging);
782       if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
783                                      DebugLogging))
784         return false;
785       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
786       return true;
787     }
788     if (auto Count = parseRepeatPassName(Name)) {
789       ModulePassManager NestedMPM(DebugLogging);
790       if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
791                                    DebugLogging))
792         return false;
793       MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
794       return true;
795     }
796     // Normal passes can't have pipelines.
797     return false;
798   }
799
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))
804       return false;
805     assert(Matches.size() == 3 && "Must capture two matched strings!");
806
807     OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
808         .Case("O0", O0)
809         .Case("O1", O1)
810         .Case("O2", O2)
811         .Case("O3", O3)
812         .Case("Os", Os)
813         .Case("Oz", Oz);
814     if (L == O0)
815       // At O0 we do nothing at all!
816       return true;
817
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));
822     } else {
823       assert(Matches[1] == "lto" && "Not one of the matched options!");
824       MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
825     }
826     return true;
827   }
828
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);                                                  \
833     return true;                                                               \
834   }
835 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
836   if (Name == "require<" NAME ">") {                                           \
837     MPM.addPass(                                                               \
838         RequireAnalysisPass<                                                   \
839             std::remove_reference<decltype(CREATE_PASS)>::type, Module>());    \
840     return true;                                                               \
841   }                                                                            \
842   if (Name == "invalidate<" NAME ">") {                                        \
843     MPM.addPass(InvalidateAnalysisPass<                                        \
844                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
845     return true;                                                               \
846   }
847 #include "PassRegistry.def"
848
849   return false;
850 }
851
852 bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
853                                  const PipelineElement &E, bool VerifyEachPass,
854                                  bool DebugLogging) {
855   auto &Name = E.Name;
856   auto &InnerPipeline = E.InnerPipeline;
857
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,
863                                   DebugLogging))
864         return false;
865       // Add the nested pass manager with the appropriate adaptor.
866       CGPM.addPass(std::move(NestedCGPM));
867       return true;
868     }
869     if (Name == "function") {
870       FunctionPassManager FPM(DebugLogging);
871       if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
872                                      DebugLogging))
873         return false;
874       // Add the nested pass manager with the appropriate adaptor.
875       CGPM.addPass(
876           createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
877       return true;
878     }
879     if (auto Count = parseRepeatPassName(Name)) {
880       CGSCCPassManager NestedCGPM(DebugLogging);
881       if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
882                                   DebugLogging))
883         return false;
884       CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
885       return true;
886     }
887     if (auto MaxRepetitions = parseDevirtPassName(Name)) {
888       CGSCCPassManager NestedCGPM(DebugLogging);
889       if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
890                                   DebugLogging))
891         return false;
892       CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
893                                                *MaxRepetitions, DebugLogging));
894       return true;
895     }
896     // Normal passes can't have pipelines.
897     return false;
898   }
899
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);                                                 \
904     return true;                                                               \
905   }
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 &>());                                      \
912     return true;                                                               \
913   }                                                                            \
914   if (Name == "invalidate<" NAME ">") {                                        \
915     CGPM.addPass(InvalidateAnalysisPass<                                       \
916                  std::remove_reference<decltype(CREATE_PASS)>::type>());       \
917     return true;                                                               \
918   }
919 #include "PassRegistry.def"
920
921   return false;
922 }
923
924 bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
925                                     const PipelineElement &E,
926                                     bool VerifyEachPass, bool DebugLogging) {
927   auto &Name = E.Name;
928   auto &InnerPipeline = E.InnerPipeline;
929
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,
935                                      DebugLogging))
936         return false;
937       // Add the nested pass manager with the appropriate adaptor.
938       FPM.addPass(std::move(NestedFPM));
939       return true;
940     }
941     if (Name == "loop") {
942       LoopPassManager LPM(DebugLogging);
943       if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
944                                  DebugLogging))
945         return false;
946       // Add the nested pass manager with the appropriate adaptor.
947       FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
948       return true;
949     }
950     if (auto Count = parseRepeatPassName(Name)) {
951       FunctionPassManager NestedFPM(DebugLogging);
952       if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
953                                      DebugLogging))
954         return false;
955       FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
956       return true;
957     }
958     // Normal passes can't have pipelines.
959     return false;
960   }
961
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);                                                  \
966     return true;                                                               \
967   }
968 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
969   if (Name == "require<" NAME ">") {                                           \
970     FPM.addPass(                                                               \
971         RequireAnalysisPass<                                                   \
972             std::remove_reference<decltype(CREATE_PASS)>::type, Function>());  \
973     return true;                                                               \
974   }                                                                            \
975   if (Name == "invalidate<" NAME ">") {                                        \
976     FPM.addPass(InvalidateAnalysisPass<                                        \
977                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
978     return true;                                                               \
979   }
980 #include "PassRegistry.def"
981
982   return false;
983 }
984
985 bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
986                                 bool VerifyEachPass, bool DebugLogging) {
987   StringRef Name = E.Name;
988   auto &InnerPipeline = E.InnerPipeline;
989
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,
995                                  DebugLogging))
996         return false;
997       // Add the nested pass manager with the appropriate adaptor.
998       LPM.addPass(std::move(NestedLPM));
999       return true;
1000     }
1001     if (auto Count = parseRepeatPassName(Name)) {
1002       LoopPassManager NestedLPM(DebugLogging);
1003       if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1004                                  DebugLogging))
1005         return false;
1006       LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
1007       return true;
1008     }
1009     // Normal passes can't have pipelines.
1010     return false;
1011   }
1012
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);                                                  \
1017     return true;                                                               \
1018   }
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>());  \
1023     return true;                                                               \
1024   }                                                                            \
1025   if (Name == "invalidate<" NAME ">") {                                        \
1026     LPM.addPass(InvalidateAnalysisPass<                                        \
1027                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
1028     return true;                                                               \
1029   }
1030 #include "PassRegistry.def"
1031
1032   return false;
1033 }
1034
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>();                 \
1040     return true;                                                               \
1041   }
1042 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS)                             \
1043   if (Name == NAME) {                                                          \
1044     AA.registerFunctionAnalysis<                                               \
1045         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
1046     return true;                                                               \
1047   }
1048 #include "PassRegistry.def"
1049
1050   return false;
1051 }
1052
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))
1059       return false;
1060     // FIXME: No verifier support for Loop passes!
1061   }
1062   return true;
1063 }
1064
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))
1071       return false;
1072     if (VerifyEachPass)
1073       FPM.addPass(VerifierPass());
1074   }
1075   return true;
1076 }
1077
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))
1084       return false;
1085     // FIXME: No verifier support for CGSCC passes!
1086   }
1087   return true;
1088 }
1089
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); });
1101 }
1102
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))
1109       return false;
1110     if (VerifyEachPass)
1111       MPM.addPass(VerifierPass());
1112   }
1113   return true;
1114 }
1115
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())
1124     return false;
1125
1126   // If the first name isn't at the module layer, wrap the pipeline up
1127   // automatically.
1128   StringRef FirstName = Pipeline->front().Name;
1129
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)}}}};
1137     else
1138       // Unknown pass name!
1139       return false;
1140   }
1141
1142   return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
1143 }
1144
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();
1150     return true;
1151   }
1152
1153   while (!PipelineText.empty()) {
1154     StringRef Name;
1155     std::tie(Name, PipelineText) = PipelineText.split(',');
1156     if (!parseAAPassName(AA, Name))
1157       return false;
1158   }
1159
1160   return true;
1161 }