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