[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / lib / Passes / PassBuilder.cpp
blobfde759026e5d78093338d7cedde1144e05fc98aa
1 //===- Parsing and selection of pass pipelines ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// This file provides the implementation of the PassBuilder based on our
11 /// static pass registry as well as related functionality. It also provides
12 /// helpers to aid in analyzing, debugging, and testing passes and pass
13 /// pipelines.
14 ///
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Passes/PassBuilder.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
20 #include "llvm/Analysis/AliasSetTracker.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/BasicAliasAnalysis.h"
23 #include "llvm/Analysis/BlockFrequencyInfo.h"
24 #include "llvm/Analysis/BranchProbabilityInfo.h"
25 #include "llvm/Analysis/CFGPrinter.h"
26 #include "llvm/Analysis/CFGSCCPrinter.h"
27 #include "llvm/Analysis/CGSCCPassManager.h"
28 #include "llvm/Analysis/CallGraph.h"
29 #include "llvm/Analysis/CallPrinter.h"
30 #include "llvm/Analysis/CostModel.h"
31 #include "llvm/Analysis/CycleAnalysis.h"
32 #include "llvm/Analysis/DDG.h"
33 #include "llvm/Analysis/DDGPrinter.h"
34 #include "llvm/Analysis/Delinearization.h"
35 #include "llvm/Analysis/DemandedBits.h"
36 #include "llvm/Analysis/DependenceAnalysis.h"
37 #include "llvm/Analysis/DomPrinter.h"
38 #include "llvm/Analysis/DominanceFrontier.h"
39 #include "llvm/Analysis/FunctionPropertiesAnalysis.h"
40 #include "llvm/Analysis/GlobalsModRef.h"
41 #include "llvm/Analysis/IRSimilarityIdentifier.h"
42 #include "llvm/Analysis/IVUsers.h"
43 #include "llvm/Analysis/InlineAdvisor.h"
44 #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h"
45 #include "llvm/Analysis/InstCount.h"
46 #include "llvm/Analysis/LazyCallGraph.h"
47 #include "llvm/Analysis/LazyValueInfo.h"
48 #include "llvm/Analysis/Lint.h"
49 #include "llvm/Analysis/LoopAccessAnalysis.h"
50 #include "llvm/Analysis/LoopCacheAnalysis.h"
51 #include "llvm/Analysis/LoopInfo.h"
52 #include "llvm/Analysis/LoopNestAnalysis.h"
53 #include "llvm/Analysis/MemDerefPrinter.h"
54 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
55 #include "llvm/Analysis/MemorySSA.h"
56 #include "llvm/Analysis/ModuleDebugInfoPrinter.h"
57 #include "llvm/Analysis/ModuleSummaryAnalysis.h"
58 #include "llvm/Analysis/MustExecute.h"
59 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
60 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
61 #include "llvm/Analysis/PhiValues.h"
62 #include "llvm/Analysis/PostDominators.h"
63 #include "llvm/Analysis/ProfileSummaryInfo.h"
64 #include "llvm/Analysis/RegionInfo.h"
65 #include "llvm/Analysis/ScalarEvolution.h"
66 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
67 #include "llvm/Analysis/ScopedNoAliasAA.h"
68 #include "llvm/Analysis/StackLifetime.h"
69 #include "llvm/Analysis/StackSafetyAnalysis.h"
70 #include "llvm/Analysis/StructuralHash.h"
71 #include "llvm/Analysis/TargetLibraryInfo.h"
72 #include "llvm/Analysis/TargetTransformInfo.h"
73 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
74 #include "llvm/Analysis/UniformityAnalysis.h"
75 #include "llvm/CodeGen/HardwareLoops.h"
76 #include "llvm/CodeGen/TypePromotion.h"
77 #include "llvm/IR/DebugInfo.h"
78 #include "llvm/IR/Dominators.h"
79 #include "llvm/IR/PassManager.h"
80 #include "llvm/IR/PrintPasses.h"
81 #include "llvm/IR/SafepointIRVerifier.h"
82 #include "llvm/IR/Verifier.h"
83 #include "llvm/IRPrinter/IRPrintingPasses.h"
84 #include "llvm/Passes/OptimizationLevel.h"
85 #include "llvm/Support/CommandLine.h"
86 #include "llvm/Support/Debug.h"
87 #include "llvm/Support/ErrorHandling.h"
88 #include "llvm/Support/FormatVariadic.h"
89 #include "llvm/Support/Regex.h"
90 #include "llvm/Target/TargetMachine.h"
91 #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
92 #include "llvm/Transforms/Coroutines/CoroCleanup.h"
93 #include "llvm/Transforms/Coroutines/CoroConditionalWrapper.h"
94 #include "llvm/Transforms/Coroutines/CoroEarly.h"
95 #include "llvm/Transforms/Coroutines/CoroElide.h"
96 #include "llvm/Transforms/Coroutines/CoroSplit.h"
97 #include "llvm/Transforms/HipStdPar/HipStdPar.h"
98 #include "llvm/Transforms/IPO/AlwaysInliner.h"
99 #include "llvm/Transforms/IPO/Annotation2Metadata.h"
100 #include "llvm/Transforms/IPO/ArgumentPromotion.h"
101 #include "llvm/Transforms/IPO/Attributor.h"
102 #include "llvm/Transforms/IPO/BlockExtractor.h"
103 #include "llvm/Transforms/IPO/CalledValuePropagation.h"
104 #include "llvm/Transforms/IPO/ConstantMerge.h"
105 #include "llvm/Transforms/IPO/CrossDSOCFI.h"
106 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
107 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
108 #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
109 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
110 #include "llvm/Transforms/IPO/FunctionAttrs.h"
111 #include "llvm/Transforms/IPO/FunctionImport.h"
112 #include "llvm/Transforms/IPO/GlobalDCE.h"
113 #include "llvm/Transforms/IPO/GlobalOpt.h"
114 #include "llvm/Transforms/IPO/GlobalSplit.h"
115 #include "llvm/Transforms/IPO/HotColdSplitting.h"
116 #include "llvm/Transforms/IPO/IROutliner.h"
117 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
118 #include "llvm/Transforms/IPO/Inliner.h"
119 #include "llvm/Transforms/IPO/Internalize.h"
120 #include "llvm/Transforms/IPO/LoopExtractor.h"
121 #include "llvm/Transforms/IPO/LowerTypeTests.h"
122 #include "llvm/Transforms/IPO/MemProfContextDisambiguation.h"
123 #include "llvm/Transforms/IPO/MergeFunctions.h"
124 #include "llvm/Transforms/IPO/ModuleInliner.h"
125 #include "llvm/Transforms/IPO/OpenMPOpt.h"
126 #include "llvm/Transforms/IPO/PartialInlining.h"
127 #include "llvm/Transforms/IPO/SCCP.h"
128 #include "llvm/Transforms/IPO/SampleProfile.h"
129 #include "llvm/Transforms/IPO/SampleProfileProbe.h"
130 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
131 #include "llvm/Transforms/IPO/StripSymbols.h"
132 #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
133 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
134 #include "llvm/Transforms/InstCombine/InstCombine.h"
135 #include "llvm/Transforms/Instrumentation.h"
136 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
137 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
138 #include "llvm/Transforms/Instrumentation/CGProfile.h"
139 #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
140 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
141 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
142 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
143 #include "llvm/Transforms/Instrumentation/InstrOrderFile.h"
144 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
145 #include "llvm/Transforms/Instrumentation/KCFI.h"
146 #include "llvm/Transforms/Instrumentation/MemProfiler.h"
147 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
148 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
149 #include "llvm/Transforms/Instrumentation/PoisonChecking.h"
150 #include "llvm/Transforms/Instrumentation/SanitizerBinaryMetadata.h"
151 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
152 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
153 #include "llvm/Transforms/ObjCARC.h"
154 #include "llvm/Transforms/Scalar/ADCE.h"
155 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
156 #include "llvm/Transforms/Scalar/AnnotationRemarks.h"
157 #include "llvm/Transforms/Scalar/BDCE.h"
158 #include "llvm/Transforms/Scalar/CallSiteSplitting.h"
159 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
160 #include "llvm/Transforms/Scalar/ConstraintElimination.h"
161 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
162 #include "llvm/Transforms/Scalar/DCE.h"
163 #include "llvm/Transforms/Scalar/DFAJumpThreading.h"
164 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
165 #include "llvm/Transforms/Scalar/DivRemPairs.h"
166 #include "llvm/Transforms/Scalar/EarlyCSE.h"
167 #include "llvm/Transforms/Scalar/FlattenCFG.h"
168 #include "llvm/Transforms/Scalar/Float2Int.h"
169 #include "llvm/Transforms/Scalar/GVN.h"
170 #include "llvm/Transforms/Scalar/GuardWidening.h"
171 #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
172 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
173 #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
174 #include "llvm/Transforms/Scalar/InferAddressSpaces.h"
175 #include "llvm/Transforms/Scalar/InferAlignment.h"
176 #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
177 #include "llvm/Transforms/Scalar/JumpThreading.h"
178 #include "llvm/Transforms/Scalar/LICM.h"
179 #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
180 #include "llvm/Transforms/Scalar/LoopBoundSplit.h"
181 #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
182 #include "llvm/Transforms/Scalar/LoopDeletion.h"
183 #include "llvm/Transforms/Scalar/LoopDistribute.h"
184 #include "llvm/Transforms/Scalar/LoopFlatten.h"
185 #include "llvm/Transforms/Scalar/LoopFuse.h"
186 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
187 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
188 #include "llvm/Transforms/Scalar/LoopInterchange.h"
189 #include "llvm/Transforms/Scalar/LoopLoadElimination.h"
190 #include "llvm/Transforms/Scalar/LoopPassManager.h"
191 #include "llvm/Transforms/Scalar/LoopPredication.h"
192 #include "llvm/Transforms/Scalar/LoopReroll.h"
193 #include "llvm/Transforms/Scalar/LoopRotation.h"
194 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
195 #include "llvm/Transforms/Scalar/LoopSink.h"
196 #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
197 #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
198 #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
199 #include "llvm/Transforms/Scalar/LoopVersioningLICM.h"
200 #include "llvm/Transforms/Scalar/LowerAtomicPass.h"
201 #include "llvm/Transforms/Scalar/LowerConstantIntrinsics.h"
202 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
203 #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
204 #include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h"
205 #include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
206 #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
207 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
208 #include "llvm/Transforms/Scalar/MergeICmps.h"
209 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
210 #include "llvm/Transforms/Scalar/NaryReassociate.h"
211 #include "llvm/Transforms/Scalar/NewGVN.h"
212 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
213 #include "llvm/Transforms/Scalar/PlaceSafepoints.h"
214 #include "llvm/Transforms/Scalar/Reassociate.h"
215 #include "llvm/Transforms/Scalar/Reg2Mem.h"
216 #include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
217 #include "llvm/Transforms/Scalar/SCCP.h"
218 #include "llvm/Transforms/Scalar/SROA.h"
219 #include "llvm/Transforms/Scalar/ScalarizeMaskedMemIntrin.h"
220 #include "llvm/Transforms/Scalar/Scalarizer.h"
221 #include "llvm/Transforms/Scalar/SeparateConstOffsetFromGEP.h"
222 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
223 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
224 #include "llvm/Transforms/Scalar/Sink.h"
225 #include "llvm/Transforms/Scalar/SpeculativeExecution.h"
226 #include "llvm/Transforms/Scalar/StraightLineStrengthReduce.h"
227 #include "llvm/Transforms/Scalar/StructurizeCFG.h"
228 #include "llvm/Transforms/Scalar/TLSVariableHoist.h"
229 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
230 #include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
231 #include "llvm/Transforms/Utils/AddDiscriminators.h"
232 #include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
233 #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
234 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
235 #include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h"
236 #include "llvm/Transforms/Utils/CountVisits.h"
237 #include "llvm/Transforms/Utils/Debugify.h"
238 #include "llvm/Transforms/Utils/DXILUpgrade.h"
239 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
240 #include "llvm/Transforms/Utils/FixIrreducible.h"
241 #include "llvm/Transforms/Utils/HelloWorld.h"
242 #include "llvm/Transforms/Utils/InjectTLIMappings.h"
243 #include "llvm/Transforms/Utils/InstructionNamer.h"
244 #include "llvm/Transforms/Utils/LCSSA.h"
245 #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
246 #include "llvm/Transforms/Utils/LoopSimplify.h"
247 #include "llvm/Transforms/Utils/LoopVersioning.h"
248 #include "llvm/Transforms/Utils/LowerGlobalDtors.h"
249 #include "llvm/Transforms/Utils/LowerIFunc.h"
250 #include "llvm/Transforms/Utils/LowerInvoke.h"
251 #include "llvm/Transforms/Utils/LowerSwitch.h"
252 #include "llvm/Transforms/Utils/Mem2Reg.h"
253 #include "llvm/Transforms/Utils/MetaRenamer.h"
254 #include "llvm/Transforms/Utils/MoveAutoInit.h"
255 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
256 #include "llvm/Transforms/Utils/PredicateInfo.h"
257 #include "llvm/Transforms/Utils/RelLookupTableConverter.h"
258 #include "llvm/Transforms/Utils/StripGCRelocates.h"
259 #include "llvm/Transforms/Utils/StripNonLineTableDebugInfo.h"
260 #include "llvm/Transforms/Utils/SymbolRewriter.h"
261 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
262 #include "llvm/Transforms/Utils/UnifyLoopExits.h"
263 #include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h"
264 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
265 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
266 #include "llvm/Transforms/Vectorize/VectorCombine.h"
267 #include <optional>
269 using namespace llvm;
271 static const Regex DefaultAliasRegex(
272 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
274 namespace llvm {
275 cl::opt<bool> PrintPipelinePasses(
276 "print-pipeline-passes",
277 cl::desc("Print a '-passes' compatible string describing the pipeline "
278 "(best-effort only)."));
279 } // namespace llvm
281 namespace {
283 // The following passes/analyses have custom names, otherwise their name will
284 // include `(anonymous namespace)`. These are special since they are only for
285 // testing purposes and don't live in a header file.
287 /// No-op module pass which does nothing.
288 struct NoOpModulePass : PassInfoMixin<NoOpModulePass> {
289 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
290 return PreservedAnalyses::all();
293 static StringRef name() { return "NoOpModulePass"; }
296 /// No-op module analysis.
297 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
298 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
299 static AnalysisKey Key;
301 public:
302 struct Result {};
303 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
304 static StringRef name() { return "NoOpModuleAnalysis"; }
307 /// No-op CGSCC pass which does nothing.
308 struct NoOpCGSCCPass : PassInfoMixin<NoOpCGSCCPass> {
309 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
310 LazyCallGraph &, CGSCCUpdateResult &UR) {
311 return PreservedAnalyses::all();
313 static StringRef name() { return "NoOpCGSCCPass"; }
316 /// No-op CGSCC analysis.
317 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
318 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
319 static AnalysisKey Key;
321 public:
322 struct Result {};
323 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
324 return Result();
326 static StringRef name() { return "NoOpCGSCCAnalysis"; }
329 /// No-op function pass which does nothing.
330 struct NoOpFunctionPass : PassInfoMixin<NoOpFunctionPass> {
331 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
332 return PreservedAnalyses::all();
334 static StringRef name() { return "NoOpFunctionPass"; }
337 /// No-op function analysis.
338 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
339 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
340 static AnalysisKey Key;
342 public:
343 struct Result {};
344 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
345 static StringRef name() { return "NoOpFunctionAnalysis"; }
348 /// No-op loop nest pass which does nothing.
349 struct NoOpLoopNestPass : PassInfoMixin<NoOpLoopNestPass> {
350 PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &,
351 LoopStandardAnalysisResults &, LPMUpdater &) {
352 return PreservedAnalyses::all();
354 static StringRef name() { return "NoOpLoopNestPass"; }
357 /// No-op loop pass which does nothing.
358 struct NoOpLoopPass : PassInfoMixin<NoOpLoopPass> {
359 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
360 LoopStandardAnalysisResults &, LPMUpdater &) {
361 return PreservedAnalyses::all();
363 static StringRef name() { return "NoOpLoopPass"; }
366 /// No-op loop analysis.
367 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
368 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
369 static AnalysisKey Key;
371 public:
372 struct Result {};
373 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
374 return Result();
376 static StringRef name() { return "NoOpLoopAnalysis"; }
379 AnalysisKey NoOpModuleAnalysis::Key;
380 AnalysisKey NoOpCGSCCAnalysis::Key;
381 AnalysisKey NoOpFunctionAnalysis::Key;
382 AnalysisKey NoOpLoopAnalysis::Key;
384 /// Whether or not we should populate a PassInstrumentationCallbacks's class to
385 /// pass name map.
387 /// This is for optimization purposes so we don't populate it if we never use
388 /// it. This should be updated if new pass instrumentation wants to use the map.
389 /// We currently only use this for --print-before/after.
390 bool shouldPopulateClassToPassNames() {
391 return PrintPipelinePasses || !printBeforePasses().empty() ||
392 !printAfterPasses().empty() || !isFilterPassesEmpty();
395 // A pass for testing -print-on-crash.
396 // DO NOT USE THIS EXCEPT FOR TESTING!
397 class TriggerCrashPass : public PassInfoMixin<TriggerCrashPass> {
398 public:
399 PreservedAnalyses run(Module &, ModuleAnalysisManager &) {
400 abort();
401 return PreservedAnalyses::all();
403 static StringRef name() { return "TriggerCrashPass"; }
406 } // namespace
408 PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
409 std::optional<PGOOptions> PGOOpt,
410 PassInstrumentationCallbacks *PIC)
411 : TM(TM), PTO(PTO), PGOOpt(PGOOpt), PIC(PIC) {
412 if (TM)
413 TM->registerPassBuilderCallbacks(*this);
414 if (PIC && shouldPopulateClassToPassNames()) {
415 #define MODULE_PASS(NAME, CREATE_PASS) \
416 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
417 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
418 PIC->addClassToPassName(CLASS, NAME);
419 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
420 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
421 #define FUNCTION_PASS(NAME, CREATE_PASS) \
422 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
423 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
424 PIC->addClassToPassName(CLASS, NAME);
425 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
426 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
427 #define LOOPNEST_PASS(NAME, CREATE_PASS) \
428 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
429 #define LOOP_PASS(NAME, CREATE_PASS) \
430 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
431 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
432 PIC->addClassToPassName(CLASS, NAME);
433 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
434 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
435 #define CGSCC_PASS(NAME, CREATE_PASS) \
436 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
437 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
438 PIC->addClassToPassName(CLASS, NAME);
439 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
440 PIC->addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
441 #include "PassRegistry.def"
445 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
446 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
447 MAM.registerPass([&] { return CREATE_PASS; });
448 #include "PassRegistry.def"
450 for (auto &C : ModuleAnalysisRegistrationCallbacks)
451 C(MAM);
454 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
455 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
456 CGAM.registerPass([&] { return CREATE_PASS; });
457 #include "PassRegistry.def"
459 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
460 C(CGAM);
463 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
464 // We almost always want the default alias analysis pipeline.
465 // If a user wants a different one, they can register their own before calling
466 // registerFunctionAnalyses().
467 FAM.registerPass([&] { return buildDefaultAAPipeline(); });
469 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
470 FAM.registerPass([&] { return CREATE_PASS; });
471 #include "PassRegistry.def"
473 for (auto &C : FunctionAnalysisRegistrationCallbacks)
474 C(FAM);
477 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
478 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
479 LAM.registerPass([&] { return CREATE_PASS; });
480 #include "PassRegistry.def"
482 for (auto &C : LoopAnalysisRegistrationCallbacks)
483 C(LAM);
486 static std::optional<int> parseRepeatPassName(StringRef Name) {
487 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
488 return std::nullopt;
489 int Count;
490 if (Name.getAsInteger(0, Count) || Count <= 0)
491 return std::nullopt;
492 return Count;
495 static std::optional<std::pair<bool, bool>>
496 parseFunctionPipelineName(StringRef Name) {
497 std::pair<bool, bool> Params;
498 if (!Name.consume_front("function"))
499 return std::nullopt;
500 if (Name.empty())
501 return Params;
502 if (!Name.consume_front("<") || !Name.consume_back(">"))
503 return std::nullopt;
504 while (!Name.empty()) {
505 auto [Front, Back] = Name.split(';');
506 Name = Back;
507 if (Front == "eager-inv")
508 Params.first = true;
509 else if (Front == "no-rerun")
510 Params.second = true;
511 else
512 return std::nullopt;
514 return Params;
517 static std::optional<int> parseDevirtPassName(StringRef Name) {
518 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
519 return std::nullopt;
520 int Count;
521 if (Name.getAsInteger(0, Count) || Count < 0)
522 return std::nullopt;
523 return Count;
526 static bool checkParametrizedPassName(StringRef Name, StringRef PassName) {
527 if (!Name.consume_front(PassName))
528 return false;
529 // normal pass name w/o parameters == default parameters
530 if (Name.empty())
531 return true;
532 return Name.startswith("<") && Name.endswith(">");
535 static std::optional<OptimizationLevel> parseOptLevel(StringRef S) {
536 return StringSwitch<std::optional<OptimizationLevel>>(S)
537 .Case("O0", OptimizationLevel::O0)
538 .Case("O1", OptimizationLevel::O1)
539 .Case("O2", OptimizationLevel::O2)
540 .Case("O3", OptimizationLevel::O3)
541 .Case("Os", OptimizationLevel::Os)
542 .Case("Oz", OptimizationLevel::Oz)
543 .Default(std::nullopt);
546 namespace {
548 /// This performs customized parsing of pass name with parameters.
550 /// We do not need parametrization of passes in textual pipeline very often,
551 /// yet on a rare occasion ability to specify parameters right there can be
552 /// useful.
554 /// \p Name - parameterized specification of a pass from a textual pipeline
555 /// is a string in a form of :
556 /// PassName '<' parameter-list '>'
558 /// Parameter list is being parsed by the parser callable argument, \p Parser,
559 /// It takes a string-ref of parameters and returns either StringError or a
560 /// parameter list in a form of a custom parameters type, all wrapped into
561 /// Expected<> template class.
563 template <typename ParametersParseCallableT>
564 auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name,
565 StringRef PassName) -> decltype(Parser(StringRef{})) {
566 using ParametersT = typename decltype(Parser(StringRef{}))::value_type;
568 StringRef Params = Name;
569 if (!Params.consume_front(PassName)) {
570 assert(false &&
571 "unable to strip pass name from parametrized pass specification");
573 if (!Params.empty() &&
574 (!Params.consume_front("<") || !Params.consume_back(">"))) {
575 assert(false && "invalid format for parametrized pass name");
578 Expected<ParametersT> Result = Parser(Params);
579 assert((Result || Result.template errorIsA<StringError>()) &&
580 "Pass parameter parser can only return StringErrors.");
581 return Result;
584 /// Parser of parameters for HardwareLoops pass.
585 Expected<HardwareLoopOptions> parseHardwareLoopOptions(StringRef Params) {
586 HardwareLoopOptions HardwareLoopOpts;
588 while (!Params.empty()) {
589 StringRef ParamName;
590 std::tie(ParamName, Params) = Params.split(';');
591 if (ParamName.consume_front("hardware-loop-decrement=")) {
592 int Count;
593 if (ParamName.getAsInteger(0, Count))
594 return make_error<StringError>(
595 formatv("invalid HardwareLoopPass parameter '{0}' ", ParamName).str(),
596 inconvertibleErrorCode());
597 HardwareLoopOpts.setDecrement(Count);
598 continue;
600 if (ParamName.consume_front("hardware-loop-counter-bitwidth=")) {
601 int Count;
602 if (ParamName.getAsInteger(0, Count))
603 return make_error<StringError>(
604 formatv("invalid HardwareLoopPass parameter '{0}' ", ParamName).str(),
605 inconvertibleErrorCode());
606 HardwareLoopOpts.setCounterBitwidth(Count);
607 continue;
609 if (ParamName == "force-hardware-loops") {
610 HardwareLoopOpts.setForce(true);
611 } else if (ParamName == "force-hardware-loop-phi") {
612 HardwareLoopOpts.setForcePhi(true);
613 } else if (ParamName == "force-nested-hardware-loop") {
614 HardwareLoopOpts.setForceNested(true);
615 } else if (ParamName == "force-hardware-loop-guard") {
616 HardwareLoopOpts.setForceGuard(true);
617 } else {
618 return make_error<StringError>(
619 formatv("invalid HardwarePass parameter '{0}' ", ParamName).str(),
620 inconvertibleErrorCode());
623 return HardwareLoopOpts;
626 /// Parser of parameters for LoopUnroll pass.
627 Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) {
628 LoopUnrollOptions UnrollOpts;
629 while (!Params.empty()) {
630 StringRef ParamName;
631 std::tie(ParamName, Params) = Params.split(';');
632 std::optional<OptimizationLevel> OptLevel = parseOptLevel(ParamName);
633 // Don't accept -Os/-Oz.
634 if (OptLevel && !OptLevel->isOptimizingForSize()) {
635 UnrollOpts.setOptLevel(OptLevel->getSpeedupLevel());
636 continue;
638 if (ParamName.consume_front("full-unroll-max=")) {
639 int Count;
640 if (ParamName.getAsInteger(0, Count))
641 return make_error<StringError>(
642 formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
643 inconvertibleErrorCode());
644 UnrollOpts.setFullUnrollMaxCount(Count);
645 continue;
648 bool Enable = !ParamName.consume_front("no-");
649 if (ParamName == "partial") {
650 UnrollOpts.setPartial(Enable);
651 } else if (ParamName == "peeling") {
652 UnrollOpts.setPeeling(Enable);
653 } else if (ParamName == "profile-peeling") {
654 UnrollOpts.setProfileBasedPeeling(Enable);
655 } else if (ParamName == "runtime") {
656 UnrollOpts.setRuntime(Enable);
657 } else if (ParamName == "upperbound") {
658 UnrollOpts.setUpperBound(Enable);
659 } else {
660 return make_error<StringError>(
661 formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
662 inconvertibleErrorCode());
665 return UnrollOpts;
668 Expected<bool> parseSinglePassOption(StringRef Params, StringRef OptionName,
669 StringRef PassName) {
670 bool Result = false;
671 while (!Params.empty()) {
672 StringRef ParamName;
673 std::tie(ParamName, Params) = Params.split(';');
675 if (ParamName == OptionName) {
676 Result = true;
677 } else {
678 return make_error<StringError>(
679 formatv("invalid {1} pass parameter '{0}' ", ParamName, PassName)
680 .str(),
681 inconvertibleErrorCode());
684 return Result;
687 Expected<bool> parseGlobalDCEPassOptions(StringRef Params) {
688 return parseSinglePassOption(Params, "vfe-linkage-unit-visibility", "GlobalDCE");
691 Expected<bool> parseInlinerPassOptions(StringRef Params) {
692 return parseSinglePassOption(Params, "only-mandatory", "InlinerPass");
695 Expected<bool> parseCoroSplitPassOptions(StringRef Params) {
696 return parseSinglePassOption(Params, "reuse-storage", "CoroSplitPass");
699 Expected<bool> parsePostOrderFunctionAttrsPassOptions(StringRef Params) {
700 return parseSinglePassOption(Params, "skip-non-recursive-function-attrs",
701 "PostOrderFunctionAttrs");
704 Expected<bool> parseEarlyCSEPassOptions(StringRef Params) {
705 return parseSinglePassOption(Params, "memssa", "EarlyCSE");
708 Expected<bool> parseEntryExitInstrumenterPassOptions(StringRef Params) {
709 return parseSinglePassOption(Params, "post-inline", "EntryExitInstrumenter");
712 Expected<bool> parseLoopExtractorPassOptions(StringRef Params) {
713 return parseSinglePassOption(Params, "single", "LoopExtractor");
716 Expected<bool> parseLowerMatrixIntrinsicsPassOptions(StringRef Params) {
717 return parseSinglePassOption(Params, "minimal", "LowerMatrixIntrinsics");
720 Expected<AddressSanitizerOptions> parseASanPassOptions(StringRef Params) {
721 AddressSanitizerOptions Result;
722 while (!Params.empty()) {
723 StringRef ParamName;
724 std::tie(ParamName, Params) = Params.split(';');
726 if (ParamName == "kernel") {
727 Result.CompileKernel = true;
728 } else {
729 return make_error<StringError>(
730 formatv("invalid AddressSanitizer pass parameter '{0}' ", ParamName)
731 .str(),
732 inconvertibleErrorCode());
735 return Result;
738 Expected<HWAddressSanitizerOptions> parseHWASanPassOptions(StringRef Params) {
739 HWAddressSanitizerOptions Result;
740 while (!Params.empty()) {
741 StringRef ParamName;
742 std::tie(ParamName, Params) = Params.split(';');
744 if (ParamName == "recover") {
745 Result.Recover = true;
746 } else if (ParamName == "kernel") {
747 Result.CompileKernel = true;
748 } else {
749 return make_error<StringError>(
750 formatv("invalid HWAddressSanitizer pass parameter '{0}' ", ParamName)
751 .str(),
752 inconvertibleErrorCode());
755 return Result;
758 Expected<EmbedBitcodeOptions> parseEmbedBitcodePassOptions(StringRef Params) {
759 EmbedBitcodeOptions Result;
760 while (!Params.empty()) {
761 StringRef ParamName;
762 std::tie(ParamName, Params) = Params.split(';');
764 if (ParamName == "thinlto") {
765 Result.IsThinLTO = true;
766 } else if (ParamName == "emit-summary") {
767 Result.EmitLTOSummary = true;
768 } else {
769 return make_error<StringError>(
770 formatv("invalid EmbedBitcode pass parameter '{0}' ", ParamName)
771 .str(),
772 inconvertibleErrorCode());
775 return Result;
778 Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
779 MemorySanitizerOptions Result;
780 while (!Params.empty()) {
781 StringRef ParamName;
782 std::tie(ParamName, Params) = Params.split(';');
784 if (ParamName == "recover") {
785 Result.Recover = true;
786 } else if (ParamName == "kernel") {
787 Result.Kernel = true;
788 } else if (ParamName.consume_front("track-origins=")) {
789 if (ParamName.getAsInteger(0, Result.TrackOrigins))
790 return make_error<StringError>(
791 formatv("invalid argument to MemorySanitizer pass track-origins "
792 "parameter: '{0}' ",
793 ParamName)
794 .str(),
795 inconvertibleErrorCode());
796 } else if (ParamName == "eager-checks") {
797 Result.EagerChecks = true;
798 } else {
799 return make_error<StringError>(
800 formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName)
801 .str(),
802 inconvertibleErrorCode());
805 return Result;
808 /// Parser of parameters for SimplifyCFG pass.
809 Expected<SimplifyCFGOptions> parseSimplifyCFGOptions(StringRef Params) {
810 SimplifyCFGOptions Result;
811 while (!Params.empty()) {
812 StringRef ParamName;
813 std::tie(ParamName, Params) = Params.split(';');
815 bool Enable = !ParamName.consume_front("no-");
816 if (ParamName == "speculate-blocks") {
817 Result.speculateBlocks(Enable);
818 } else if (ParamName == "simplify-cond-branch") {
819 Result.setSimplifyCondBranch(Enable);
820 } else if (ParamName == "forward-switch-cond") {
821 Result.forwardSwitchCondToPhi(Enable);
822 } else if (ParamName == "switch-range-to-icmp") {
823 Result.convertSwitchRangeToICmp(Enable);
824 } else if (ParamName == "switch-to-lookup") {
825 Result.convertSwitchToLookupTable(Enable);
826 } else if (ParamName == "keep-loops") {
827 Result.needCanonicalLoops(Enable);
828 } else if (ParamName == "hoist-common-insts") {
829 Result.hoistCommonInsts(Enable);
830 } else if (ParamName == "sink-common-insts") {
831 Result.sinkCommonInsts(Enable);
832 } else if (Enable && ParamName.consume_front("bonus-inst-threshold=")) {
833 APInt BonusInstThreshold;
834 if (ParamName.getAsInteger(0, BonusInstThreshold))
835 return make_error<StringError>(
836 formatv("invalid argument to SimplifyCFG pass bonus-threshold "
837 "parameter: '{0}' ",
838 ParamName).str(),
839 inconvertibleErrorCode());
840 Result.bonusInstThreshold(BonusInstThreshold.getSExtValue());
841 } else {
842 return make_error<StringError>(
843 formatv("invalid SimplifyCFG pass parameter '{0}' ", ParamName).str(),
844 inconvertibleErrorCode());
847 return Result;
850 Expected<InstCombineOptions> parseInstCombineOptions(StringRef Params) {
851 InstCombineOptions Result;
852 // When specifying "instcombine" in -passes enable fix-point verification by
853 // default, as this is what most tests should use.
854 Result.setVerifyFixpoint(true);
855 while (!Params.empty()) {
856 StringRef ParamName;
857 std::tie(ParamName, Params) = Params.split(';');
859 bool Enable = !ParamName.consume_front("no-");
860 if (ParamName == "use-loop-info") {
861 Result.setUseLoopInfo(Enable);
862 } else if (ParamName == "verify-fixpoint") {
863 Result.setVerifyFixpoint(Enable);
864 } else if (Enable && ParamName.consume_front("max-iterations=")) {
865 APInt MaxIterations;
866 if (ParamName.getAsInteger(0, MaxIterations))
867 return make_error<StringError>(
868 formatv("invalid argument to InstCombine pass max-iterations "
869 "parameter: '{0}' ",
870 ParamName).str(),
871 inconvertibleErrorCode());
872 Result.setMaxIterations((unsigned)MaxIterations.getZExtValue());
873 } else {
874 return make_error<StringError>(
875 formatv("invalid InstCombine pass parameter '{0}' ", ParamName).str(),
876 inconvertibleErrorCode());
879 return Result;
882 /// Parser of parameters for LoopVectorize pass.
883 Expected<LoopVectorizeOptions> parseLoopVectorizeOptions(StringRef Params) {
884 LoopVectorizeOptions Opts;
885 while (!Params.empty()) {
886 StringRef ParamName;
887 std::tie(ParamName, Params) = Params.split(';');
889 bool Enable = !ParamName.consume_front("no-");
890 if (ParamName == "interleave-forced-only") {
891 Opts.setInterleaveOnlyWhenForced(Enable);
892 } else if (ParamName == "vectorize-forced-only") {
893 Opts.setVectorizeOnlyWhenForced(Enable);
894 } else {
895 return make_error<StringError>(
896 formatv("invalid LoopVectorize parameter '{0}' ", ParamName).str(),
897 inconvertibleErrorCode());
900 return Opts;
903 Expected<std::pair<bool, bool>> parseLoopUnswitchOptions(StringRef Params) {
904 std::pair<bool, bool> Result = {false, true};
905 while (!Params.empty()) {
906 StringRef ParamName;
907 std::tie(ParamName, Params) = Params.split(';');
909 bool Enable = !ParamName.consume_front("no-");
910 if (ParamName == "nontrivial") {
911 Result.first = Enable;
912 } else if (ParamName == "trivial") {
913 Result.second = Enable;
914 } else {
915 return make_error<StringError>(
916 formatv("invalid LoopUnswitch pass parameter '{0}' ", ParamName)
917 .str(),
918 inconvertibleErrorCode());
921 return Result;
924 Expected<LICMOptions> parseLICMOptions(StringRef Params) {
925 LICMOptions Result;
926 while (!Params.empty()) {
927 StringRef ParamName;
928 std::tie(ParamName, Params) = Params.split(';');
930 bool Enable = !ParamName.consume_front("no-");
931 if (ParamName == "allowspeculation") {
932 Result.AllowSpeculation = Enable;
933 } else {
934 return make_error<StringError>(
935 formatv("invalid LICM pass parameter '{0}' ", ParamName).str(),
936 inconvertibleErrorCode());
939 return Result;
942 Expected<std::pair<bool, bool>> parseLoopRotateOptions(StringRef Params) {
943 std::pair<bool, bool> Result = {true, false};
944 while (!Params.empty()) {
945 StringRef ParamName;
946 std::tie(ParamName, Params) = Params.split(';');
948 bool Enable = !ParamName.consume_front("no-");
949 if (ParamName == "header-duplication") {
950 Result.first = Enable;
951 } else if (ParamName == "prepare-for-lto") {
952 Result.second = Enable;
953 } else {
954 return make_error<StringError>(
955 formatv("invalid LoopRotate pass parameter '{0}' ", ParamName).str(),
956 inconvertibleErrorCode());
959 return Result;
962 Expected<bool> parseMergedLoadStoreMotionOptions(StringRef Params) {
963 bool Result = false;
964 while (!Params.empty()) {
965 StringRef ParamName;
966 std::tie(ParamName, Params) = Params.split(';');
968 bool Enable = !ParamName.consume_front("no-");
969 if (ParamName == "split-footer-bb") {
970 Result = Enable;
971 } else {
972 return make_error<StringError>(
973 formatv("invalid MergedLoadStoreMotion pass parameter '{0}' ",
974 ParamName)
975 .str(),
976 inconvertibleErrorCode());
979 return Result;
982 Expected<GVNOptions> parseGVNOptions(StringRef Params) {
983 GVNOptions Result;
984 while (!Params.empty()) {
985 StringRef ParamName;
986 std::tie(ParamName, Params) = Params.split(';');
988 bool Enable = !ParamName.consume_front("no-");
989 if (ParamName == "pre") {
990 Result.setPRE(Enable);
991 } else if (ParamName == "load-pre") {
992 Result.setLoadPRE(Enable);
993 } else if (ParamName == "split-backedge-load-pre") {
994 Result.setLoadPRESplitBackedge(Enable);
995 } else if (ParamName == "memdep") {
996 Result.setMemDep(Enable);
997 } else {
998 return make_error<StringError>(
999 formatv("invalid GVN pass parameter '{0}' ", ParamName).str(),
1000 inconvertibleErrorCode());
1003 return Result;
1006 Expected<IPSCCPOptions> parseIPSCCPOptions(StringRef Params) {
1007 IPSCCPOptions Result;
1008 while (!Params.empty()) {
1009 StringRef ParamName;
1010 std::tie(ParamName, Params) = Params.split(';');
1012 bool Enable = !ParamName.consume_front("no-");
1013 if (ParamName == "func-spec")
1014 Result.setFuncSpec(Enable);
1015 else
1016 return make_error<StringError>(
1017 formatv("invalid IPSCCP pass parameter '{0}' ", ParamName).str(),
1018 inconvertibleErrorCode());
1020 return Result;
1023 Expected<SROAOptions> parseSROAOptions(StringRef Params) {
1024 if (Params.empty() || Params == "modify-cfg")
1025 return SROAOptions::ModifyCFG;
1026 if (Params == "preserve-cfg")
1027 return SROAOptions::PreserveCFG;
1028 return make_error<StringError>(
1029 formatv("invalid SROA pass parameter '{0}' (either preserve-cfg or "
1030 "modify-cfg can be specified)",
1031 Params)
1032 .str(),
1033 inconvertibleErrorCode());
1036 Expected<StackLifetime::LivenessType>
1037 parseStackLifetimeOptions(StringRef Params) {
1038 StackLifetime::LivenessType Result = StackLifetime::LivenessType::May;
1039 while (!Params.empty()) {
1040 StringRef ParamName;
1041 std::tie(ParamName, Params) = Params.split(';');
1043 if (ParamName == "may") {
1044 Result = StackLifetime::LivenessType::May;
1045 } else if (ParamName == "must") {
1046 Result = StackLifetime::LivenessType::Must;
1047 } else {
1048 return make_error<StringError>(
1049 formatv("invalid StackLifetime parameter '{0}' ", ParamName).str(),
1050 inconvertibleErrorCode());
1053 return Result;
1056 Expected<bool> parseDependenceAnalysisPrinterOptions(StringRef Params) {
1057 return parseSinglePassOption(Params, "normalized-results",
1058 "DependenceAnalysisPrinter");
1061 Expected<bool> parseSeparateConstOffsetFromGEPPassOptions(StringRef Params) {
1062 return parseSinglePassOption(Params, "lower-gep",
1063 "SeparateConstOffsetFromGEP");
1066 Expected<OptimizationLevel>
1067 parseFunctionSimplificationPipelineOptions(StringRef Params) {
1068 std::optional<OptimizationLevel> L = parseOptLevel(Params);
1069 if (!L || *L == OptimizationLevel::O0) {
1070 return make_error<StringError>(
1071 formatv("invalid function-simplification parameter '{0}' ", Params)
1072 .str(),
1073 inconvertibleErrorCode());
1075 return *L;
1078 Expected<bool> parseMemorySSAPrinterPassOptions(StringRef Params) {
1079 return parseSinglePassOption(Params, "no-ensure-optimized-uses",
1080 "MemorySSAPrinterPass");
1083 Expected<std::string> parseMemProfUsePassOptions(StringRef Params) {
1084 std::string Result;
1085 while (!Params.empty()) {
1086 StringRef ParamName;
1087 std::tie(ParamName, Params) = Params.split(';');
1089 if (ParamName.consume_front("profile-filename=")) {
1090 Result = ParamName.str();
1091 } else {
1092 return make_error<StringError>(
1093 formatv("invalid MemProfUse pass parameter '{0}' ", ParamName).str(),
1094 inconvertibleErrorCode());
1097 return Result;
1100 Expected<bool> parseStructuralHashPrinterPassOptions(StringRef Params) {
1101 return parseSinglePassOption(Params, "detailed",
1102 "StructuralHashPrinterPass");
1105 } // namespace
1107 /// Tests whether a pass name starts with a valid prefix for a default pipeline
1108 /// alias.
1109 static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1110 return Name.startswith("default") || Name.startswith("thinlto") ||
1111 Name.startswith("lto");
1114 /// Tests whether registered callbacks will accept a given pass name.
1116 /// When parsing a pipeline text, the type of the outermost pipeline may be
1117 /// omitted, in which case the type is automatically determined from the first
1118 /// pass name in the text. This may be a name that is handled through one of the
1119 /// callbacks. We check this through the oridinary parsing callbacks by setting
1120 /// up a dummy PassManager in order to not force the client to also handle this
1121 /// type of query.
1122 template <typename PassManagerT, typename CallbacksT>
1123 static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1124 if (!Callbacks.empty()) {
1125 PassManagerT DummyPM;
1126 for (auto &CB : Callbacks)
1127 if (CB(Name, DummyPM, {}))
1128 return true;
1130 return false;
1133 template <typename CallbacksT>
1134 static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
1135 // Manually handle aliases for pre-configured pipeline fragments.
1136 if (startsWithDefaultPipelineAliasPrefix(Name))
1137 return DefaultAliasRegex.match(Name);
1139 StringRef NameNoBracket = Name.take_until([](char C) { return C == '<'; });
1141 // Explicitly handle pass manager names.
1142 if (Name == "module")
1143 return true;
1144 if (Name == "cgscc")
1145 return true;
1146 if (NameNoBracket == "function")
1147 return true;
1148 if (Name == "coro-cond")
1149 return true;
1151 // Explicitly handle custom-parsed pass names.
1152 if (parseRepeatPassName(Name))
1153 return true;
1155 #define MODULE_PASS(NAME, CREATE_PASS) \
1156 if (Name == NAME) \
1157 return true;
1158 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1159 if (checkParametrizedPassName(Name, NAME)) \
1160 return true;
1161 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1162 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1163 return true;
1164 #include "PassRegistry.def"
1166 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
1169 template <typename CallbacksT>
1170 static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
1171 // Explicitly handle pass manager names.
1172 StringRef NameNoBracket = Name.take_until([](char C) { return C == '<'; });
1173 if (Name == "cgscc")
1174 return true;
1175 if (NameNoBracket == "function")
1176 return true;
1178 // Explicitly handle custom-parsed pass names.
1179 if (parseRepeatPassName(Name))
1180 return true;
1181 if (parseDevirtPassName(Name))
1182 return true;
1184 #define CGSCC_PASS(NAME, CREATE_PASS) \
1185 if (Name == NAME) \
1186 return true;
1187 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1188 if (checkParametrizedPassName(Name, NAME)) \
1189 return true;
1190 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1191 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1192 return true;
1193 #include "PassRegistry.def"
1195 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
1198 template <typename CallbacksT>
1199 static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
1200 // Explicitly handle pass manager names.
1201 StringRef NameNoBracket = Name.take_until([](char C) { return C == '<'; });
1202 if (NameNoBracket == "function")
1203 return true;
1204 if (Name == "loop" || Name == "loop-mssa")
1205 return true;
1207 // Explicitly handle custom-parsed pass names.
1208 if (parseRepeatPassName(Name))
1209 return true;
1211 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1212 if (Name == NAME) \
1213 return true;
1214 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1215 if (checkParametrizedPassName(Name, NAME)) \
1216 return true;
1217 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1218 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1219 return true;
1220 #include "PassRegistry.def"
1222 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
1225 template <typename CallbacksT>
1226 static bool isLoopNestPassName(StringRef Name, CallbacksT &Callbacks,
1227 bool &UseMemorySSA) {
1228 UseMemorySSA = false;
1230 // Explicitly handle custom-parsed pass names.
1231 if (parseRepeatPassName(Name))
1232 return true;
1234 if (checkParametrizedPassName(Name, "lnicm")) {
1235 UseMemorySSA = true;
1236 return true;
1239 #define LOOPNEST_PASS(NAME, CREATE_PASS) \
1240 if (Name == NAME) \
1241 return true;
1242 #include "PassRegistry.def"
1244 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
1247 template <typename CallbacksT>
1248 static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks,
1249 bool &UseMemorySSA) {
1250 UseMemorySSA = false;
1252 // Explicitly handle custom-parsed pass names.
1253 if (parseRepeatPassName(Name))
1254 return true;
1256 if (checkParametrizedPassName(Name, "licm")) {
1257 UseMemorySSA = true;
1258 return true;
1261 #define LOOP_PASS(NAME, CREATE_PASS) \
1262 if (Name == NAME) \
1263 return true;
1264 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1265 if (checkParametrizedPassName(Name, NAME)) \
1266 return true;
1267 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1268 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1269 return true;
1270 #include "PassRegistry.def"
1272 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
1275 std::optional<std::vector<PassBuilder::PipelineElement>>
1276 PassBuilder::parsePipelineText(StringRef Text) {
1277 std::vector<PipelineElement> ResultPipeline;
1279 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1280 &ResultPipeline};
1281 for (;;) {
1282 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1283 size_t Pos = Text.find_first_of(",()");
1284 Pipeline.push_back({Text.substr(0, Pos), {}});
1286 // If we have a single terminating name, we're done.
1287 if (Pos == Text.npos)
1288 break;
1290 char Sep = Text[Pos];
1291 Text = Text.substr(Pos + 1);
1292 if (Sep == ',')
1293 // Just a name ending in a comma, continue.
1294 continue;
1296 if (Sep == '(') {
1297 // Push the inner pipeline onto the stack to continue processing.
1298 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1299 continue;
1302 assert(Sep == ')' && "Bogus separator!");
1303 // When handling the close parenthesis, we greedily consume them to avoid
1304 // empty strings in the pipeline.
1305 do {
1306 // If we try to pop the outer pipeline we have unbalanced parentheses.
1307 if (PipelineStack.size() == 1)
1308 return std::nullopt;
1310 PipelineStack.pop_back();
1311 } while (Text.consume_front(")"));
1313 // Check if we've finished parsing.
1314 if (Text.empty())
1315 break;
1317 // Otherwise, the end of an inner pipeline always has to be followed by
1318 // a comma, and then we can continue.
1319 if (!Text.consume_front(","))
1320 return std::nullopt;
1323 if (PipelineStack.size() > 1)
1324 // Unbalanced paretheses.
1325 return std::nullopt;
1327 assert(PipelineStack.back() == &ResultPipeline &&
1328 "Wrong pipeline at the bottom of the stack!");
1329 return {std::move(ResultPipeline)};
1332 Error PassBuilder::parseModulePass(ModulePassManager &MPM,
1333 const PipelineElement &E) {
1334 auto &Name = E.Name;
1335 auto &InnerPipeline = E.InnerPipeline;
1337 // First handle complex passes like the pass managers which carry pipelines.
1338 if (!InnerPipeline.empty()) {
1339 if (Name == "module") {
1340 ModulePassManager NestedMPM;
1341 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline))
1342 return Err;
1343 MPM.addPass(std::move(NestedMPM));
1344 return Error::success();
1346 if (Name == "coro-cond") {
1347 ModulePassManager NestedMPM;
1348 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline))
1349 return Err;
1350 MPM.addPass(CoroConditionalWrapper(std::move(NestedMPM)));
1351 return Error::success();
1353 if (Name == "cgscc") {
1354 CGSCCPassManager CGPM;
1355 if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline))
1356 return Err;
1357 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
1358 return Error::success();
1360 if (auto Params = parseFunctionPipelineName(Name)) {
1361 if (Params->second)
1362 return make_error<StringError>(
1363 "cannot have a no-rerun module to function adaptor",
1364 inconvertibleErrorCode());
1365 FunctionPassManager FPM;
1366 if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline))
1367 return Err;
1368 MPM.addPass(
1369 createModuleToFunctionPassAdaptor(std::move(FPM), Params->first));
1370 return Error::success();
1372 if (auto Count = parseRepeatPassName(Name)) {
1373 ModulePassManager NestedMPM;
1374 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline))
1375 return Err;
1376 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
1377 return Error::success();
1380 for (auto &C : ModulePipelineParsingCallbacks)
1381 if (C(Name, MPM, InnerPipeline))
1382 return Error::success();
1384 // Normal passes can't have pipelines.
1385 return make_error<StringError>(
1386 formatv("invalid use of '{0}' pass as module pipeline", Name).str(),
1387 inconvertibleErrorCode());
1391 // Manually handle aliases for pre-configured pipeline fragments.
1392 if (startsWithDefaultPipelineAliasPrefix(Name)) {
1393 SmallVector<StringRef, 3> Matches;
1394 if (!DefaultAliasRegex.match(Name, &Matches))
1395 return make_error<StringError>(
1396 formatv("unknown default pipeline alias '{0}'", Name).str(),
1397 inconvertibleErrorCode());
1399 assert(Matches.size() == 3 && "Must capture two matched strings!");
1401 OptimizationLevel L = *parseOptLevel(Matches[2]);
1403 // This is consistent with old pass manager invoked via opt, but
1404 // inconsistent with clang. Clang doesn't enable loop vectorization
1405 // but does enable slp vectorization at Oz.
1406 PTO.LoopVectorization =
1407 L.getSpeedupLevel() > 1 && L != OptimizationLevel::Oz;
1408 PTO.SLPVectorization =
1409 L.getSpeedupLevel() > 1 && L != OptimizationLevel::Oz;
1411 if (Matches[1] == "default") {
1412 MPM.addPass(buildPerModuleDefaultPipeline(L));
1413 } else if (Matches[1] == "thinlto-pre-link") {
1414 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L));
1415 } else if (Matches[1] == "thinlto") {
1416 MPM.addPass(buildThinLTODefaultPipeline(L, nullptr));
1417 } else if (Matches[1] == "lto-pre-link") {
1418 if (PTO.UnifiedLTO)
1419 // When UnifiedLTO is enabled, use the ThinLTO pre-link pipeline. This
1420 // avoids compile-time performance regressions and keeps the pre-link
1421 // LTO pipeline "unified" for both LTO modes.
1422 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L));
1423 else
1424 MPM.addPass(buildLTOPreLinkDefaultPipeline(L));
1425 } else {
1426 assert(Matches[1] == "lto" && "Not one of the matched options!");
1427 MPM.addPass(buildLTODefaultPipeline(L, nullptr));
1429 return Error::success();
1432 // Finally expand the basic registered passes from the .inc file.
1433 #define MODULE_PASS(NAME, CREATE_PASS) \
1434 if (Name == NAME) { \
1435 MPM.addPass(CREATE_PASS); \
1436 return Error::success(); \
1438 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1439 if (checkParametrizedPassName(Name, NAME)) { \
1440 auto Params = parsePassParameters(PARSER, Name, NAME); \
1441 if (!Params) \
1442 return Params.takeError(); \
1443 MPM.addPass(CREATE_PASS(Params.get())); \
1444 return Error::success(); \
1446 #define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1447 if (Name == "require<" NAME ">") { \
1448 MPM.addPass( \
1449 RequireAnalysisPass< \
1450 std::remove_reference_t<decltype(CREATE_PASS)>, Module>()); \
1451 return Error::success(); \
1453 if (Name == "invalidate<" NAME ">") { \
1454 MPM.addPass(InvalidateAnalysisPass< \
1455 std::remove_reference_t<decltype(CREATE_PASS)>>()); \
1456 return Error::success(); \
1458 #define CGSCC_PASS(NAME, CREATE_PASS) \
1459 if (Name == NAME) { \
1460 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(CREATE_PASS)); \
1461 return Error::success(); \
1463 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1464 if (checkParametrizedPassName(Name, NAME)) { \
1465 auto Params = parsePassParameters(PARSER, Name, NAME); \
1466 if (!Params) \
1467 return Params.takeError(); \
1468 MPM.addPass( \
1469 createModuleToPostOrderCGSCCPassAdaptor(CREATE_PASS(Params.get()))); \
1470 return Error::success(); \
1472 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1473 if (Name == NAME) { \
1474 MPM.addPass(createModuleToFunctionPassAdaptor(CREATE_PASS)); \
1475 return Error::success(); \
1477 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1478 if (checkParametrizedPassName(Name, NAME)) { \
1479 auto Params = parsePassParameters(PARSER, Name, NAME); \
1480 if (!Params) \
1481 return Params.takeError(); \
1482 MPM.addPass(createModuleToFunctionPassAdaptor(CREATE_PASS(Params.get()))); \
1483 return Error::success(); \
1485 #define LOOPNEST_PASS(NAME, CREATE_PASS) \
1486 if (Name == NAME) { \
1487 MPM.addPass(createModuleToFunctionPassAdaptor( \
1488 createFunctionToLoopPassAdaptor(CREATE_PASS, false, false))); \
1489 return Error::success(); \
1491 #define LOOP_PASS(NAME, CREATE_PASS) \
1492 if (Name == NAME) { \
1493 MPM.addPass(createModuleToFunctionPassAdaptor( \
1494 createFunctionToLoopPassAdaptor(CREATE_PASS, false, false))); \
1495 return Error::success(); \
1497 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1498 if (checkParametrizedPassName(Name, NAME)) { \
1499 auto Params = parsePassParameters(PARSER, Name, NAME); \
1500 if (!Params) \
1501 return Params.takeError(); \
1502 MPM.addPass( \
1503 createModuleToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( \
1504 CREATE_PASS(Params.get()), false, false))); \
1505 return Error::success(); \
1507 #include "PassRegistry.def"
1509 for (auto &C : ModulePipelineParsingCallbacks)
1510 if (C(Name, MPM, InnerPipeline))
1511 return Error::success();
1512 return make_error<StringError>(
1513 formatv("unknown module pass '{0}'", Name).str(),
1514 inconvertibleErrorCode());
1517 Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1518 const PipelineElement &E) {
1519 auto &Name = E.Name;
1520 auto &InnerPipeline = E.InnerPipeline;
1522 // First handle complex passes like the pass managers which carry pipelines.
1523 if (!InnerPipeline.empty()) {
1524 if (Name == "cgscc") {
1525 CGSCCPassManager NestedCGPM;
1526 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline))
1527 return Err;
1528 // Add the nested pass manager with the appropriate adaptor.
1529 CGPM.addPass(std::move(NestedCGPM));
1530 return Error::success();
1532 if (auto Params = parseFunctionPipelineName(Name)) {
1533 FunctionPassManager FPM;
1534 if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline))
1535 return Err;
1536 // Add the nested pass manager with the appropriate adaptor.
1537 CGPM.addPass(createCGSCCToFunctionPassAdaptor(
1538 std::move(FPM), Params->first, Params->second));
1539 return Error::success();
1541 if (auto Count = parseRepeatPassName(Name)) {
1542 CGSCCPassManager NestedCGPM;
1543 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline))
1544 return Err;
1545 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
1546 return Error::success();
1548 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1549 CGSCCPassManager NestedCGPM;
1550 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline))
1551 return Err;
1552 CGPM.addPass(
1553 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
1554 return Error::success();
1557 for (auto &C : CGSCCPipelineParsingCallbacks)
1558 if (C(Name, CGPM, InnerPipeline))
1559 return Error::success();
1561 // Normal passes can't have pipelines.
1562 return make_error<StringError>(
1563 formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(),
1564 inconvertibleErrorCode());
1567 // Now expand the basic registered passes from the .inc file.
1568 #define CGSCC_PASS(NAME, CREATE_PASS) \
1569 if (Name == NAME) { \
1570 CGPM.addPass(CREATE_PASS); \
1571 return Error::success(); \
1573 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1574 if (checkParametrizedPassName(Name, NAME)) { \
1575 auto Params = parsePassParameters(PARSER, Name, NAME); \
1576 if (!Params) \
1577 return Params.takeError(); \
1578 CGPM.addPass(CREATE_PASS(Params.get())); \
1579 return Error::success(); \
1581 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1582 if (Name == "require<" NAME ">") { \
1583 CGPM.addPass(RequireAnalysisPass< \
1584 std::remove_reference_t<decltype(CREATE_PASS)>, \
1585 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1586 CGSCCUpdateResult &>()); \
1587 return Error::success(); \
1589 if (Name == "invalidate<" NAME ">") { \
1590 CGPM.addPass(InvalidateAnalysisPass< \
1591 std::remove_reference_t<decltype(CREATE_PASS)>>()); \
1592 return Error::success(); \
1594 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1595 if (Name == NAME) { \
1596 CGPM.addPass(createCGSCCToFunctionPassAdaptor(CREATE_PASS)); \
1597 return Error::success(); \
1599 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1600 if (checkParametrizedPassName(Name, NAME)) { \
1601 auto Params = parsePassParameters(PARSER, Name, NAME); \
1602 if (!Params) \
1603 return Params.takeError(); \
1604 CGPM.addPass(createCGSCCToFunctionPassAdaptor(CREATE_PASS(Params.get()))); \
1605 return Error::success(); \
1607 #define LOOPNEST_PASS(NAME, CREATE_PASS) \
1608 if (Name == NAME) { \
1609 CGPM.addPass(createCGSCCToFunctionPassAdaptor( \
1610 createFunctionToLoopPassAdaptor(CREATE_PASS, false, false))); \
1611 return Error::success(); \
1613 #define LOOP_PASS(NAME, CREATE_PASS) \
1614 if (Name == NAME) { \
1615 CGPM.addPass(createCGSCCToFunctionPassAdaptor( \
1616 createFunctionToLoopPassAdaptor(CREATE_PASS, false, false))); \
1617 return Error::success(); \
1619 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1620 if (checkParametrizedPassName(Name, NAME)) { \
1621 auto Params = parsePassParameters(PARSER, Name, NAME); \
1622 if (!Params) \
1623 return Params.takeError(); \
1624 CGPM.addPass( \
1625 createCGSCCToFunctionPassAdaptor(createFunctionToLoopPassAdaptor( \
1626 CREATE_PASS(Params.get()), false, false))); \
1627 return Error::success(); \
1629 #include "PassRegistry.def"
1631 for (auto &C : CGSCCPipelineParsingCallbacks)
1632 if (C(Name, CGPM, InnerPipeline))
1633 return Error::success();
1634 return make_error<StringError>(
1635 formatv("unknown cgscc pass '{0}'", Name).str(),
1636 inconvertibleErrorCode());
1639 Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1640 const PipelineElement &E) {
1641 auto &Name = E.Name;
1642 auto &InnerPipeline = E.InnerPipeline;
1644 // First handle complex passes like the pass managers which carry pipelines.
1645 if (!InnerPipeline.empty()) {
1646 if (Name == "function") {
1647 FunctionPassManager NestedFPM;
1648 if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline))
1649 return Err;
1650 // Add the nested pass manager with the appropriate adaptor.
1651 FPM.addPass(std::move(NestedFPM));
1652 return Error::success();
1654 if (Name == "loop" || Name == "loop-mssa") {
1655 LoopPassManager LPM;
1656 if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline))
1657 return Err;
1658 // Add the nested pass manager with the appropriate adaptor.
1659 bool UseMemorySSA = (Name == "loop-mssa");
1660 bool UseBFI = llvm::any_of(InnerPipeline, [](auto Pipeline) {
1661 return Pipeline.Name.contains("simple-loop-unswitch");
1663 bool UseBPI = llvm::any_of(InnerPipeline, [](auto Pipeline) {
1664 return Pipeline.Name == "loop-predication";
1666 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM), UseMemorySSA,
1667 UseBFI, UseBPI));
1668 return Error::success();
1670 if (auto Count = parseRepeatPassName(Name)) {
1671 FunctionPassManager NestedFPM;
1672 if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline))
1673 return Err;
1674 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
1675 return Error::success();
1678 for (auto &C : FunctionPipelineParsingCallbacks)
1679 if (C(Name, FPM, InnerPipeline))
1680 return Error::success();
1682 // Normal passes can't have pipelines.
1683 return make_error<StringError>(
1684 formatv("invalid use of '{0}' pass as function pipeline", Name).str(),
1685 inconvertibleErrorCode());
1688 // Now expand the basic registered passes from the .inc file.
1689 #define FUNCTION_PASS(NAME, CREATE_PASS) \
1690 if (Name == NAME) { \
1691 FPM.addPass(CREATE_PASS); \
1692 return Error::success(); \
1694 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1695 if (checkParametrizedPassName(Name, NAME)) { \
1696 auto Params = parsePassParameters(PARSER, Name, NAME); \
1697 if (!Params) \
1698 return Params.takeError(); \
1699 FPM.addPass(CREATE_PASS(Params.get())); \
1700 return Error::success(); \
1702 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1703 if (Name == "require<" NAME ">") { \
1704 FPM.addPass( \
1705 RequireAnalysisPass< \
1706 std::remove_reference_t<decltype(CREATE_PASS)>, Function>()); \
1707 return Error::success(); \
1709 if (Name == "invalidate<" NAME ">") { \
1710 FPM.addPass(InvalidateAnalysisPass< \
1711 std::remove_reference_t<decltype(CREATE_PASS)>>()); \
1712 return Error::success(); \
1714 // FIXME: UseMemorySSA is set to false. Maybe we could do things like:
1715 // bool UseMemorySSA = !("canon-freeze" || "loop-predication" ||
1716 // "guard-widening");
1717 // The risk is that it may become obsolete if we're not careful.
1718 #define LOOPNEST_PASS(NAME, CREATE_PASS) \
1719 if (Name == NAME) { \
1720 FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)); \
1721 return Error::success(); \
1723 #define LOOP_PASS(NAME, CREATE_PASS) \
1724 if (Name == NAME) { \
1725 FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS, false, false)); \
1726 return Error::success(); \
1728 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1729 if (checkParametrizedPassName(Name, NAME)) { \
1730 auto Params = parsePassParameters(PARSER, Name, NAME); \
1731 if (!Params) \
1732 return Params.takeError(); \
1733 FPM.addPass(createFunctionToLoopPassAdaptor(CREATE_PASS(Params.get()), \
1734 false, false)); \
1735 return Error::success(); \
1737 #include "PassRegistry.def"
1739 for (auto &C : FunctionPipelineParsingCallbacks)
1740 if (C(Name, FPM, InnerPipeline))
1741 return Error::success();
1742 return make_error<StringError>(
1743 formatv("unknown function pass '{0}'", Name).str(),
1744 inconvertibleErrorCode());
1747 Error PassBuilder::parseLoopPass(LoopPassManager &LPM,
1748 const PipelineElement &E) {
1749 StringRef Name = E.Name;
1750 auto &InnerPipeline = E.InnerPipeline;
1752 // First handle complex passes like the pass managers which carry pipelines.
1753 if (!InnerPipeline.empty()) {
1754 if (Name == "loop") {
1755 LoopPassManager NestedLPM;
1756 if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline))
1757 return Err;
1758 // Add the nested pass manager with the appropriate adaptor.
1759 LPM.addPass(std::move(NestedLPM));
1760 return Error::success();
1762 if (auto Count = parseRepeatPassName(Name)) {
1763 LoopPassManager NestedLPM;
1764 if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline))
1765 return Err;
1766 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
1767 return Error::success();
1770 for (auto &C : LoopPipelineParsingCallbacks)
1771 if (C(Name, LPM, InnerPipeline))
1772 return Error::success();
1774 // Normal passes can't have pipelines.
1775 return make_error<StringError>(
1776 formatv("invalid use of '{0}' pass as loop pipeline", Name).str(),
1777 inconvertibleErrorCode());
1780 // Now expand the basic registered passes from the .inc file.
1781 #define LOOPNEST_PASS(NAME, CREATE_PASS) \
1782 if (Name == NAME) { \
1783 LPM.addPass(CREATE_PASS); \
1784 return Error::success(); \
1786 #define LOOP_PASS(NAME, CREATE_PASS) \
1787 if (Name == NAME) { \
1788 LPM.addPass(CREATE_PASS); \
1789 return Error::success(); \
1791 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
1792 if (checkParametrizedPassName(Name, NAME)) { \
1793 auto Params = parsePassParameters(PARSER, Name, NAME); \
1794 if (!Params) \
1795 return Params.takeError(); \
1796 LPM.addPass(CREATE_PASS(Params.get())); \
1797 return Error::success(); \
1799 #define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1800 if (Name == "require<" NAME ">") { \
1801 LPM.addPass(RequireAnalysisPass< \
1802 std::remove_reference_t<decltype(CREATE_PASS)>, Loop, \
1803 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1804 LPMUpdater &>()); \
1805 return Error::success(); \
1807 if (Name == "invalidate<" NAME ">") { \
1808 LPM.addPass(InvalidateAnalysisPass< \
1809 std::remove_reference_t<decltype(CREATE_PASS)>>()); \
1810 return Error::success(); \
1812 #include "PassRegistry.def"
1814 for (auto &C : LoopPipelineParsingCallbacks)
1815 if (C(Name, LPM, InnerPipeline))
1816 return Error::success();
1817 return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(),
1818 inconvertibleErrorCode());
1821 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
1822 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1823 if (Name == NAME) { \
1824 AA.registerModuleAnalysis< \
1825 std::remove_reference_t<decltype(CREATE_PASS)>>(); \
1826 return true; \
1828 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1829 if (Name == NAME) { \
1830 AA.registerFunctionAnalysis< \
1831 std::remove_reference_t<decltype(CREATE_PASS)>>(); \
1832 return true; \
1834 #include "PassRegistry.def"
1836 for (auto &C : AAParsingCallbacks)
1837 if (C(Name, AA))
1838 return true;
1839 return false;
1842 Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
1843 ArrayRef<PipelineElement> Pipeline) {
1844 for (const auto &Element : Pipeline) {
1845 if (auto Err = parseLoopPass(LPM, Element))
1846 return Err;
1848 return Error::success();
1851 Error PassBuilder::parseFunctionPassPipeline(
1852 FunctionPassManager &FPM, ArrayRef<PipelineElement> Pipeline) {
1853 for (const auto &Element : Pipeline) {
1854 if (auto Err = parseFunctionPass(FPM, Element))
1855 return Err;
1857 return Error::success();
1860 Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
1861 ArrayRef<PipelineElement> Pipeline) {
1862 for (const auto &Element : Pipeline) {
1863 if (auto Err = parseCGSCCPass(CGPM, Element))
1864 return Err;
1866 return Error::success();
1869 void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1870 FunctionAnalysisManager &FAM,
1871 CGSCCAnalysisManager &CGAM,
1872 ModuleAnalysisManager &MAM) {
1873 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1874 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
1875 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1876 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1877 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1878 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1879 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1882 Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
1883 ArrayRef<PipelineElement> Pipeline) {
1884 for (const auto &Element : Pipeline) {
1885 if (auto Err = parseModulePass(MPM, Element))
1886 return Err;
1888 return Error::success();
1891 // Primary pass pipeline description parsing routine for a \c ModulePassManager
1892 // FIXME: Should this routine accept a TargetMachine or require the caller to
1893 // pre-populate the analysis managers with target-specific stuff?
1894 Error PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1895 StringRef PipelineText) {
1896 auto Pipeline = parsePipelineText(PipelineText);
1897 if (!Pipeline || Pipeline->empty())
1898 return make_error<StringError>(
1899 formatv("invalid pipeline '{0}'", PipelineText).str(),
1900 inconvertibleErrorCode());
1902 // If the first name isn't at the module layer, wrap the pipeline up
1903 // automatically.
1904 StringRef FirstName = Pipeline->front().Name;
1906 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1907 bool UseMemorySSA;
1908 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
1909 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1910 } else if (isFunctionPassName(FirstName,
1911 FunctionPipelineParsingCallbacks)) {
1912 Pipeline = {{"function", std::move(*Pipeline)}};
1913 } else if (isLoopNestPassName(FirstName, LoopPipelineParsingCallbacks,
1914 UseMemorySSA)) {
1915 Pipeline = {{"function", {{UseMemorySSA ? "loop-mssa" : "loop",
1916 std::move(*Pipeline)}}}};
1917 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks,
1918 UseMemorySSA)) {
1919 Pipeline = {{"function", {{UseMemorySSA ? "loop-mssa" : "loop",
1920 std::move(*Pipeline)}}}};
1921 } else {
1922 for (auto &C : TopLevelPipelineParsingCallbacks)
1923 if (C(MPM, *Pipeline))
1924 return Error::success();
1926 // Unknown pass or pipeline name!
1927 auto &InnerPipeline = Pipeline->front().InnerPipeline;
1928 return make_error<StringError>(
1929 formatv("unknown {0} name '{1}'",
1930 (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName)
1931 .str(),
1932 inconvertibleErrorCode());
1936 if (auto Err = parseModulePassPipeline(MPM, *Pipeline))
1937 return Err;
1938 return Error::success();
1941 // Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1942 Error PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1943 StringRef PipelineText) {
1944 auto Pipeline = parsePipelineText(PipelineText);
1945 if (!Pipeline || Pipeline->empty())
1946 return make_error<StringError>(
1947 formatv("invalid pipeline '{0}'", PipelineText).str(),
1948 inconvertibleErrorCode());
1950 StringRef FirstName = Pipeline->front().Name;
1951 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1952 return make_error<StringError>(
1953 formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName,
1954 PipelineText)
1955 .str(),
1956 inconvertibleErrorCode());
1958 if (auto Err = parseCGSCCPassPipeline(CGPM, *Pipeline))
1959 return Err;
1960 return Error::success();
1963 // Primary pass pipeline description parsing routine for a \c
1964 // FunctionPassManager
1965 Error PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1966 StringRef PipelineText) {
1967 auto Pipeline = parsePipelineText(PipelineText);
1968 if (!Pipeline || Pipeline->empty())
1969 return make_error<StringError>(
1970 formatv("invalid pipeline '{0}'", PipelineText).str(),
1971 inconvertibleErrorCode());
1973 StringRef FirstName = Pipeline->front().Name;
1974 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1975 return make_error<StringError>(
1976 formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName,
1977 PipelineText)
1978 .str(),
1979 inconvertibleErrorCode());
1981 if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline))
1982 return Err;
1983 return Error::success();
1986 // Primary pass pipeline description parsing routine for a \c LoopPassManager
1987 Error PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1988 StringRef PipelineText) {
1989 auto Pipeline = parsePipelineText(PipelineText);
1990 if (!Pipeline || Pipeline->empty())
1991 return make_error<StringError>(
1992 formatv("invalid pipeline '{0}'", PipelineText).str(),
1993 inconvertibleErrorCode());
1995 if (auto Err = parseLoopPassPipeline(CGPM, *Pipeline))
1996 return Err;
1998 return Error::success();
2001 Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
2002 // If the pipeline just consists of the word 'default' just replace the AA
2003 // manager with our default one.
2004 if (PipelineText == "default") {
2005 AA = buildDefaultAAPipeline();
2006 return Error::success();
2009 while (!PipelineText.empty()) {
2010 StringRef Name;
2011 std::tie(Name, PipelineText) = PipelineText.split(',');
2012 if (!parseAAPassName(AA, Name))
2013 return make_error<StringError>(
2014 formatv("unknown alias analysis name '{0}'", Name).str(),
2015 inconvertibleErrorCode());
2018 return Error::success();
2021 static void printPassName(StringRef PassName, raw_ostream &OS) {
2022 OS << " " << PassName << "\n";
2024 static void printPassName(StringRef PassName, StringRef Params,
2025 raw_ostream &OS) {
2026 OS << " " << PassName << "<" << Params << ">\n";
2029 void PassBuilder::printPassNames(raw_ostream &OS) {
2030 // TODO: print pass descriptions when they are available
2032 OS << "Module passes:\n";
2033 #define MODULE_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2034 #include "PassRegistry.def"
2036 OS << "Module passes with params:\n";
2037 #define MODULE_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
2038 printPassName(NAME, PARAMS, OS);
2039 #include "PassRegistry.def"
2041 OS << "Module analyses:\n";
2042 #define MODULE_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2043 #include "PassRegistry.def"
2045 OS << "Module alias analyses:\n";
2046 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2047 #include "PassRegistry.def"
2049 OS << "CGSCC passes:\n";
2050 #define CGSCC_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2051 #include "PassRegistry.def"
2053 OS << "CGSCC passes with params:\n";
2054 #define CGSCC_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
2055 printPassName(NAME, PARAMS, OS);
2056 #include "PassRegistry.def"
2058 OS << "CGSCC analyses:\n";
2059 #define CGSCC_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2060 #include "PassRegistry.def"
2062 OS << "Function passes:\n";
2063 #define FUNCTION_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2064 #include "PassRegistry.def"
2066 OS << "Function passes with params:\n";
2067 #define FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
2068 printPassName(NAME, PARAMS, OS);
2069 #include "PassRegistry.def"
2071 OS << "Function analyses:\n";
2072 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2073 #include "PassRegistry.def"
2075 OS << "Function alias analyses:\n";
2076 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2077 #include "PassRegistry.def"
2079 OS << "LoopNest passes:\n";
2080 #define LOOPNEST_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2081 #include "PassRegistry.def"
2083 OS << "Loop passes:\n";
2084 #define LOOP_PASS(NAME, CREATE_PASS) printPassName(NAME, OS);
2085 #include "PassRegistry.def"
2087 OS << "Loop passes with params:\n";
2088 #define LOOP_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, PARAMS) \
2089 printPassName(NAME, PARAMS, OS);
2090 #include "PassRegistry.def"
2092 OS << "Loop analyses:\n";
2093 #define LOOP_ANALYSIS(NAME, CREATE_PASS) printPassName(NAME, OS);
2094 #include "PassRegistry.def"
2097 void PassBuilder::registerParseTopLevelPipelineCallback(
2098 const std::function<bool(ModulePassManager &, ArrayRef<PipelineElement>)>
2099 &C) {
2100 TopLevelPipelineParsingCallbacks.push_back(C);