1 //===- Parsing, selection, and construction of pass pipelines -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file provides the implementation of the PassBuilder based on our
11 /// static pass registry as well as related functionality. It also provides
12 /// helpers to aid in analyzing, debugging, and testing passes and pass
13 /// pipelines.
14 ///
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/Passes/PassBuilder.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/BasicAliasAnalysis.h"
23 #include "llvm/Analysis/BlockFrequencyInfo.h"
24 #include "llvm/Analysis/BranchProbabilityInfo.h"
25 #include "llvm/Analysis/CFGPrinter.h"
26 #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
27 #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
28 #include "llvm/Analysis/CGSCCPassManager.h"
29 #include "llvm/Analysis/CallGraph.h"
30 #include "llvm/Analysis/DemandedBits.h"
31 #include "llvm/Analysis/DependenceAnalysis.h"
32 #include "llvm/Analysis/DominanceFrontier.h"
33 #include "llvm/Analysis/GlobalsModRef.h"
34 #include "llvm/Analysis/IVUsers.h"
35 #include "llvm/Analysis/LazyCallGraph.h"
36 #include "llvm/Analysis/LazyValueInfo.h"
37 #include "llvm/Analysis/LoopAccessAnalysis.h"
38 #include "llvm/Analysis/LoopInfo.h"
39 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
40 #include "llvm/Analysis/MemorySSA.h"
41 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
42 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
43 #include "llvm/Analysis/PhiValues.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/StackSafetyAnalysis.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/SafepointIRVerifier.h"
60 #include "llvm/IR/Verifier.h"
61 #include "llvm/Support/Debug.h"
62 #include "llvm/Support/FormatVariadic.h"
63 #include "llvm/Support/Regex.h"
64 #include "llvm/Target/TargetMachine.h"
65 #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
66 #include "llvm/Transforms/IPO/AlwaysInliner.h"
67 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
68 #include "llvm/Transforms/IPO/Attributor.h"
69 #include "llvm/Transforms/IPO/CalledValuePropagation.h"
70 #include "llvm/Transforms/IPO/ConstantMerge.h"
71 #include "llvm/Transforms/IPO/CrossDSOCFI.h"
72 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
73 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
74 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
75 #include "llvm/Transforms/IPO/FunctionAttrs.h"
76 #include "llvm/Transforms/IPO/FunctionImport.h"
77 #include "llvm/Transforms/IPO/GlobalDCE.h"
78 #include "llvm/Transforms/IPO/GlobalOpt.h"
79 #include "llvm/Transforms/IPO/GlobalSplit.h"
80 #include "llvm/Transforms/IPO/HotColdSplitting.h"
81 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
82 #include "llvm/Transforms/IPO/Inliner.h"
83 #include "llvm/Transforms/IPO/Internalize.h"
84 #include "llvm/Transforms/IPO/LowerTypeTests.h"
85 #include "llvm/Transforms/IPO/PartialInlining.h"
86 #include "llvm/Transforms/IPO/SCCP.h"
87 #include "llvm/Transforms/IPO/SampleProfile.h"
88 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
89 #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
90 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
91 #include "llvm/Transforms/InstCombine/InstCombine.h"
92 #include "llvm/Transforms/Instrumentation.h"
93 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
94 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
95 #include "llvm/Transforms/Instrumentation/CGProfile.h"
96 #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
97 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
98 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
99 #include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
100 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
101 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
102 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
103 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
104 #include "llvm/Transforms/Scalar/ADCE.h"
105 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
106 #include "llvm/Transforms/Scalar/BDCE.h"
107 #include "llvm/Transforms/Scalar/CallSiteSplitting.h"
108 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
109 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
110 #include "llvm/Transforms/Scalar/DCE.h"
111 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
112 #include "llvm/Transforms/Scalar/DivRemPairs.h"
113 #include "llvm/Transforms/Scalar/EarlyCSE.h"
114 #include "llvm/Transforms/Scalar/Float2Int.h"
115 #include "llvm/Transforms/Scalar/GVN.h"
116 #include "llvm/Transforms/Scalar/GuardWidening.h"
117 #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
118 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
119 #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
120 #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
121 #include "llvm/Transforms/Scalar/JumpThreading.h"
122 #include "llvm/Transforms/Scalar/LICM.h"
123 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
124 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
125 #include "llvm/Transforms/Scalar/LoopDeletion.h"
126 #include "llvm/Transforms/Scalar/LoopDistribute.h"
127 #include "llvm/Transforms/Scalar/LoopFuse.h"
128 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
129 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
130 #include "llvm/Transforms/Scalar/LoopLoadElimination.h"
131 #include "llvm/Transforms/Scalar/LoopPassManager.h"
132 #include "llvm/Transforms/Scalar/LoopPredication.h"
133 #include "llvm/Transforms/Scalar/LoopRotation.h"
134 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
135 #include "llvm/Transforms/Scalar/LoopSink.h"
136 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
137 #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
138 #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
139 #include "llvm/Transforms/Scalar/LowerAtomic.h"
140 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
141 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
142 #include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
143 #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
144 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
145 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
146 #include "llvm/Transforms/Scalar/MergeICmps.h"
147 #include "llvm/Transforms/Scalar/NaryReassociate.h"
148 #include "llvm/Transforms/Scalar/NewGVN.h"
149 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
150 #include "llvm/Transforms/Scalar/Reassociate.h"
151 #include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
152 #include "llvm/Transforms/Scalar/SCCP.h"
153 #include "llvm/Transforms/Scalar/SROA.h"
154 #include "llvm/Transforms/Scalar/Scalarizer.h"
155 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
156 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
157 #include "llvm/Transforms/Scalar/Sink.h"
158 #include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
159 #include "llvm/Transforms/Scalar/SpeculativeExecution.h"
160 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
161 #include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
162 #include "llvm/Transforms/Utils/AddDiscriminators.h"
163 #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
164 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
165 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
166 #include "llvm/Transforms/Utils/LCSSA.h"
167 #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
168 #include "llvm/Transforms/Utils/LoopSimplify.h"
169 #include "llvm/Transforms/Utils/LowerInvoke.h"
170 #include "llvm/Transforms/Utils/Mem2Reg.h"
171 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
172 #include "llvm/Transforms/Utils/SymbolRewriter.h"
173 #include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h"
174 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
175 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
176 
177 using namespace llvm;
178 
179 static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
180                                              cl::ReallyHidden, cl::init(4));
181 static cl::opt<bool>
182     RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
183                        cl::Hidden, cl::ZeroOrMore,
184                        cl::desc("Run Partial inlinining pass"));
185 
186 static cl::opt<bool>
187     RunNewGVN("enable-npm-newgvn", cl::init(false),
188               cl::Hidden, cl::ZeroOrMore,
189               cl::desc("Run NewGVN instead of GVN"));
190 
191 static cl::opt<bool> EnableGVNHoist(
192     "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
193     cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
194 
195 static cl::opt<bool> EnableGVNSink(
196     "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
197     cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
198 
199 static cl::opt<bool> EnableUnrollAndJam(
200     "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
201     cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
202 
203 static cl::opt<bool> EnableSyntheticCounts(
204     "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
205     cl::desc("Run synthetic function entry count generation "
206              "pass"));
207 
208 static Regex DefaultAliasRegex(
209     "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
210 
211 // This option is used in simplifying testing SampleFDO optimizations for
212 // profile loading.
213 static cl::opt<bool>
214     EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden,
215               cl::desc("Enable control height reduction optimization (CHR)"));
216 
217 PipelineTuningOptions::PipelineTuningOptions() {
218   LoopInterleaving = EnableLoopInterleaving;
219   LoopVectorization = EnableLoopVectorization;
220   SLPVectorization = RunSLPVectorization;
221   LoopUnrolling = true;
222   ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll;
223   LicmMssaOptCap = SetLicmMssaOptCap;
224   LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap;
225 }
226 
227 extern cl::opt<bool> EnableHotColdSplit;
228 extern cl::opt<bool> EnableOrderFileInstrumentation;
229 
230 extern cl::opt<bool> FlattenedProfileUsed;
231 
232 static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
233   switch (Level) {
234   case PassBuilder::O0:
235   case PassBuilder::O1:
236   case PassBuilder::O2:
237   case PassBuilder::O3:
238     return false;
239 
240   case PassBuilder::Os:
241   case PassBuilder::Oz:
242     return true;
243   }
244   llvm_unreachable("Invalid optimization level!");
245 }
246 
247 namespace {
248 
249 /// No-op module pass which does nothing.
250 struct NoOpModulePass {
251   PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
252     return PreservedAnalyses::all();
253   }
254   static StringRef name() { return "NoOpModulePass"; }
255 };
256 
257 /// No-op module analysis.
258 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
259   friend AnalysisInfoMixin<NoOpModuleAnalysis>;
260   static AnalysisKey Key;
261 
262 public:
263   struct Result {};
264   Result run(Module &, ModuleAnalysisManager &) { return Result(); }
265   static StringRef name() { return "NoOpModuleAnalysis"; }
266 };
267 
268 /// No-op CGSCC pass which does nothing.
269 struct NoOpCGSCCPass {
270   PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
271                         LazyCallGraph &, CGSCCUpdateResult &UR) {
272     return PreservedAnalyses::all();
273   }
274   static StringRef name() { return "NoOpCGSCCPass"; }
275 };
276 
277 /// No-op CGSCC analysis.
278 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
279   friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
280   static AnalysisKey Key;
281 
282 public:
283   struct Result {};
284   Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
285     return Result();
286   }
287   static StringRef name() { return "NoOpCGSCCAnalysis"; }
288 };
289 
290 /// No-op function pass which does nothing.
291 struct NoOpFunctionPass {
292   PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
293     return PreservedAnalyses::all();
294   }
295   static StringRef name() { return "NoOpFunctionPass"; }
296 };
297 
298 /// No-op function analysis.
299 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
300   friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
301   static AnalysisKey Key;
302 
303 public:
304   struct Result {};
305   Result run(Function &, FunctionAnalysisManager &) { return Result(); }
306   static StringRef name() { return "NoOpFunctionAnalysis"; }
307 };
308 
309 /// No-op loop pass which does nothing.
310 struct NoOpLoopPass {
311   PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
312                         LoopStandardAnalysisResults &, LPMUpdater &) {
313     return PreservedAnalyses::all();
314   }
315   static StringRef name() { return "NoOpLoopPass"; }
316 };
317 
318 /// No-op loop analysis.
319 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
320   friend AnalysisInfoMixin<NoOpLoopAnalysis>;
321   static AnalysisKey Key;
322 
323 public:
324   struct Result {};
325   Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
326     return Result();
327   }
328   static StringRef name() { return "NoOpLoopAnalysis"; }
329 };
330 
331 AnalysisKey NoOpModuleAnalysis::Key;
332 AnalysisKey NoOpCGSCCAnalysis::Key;
333 AnalysisKey NoOpFunctionAnalysis::Key;
334 AnalysisKey NoOpLoopAnalysis::Key;
335 
336 } // End anonymous namespace.
337 
338 void PassBuilder::invokePeepholeEPCallbacks(
339     FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
340   for (auto &C : PeepholeEPCallbacks)
341     C(FPM, Level);
342 }
343 
344 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
345 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
346   MAM.registerPass([&] { return CREATE_PASS; });
347 #include "PassRegistry.def"
348 
349   for (auto &C : ModuleAnalysisRegistrationCallbacks)
350     C(MAM);
351 }
352 
353 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
354 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
355   CGAM.registerPass([&] { return CREATE_PASS; });
356 #include "PassRegistry.def"
357 
358   for (auto &C : CGSCCAnalysisRegistrationCallbacks)
359     C(CGAM);
360 }
361 
362 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
363 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
364   FAM.registerPass([&] { return CREATE_PASS; });
365 #include "PassRegistry.def"
366 
367   for (auto &C : FunctionAnalysisRegistrationCallbacks)
368     C(FAM);
369 }
370 
371 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
372 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
373   LAM.registerPass([&] { return CREATE_PASS; });
374 #include "PassRegistry.def"
375 
376   for (auto &C : LoopAnalysisRegistrationCallbacks)
377     C(LAM);
378 }
379 
380 FunctionPassManager
381 PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
382                                                  ThinLTOPhase Phase,
383                                                  bool DebugLogging) {
384   assert(Level != O0 && "Must request optimizations!");
385   FunctionPassManager FPM(DebugLogging);
386 
387   // Form SSA out of local memory accesses after breaking apart aggregates into
388   // scalars.
389   FPM.addPass(SROA());
390 
391   // Catch trivial redundancies
392   FPM.addPass(EarlyCSEPass(true /* Enable mem-ssa. */));
393 
394   // Hoisting of scalars and load expressions.
395   if (EnableGVNHoist)
396     FPM.addPass(GVNHoistPass());
397 
398   // Global value numbering based sinking.
399   if (EnableGVNSink) {
400     FPM.addPass(GVNSinkPass());
401     FPM.addPass(SimplifyCFGPass());
402   }
403 
404   // Speculative execution if the target has divergent branches; otherwise nop.
405   FPM.addPass(SpeculativeExecutionPass());
406 
407   // Optimize based on known information about branches, and cleanup afterward.
408   FPM.addPass(JumpThreadingPass());
409   FPM.addPass(CorrelatedValuePropagationPass());
410   FPM.addPass(SimplifyCFGPass());
411   if (Level == O3)
412     FPM.addPass(AggressiveInstCombinePass());
413   FPM.addPass(InstCombinePass());
414 
415   if (!isOptimizingForSize(Level))
416     FPM.addPass(LibCallsShrinkWrapPass());
417 
418   invokePeepholeEPCallbacks(FPM, Level);
419 
420   // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
421   // using the size value profile. Don't perform this when optimizing for size.
422   if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse &&
423       !isOptimizingForSize(Level))
424     FPM.addPass(PGOMemOPSizeOpt());
425 
426   FPM.addPass(TailCallElimPass());
427   FPM.addPass(SimplifyCFGPass());
428 
429   // Form canonically associated expression trees, and simplify the trees using
430   // basic mathematical properties. For example, this will form (nearly)
431   // minimal multiplication trees.
432   FPM.addPass(ReassociatePass());
433 
434   // Add the primary loop simplification pipeline.
435   // FIXME: Currently this is split into two loop pass pipelines because we run
436   // some function passes in between them. These can and should be removed
437   // and/or replaced by scheduling the loop pass equivalents in the correct
438   // positions. But those equivalent passes aren't powerful enough yet.
439   // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
440   // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
441   // fully replace `SimplifyCFGPass`, and the closest to the other we have is
442   // `LoopInstSimplify`.
443   LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
444 
445   // Simplify the loop body. We do this initially to clean up after other loop
446   // passes run, either when iterating on a loop or on inner loops with
447   // implications on the outer loop.
448   LPM1.addPass(LoopInstSimplifyPass());
449   LPM1.addPass(LoopSimplifyCFGPass());
450 
451   // Rotate Loop - disable header duplication at -Oz
452   LPM1.addPass(LoopRotatePass(Level != Oz));
453   LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap));
454   LPM1.addPass(SimpleLoopUnswitchPass());
455   LPM2.addPass(IndVarSimplifyPass());
456   LPM2.addPass(LoopIdiomRecognizePass());
457 
458   for (auto &C : LateLoopOptimizationsEPCallbacks)
459     C(LPM2, Level);
460 
461   LPM2.addPass(LoopDeletionPass());
462   // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
463   // because it changes IR to makes profile annotation in back compile
464   // inaccurate.
465   if ((Phase != ThinLTOPhase::PreLink || !PGOOpt ||
466        PGOOpt->Action != PGOOptions::SampleUse) &&
467       PTO.LoopUnrolling)
468     LPM2.addPass(
469         LoopFullUnrollPass(Level, false, PTO.ForgetAllSCEVInLoopUnroll));
470 
471   for (auto &C : LoopOptimizerEndEPCallbacks)
472     C(LPM2, Level);
473 
474   // We provide the opt remark emitter pass for LICM to use. We only need to do
475   // this once as it is immutable.
476   FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
477   FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
478   FPM.addPass(SimplifyCFGPass());
479   FPM.addPass(InstCombinePass());
480   FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
481 
482   // Eliminate redundancies.
483   if (Level != O1) {
484     // These passes add substantial compile time so skip them at O1.
485     FPM.addPass(MergedLoadStoreMotionPass());
486     if (RunNewGVN)
487       FPM.addPass(NewGVNPass());
488     else
489       FPM.addPass(GVN());
490   }
491 
492   // Specially optimize memory movement as it doesn't look like dataflow in SSA.
493   FPM.addPass(MemCpyOptPass());
494 
495   // Sparse conditional constant propagation.
496   // FIXME: It isn't clear why we do this *after* loop passes rather than
497   // before...
498   FPM.addPass(SCCPPass());
499 
500   // Delete dead bit computations (instcombine runs after to fold away the dead
501   // computations, and then ADCE will run later to exploit any new DCE
502   // opportunities that creates).
503   FPM.addPass(BDCEPass());
504 
505   // Run instcombine after redundancy and dead bit elimination to exploit
506   // opportunities opened up by them.
507   FPM.addPass(InstCombinePass());
508   invokePeepholeEPCallbacks(FPM, Level);
509 
510   // Re-consider control flow based optimizations after redundancy elimination,
511   // redo DCE, etc.
512   FPM.addPass(JumpThreadingPass());
513   FPM.addPass(CorrelatedValuePropagationPass());
514   FPM.addPass(DSEPass());
515   FPM.addPass(createFunctionToLoopPassAdaptor(
516       LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap),
517       DebugLogging));
518 
519   for (auto &C : ScalarOptimizerLateEPCallbacks)
520     C(FPM, Level);
521 
522   // Finally, do an expensive DCE pass to catch all the dead code exposed by
523   // the simplifications and basic cleanup after all the simplifications.
524   FPM.addPass(ADCEPass());
525   FPM.addPass(SimplifyCFGPass());
526   FPM.addPass(InstCombinePass());
527   invokePeepholeEPCallbacks(FPM, Level);
528 
529   if (EnableCHR && Level == O3 && PGOOpt &&
530       (PGOOpt->Action == PGOOptions::IRUse ||
531        PGOOpt->Action == PGOOptions::SampleUse))
532     FPM.addPass(ControlHeightReductionPass());
533 
534   return FPM;
535 }
536 
537 void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
538                                     PassBuilder::OptimizationLevel Level,
539                                     bool RunProfileGen, bool IsCS,
540                                     std::string ProfileFile,
541                                     std::string ProfileRemappingFile) {
542   // Generally running simplification passes and the inliner with an high
543   // threshold results in smaller executables, but there may be cases where
544   // the size grows, so let's be conservative here and skip this simplification
545   // at -Os/Oz. We will not do this  inline for context sensistive PGO (when
546   // IsCS is true).
547   if (!isOptimizingForSize(Level) && !IsCS) {
548     InlineParams IP;
549 
550     // In the old pass manager, this is a cl::opt. Should still this be one?
551     IP.DefaultThreshold = 75;
552 
553     // FIXME: The hint threshold has the same value used by the regular inliner.
554     // This should probably be lowered after performance testing.
555     // FIXME: this comment is cargo culted from the old pass manager, revisit).
556     IP.HintThreshold = 325;
557 
558     CGSCCPassManager CGPipeline(DebugLogging);
559 
560     CGPipeline.addPass(InlinerPass(IP));
561 
562     FunctionPassManager FPM;
563     FPM.addPass(SROA());
564     FPM.addPass(EarlyCSEPass());    // Catch trivial redundancies.
565     FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
566     FPM.addPass(InstCombinePass()); // Combine silly sequences.
567     invokePeepholeEPCallbacks(FPM, Level);
568 
569     CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
570 
571     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
572   }
573 
574   // Delete anything that is now dead to make sure that we don't instrument
575   // dead code. Instrumentation can end up keeping dead code around and
576   // dramatically increase code size.
577   MPM.addPass(GlobalDCEPass());
578 
579   if (RunProfileGen) {
580     MPM.addPass(PGOInstrumentationGen(IsCS));
581 
582     FunctionPassManager FPM;
583     FPM.addPass(
584         createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
585     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
586 
587     // Add the profile lowering pass.
588     InstrProfOptions Options;
589     if (!ProfileFile.empty())
590       Options.InstrProfileOutput = ProfileFile;
591     Options.DoCounterPromotion = true;
592     Options.UseBFIInPromotion = IsCS;
593     MPM.addPass(InstrProfiling(Options, IsCS));
594   } else if (!ProfileFile.empty()) {
595     MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS));
596     // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
597     // RequireAnalysisPass for PSI before subsequent non-module passes.
598     MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
599   }
600 }
601 
602 static InlineParams
603 getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
604   auto O3 = PassBuilder::O3;
605   unsigned OptLevel = Level > O3 ? 2 : Level;
606   unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
607   return getInlineParams(OptLevel, SizeLevel);
608 }
609 
610 ModulePassManager
611 PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
612                                                ThinLTOPhase Phase,
613                                                bool DebugLogging) {
614   ModulePassManager MPM(DebugLogging);
615 
616   bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse);
617 
618   // In ThinLTO mode, when flattened profile is used, all the available
619   // profile information will be annotated in PreLink phase so there is
620   // no need to load the profile again in PostLink.
621   bool LoadSampleProfile =
622       HasSampleProfile &&
623       !(FlattenedProfileUsed && Phase == ThinLTOPhase::PostLink);
624 
625   // During the ThinLTO backend phase we perform early indirect call promotion
626   // here, before globalopt. Otherwise imported available_externally functions
627   // look unreferenced and are removed. If we are going to load the sample
628   // profile then defer until later.
629   // TODO: See if we can move later and consolidate with the location where
630   // we perform ICP when we are loading a sample profile.
631   // TODO: We pass HasSampleProfile (whether there was a sample profile file
632   // passed to the compile) to the SamplePGO flag of ICP. This is used to
633   // determine whether the new direct calls are annotated with prof metadata.
634   // Ideally this should be determined from whether the IR is annotated with
635   // sample profile, and not whether the a sample profile was provided on the
636   // command line. E.g. for flattened profiles where we will not be reloading
637   // the sample profile in the ThinLTO backend, we ideally shouldn't have to
638   // provide the sample profile file.
639   if (Phase == ThinLTOPhase::PostLink && !LoadSampleProfile)
640     MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile));
641 
642   // Do basic inference of function attributes from known properties of system
643   // libraries and other oracles.
644   MPM.addPass(InferFunctionAttrsPass());
645 
646   // Create an early function pass manager to cleanup the output of the
647   // frontend.
648   FunctionPassManager EarlyFPM(DebugLogging);
649   EarlyFPM.addPass(SimplifyCFGPass());
650   EarlyFPM.addPass(SROA());
651   EarlyFPM.addPass(EarlyCSEPass());
652   EarlyFPM.addPass(LowerExpectIntrinsicPass());
653   if (Level == O3)
654     EarlyFPM.addPass(CallSiteSplittingPass());
655 
656   // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
657   // to convert bitcast to direct calls so that they can be inlined during the
658   // profile annotation prepration step.
659   // More details about SamplePGO design can be found in:
660   // https://research.google.com/pubs/pub45290.html
661   // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
662   if (LoadSampleProfile)
663     EarlyFPM.addPass(InstCombinePass());
664   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
665 
666   if (LoadSampleProfile) {
667     // Annotate sample profile right after early FPM to ensure freshness of
668     // the debug info.
669     MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
670                                         PGOOpt->ProfileRemappingFile,
671                                         Phase == ThinLTOPhase::PreLink));
672     // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
673     // RequireAnalysisPass for PSI before subsequent non-module passes.
674     MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
675     // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
676     // for the profile annotation to be accurate in the ThinLTO backend.
677     if (Phase != ThinLTOPhase::PreLink)
678       // We perform early indirect call promotion here, before globalopt.
679       // This is important for the ThinLTO backend phase because otherwise
680       // imported available_externally functions look unreferenced and are
681       // removed.
682       MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
683                                            true /* SamplePGO */));
684   }
685 
686   // Interprocedural constant propagation now that basic cleanup has occurred
687   // and prior to optimizing globals.
688   // FIXME: This position in the pipeline hasn't been carefully considered in
689   // years, it should be re-analyzed.
690   MPM.addPass(IPSCCPPass());
691 
692   // Attach metadata to indirect call sites indicating the set of functions
693   // they may target at run-time. This should follow IPSCCP.
694   MPM.addPass(CalledValuePropagationPass());
695 
696   // Optimize globals to try and fold them into constants.
697   MPM.addPass(GlobalOptPass());
698 
699   // Promote any localized globals to SSA registers.
700   // FIXME: Should this instead by a run of SROA?
701   // FIXME: We should probably run instcombine and simplify-cfg afterward to
702   // delete control flows that are dead once globals have been folded to
703   // constants.
704   MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
705 
706   // Remove any dead arguments exposed by cleanups and constand folding
707   // globals.
708   MPM.addPass(DeadArgumentEliminationPass());
709 
710   // Create a small function pass pipeline to cleanup after all the global
711   // optimizations.
712   FunctionPassManager GlobalCleanupPM(DebugLogging);
713   GlobalCleanupPM.addPass(InstCombinePass());
714   invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
715 
716   GlobalCleanupPM.addPass(SimplifyCFGPass());
717   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
718 
719   // Add all the requested passes for instrumentation PGO, if requested.
720   if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
721       (PGOOpt->Action == PGOOptions::IRInstr ||
722        PGOOpt->Action == PGOOptions::IRUse)) {
723     addPGOInstrPasses(MPM, DebugLogging, Level,
724                       /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr,
725                       /* IsCS */ false, PGOOpt->ProfileFile,
726                       PGOOpt->ProfileRemappingFile);
727     MPM.addPass(PGOIndirectCallPromotion(false, false));
728   }
729   if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
730       PGOOpt->CSAction == PGOOptions::CSIRInstr)
731     MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile));
732 
733   // Synthesize function entry counts for non-PGO compilation.
734   if (EnableSyntheticCounts && !PGOOpt)
735     MPM.addPass(SyntheticCountsPropagation());
736 
737   // Require the GlobalsAA analysis for the module so we can query it within
738   // the CGSCC pipeline.
739   MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
740 
741   // Require the ProfileSummaryAnalysis for the module so we can query it within
742   // the inliner pass.
743   MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
744 
745   // Now begin the main postorder CGSCC pipeline.
746   // FIXME: The current CGSCC pipeline has its origins in the legacy pass
747   // manager and trying to emulate its precise behavior. Much of this doesn't
748   // make a lot of sense and we should revisit the core CGSCC structure.
749   CGSCCPassManager MainCGPipeline(DebugLogging);
750 
751   // Note: historically, the PruneEH pass was run first to deduce nounwind and
752   // generally clean up exception handling overhead. It isn't clear this is
753   // valuable as the inliner doesn't currently care whether it is inlining an
754   // invoke or a call.
755 
756   // Run the inliner first. The theory is that we are walking bottom-up and so
757   // the callees have already been fully optimized, and we want to inline them
758   // into the callers so that our optimizations can reflect that.
759   // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
760   // because it makes profile annotation in the backend inaccurate.
761   InlineParams IP = getInlineParamsFromOptLevel(Level);
762   if (Phase == ThinLTOPhase::PreLink && PGOOpt &&
763       PGOOpt->Action == PGOOptions::SampleUse)
764     IP.HotCallSiteThreshold = 0;
765   MainCGPipeline.addPass(InlinerPass(IP));
766 
767   // Now deduce any function attributes based in the current code.
768   MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
769 
770   // When at O3 add argument promotion to the pass pipeline.
771   // FIXME: It isn't at all clear why this should be limited to O3.
772   if (Level == O3)
773     MainCGPipeline.addPass(ArgumentPromotionPass());
774 
775   // Lastly, add the core function simplification pipeline nested inside the
776   // CGSCC walk.
777   MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
778       buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
779 
780   for (auto &C : CGSCCOptimizerLateEPCallbacks)
781     C(MainCGPipeline, Level);
782 
783   // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
784   // to detect when we devirtualize indirect calls and iterate the SCC passes
785   // in that case to try and catch knock-on inlining or function attrs
786   // opportunities. Then we add it to the module pipeline by walking the SCCs
787   // in postorder (or bottom-up).
788   MPM.addPass(
789       createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
790           std::move(MainCGPipeline), MaxDevirtIterations)));
791 
792   return MPM;
793 }
794 
795 ModulePassManager PassBuilder::buildModuleOptimizationPipeline(
796     OptimizationLevel Level, bool DebugLogging, bool LTOPreLink) {
797   ModulePassManager MPM(DebugLogging);
798 
799   // Optimize globals now that the module is fully simplified.
800   MPM.addPass(GlobalOptPass());
801   MPM.addPass(GlobalDCEPass());
802 
803   // Run partial inlining pass to partially inline functions that have
804   // large bodies.
805   if (RunPartialInlining)
806     MPM.addPass(PartialInlinerPass());
807 
808   // Remove avail extern fns and globals definitions since we aren't compiling
809   // an object file for later LTO. For LTO we want to preserve these so they
810   // are eligible for inlining at link-time. Note if they are unreferenced they
811   // will be removed by GlobalDCE later, so this only impacts referenced
812   // available externally globals. Eventually they will be suppressed during
813   // codegen, but eliminating here enables more opportunity for GlobalDCE as it
814   // may make globals referenced by available external functions dead and saves
815   // running remaining passes on the eliminated functions. These should be
816   // preserved during prelinking for link-time inlining decisions.
817   if (!LTOPreLink)
818     MPM.addPass(EliminateAvailableExternallyPass());
819 
820   if (EnableOrderFileInstrumentation)
821     MPM.addPass(InstrOrderFilePass());
822 
823   // Do RPO function attribute inference across the module to forward-propagate
824   // attributes where applicable.
825   // FIXME: Is this really an optimization rather than a canonicalization?
826   MPM.addPass(ReversePostOrderFunctionAttrsPass());
827 
828   // Do a post inline PGO instrumentation and use pass. This is a context
829   // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as
830   // cross-module inline has not been done yet. The context sensitive
831   // instrumentation is after all the inlines are done.
832   if (!LTOPreLink && PGOOpt) {
833     if (PGOOpt->CSAction == PGOOptions::CSIRInstr)
834       addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true,
835                         /* IsCS */ true, PGOOpt->CSProfileGenFile,
836                         PGOOpt->ProfileRemappingFile);
837     else if (PGOOpt->CSAction == PGOOptions::CSIRUse)
838       addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false,
839                         /* IsCS */ true, PGOOpt->ProfileFile,
840                         PGOOpt->ProfileRemappingFile);
841   }
842 
843   // Re-require GloblasAA here prior to function passes. This is particularly
844   // useful as the above will have inlined, DCE'ed, and function-attr
845   // propagated everything. We should at this point have a reasonably minimal
846   // and richly annotated call graph. By computing aliasing and mod/ref
847   // information for all local globals here, the late loop passes and notably
848   // the vectorizer will be able to use them to help recognize vectorizable
849   // memory operations.
850   MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
851 
852   FunctionPassManager OptimizePM(DebugLogging);
853   OptimizePM.addPass(Float2IntPass());
854   // FIXME: We need to run some loop optimizations to re-rotate loops after
855   // simplify-cfg and others undo their rotation.
856 
857   // Optimize the loop execution. These passes operate on entire loop nests
858   // rather than on each loop in an inside-out manner, and so they are actually
859   // function passes.
860 
861   for (auto &C : VectorizerStartEPCallbacks)
862     C(OptimizePM, Level);
863 
864   // First rotate loops that may have been un-rotated by prior passes.
865   OptimizePM.addPass(
866       createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
867 
868   // Distribute loops to allow partial vectorization.  I.e. isolate dependences
869   // into separate loop that would otherwise inhibit vectorization.  This is
870   // currently only performed for loops marked with the metadata
871   // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
872   OptimizePM.addPass(LoopDistributePass());
873 
874   // Now run the core loop vectorizer.
875   OptimizePM.addPass(LoopVectorizePass(
876       LoopVectorizeOptions(!PTO.LoopInterleaving, !PTO.LoopVectorization)));
877 
878   // Eliminate loads by forwarding stores from the previous iteration to loads
879   // of the current iteration.
880   OptimizePM.addPass(LoopLoadEliminationPass());
881 
882   // Cleanup after the loop optimization passes.
883   OptimizePM.addPass(InstCombinePass());
884 
885   // Now that we've formed fast to execute loop structures, we do further
886   // optimizations. These are run afterward as they might block doing complex
887   // analyses and transforms such as what are needed for loop vectorization.
888 
889   // Cleanup after loop vectorization, etc. Simplification passes like CVP and
890   // GVN, loop transforms, and others have already run, so it's now better to
891   // convert to more optimized IR using more aggressive simplify CFG options.
892   // The extra sinking transform can create larger basic blocks, so do this
893   // before SLP vectorization.
894   OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
895                                      forwardSwitchCondToPhi(true).
896                                      convertSwitchToLookupTable(true).
897                                      needCanonicalLoops(false).
898                                      sinkCommonInsts(true)));
899 
900   // Optimize parallel scalar instruction chains into SIMD instructions.
901   if (PTO.SLPVectorization)
902     OptimizePM.addPass(SLPVectorizerPass());
903 
904   OptimizePM.addPass(InstCombinePass());
905 
906   // Unroll small loops to hide loop backedge latency and saturate any parallel
907   // execution resources of an out-of-order processor. We also then need to
908   // clean up redundancies and loop invariant code.
909   // FIXME: It would be really good to use a loop-integrated instruction
910   // combiner for cleanup here so that the unrolling and LICM can be pipelined
911   // across the loop nests.
912   // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
913   if (EnableUnrollAndJam) {
914     OptimizePM.addPass(
915         createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
916   }
917   if (PTO.LoopUnrolling)
918     OptimizePM.addPass(LoopUnrollPass(
919         LoopUnrollOptions(Level, false, PTO.ForgetAllSCEVInLoopUnroll)));
920   OptimizePM.addPass(WarnMissedTransformationsPass());
921   OptimizePM.addPass(InstCombinePass());
922   OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
923   OptimizePM.addPass(createFunctionToLoopPassAdaptor(
924       LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap),
925       DebugLogging));
926 
927   // Now that we've vectorized and unrolled loops, we may have more refined
928   // alignment information, try to re-derive it here.
929   OptimizePM.addPass(AlignmentFromAssumptionsPass());
930 
931   // Split out cold code. Splitting is done late to avoid hiding context from
932   // other optimizations and inadvertently regressing performance. The tradeoff
933   // is that this has a higher code size cost than splitting early.
934   if (EnableHotColdSplit && !LTOPreLink)
935     MPM.addPass(HotColdSplittingPass());
936 
937   // LoopSink pass sinks instructions hoisted by LICM, which serves as a
938   // canonicalization pass that enables other optimizations. As a result,
939   // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
940   // result too early.
941   OptimizePM.addPass(LoopSinkPass());
942 
943   // And finally clean up LCSSA form before generating code.
944   OptimizePM.addPass(InstSimplifyPass());
945 
946   // This hoists/decomposes div/rem ops. It should run after other sink/hoist
947   // passes to avoid re-sinking, but before SimplifyCFG because it can allow
948   // flattening of blocks.
949   OptimizePM.addPass(DivRemPairsPass());
950 
951   // LoopSink (and other loop passes since the last simplifyCFG) might have
952   // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
953   OptimizePM.addPass(SimplifyCFGPass());
954 
955   // Optimize PHIs by speculating around them when profitable. Note that this
956   // pass needs to be run after any PRE or similar pass as it is essentially
957   // inserting redundancies into the program. This even includes SimplifyCFG.
958   OptimizePM.addPass(SpeculateAroundPHIsPass());
959 
960   for (auto &C : OptimizerLastEPCallbacks)
961     C(OptimizePM, Level);
962 
963   // Add the core optimizing pipeline.
964   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
965 
966   MPM.addPass(CGProfilePass());
967 
968   // Now we need to do some global optimization transforms.
969   // FIXME: It would seem like these should come first in the optimization
970   // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
971   // ordering here.
972   MPM.addPass(GlobalDCEPass());
973   MPM.addPass(ConstantMergePass());
974 
975   return MPM;
976 }
977 
978 ModulePassManager
979 PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
980                                            bool DebugLogging, bool LTOPreLink) {
981   assert(Level != O0 && "Must request optimizations for the default pipeline!");
982 
983   ModulePassManager MPM(DebugLogging);
984 
985   // Force any function attributes we want the rest of the pipeline to observe.
986   MPM.addPass(ForceFunctionAttrsPass());
987 
988   // Apply module pipeline start EP callback.
989   for (auto &C : PipelineStartEPCallbacks)
990     C(MPM);
991 
992   if (PGOOpt && PGOOpt->SamplePGOSupport)
993     MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
994 
995   // Add the core simplification pipeline.
996   MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
997                                                 DebugLogging));
998 
999   // Now add the optimization pipeline.
1000   MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging, LTOPreLink));
1001 
1002   return MPM;
1003 }
1004 
1005 ModulePassManager
1006 PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
1007                                                 bool DebugLogging) {
1008   assert(Level != O0 && "Must request optimizations for the default pipeline!");
1009 
1010   ModulePassManager MPM(DebugLogging);
1011 
1012   // Force any function attributes we want the rest of the pipeline to observe.
1013   MPM.addPass(ForceFunctionAttrsPass());
1014 
1015   if (PGOOpt && PGOOpt->SamplePGOSupport)
1016     MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
1017 
1018   // Apply module pipeline start EP callback.
1019   for (auto &C : PipelineStartEPCallbacks)
1020     C(MPM);
1021 
1022   // If we are planning to perform ThinLTO later, we don't bloat the code with
1023   // unrolling/vectorization/... now. Just simplify the module as much as we
1024   // can.
1025   MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
1026                                                 DebugLogging));
1027 
1028   // Run partial inlining pass to partially inline functions that have
1029   // large bodies.
1030   // FIXME: It isn't clear whether this is really the right place to run this
1031   // in ThinLTO. Because there is another canonicalization and simplification
1032   // phase that will run after the thin link, running this here ends up with
1033   // less information than will be available later and it may grow functions in
1034   // ways that aren't beneficial.
1035   if (RunPartialInlining)
1036     MPM.addPass(PartialInlinerPass());
1037 
1038   // Reduce the size of the IR as much as possible.
1039   MPM.addPass(GlobalOptPass());
1040 
1041   return MPM;
1042 }
1043 
1044 ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
1045     OptimizationLevel Level, bool DebugLogging,
1046     const ModuleSummaryIndex *ImportSummary) {
1047   ModulePassManager MPM(DebugLogging);
1048 
1049   if (ImportSummary) {
1050     // These passes import type identifier resolutions for whole-program
1051     // devirtualization and CFI. They must run early because other passes may
1052     // disturb the specific instruction patterns that these passes look for,
1053     // creating dependencies on resolutions that may not appear in the summary.
1054     //
1055     // For example, GVN may transform the pattern assume(type.test) appearing in
1056     // two basic blocks into assume(phi(type.test, type.test)), which would
1057     // transform a dependency on a WPD resolution into a dependency on a type
1058     // identifier resolution for CFI.
1059     //
1060     // Also, WPD has access to more precise information than ICP and can
1061     // devirtualize more effectively, so it should operate on the IR first.
1062     //
1063     // The WPD and LowerTypeTest passes need to run at -O0 to lower type
1064     // metadata and intrinsics.
1065     MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
1066     MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
1067   }
1068 
1069   if (Level == O0)
1070     return MPM;
1071 
1072   // Force any function attributes we want the rest of the pipeline to observe.
1073   MPM.addPass(ForceFunctionAttrsPass());
1074 
1075   // Add the core simplification pipeline.
1076   MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
1077                                                 DebugLogging));
1078 
1079   // Now add the optimization pipeline.
1080   MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
1081 
1082   return MPM;
1083 }
1084 
1085 ModulePassManager
1086 PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
1087                                             bool DebugLogging) {
1088   assert(Level != O0 && "Must request optimizations for the default pipeline!");
1089   // FIXME: We should use a customized pre-link pipeline!
1090   return buildPerModuleDefaultPipeline(Level, DebugLogging,
1091                                        /* LTOPreLink */true);
1092 }
1093 
1094 ModulePassManager
1095 PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
1096                                      ModuleSummaryIndex *ExportSummary) {
1097   ModulePassManager MPM(DebugLogging);
1098 
1099   if (Level == O0) {
1100     // The WPD and LowerTypeTest passes need to run at -O0 to lower type
1101     // metadata and intrinsics.
1102     MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
1103     MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
1104     return MPM;
1105   }
1106 
1107   if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) {
1108     // Load sample profile before running the LTO optimization pipeline.
1109     MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile,
1110                                         PGOOpt->ProfileRemappingFile,
1111                                         false /* ThinLTOPhase::PreLink */));
1112     // Cache ProfileSummaryAnalysis once to avoid the potential need to insert
1113     // RequireAnalysisPass for PSI before subsequent non-module passes.
1114     MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
1115   }
1116 
1117   // Remove unused virtual tables to improve the quality of code generated by
1118   // whole-program devirtualization and bitset lowering.
1119   MPM.addPass(GlobalDCEPass());
1120 
1121   // Force any function attributes we want the rest of the pipeline to observe.
1122   MPM.addPass(ForceFunctionAttrsPass());
1123 
1124   // Do basic inference of function attributes from known properties of system
1125   // libraries and other oracles.
1126   MPM.addPass(InferFunctionAttrsPass());
1127 
1128   if (Level > 1) {
1129     FunctionPassManager EarlyFPM(DebugLogging);
1130     EarlyFPM.addPass(CallSiteSplittingPass());
1131     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
1132 
1133     // Indirect call promotion. This should promote all the targets that are
1134     // left by the earlier promotion pass that promotes intra-module targets.
1135     // This two-step promotion is to save the compile time. For LTO, it should
1136     // produce the same result as if we only do promotion here.
1137     MPM.addPass(PGOIndirectCallPromotion(
1138         true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse));
1139     // Propagate constants at call sites into the functions they call.  This
1140     // opens opportunities for globalopt (and inlining) by substituting function
1141     // pointers passed as arguments to direct uses of functions.
1142    MPM.addPass(IPSCCPPass());
1143 
1144    // Attach metadata to indirect call sites indicating the set of functions
1145    // they may target at run-time. This should follow IPSCCP.
1146    MPM.addPass(CalledValuePropagationPass());
1147   }
1148 
1149   // Now deduce any function attributes based in the current code.
1150   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1151               PostOrderFunctionAttrsPass()));
1152 
1153   // Do RPO function attribute inference across the module to forward-propagate
1154   // attributes where applicable.
1155   // FIXME: Is this really an optimization rather than a canonicalization?
1156   MPM.addPass(ReversePostOrderFunctionAttrsPass());
1157 
1158   // Use in-range annotations on GEP indices to split globals where beneficial.
1159   MPM.addPass(GlobalSplitPass());
1160 
1161   // Run whole program optimization of virtual call when the list of callees
1162   // is fixed.
1163   MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
1164 
1165   // Stop here at -O1.
1166   if (Level == 1) {
1167     // The LowerTypeTestsPass needs to run to lower type metadata and the
1168     // type.test intrinsics. The pass does nothing if CFI is disabled.
1169     MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
1170     return MPM;
1171   }
1172 
1173   // Optimize globals to try and fold them into constants.
1174   MPM.addPass(GlobalOptPass());
1175 
1176   // Promote any localized globals to SSA registers.
1177   MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1178 
1179   // Linking modules together can lead to duplicate global constant, only
1180   // keep one copy of each constant.
1181   MPM.addPass(ConstantMergePass());
1182 
1183   // Remove unused arguments from functions.
1184   MPM.addPass(DeadArgumentEliminationPass());
1185 
1186   // Reduce the code after globalopt and ipsccp.  Both can open up significant
1187   // simplification opportunities, and both can propagate functions through
1188   // function pointers.  When this happens, we often have to resolve varargs
1189   // calls, etc, so let instcombine do this.
1190   FunctionPassManager PeepholeFPM(DebugLogging);
1191   if (Level == O3)
1192     PeepholeFPM.addPass(AggressiveInstCombinePass());
1193   PeepholeFPM.addPass(InstCombinePass());
1194   invokePeepholeEPCallbacks(PeepholeFPM, Level);
1195 
1196   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
1197 
1198   // Note: historically, the PruneEH pass was run first to deduce nounwind and
1199   // generally clean up exception handling overhead. It isn't clear this is
1200   // valuable as the inliner doesn't currently care whether it is inlining an
1201   // invoke or a call.
1202   // Run the inliner now.
1203   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1204       InlinerPass(getInlineParamsFromOptLevel(Level))));
1205 
1206   // Optimize globals again after we ran the inliner.
1207   MPM.addPass(GlobalOptPass());
1208 
1209   // Garbage collect dead functions.
1210   // FIXME: Add ArgumentPromotion pass after once it's ported.
1211   MPM.addPass(GlobalDCEPass());
1212 
1213   FunctionPassManager FPM(DebugLogging);
1214   // The IPO Passes may leave cruft around. Clean up after them.
1215   FPM.addPass(InstCombinePass());
1216   invokePeepholeEPCallbacks(FPM, Level);
1217 
1218   FPM.addPass(JumpThreadingPass());
1219 
1220   // Do a post inline PGO instrumentation and use pass. This is a context
1221   // sensitive PGO pass.
1222   if (PGOOpt) {
1223     if (PGOOpt->CSAction == PGOOptions::CSIRInstr)
1224       addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true,
1225                         /* IsCS */ true, PGOOpt->CSProfileGenFile,
1226                         PGOOpt->ProfileRemappingFile);
1227     else if (PGOOpt->CSAction == PGOOptions::CSIRUse)
1228       addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false,
1229                         /* IsCS */ true, PGOOpt->ProfileFile,
1230                         PGOOpt->ProfileRemappingFile);
1231   }
1232 
1233   // Break up allocas
1234   FPM.addPass(SROA());
1235 
1236   // LTO provides additional opportunities for tailcall elimination due to
1237   // link-time inlining, and visibility of nocapture attribute.
1238   FPM.addPass(TailCallElimPass());
1239 
1240   // Run a few AA driver optimizations here and now to cleanup the code.
1241   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1242 
1243   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1244               PostOrderFunctionAttrsPass()));
1245   // FIXME: here we run IP alias analysis in the legacy PM.
1246 
1247   FunctionPassManager MainFPM;
1248 
1249   // FIXME: once we fix LoopPass Manager, add LICM here.
1250   // FIXME: once we provide support for enabling MLSM, add it here.
1251   if (RunNewGVN)
1252     MainFPM.addPass(NewGVNPass());
1253   else
1254     MainFPM.addPass(GVN());
1255 
1256   // Remove dead memcpy()'s.
1257   MainFPM.addPass(MemCpyOptPass());
1258 
1259   // Nuke dead stores.
1260   MainFPM.addPass(DSEPass());
1261 
1262   // FIXME: at this point, we run a bunch of loop passes:
1263   // indVarSimplify, loopDeletion, loopInterchange, loopUnroll,
1264   // loopVectorize. Enable them once the remaining issue with LPM
1265   // are sorted out.
1266 
1267   MainFPM.addPass(InstCombinePass());
1268   MainFPM.addPass(SimplifyCFGPass());
1269   MainFPM.addPass(SCCPPass());
1270   MainFPM.addPass(InstCombinePass());
1271   MainFPM.addPass(BDCEPass());
1272 
1273   // FIXME: We may want to run SLPVectorizer here.
1274   // After vectorization, assume intrinsics may tell us more
1275   // about pointer alignments.
1276 #if 0
1277   MainFPM.add(AlignmentFromAssumptionsPass());
1278 #endif
1279 
1280   // FIXME: Conditionally run LoadCombine here, after it's ported
1281   // (in case we still have this pass, given its questionable usefulness).
1282 
1283   MainFPM.addPass(InstCombinePass());
1284   invokePeepholeEPCallbacks(MainFPM, Level);
1285   MainFPM.addPass(JumpThreadingPass());
1286   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1287 
1288   // Create a function that performs CFI checks for cross-DSO calls with
1289   // targets in the current module.
1290   MPM.addPass(CrossDSOCFIPass());
1291 
1292   // Lower type metadata and the type.test intrinsic. This pass supports
1293   // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1294   // to be run at link time if CFI is enabled. This pass does nothing if
1295   // CFI is disabled.
1296   MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
1297 
1298   // Enable splitting late in the FullLTO post-link pipeline. This is done in
1299   // the same stage in the old pass manager (\ref addLateLTOOptimizationPasses).
1300   if (EnableHotColdSplit)
1301     MPM.addPass(HotColdSplittingPass());
1302 
1303   // Add late LTO optimization passes.
1304   // Delete basic blocks, which optimization passes may have killed.
1305   MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1306 
1307   // Drop bodies of available eternally objects to improve GlobalDCE.
1308   MPM.addPass(EliminateAvailableExternallyPass());
1309 
1310   // Now that we have optimized the program, discard unreachable functions.
1311   MPM.addPass(GlobalDCEPass());
1312 
1313   // FIXME: Maybe enable MergeFuncs conditionally after it's ported.
1314   return MPM;
1315 }
1316 
1317 AAManager PassBuilder::buildDefaultAAPipeline() {
1318   AAManager AA;
1319 
1320   // The order in which these are registered determines their priority when
1321   // being queried.
1322 
1323   // First we register the basic alias analysis that provides the majority of
1324   // per-function local AA logic. This is a stateless, on-demand local set of
1325   // AA techniques.
1326   AA.registerFunctionAnalysis<BasicAA>();
1327 
1328   // Next we query fast, specialized alias analyses that wrap IR-embedded
1329   // information about aliasing.
1330   AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1331   AA.registerFunctionAnalysis<TypeBasedAA>();
1332 
1333   // Add support for querying global aliasing information when available.
1334   // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1335   // analysis, all that the `AAManager` can do is query for any *cached*
1336   // results from `GlobalsAA` through a readonly proxy.
1337   AA.registerModuleAnalysis<GlobalsAA>();
1338 
1339   return AA;
1340 }
1341 
1342 static Optional<int> parseRepeatPassName(StringRef Name) {
1343   if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1344     return None;
1345   int Count;
1346   if (Name.getAsInteger(0, Count) || Count <= 0)
1347     return None;
1348   return Count;
1349 }
1350 
1351 static Optional<int> parseDevirtPassName(StringRef Name) {
1352   if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1353     return None;
1354   int Count;
1355   if (Name.getAsInteger(0, Count) || Count <= 0)
1356     return None;
1357   return Count;
1358 }
1359 
1360 static bool checkParametrizedPassName(StringRef Name, StringRef PassName) {
1361   if (!Name.consume_front(PassName))
1362     return false;
1363   // normal pass name w/o parameters == default parameters
1364   if (Name.empty())
1365     return true;
1366   return Name.startswith("<") && Name.endswith(">");
1367 }
1368 
1369 namespace {
1370 
1371 /// This performs customized parsing of pass name with parameters.
1372 ///
1373 /// We do not need parametrization of passes in textual pipeline very often,
1374 /// yet on a rare occasion ability to specify parameters right there can be
1375 /// useful.
1376 ///
1377 /// \p Name - parameterized specification of a pass from a textual pipeline
1378 /// is a string in a form of :
1379 ///      PassName '<' parameter-list '>'
1380 ///
1381 /// Parameter list is being parsed by the parser callable argument, \p Parser,
1382 /// It takes a string-ref of parameters and returns either StringError or a
1383 /// parameter list in a form of a custom parameters type, all wrapped into
1384 /// Expected<> template class.
1385 ///
1386 template <typename ParametersParseCallableT>
1387 auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name,
1388                          StringRef PassName) -> decltype(Parser(StringRef{})) {
1389   using ParametersT = typename decltype(Parser(StringRef{}))::value_type;
1390 
1391   StringRef Params = Name;
1392   if (!Params.consume_front(PassName)) {
1393     assert(false &&
1394            "unable to strip pass name from parametrized pass specification");
1395   }
1396   if (Params.empty())
1397     return ParametersT{};
1398   if (!Params.consume_front("<") || !Params.consume_back(">")) {
1399     assert(false && "invalid format for parametrized pass name");
1400   }
1401 
1402   Expected<ParametersT> Result = Parser(Params);
1403   assert((Result || Result.template errorIsA<StringError>()) &&
1404          "Pass parameter parser can only return StringErrors.");
1405   return std::move(Result);
1406 }
1407 
1408 /// Parser of parameters for LoopUnroll pass.
1409 Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) {
1410   LoopUnrollOptions UnrollOpts;
1411   while (!Params.empty()) {
1412     StringRef ParamName;
1413     std::tie(ParamName, Params) = Params.split(';');
1414     int OptLevel = StringSwitch<int>(ParamName)
1415                        .Case("O0", 0)
1416                        .Case("O1", 1)
1417                        .Case("O2", 2)
1418                        .Case("O3", 3)
1419                        .Default(-1);
1420     if (OptLevel >= 0) {
1421       UnrollOpts.setOptLevel(OptLevel);
1422       continue;
1423     }
1424 
1425     bool Enable = !ParamName.consume_front("no-");
1426     if (ParamName == "partial") {
1427       UnrollOpts.setPartial(Enable);
1428     } else if (ParamName == "peeling") {
1429       UnrollOpts.setPeeling(Enable);
1430     } else if (ParamName == "runtime") {
1431       UnrollOpts.setRuntime(Enable);
1432     } else if (ParamName == "upperbound") {
1433       UnrollOpts.setUpperBound(Enable);
1434     } else {
1435       return make_error<StringError>(
1436           formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
1437           inconvertibleErrorCode());
1438     }
1439   }
1440   return UnrollOpts;
1441 }
1442 
1443 Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
1444   MemorySanitizerOptions Result;
1445   while (!Params.empty()) {
1446     StringRef ParamName;
1447     std::tie(ParamName, Params) = Params.split(';');
1448 
1449     if (ParamName == "recover") {
1450       Result.Recover = true;
1451     } else if (ParamName == "kernel") {
1452       Result.Kernel = true;
1453     } else if (ParamName.consume_front("track-origins=")) {
1454       if (ParamName.getAsInteger(0, Result.TrackOrigins))
1455         return make_error<StringError>(
1456             formatv("invalid argument to MemorySanitizer pass track-origins "
1457                     "parameter: '{0}' ",
1458                     ParamName)
1459                 .str(),
1460             inconvertibleErrorCode());
1461     } else {
1462       return make_error<StringError>(
1463           formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName)
1464               .str(),
1465           inconvertibleErrorCode());
1466     }
1467   }
1468   return Result;
1469 }
1470 
1471 /// Parser of parameters for SimplifyCFG pass.
1472 Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
1473   SimplifyCFGOptions Result;
1474   while (!Params.empty()) {
1475     StringRef ParamName;
1476     std::tie(ParamName, Params) = Params.split(';');
1477 
1478     bool Enable = !ParamName.consume_front("no-");
1479     if (ParamName == "forward-switch-cond") {
1480       Result.forwardSwitchCondToPhi(Enable);
1481     } else if (ParamName == "switch-to-lookup") {
1482       Result.convertSwitchToLookupTable(Enable);
1483     } else if (ParamName == "keep-loops") {
1484       Result.needCanonicalLoops(Enable);
1485     } else if (ParamName == "sink-common-insts") {
1486       Result.sinkCommonInsts(Enable);
1487     } else if (Enable && ParamName.consume_front("bonus-inst-threshold=")) {
1488       APInt BonusInstThreshold;
1489       if (ParamName.getAsInteger(0, BonusInstThreshold))
1490         return make_error<StringError>(
1491             formatv("invalid argument to SimplifyCFG pass bonus-threshold "
1492                     "parameter: '{0}' ",
1493                     ParamName).str(),
1494             inconvertibleErrorCode());
1495       Result.bonusInstThreshold(BonusInstThreshold.getSExtValue());
1496     } else {
1497       return make_error<StringError>(
1498           formatv("invalid SimplifyCFG pass parameter '{0}' ", ParamName).str(),
1499           inconvertibleErrorCode());
1500     }
1501   }
1502   return Result;
1503 }
1504 
1505 /// Parser of parameters for LoopVectorize pass.
1506 Expected<LoopVectorizeOptions> parseLoopVectorizeOptions(StringRef Params) {
1507   LoopVectorizeOptions Opts;
1508   while (!Params.empty()) {
1509     StringRef ParamName;
1510     std::tie(ParamName, Params) = Params.split(';');
1511 
1512     bool Enable = !ParamName.consume_front("no-");
1513     if (ParamName == "interleave-forced-only") {
1514       Opts.setInterleaveOnlyWhenForced(Enable);
1515     } else if (ParamName == "vectorize-forced-only") {
1516       Opts.setVectorizeOnlyWhenForced(Enable);
1517     } else {
1518       return make_error<StringError>(
1519           formatv("invalid LoopVectorize parameter '{0}' ", ParamName).str(),
1520           inconvertibleErrorCode());
1521     }
1522   }
1523   return Opts;
1524 }
1525 
1526 Expected<bool> parseLoopUnswitchOptions(StringRef Params) {
1527   bool Result = false;
1528   while (!Params.empty()) {
1529     StringRef ParamName;
1530     std::tie(ParamName, Params) = Params.split(';');
1531 
1532     bool Enable = !ParamName.consume_front("no-");
1533     if (ParamName == "nontrivial") {
1534       Result = Enable;
1535     } else {
1536       return make_error<StringError>(
1537           formatv("invalid LoopUnswitch pass parameter '{0}' ", ParamName)
1538               .str(),
1539           inconvertibleErrorCode());
1540     }
1541   }
1542   return Result;
1543 }
1544 } // namespace
1545 
1546 /// Tests whether a pass name starts with a valid prefix for a default pipeline
1547 /// alias.
1548 static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1549   return Name.startswith("default") || Name.startswith("thinlto") ||
1550          Name.startswith("lto");
1551 }
1552 
1553 /// Tests whether registered callbacks will accept a given pass name.
1554 ///
1555 /// When parsing a pipeline text, the type of the outermost pipeline may be
1556 /// omitted, in which case the type is automatically determined from the first
1557 /// pass name in the text. This may be a name that is handled through one of the
1558 /// callbacks. We check this through the oridinary parsing callbacks by setting
1559 /// up a dummy PassManager in order to not force the client to also handle this
1560 /// type of query.
1561 template <typename PassManagerT, typename CallbacksT>
1562 static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1563   if (!Callbacks.empty()) {
1564     PassManagerT DummyPM;
1565     for (auto &CB : Callbacks)
1566       if (CB(Name, DummyPM, {}))
1567         return true;
1568   }
1569   return false;
1570 }
1571 
1572 template <typename CallbacksT>
1573 static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
1574   // Manually handle aliases for pre-configured pipeline fragments.
1575   if (startsWithDefaultPipelineAliasPrefix(Name))
1576     return DefaultAliasRegex.match(Name);
1577 
1578   // Explicitly handle pass manager names.
1579   if (Name == "module")
1580     return true;
1581   if (Name == "cgscc")
1582     return true;
1583   if (Name == "function")
1584     return true;
1585 
1586   // Explicitly handle custom-parsed pass names.
1587   if (parseRepeatPassName(Name))
1588     return true;
1589 
1590 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
1591   if (Name == NAME)                                                            \
1592     return true;
1593 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
1594   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1595     return true;
1596 #include "PassRegistry.def"
1597 
1598   return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
1599 }
1600 
1601 template <typename CallbacksT>
1602 static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
1603   // Explicitly handle pass manager names.
1604   if (Name == "cgscc")
1605     return true;
1606   if (Name == "function")
1607     return true;
1608 
1609   // Explicitly handle custom-parsed pass names.
1610   if (parseRepeatPassName(Name))
1611     return true;
1612   if (parseDevirtPassName(Name))
1613     return true;
1614 
1615 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
1616   if (Name == NAME)                                                            \
1617     return true;
1618 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
1619   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1620     return true;
1621 #include "PassRegistry.def"
1622 
1623   return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
1624 }
1625 
1626 template <typename CallbacksT>
1627 static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
1628   // Explicitly handle pass manager names.
1629   if (Name == "function")
1630     return true;
1631   if (Name == "loop")
1632     return true;
1633 
1634   // Explicitly handle custom-parsed pass names.
1635   if (parseRepeatPassName(Name))
1636     return true;
1637 
1638 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
1639   if (Name == NAME)                                                            \
1640     return true;
1641 #define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER)                   \
1642   if (checkParametrizedPassName(Name, NAME))                                   \
1643     return true;
1644 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
1645   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1646     return true;
1647 #include "PassRegistry.def"
1648 
1649   return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
1650 }
1651 
1652 template <typename CallbacksT>
1653 static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
1654   // Explicitly handle pass manager names.
1655   if (Name == "loop")
1656     return true;
1657 
1658   // Explicitly handle custom-parsed pass names.
1659   if (parseRepeatPassName(Name))
1660     return true;
1661 
1662 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
1663   if (Name == NAME)                                                            \
1664     return true;
1665 #define LOOP_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER)                       \
1666   if (checkParametrizedPassName(Name, NAME))                                   \
1667     return true;
1668 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
1669   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1670     return true;
1671 #include "PassRegistry.def"
1672 
1673   return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
1674 }
1675 
1676 Optional<std::vector<PassBuilder::PipelineElement>>
1677 PassBuilder::parsePipelineText(StringRef Text) {
1678   std::vector<PipelineElement> ResultPipeline;
1679 
1680   SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1681       &ResultPipeline};
1682   for (;;) {
1683     std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1684     size_t Pos = Text.find_first_of(",()");
1685     Pipeline.push_back({Text.substr(0, Pos), {}});
1686 
1687     // If we have a single terminating name, we're done.
1688     if (Pos == Text.npos)
1689       break;
1690 
1691     char Sep = Text[Pos];
1692     Text = Text.substr(Pos + 1);
1693     if (Sep == ',')
1694       // Just a name ending in a comma, continue.
1695       continue;
1696 
1697     if (Sep == '(') {
1698       // Push the inner pipeline onto the stack to continue processing.
1699       PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1700       continue;
1701     }
1702 
1703     assert(Sep == ')' && "Bogus separator!");
1704     // When handling the close parenthesis, we greedily consume them to avoid
1705     // empty strings in the pipeline.
1706     do {
1707       // If we try to pop the outer pipeline we have unbalanced parentheses.
1708       if (PipelineStack.size() == 1)
1709         return None;
1710 
1711       PipelineStack.pop_back();
1712     } while (Text.consume_front(")"));
1713 
1714     // Check if we've finished parsing.
1715     if (Text.empty())
1716       break;
1717 
1718     // Otherwise, the end of an inner pipeline always has to be followed by
1719     // a comma, and then we can continue.
1720     if (!Text.consume_front(","))
1721       return None;
1722   }
1723 
1724   if (PipelineStack.size() > 1)
1725     // Unbalanced paretheses.
1726     return None;
1727 
1728   assert(PipelineStack.back() == &ResultPipeline &&
1729          "Wrong pipeline at the bottom of the stack!");
1730   return {std::move(ResultPipeline)};
1731 }
1732 
1733 Error PassBuilder::parseModulePass(ModulePassManager &MPM,
1734                                    const PipelineElement &E,
1735                                    bool VerifyEachPass, bool DebugLogging) {
1736   auto &Name = E.Name;
1737   auto &InnerPipeline = E.InnerPipeline;
1738 
1739   // First handle complex passes like the pass managers which carry pipelines.
1740   if (!InnerPipeline.empty()) {
1741     if (Name == "module") {
1742       ModulePassManager NestedMPM(DebugLogging);
1743       if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline,
1744                                              VerifyEachPass, DebugLogging))
1745         return Err;
1746       MPM.addPass(std::move(NestedMPM));
1747       return Error::success();
1748     }
1749     if (Name == "cgscc") {
1750       CGSCCPassManager CGPM(DebugLogging);
1751       if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1752                                             DebugLogging))
1753         return Err;
1754       MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
1755       return Error::success();
1756     }
1757     if (Name == "function") {
1758       FunctionPassManager FPM(DebugLogging);
1759       if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline,
1760                                                VerifyEachPass, DebugLogging))
1761         return Err;
1762       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1763       return Error::success();
1764     }
1765     if (auto Count = parseRepeatPassName(Name)) {
1766       ModulePassManager NestedMPM(DebugLogging);
1767       if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline,
1768                                              VerifyEachPass, DebugLogging))
1769         return Err;
1770       MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
1771       return Error::success();
1772     }
1773 
1774     for (auto &C : ModulePipelineParsingCallbacks)
1775       if (C(Name, MPM, InnerPipeline))
1776         return Error::success();
1777 
1778     // Normal passes can't have pipelines.
1779     return make_error<StringError>(
1780         formatv("invalid use of '{0}' pass as module pipeline", Name).str(),
1781         inconvertibleErrorCode());
1782     ;
1783   }
1784 
1785   // Manually handle aliases for pre-configured pipeline fragments.
1786   if (startsWithDefaultPipelineAliasPrefix(Name)) {
1787     SmallVector<StringRef, 3> Matches;
1788     if (!DefaultAliasRegex.match(Name, &Matches))
1789       return make_error<StringError>(
1790           formatv("unknown default pipeline alias '{0}'", Name).str(),
1791           inconvertibleErrorCode());
1792 
1793     assert(Matches.size() == 3 && "Must capture two matched strings!");
1794 
1795     OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1796                               .Case("O0", O0)
1797                               .Case("O1", O1)
1798                               .Case("O2", O2)
1799                               .Case("O3", O3)
1800                               .Case("Os", Os)
1801                               .Case("Oz", Oz);
1802     if (L == O0)
1803       // At O0 we do nothing at all!
1804       return Error::success();
1805 
1806     if (Matches[1] == "default") {
1807       MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
1808     } else if (Matches[1] == "thinlto-pre-link") {
1809       MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1810     } else if (Matches[1] == "thinlto") {
1811       MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
1812     } else if (Matches[1] == "lto-pre-link") {
1813       MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
1814     } else {
1815       assert(Matches[1] == "lto" && "Not one of the matched options!");
1816       MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
1817     }
1818     return Error::success();
1819   }
1820 
1821   // Finally expand the basic registered passes from the .inc file.
1822 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
1823   if (Name == NAME) {                                                          \
1824     MPM.addPass(CREATE_PASS);                                                  \
1825     return Error::success();                                                   \
1826   }
1827 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
1828   if (Name == "require<" NAME ">") {                                           \
1829     MPM.addPass(                                                               \
1830         RequireAnalysisPass<                                                   \
1831             std::remove_reference<decltype(CREATE_PASS)>::type, Module>());    \
1832     return Error::success();                                                   \
1833   }                                                                            \
1834   if (Name == "invalidate<" NAME ">") {                                        \
1835     MPM.addPass(InvalidateAnalysisPass<                                        \
1836                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
1837     return Error::success();                                                   \
1838   }
1839 #include "PassRegistry.def"
1840 
1841   for (auto &C : ModulePipelineParsingCallbacks)
1842     if (C(Name, MPM, InnerPipeline))
1843       return Error::success();
1844   return make_error<StringError>(
1845       formatv("unknown module pass '{0}'", Name).str(),
1846       inconvertibleErrorCode());
1847 }
1848 
1849 Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1850                                   const PipelineElement &E, bool VerifyEachPass,
1851                                   bool DebugLogging) {
1852   auto &Name = E.Name;
1853   auto &InnerPipeline = E.InnerPipeline;
1854 
1855   // First handle complex passes like the pass managers which carry pipelines.
1856   if (!InnerPipeline.empty()) {
1857     if (Name == "cgscc") {
1858       CGSCCPassManager NestedCGPM(DebugLogging);
1859       if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1860                                             VerifyEachPass, DebugLogging))
1861         return Err;
1862       // Add the nested pass manager with the appropriate adaptor.
1863       CGPM.addPass(std::move(NestedCGPM));
1864       return Error::success();
1865     }
1866     if (Name == "function") {
1867       FunctionPassManager FPM(DebugLogging);
1868       if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline,
1869                                                VerifyEachPass, DebugLogging))
1870         return Err;
1871       // Add the nested pass manager with the appropriate adaptor.
1872       CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
1873       return Error::success();
1874     }
1875     if (auto Count = parseRepeatPassName(Name)) {
1876       CGSCCPassManager NestedCGPM(DebugLogging);
1877       if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1878                                             VerifyEachPass, DebugLogging))
1879         return Err;
1880       CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
1881       return Error::success();
1882     }
1883     if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1884       CGSCCPassManager NestedCGPM(DebugLogging);
1885       if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1886                                             VerifyEachPass, DebugLogging))
1887         return Err;
1888       CGPM.addPass(
1889           createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
1890       return Error::success();
1891     }
1892 
1893     for (auto &C : CGSCCPipelineParsingCallbacks)
1894       if (C(Name, CGPM, InnerPipeline))
1895         return Error::success();
1896 
1897     // Normal passes can't have pipelines.
1898     return make_error<StringError>(
1899         formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(),
1900         inconvertibleErrorCode());
1901   }
1902 
1903 // Now expand the basic registered passes from the .inc file.
1904 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
1905   if (Name == NAME) {                                                          \
1906     CGPM.addPass(CREATE_PASS);                                                 \
1907     return Error::success();                                                   \
1908   }
1909 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
1910   if (Name == "require<" NAME ">") {                                           \
1911     CGPM.addPass(RequireAnalysisPass<                                          \
1912                  std::remove_reference<decltype(CREATE_PASS)>::type,           \
1913                  LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,    \
1914                  CGSCCUpdateResult &>());                                      \
1915     return Error::success();                                                   \
1916   }                                                                            \
1917   if (Name == "invalidate<" NAME ">") {                                        \
1918     CGPM.addPass(InvalidateAnalysisPass<                                       \
1919                  std::remove_reference<decltype(CREATE_PASS)>::type>());       \
1920     return Error::success();                                                   \
1921   }
1922 #include "PassRegistry.def"
1923 
1924   for (auto &C : CGSCCPipelineParsingCallbacks)
1925     if (C(Name, CGPM, InnerPipeline))
1926       return Error::success();
1927   return make_error<StringError>(
1928       formatv("unknown cgscc pass '{0}'", Name).str(),
1929       inconvertibleErrorCode());
1930 }
1931 
1932 Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1933                                      const PipelineElement &E,
1934                                      bool VerifyEachPass, bool DebugLogging) {
1935   auto &Name = E.Name;
1936   auto &InnerPipeline = E.InnerPipeline;
1937 
1938   // First handle complex passes like the pass managers which carry pipelines.
1939   if (!InnerPipeline.empty()) {
1940     if (Name == "function") {
1941       FunctionPassManager NestedFPM(DebugLogging);
1942       if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline,
1943                                                VerifyEachPass, DebugLogging))
1944         return Err;
1945       // Add the nested pass manager with the appropriate adaptor.
1946       FPM.addPass(std::move(NestedFPM));
1947       return Error::success();
1948     }
1949     if (Name == "loop") {
1950       LoopPassManager LPM(DebugLogging);
1951       if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1952                                            DebugLogging))
1953         return Err;
1954       // Add the nested pass manager with the appropriate adaptor.
1955       FPM.addPass(
1956           createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
1957       return Error::success();
1958     }
1959     if (auto Count = parseRepeatPassName(Name)) {
1960       FunctionPassManager NestedFPM(DebugLogging);
1961       if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline,
1962                                                VerifyEachPass, DebugLogging))
1963         return Err;
1964       FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
1965       return Error::success();
1966     }
1967 
1968     for (auto &C : FunctionPipelineParsingCallbacks)
1969       if (C(Name, FPM, InnerPipeline))
1970         return Error::success();
1971 
1972     // Normal passes can't have pipelines.
1973     return make_error<StringError>(
1974         formatv("invalid use of '{0}' pass as function pipeline", Name).str(),
1975         inconvertibleErrorCode());
1976   }
1977 
1978 // Now expand the basic registered passes from the .inc file.
1979 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
1980   if (Name == NAME) {                                                          \
1981     FPM.addPass(CREATE_PASS);                                                  \
1982     return Error::success();                                                   \
1983   }
1984 #define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER)                   \
1985   if (checkParametrizedPassName(Name, NAME)) {                                 \
1986     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1987     if (!Params)                                                               \
1988       return Params.takeError();                                               \
1989     FPM.addPass(CREATE_PASS(Params.get()));                                    \
1990     return Error::success();                                                   \
1991   }
1992 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
1993   if (Name == "require<" NAME ">") {                                           \
1994     FPM.addPass(                                                               \
1995         RequireAnalysisPass<                                                   \
1996             std::remove_reference<decltype(CREATE_PASS)>::type, Function>());  \
1997     return Error::success();                                                   \
1998   }                                                                            \
1999   if (Name == "invalidate<" NAME ">") {                                        \
2000     FPM.addPass(InvalidateAnalysisPass<                                        \
2001                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
2002     return Error::success();                                                   \
2003   }
2004 #include "PassRegistry.def"
2005 
2006   for (auto &C : FunctionPipelineParsingCallbacks)
2007     if (C(Name, FPM, InnerPipeline))
2008       return Error::success();
2009   return make_error<StringError>(
2010       formatv("unknown function pass '{0}'", Name).str(),
2011       inconvertibleErrorCode());
2012 }
2013 
2014 Error PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
2015                                  bool VerifyEachPass, bool DebugLogging) {
2016   StringRef Name = E.Name;
2017   auto &InnerPipeline = E.InnerPipeline;
2018 
2019   // First handle complex passes like the pass managers which carry pipelines.
2020   if (!InnerPipeline.empty()) {
2021     if (Name == "loop") {
2022       LoopPassManager NestedLPM(DebugLogging);
2023       if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline,
2024                                            VerifyEachPass, DebugLogging))
2025         return Err;
2026       // Add the nested pass manager with the appropriate adaptor.
2027       LPM.addPass(std::move(NestedLPM));
2028       return Error::success();
2029     }
2030     if (auto Count = parseRepeatPassName(Name)) {
2031       LoopPassManager NestedLPM(DebugLogging);
2032       if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline,
2033                                            VerifyEachPass, DebugLogging))
2034         return Err;
2035       LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
2036       return Error::success();
2037     }
2038 
2039     for (auto &C : LoopPipelineParsingCallbacks)
2040       if (C(Name, LPM, InnerPipeline))
2041         return Error::success();
2042 
2043     // Normal passes can't have pipelines.
2044     return make_error<StringError>(
2045         formatv("invalid use of '{0}' pass as loop pipeline", Name).str(),
2046         inconvertibleErrorCode());
2047   }
2048 
2049 // Now expand the basic registered passes from the .inc file.
2050 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
2051   if (Name == NAME) {                                                          \
2052     LPM.addPass(CREATE_PASS);                                                  \
2053     return Error::success();                                                   \
2054   }
2055 #define LOOP_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER)                       \
2056   if (checkParametrizedPassName(Name, NAME)) {                                 \
2057     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
2058     if (!Params)                                                               \
2059       return Params.takeError();                                               \
2060     LPM.addPass(CREATE_PASS(Params.get()));                                    \
2061     return Error::success();                                                   \
2062   }
2063 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
2064   if (Name == "require<" NAME ">") {                                           \
2065     LPM.addPass(RequireAnalysisPass<                                           \
2066                 std::remove_reference<decltype(CREATE_PASS)>::type, Loop,      \
2067                 LoopAnalysisManager, LoopStandardAnalysisResults &,            \
2068                 LPMUpdater &>());                                              \
2069     return Error::success();                                                   \
2070   }                                                                            \
2071   if (Name == "invalidate<" NAME ">") {                                        \
2072     LPM.addPass(InvalidateAnalysisPass<                                        \
2073                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
2074     return Error::success();                                                   \
2075   }
2076 #include "PassRegistry.def"
2077 
2078   for (auto &C : LoopPipelineParsingCallbacks)
2079     if (C(Name, LPM, InnerPipeline))
2080       return Error::success();
2081   return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(),
2082                                  inconvertibleErrorCode());
2083 }
2084 
2085 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
2086 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS)                               \
2087   if (Name == NAME) {                                                          \
2088     AA.registerModuleAnalysis<                                                 \
2089         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
2090     return true;                                                               \
2091   }
2092 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS)                             \
2093   if (Name == NAME) {                                                          \
2094     AA.registerFunctionAnalysis<                                               \
2095         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
2096     return true;                                                               \
2097   }
2098 #include "PassRegistry.def"
2099 
2100   for (auto &C : AAParsingCallbacks)
2101     if (C(Name, AA))
2102       return true;
2103   return false;
2104 }
2105 
2106 Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
2107                                          ArrayRef<PipelineElement> Pipeline,
2108                                          bool VerifyEachPass,
2109                                          bool DebugLogging) {
2110   for (const auto &Element : Pipeline) {
2111     if (auto Err = parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
2112       return Err;
2113     // FIXME: No verifier support for Loop passes!
2114   }
2115   return Error::success();
2116 }
2117 
2118 Error PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
2119                                              ArrayRef<PipelineElement> Pipeline,
2120                                              bool VerifyEachPass,
2121                                              bool DebugLogging) {
2122   for (const auto &Element : Pipeline) {
2123     if (auto Err =
2124             parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
2125       return Err;
2126     if (VerifyEachPass)
2127       FPM.addPass(VerifierPass());
2128   }
2129   return Error::success();
2130 }
2131 
2132 Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
2133                                           ArrayRef<PipelineElement> Pipeline,
2134                                           bool VerifyEachPass,
2135                                           bool DebugLogging) {
2136   for (const auto &Element : Pipeline) {
2137     if (auto Err = parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
2138       return Err;
2139     // FIXME: No verifier support for CGSCC passes!
2140   }
2141   return Error::success();
2142 }
2143 
2144 void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
2145                                        FunctionAnalysisManager &FAM,
2146                                        CGSCCAnalysisManager &CGAM,
2147                                        ModuleAnalysisManager &MAM) {
2148   MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
2149   MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
2150   CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
2151   FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
2152   FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
2153   FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
2154   LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
2155 }
2156 
2157 Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
2158                                            ArrayRef<PipelineElement> Pipeline,
2159                                            bool VerifyEachPass,
2160                                            bool DebugLogging) {
2161   for (const auto &Element : Pipeline) {
2162     if (auto Err = parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
2163       return Err;
2164     if (VerifyEachPass)
2165       MPM.addPass(VerifierPass());
2166   }
2167   return Error::success();
2168 }
2169 
2170 // Primary pass pipeline description parsing routine for a \c ModulePassManager
2171 // FIXME: Should this routine accept a TargetMachine or require the caller to
2172 // pre-populate the analysis managers with target-specific stuff?
2173 Error PassBuilder::parsePassPipeline(ModulePassManager &MPM,
2174                                      StringRef PipelineText,
2175                                      bool VerifyEachPass, bool DebugLogging) {
2176   auto Pipeline = parsePipelineText(PipelineText);
2177   if (!Pipeline || Pipeline->empty())
2178     return make_error<StringError>(
2179         formatv("invalid pipeline '{0}'", PipelineText).str(),
2180         inconvertibleErrorCode());
2181 
2182   // If the first name isn't at the module layer, wrap the pipeline up
2183   // automatically.
2184   StringRef FirstName = Pipeline->front().Name;
2185 
2186   if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
2187     if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
2188       Pipeline = {{"cgscc", std::move(*Pipeline)}};
2189     } else if (isFunctionPassName(FirstName,
2190                                   FunctionPipelineParsingCallbacks)) {
2191       Pipeline = {{"function", std::move(*Pipeline)}};
2192     } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
2193       Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
2194     } else {
2195       for (auto &C : TopLevelPipelineParsingCallbacks)
2196         if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
2197           return Error::success();
2198 
2199       // Unknown pass or pipeline name!
2200       auto &InnerPipeline = Pipeline->front().InnerPipeline;
2201       return make_error<StringError>(
2202           formatv("unknown {0} name '{1}'",
2203                   (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName)
2204               .str(),
2205           inconvertibleErrorCode());
2206     }
2207   }
2208 
2209   if (auto Err =
2210           parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging))
2211     return Err;
2212   return Error::success();
2213 }
2214 
2215 // Primary pass pipeline description parsing routine for a \c CGSCCPassManager
2216 Error PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
2217                                      StringRef PipelineText,
2218                                      bool VerifyEachPass, bool DebugLogging) {
2219   auto Pipeline = parsePipelineText(PipelineText);
2220   if (!Pipeline || Pipeline->empty())
2221     return make_error<StringError>(
2222         formatv("invalid pipeline '{0}'", PipelineText).str(),
2223         inconvertibleErrorCode());
2224 
2225   StringRef FirstName = Pipeline->front().Name;
2226   if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
2227     return make_error<StringError>(
2228         formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName,
2229                 PipelineText)
2230             .str(),
2231         inconvertibleErrorCode());
2232 
2233   if (auto Err =
2234           parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging))
2235     return Err;
2236   return Error::success();
2237 }
2238 
2239 // Primary pass pipeline description parsing routine for a \c
2240 // FunctionPassManager
2241 Error PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
2242                                      StringRef PipelineText,
2243                                      bool VerifyEachPass, bool DebugLogging) {
2244   auto Pipeline = parsePipelineText(PipelineText);
2245   if (!Pipeline || Pipeline->empty())
2246     return make_error<StringError>(
2247         formatv("invalid pipeline '{0}'", PipelineText).str(),
2248         inconvertibleErrorCode());
2249 
2250   StringRef FirstName = Pipeline->front().Name;
2251   if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
2252     return make_error<StringError>(
2253         formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName,
2254                 PipelineText)
2255             .str(),
2256         inconvertibleErrorCode());
2257 
2258   if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
2259                                            DebugLogging))
2260     return Err;
2261   return Error::success();
2262 }
2263 
2264 // Primary pass pipeline description parsing routine for a \c LoopPassManager
2265 Error PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
2266                                      StringRef PipelineText,
2267                                      bool VerifyEachPass, bool DebugLogging) {
2268   auto Pipeline = parsePipelineText(PipelineText);
2269   if (!Pipeline || Pipeline->empty())
2270     return make_error<StringError>(
2271         formatv("invalid pipeline '{0}'", PipelineText).str(),
2272         inconvertibleErrorCode());
2273 
2274   if (auto Err =
2275           parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging))
2276     return Err;
2277 
2278   return Error::success();
2279 }
2280 
2281 Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
2282   // If the pipeline just consists of the word 'default' just replace the AA
2283   // manager with our default one.
2284   if (PipelineText == "default") {
2285     AA = buildDefaultAAPipeline();
2286     return Error::success();
2287   }
2288 
2289   while (!PipelineText.empty()) {
2290     StringRef Name;
2291     std::tie(Name, PipelineText) = PipelineText.split(',');
2292     if (!parseAAPassName(AA, Name))
2293       return make_error<StringError>(
2294           formatv("unknown alias analysis name '{0}'", Name).str(),
2295           inconvertibleErrorCode());
2296   }
2297 
2298   return Error::success();
2299 }
2300