]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Passes/PassBuilder.cpp
Import mandoc 1.4.1rc2
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Passes / PassBuilder.cpp
1 //===- Parsing, selection, and construction of pass pipelines -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 ///
11 /// This file provides the implementation of the PassBuilder based on our
12 /// static pass registry as well as related functionality. It also provides
13 /// helpers to aid in analyzing, debugging, and testing passes and pass
14 /// pipelines.
15 ///
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/Passes/PassBuilder.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
22 #include "llvm/Analysis/AssumptionCache.h"
23 #include "llvm/Analysis/BasicAliasAnalysis.h"
24 #include "llvm/Analysis/BlockFrequencyInfo.h"
25 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
26 #include "llvm/Analysis/BranchProbabilityInfo.h"
27 #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
28 #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
29 #include "llvm/Analysis/CGSCCPassManager.h"
30 #include "llvm/Analysis/CallGraph.h"
31 #include "llvm/Analysis/DemandedBits.h"
32 #include "llvm/Analysis/DependenceAnalysis.h"
33 #include "llvm/Analysis/DominanceFrontier.h"
34 #include "llvm/Analysis/GlobalsModRef.h"
35 #include "llvm/Analysis/IVUsers.h"
36 #include "llvm/Analysis/LazyCallGraph.h"
37 #include "llvm/Analysis/LazyValueInfo.h"
38 #include "llvm/Analysis/LoopAccessAnalysis.h"
39 #include "llvm/Analysis/LoopInfo.h"
40 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
41 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
42 #include "llvm/Analysis/PostDominators.h"
43 #include "llvm/Analysis/ProfileSummaryInfo.h"
44 #include "llvm/Analysis/RegionInfo.h"
45 #include "llvm/Analysis/ScalarEvolution.h"
46 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
47 #include "llvm/Analysis/ScopedNoAliasAA.h"
48 #include "llvm/Analysis/TargetLibraryInfo.h"
49 #include "llvm/Analysis/TargetTransformInfo.h"
50 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
51 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
52 #include "llvm/CodeGen/UnreachableBlockElim.h"
53 #include "llvm/IR/Dominators.h"
54 #include "llvm/IR/IRPrintingPasses.h"
55 #include "llvm/IR/PassManager.h"
56 #include "llvm/IR/Verifier.h"
57 #include "llvm/Support/Debug.h"
58 #include "llvm/Support/Regex.h"
59 #include "llvm/Target/TargetMachine.h"
60 #include "llvm/Transforms/GCOVProfiler.h"
61 #include "llvm/Transforms/IPO/ConstantMerge.h"
62 #include "llvm/Transforms/IPO/CrossDSOCFI.h"
63 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
64 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
65 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
66 #include "llvm/Transforms/IPO/FunctionAttrs.h"
67 #include "llvm/Transforms/IPO/GlobalDCE.h"
68 #include "llvm/Transforms/IPO/GlobalOpt.h"
69 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
70 #include "llvm/Transforms/IPO/Internalize.h"
71 #include "llvm/Transforms/IPO/LowerTypeTests.h"
72 #include "llvm/Transforms/IPO/PartialInlining.h"
73 #include "llvm/Transforms/IPO/SCCP.h"
74 #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
75 #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
76 #include "llvm/Transforms/InstCombine/InstCombine.h"
77 #include "llvm/Transforms/InstrProfiling.h"
78 #include "llvm/Transforms/PGOInstrumentation.h"
79 #include "llvm/Transforms/SampleProfile.h"
80 #include "llvm/Transforms/Scalar/ADCE.h"
81 #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
82 #include "llvm/Transforms/Scalar/BDCE.h"
83 #include "llvm/Transforms/Scalar/ConstantHoisting.h"
84 #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
85 #include "llvm/Transforms/Scalar/DCE.h"
86 #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
87 #include "llvm/Transforms/Scalar/EarlyCSE.h"
88 #include "llvm/Transforms/Scalar/Float2Int.h"
89 #include "llvm/Transforms/Scalar/GVN.h"
90 #include "llvm/Transforms/Scalar/GuardWidening.h"
91 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
92 #include "llvm/Transforms/Scalar/JumpThreading.h"
93 #include "llvm/Transforms/Scalar/LICM.h"
94 #include "llvm/Transforms/Scalar/LoopDeletion.h"
95 #include "llvm/Transforms/Scalar/LoopDistribute.h"
96 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
97 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
98 #include "llvm/Transforms/Scalar/LoopRotation.h"
99 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
100 #include "llvm/Transforms/Scalar/LowerAtomic.h"
101 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
102 #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
103 #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
104 #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
105 #include "llvm/Transforms/Scalar/Reassociate.h"
106 #include "llvm/Transforms/Scalar/SCCP.h"
107 #include "llvm/Transforms/Scalar/SROA.h"
108 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
109 #include "llvm/Transforms/Scalar/Sink.h"
110 #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
111 #include "llvm/Transforms/Utils/AddDiscriminators.h"
112 #include "llvm/Transforms/Utils/LCSSA.h"
113 #include "llvm/Transforms/Utils/LoopSimplify.h"
114 #include "llvm/Transforms/Utils/Mem2Reg.h"
115 #include "llvm/Transforms/Utils/MemorySSA.h"
116 #include "llvm/Transforms/Utils/SimplifyInstructions.h"
117 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
118 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
119
120 #include <type_traits>
121
122 using namespace llvm;
123
124 static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
125
126 namespace {
127
128 /// \brief No-op module pass which does nothing.
129 struct NoOpModulePass {
130   PreservedAnalyses run(Module &M, AnalysisManager<Module> &) {
131     return PreservedAnalyses::all();
132   }
133   static StringRef name() { return "NoOpModulePass"; }
134 };
135
136 /// \brief No-op module analysis.
137 class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
138   friend AnalysisInfoMixin<NoOpModuleAnalysis>;
139   static char PassID;
140
141 public:
142   struct Result {};
143   Result run(Module &, AnalysisManager<Module> &) { return Result(); }
144   static StringRef name() { return "NoOpModuleAnalysis"; }
145 };
146
147 /// \brief No-op CGSCC pass which does nothing.
148 struct NoOpCGSCCPass {
149   PreservedAnalyses run(LazyCallGraph::SCC &C,
150                         AnalysisManager<LazyCallGraph::SCC> &) {
151     return PreservedAnalyses::all();
152   }
153   static StringRef name() { return "NoOpCGSCCPass"; }
154 };
155
156 /// \brief No-op CGSCC analysis.
157 class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
158   friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
159   static char PassID;
160
161 public:
162   struct Result {};
163   Result run(LazyCallGraph::SCC &, AnalysisManager<LazyCallGraph::SCC> &) {
164     return Result();
165   }
166   static StringRef name() { return "NoOpCGSCCAnalysis"; }
167 };
168
169 /// \brief No-op function pass which does nothing.
170 struct NoOpFunctionPass {
171   PreservedAnalyses run(Function &F, AnalysisManager<Function> &) {
172     return PreservedAnalyses::all();
173   }
174   static StringRef name() { return "NoOpFunctionPass"; }
175 };
176
177 /// \brief No-op function analysis.
178 class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
179   friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
180   static char PassID;
181
182 public:
183   struct Result {};
184   Result run(Function &, AnalysisManager<Function> &) { return Result(); }
185   static StringRef name() { return "NoOpFunctionAnalysis"; }
186 };
187
188 /// \brief No-op loop pass which does nothing.
189 struct NoOpLoopPass {
190   PreservedAnalyses run(Loop &L, AnalysisManager<Loop> &) {
191     return PreservedAnalyses::all();
192   }
193   static StringRef name() { return "NoOpLoopPass"; }
194 };
195
196 /// \brief No-op loop analysis.
197 class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
198   friend AnalysisInfoMixin<NoOpLoopAnalysis>;
199   static char PassID;
200
201 public:
202   struct Result {};
203   Result run(Loop &, AnalysisManager<Loop> &) { return Result(); }
204   static StringRef name() { return "NoOpLoopAnalysis"; }
205 };
206
207 char NoOpModuleAnalysis::PassID;
208 char NoOpCGSCCAnalysis::PassID;
209 char NoOpFunctionAnalysis::PassID;
210 char NoOpLoopAnalysis::PassID;
211
212 } // End anonymous namespace.
213
214 void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
215 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
216   MAM.registerPass([&] { return CREATE_PASS; });
217 #include "PassRegistry.def"
218 }
219
220 void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
221 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
222   CGAM.registerPass([&] { return CREATE_PASS; });
223 #include "PassRegistry.def"
224 }
225
226 void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
227 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
228   FAM.registerPass([&] { return CREATE_PASS; });
229 #include "PassRegistry.def"
230 }
231
232 void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
233 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
234   LAM.registerPass([&] { return CREATE_PASS; });
235 #include "PassRegistry.def"
236 }
237
238 void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
239                                               OptimizationLevel Level,
240                                               bool DebugLogging) {
241   // FIXME: Finish fleshing this out to match the legacy pipelines.
242   FunctionPassManager EarlyFPM(DebugLogging);
243   EarlyFPM.addPass(SimplifyCFGPass());
244   EarlyFPM.addPass(SROA());
245   EarlyFPM.addPass(EarlyCSEPass());
246   EarlyFPM.addPass(LowerExpectIntrinsicPass());
247
248   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
249 }
250
251 void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
252                                                OptimizationLevel Level,
253                                                bool DebugLogging) {
254   // FIXME: We should use a customized pre-link pipeline!
255   addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
256 }
257
258 void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
259                                         OptimizationLevel Level,
260                                         bool DebugLogging) {
261   // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
262   FunctionPassManager LateFPM(DebugLogging);
263   LateFPM.addPass(InstCombinePass());
264   LateFPM.addPass(SimplifyCFGPass());
265
266   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
267 }
268
269 #ifndef NDEBUG
270 static bool isModulePassName(StringRef Name) {
271   // Manually handle aliases for pre-configured pipeline fragments.
272   if (Name.startswith("default") || Name.startswith("lto"))
273     return DefaultAliasRegex.match(Name);
274
275 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
276   if (Name == NAME)                                                            \
277     return true;
278 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
279   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
280     return true;
281 #include "PassRegistry.def"
282
283   return false;
284 }
285 #endif
286
287 static bool isCGSCCPassName(StringRef Name) {
288 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
289   if (Name == NAME)                                                            \
290     return true;
291 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
292   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
293     return true;
294 #include "PassRegistry.def"
295
296   return false;
297 }
298
299 static bool isFunctionPassName(StringRef Name) {
300 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
301   if (Name == NAME)                                                            \
302     return true;
303 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
304   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
305     return true;
306 #include "PassRegistry.def"
307
308   return false;
309 }
310
311 static bool isLoopPassName(StringRef Name) {
312 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
313   if (Name == NAME)                                                            \
314     return true;
315 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
316   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
317     return true;
318 #include "PassRegistry.def"
319
320   return false;
321 }
322
323 bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
324                                       bool DebugLogging) {
325   // Manually handle aliases for pre-configured pipeline fragments.
326   if (Name.startswith("default") || Name.startswith("lto")) {
327     SmallVector<StringRef, 3> Matches;
328     if (!DefaultAliasRegex.match(Name, &Matches))
329       return false;
330     assert(Matches.size() == 3 && "Must capture two matched strings!");
331
332     auto L = StringSwitch<OptimizationLevel>(Matches[2])
333                  .Case("O0", O0)
334                  .Case("O1", O1)
335                  .Case("O2", O2)
336                  .Case("O3", O3)
337                  .Case("Os", Os)
338                  .Case("Oz", Oz);
339
340     if (Matches[1] == "default") {
341       addPerModuleDefaultPipeline(MPM, L, DebugLogging);
342     } else if (Matches[1] == "lto-pre-link") {
343       addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
344     } else {
345       assert(Matches[1] == "lto" && "Not one of the matched options!");
346       addLTODefaultPipeline(MPM, L, DebugLogging);
347     }
348     return true;
349   }
350
351 #define MODULE_PASS(NAME, CREATE_PASS)                                         \
352   if (Name == NAME) {                                                          \
353     MPM.addPass(CREATE_PASS);                                                  \
354     return true;                                                               \
355   }
356 #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
357   if (Name == "require<" NAME ">") {                                           \
358     MPM.addPass(RequireAnalysisPass<                                           \
359                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
360     return true;                                                               \
361   }                                                                            \
362   if (Name == "invalidate<" NAME ">") {                                        \
363     MPM.addPass(InvalidateAnalysisPass<                                        \
364                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
365     return true;                                                               \
366   }
367 #include "PassRegistry.def"
368
369   return false;
370 }
371
372 bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
373 #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
374   if (Name == NAME) {                                                          \
375     CGPM.addPass(CREATE_PASS);                                                 \
376     return true;                                                               \
377   }
378 #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
379   if (Name == "require<" NAME ">") {                                           \
380     CGPM.addPass(RequireAnalysisPass<                                          \
381                  std::remove_reference<decltype(CREATE_PASS)>::type>());       \
382     return true;                                                               \
383   }                                                                            \
384   if (Name == "invalidate<" NAME ">") {                                        \
385     CGPM.addPass(InvalidateAnalysisPass<                                       \
386                  std::remove_reference<decltype(CREATE_PASS)>::type>());       \
387     return true;                                                               \
388   }
389 #include "PassRegistry.def"
390
391   return false;
392 }
393
394 bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
395                                         StringRef Name) {
396 #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
397   if (Name == NAME) {                                                          \
398     FPM.addPass(CREATE_PASS);                                                  \
399     return true;                                                               \
400   }
401 #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
402   if (Name == "require<" NAME ">") {                                           \
403     FPM.addPass(RequireAnalysisPass<                                           \
404                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
405     return true;                                                               \
406   }                                                                            \
407   if (Name == "invalidate<" NAME ">") {                                        \
408     FPM.addPass(InvalidateAnalysisPass<                                        \
409                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
410     return true;                                                               \
411   }
412 #include "PassRegistry.def"
413
414   return false;
415 }
416
417 bool PassBuilder::parseLoopPassName(LoopPassManager &FPM, StringRef Name) {
418 #define LOOP_PASS(NAME, CREATE_PASS)                                           \
419   if (Name == NAME) {                                                          \
420     FPM.addPass(CREATE_PASS);                                                  \
421     return true;                                                               \
422   }
423 #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
424   if (Name == "require<" NAME ">") {                                           \
425     FPM.addPass(RequireAnalysisPass<                                           \
426                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
427     return true;                                                               \
428   }                                                                            \
429   if (Name == "invalidate<" NAME ">") {                                        \
430     FPM.addPass(InvalidateAnalysisPass<                                        \
431                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
432     return true;                                                               \
433   }
434 #include "PassRegistry.def"
435
436   return false;
437 }
438
439 bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
440 #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS)                               \
441   if (Name == NAME) {                                                          \
442     AA.registerModuleAnalysis<                                                 \
443         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
444     return true;                                                               \
445   }
446 #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS)                             \
447   if (Name == NAME) {                                                          \
448     AA.registerFunctionAnalysis<                                               \
449         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
450     return true;                                                               \
451   }
452 #include "PassRegistry.def"
453
454   return false;
455 }
456
457 bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
458                                         StringRef &PipelineText,
459                                         bool VerifyEachPass,
460                                         bool DebugLogging) {
461   for (;;) {
462     // Parse nested pass managers by recursing.
463     if (PipelineText.startswith("loop(")) {
464       LoopPassManager NestedLPM(DebugLogging);
465
466       // Parse the inner pipeline inte the nested manager.
467       PipelineText = PipelineText.substr(strlen("loop("));
468       if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
469                                  DebugLogging) ||
470           PipelineText.empty())
471         return false;
472       assert(PipelineText[0] == ')');
473       PipelineText = PipelineText.substr(1);
474
475       // Add the nested pass manager with the appropriate adaptor.
476       LPM.addPass(std::move(NestedLPM));
477     } else {
478       // Otherwise try to parse a pass name.
479       size_t End = PipelineText.find_first_of(",)");
480       if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
481         return false;
482       // TODO: Ideally, we would run a LoopVerifierPass() here in the
483       // VerifyEachPass case, but we don't have such a verifier yet.
484
485       PipelineText = PipelineText.substr(End);
486     }
487
488     if (PipelineText.empty() || PipelineText[0] == ')')
489       return true;
490
491     assert(PipelineText[0] == ',');
492     PipelineText = PipelineText.substr(1);
493   }
494 }
495
496 bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
497                                             StringRef &PipelineText,
498                                             bool VerifyEachPass,
499                                             bool DebugLogging) {
500   for (;;) {
501     // Parse nested pass managers by recursing.
502     if (PipelineText.startswith("function(")) {
503       FunctionPassManager NestedFPM(DebugLogging);
504
505       // Parse the inner pipeline inte the nested manager.
506       PipelineText = PipelineText.substr(strlen("function("));
507       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
508                                      DebugLogging) ||
509           PipelineText.empty())
510         return false;
511       assert(PipelineText[0] == ')');
512       PipelineText = PipelineText.substr(1);
513
514       // Add the nested pass manager with the appropriate adaptor.
515       FPM.addPass(std::move(NestedFPM));
516     } else if (PipelineText.startswith("loop(")) {
517       LoopPassManager NestedLPM(DebugLogging);
518
519       // Parse the inner pipeline inte the nested manager.
520       PipelineText = PipelineText.substr(strlen("loop("));
521       if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
522                                  DebugLogging) ||
523           PipelineText.empty())
524         return false;
525       assert(PipelineText[0] == ')');
526       PipelineText = PipelineText.substr(1);
527
528       // Add the nested pass manager with the appropriate adaptor.
529       FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
530     } else {
531       // Otherwise try to parse a pass name.
532       size_t End = PipelineText.find_first_of(",)");
533       if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
534         return false;
535       if (VerifyEachPass)
536         FPM.addPass(VerifierPass());
537
538       PipelineText = PipelineText.substr(End);
539     }
540
541     if (PipelineText.empty() || PipelineText[0] == ')')
542       return true;
543
544     assert(PipelineText[0] == ',');
545     PipelineText = PipelineText.substr(1);
546   }
547 }
548
549 bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
550                                          StringRef &PipelineText,
551                                          bool VerifyEachPass,
552                                          bool DebugLogging) {
553   for (;;) {
554     // Parse nested pass managers by recursing.
555     if (PipelineText.startswith("cgscc(")) {
556       CGSCCPassManager NestedCGPM(DebugLogging);
557
558       // Parse the inner pipeline into the nested manager.
559       PipelineText = PipelineText.substr(strlen("cgscc("));
560       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
561                                   DebugLogging) ||
562           PipelineText.empty())
563         return false;
564       assert(PipelineText[0] == ')');
565       PipelineText = PipelineText.substr(1);
566
567       // Add the nested pass manager with the appropriate adaptor.
568       CGPM.addPass(std::move(NestedCGPM));
569     } else if (PipelineText.startswith("function(")) {
570       FunctionPassManager NestedFPM(DebugLogging);
571
572       // Parse the inner pipeline inte the nested manager.
573       PipelineText = PipelineText.substr(strlen("function("));
574       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
575                                      DebugLogging) ||
576           PipelineText.empty())
577         return false;
578       assert(PipelineText[0] == ')');
579       PipelineText = PipelineText.substr(1);
580
581       // Add the nested pass manager with the appropriate adaptor.
582       CGPM.addPass(
583           createCGSCCToFunctionPassAdaptor(std::move(NestedFPM), DebugLogging));
584     } else {
585       // Otherwise try to parse a pass name.
586       size_t End = PipelineText.find_first_of(",)");
587       if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
588         return false;
589       // FIXME: No verifier support for CGSCC passes!
590
591       PipelineText = PipelineText.substr(End);
592     }
593
594     if (PipelineText.empty() || PipelineText[0] == ')')
595       return true;
596
597     assert(PipelineText[0] == ',');
598     PipelineText = PipelineText.substr(1);
599   }
600 }
601
602 void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
603                                        FunctionAnalysisManager &FAM,
604                                        CGSCCAnalysisManager &CGAM,
605                                        ModuleAnalysisManager &MAM) {
606   MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
607   MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
608   CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
609   CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
610   FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
611   FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
612   FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
613   LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
614 }
615
616 bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
617                                           StringRef &PipelineText,
618                                           bool VerifyEachPass,
619                                           bool DebugLogging) {
620   for (;;) {
621     // Parse nested pass managers by recursing.
622     if (PipelineText.startswith("module(")) {
623       ModulePassManager NestedMPM(DebugLogging);
624
625       // Parse the inner pipeline into the nested manager.
626       PipelineText = PipelineText.substr(strlen("module("));
627       if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
628                                    DebugLogging) ||
629           PipelineText.empty())
630         return false;
631       assert(PipelineText[0] == ')');
632       PipelineText = PipelineText.substr(1);
633
634       // Now add the nested manager as a module pass.
635       MPM.addPass(std::move(NestedMPM));
636     } else if (PipelineText.startswith("cgscc(")) {
637       CGSCCPassManager NestedCGPM(DebugLogging);
638
639       // Parse the inner pipeline inte the nested manager.
640       PipelineText = PipelineText.substr(strlen("cgscc("));
641       if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
642                                   DebugLogging) ||
643           PipelineText.empty())
644         return false;
645       assert(PipelineText[0] == ')');
646       PipelineText = PipelineText.substr(1);
647
648       // Add the nested pass manager with the appropriate adaptor.
649       MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM),
650                                                           DebugLogging));
651     } else if (PipelineText.startswith("function(")) {
652       FunctionPassManager NestedFPM(DebugLogging);
653
654       // Parse the inner pipeline inte the nested manager.
655       PipelineText = PipelineText.substr(strlen("function("));
656       if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
657                                      DebugLogging) ||
658           PipelineText.empty())
659         return false;
660       assert(PipelineText[0] == ')');
661       PipelineText = PipelineText.substr(1);
662
663       // Add the nested pass manager with the appropriate adaptor.
664       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
665     } else {
666       // Otherwise try to parse a pass name.
667       size_t End = PipelineText.find_first_of(",)");
668       if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
669         return false;
670       if (VerifyEachPass)
671         MPM.addPass(VerifierPass());
672
673       PipelineText = PipelineText.substr(End);
674     }
675
676     if (PipelineText.empty() || PipelineText[0] == ')')
677       return true;
678
679     assert(PipelineText[0] == ',');
680     PipelineText = PipelineText.substr(1);
681   }
682 }
683
684 // Primary pass pipeline description parsing routine.
685 // FIXME: Should this routine accept a TargetMachine or require the caller to
686 // pre-populate the analysis managers with target-specific stuff?
687 bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
688                                     StringRef PipelineText, bool VerifyEachPass,
689                                     bool DebugLogging) {
690   // By default, try to parse the pipeline as-if it were within an implicit
691   // 'module(...)' pass pipeline. If this will parse at all, it needs to
692   // consume the entire string.
693   if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
694     return PipelineText.empty();
695
696   // This isn't parsable as a module pipeline, look for the end of a pass name
697   // and directly drop down to that layer.
698   StringRef FirstName =
699       PipelineText.substr(0, PipelineText.find_first_of(",)"));
700   assert(!isModulePassName(FirstName) &&
701          "Already handled all module pipeline options.");
702
703   // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
704   // pipeline.
705   if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
706     CGSCCPassManager CGPM(DebugLogging);
707     if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
708                                 DebugLogging) ||
709         !PipelineText.empty())
710       return false;
711     MPM.addPass(
712         createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM), DebugLogging));
713     return true;
714   }
715
716   // Similarly, if this looks like a Function pass, parse the whole thing as
717   // a Function pipelien.
718   if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
719     FunctionPassManager FPM(DebugLogging);
720     if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
721                                    DebugLogging) ||
722         !PipelineText.empty())
723       return false;
724     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
725     return true;
726   }
727
728   // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
729   if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
730     LoopPassManager LPM(DebugLogging);
731     if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
732                                DebugLogging) ||
733         !PipelineText.empty())
734       return false;
735     FunctionPassManager FPM(DebugLogging);
736     FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
737     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
738     return true;
739   }
740
741   return false;
742 }
743
744 bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
745   while (!PipelineText.empty()) {
746     StringRef Name;
747     std::tie(Name, PipelineText) = PipelineText.split(',');
748     if (!parseAAPassName(AA, Name))
749       return false;
750   }
751
752   return true;
753 }