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