xref: /freebsd/contrib/llvm-project/llvm/lib/Passes/PassBuilder.cpp (revision c57c26179033f64c2011a2d2a904ee3fa62e826a)
1 //===- Parsing and selection 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/AliasAnalysisEvaluator.h"
20 #include "llvm/Analysis/AliasSetTracker.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/CFGSCCPrinter.h"
27 #include "llvm/Analysis/CGSCCPassManager.h"
28 #include "llvm/Analysis/CallGraph.h"
29 #include "llvm/Analysis/CallPrinter.h"
30 #include "llvm/Analysis/CostModel.h"
31 #include "llvm/Analysis/CycleAnalysis.h"
32 #include "llvm/Analysis/DDG.h"
33 #include "llvm/Analysis/DDGPrinter.h"
34 #include "llvm/Analysis/Delinearization.h"
35 #include "llvm/Analysis/DemandedBits.h"
36 #include "llvm/Analysis/DependenceAnalysis.h"
37 #include "llvm/Analysis/DomPrinter.h"
38 #include "llvm/Analysis/DominanceFrontier.h"
39 #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
40 #include "llvm/Analysis/GlobalsModRef.h"
41 #include "llvm/Analysis/IRSimilarityIdentifier.h"
42 #include "llvm/Analysis/IVUsers.h"
43 #include "llvm/Analysis/InlineAdvisor.h"
44 #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h"
45 #include "llvm/Analysis/InstCount.h"
46 #include "llvm/Analysis/LazyCallGraph.h"
47 #include "llvm/Analysis/LazyValueInfo.h"
48 #include "llvm/Analysis/Lint.h"
49 #include "llvm/Analysis/LoopAccessAnalysis.h"
50 #include "llvm/Analysis/LoopCacheAnalysis.h"
51 #include "llvm/Analysis/LoopInfo.h"
52 #include "llvm/Analysis/LoopNestAnalysis.h"
53 #include "llvm/Analysis/MemDerefPrinter.h"
54 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
55 #include "llvm/Analysis/MemorySSA.h"
56 #include "llvm/Analysis/ModuleDebugInfoPrinter.h"
57 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
58 #include "llvm/Analysis/MustExecute.h"
59 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
60 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
61 #include "llvm/Analysis/PhiValues.h"
62 #include "llvm/Analysis/PostDominators.h"
63 #include "llvm/Analysis/ProfileSummaryInfo.h"
64 #include "llvm/Analysis/RegionInfo.h"
65 #include "llvm/Analysis/ScalarEvolution.h"
66 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
67 #include "llvm/Analysis/ScopedNoAliasAA.h"
68 #include "llvm/Analysis/StackLifetime.h"
69 #include "llvm/Analysis/StackSafetyAnalysis.h"
70 #include "llvm/Analysis/StructuralHash.h"
71 #include "llvm/Analysis/TargetLibraryInfo.h"
72 #include "llvm/Analysis/TargetTransformInfo.h"
73 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
74 #include "llvm/Analysis/UniformityAnalysis.h"
75 #include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
76 #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
77 #include "llvm/CodeGen/CallBrPrepare.h"
78 #include "llvm/CodeGen/CodeGenPrepare.h"
79 #include "llvm/CodeGen/DwarfEHPrepare.h"
80 #include "llvm/CodeGen/ExpandLargeDivRem.h"
81 #include "llvm/CodeGen/ExpandLargeFpConvert.h"
82 #include "llvm/CodeGen/ExpandMemCmp.h"
83 #include "llvm/CodeGen/GCMetadata.h"
84 #include "llvm/CodeGen/GlobalMerge.h"
85 #include "llvm/CodeGen/HardwareLoops.h"
86 #include "llvm/CodeGen/IndirectBrExpand.h"
87 #include "llvm/CodeGen/InterleavedAccess.h"
88 #include "llvm/CodeGen/InterleavedLoadCombine.h"
89 #include "llvm/CodeGen/JMCInstrumenter.h"
90 #include "llvm/CodeGen/LowerEmuTLS.h"
91 #include "llvm/CodeGen/SafeStack.h"
92 #include "llvm/CodeGen/SelectOptimize.h"
93 #include "llvm/CodeGen/ShadowStackGCLowering.h"
94 #include "llvm/CodeGen/SjLjEHPrepare.h"
95 #include "llvm/CodeGen/StackProtector.h"
96 #include "llvm/CodeGen/TargetPassConfig.h"
97 #include "llvm/CodeGen/TypePromotion.h"
98 #include "llvm/CodeGen/WasmEHPrepare.h"
99 #include "llvm/CodeGen/WinEHPrepare.h"
100 #include "llvm/IR/DebugInfo.h"
101 #include "llvm/IR/Dominators.h"
102 #include "llvm/IR/PassManager.h"
103 #include "llvm/IR/PrintPasses.h"
104 #include "llvm/IR/SafepointIRVerifier.h"
105 #include "llvm/IR/Verifier.h"
106 #include "llvm/IRPrinter/IRPrintingPasses.h"
107 #include "llvm/Passes/OptimizationLevel.h"
108 #include "llvm/Support/CommandLine.h"
109 #include "llvm/Support/Debug.h"
110 #include "llvm/Support/ErrorHandling.h"
111 #include "llvm/Support/FormatVariadic.h"
112 #include "llvm/Support/Regex.h"
113 #include "llvm/Target/TargetMachine.h"
114 #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
115 #include "llvm/Transforms/CFGuard.h"
116 #include "llvm/Transforms/Coroutines/CoroCleanup.h"
117 #include "llvm/Transforms/Coroutines/CoroConditionalWrapper.h"
118 #include "llvm/Transforms/Coroutines/CoroEarly.h"
119 #include "llvm/Transforms/Coroutines/CoroElide.h"
120 #include "llvm/Transforms/Coroutines/CoroSplit.h"
121 #include "llvm/Transforms/HipStdPar/HipStdPar.h"
122 #include "llvm/Transforms/IPO/AlwaysInliner.h"
123 #include "llvm/Transforms/IPO/Annotation2Metadata.h"
124 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
125 #include "llvm/Transforms/IPO/Attributor.h"
126 #include "llvm/Transforms/IPO/BlockExtractor.h"
127 #include "llvm/Transforms/IPO/CalledValuePropagation.h"
128 #include "llvm/Transforms/IPO/ConstantMerge.h"
129 #include "llvm/Transforms/IPO/CrossDSOCFI.h"
130 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
131 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
132 #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
133 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
134 #include "llvm/Transforms/IPO/FunctionAttrs.h"
135 #include "llvm/Transforms/IPO/FunctionImport.h"
136 #include "llvm/Transforms/IPO/GlobalDCE.h"
137 #include "llvm/Transforms/IPO/GlobalOpt.h"
138 #include "llvm/Transforms/IPO/GlobalSplit.h"
139 #include "llvm/Transforms/IPO/HotColdSplitting.h"
140 #include "llvm/Transforms/IPO/IROutliner.h"
141 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
142 #include "llvm/Transforms/IPO/Inliner.h"
143 #include "llvm/Transforms/IPO/Internalize.h"
144 #include "llvm/Transforms/IPO/LoopExtractor.h"
145 #include "llvm/Transforms/IPO/LowerTypeTests.h"
146 #include "llvm/Transforms/IPO/MemProfContextDisambiguation.h"
147 #include "llvm/Transforms/IPO/MergeFunctions.h"
148 #include "llvm/Transforms/IPO/ModuleInliner.h"
149 #include "llvm/Transforms/IPO/OpenMPOpt.h"
150 #include "llvm/Transforms/IPO/PartialInlining.h"
151 #include "llvm/Transforms/IPO/SCCP.h"
152 #include "llvm/Transforms/IPO/SampleProfile.h"
153 #include "llvm/Transforms/IPO/SampleProfileProbe.h"
154 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
155 #include "llvm/Transforms/IPO/StripSymbols.h"
156 #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
157 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
158 #include "llvm/Transforms/InstCombine/InstCombine.h"
159 #include "llvm/Transforms/Instrumentation.h"
160 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
161 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
162 #include "llvm/Transforms/Instrumentation/CGProfile.h"
163 #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
164 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
165 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
166 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
167 #include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
168 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
169 #include "llvm/Transforms/Instrumentation/KCFI.h"
170 #include "llvm/Transforms/Instrumentation/MemProfiler.h"
171 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
172 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
173 #include "llvm/Transforms/Instrumentation/PoisonChecking.h"
174 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
175 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
176 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
177 #include "llvm/Transforms/ObjCARC.h"
178 #include "llvm/Transforms/Scalar/ADCE.h"
179 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
180 #include "llvm/Transforms/Scalar/AnnotationRemarks.h"
181 #include "llvm/Transforms/Scalar/BDCE.h"
182 #include "llvm/Transforms/Scalar/CallSiteSplitting.h"
183 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
184 #include "llvm/Transforms/Scalar/ConstraintElimination.h"
185 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
186 #include "llvm/Transforms/Scalar/DCE.h"
187 #include "llvm/Transforms/Scalar/DFAJumpThreading.h"
188 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
189 #include "llvm/Transforms/Scalar/DivRemPairs.h"
190 #include "llvm/Transforms/Scalar/EarlyCSE.h"
191 #include "llvm/Transforms/Scalar/FlattenCFG.h"
192 #include "llvm/Transforms/Scalar/Float2Int.h"
193 #include "llvm/Transforms/Scalar/GVN.h"
194 #include "llvm/Transforms/Scalar/GuardWidening.h"
195 #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
196 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
197 #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
198 #include "llvm/Transforms/Scalar/InferAddressSpaces.h"
199 #include "llvm/Transforms/Scalar/InferAlignment.h"
200 #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
201 #include "llvm/Transforms/Scalar/JumpThreading.h"
202 #include "llvm/Transforms/Scalar/LICM.h"
203 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
204 #include "llvm/Transforms/Scalar/LoopBoundSplit.h"
205 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
206 #include "llvm/Transforms/Scalar/LoopDeletion.h"
207 #include "llvm/Transforms/Scalar/LoopDistribute.h"
208 #include "llvm/Transforms/Scalar/LoopFlatten.h"
209 #include "llvm/Transforms/Scalar/LoopFuse.h"
210 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
211 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
212 #include "llvm/Transforms/Scalar/LoopInterchange.h"
213 #include "llvm/Transforms/Scalar/LoopLoadElimination.h"
214 #include "llvm/Transforms/Scalar/LoopPassManager.h"
215 #include "llvm/Transforms/Scalar/LoopPredication.h"
216 #include "llvm/Transforms/Scalar/LoopReroll.h"
217 #include "llvm/Transforms/Scalar/LoopRotation.h"
218 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
219 #include "llvm/Transforms/Scalar/LoopSink.h"
220 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
221 #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
222 #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
223 #include "llvm/Transforms/Scalar/LoopVersioningLICM.h"
224 #include "llvm/Transforms/Scalar/LowerAtomicPass.h"
225 #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
226 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
227 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
228 #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h"
229 #include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
230 #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
231 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
232 #include "llvm/Transforms/Scalar/MergeICmps.h"
233 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
234 #include "llvm/Transforms/Scalar/NaryReassociate.h"
235 #include "llvm/Transforms/Scalar/NewGVN.h"
236 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
237 #include "llvm/Transforms/Scalar/PlaceSafepoints.h"
238 #include "llvm/Transforms/Scalar/Reassociate.h"
239 #include "llvm/Transforms/Scalar/Reg2Mem.h"
240 #include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
241 #include "llvm/Transforms/Scalar/SCCP.h"
242 #include "llvm/Transforms/Scalar/SROA.h"
243 #include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h"
244 #include "llvm/Transforms/Scalar/Scalarizer.h"
245 #include "llvm/Transforms/Scalar/SeparateConstOffsetFromGEP.h"
246 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
247 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
248 #include "llvm/Transforms/Scalar/Sink.h"
249 #include "llvm/Transforms/Scalar/SpeculativeExecution.h"
250 #include "llvm/Transforms/Scalar/StraightLineStrengthReduce.h"
251 #include "llvm/Transforms/Scalar/StructurizeCFG.h"
252 #include "llvm/Transforms/Scalar/TLSVariableHoist.h"
253 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
254 #include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
255 #include "llvm/Transforms/Utils/AddDiscriminators.h"
256 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
257 #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
258 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
259 #include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h"
260 #include "llvm/Transforms/Utils/CountVisits.h"
261 #include "llvm/Transforms/Utils/DXILUpgrade.h"
262 #include "llvm/Transforms/Utils/Debugify.h"
263 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
264 #include "llvm/Transforms/Utils/FixIrreducible.h"
265 #include "llvm/Transforms/Utils/HelloWorld.h"
266 #include "llvm/Transforms/Utils/InjectTLIMappings.h"
267 #include "llvm/Transforms/Utils/InstructionNamer.h"
268 #include "llvm/Transforms/Utils/LCSSA.h"
269 #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
270 #include "llvm/Transforms/Utils/LoopSimplify.h"
271 #include "llvm/Transforms/Utils/LoopVersioning.h"
272 #include "llvm/Transforms/Utils/LowerGlobalDtors.h"
273 #include "llvm/Transforms/Utils/LowerIFunc.h"
274 #include "llvm/Transforms/Utils/LowerInvoke.h"
275 #include "llvm/Transforms/Utils/LowerSwitch.h"
276 #include "llvm/Transforms/Utils/Mem2Reg.h"
277 #include "llvm/Transforms/Utils/MetaRenamer.h"
278 #include "llvm/Transforms/Utils/MoveAutoInit.h"
279 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
280 #include "llvm/Transforms/Utils/PredicateInfo.h"
281 #include "llvm/Transforms/Utils/RelLookupTableConverter.h"
282 #include "llvm/Transforms/Utils/StripGCRelocates.h"
283 #include "llvm/Transforms/Utils/StripNonLineTableDebugInfo.h"
284 #include "llvm/Transforms/Utils/SymbolRewriter.h"
285 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
286 #include "llvm/Transforms/Utils/UnifyLoopExits.h"
287 #include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h"
288 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
289 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
290 #include "llvm/Transforms/Vectorize/VectorCombine.h"
291 #include <optional>
292 
293 using namespace llvm;
294 
295 static const Regex DefaultAliasRegex(
296     "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
297 
298 namespace llvm {
299 cl::opt<bool> PrintPipelinePasses(
300     "print-pipeline-passes",
301     cl::desc("Print a '-passes' compatible string describing the pipeline "
302              "(best-effort only)."));
303 } // namespace llvm
304 
305 AnalysisKey NoOpModuleAnalysis::Key;
306 AnalysisKey NoOpCGSCCAnalysis::Key;
307 AnalysisKey NoOpFunctionAnalysis::Key;
308 AnalysisKey NoOpLoopAnalysis::Key;
309 
310 namespace {
311 
312 /// Whether or not we should populate a PassInstrumentationCallbacks's class to
313 /// pass name map.
314 ///
315 /// This is for optimization purposes so we don't populate it if we never use
316 /// it. This should be updated if new pass instrumentation wants to use the map.
317 /// We currently only use this for --print-before/after.
318 bool shouldPopulateClassToPassNames() {
319   return PrintPipelinePasses || !printBeforePasses().empty() ||
320          !printAfterPasses().empty() || !isFilterPassesEmpty() ||
321          TargetPassConfig::hasLimitedCodeGenPipeline();
322 }
323 
324 // A pass for testing -print-on-crash.
325 // DO NOT USE THIS EXCEPT FOR TESTING!
326 class TriggerCrashPass : public PassInfoMixin<TriggerCrashPass> {
327 public:
328   PreservedAnalyses run(Module &, ModuleAnalysisManager &) {
329     abort();
330     return PreservedAnalyses::all();
331   }
332   static StringRef name() { return "TriggerCrashPass"; }
333 };
334 
335 // A pass for testing message reporting of -verify-each failures.
336 // DO NOT USE THIS EXCEPT FOR TESTING!
337 class TriggerVerifierErrorPass
338     : public PassInfoMixin<TriggerVerifierErrorPass> {
339 public:
340   PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
341     // Intentionally break the Module by creating an alias without setting the
342     // aliasee.
343     auto *PtrTy = llvm::PointerType::getUnqual(M.getContext());
344     GlobalAlias::create(PtrTy, PtrTy->getAddressSpace(),
345                         GlobalValue::LinkageTypes::InternalLinkage,
346                         "__bad_alias", nullptr, &M);
347     return PreservedAnalyses::none();
348   }
349 
350   PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
351     // Intentionally break the Function by inserting a terminator
352     // instruction in the middle of a basic block.
353     BasicBlock &BB = F.getEntryBlock();
354     new UnreachableInst(F.getContext(), BB.getTerminator());
355     return PreservedAnalyses::none();
356   }
357 
358   static StringRef name() { return "TriggerVerifierErrorPass"; }
359 };
360 
361 } // namespace
362 
363 PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
364                          std::optional<PGOOptions> PGOOpt,
365                          PassInstrumentationCallbacks *PIC)
366     : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) {
367   bool ShouldPopulateClassToPassNames = PIC && shouldPopulateClassToPassNames();
368   if (TM)
369     TM->registerPassBuilderCallbacks(*this, ShouldPopulateClassToPassNames);
370   if (ShouldPopulateClassToPassNames) {
371 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
372   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
373 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)      \
374   PIC->addClassToPassName(CLASS, NAME);
375 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
376   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
377 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
378   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
379 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)    \
380   PIC->addClassToPassName(CLASS, NAME);
381 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
382   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
383 #define LOOPNEST_PASS(NAME, CREATE_PASS)                                       \
384   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
385 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
386   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
387 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
388   PIC->addClassToPassName(CLASS, NAME);
389 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
390   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
391 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
392   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
393 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)       \
394   PIC->addClassToPassName(CLASS, NAME);
395 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
396   PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
397 #include "PassRegistry.def"
398 
399 #define MACHINE_FUNCTION_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR)                \
400   PIC->addClassToPassName(PASS_NAME::name(), NAME);
401 #define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)                    \
402   PIC->addClassToPassName(PASS_NAME::name(), NAME);
403 #include "llvm/CodeGen/MachinePassRegistry.def"
404   }
405 }
406 
407 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
408 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
409   MAM.registerPass([&] { return CREATE_PASS; });
410 #include "PassRegistry.def"
411 
412   for (auto &C : ModuleAnalysisRegistrationCallbacks)
413     C(MAM);
414 }
415 
416 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
417 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
418   CGAM.registerPass([&] { return CREATE_PASS; });
419 #include "PassRegistry.def"
420 
421   for (auto &C : CGSCCAnalysisRegistrationCallbacks)
422     C(CGAM);
423 }
424 
425 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
426   // We almost always want the default alias analysis pipeline.
427   // If a user wants a different one, they can register their own before calling
428   // registerFunctionAnalyses().
429   FAM.registerPass([&] { return buildDefaultAAPipeline(); });
430 
431 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
432   FAM.registerPass([&] { return CREATE_PASS; });
433 #include "PassRegistry.def"
434 
435   for (auto &C : FunctionAnalysisRegistrationCallbacks)
436     C(FAM);
437 }
438 
439 void PassBuilder::registerMachineFunctionAnalyses(
440     MachineFunctionAnalysisManager &MFAM) {
441 
442 #define MACHINE_FUNCTION_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR)                \
443   MFAM.registerPass([&] { return PASS_NAME(); });
444 #include "llvm/CodeGen/MachinePassRegistry.def"
445 
446   for (auto &C : MachineFunctionAnalysisRegistrationCallbacks)
447     C(MFAM);
448 }
449 
450 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
451 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
452   LAM.registerPass([&] { return CREATE_PASS; });
453 #include "PassRegistry.def"
454 
455   for (auto &C : LoopAnalysisRegistrationCallbacks)
456     C(LAM);
457 }
458 
459 static std::optional<int> parseRepeatPassName(StringRef Name) {
460   if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
461     return std::nullopt;
462   int Count;
463   if (Name.getAsInteger(0, Count) || Count <= 0)
464     return std::nullopt;
465   return Count;
466 }
467 
468 static std::optional<std::pair<bool, bool>>
469 parseFunctionPipelineName(StringRef Name) {
470   std::pair<bool, bool> Params;
471   if (!Name.consume_front("function"))
472     return std::nullopt;
473   if (Name.empty())
474     return Params;
475   if (!Name.consume_front("<") || !Name.consume_back(">"))
476     return std::nullopt;
477   while (!Name.empty()) {
478     auto [Front, Back] = Name.split(';');
479     Name = Back;
480     if (Front == "eager-inv")
481       Params.first = true;
482     else if (Front == "no-rerun")
483       Params.second = true;
484     else
485       return std::nullopt;
486   }
487   return Params;
488 }
489 
490 static std::optional<int> parseDevirtPassName(StringRef Name) {
491   if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
492     return std::nullopt;
493   int Count;
494   if (Name.getAsInteger(0, Count) || Count < 0)
495     return std::nullopt;
496   return Count;
497 }
498 
499 static bool checkParametrizedPassName(StringRef Name, StringRef PassName) {
500   if (!Name.consume_front(PassName))
501     return false;
502   // normal pass name w/o parameters == default parameters
503   if (Name.empty())
504     return true;
505   return Name.starts_with("<") && Name.ends_with(">");
506 }
507 
508 static std::optional<OptimizationLevel> parseOptLevel(StringRef S) {
509   return StringSwitch<std::optional<OptimizationLevel>>(S)
510       .Case("O0", OptimizationLevel::O0)
511       .Case("O1", OptimizationLevel::O1)
512       .Case("O2", OptimizationLevel::O2)
513       .Case("O3", OptimizationLevel::O3)
514       .Case("Os", OptimizationLevel::Os)
515       .Case("Oz", OptimizationLevel::Oz)
516       .Default(std::nullopt);
517 }
518 
519 namespace {
520 
521 /// This performs customized parsing of pass name with parameters.
522 ///
523 /// We do not need parametrization of passes in textual pipeline very often,
524 /// yet on a rare occasion ability to specify parameters right there can be
525 /// useful.
526 ///
527 /// \p Name - parameterized specification of a pass from a textual pipeline
528 /// is a string in a form of :
529 ///      PassName '<' parameter-list '>'
530 ///
531 /// Parameter list is being parsed by the parser callable argument, \p Parser,
532 /// It takes a string-ref of parameters and returns either StringError or a
533 /// parameter list in a form of a custom parameters type, all wrapped into
534 /// Expected<> template class.
535 ///
536 template <typename ParametersParseCallableT>
537 auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name,
538                          StringRef PassName) -> decltype(Parser(StringRef{})) {
539   using ParametersT = typename decltype(Parser(StringRef{}))::value_type;
540 
541   StringRef Params = Name;
542   if (!Params.consume_front(PassName)) {
543     assert(false &&
544            "unable to strip pass name from parametrized pass specification");
545   }
546   if (!Params.empty() &&
547       (!Params.consume_front("<") || !Params.consume_back(">"))) {
548     assert(false && "invalid format for parametrized pass name");
549   }
550 
551   Expected<ParametersT> Result = Parser(Params);
552   assert((Result || Result.template errorIsA<StringError>()) &&
553          "Pass parameter parser can only return StringErrors.");
554   return Result;
555 }
556 
557 /// Parser of parameters for HardwareLoops  pass.
558 Expected<HardwareLoopOptions> parseHardwareLoopOptions(StringRef Params) {
559   HardwareLoopOptions HardwareLoopOpts;
560 
561   while (!Params.empty()) {
562     StringRef ParamName;
563     std::tie(ParamName, Params) = Params.split(';');
564     if (ParamName.consume_front("hardware-loop-decrement=")) {
565       int Count;
566       if (ParamName.getAsInteger(0, Count))
567         return make_error<StringError>(
568             formatv("invalid HardwareLoopPass parameter '{0}' ", ParamName).str(),
569             inconvertibleErrorCode());
570       HardwareLoopOpts.setDecrement(Count);
571       continue;
572     }
573     if (ParamName.consume_front("hardware-loop-counter-bitwidth=")) {
574       int Count;
575       if (ParamName.getAsInteger(0, Count))
576         return make_error<StringError>(
577             formatv("invalid HardwareLoopPass parameter '{0}' ", ParamName).str(),
578             inconvertibleErrorCode());
579       HardwareLoopOpts.setCounterBitwidth(Count);
580       continue;
581     }
582     if (ParamName == "force-hardware-loops") {
583       HardwareLoopOpts.setForce(true);
584     } else if (ParamName == "force-hardware-loop-phi") {
585       HardwareLoopOpts.setForcePhi(true);
586     } else if (ParamName == "force-nested-hardware-loop") {
587       HardwareLoopOpts.setForceNested(true);
588     } else if (ParamName == "force-hardware-loop-guard") {
589       HardwareLoopOpts.setForceGuard(true);
590     } else {
591       return make_error<StringError>(
592           formatv("invalid HardwarePass parameter '{0}' ", ParamName).str(),
593           inconvertibleErrorCode());
594     }
595   }
596   return HardwareLoopOpts;
597 }
598 
599 /// Parser of parameters for LoopUnroll pass.
600 Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) {
601   LoopUnrollOptions UnrollOpts;
602   while (!Params.empty()) {
603     StringRef ParamName;
604     std::tie(ParamName, Params) = Params.split(';');
605     std::optional<OptimizationLevel> OptLevel = parseOptLevel(ParamName);
606     // Don't accept -Os/-Oz.
607     if (OptLevel && !OptLevel->isOptimizingForSize()) {
608       UnrollOpts.setOptLevel(OptLevel->getSpeedupLevel());
609       continue;
610     }
611     if (ParamName.consume_front("full-unroll-max=")) {
612       int Count;
613       if (ParamName.getAsInteger(0, Count))
614         return make_error<StringError>(
615             formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
616             inconvertibleErrorCode());
617       UnrollOpts.setFullUnrollMaxCount(Count);
618       continue;
619     }
620 
621     bool Enable = !ParamName.consume_front("no-");
622     if (ParamName == "partial") {
623       UnrollOpts.setPartial(Enable);
624     } else if (ParamName == "peeling") {
625       UnrollOpts.setPeeling(Enable);
626     } else if (ParamName == "profile-peeling") {
627       UnrollOpts.setProfileBasedPeeling(Enable);
628     } else if (ParamName == "runtime") {
629       UnrollOpts.setRuntime(Enable);
630     } else if (ParamName == "upperbound") {
631       UnrollOpts.setUpperBound(Enable);
632     } else {
633       return make_error<StringError>(
634           formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
635           inconvertibleErrorCode());
636     }
637   }
638   return UnrollOpts;
639 }
640 
641 Expected<bool> parseSinglePassOption(StringRef Params, StringRef OptionName,
642                                      StringRef PassName) {
643   bool Result = false;
644   while (!Params.empty()) {
645     StringRef ParamName;
646     std::tie(ParamName, Params) = Params.split(';');
647 
648     if (ParamName == OptionName) {
649       Result = true;
650     } else {
651       return make_error<StringError>(
652           formatv("invalid {1} pass parameter '{0}' ", ParamName, PassName)
653               .str(),
654           inconvertibleErrorCode());
655     }
656   }
657   return Result;
658 }
659 
660 Expected<bool> parseGlobalDCEPassOptions(StringRef Params) {
661   return parseSinglePassOption(Params, "vfe-linkage-unit-visibility", "GlobalDCE");
662 }
663 
664 Expected<bool> parseCGProfilePassOptions(StringRef Params) {
665   return parseSinglePassOption(Params, "in-lto-post-link", "CGProfile");
666 }
667 
668 Expected<bool> parseInlinerPassOptions(StringRef Params) {
669   return parseSinglePassOption(Params, "only-mandatory", "InlinerPass");
670 }
671 
672 Expected<bool> parseCoroSplitPassOptions(StringRef Params) {
673   return parseSinglePassOption(Params, "reuse-storage", "CoroSplitPass");
674 }
675 
676 Expected<bool> parsePostOrderFunctionAttrsPassOptions(StringRef Params) {
677   return parseSinglePassOption(Params, "skip-non-recursive-function-attrs",
678                                "PostOrderFunctionAttrs");
679 }
680 
681 Expected<CFGuardPass::Mechanism> parseCFGuardPassOptions(StringRef Params) {
682   if (Params.empty())
683     return CFGuardPass::Mechanism::Check;
684 
685   auto [Param, RHS] = Params.split(';');
686   if (!RHS.empty())
687     return make_error<StringError>(
688         formatv("too many CFGuardPass parameters '{0}' ", Params).str(),
689         inconvertibleErrorCode());
690 
691   if (Param == "check")
692     return CFGuardPass::Mechanism::Check;
693   if (Param == "dispatch")
694     return CFGuardPass::Mechanism::Dispatch;
695 
696   return make_error<StringError>(
697       formatv("invalid CFGuardPass mechanism: '{0}' ", Param).str(),
698       inconvertibleErrorCode());
699 }
700 
701 Expected<bool> parseEarlyCSEPassOptions(StringRef Params) {
702   return parseSinglePassOption(Params, "memssa", "EarlyCSE");
703 }
704 
705 Expected<bool> parseEntryExitInstrumenterPassOptions(StringRef Params) {
706   return parseSinglePassOption(Params, "post-inline", "EntryExitInstrumenter");
707 }
708 
709 Expected<bool> parseLoopExtractorPassOptions(StringRef Params) {
710   return parseSinglePassOption(Params, "single", "LoopExtractor");
711 }
712 
713 Expected<bool> parseLowerMatrixIntrinsicsPassOptions(StringRef Params) {
714   return parseSinglePassOption(Params, "minimal", "LowerMatrixIntrinsics");
715 }
716 
717 Expected<AddressSanitizerOptions> parseASanPassOptions(StringRef Params) {
718   AddressSanitizerOptions Result;
719   while (!Params.empty()) {
720     StringRef ParamName;
721     std::tie(ParamName, Params) = Params.split(';');
722 
723     if (ParamName == "kernel") {
724       Result.CompileKernel = true;
725     } else {
726       return make_error<StringError>(
727           formatv("invalid AddressSanitizer pass parameter '{0}' ", ParamName)
728               .str(),
729           inconvertibleErrorCode());
730     }
731   }
732   return Result;
733 }
734 
735 Expected<HWAddressSanitizerOptions> parseHWASanPassOptions(StringRef Params) {
736   HWAddressSanitizerOptions Result;
737   while (!Params.empty()) {
738     StringRef ParamName;
739     std::tie(ParamName, Params) = Params.split(';');
740 
741     if (ParamName == "recover") {
742       Result.Recover = true;
743     } else if (ParamName == "kernel") {
744       Result.CompileKernel = true;
745     } else {
746       return make_error<StringError>(
747           formatv("invalid HWAddressSanitizer pass parameter '{0}' ", ParamName)
748               .str(),
749           inconvertibleErrorCode());
750     }
751   }
752   return Result;
753 }
754 
755 Expected<EmbedBitcodeOptions> parseEmbedBitcodePassOptions(StringRef Params) {
756   EmbedBitcodeOptions Result;
757   while (!Params.empty()) {
758     StringRef ParamName;
759     std::tie(ParamName, Params) = Params.split(';');
760 
761     if (ParamName == "thinlto") {
762       Result.IsThinLTO = true;
763     } else if (ParamName == "emit-summary") {
764       Result.EmitLTOSummary = true;
765     } else {
766       return make_error<StringError>(
767           formatv("invalid EmbedBitcode pass parameter '{0}' ", ParamName)
768               .str(),
769           inconvertibleErrorCode());
770     }
771   }
772   return Result;
773 }
774 
775 Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
776   MemorySanitizerOptions Result;
777   while (!Params.empty()) {
778     StringRef ParamName;
779     std::tie(ParamName, Params) = Params.split(';');
780 
781     if (ParamName == "recover") {
782       Result.Recover = true;
783     } else if (ParamName == "kernel") {
784       Result.Kernel = true;
785     } else if (ParamName.consume_front("track-origins=")) {
786       if (ParamName.getAsInteger(0, Result.TrackOrigins))
787         return make_error<StringError>(
788             formatv("invalid argument to MemorySanitizer pass track-origins "
789                     "parameter: '{0}' ",
790                     ParamName)
791                 .str(),
792             inconvertibleErrorCode());
793     } else if (ParamName == "eager-checks") {
794       Result.EagerChecks = true;
795     } else {
796       return make_error<StringError>(
797           formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName)
798               .str(),
799           inconvertibleErrorCode());
800     }
801   }
802   return Result;
803 }
804 
805 /// Parser of parameters for SimplifyCFG pass.
806 Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
807   SimplifyCFGOptions Result;
808   while (!Params.empty()) {
809     StringRef ParamName;
810     std::tie(ParamName, Params) = Params.split(';');
811 
812     bool Enable = !ParamName.consume_front("no-");
813     if (ParamName == "speculate-blocks") {
814       Result.speculateBlocks(Enable);
815     } else if (ParamName == "simplify-cond-branch") {
816       Result.setSimplifyCondBranch(Enable);
817     } else if (ParamName == "forward-switch-cond") {
818       Result.forwardSwitchCondToPhi(Enable);
819     } else if (ParamName == "switch-range-to-icmp") {
820       Result.convertSwitchRangeToICmp(Enable);
821     } else if (ParamName == "switch-to-lookup") {
822       Result.convertSwitchToLookupTable(Enable);
823     } else if (ParamName == "keep-loops") {
824       Result.needCanonicalLoops(Enable);
825     } else if (ParamName == "hoist-common-insts") {
826       Result.hoistCommonInsts(Enable);
827     } else if (ParamName == "sink-common-insts") {
828       Result.sinkCommonInsts(Enable);
829     } else if (Enable && ParamName.consume_front("bonus-inst-threshold=")) {
830       APInt BonusInstThreshold;
831       if (ParamName.getAsInteger(0, BonusInstThreshold))
832         return make_error<StringError>(
833             formatv("invalid argument to SimplifyCFG pass bonus-threshold "
834                     "parameter: '{0}' ",
835                     ParamName).str(),
836             inconvertibleErrorCode());
837       Result.bonusInstThreshold(BonusInstThreshold.getSExtValue());
838     } else {
839       return make_error<StringError>(
840           formatv("invalid SimplifyCFG pass parameter '{0}' ", ParamName).str(),
841           inconvertibleErrorCode());
842     }
843   }
844   return Result;
845 }
846 
847 Expected<InstCombineOptions> parseInstCombineOptions(StringRef Params) {
848   InstCombineOptions Result;
849   // When specifying "instcombine" in -passes enable fix-point verification by
850   // default, as this is what most tests should use.
851   Result.setVerifyFixpoint(true);
852   while (!Params.empty()) {
853     StringRef ParamName;
854     std::tie(ParamName, Params) = Params.split(';');
855 
856     bool Enable = !ParamName.consume_front("no-");
857     if (ParamName == "use-loop-info") {
858       Result.setUseLoopInfo(Enable);
859     } else if (ParamName == "verify-fixpoint") {
860       Result.setVerifyFixpoint(Enable);
861     } else if (Enable && ParamName.consume_front("max-iterations=")) {
862       APInt MaxIterations;
863       if (ParamName.getAsInteger(0, MaxIterations))
864         return make_error<StringError>(
865             formatv("invalid argument to InstCombine pass max-iterations "
866                     "parameter: '{0}' ",
867                     ParamName).str(),
868             inconvertibleErrorCode());
869       Result.setMaxIterations((unsigned)MaxIterations.getZExtValue());
870     } else {
871       return make_error<StringError>(
872           formatv("invalid InstCombine pass parameter '{0}' ", ParamName).str(),
873           inconvertibleErrorCode());
874     }
875   }
876   return Result;
877 }
878 
879 /// Parser of parameters for LoopVectorize pass.
880 Expected<LoopVectorizeOptions> parseLoopVectorizeOptions(StringRef Params) {
881   LoopVectorizeOptions Opts;
882   while (!Params.empty()) {
883     StringRef ParamName;
884     std::tie(ParamName, Params) = Params.split(';');
885 
886     bool Enable = !ParamName.consume_front("no-");
887     if (ParamName == "interleave-forced-only") {
888       Opts.setInterleaveOnlyWhenForced(Enable);
889     } else if (ParamName == "vectorize-forced-only") {
890       Opts.setVectorizeOnlyWhenForced(Enable);
891     } else {
892       return make_error<StringError>(
893           formatv("invalid LoopVectorize parameter '{0}' ", ParamName).str(),
894           inconvertibleErrorCode());
895     }
896   }
897   return Opts;
898 }
899 
900 Expected<std::pair<bool, bool>> parseLoopUnswitchOptions(StringRef Params) {
901   std::pair<bool, bool> Result = {false, true};
902   while (!Params.empty()) {
903     StringRef ParamName;
904     std::tie(ParamName, Params) = Params.split(';');
905 
906     bool Enable = !ParamName.consume_front("no-");
907     if (ParamName == "nontrivial") {
908       Result.first = Enable;
909     } else if (ParamName == "trivial") {
910       Result.second = Enable;
911     } else {
912       return make_error<StringError>(
913           formatv("invalid LoopUnswitch pass parameter '{0}' ", ParamName)
914               .str(),
915           inconvertibleErrorCode());
916     }
917   }
918   return Result;
919 }
920 
921 Expected<LICMOptions> parseLICMOptions(StringRef Params) {
922   LICMOptions Result;
923   while (!Params.empty()) {
924     StringRef ParamName;
925     std::tie(ParamName, Params) = Params.split(';');
926 
927     bool Enable = !ParamName.consume_front("no-");
928     if (ParamName == "allowspeculation") {
929       Result.AllowSpeculation = Enable;
930     } else {
931       return make_error<StringError>(
932           formatv("invalid LICM pass parameter '{0}' ", ParamName).str(),
933           inconvertibleErrorCode());
934     }
935   }
936   return Result;
937 }
938 
939 Expected<std::pair<bool, bool>> parseLoopRotateOptions(StringRef Params) {
940   std::pair<bool, bool> Result = {true, false};
941   while (!Params.empty()) {
942     StringRef ParamName;
943     std::tie(ParamName, Params) = Params.split(';');
944 
945     bool Enable = !ParamName.consume_front("no-");
946     if (ParamName == "header-duplication") {
947       Result.first = Enable;
948     } else if (ParamName == "prepare-for-lto") {
949       Result.second = Enable;
950     } else {
951       return make_error<StringError>(
952           formatv("invalid LoopRotate pass parameter '{0}' ", ParamName).str(),
953           inconvertibleErrorCode());
954     }
955   }
956   return Result;
957 }
958 
959 Expected<bool> parseMergedLoadStoreMotionOptions(StringRef Params) {
960   bool Result = false;
961   while (!Params.empty()) {
962     StringRef ParamName;
963     std::tie(ParamName, Params) = Params.split(';');
964 
965     bool Enable = !ParamName.consume_front("no-");
966     if (ParamName == "split-footer-bb") {
967       Result = Enable;
968     } else {
969       return make_error<StringError>(
970           formatv("invalid MergedLoadStoreMotion pass parameter '{0}' ",
971                   ParamName)
972               .str(),
973           inconvertibleErrorCode());
974     }
975   }
976   return Result;
977 }
978 
979 Expected<GVNOptions> parseGVNOptions(StringRef Params) {
980   GVNOptions Result;
981   while (!Params.empty()) {
982     StringRef ParamName;
983     std::tie(ParamName, Params) = Params.split(';');
984 
985     bool Enable = !ParamName.consume_front("no-");
986     if (ParamName == "pre") {
987       Result.setPRE(Enable);
988     } else if (ParamName == "load-pre") {
989       Result.setLoadPRE(Enable);
990     } else if (ParamName == "split-backedge-load-pre") {
991       Result.setLoadPRESplitBackedge(Enable);
992     } else if (ParamName == "memdep") {
993       Result.setMemDep(Enable);
994     } else {
995       return make_error<StringError>(
996           formatv("invalid GVN pass parameter '{0}' ", ParamName).str(),
997           inconvertibleErrorCode());
998     }
999   }
1000   return Result;
1001 }
1002 
1003 Expected<IPSCCPOptions> parseIPSCCPOptions(StringRef Params) {
1004   IPSCCPOptions Result;
1005   while (!Params.empty()) {
1006     StringRef ParamName;
1007     std::tie(ParamName, Params) = Params.split(';');
1008 
1009     bool Enable = !ParamName.consume_front("no-");
1010     if (ParamName == "func-spec")
1011       Result.setFuncSpec(Enable);
1012     else
1013       return make_error<StringError>(
1014           formatv("invalid IPSCCP pass parameter '{0}' ", ParamName).str(),
1015           inconvertibleErrorCode());
1016   }
1017   return Result;
1018 }
1019 
1020 Expected<SROAOptions> parseSROAOptions(StringRef Params) {
1021   if (Params.empty() || Params == "modify-cfg")
1022     return SROAOptions::ModifyCFG;
1023   if (Params == "preserve-cfg")
1024     return SROAOptions::PreserveCFG;
1025   return make_error<StringError>(
1026       formatv("invalid SROA pass parameter '{0}' (either preserve-cfg or "
1027               "modify-cfg can be specified)",
1028               Params)
1029           .str(),
1030       inconvertibleErrorCode());
1031 }
1032 
1033 Expected<StackLifetime::LivenessType>
1034 parseStackLifetimeOptions(StringRef Params) {
1035   StackLifetime::LivenessType Result = StackLifetime::LivenessType::May;
1036   while (!Params.empty()) {
1037     StringRef ParamName;
1038     std::tie(ParamName, Params) = Params.split(';');
1039 
1040     if (ParamName == "may") {
1041       Result = StackLifetime::LivenessType::May;
1042     } else if (ParamName == "must") {
1043       Result = StackLifetime::LivenessType::Must;
1044     } else {
1045       return make_error<StringError>(
1046           formatv("invalid StackLifetime parameter '{0}' ", ParamName).str(),
1047           inconvertibleErrorCode());
1048     }
1049   }
1050   return Result;
1051 }
1052 
1053 Expected<bool> parseDependenceAnalysisPrinterOptions(StringRef Params) {
1054   return parseSinglePassOption(Params, "normalized-results",
1055                                "DependenceAnalysisPrinter");
1056 }
1057 
1058 Expected<bool> parseSeparateConstOffsetFromGEPPassOptions(StringRef Params) {
1059   return parseSinglePassOption(Params, "lower-gep",
1060                                "SeparateConstOffsetFromGEP");
1061 }
1062 
1063 Expected<OptimizationLevel>
1064 parseFunctionSimplificationPipelineOptions(StringRef Params) {
1065   std::optional<OptimizationLevel> L = parseOptLevel(Params);
1066   if (!L || *L == OptimizationLevel::O0) {
1067     return make_error<StringError>(
1068         formatv("invalid function-simplification parameter '{0}' ", Params)
1069             .str(),
1070         inconvertibleErrorCode());
1071   };
1072   return *L;
1073 }
1074 
1075 Expected<bool> parseMemorySSAPrinterPassOptions(StringRef Params) {
1076   return parseSinglePassOption(Params, "no-ensure-optimized-uses",
1077                                "MemorySSAPrinterPass");
1078 }
1079 
1080 Expected<bool> parseSpeculativeExecutionPassOptions(StringRef Params) {
1081   return parseSinglePassOption(Params, "only-if-divergent-target",
1082                                "SpeculativeExecutionPass");
1083 }
1084 
1085 Expected<std::string> parseMemProfUsePassOptions(StringRef Params) {
1086   std::string Result;
1087   while (!Params.empty()) {
1088     StringRef ParamName;
1089     std::tie(ParamName, Params) = Params.split(';');
1090 
1091     if (ParamName.consume_front("profile-filename=")) {
1092       Result = ParamName.str();
1093     } else {
1094       return make_error<StringError>(
1095           formatv("invalid MemProfUse pass parameter '{0}' ", ParamName).str(),
1096           inconvertibleErrorCode());
1097     }
1098   }
1099   return Result;
1100 }
1101 
1102 Expected<bool> parseStructuralHashPrinterPassOptions(StringRef Params) {
1103   return parseSinglePassOption(Params, "detailed",
1104                                "StructuralHashPrinterPass");
1105 }
1106 
1107 Expected<bool> parseWinEHPrepareOptions(StringRef Params) {
1108   return parseSinglePassOption(Params, "demote-catchswitch-only",
1109                                "WinEHPreparePass");
1110 }
1111 
1112 Expected<GlobalMergeOptions> parseGlobalMergeOptions(StringRef Params) {
1113   GlobalMergeOptions Result;
1114   while (!Params.empty()) {
1115     StringRef ParamName;
1116     std::tie(ParamName, Params) = Params.split(';');
1117 
1118     bool Enable = !ParamName.consume_front("no-");
1119     if (ParamName == "group-by-use")
1120       Result.GroupByUse = Enable;
1121     else if (ParamName == "ignore-single-use")
1122       Result.IgnoreSingleUse = Enable;
1123     else if (ParamName == "merge-const")
1124       Result.MergeConst = Enable;
1125     else if (ParamName == "merge-external")
1126       Result.MergeExternal = Enable;
1127     else if (ParamName.consume_front("max-offset=")) {
1128       if (ParamName.getAsInteger(0, Result.MaxOffset))
1129         return make_error<StringError>(
1130             formatv("invalid GlobalMergePass parameter '{0}' ", ParamName)
1131                 .str(),
1132             inconvertibleErrorCode());
1133     }
1134   }
1135   return Result;
1136 }
1137 
1138 } // namespace
1139 
1140 /// Tests whether a pass name starts with a valid prefix for a default pipeline
1141 /// alias.
1142 static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1143   return Name.starts_with("default") || Name.starts_with("thinlto") ||
1144          Name.starts_with("lto");
1145 }
1146 
1147 /// Tests whether registered callbacks will accept a given pass name.
1148 ///
1149 /// When parsing a pipeline text, the type of the outermost pipeline may be
1150 /// omitted, in which case the type is automatically determined from the first
1151 /// pass name in the text. This may be a name that is handled through one of the
1152 /// callbacks. We check this through the oridinary parsing callbacks by setting
1153 /// up a dummy PassManager in order to not force the client to also handle this
1154 /// type of query.
1155 template <typename PassManagerT, typename CallbacksT>
1156 static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1157   if (!Callbacks.empty()) {
1158     PassManagerT DummyPM;
1159     for (auto &CB : Callbacks)
1160       if (CB(Name, DummyPM, {}))
1161         return true;
1162   }
1163   return false;
1164 }
1165 
1166 template <typename CallbacksT>
1167 static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
1168   // Manually handle aliases for pre-configured pipeline fragments.
1169   if (startsWithDefaultPipelineAliasPrefix(Name))
1170     return DefaultAliasRegex.match(Name);
1171 
1172   StringRef NameNoBracket = Name.take_until([](char C) { return C == '<'; });
1173 
1174   // Explicitly handle pass manager names.
1175   if (Name == "module")
1176     return true;
1177   if (Name == "cgscc")
1178     return true;
1179   if (NameNoBracket == "function")
1180     return true;
1181   if (Name == "coro-cond")
1182     return true;
1183 
1184   // Explicitly handle custom-parsed pass names.
1185   if (parseRepeatPassName(Name))
1186     return true;
1187 
1188 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
1189   if (Name == NAME)                                                            \
1190     return true;
1191 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)      \
1192   if (checkParametrizedPassName(Name, NAME))                                   \
1193     return true;
1194 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
1195   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1196     return true;
1197 #include "PassRegistry.def"
1198 
1199   return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
1200 }
1201 
1202 template <typename CallbacksT>
1203 static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
1204   // Explicitly handle pass manager names.
1205   StringRef NameNoBracket = Name.take_until([](char C) { return C == '<'; });
1206   if (Name == "cgscc")
1207     return true;
1208   if (NameNoBracket == "function")
1209     return true;
1210 
1211   // Explicitly handle custom-parsed pass names.
1212   if (parseRepeatPassName(Name))
1213     return true;
1214   if (parseDevirtPassName(Name))
1215     return true;
1216 
1217 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
1218   if (Name == NAME)                                                            \
1219     return true;
1220 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)       \
1221   if (checkParametrizedPassName(Name, NAME))                                   \
1222     return true;
1223 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
1224   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1225     return true;
1226 #include "PassRegistry.def"
1227 
1228   return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
1229 }
1230 
1231 template <typename CallbacksT>
1232 static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
1233   // Explicitly handle pass manager names.
1234   StringRef NameNoBracket = Name.take_until([](char C) { return C == '<'; });
1235   if (NameNoBracket == "function")
1236     return true;
1237   if (Name == "loop" || Name == "loop-mssa")
1238     return true;
1239 
1240   // Explicitly handle custom-parsed pass names.
1241   if (parseRepeatPassName(Name))
1242     return true;
1243 
1244 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
1245   if (Name == NAME)                                                            \
1246     return true;
1247 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)    \
1248   if (checkParametrizedPassName(Name, NAME))                                   \
1249     return true;
1250 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
1251   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1252     return true;
1253 #include "PassRegistry.def"
1254 
1255   return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
1256 }
1257 
1258 template <typename CallbacksT>
1259 static bool isLoopNestPassName(StringRef Name, CallbacksT &Callbacks,
1260                                bool &UseMemorySSA) {
1261   UseMemorySSA = false;
1262 
1263   // Explicitly handle custom-parsed pass names.
1264   if (parseRepeatPassName(Name))
1265     return true;
1266 
1267   if (checkParametrizedPassName(Name, "lnicm")) {
1268     UseMemorySSA = true;
1269     return true;
1270   }
1271 
1272 #define LOOPNEST_PASS(NAME, CREATE_PASS)                                       \
1273   if (Name == NAME)                                                            \
1274     return true;
1275 #include "PassRegistry.def"
1276 
1277   return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
1278 }
1279 
1280 template <typename CallbacksT>
1281 static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks,
1282                            bool &UseMemorySSA) {
1283   UseMemorySSA = false;
1284 
1285   // Explicitly handle custom-parsed pass names.
1286   if (parseRepeatPassName(Name))
1287     return true;
1288 
1289   if (checkParametrizedPassName(Name, "licm")) {
1290     UseMemorySSA = true;
1291     return true;
1292   }
1293 
1294 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
1295   if (Name == NAME)                                                            \
1296     return true;
1297 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
1298   if (checkParametrizedPassName(Name, NAME))                                   \
1299     return true;
1300 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
1301   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
1302     return true;
1303 #include "PassRegistry.def"
1304 
1305   return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
1306 }
1307 
1308 std::optional<std::vector<PassBuilder::PipelineElement>>
1309 PassBuilder::parsePipelineText(StringRef Text) {
1310   std::vector<PipelineElement> ResultPipeline;
1311 
1312   SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1313       &ResultPipeline};
1314   for (;;) {
1315     std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1316     size_t Pos = Text.find_first_of(",()");
1317     Pipeline.push_back({Text.substr(0, Pos), {}});
1318 
1319     // If we have a single terminating name, we're done.
1320     if (Pos == Text.npos)
1321       break;
1322 
1323     char Sep = Text[Pos];
1324     Text = Text.substr(Pos + 1);
1325     if (Sep == ',')
1326       // Just a name ending in a comma, continue.
1327       continue;
1328 
1329     if (Sep == '(') {
1330       // Push the inner pipeline onto the stack to continue processing.
1331       PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1332       continue;
1333     }
1334 
1335     assert(Sep == ')' && "Bogus separator!");
1336     // When handling the close parenthesis, we greedily consume them to avoid
1337     // empty strings in the pipeline.
1338     do {
1339       // If we try to pop the outer pipeline we have unbalanced parentheses.
1340       if (PipelineStack.size() == 1)
1341         return std::nullopt;
1342 
1343       PipelineStack.pop_back();
1344     } while (Text.consume_front(")"));
1345 
1346     // Check if we've finished parsing.
1347     if (Text.empty())
1348       break;
1349 
1350     // Otherwise, the end of an inner pipeline always has to be followed by
1351     // a comma, and then we can continue.
1352     if (!Text.consume_front(","))
1353       return std::nullopt;
1354   }
1355 
1356   if (PipelineStack.size() > 1)
1357     // Unbalanced paretheses.
1358     return std::nullopt;
1359 
1360   assert(PipelineStack.back() == &ResultPipeline &&
1361          "Wrong pipeline at the bottom of the stack!");
1362   return {std::move(ResultPipeline)};
1363 }
1364 
1365 Error PassBuilder::parseModulePass(ModulePassManager &MPM,
1366                                    const PipelineElement &E) {
1367   auto &Name = E.Name;
1368   auto &InnerPipeline = E.InnerPipeline;
1369 
1370   // First handle complex passes like the pass managers which carry pipelines.
1371   if (!InnerPipeline.empty()) {
1372     if (Name == "module") {
1373       ModulePassManager NestedMPM;
1374       if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline))
1375         return Err;
1376       MPM.addPass(std::move(NestedMPM));
1377       return Error::success();
1378     }
1379     if (Name == "coro-cond") {
1380       ModulePassManager NestedMPM;
1381       if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline))
1382         return Err;
1383       MPM.addPass(CoroConditionalWrapper(std::move(NestedMPM)));
1384       return Error::success();
1385     }
1386     if (Name == "cgscc") {
1387       CGSCCPassManager CGPM;
1388       if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline))
1389         return Err;
1390       MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
1391       return Error::success();
1392     }
1393     if (auto Params = parseFunctionPipelineName(Name)) {
1394       if (Params->second)
1395         return make_error<StringError>(
1396             "cannot have a no-rerun module to function adaptor",
1397             inconvertibleErrorCode());
1398       FunctionPassManager FPM;
1399       if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline))
1400         return Err;
1401       MPM.addPass(
1402           createModuleToFunctionPassAdaptor(std::move(FPM), Params->first));
1403       return Error::success();
1404     }
1405     if (auto Count = parseRepeatPassName(Name)) {
1406       ModulePassManager NestedMPM;
1407       if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline))
1408         return Err;
1409       MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
1410       return Error::success();
1411     }
1412 
1413     for (auto &C : ModulePipelineParsingCallbacks)
1414       if (C(Name, MPM, InnerPipeline))
1415         return Error::success();
1416 
1417     // Normal passes can't have pipelines.
1418     return make_error<StringError>(
1419         formatv("invalid use of '{0}' pass as module pipeline", Name).str(),
1420         inconvertibleErrorCode());
1421     ;
1422   }
1423 
1424   // Manually handle aliases for pre-configured pipeline fragments.
1425   if (startsWithDefaultPipelineAliasPrefix(Name)) {
1426     SmallVector<StringRef, 3> Matches;
1427     if (!DefaultAliasRegex.match(Name, &Matches))
1428       return make_error<StringError>(
1429           formatv("unknown default pipeline alias '{0}'", Name).str(),
1430           inconvertibleErrorCode());
1431 
1432     assert(Matches.size() == 3 && "Must capture two matched strings!");
1433 
1434     OptimizationLevel L = *parseOptLevel(Matches[2]);
1435 
1436     // This is consistent with old pass manager invoked via opt, but
1437     // inconsistent with clang. Clang doesn't enable loop vectorization
1438     // but does enable slp vectorization at Oz.
1439     PTO.LoopVectorization =
1440         L.getSpeedupLevel() > 1 && L != OptimizationLevel::Oz;
1441     PTO.SLPVectorization =
1442         L.getSpeedupLevel() > 1 && L != OptimizationLevel::Oz;
1443 
1444     if (Matches[1] == "default") {
1445       MPM.addPass(buildPerModuleDefaultPipeline(L));
1446     } else if (Matches[1] == "thinlto-pre-link") {
1447       MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L));
1448     } else if (Matches[1] == "thinlto") {
1449       MPM.addPass(buildThinLTODefaultPipeline(L, nullptr));
1450     } else if (Matches[1] == "lto-pre-link") {
1451       if (PTO.UnifiedLTO)
1452         // When UnifiedLTO is enabled, use the ThinLTO pre-link pipeline. This
1453         // avoids compile-time performance regressions and keeps the pre-link
1454         // LTO pipeline "unified" for both LTO modes.
1455         MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L));
1456       else
1457         MPM.addPass(buildLTOPreLinkDefaultPipeline(L));
1458     } else {
1459       assert(Matches[1] == "lto" && "Not one of the matched options!");
1460       MPM.addPass(buildLTODefaultPipeline(L, nullptr));
1461     }
1462     return Error::success();
1463   }
1464 
1465   // Finally expand the basic registered passes from the .inc file.
1466 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
1467   if (Name == NAME) {                                                          \
1468     MPM.addPass(CREATE_PASS);                                                  \
1469     return Error::success();                                                   \
1470   }
1471 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)      \
1472   if (checkParametrizedPassName(Name, NAME)) {                                 \
1473     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1474     if (!Params)                                                               \
1475       return Params.takeError();                                               \
1476     MPM.addPass(CREATE_PASS(Params.get()));                                    \
1477     return Error::success();                                                   \
1478   }
1479 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
1480   if (Name == "require<" NAME ">") {                                           \
1481     MPM.addPass(                                                               \
1482         RequireAnalysisPass<                                                   \
1483             std::remove_reference_t<decltype(CREATE_PASS)>, Module>());        \
1484     return Error::success();                                                   \
1485   }                                                                            \
1486   if (Name == "invalidate<" NAME ">") {                                        \
1487     MPM.addPass(InvalidateAnalysisPass<                                        \
1488                 std::remove_reference_t<decltype(CREATE_PASS)>>());            \
1489     return Error::success();                                                   \
1490   }
1491 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
1492   if (Name == NAME) {                                                          \
1493     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(CREATE_PASS));         \
1494     return Error::success();                                                   \
1495   }
1496 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)       \
1497   if (checkParametrizedPassName(Name, NAME)) {                                 \
1498     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1499     if (!Params)                                                               \
1500       return Params.takeError();                                               \
1501     MPM.addPass(                                                               \
1502         createModuleToPostOrderCGSCCPassAdaptor(CREATE_PASS(Params.get())));   \
1503     return Error::success();                                                   \
1504   }
1505 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
1506   if (Name == NAME) {                                                          \
1507     MPM.addPass(createModuleToFunctionPassAdaptor(CREATE_PASS));               \
1508     return Error::success();                                                   \
1509   }
1510 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)    \
1511   if (checkParametrizedPassName(Name, NAME)) {                                 \
1512     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1513     if (!Params)                                                               \
1514       return Params.takeError();                                               \
1515     MPM.addPass(createModuleToFunctionPassAdaptor(CREATE_PASS(Params.get()))); \
1516     return Error::success();                                                   \
1517   }
1518 #define LOOPNEST_PASS(NAME, CREATE_PASS)                                       \
1519   if (Name == NAME) {                                                          \
1520     MPM.addPass(createModuleToFunctionPassAdaptor(                             \
1521         createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)));          \
1522     return Error::success();                                                   \
1523   }
1524 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
1525   if (Name == NAME) {                                                          \
1526     MPM.addPass(createModuleToFunctionPassAdaptor(                             \
1527         createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)));          \
1528     return Error::success();                                                   \
1529   }
1530 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
1531   if (checkParametrizedPassName(Name, NAME)) {                                 \
1532     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1533     if (!Params)                                                               \
1534       return Params.takeError();                                               \
1535     MPM.addPass(                                                               \
1536         createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(     \
1537             CREATE_PASS(Params.get()), false, false)));                        \
1538     return Error::success();                                                   \
1539   }
1540 #include "PassRegistry.def"
1541 
1542   for (auto &C : ModulePipelineParsingCallbacks)
1543     if (C(Name, MPM, InnerPipeline))
1544       return Error::success();
1545   return make_error<StringError>(
1546       formatv("unknown module pass '{0}'", Name).str(),
1547       inconvertibleErrorCode());
1548 }
1549 
1550 Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1551                                   const PipelineElement &E) {
1552   auto &Name = E.Name;
1553   auto &InnerPipeline = E.InnerPipeline;
1554 
1555   // First handle complex passes like the pass managers which carry pipelines.
1556   if (!InnerPipeline.empty()) {
1557     if (Name == "cgscc") {
1558       CGSCCPassManager NestedCGPM;
1559       if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline))
1560         return Err;
1561       // Add the nested pass manager with the appropriate adaptor.
1562       CGPM.addPass(std::move(NestedCGPM));
1563       return Error::success();
1564     }
1565     if (auto Params = parseFunctionPipelineName(Name)) {
1566       FunctionPassManager FPM;
1567       if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline))
1568         return Err;
1569       // Add the nested pass manager with the appropriate adaptor.
1570       CGPM.addPass(createCGSCCToFunctionPassAdaptor(
1571           std::move(FPM), Params->first, Params->second));
1572       return Error::success();
1573     }
1574     if (auto Count = parseRepeatPassName(Name)) {
1575       CGSCCPassManager NestedCGPM;
1576       if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline))
1577         return Err;
1578       CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
1579       return Error::success();
1580     }
1581     if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1582       CGSCCPassManager NestedCGPM;
1583       if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline))
1584         return Err;
1585       CGPM.addPass(
1586           createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
1587       return Error::success();
1588     }
1589 
1590     for (auto &C : CGSCCPipelineParsingCallbacks)
1591       if (C(Name, CGPM, InnerPipeline))
1592         return Error::success();
1593 
1594     // Normal passes can't have pipelines.
1595     return make_error<StringError>(
1596         formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(),
1597         inconvertibleErrorCode());
1598   }
1599 
1600 // Now expand the basic registered passes from the .inc file.
1601 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
1602   if (Name == NAME) {                                                          \
1603     CGPM.addPass(CREATE_PASS);                                                 \
1604     return Error::success();                                                   \
1605   }
1606 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)       \
1607   if (checkParametrizedPassName(Name, NAME)) {                                 \
1608     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1609     if (!Params)                                                               \
1610       return Params.takeError();                                               \
1611     CGPM.addPass(CREATE_PASS(Params.get()));                                   \
1612     return Error::success();                                                   \
1613   }
1614 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
1615   if (Name == "require<" NAME ">") {                                           \
1616     CGPM.addPass(RequireAnalysisPass<                                          \
1617                  std::remove_reference_t<decltype(CREATE_PASS)>,               \
1618                  LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,    \
1619                  CGSCCUpdateResult &>());                                      \
1620     return Error::success();                                                   \
1621   }                                                                            \
1622   if (Name == "invalidate<" NAME ">") {                                        \
1623     CGPM.addPass(InvalidateAnalysisPass<                                       \
1624                  std::remove_reference_t<decltype(CREATE_PASS)>>());           \
1625     return Error::success();                                                   \
1626   }
1627 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
1628   if (Name == NAME) {                                                          \
1629     CGPM.addPass(createCGSCCToFunctionPassAdaptor(CREATE_PASS));               \
1630     return Error::success();                                                   \
1631   }
1632 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)    \
1633   if (checkParametrizedPassName(Name, NAME)) {                                 \
1634     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1635     if (!Params)                                                               \
1636       return Params.takeError();                                               \
1637     CGPM.addPass(createCGSCCToFunctionPassAdaptor(CREATE_PASS(Params.get()))); \
1638     return Error::success();                                                   \
1639   }
1640 #define LOOPNEST_PASS(NAME, CREATE_PASS)                                       \
1641   if (Name == NAME) {                                                          \
1642     CGPM.addPass(createCGSCCToFunctionPassAdaptor(                             \
1643         createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)));          \
1644     return Error::success();                                                   \
1645   }
1646 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
1647   if (Name == NAME) {                                                          \
1648     CGPM.addPass(createCGSCCToFunctionPassAdaptor(                             \
1649         createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)));          \
1650     return Error::success();                                                   \
1651   }
1652 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
1653   if (checkParametrizedPassName(Name, NAME)) {                                 \
1654     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1655     if (!Params)                                                               \
1656       return Params.takeError();                                               \
1657     CGPM.addPass(                                                              \
1658         createCGSCCToFunctionPassAdaptor(createFunctionToLoopPassAdaptor(      \
1659             CREATE_PASS(Params.get()), false, false)));                        \
1660     return Error::success();                                                   \
1661   }
1662 #include "PassRegistry.def"
1663 
1664   for (auto &C : CGSCCPipelineParsingCallbacks)
1665     if (C(Name, CGPM, InnerPipeline))
1666       return Error::success();
1667   return make_error<StringError>(
1668       formatv("unknown cgscc pass '{0}'", Name).str(),
1669       inconvertibleErrorCode());
1670 }
1671 
1672 Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1673                                      const PipelineElement &E) {
1674   auto &Name = E.Name;
1675   auto &InnerPipeline = E.InnerPipeline;
1676 
1677   // First handle complex passes like the pass managers which carry pipelines.
1678   if (!InnerPipeline.empty()) {
1679     if (Name == "function") {
1680       FunctionPassManager NestedFPM;
1681       if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline))
1682         return Err;
1683       // Add the nested pass manager with the appropriate adaptor.
1684       FPM.addPass(std::move(NestedFPM));
1685       return Error::success();
1686     }
1687     if (Name == "loop" || Name == "loop-mssa") {
1688       LoopPassManager LPM;
1689       if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline))
1690         return Err;
1691       // Add the nested pass manager with the appropriate adaptor.
1692       bool UseMemorySSA = (Name == "loop-mssa");
1693       bool UseBFI = llvm::any_of(InnerPipeline, [](auto Pipeline) {
1694         return Pipeline.Name.contains("simple-loop-unswitch");
1695       });
1696       bool UseBPI = llvm::any_of(InnerPipeline, [](auto Pipeline) {
1697         return Pipeline.Name == "loop-predication";
1698       });
1699       FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM), UseMemorySSA,
1700                                                   UseBFI, UseBPI));
1701       return Error::success();
1702     }
1703     if (auto Count = parseRepeatPassName(Name)) {
1704       FunctionPassManager NestedFPM;
1705       if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline))
1706         return Err;
1707       FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
1708       return Error::success();
1709     }
1710 
1711     for (auto &C : FunctionPipelineParsingCallbacks)
1712       if (C(Name, FPM, InnerPipeline))
1713         return Error::success();
1714 
1715     // Normal passes can't have pipelines.
1716     return make_error<StringError>(
1717         formatv("invalid use of '{0}' pass as function pipeline", Name).str(),
1718         inconvertibleErrorCode());
1719   }
1720 
1721 // Now expand the basic registered passes from the .inc file.
1722 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
1723   if (Name == NAME) {                                                          \
1724     FPM.addPass(CREATE_PASS);                                                  \
1725     return Error::success();                                                   \
1726   }
1727 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)    \
1728   if (checkParametrizedPassName(Name, NAME)) {                                 \
1729     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1730     if (!Params)                                                               \
1731       return Params.takeError();                                               \
1732     FPM.addPass(CREATE_PASS(Params.get()));                                    \
1733     return Error::success();                                                   \
1734   }
1735 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
1736   if (Name == "require<" NAME ">") {                                           \
1737     FPM.addPass(                                                               \
1738         RequireAnalysisPass<                                                   \
1739             std::remove_reference_t<decltype(CREATE_PASS)>, Function>());      \
1740     return Error::success();                                                   \
1741   }                                                                            \
1742   if (Name == "invalidate<" NAME ">") {                                        \
1743     FPM.addPass(InvalidateAnalysisPass<                                        \
1744                 std::remove_reference_t<decltype(CREATE_PASS)>>());            \
1745     return Error::success();                                                   \
1746   }
1747 // FIXME: UseMemorySSA is set to false. Maybe we could do things like:
1748 //        bool UseMemorySSA = !("canon-freeze" || "loop-predication" ||
1749 //                              "guard-widening");
1750 //        The risk is that it may become obsolete if we're not careful.
1751 #define LOOPNEST_PASS(NAME, CREATE_PASS)                                       \
1752   if (Name == NAME) {                                                          \
1753     FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS, false, false));   \
1754     return Error::success();                                                   \
1755   }
1756 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
1757   if (Name == NAME) {                                                          \
1758     FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS, false, false));   \
1759     return Error::success();                                                   \
1760   }
1761 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
1762   if (checkParametrizedPassName(Name, NAME)) {                                 \
1763     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1764     if (!Params)                                                               \
1765       return Params.takeError();                                               \
1766     FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS(Params.get()),     \
1767                                                 false, false));                \
1768     return Error::success();                                                   \
1769   }
1770 #include "PassRegistry.def"
1771 
1772   for (auto &C : FunctionPipelineParsingCallbacks)
1773     if (C(Name, FPM, InnerPipeline))
1774       return Error::success();
1775   return make_error<StringError>(
1776       formatv("unknown function pass '{0}'", Name).str(),
1777       inconvertibleErrorCode());
1778 }
1779 
1780 Error PassBuilder::parseLoopPass(LoopPassManager &LPM,
1781                                  const PipelineElement &E) {
1782   StringRef Name = E.Name;
1783   auto &InnerPipeline = E.InnerPipeline;
1784 
1785   // First handle complex passes like the pass managers which carry pipelines.
1786   if (!InnerPipeline.empty()) {
1787     if (Name == "loop") {
1788       LoopPassManager NestedLPM;
1789       if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline))
1790         return Err;
1791       // Add the nested pass manager with the appropriate adaptor.
1792       LPM.addPass(std::move(NestedLPM));
1793       return Error::success();
1794     }
1795     if (auto Count = parseRepeatPassName(Name)) {
1796       LoopPassManager NestedLPM;
1797       if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline))
1798         return Err;
1799       LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
1800       return Error::success();
1801     }
1802 
1803     for (auto &C : LoopPipelineParsingCallbacks)
1804       if (C(Name, LPM, InnerPipeline))
1805         return Error::success();
1806 
1807     // Normal passes can't have pipelines.
1808     return make_error<StringError>(
1809         formatv("invalid use of '{0}' pass as loop pipeline", Name).str(),
1810         inconvertibleErrorCode());
1811   }
1812 
1813 // Now expand the basic registered passes from the .inc file.
1814 #define LOOPNEST_PASS(NAME, CREATE_PASS)                                       \
1815   if (Name == NAME) {                                                          \
1816     LPM.addPass(CREATE_PASS);                                                  \
1817     return Error::success();                                                   \
1818   }
1819 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
1820   if (Name == NAME) {                                                          \
1821     LPM.addPass(CREATE_PASS);                                                  \
1822     return Error::success();                                                   \
1823   }
1824 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
1825   if (checkParametrizedPassName(Name, NAME)) {                                 \
1826     auto Params = parsePassParameters(PARSER, Name, NAME);                     \
1827     if (!Params)                                                               \
1828       return Params.takeError();                                               \
1829     LPM.addPass(CREATE_PASS(Params.get()));                                    \
1830     return Error::success();                                                   \
1831   }
1832 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
1833   if (Name == "require<" NAME ">") {                                           \
1834     LPM.addPass(RequireAnalysisPass<                                           \
1835                 std::remove_reference_t<decltype(CREATE_PASS)>, Loop,          \
1836                 LoopAnalysisManager, LoopStandardAnalysisResults &,            \
1837                 LPMUpdater &>());                                              \
1838     return Error::success();                                                   \
1839   }                                                                            \
1840   if (Name == "invalidate<" NAME ">") {                                        \
1841     LPM.addPass(InvalidateAnalysisPass<                                        \
1842                 std::remove_reference_t<decltype(CREATE_PASS)>>());            \
1843     return Error::success();                                                   \
1844   }
1845 #include "PassRegistry.def"
1846 
1847   for (auto &C : LoopPipelineParsingCallbacks)
1848     if (C(Name, LPM, InnerPipeline))
1849       return Error::success();
1850   return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(),
1851                                  inconvertibleErrorCode());
1852 }
1853 
1854 Error PassBuilder::parseMachinePass(MachineFunctionPassManager &MFPM,
1855                                     const PipelineElement &E) {
1856   StringRef Name = E.Name;
1857   if (!E.InnerPipeline.empty())
1858     return make_error<StringError>("invalid pipeline",
1859                                    inconvertibleErrorCode());
1860 
1861 #define MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR)                      \
1862   if (Name == NAME) {                                                          \
1863     MFPM.addPass(PASS_NAME());                                                 \
1864     return Error::success();                                                   \
1865   }
1866 #define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)                    \
1867   if (Name == NAME) {                                                          \
1868     MFPM.addPass(PASS_NAME());                                                 \
1869     return Error::success();                                                   \
1870   }
1871 #include "llvm/CodeGen/MachinePassRegistry.def"
1872 
1873   for (auto &C : MachinePipelineParsingCallbacks)
1874     if (C(Name, MFPM))
1875       return Error::success();
1876   return make_error<StringError>(
1877       formatv("unknown machine pass '{0}'", Name).str(),
1878       inconvertibleErrorCode());
1879 }
1880 
1881 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
1882 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS)                               \
1883   if (Name == NAME) {                                                          \
1884     AA.registerModuleAnalysis<                                                 \
1885         std::remove_reference_t<decltype(CREATE_PASS)>>();                     \
1886     return true;                                                               \
1887   }
1888 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS)                             \
1889   if (Name == NAME) {                                                          \
1890     AA.registerFunctionAnalysis<                                               \
1891         std::remove_reference_t<decltype(CREATE_PASS)>>();                     \
1892     return true;                                                               \
1893   }
1894 #include "PassRegistry.def"
1895 
1896   for (auto &C : AAParsingCallbacks)
1897     if (C(Name, AA))
1898       return true;
1899   return false;
1900 }
1901 
1902 Error PassBuilder::parseMachinePassPipeline(
1903     MachineFunctionPassManager &MFPM, ArrayRef<PipelineElement> Pipeline) {
1904   for (const auto &Element : Pipeline) {
1905     if (auto Err = parseMachinePass(MFPM, Element))
1906       return Err;
1907   }
1908   return Error::success();
1909 }
1910 
1911 Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
1912                                          ArrayRef<PipelineElement> Pipeline) {
1913   for (const auto &Element : Pipeline) {
1914     if (auto Err = parseLoopPass(LPM, Element))
1915       return Err;
1916   }
1917   return Error::success();
1918 }
1919 
1920 Error PassBuilder::parseFunctionPassPipeline(
1921     FunctionPassManager &FPM, ArrayRef<PipelineElement> Pipeline) {
1922   for (const auto &Element : Pipeline) {
1923     if (auto Err = parseFunctionPass(FPM, Element))
1924       return Err;
1925   }
1926   return Error::success();
1927 }
1928 
1929 Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
1930                                           ArrayRef<PipelineElement> Pipeline) {
1931   for (const auto &Element : Pipeline) {
1932     if (auto Err = parseCGSCCPass(CGPM, Element))
1933       return Err;
1934   }
1935   return Error::success();
1936 }
1937 
1938 void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1939                                        FunctionAnalysisManager &FAM,
1940                                        CGSCCAnalysisManager &CGAM,
1941                                        ModuleAnalysisManager &MAM) {
1942   MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1943   MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
1944   CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1945   FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1946   FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1947   FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1948   LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1949 }
1950 
1951 Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
1952                                            ArrayRef<PipelineElement> Pipeline) {
1953   for (const auto &Element : Pipeline) {
1954     if (auto Err = parseModulePass(MPM, Element))
1955       return Err;
1956   }
1957   return Error::success();
1958 }
1959 
1960 // Primary pass pipeline description parsing routine for a \c ModulePassManager
1961 // FIXME: Should this routine accept a TargetMachine or require the caller to
1962 // pre-populate the analysis managers with target-specific stuff?
1963 Error PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1964                                      StringRef PipelineText) {
1965   auto Pipeline = parsePipelineText(PipelineText);
1966   if (!Pipeline || Pipeline->empty())
1967     return make_error<StringError>(
1968         formatv("invalid pipeline '{0}'", PipelineText).str(),
1969         inconvertibleErrorCode());
1970 
1971   // If the first name isn't at the module layer, wrap the pipeline up
1972   // automatically.
1973   StringRef FirstName = Pipeline->front().Name;
1974 
1975   if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1976     bool UseMemorySSA;
1977     if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
1978       Pipeline = {{"cgscc", std::move(*Pipeline)}};
1979     } else if (isFunctionPassName(FirstName,
1980                                   FunctionPipelineParsingCallbacks)) {
1981       Pipeline = {{"function", std::move(*Pipeline)}};
1982     } else if (isLoopNestPassName(FirstName, LoopPipelineParsingCallbacks,
1983                                   UseMemorySSA)) {
1984       Pipeline = {{"function", {{UseMemorySSA ? "loop-mssa" : "loop",
1985                                  std::move(*Pipeline)}}}};
1986     } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks,
1987                               UseMemorySSA)) {
1988       Pipeline = {{"function", {{UseMemorySSA ? "loop-mssa" : "loop",
1989                                  std::move(*Pipeline)}}}};
1990     } else {
1991       for (auto &C : TopLevelPipelineParsingCallbacks)
1992         if (C(MPM, *Pipeline))
1993           return Error::success();
1994 
1995       // Unknown pass or pipeline name!
1996       auto &InnerPipeline = Pipeline->front().InnerPipeline;
1997       return make_error<StringError>(
1998           formatv("unknown {0} name '{1}'",
1999                   (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName)
2000               .str(),
2001           inconvertibleErrorCode());
2002     }
2003   }
2004 
2005   if (auto Err = parseModulePassPipeline(MPM, *Pipeline))
2006     return Err;
2007   return Error::success();
2008 }
2009 
2010 // Primary pass pipeline description parsing routine for a \c CGSCCPassManager
2011 Error PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
2012                                      StringRef PipelineText) {
2013   auto Pipeline = parsePipelineText(PipelineText);
2014   if (!Pipeline || Pipeline->empty())
2015     return make_error<StringError>(
2016         formatv("invalid pipeline '{0}'", PipelineText).str(),
2017         inconvertibleErrorCode());
2018 
2019   StringRef FirstName = Pipeline->front().Name;
2020   if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
2021     return make_error<StringError>(
2022         formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName,
2023                 PipelineText)
2024             .str(),
2025         inconvertibleErrorCode());
2026 
2027   if (auto Err = parseCGSCCPassPipeline(CGPM, *Pipeline))
2028     return Err;
2029   return Error::success();
2030 }
2031 
2032 // Primary pass pipeline description parsing routine for a \c
2033 // FunctionPassManager
2034 Error PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
2035                                      StringRef PipelineText) {
2036   auto Pipeline = parsePipelineText(PipelineText);
2037   if (!Pipeline || Pipeline->empty())
2038     return make_error<StringError>(
2039         formatv("invalid pipeline '{0}'", PipelineText).str(),
2040         inconvertibleErrorCode());
2041 
2042   StringRef FirstName = Pipeline->front().Name;
2043   if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
2044     return make_error<StringError>(
2045         formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName,
2046                 PipelineText)
2047             .str(),
2048         inconvertibleErrorCode());
2049 
2050   if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline))
2051     return Err;
2052   return Error::success();
2053 }
2054 
2055 // Primary pass pipeline description parsing routine for a \c LoopPassManager
2056 Error PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
2057                                      StringRef PipelineText) {
2058   auto Pipeline = parsePipelineText(PipelineText);
2059   if (!Pipeline || Pipeline->empty())
2060     return make_error<StringError>(
2061         formatv("invalid pipeline '{0}'", PipelineText).str(),
2062         inconvertibleErrorCode());
2063 
2064   if (auto Err = parseLoopPassPipeline(CGPM, *Pipeline))
2065     return Err;
2066 
2067   return Error::success();
2068 }
2069 
2070 Error PassBuilder::parsePassPipeline(MachineFunctionPassManager &MFPM,
2071                                      StringRef PipelineText) {
2072   auto Pipeline = parsePipelineText(PipelineText);
2073   if (!Pipeline || Pipeline->empty())
2074     return make_error<StringError>(
2075         formatv("invalid machine pass pipeline '{0}'", PipelineText).str(),
2076         inconvertibleErrorCode());
2077 
2078   if (auto Err = parseMachinePassPipeline(MFPM, *Pipeline))
2079     return Err;
2080 
2081   return Error::success();
2082 }
2083 
2084 Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
2085   // If the pipeline just consists of the word 'default' just replace the AA
2086   // manager with our default one.
2087   if (PipelineText == "default") {
2088     AA = buildDefaultAAPipeline();
2089     return Error::success();
2090   }
2091 
2092   while (!PipelineText.empty()) {
2093     StringRef Name;
2094     std::tie(Name, PipelineText) = PipelineText.split(',');
2095     if (!parseAAPassName(AA, Name))
2096       return make_error<StringError>(
2097           formatv("unknown alias analysis name '{0}'", Name).str(),
2098           inconvertibleErrorCode());
2099   }
2100 
2101   return Error::success();
2102 }
2103 
2104 static void printPassName(StringRef PassName, raw_ostream &OS) {
2105   OS << "  " << PassName << "\n";
2106 }
2107 static void printPassName(StringRef PassName, StringRef Params,
2108                           raw_ostream &OS) {
2109   OS << "  " << PassName << "<" << Params << ">\n";
2110 }
2111 
2112 void PassBuilder::printPassNames(raw_ostream &OS) {
2113   // TODO: print pass descriptions when they are available
2114 
2115   OS << "Module passes:\n";
2116 #define MODULE_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2117 #include "PassRegistry.def"
2118 
2119   OS << "Module passes with params:\n";
2120 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)      \
2121   printPassName(NAME, PARAMS, OS);
2122 #include "PassRegistry.def"
2123 
2124   OS << "Module analyses:\n";
2125 #define MODULE_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2126 #include "PassRegistry.def"
2127 
2128   OS << "Module alias analyses:\n";
2129 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2130 #include "PassRegistry.def"
2131 
2132   OS << "CGSCC passes:\n";
2133 #define CGSCC_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2134 #include "PassRegistry.def"
2135 
2136   OS << "CGSCC passes with params:\n";
2137 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)       \
2138   printPassName(NAME, PARAMS, OS);
2139 #include "PassRegistry.def"
2140 
2141   OS << "CGSCC analyses:\n";
2142 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2143 #include "PassRegistry.def"
2144 
2145   OS << "Function passes:\n";
2146 #define FUNCTION_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2147 #include "PassRegistry.def"
2148 
2149   OS << "Function passes with params:\n";
2150 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)    \
2151   printPassName(NAME, PARAMS, OS);
2152 #include "PassRegistry.def"
2153 
2154   OS << "Function analyses:\n";
2155 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2156 #include "PassRegistry.def"
2157 
2158   OS << "Function alias analyses:\n";
2159 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2160 #include "PassRegistry.def"
2161 
2162   OS << "LoopNest passes:\n";
2163 #define LOOPNEST_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2164 #include "PassRegistry.def"
2165 
2166   OS << "Loop passes:\n";
2167 #define LOOP_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2168 #include "PassRegistry.def"
2169 
2170   OS << "Loop passes with params:\n";
2171 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS)        \
2172   printPassName(NAME, PARAMS, OS);
2173 #include "PassRegistry.def"
2174 
2175   OS << "Loop analyses:\n";
2176 #define LOOP_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2177 #include "PassRegistry.def"
2178 
2179   OS << "Machine module passes (WIP):\n";
2180 #define MACHINE_MODULE_PASS(NAME, PASS_NAME, CONSTRUCTOR)                      \
2181   printPassName(NAME, OS);
2182 #include "llvm/CodeGen/MachinePassRegistry.def"
2183 
2184   OS << "Machine function passes (WIP):\n";
2185 #define MACHINE_FUNCTION_PASS(NAME, PASS_NAME, CONSTRUCTOR)                    \
2186   printPassName(NAME, OS);
2187 #include "llvm/CodeGen/MachinePassRegistry.def"
2188 
2189   OS << "Machine function analyses (WIP):\n";
2190 #define MACHINE_FUNCTION_ANALYSIS(NAME, PASS_NAME, CONSTRUCTOR)                \
2191   printPassName(NAME, OS);
2192 #include "llvm/CodeGen/MachinePassRegistry.def"
2193 }
2194 
2195 void PassBuilder::registerParseTopLevelPipelineCallback(
2196     const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>
2197         &C) {
2198   TopLevelPipelineParsingCallbacks.push_back(C);
2199 }
2200