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