LCOV - code coverage report
Current view: top level - lib/Passes - PassBuilder.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 476 498 95.6 %
Date: 2018-07-13 00:08:38 Functions: 44 44 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/BranchProbabilityInfo.h"
      26             : #include "llvm/Analysis/CFGPrinter.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/MemorySSA.h"
      42             : #include "llvm/Analysis/ModuleSummaryAnalysis.h"
      43             : #include "llvm/Analysis/OptimizationRemarkEmitter.h"
      44             : #include "llvm/Analysis/PhiValues.h"
      45             : #include "llvm/Analysis/PostDominators.h"
      46             : #include "llvm/Analysis/ProfileSummaryInfo.h"
      47             : #include "llvm/Analysis/RegionInfo.h"
      48             : #include "llvm/Analysis/ScalarEvolution.h"
      49             : #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
      50             : #include "llvm/Analysis/ScopedNoAliasAA.h"
      51             : #include "llvm/Analysis/TargetLibraryInfo.h"
      52             : #include "llvm/Analysis/TargetTransformInfo.h"
      53             : #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
      54             : #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
      55             : #include "llvm/CodeGen/UnreachableBlockElim.h"
      56             : #include "llvm/IR/Dominators.h"
      57             : #include "llvm/IR/IRPrintingPasses.h"
      58             : #include "llvm/IR/PassManager.h"
      59             : #include "llvm/IR/Verifier.h"
      60             : #include "llvm/Support/Debug.h"
      61             : #include "llvm/Support/Regex.h"
      62             : #include "llvm/Target/TargetMachine.h"
      63             : #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
      64             : #include "llvm/Transforms/IPO/AlwaysInliner.h"
      65             : #include "llvm/Transforms/IPO/ArgumentPromotion.h"
      66             : #include "llvm/Transforms/IPO/CalledValuePropagation.h"
      67             : #include "llvm/Transforms/IPO/ConstantMerge.h"
      68             : #include "llvm/Transforms/IPO/CrossDSOCFI.h"
      69             : #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
      70             : #include "llvm/Transforms/IPO/ElimAvailExtern.h"
      71             : #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
      72             : #include "llvm/Transforms/IPO/FunctionAttrs.h"
      73             : #include "llvm/Transforms/IPO/FunctionImport.h"
      74             : #include "llvm/Transforms/IPO/GlobalDCE.h"
      75             : #include "llvm/Transforms/IPO/GlobalOpt.h"
      76             : #include "llvm/Transforms/IPO/GlobalSplit.h"
      77             : #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
      78             : #include "llvm/Transforms/IPO/Inliner.h"
      79             : #include "llvm/Transforms/IPO/Internalize.h"
      80             : #include "llvm/Transforms/IPO/LowerTypeTests.h"
      81             : #include "llvm/Transforms/IPO/PartialInlining.h"
      82             : #include "llvm/Transforms/IPO/SCCP.h"
      83             : #include "llvm/Transforms/IPO/SampleProfile.h"
      84             : #include "llvm/Transforms/IPO/StripDeadPrototypes.h"
      85             : #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
      86             : #include "llvm/Transforms/IPO/WholeProgramDevirt.h"
      87             : #include "llvm/Transforms/InstCombine/InstCombine.h"
      88             : #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
      89             : #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
      90             : #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
      91             : #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
      92             : #include "llvm/Transforms/Scalar/ADCE.h"
      93             : #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
      94             : #include "llvm/Transforms/Scalar/BDCE.h"
      95             : #include "llvm/Transforms/Scalar/CallSiteSplitting.h"
      96             : #include "llvm/Transforms/Scalar/ConstantHoisting.h"
      97             : #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
      98             : #include "llvm/Transforms/Scalar/DCE.h"
      99             : #include "llvm/Transforms/Scalar/DeadStoreElimination.h"
     100             : #include "llvm/Transforms/Scalar/DivRemPairs.h"
     101             : #include "llvm/Transforms/Scalar/EarlyCSE.h"
     102             : #include "llvm/Transforms/Scalar/Float2Int.h"
     103             : #include "llvm/Transforms/Scalar/GVN.h"
     104             : #include "llvm/Transforms/Scalar/GuardWidening.h"
     105             : #include "llvm/Transforms/Scalar/IVUsersPrinter.h"
     106             : #include "llvm/Transforms/Scalar/IndVarSimplify.h"
     107             : #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
     108             : #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
     109             : #include "llvm/Transforms/Scalar/JumpThreading.h"
     110             : #include "llvm/Transforms/Scalar/LICM.h"
     111             : #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
     112             : #include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
     113             : #include "llvm/Transforms/Scalar/LoopDeletion.h"
     114             : #include "llvm/Transforms/Scalar/LoopDistribute.h"
     115             : #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
     116             : #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
     117             : #include "llvm/Transforms/Scalar/LoopLoadElimination.h"
     118             : #include "llvm/Transforms/Scalar/LoopPassManager.h"
     119             : #include "llvm/Transforms/Scalar/LoopPredication.h"
     120             : #include "llvm/Transforms/Scalar/LoopRotation.h"
     121             : #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
     122             : #include "llvm/Transforms/Scalar/LoopSink.h"
     123             : #include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
     124             : #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
     125             : #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
     126             : #include "llvm/Transforms/Scalar/LowerAtomic.h"
     127             : #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
     128             : #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
     129             : #include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
     130             : #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
     131             : #include "llvm/Transforms/Scalar/NaryReassociate.h"
     132             : #include "llvm/Transforms/Scalar/NewGVN.h"
     133             : #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
     134             : #include "llvm/Transforms/Scalar/Reassociate.h"
     135             : #include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
     136             : #include "llvm/Transforms/Scalar/SCCP.h"
     137             : #include "llvm/Transforms/Scalar/SROA.h"
     138             : #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
     139             : #include "llvm/Transforms/Scalar/SimplifyCFG.h"
     140             : #include "llvm/Transforms/Scalar/Sink.h"
     141             : #include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
     142             : #include "llvm/Transforms/Scalar/SpeculativeExecution.h"
     143             : #include "llvm/Transforms/Scalar/TailRecursionElimination.h"
     144             : #include "llvm/Transforms/Utils/AddDiscriminators.h"
     145             : #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
     146             : #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
     147             : #include "llvm/Transforms/Utils/LCSSA.h"
     148             : #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
     149             : #include "llvm/Transforms/Utils/LoopSimplify.h"
     150             : #include "llvm/Transforms/Utils/LowerInvoke.h"
     151             : #include "llvm/Transforms/Utils/Mem2Reg.h"
     152             : #include "llvm/Transforms/Utils/NameAnonGlobals.h"
     153             : #include "llvm/Transforms/Utils/SymbolRewriter.h"
     154             : #include "llvm/Transforms/Vectorize/LoopVectorize.h"
     155             : #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
     156             : 
     157             : using namespace llvm;
     158             : 
     159       99743 : static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
     160       99743 :                                              cl::ReallyHidden, cl::init(4));
     161             : static cl::opt<bool>
     162      299229 :     RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
     163             :                        cl::Hidden, cl::ZeroOrMore,
     164      299229 :                        cl::desc("Run Partial inlinining pass"));
     165             : 
     166             : static cl::opt<bool>
     167      299229 :     RunNewGVN("enable-npm-newgvn", cl::init(false),
     168             :               cl::Hidden, cl::ZeroOrMore,
     169      299229 :               cl::desc("Run NewGVN instead of GVN"));
     170             : 
     171       99743 : static cl::opt<bool> EnableEarlyCSEMemSSA(
     172      199486 :     "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
     173      299229 :     cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
     174             : 
     175       99743 : static cl::opt<bool> EnableGVNHoist(
     176      199486 :     "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
     177      299229 :     cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
     178             : 
     179       99743 : static cl::opt<bool> EnableGVNSink(
     180      199486 :     "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
     181      299229 :     cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
     182             : 
     183       99743 : static cl::opt<bool> EnableUnrollAndJam(
     184      199486 :     "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
     185      299229 :     cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
     186             : 
     187       99743 : static cl::opt<bool> EnableSyntheticCounts(
     188      199486 :     "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
     189       99743 :     cl::desc("Run synthetic function entry count generation "
     190      299229 :              "pass"));
     191             : 
     192       99743 : static Regex DefaultAliasRegex(
     193       99743 :     "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
     194             : 
     195             : static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
     196          69 :   switch (Level) {
     197             :   case PassBuilder::O0:
     198             :   case PassBuilder::O1:
     199             :   case PassBuilder::O2:
     200             :   case PassBuilder::O3:
     201             :     return false;
     202             : 
     203             :   case PassBuilder::Os:
     204             :   case PassBuilder::Oz:
     205             :     return true;
     206             :   }
     207           0 :   llvm_unreachable("Invalid optimization level!");
     208             : }
     209             : 
     210             : namespace {
     211             : 
     212             : /// No-op module pass which does nothing.
     213             : struct NoOpModulePass {
     214             :   PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
     215             :     return PreservedAnalyses::all();
     216             :   }
     217             :   static StringRef name() { return "NoOpModulePass"; }
     218             : };
     219             : 
     220             : /// No-op module analysis.
     221             : class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
     222             :   friend AnalysisInfoMixin<NoOpModuleAnalysis>;
     223             :   static AnalysisKey Key;
     224             : 
     225             : public:
     226             :   struct Result {};
     227             :   Result run(Module &, ModuleAnalysisManager &) { return Result(); }
     228             :   static StringRef name() { return "NoOpModuleAnalysis"; }
     229             : };
     230             : 
     231             : /// No-op CGSCC pass which does nothing.
     232             : struct NoOpCGSCCPass {
     233             :   PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
     234             :                         LazyCallGraph &, CGSCCUpdateResult &UR) {
     235             :     return PreservedAnalyses::all();
     236             :   }
     237             :   static StringRef name() { return "NoOpCGSCCPass"; }
     238             : };
     239             : 
     240             : /// No-op CGSCC analysis.
     241             : class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
     242             :   friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
     243             :   static AnalysisKey Key;
     244             : 
     245             : public:
     246             :   struct Result {};
     247             :   Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
     248             :     return Result();
     249             :   }
     250             :   static StringRef name() { return "NoOpCGSCCAnalysis"; }
     251             : };
     252             : 
     253             : /// No-op function pass which does nothing.
     254             : struct NoOpFunctionPass {
     255             :   PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
     256             :     return PreservedAnalyses::all();
     257             :   }
     258             :   static StringRef name() { return "NoOpFunctionPass"; }
     259             : };
     260             : 
     261             : /// No-op function analysis.
     262             : class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
     263             :   friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
     264             :   static AnalysisKey Key;
     265             : 
     266             : public:
     267             :   struct Result {};
     268             :   Result run(Function &, FunctionAnalysisManager &) { return Result(); }
     269             :   static StringRef name() { return "NoOpFunctionAnalysis"; }
     270             : };
     271             : 
     272             : /// No-op loop pass which does nothing.
     273             : struct NoOpLoopPass {
     274             :   PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
     275             :                         LoopStandardAnalysisResults &, LPMUpdater &) {
     276             :     return PreservedAnalyses::all();
     277             :   }
     278             :   static StringRef name() { return "NoOpLoopPass"; }
     279             : };
     280             : 
     281             : /// No-op loop analysis.
     282             : class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
     283             :   friend AnalysisInfoMixin<NoOpLoopAnalysis>;
     284             :   static AnalysisKey Key;
     285             : 
     286             : public:
     287             :   struct Result {};
     288             :   Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
     289             :     return Result();
     290             :   }
     291             :   static StringRef name() { return "NoOpLoopAnalysis"; }
     292             : };
     293             : 
     294             : AnalysisKey NoOpModuleAnalysis::Key;
     295             : AnalysisKey NoOpCGSCCAnalysis::Key;
     296             : AnalysisKey NoOpFunctionAnalysis::Key;
     297             : AnalysisKey NoOpLoopAnalysis::Key;
     298             : 
     299             : } // End anonymous namespace.
     300             : 
     301         308 : void PassBuilder::invokePeepholeEPCallbacks(
     302             :     FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
     303         322 :   for (auto &C : PeepholeEPCallbacks)
     304             :     C(FPM, Level);
     305         308 : }
     306             : 
     307         961 : void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
     308             : #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
     309             :   MAM.registerPass([&] { return CREATE_PASS; });
     310             : #include "PassRegistry.def"
     311             : 
     312         967 :   for (auto &C : ModuleAnalysisRegistrationCallbacks)
     313             :     C(MAM);
     314         961 : }
     315             : 
     316         957 : void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
     317             : #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
     318             :   CGAM.registerPass([&] { return CREATE_PASS; });
     319             : #include "PassRegistry.def"
     320             : 
     321         961 :   for (auto &C : CGSCCAnalysisRegistrationCallbacks)
     322             :     C(CGAM);
     323         957 : }
     324             : 
     325         961 : void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
     326             : #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
     327             :   FAM.registerPass([&] { return CREATE_PASS; });
     328             : #include "PassRegistry.def"
     329             : 
     330        2733 :   for (auto &C : FunctionAnalysisRegistrationCallbacks)
     331             :     C(FAM);
     332         961 : }
     333             : 
     334         957 : void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
     335             : #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
     336             :   LAM.registerPass([&] { return CREATE_PASS; });
     337             : #include "PassRegistry.def"
     338             : 
     339         961 :   for (auto &C : LoopAnalysisRegistrationCallbacks)
     340             :     C(LAM);
     341         957 : }
     342             : 
     343             : FunctionPassManager
     344          66 : PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
     345             :                                                  ThinLTOPhase Phase,
     346             :                                                  bool DebugLogging) {
     347             :   assert(Level != O0 && "Must request optimizations!");
     348          66 :   FunctionPassManager FPM(DebugLogging);
     349             : 
     350             :   // Form SSA out of local memory accesses after breaking apart aggregates into
     351             :   // scalars.
     352          66 :   FPM.addPass(SROA());
     353             : 
     354             :   // Catch trivial redundancies
     355          66 :   FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
     356             : 
     357             :   // Hoisting of scalars and load expressions.
     358          66 :   if (EnableGVNHoist)
     359           0 :     FPM.addPass(GVNHoistPass());
     360             : 
     361             :   // Global value numbering based sinking.
     362          66 :   if (EnableGVNSink) {
     363           0 :     FPM.addPass(GVNSinkPass());
     364           0 :     FPM.addPass(SimplifyCFGPass());
     365             :   }
     366             : 
     367             :   // Speculative execution if the target has divergent branches; otherwise nop.
     368          66 :   FPM.addPass(SpeculativeExecutionPass());
     369             : 
     370             :   // Optimize based on known information about branches, and cleanup afterward.
     371          66 :   FPM.addPass(JumpThreadingPass());
     372          66 :   FPM.addPass(CorrelatedValuePropagationPass());
     373          66 :   FPM.addPass(SimplifyCFGPass());
     374          66 :   if (Level == O3)
     375          18 :     FPM.addPass(AggressiveInstCombinePass());
     376         132 :   FPM.addPass(InstCombinePass());
     377             : 
     378             :   if (!isOptimizingForSize(Level))
     379          55 :     FPM.addPass(LibCallsShrinkWrapPass());
     380             : 
     381          66 :   invokePeepholeEPCallbacks(FPM, Level);
     382             : 
     383             :   // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
     384             :   // using the size value profile. Don't perform this when optimizing for size.
     385          78 :   if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
     386             :       !isOptimizingForSize(Level))
     387           1 :     FPM.addPass(PGOMemOPSizeOpt());
     388             : 
     389          66 :   FPM.addPass(TailCallElimPass());
     390          66 :   FPM.addPass(SimplifyCFGPass());
     391             : 
     392             :   // Form canonically associated expression trees, and simplify the trees using
     393             :   // basic mathematical properties. For example, this will form (nearly)
     394             :   // minimal multiplication trees.
     395          66 :   FPM.addPass(ReassociatePass());
     396             : 
     397             :   // Add the primary loop simplification pipeline.
     398             :   // FIXME: Currently this is split into two loop pass pipelines because we run
     399             :   // some function passes in between them. These can and should be removed
     400             :   // and/or replaced by scheduling the loop pass equivalents in the correct
     401             :   // positions. But those equivalent passes aren't powerful enough yet.
     402             :   // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
     403             :   // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
     404             :   // fully replace `SimplifyCFGPass`, and the closest to the other we have is
     405             :   // `LoopInstSimplify`.
     406             :   LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
     407             : 
     408             :   // Simplify the loop body. We do this initially to clean up after other loop
     409             :   // passes run, either when iterating on a loop or on inner loops with
     410             :   // implications on the outer loop.
     411          66 :   LPM1.addPass(LoopInstSimplifyPass());
     412          66 :   LPM1.addPass(LoopSimplifyCFGPass());
     413             : 
     414             :   // Rotate Loop - disable header duplication at -Oz
     415          66 :   LPM1.addPass(LoopRotatePass(Level != Oz));
     416          66 :   LPM1.addPass(LICMPass());
     417          66 :   LPM1.addPass(SimpleLoopUnswitchPass());
     418          66 :   LPM2.addPass(IndVarSimplifyPass());
     419          66 :   LPM2.addPass(LoopIdiomRecognizePass());
     420             : 
     421          68 :   for (auto &C : LateLoopOptimizationsEPCallbacks)
     422             :     C(LPM2, Level);
     423             : 
     424          66 :   LPM2.addPass(LoopDeletionPass());
     425             :   // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
     426             :   // because it changes IR to makes profile annotation in back compile
     427             :   // inaccurate.
     428          14 :   if (Phase != ThinLTOPhase::PreLink ||
     429          70 :       !PGOOpt || PGOOpt->SampleProfileFile.empty())
     430         126 :     LPM2.addPass(LoopFullUnrollPass(Level));
     431             : 
     432          68 :   for (auto &C : LoopOptimizerEndEPCallbacks)
     433             :     C(LPM2, Level);
     434             : 
     435             :   // We provide the opt remark emitter pass for LICM to use. We only need to do
     436             :   // this once as it is immutable.
     437          66 :   FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
     438         198 :   FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
     439          66 :   FPM.addPass(SimplifyCFGPass());
     440         132 :   FPM.addPass(InstCombinePass());
     441         198 :   FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
     442             : 
     443             :   // Eliminate redundancies.
     444          66 :   if (Level != O1) {
     445             :     // These passes add substantial compile time so skip them at O1.
     446          57 :     FPM.addPass(MergedLoadStoreMotionPass());
     447          57 :     if (RunNewGVN)
     448           0 :       FPM.addPass(NewGVNPass());
     449             :     else
     450          57 :       FPM.addPass(GVN());
     451             :   }
     452             : 
     453             :   // Specially optimize memory movement as it doesn't look like dataflow in SSA.
     454         132 :   FPM.addPass(MemCpyOptPass());
     455             : 
     456             :   // Sparse conditional constant propagation.
     457             :   // FIXME: It isn't clear why we do this *after* loop passes rather than
     458             :   // before...
     459          66 :   FPM.addPass(SCCPPass());
     460             : 
     461             :   // Delete dead bit computations (instcombine runs after to fold away the dead
     462             :   // computations, and then ADCE will run later to exploit any new DCE
     463             :   // opportunities that creates).
     464          66 :   FPM.addPass(BDCEPass());
     465             : 
     466             :   // Run instcombine after redundancy and dead bit elimination to exploit
     467             :   // opportunities opened up by them.
     468         132 :   FPM.addPass(InstCombinePass());
     469          66 :   invokePeepholeEPCallbacks(FPM, Level);
     470             : 
     471             :   // Re-consider control flow based optimizations after redundancy elimination,
     472             :   // redo DCE, etc.
     473          66 :   FPM.addPass(JumpThreadingPass());
     474          66 :   FPM.addPass(CorrelatedValuePropagationPass());
     475          66 :   FPM.addPass(DSEPass());
     476         132 :   FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
     477             : 
     478          68 :   for (auto &C : ScalarOptimizerLateEPCallbacks)
     479             :     C(FPM, Level);
     480             : 
     481             :   // Finally, do an expensive DCE pass to catch all the dead code exposed by
     482             :   // the simplifications and basic cleanup after all the simplifications.
     483          66 :   FPM.addPass(ADCEPass());
     484          66 :   FPM.addPass(SimplifyCFGPass());
     485         132 :   FPM.addPass(InstCombinePass());
     486          66 :   invokePeepholeEPCallbacks(FPM, Level);
     487             : 
     488          66 :   return FPM;
     489             : }
     490             : 
     491           2 : void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
     492             :                                     PassBuilder::OptimizationLevel Level,
     493             :                                     bool RunProfileGen,
     494             :                                     std::string ProfileGenFile,
     495             :                                     std::string ProfileUseFile) {
     496             :   // Generally running simplification passes and the inliner with an high
     497             :   // threshold results in smaller executables, but there may be cases where
     498             :   // the size grows, so let's be conservative here and skip this simplification
     499             :   // at -Os/Oz.
     500             :   if (!isOptimizingForSize(Level)) {
     501             :     InlineParams IP;
     502             : 
     503             :     // In the old pass manager, this is a cl::opt. Should still this be one?
     504           2 :     IP.DefaultThreshold = 75;
     505             : 
     506             :     // FIXME: The hint threshold has the same value used by the regular inliner.
     507             :     // This should probably be lowered after performance testing.
     508             :     // FIXME: this comment is cargo culted from the old pass manager, revisit).
     509             :     IP.HintThreshold = 325;
     510             : 
     511             :     CGSCCPassManager CGPipeline(DebugLogging);
     512             : 
     513           4 :     CGPipeline.addPass(InlinerPass(IP));
     514             : 
     515             :     FunctionPassManager FPM;
     516           2 :     FPM.addPass(SROA());
     517           2 :     FPM.addPass(EarlyCSEPass());    // Catch trivial redundancies.
     518           2 :     FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
     519           4 :     FPM.addPass(InstCombinePass()); // Combine silly sequences.
     520           2 :     invokePeepholeEPCallbacks(FPM, Level);
     521             : 
     522           6 :     CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
     523             : 
     524           6 :     MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
     525             :   }
     526             : 
     527             :   // Delete anything that is now dead to make sure that we don't instrument
     528             :   // dead code. Instrumentation can end up keeping dead code around and
     529             :   // dramatically increase code size.
     530           2 :   MPM.addPass(GlobalDCEPass());
     531             : 
     532           2 :   if (RunProfileGen) {
     533           1 :     MPM.addPass(PGOInstrumentationGen());
     534             : 
     535             :     FunctionPassManager FPM;
     536           1 :     FPM.addPass(
     537           2 :         createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
     538           3 :     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
     539             : 
     540             :     // Add the profile lowering pass.
     541             :     InstrProfOptions Options;
     542           1 :     if (!ProfileGenFile.empty())
     543             :       Options.InstrProfileOutput = ProfileGenFile;
     544           1 :     Options.DoCounterPromotion = true;
     545           1 :     MPM.addPass(InstrProfiling(Options));
     546             :   }
     547             : 
     548           2 :   if (!ProfileUseFile.empty())
     549           3 :     MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
     550           2 : }
     551             : 
     552             : static InlineParams
     553             : getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
     554             :   auto O3 = PassBuilder::O3;
     555          80 :   unsigned OptLevel = Level > O3 ? 2 : Level;
     556          80 :   unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
     557          80 :   return getInlineParams(OptLevel, SizeLevel);
     558             : }
     559             : 
     560             : ModulePassManager
     561          66 : PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
     562             :                                                ThinLTOPhase Phase,
     563             :                                                bool DebugLogging) {
     564          66 :   ModulePassManager MPM(DebugLogging);
     565             : 
     566             :   // Do basic inference of function attributes from known properties of system
     567             :   // libraries and other oracles.
     568          66 :   MPM.addPass(InferFunctionAttrsPass());
     569             : 
     570             :   // Create an early function pass manager to cleanup the output of the
     571             :   // frontend.
     572             :   FunctionPassManager EarlyFPM(DebugLogging);
     573          66 :   EarlyFPM.addPass(SimplifyCFGPass());
     574          66 :   EarlyFPM.addPass(SROA());
     575          66 :   EarlyFPM.addPass(EarlyCSEPass());
     576          66 :   EarlyFPM.addPass(LowerExpectIntrinsicPass());
     577          66 :   if (Level == O3)
     578          18 :     EarlyFPM.addPass(CallSiteSplittingPass());
     579             : 
     580             :   // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
     581             :   // to convert bitcast to direct calls so that they can be inlined during the
     582             :   // profile annotation prepration step.
     583             :   // More details about SamplePGO design can be found in:
     584             :   // https://research.google.com/pubs/pub45290.html
     585             :   // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
     586          78 :   if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
     587             :       Phase == ThinLTOPhase::PostLink)
     588           4 :     EarlyFPM.addPass(InstCombinePass());
     589         198 :   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
     590             : 
     591          78 :   if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
     592             :     // Annotate sample profile right after early FPM to ensure freshness of
     593             :     // the debug info.
     594          28 :     MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
     595             :                                         Phase == ThinLTOPhase::PreLink));
     596             :     // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
     597             :     // for the profile annotation to be accurate in the ThinLTO backend.
     598           7 :     if (Phase != ThinLTOPhase::PreLink)
     599             :       // We perform early indirect call promotion here, before globalopt.
     600             :       // This is important for the ThinLTO backend phase because otherwise
     601             :       // imported available_externally functions look unreferenced and are
     602             :       // removed.
     603           8 :       MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
     604             :                                            true));
     605             :   }
     606             : 
     607             :   // Interprocedural constant propagation now that basic cleanup has occurred
     608             :   // and prior to optimizing globals.
     609             :   // FIXME: This position in the pipeline hasn't been carefully considered in
     610             :   // years, it should be re-analyzed.
     611          66 :   MPM.addPass(IPSCCPPass());
     612             : 
     613             :   // Attach metadata to indirect call sites indicating the set of functions
     614             :   // they may target at run-time. This should follow IPSCCP.
     615          66 :   MPM.addPass(CalledValuePropagationPass());
     616             : 
     617             :   // Optimize globals to try and fold them into constants.
     618          66 :   MPM.addPass(GlobalOptPass());
     619             : 
     620             :   // Promote any localized globals to SSA registers.
     621             :   // FIXME: Should this instead by a run of SROA?
     622             :   // FIXME: We should probably run instcombine and simplify-cfg afterward to
     623             :   // delete control flows that are dead once globals have been folded to
     624             :   // constants.
     625          66 :   MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
     626             : 
     627             :   // Remove any dead arguments exposed by cleanups and constand folding
     628             :   // globals.
     629          66 :   MPM.addPass(DeadArgumentEliminationPass());
     630             : 
     631             :   // Create a small function pass pipeline to cleanup after all the global
     632             :   // optimizations.
     633             :   FunctionPassManager GlobalCleanupPM(DebugLogging);
     634         132 :   GlobalCleanupPM.addPass(InstCombinePass());
     635          66 :   invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
     636             : 
     637          66 :   GlobalCleanupPM.addPass(SimplifyCFGPass());
     638         198 :   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
     639             : 
     640             :   // Add all the requested passes for instrumentation PGO, if requested.
     641          75 :   if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
     642           8 :       (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
     643           6 :     addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
     644             :                       PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
     645           2 :     MPM.addPass(PGOIndirectCallPromotion(false, false));
     646             :   }
     647             : 
     648             :   // Synthesize function entry counts for non-PGO compilation.
     649          66 :   if (EnableSyntheticCounts && !PGOOpt)
     650           0 :     MPM.addPass(SyntheticCountsPropagation());
     651             : 
     652             :   // Require the GlobalsAA analysis for the module so we can query it within
     653             :   // the CGSCC pipeline.
     654          66 :   MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
     655             : 
     656             :   // Require the ProfileSummaryAnalysis for the module so we can query it within
     657             :   // the inliner pass.
     658          66 :   MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
     659             : 
     660             :   // Now begin the main postorder CGSCC pipeline.
     661             :   // FIXME: The current CGSCC pipeline has its origins in the legacy pass
     662             :   // manager and trying to emulate its precise behavior. Much of this doesn't
     663             :   // make a lot of sense and we should revisit the core CGSCC structure.
     664             :   CGSCCPassManager MainCGPipeline(DebugLogging);
     665             : 
     666             :   // Note: historically, the PruneEH pass was run first to deduce nounwind and
     667             :   // generally clean up exception handling overhead. It isn't clear this is
     668             :   // valuable as the inliner doesn't currently care whether it is inlining an
     669             :   // invoke or a call.
     670             : 
     671             :   // Run the inliner first. The theory is that we are walking bottom-up and so
     672             :   // the callees have already been fully optimized, and we want to inline them
     673             :   // into the callers so that our optimizations can reflect that.
     674             :   // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
     675             :   // because it makes profile annotation in the backend inaccurate.
     676             :   InlineParams IP = getInlineParamsFromOptLevel(Level);
     677          14 :   if (Phase == ThinLTOPhase::PreLink &&
     678          70 :       PGOOpt && !PGOOpt->SampleProfileFile.empty())
     679             :     IP.HotCallSiteThreshold = 0;
     680         132 :   MainCGPipeline.addPass(InlinerPass(IP));
     681             : 
     682             :   // Now deduce any function attributes based in the current code.
     683          66 :   MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
     684             : 
     685             :   // When at O3 add argument promotion to the pass pipeline.
     686             :   // FIXME: It isn't at all clear why this should be limited to O3.
     687          66 :   if (Level == O3)
     688          18 :     MainCGPipeline.addPass(ArgumentPromotionPass());
     689             : 
     690             :   // Lastly, add the core function simplification pipeline nested inside the
     691             :   // CGSCC walk.
     692         132 :   MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
     693         132 :       buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
     694             : 
     695          68 :   for (auto &C : CGSCCOptimizerLateEPCallbacks)
     696             :     C(MainCGPipeline, Level);
     697             : 
     698             :   // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
     699             :   // to detect when we devirtualize indirect calls and iterate the SCC passes
     700             :   // in that case to try and catch knock-on inlining or function attrs
     701             :   // opportunities. Then we add it to the module pipeline by walking the SCCs
     702             :   // in postorder (or bottom-up).
     703          66 :   MPM.addPass(
     704         330 :       createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
     705             :           std::move(MainCGPipeline), MaxDevirtIterations)));
     706             : 
     707          66 :   return MPM;
     708             : }
     709             : 
     710             : ModulePassManager
     711          52 : PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
     712             :                                              bool DebugLogging) {
     713             :   ModulePassManager MPM(DebugLogging);
     714             : 
     715             :   // Optimize globals now that the module is fully simplified.
     716          52 :   MPM.addPass(GlobalOptPass());
     717          52 :   MPM.addPass(GlobalDCEPass());
     718             : 
     719             :   // Run partial inlining pass to partially inline functions that have
     720             :   // large bodies.
     721          52 :   if (RunPartialInlining)
     722           0 :     MPM.addPass(PartialInlinerPass());
     723             : 
     724             :   // Remove avail extern fns and globals definitions since we aren't compiling
     725             :   // an object file for later LTO. For LTO we want to preserve these so they
     726             :   // are eligible for inlining at link-time. Note if they are unreferenced they
     727             :   // will be removed by GlobalDCE later, so this only impacts referenced
     728             :   // available externally globals. Eventually they will be suppressed during
     729             :   // codegen, but eliminating here enables more opportunity for GlobalDCE as it
     730             :   // may make globals referenced by available external functions dead and saves
     731             :   // running remaining passes on the eliminated functions.
     732          52 :   MPM.addPass(EliminateAvailableExternallyPass());
     733             : 
     734             :   // Do RPO function attribute inference across the module to forward-propagate
     735             :   // attributes where applicable.
     736             :   // FIXME: Is this really an optimization rather than a canonicalization?
     737          52 :   MPM.addPass(ReversePostOrderFunctionAttrsPass());
     738             : 
     739             :   // Re-require GloblasAA here prior to function passes. This is particularly
     740             :   // useful as the above will have inlined, DCE'ed, and function-attr
     741             :   // propagated everything. We should at this point have a reasonably minimal
     742             :   // and richly annotated call graph. By computing aliasing and mod/ref
     743             :   // information for all local globals here, the late loop passes and notably
     744             :   // the vectorizer will be able to use them to help recognize vectorizable
     745             :   // memory operations.
     746          52 :   MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
     747             : 
     748             :   FunctionPassManager OptimizePM(DebugLogging);
     749          52 :   OptimizePM.addPass(Float2IntPass());
     750             :   // FIXME: We need to run some loop optimizations to re-rotate loops after
     751             :   // simplify-cfg and others undo their rotation.
     752             : 
     753             :   // Optimize the loop execution. These passes operate on entire loop nests
     754             :   // rather than on each loop in an inside-out manner, and so they are actually
     755             :   // function passes.
     756             : 
     757         112 :   for (auto &C : VectorizerStartEPCallbacks)
     758             :     C(OptimizePM, Level);
     759             : 
     760             :   // First rotate loops that may have been un-rotated by prior passes.
     761          52 :   OptimizePM.addPass(
     762         104 :       createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
     763             : 
     764             :   // Distribute loops to allow partial vectorization.  I.e. isolate dependences
     765             :   // into separate loop that would otherwise inhibit vectorization.  This is
     766             :   // currently only performed for loops marked with the metadata
     767             :   // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
     768          52 :   OptimizePM.addPass(LoopDistributePass());
     769             : 
     770             :   // Now run the core loop vectorizer.
     771          52 :   OptimizePM.addPass(LoopVectorizePass());
     772             : 
     773             :   // Eliminate loads by forwarding stores from the previous iteration to loads
     774             :   // of the current iteration.
     775          52 :   OptimizePM.addPass(LoopLoadEliminationPass());
     776             : 
     777             :   // Cleanup after the loop optimization passes.
     778         104 :   OptimizePM.addPass(InstCombinePass());
     779             : 
     780             :   // Now that we've formed fast to execute loop structures, we do further
     781             :   // optimizations. These are run afterward as they might block doing complex
     782             :   // analyses and transforms such as what are needed for loop vectorization.
     783             : 
     784             :   // Cleanup after loop vectorization, etc. Simplification passes like CVP and
     785             :   // GVN, loop transforms, and others have already run, so it's now better to
     786             :   // convert to more optimized IR using more aggressive simplify CFG options.
     787             :   // The extra sinking transform can create larger basic blocks, so do this
     788             :   // before SLP vectorization.
     789          52 :   OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
     790             :                                      forwardSwitchCondToPhi(true).
     791             :                                      convertSwitchToLookupTable(true).
     792             :                                      needCanonicalLoops(false).
     793             :                                      sinkCommonInsts(true)));
     794             : 
     795             :   // Optimize parallel scalar instruction chains into SIMD instructions.
     796          52 :   OptimizePM.addPass(SLPVectorizerPass());
     797             : 
     798         104 :   OptimizePM.addPass(InstCombinePass());
     799             : 
     800             :   // Unroll small loops to hide loop backedge latency and saturate any parallel
     801             :   // execution resources of an out-of-order processor. We also then need to
     802             :   // clean up redundancies and loop invariant code.
     803             :   // FIXME: It would be really good to use a loop-integrated instruction
     804             :   // combiner for cleanup here so that the unrolling and LICM can be pipelined
     805             :   // across the loop nests.
     806             :   // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
     807          52 :   if (EnableUnrollAndJam) {
     808           0 :     OptimizePM.addPass(
     809           0 :         createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
     810             :   }
     811         104 :   OptimizePM.addPass(LoopUnrollPass(Level));
     812         104 :   OptimizePM.addPass(InstCombinePass());
     813          52 :   OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
     814         104 :   OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
     815             : 
     816             :   // Now that we've vectorized and unrolled loops, we may have more refined
     817             :   // alignment information, try to re-derive it here.
     818          52 :   OptimizePM.addPass(AlignmentFromAssumptionsPass());
     819             : 
     820             :   // LoopSink pass sinks instructions hoisted by LICM, which serves as a
     821             :   // canonicalization pass that enables other optimizations. As a result,
     822             :   // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
     823             :   // result too early.
     824          52 :   OptimizePM.addPass(LoopSinkPass());
     825             : 
     826             :   // And finally clean up LCSSA form before generating code.
     827          52 :   OptimizePM.addPass(InstSimplifyPass());
     828             : 
     829             :   // This hoists/decomposes div/rem ops. It should run after other sink/hoist
     830             :   // passes to avoid re-sinking, but before SimplifyCFG because it can allow
     831             :   // flattening of blocks.
     832          52 :   OptimizePM.addPass(DivRemPairsPass());
     833             : 
     834             :   // LoopSink (and other loop passes since the last simplifyCFG) might have
     835             :   // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
     836          52 :   OptimizePM.addPass(SimplifyCFGPass());
     837             : 
     838             :   // Optimize PHIs by speculating around them when profitable. Note that this
     839             :   // pass needs to be run after any PRE or similar pass as it is essentially
     840             :   // inserting redudnancies into the progrem. This even includes SimplifyCFG.
     841          52 :   OptimizePM.addPass(SpeculateAroundPHIsPass());
     842             : 
     843             :   // Add the core optimizing pipeline.
     844         156 :   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
     845             : 
     846             :   // Now we need to do some global optimization transforms.
     847             :   // FIXME: It would seem like these should come first in the optimization
     848             :   // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
     849             :   // ordering here.
     850          52 :   MPM.addPass(GlobalDCEPass());
     851          52 :   MPM.addPass(ConstantMergePass());
     852             : 
     853          52 :   return MPM;
     854             : }
     855             : 
     856             : ModulePassManager
     857          37 : PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
     858             :                                            bool DebugLogging) {
     859             :   assert(Level != O0 && "Must request optimizations for the default pipeline!");
     860             : 
     861          37 :   ModulePassManager MPM(DebugLogging);
     862             : 
     863             :   // Force any function attributes we want the rest of the pipeline to observe.
     864          37 :   MPM.addPass(ForceFunctionAttrsPass());
     865             : 
     866             :   // Apply module pipeline start EP callback.
     867          43 :   for (auto &C : PipelineStartEPCallbacks)
     868             :     C(MPM);
     869             : 
     870          37 :   if (PGOOpt && PGOOpt->SamplePGOSupport)
     871           3 :     MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
     872             : 
     873             :   // Add the core simplification pipeline.
     874          74 :   MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
     875             :                                                 DebugLogging));
     876             : 
     877             :   // Now add the optimization pipeline.
     878          74 :   MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
     879             : 
     880          37 :   return MPM;
     881             : }
     882             : 
     883             : ModulePassManager
     884          14 : PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
     885             :                                                 bool DebugLogging) {
     886             :   assert(Level != O0 && "Must request optimizations for the default pipeline!");
     887             : 
     888          14 :   ModulePassManager MPM(DebugLogging);
     889             : 
     890             :   // Force any function attributes we want the rest of the pipeline to observe.
     891          14 :   MPM.addPass(ForceFunctionAttrsPass());
     892             : 
     893          14 :   if (PGOOpt && PGOOpt->SamplePGOSupport)
     894           4 :     MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
     895             : 
     896             :   // Apply module pipeline start EP callback.
     897          16 :   for (auto &C : PipelineStartEPCallbacks)
     898             :     C(MPM);
     899             : 
     900             :   // If we are planning to perform ThinLTO later, we don't bloat the code with
     901             :   // unrolling/vectorization/... now. Just simplify the module as much as we
     902             :   // can.
     903          28 :   MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
     904             :                                                 DebugLogging));
     905             : 
     906             :   // Run partial inlining pass to partially inline functions that have
     907             :   // large bodies.
     908             :   // FIXME: It isn't clear whether this is really the right place to run this
     909             :   // in ThinLTO. Because there is another canonicalization and simplification
     910             :   // phase that will run after the thin link, running this here ends up with
     911             :   // less information than will be available later and it may grow functions in
     912             :   // ways that aren't beneficial.
     913          14 :   if (RunPartialInlining)
     914           0 :     MPM.addPass(PartialInlinerPass());
     915             : 
     916             :   // Reduce the size of the IR as much as possible.
     917          14 :   MPM.addPass(GlobalOptPass());
     918             : 
     919          14 :   return MPM;
     920             : }
     921             : 
     922             : ModulePassManager
     923          15 : PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
     924             :                                          bool DebugLogging) {
     925             :   // FIXME: The summary index is not hooked in the new pass manager yet.
     926             :   // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
     927             :   // here.
     928             : 
     929          15 :   ModulePassManager MPM(DebugLogging);
     930             : 
     931             :   // Force any function attributes we want the rest of the pipeline to observe.
     932          15 :   MPM.addPass(ForceFunctionAttrsPass());
     933             : 
     934             :   // During the ThinLTO backend phase we perform early indirect call promotion
     935             :   // here, before globalopt. Otherwise imported available_externally functions
     936             :   // look unreferenced and are removed.
     937             :   // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
     938             :   //        with SamplePGO.
     939          18 :   if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
     940          13 :     MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
     941             :                                          false /* SamplePGO */));
     942             : 
     943             :   // Add the core simplification pipeline.
     944          30 :   MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
     945             :                                                 DebugLogging));
     946             : 
     947             :   // Now add the optimization pipeline.
     948          30 :   MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
     949             : 
     950          15 :   return MPM;
     951             : }
     952             : 
     953             : ModulePassManager
     954           7 : PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
     955             :                                             bool DebugLogging) {
     956             :   assert(Level != O0 && "Must request optimizations for the default pipeline!");
     957             :   // FIXME: We should use a customized pre-link pipeline!
     958           7 :   return buildPerModuleDefaultPipeline(Level, DebugLogging);
     959             : }
     960             : 
     961          15 : ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
     962             :                                                        bool DebugLogging) {
     963             :   assert(Level != O0 && "Must request optimizations for the default pipeline!");
     964             :   ModulePassManager MPM(DebugLogging);
     965             : 
     966             :   // Remove unused virtual tables to improve the quality of code generated by
     967             :   // whole-program devirtualization and bitset lowering.
     968          15 :   MPM.addPass(GlobalDCEPass());
     969             : 
     970             :   // Force any function attributes we want the rest of the pipeline to observe.
     971          15 :   MPM.addPass(ForceFunctionAttrsPass());
     972             : 
     973             :   // Do basic inference of function attributes from known properties of system
     974             :   // libraries and other oracles.
     975          15 :   MPM.addPass(InferFunctionAttrsPass());
     976             : 
     977          15 :   if (Level > 1) {
     978             :     FunctionPassManager EarlyFPM(DebugLogging);
     979          14 :     EarlyFPM.addPass(CallSiteSplittingPass());
     980          42 :     MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
     981             : 
     982             :     // Indirect call promotion. This should promote all the targets that are
     983             :     // left by the earlier promotion pass that promotes intra-module targets.
     984             :     // This two-step promotion is to save the compile time. For LTO, it should
     985             :     // produce the same result as if we only do promotion here.
     986          14 :     MPM.addPass(PGOIndirectCallPromotion(
     987          15 :         true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
     988             :     // Propagate constants at call sites into the functions they call.  This
     989             :     // opens opportunities for globalopt (and inlining) by substituting function
     990             :     // pointers passed as arguments to direct uses of functions.
     991          14 :    MPM.addPass(IPSCCPPass());
     992             : 
     993             :    // Attach metadata to indirect call sites indicating the set of functions
     994             :    // they may target at run-time. This should follow IPSCCP.
     995          14 :    MPM.addPass(CalledValuePropagationPass());
     996             :   }
     997             : 
     998             :   // Now deduce any function attributes based in the current code.
     999          15 :   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
    1000             :               PostOrderFunctionAttrsPass()));
    1001             : 
    1002             :   // Do RPO function attribute inference across the module to forward-propagate
    1003             :   // attributes where applicable.
    1004             :   // FIXME: Is this really an optimization rather than a canonicalization?
    1005          15 :   MPM.addPass(ReversePostOrderFunctionAttrsPass());
    1006             : 
    1007             :   // Use inragne annotations on GEP indices to split globals where beneficial.
    1008          15 :   MPM.addPass(GlobalSplitPass());
    1009             : 
    1010             :   // Run whole program optimization of virtual call when the list of callees
    1011             :   // is fixed.
    1012          15 :   MPM.addPass(WholeProgramDevirtPass());
    1013             : 
    1014             :   // Stop here at -O1.
    1015          15 :   if (Level == 1)
    1016             :     return MPM;
    1017             : 
    1018             :   // Optimize globals to try and fold them into constants.
    1019          14 :   MPM.addPass(GlobalOptPass());
    1020             : 
    1021             :   // Promote any localized globals to SSA registers.
    1022          14 :   MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
    1023             : 
    1024             :   // Linking modules together can lead to duplicate global constant, only
    1025             :   // keep one copy of each constant.
    1026          14 :   MPM.addPass(ConstantMergePass());
    1027             : 
    1028             :   // Remove unused arguments from functions.
    1029          14 :   MPM.addPass(DeadArgumentEliminationPass());
    1030             : 
    1031             :   // Reduce the code after globalopt and ipsccp.  Both can open up significant
    1032             :   // simplification opportunities, and both can propagate functions through
    1033             :   // function pointers.  When this happens, we often have to resolve varargs
    1034             :   // calls, etc, so let instcombine do this.
    1035             :   FunctionPassManager PeepholeFPM(DebugLogging);
    1036          14 :   if (Level == O3)
    1037           2 :     PeepholeFPM.addPass(AggressiveInstCombinePass());
    1038          28 :   PeepholeFPM.addPass(InstCombinePass());
    1039          14 :   invokePeepholeEPCallbacks(PeepholeFPM, Level);
    1040             : 
    1041          42 :   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
    1042             : 
    1043             :   // Note: historically, the PruneEH pass was run first to deduce nounwind and
    1044             :   // generally clean up exception handling overhead. It isn't clear this is
    1045             :   // valuable as the inliner doesn't currently care whether it is inlining an
    1046             :   // invoke or a call.
    1047             :   // Run the inliner now.
    1048          28 :   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
    1049          14 :       InlinerPass(getInlineParamsFromOptLevel(Level))));
    1050             : 
    1051             :   // Optimize globals again after we ran the inliner.
    1052          14 :   MPM.addPass(GlobalOptPass());
    1053             : 
    1054             :   // Garbage collect dead functions.
    1055             :   // FIXME: Add ArgumentPromotion pass after once it's ported.
    1056          14 :   MPM.addPass(GlobalDCEPass());
    1057             : 
    1058             :   FunctionPassManager FPM(DebugLogging);
    1059             :   // The IPO Passes may leave cruft around. Clean up after them.
    1060          28 :   FPM.addPass(InstCombinePass());
    1061          14 :   invokePeepholeEPCallbacks(FPM, Level);
    1062             : 
    1063          14 :   FPM.addPass(JumpThreadingPass());
    1064             : 
    1065             :   // Break up allocas
    1066          14 :   FPM.addPass(SROA());
    1067             : 
    1068             :   // Run a few AA driver optimizations here and now to cleanup the code.
    1069          42 :   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
    1070             : 
    1071          14 :   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
    1072             :               PostOrderFunctionAttrsPass()));
    1073             :   // FIXME: here we run IP alias analysis in the legacy PM.
    1074             : 
    1075             :   FunctionPassManager MainFPM;
    1076             : 
    1077             :   // FIXME: once we fix LoopPass Manager, add LICM here.
    1078             :   // FIXME: once we provide support for enabling MLSM, add it here.
    1079             :   // FIXME: once we provide support for enabling NewGVN, add it here.
    1080          14 :   if (RunNewGVN)
    1081           0 :     MainFPM.addPass(NewGVNPass());
    1082             :   else
    1083          14 :     MainFPM.addPass(GVN());
    1084             : 
    1085             :   // Remove dead memcpy()'s.
    1086          28 :   MainFPM.addPass(MemCpyOptPass());
    1087             : 
    1088             :   // Nuke dead stores.
    1089          14 :   MainFPM.addPass(DSEPass());
    1090             : 
    1091             :   // FIXME: at this point, we run a bunch of loop passes:
    1092             :   // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
    1093             :   // loopVectorize. Enable them once the remaining issue with LPM
    1094             :   // are sorted out.
    1095             : 
    1096          28 :   MainFPM.addPass(InstCombinePass());
    1097          14 :   MainFPM.addPass(SimplifyCFGPass());
    1098          14 :   MainFPM.addPass(SCCPPass());
    1099          28 :   MainFPM.addPass(InstCombinePass());
    1100          14 :   MainFPM.addPass(BDCEPass());
    1101             : 
    1102             :   // FIXME: We may want to run SLPVectorizer here.
    1103             :   // After vectorization, assume intrinsics may tell us more
    1104             :   // about pointer alignments.
    1105             : #if 0
    1106             :   MainFPM.add(AlignmentFromAssumptionsPass());
    1107             : #endif
    1108             : 
    1109             :   // FIXME: Conditionally run LoadCombine here, after it's ported
    1110             :   // (in case we still have this pass, given its questionable usefulness).
    1111             : 
    1112          28 :   MainFPM.addPass(InstCombinePass());
    1113          14 :   invokePeepholeEPCallbacks(MainFPM, Level);
    1114          14 :   MainFPM.addPass(JumpThreadingPass());
    1115          42 :   MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
    1116             : 
    1117             :   // Create a function that performs CFI checks for cross-DSO calls with
    1118             :   // targets in the current module.
    1119          14 :   MPM.addPass(CrossDSOCFIPass());
    1120             : 
    1121             :   // Lower type metadata and the type.test intrinsic. This pass supports
    1122             :   // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
    1123             :   // to be run at link time if CFI is enabled. This pass does nothing if
    1124             :   // CFI is disabled.
    1125             :   // Enable once we add support for the summary in the new PM.
    1126             : #if 0
    1127             :   MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
    1128             :                                            PassSummaryAction::None,
    1129             :                                 Summary));
    1130             : #endif
    1131             : 
    1132             :   // Add late LTO optimization passes.
    1133             :   // Delete basic blocks, which optimization passes may have killed.
    1134          14 :   MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
    1135             : 
    1136             :   // Drop bodies of available eternally objects to improve GlobalDCE.
    1137          14 :   MPM.addPass(EliminateAvailableExternallyPass());
    1138             : 
    1139             :   // Now that we have optimized the program, discard unreachable functions.
    1140          14 :   MPM.addPass(GlobalDCEPass());
    1141             : 
    1142             :   // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
    1143             :   return MPM;
    1144             : }
    1145             : 
    1146          59 : AAManager PassBuilder::buildDefaultAAPipeline() {
    1147             :   AAManager AA;
    1148             : 
    1149             :   // The order in which these are registered determines their priority when
    1150             :   // being queried.
    1151             : 
    1152             :   // First we register the basic alias analysis that provides the majority of
    1153             :   // per-function local AA logic. This is a stateless, on-demand local set of
    1154             :   // AA techniques.
    1155             :   AA.registerFunctionAnalysis<BasicAA>();
    1156             : 
    1157             :   // Next we query fast, specialized alias analyses that wrap IR-embedded
    1158             :   // information about aliasing.
    1159             :   AA.registerFunctionAnalysis<ScopedNoAliasAA>();
    1160             :   AA.registerFunctionAnalysis<TypeBasedAA>();
    1161             : 
    1162             :   // Add support for querying global aliasing information when available.
    1163             :   // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
    1164             :   // analysis, all that the `AAManager` can do is query for any *cached*
    1165             :   // results from `GlobalsAA` through a readonly proxy.
    1166             :   AA.registerModuleAnalysis<GlobalsAA>();
    1167             : 
    1168          59 :   return AA;
    1169             : }
    1170             : 
    1171        1648 : static Optional<int> parseRepeatPassName(StringRef Name) {
    1172             :   if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
    1173             :     return None;
    1174             :   int Count;
    1175           5 :   if (Name.getAsInteger(0, Count) || Count <= 0)
    1176             :     return None;
    1177             :   return Count;
    1178             : }
    1179             : 
    1180         474 : static Optional<int> parseDevirtPassName(StringRef Name) {
    1181             :   if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
    1182             :     return None;
    1183             :   int Count;
    1184           3 :   if (Name.getAsInteger(0, Count) || Count <= 0)
    1185             :     return None;
    1186             :   return Count;
    1187             : }
    1188             : 
    1189             : /// Tests whether a pass name starts with a valid prefix for a default pipeline
    1190             : /// alias.
    1191        1268 : static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
    1192             :   return Name.startswith("default") || Name.startswith("thinlto") ||
    1193        1268 :          Name.startswith("lto");
    1194             : }
    1195             : 
    1196             : /// Tests whether registered callbacks will accept a given pass name.
    1197             : ///
    1198             : /// When parsing a pipeline text, the type of the outermost pipeline may be
    1199             : /// omitted, in which case the type is automatically determined from the first
    1200             : /// pass name in the text. This may be a name that is handled through one of the
    1201             : /// callbacks. We check this through the oridinary parsing callbacks by setting
    1202             : /// up a dummy PassManager in order to not force the client to also handle this
    1203             : /// type of query.
    1204             : template <typename PassManagerT, typename CallbacksT>
    1205         914 : static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
    1206         914 :   if (!Callbacks.empty()) {
    1207             :     PassManagerT DummyPM;
    1208        1426 :     for (auto &CB : Callbacks)
    1209         489 :       if (CB(Name, DummyPM, {}))
    1210             :         return true;
    1211             :   }
    1212             :   return false;
    1213             : }
    1214             : 
    1215             : template <typename CallbacksT>
    1216         905 : static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
    1217             :   // Manually handle aliases for pre-configured pipeline fragments.
    1218         905 :   if (startsWithDefaultPipelineAliasPrefix(Name))
    1219          44 :     return DefaultAliasRegex.match(Name);
    1220             : 
    1221             :   // Explicitly handle pass manager names.
    1222             :   if (Name == "module")
    1223             :     return true;
    1224             :   if (Name == "cgscc")
    1225             :     return true;
    1226             :   if (Name == "function")
    1227             :     return true;
    1228             : 
    1229             :   // Explicitly handle custom-parsed pass names.
    1230         738 :   if (parseRepeatPassName(Name))
    1231             :     return true;
    1232             : 
    1233             : #define MODULE_PASS(NAME, CREATE_PASS)                                         \
    1234             :   if (Name == NAME)                                                            \
    1235             :     return true;
    1236             : #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
    1237             :   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
    1238             :     return true;
    1239             : #include "PassRegistry.def"
    1240             : 
    1241         474 :   return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
    1242             : }
    1243             : 
    1244             : template <typename CallbacksT>
    1245         470 : static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
    1246             :   // Explicitly handle pass manager names.
    1247             :   if (Name == "cgscc")
    1248             :     return true;
    1249             :   if (Name == "function")
    1250             :     return true;
    1251             : 
    1252             :   // Explicitly handle custom-parsed pass names.
    1253         470 :   if (parseRepeatPassName(Name))
    1254             :     return true;
    1255         470 :   if (parseDevirtPassName(Name))
    1256             :     return true;
    1257             : 
    1258             : #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
    1259             :   if (Name == NAME)                                                            \
    1260             :     return true;
    1261             : #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
    1262             :   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
    1263             :     return true;
    1264             : #include "PassRegistry.def"
    1265             : 
    1266         407 :   return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
    1267             : }
    1268             : 
    1269             : template <typename CallbacksT>
    1270         418 : static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
    1271             :   // Explicitly handle pass manager names.
    1272             :   if (Name == "function")
    1273             :     return true;
    1274             :   if (Name == "loop")
    1275             :     return true;
    1276             : 
    1277             :   // Explicitly handle custom-parsed pass names.
    1278         405 :   if (parseRepeatPassName(Name))
    1279             :     return true;
    1280             : 
    1281             : #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
    1282             :   if (Name == NAME)                                                            \
    1283             :     return true;
    1284             : #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
    1285             :   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
    1286             :     return true;
    1287             : #include "PassRegistry.def"
    1288             : 
    1289          25 :   return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
    1290             : }
    1291             : 
    1292             : template <typename CallbacksT>
    1293          21 : static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
    1294             :   // Explicitly handle pass manager names.
    1295             :   if (Name == "loop")
    1296             :     return true;
    1297             : 
    1298             :   // Explicitly handle custom-parsed pass names.
    1299          21 :   if (parseRepeatPassName(Name))
    1300             :     return true;
    1301             : 
    1302             : #define LOOP_PASS(NAME, CREATE_PASS)                                           \
    1303             :   if (Name == NAME)                                                            \
    1304             :     return true;
    1305             : #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
    1306             :   if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">")           \
    1307             :     return true;
    1308             : #include "PassRegistry.def"
    1309             : 
    1310           8 :   return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
    1311             : }
    1312             : 
    1313             : Optional<std::vector<PassBuilder::PipelineElement>>
    1314         935 : PassBuilder::parsePipelineText(StringRef Text) {
    1315         935 :   std::vector<PipelineElement> ResultPipeline;
    1316             : 
    1317             :   SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
    1318        1870 :       &ResultPipeline};
    1319             :   for (;;) {
    1320        1776 :     std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
    1321        1776 :     size_t Pos = Text.find_first_of(",()");
    1322        3552 :     Pipeline.push_back({Text.substr(0, Pos), {}});
    1323             : 
    1324             :     // If we have a single terminating name, we're done.
    1325        1776 :     if (Pos == Text.npos)
    1326             :       break;
    1327             : 
    1328        1119 :     char Sep = Text[Pos];
    1329        2238 :     Text = Text.substr(Pos + 1);
    1330        1119 :     if (Sep == ',')
    1331             :       // Just a name ending in a comma, continue.
    1332         438 :       continue;
    1333             : 
    1334        1035 :     if (Sep == '(') {
    1335             :       // Push the inner pipeline onto the stack to continue processing.
    1336         354 :       PipelineStack.push_back(&Pipeline.back().InnerPipeline);
    1337         354 :       continue;
    1338             :     }
    1339             : 
    1340             :     assert(Sep == ')' && "Bogus separator!");
    1341             :     // When handling the close parenthesis, we greedily consume them to avoid
    1342             :     // empty strings in the pipeline.
    1343             :     do {
    1344             :       // If we try to pop the outer pipeline we have unbalanced parentheses.
    1345         358 :       if (PipelineStack.size() == 1)
    1346             :         return None;
    1347             : 
    1348             :       PipelineStack.pop_back();
    1349             :     } while (Text.consume_front(")"));
    1350             : 
    1351             :     // Check if we've finished parsing.
    1352         320 :     if (Text.empty())
    1353             :       break;
    1354             : 
    1355             :     // Otherwise, the end of an inner pipeline always has to be followed by
    1356             :     // a comma, and then we can continue.
    1357             :     if (!Text.consume_front(","))
    1358             :       return None;
    1359             :   }
    1360             : 
    1361         927 :   if (PipelineStack.size() > 1)
    1362             :     // Unbalanced paretheses.
    1363             :     return None;
    1364             : 
    1365             :   assert(PipelineStack.back() == &ResultPipeline &&
    1366             :          "Wrong pipeline at the bottom of the stack!");
    1367             :   return {std::move(ResultPipeline)};
    1368             : }
    1369             : 
    1370         994 : bool PassBuilder::parseModulePass(ModulePassManager &MPM,
    1371             :                                   const PipelineElement &E, bool VerifyEachPass,
    1372             :                                   bool DebugLogging) {
    1373         994 :   auto &Name = E.Name;
    1374             :   auto &InnerPipeline = E.InnerPipeline;
    1375             : 
    1376             :   // First handle complex passes like the pass managers which carry pipelines.
    1377         994 :   if (!InnerPipeline.empty()) {
    1378             :     if (Name == "module") {
    1379           8 :       ModulePassManager NestedMPM(DebugLogging);
    1380          16 :       if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
    1381             :                                    DebugLogging))
    1382             :         return false;
    1383          16 :       MPM.addPass(std::move(NestedMPM));
    1384           8 :       return true;
    1385             :     }
    1386             :     if (Name == "cgscc") {
    1387         171 :       CGSCCPassManager CGPM(DebugLogging);
    1388         342 :       if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
    1389             :                                   DebugLogging))
    1390             :         return false;
    1391         510 :       MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
    1392         170 :       return true;
    1393             :     }
    1394             :     if (Name == "function") {
    1395         450 :       FunctionPassManager FPM(DebugLogging);
    1396         900 :       if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
    1397             :                                      DebugLogging))
    1398             :         return false;
    1399        1338 :       MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
    1400         446 :       return true;
    1401             :     }
    1402           2 :     if (auto Count = parseRepeatPassName(Name)) {
    1403           1 :       ModulePassManager NestedMPM(DebugLogging);
    1404           2 :       if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
    1405             :                                    DebugLogging))
    1406             :         return false;
    1407           4 :       MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
    1408           1 :       return true;
    1409             :     }
    1410             : 
    1411           3 :     for (auto &C : ModulePipelineParsingCallbacks)
    1412           1 :       if (C(Name, MPM, InnerPipeline))
    1413             :         return true;
    1414             : 
    1415             :     // Normal passes can't have pipelines.
    1416             :     return false;
    1417             :   }
    1418             : 
    1419             :   // Manually handle aliases for pre-configured pipeline fragments.
    1420         363 :   if (startsWithDefaultPipelineAliasPrefix(Name)) {
    1421             :     SmallVector<StringRef, 3> Matches;
    1422          44 :     if (!DefaultAliasRegex.match(Name, &Matches))
    1423             :       return false;
    1424             :     assert(Matches.size() == 3 && "Must capture two matched strings!");
    1425             : 
    1426          44 :     OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
    1427             :                               .Case("O0", O0)
    1428             :                               .Case("O1", O1)
    1429             :                               .Case("O2", O2)
    1430             :                               .Case("O3", O3)
    1431             :                               .Case("Os", Os)
    1432             :                               .Case("Oz", Oz);
    1433          44 :     if (L == O0)
    1434             :       // At O0 we do nothing at all!
    1435             :       return true;
    1436             : 
    1437             :     if (Matches[1] == "default") {
    1438          40 :       MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
    1439             :     } else if (Matches[1] == "thinlto-pre-link") {
    1440          16 :       MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
    1441             :     } else if (Matches[1] == "thinlto") {
    1442          14 :       MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
    1443             :     } else if (Matches[1] == "lto-pre-link") {
    1444           4 :       MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
    1445             :     } else {
    1446             :       assert(Matches[1] == "lto" && "Not one of the matched options!");
    1447          12 :       MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
    1448             :     }
    1449             :     return true;
    1450             :   }
    1451             : 
    1452             :   // Finally expand the basic registered passes from the .inc file.
    1453             : #define MODULE_PASS(NAME, CREATE_PASS)                                         \
    1454             :   if (Name == NAME) {                                                          \
    1455             :     MPM.addPass(CREATE_PASS);                                                  \
    1456             :     return true;                                                               \
    1457             :   }
    1458             : #define MODULE_ANALYSIS(NAME, CREATE_PASS)                                     \
    1459             :   if (Name == "require<" NAME ">") {                                           \
    1460             :     MPM.addPass(                                                               \
    1461             :         RequireAnalysisPass<                                                   \
    1462             :             std::remove_reference<decltype(CREATE_PASS)>::type, Module>());    \
    1463             :     return true;                                                               \
    1464             :   }                                                                            \
    1465             :   if (Name == "invalidate<" NAME ">") {                                        \
    1466             :     MPM.addPass(InvalidateAnalysisPass<                                        \
    1467             :                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
    1468             :     return true;                                                               \
    1469             :   }
    1470             : #include "PassRegistry.def"
    1471             : 
    1472          10 :   for (auto &C : ModulePipelineParsingCallbacks)
    1473          10 :     if (C(Name, MPM, InnerPipeline))
    1474             :       return true;
    1475             :   return false;
    1476             : }
    1477             : 
    1478         218 : bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
    1479             :                                  const PipelineElement &E, bool VerifyEachPass,
    1480             :                                  bool DebugLogging) {
    1481         218 :   auto &Name = E.Name;
    1482             :   auto &InnerPipeline = E.InnerPipeline;
    1483             : 
    1484             :   // First handle complex passes like the pass managers which carry pipelines.
    1485         218 :   if (!InnerPipeline.empty()) {
    1486             :     if (Name == "cgscc") {
    1487           0 :       CGSCCPassManager NestedCGPM(DebugLogging);
    1488           0 :       if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
    1489             :                                   DebugLogging))
    1490             :         return false;
    1491             :       // Add the nested pass manager with the appropriate adaptor.
    1492           0 :       CGPM.addPass(std::move(NestedCGPM));
    1493           0 :       return true;
    1494             :     }
    1495             :     if (Name == "function") {
    1496          30 :       FunctionPassManager FPM(DebugLogging);
    1497          60 :       if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
    1498             :                                      DebugLogging))
    1499             :         return false;
    1500             :       // Add the nested pass manager with the appropriate adaptor.
    1501          90 :       CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
    1502          30 :       return true;
    1503             :     }
    1504           5 :     if (auto Count = parseRepeatPassName(Name)) {
    1505           1 :       CGSCCPassManager NestedCGPM(DebugLogging);
    1506           2 :       if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
    1507             :                                   DebugLogging))
    1508             :         return false;
    1509           4 :       CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
    1510           1 :       return true;
    1511             :     }
    1512           4 :     if (auto MaxRepetitions = parseDevirtPassName(Name)) {
    1513           3 :       CGSCCPassManager NestedCGPM(DebugLogging);
    1514           6 :       if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
    1515             :                                   DebugLogging))
    1516             :         return false;
    1517           3 :       CGPM.addPass(
    1518           9 :           createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
    1519           3 :       return true;
    1520             :     }
    1521             : 
    1522           1 :     for (auto &C : CGSCCPipelineParsingCallbacks)
    1523           0 :       if (C(Name, CGPM, InnerPipeline))
    1524             :         return true;
    1525             : 
    1526             :     // Normal passes can't have pipelines.
    1527             :     return false;
    1528             :   }
    1529             : 
    1530             : // Now expand the basic registered passes from the .inc file.
    1531             : #define CGSCC_PASS(NAME, CREATE_PASS)                                          \
    1532             :   if (Name == NAME) {                                                          \
    1533             :     CGPM.addPass(CREATE_PASS);                                                 \
    1534             :     return true;                                                               \
    1535             :   }
    1536             : #define CGSCC_ANALYSIS(NAME, CREATE_PASS)                                      \
    1537             :   if (Name == "require<" NAME ">") {                                           \
    1538             :     CGPM.addPass(RequireAnalysisPass<                                          \
    1539             :                  std::remove_reference<decltype(CREATE_PASS)>::type,           \
    1540             :                  LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &,    \
    1541             :                  CGSCCUpdateResult &>());                                      \
    1542             :     return true;                                                               \
    1543             :   }                                                                            \
    1544             :   if (Name == "invalidate<" NAME ">") {                                        \
    1545             :     CGPM.addPass(InvalidateAnalysisPass<                                       \
    1546             :                  std::remove_reference<decltype(CREATE_PASS)>::type>());       \
    1547             :     return true;                                                               \
    1548             :   }
    1549             : #include "PassRegistry.def"
    1550             : 
    1551           3 :   for (auto &C : CGSCCPipelineParsingCallbacks)
    1552           3 :     if (C(Name, CGPM, InnerPipeline))
    1553             :       return true;
    1554             :   return false;
    1555             : }
    1556             : 
    1557         836 : bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
    1558             :                                     const PipelineElement &E,
    1559             :                                     bool VerifyEachPass, bool DebugLogging) {
    1560         836 :   auto &Name = E.Name;
    1561             :   auto &InnerPipeline = E.InnerPipeline;
    1562             : 
    1563             :   // First handle complex passes like the pass managers which carry pipelines.
    1564         836 :   if (!InnerPipeline.empty()) {
    1565             :     if (Name == "function") {
    1566           1 :       FunctionPassManager NestedFPM(DebugLogging);
    1567           2 :       if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
    1568             :                                      DebugLogging))
    1569             :         return false;
    1570             :       // Add the nested pass manager with the appropriate adaptor.
    1571           2 :       FPM.addPass(std::move(NestedFPM));
    1572           1 :       return true;
    1573             :     }
    1574             :     if (Name == "loop") {
    1575         146 :       LoopPassManager LPM(DebugLogging);
    1576         292 :       if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
    1577             :                                  DebugLogging))
    1578             :         return false;
    1579             :       // Add the nested pass manager with the appropriate adaptor.
    1580         145 :       FPM.addPass(
    1581         435 :           createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
    1582         145 :       return true;
    1583             :     }
    1584           5 :     if (auto Count = parseRepeatPassName(Name)) {
    1585           1 :       FunctionPassManager NestedFPM(DebugLogging);
    1586           2 :       if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
    1587             :                                      DebugLogging))
    1588             :         return false;
    1589           4 :       FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
    1590           1 :       return true;
    1591             :     }
    1592             : 
    1593          16 :     for (auto &C : FunctionPipelineParsingCallbacks)
    1594           8 :       if (C(Name, FPM, InnerPipeline))
    1595             :         return true;
    1596             : 
    1597             :     // Normal passes can't have pipelines.
    1598             :     return false;
    1599             :   }
    1600             : 
    1601             : // Now expand the basic registered passes from the .inc file.
    1602             : #define FUNCTION_PASS(NAME, CREATE_PASS)                                       \
    1603             :   if (Name == NAME) {                                                          \
    1604             :     FPM.addPass(CREATE_PASS);                                                  \
    1605             :     return true;                                                               \
    1606             :   }
    1607             : #define FUNCTION_ANALYSIS(NAME, CREATE_PASS)                                   \
    1608             :   if (Name == "require<" NAME ">") {                                           \
    1609             :     FPM.addPass(                                                               \
    1610             :         RequireAnalysisPass<                                                   \
    1611             :             std::remove_reference<decltype(CREATE_PASS)>::type, Function>());  \
    1612             :     return true;                                                               \
    1613             :   }                                                                            \
    1614             :   if (Name == "invalidate<" NAME ">") {                                        \
    1615             :     FPM.addPass(InvalidateAnalysisPass<                                        \
    1616             :                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
    1617             :     return true;                                                               \
    1618             :   }
    1619             : #include "PassRegistry.def"
    1620             : 
    1621          10 :   for (auto &C : FunctionPipelineParsingCallbacks)
    1622           7 :     if (C(Name, FPM, InnerPipeline))
    1623             :       return true;
    1624             :   return false;
    1625             : }
    1626             : 
    1627         163 : bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
    1628             :                                 bool VerifyEachPass, bool DebugLogging) {
    1629         163 :   StringRef Name = E.Name;
    1630             :   auto &InnerPipeline = E.InnerPipeline;
    1631             : 
    1632             :   // First handle complex passes like the pass managers which carry pipelines.
    1633         163 :   if (!InnerPipeline.empty()) {
    1634             :     if (Name == "loop") {
    1635           0 :       LoopPassManager NestedLPM(DebugLogging);
    1636           0 :       if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
    1637             :                                  DebugLogging))
    1638             :         return false;
    1639             :       // Add the nested pass manager with the appropriate adaptor.
    1640           0 :       LPM.addPass(std::move(NestedLPM));
    1641           0 :       return true;
    1642             :     }
    1643           2 :     if (auto Count = parseRepeatPassName(Name)) {
    1644           1 :       LoopPassManager NestedLPM(DebugLogging);
    1645           2 :       if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
    1646             :                                  DebugLogging))
    1647             :         return false;
    1648           4 :       LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
    1649           1 :       return true;
    1650             :     }
    1651             : 
    1652           1 :     for (auto &C : LoopPipelineParsingCallbacks)
    1653           0 :       if (C(Name, LPM, InnerPipeline))
    1654             :         return true;
    1655             : 
    1656             :     // Normal passes can't have pipelines.
    1657             :     return false;
    1658             :   }
    1659             : 
    1660             : // Now expand the basic registered passes from the .inc file.
    1661             : #define LOOP_PASS(NAME, CREATE_PASS)                                           \
    1662             :   if (Name == NAME) {                                                          \
    1663             :     LPM.addPass(CREATE_PASS);                                                  \
    1664             :     return true;                                                               \
    1665             :   }
    1666             : #define LOOP_ANALYSIS(NAME, CREATE_PASS)                                       \
    1667             :   if (Name == "require<" NAME ">") {                                           \
    1668             :     LPM.addPass(RequireAnalysisPass<                                           \
    1669             :                 std::remove_reference<decltype(CREATE_PASS)>::type, Loop,      \
    1670             :                 LoopAnalysisManager, LoopStandardAnalysisResults &,            \
    1671             :                 LPMUpdater &>());                                              \
    1672             :     return true;                                                               \
    1673             :   }                                                                            \
    1674             :   if (Name == "invalidate<" NAME ">") {                                        \
    1675             :     LPM.addPass(InvalidateAnalysisPass<                                        \
    1676             :                 std::remove_reference<decltype(CREATE_PASS)>::type>());        \
    1677             :     return true;                                                               \
    1678             :   }
    1679             : #include "PassRegistry.def"
    1680             : 
    1681           3 :   for (auto &C : LoopPipelineParsingCallbacks)
    1682           3 :     if (C(Name, LPM, InnerPipeline))
    1683             :       return true;
    1684             :   return false;
    1685             : }
    1686             : 
    1687         145 : bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
    1688             : #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS)                               \
    1689             :   if (Name == NAME) {                                                          \
    1690             :     AA.registerModuleAnalysis<                                                 \
    1691             :         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
    1692             :     return true;                                                               \
    1693             :   }
    1694             : #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS)                             \
    1695             :   if (Name == NAME) {                                                          \
    1696             :     AA.registerFunctionAnalysis<                                               \
    1697             :         std::remove_reference<decltype(CREATE_PASS)>::type>();                 \
    1698             :     return true;                                                               \
    1699             :   }
    1700             : #include "PassRegistry.def"
    1701             : 
    1702           2 :   for (auto &C : AAParsingCallbacks)
    1703           0 :     if (C(Name, AA))
    1704             :       return true;
    1705             :   return false;
    1706             : }
    1707             : 
    1708         151 : bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
    1709             :                                         ArrayRef<PipelineElement> Pipeline,
    1710             :                                         bool VerifyEachPass,
    1711             :                                         bool DebugLogging) {
    1712         475 :   for (const auto &Element : Pipeline) {
    1713         163 :     if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
    1714             :       return false;
    1715             :     // FIXME: No verifier support for Loop passes!
    1716             :   }
    1717             :   return true;
    1718             : }
    1719             : 
    1720         495 : bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
    1721             :                                             ArrayRef<PipelineElement> Pipeline,
    1722             :                                             bool VerifyEachPass,
    1723             :                                             bool DebugLogging) {
    1724        2159 :   for (const auto &Element : Pipeline) {
    1725         836 :     if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
    1726             :       return false;
    1727         832 :     if (VerifyEachPass)
    1728          14 :       FPM.addPass(VerifierPass());
    1729             :   }
    1730             :   return true;
    1731             : }
    1732             : 
    1733         177 : bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
    1734             :                                          ArrayRef<PipelineElement> Pipeline,
    1735             :                                          bool VerifyEachPass,
    1736             :                                          bool DebugLogging) {
    1737         611 :   for (const auto &Element : Pipeline) {
    1738         218 :     if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
    1739             :       return false;
    1740             :     // FIXME: No verifier support for CGSCC passes!
    1741             :   }
    1742             :   return true;
    1743             : }
    1744             : 
    1745         957 : void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
    1746             :                                        FunctionAnalysisManager &FAM,
    1747             :                                        CGSCCAnalysisManager &CGAM,
    1748             :                                        ModuleAnalysisManager &MAM) {
    1749        1914 :   MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
    1750        1914 :   MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
    1751        1914 :   CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
    1752        1914 :   FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
    1753        1914 :   FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
    1754        1914 :   FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
    1755        1914 :   LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
    1756         957 : }
    1757             : 
    1758         908 : bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
    1759             :                                           ArrayRef<PipelineElement> Pipeline,
    1760             :                                           bool VerifyEachPass,
    1761             :                                           bool DebugLogging) {
    1762        2882 :   for (const auto &Element : Pipeline) {
    1763         994 :     if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
    1764             :       return false;
    1765         987 :     if (VerifyEachPass)
    1766          21 :       MPM.addPass(VerifierPass());
    1767             :   }
    1768             :   return true;
    1769             : }
    1770             : 
    1771             : // Primary pass pipeline description parsing routine for a \c ModulePassManager
    1772             : // FIXME: Should this routine accept a TargetMachine or require the caller to
    1773             : // pre-populate the analysis managers with target-specific stuff?
    1774         916 : bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
    1775             :                                     StringRef PipelineText, bool VerifyEachPass,
    1776             :                                     bool DebugLogging) {
    1777         916 :   auto Pipeline = parsePipelineText(PipelineText);
    1778        1821 :   if (!Pipeline || Pipeline->empty())
    1779             :     return false;
    1780             : 
    1781             :   // If the first name isn't at the module layer, wrap the pipeline up
    1782             :   // automatically.
    1783         905 :   StringRef FirstName = Pipeline->front().Name;
    1784             : 
    1785         905 :   if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
    1786         468 :     if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
    1787         126 :       Pipeline = {{"cgscc", std::move(*Pipeline)}};
    1788         405 :     } else if (isFunctionPassName(FirstName,
    1789             :                                   FunctionPipelineParsingCallbacks)) {
    1790         768 :       Pipeline = {{"function", std::move(*Pipeline)}};
    1791          21 :     } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
    1792          45 :       Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
    1793             :     } else {
    1794           8 :       for (auto &C : TopLevelPipelineParsingCallbacks)
    1795           2 :         if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
    1796             :           return true;
    1797             : 
    1798             :       // Unknown pass name!
    1799             :       return false;
    1800             :     }
    1801             :   }
    1802             : 
    1803        1798 :   return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
    1804             : }
    1805             : 
    1806             : // Primary pass pipeline description parsing routine for a \c CGSCCPassManager
    1807           2 : bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
    1808             :                                     StringRef PipelineText, bool VerifyEachPass,
    1809             :                                     bool DebugLogging) {
    1810           2 :   auto Pipeline = parsePipelineText(PipelineText);
    1811           4 :   if (!Pipeline || Pipeline->empty())
    1812             :     return false;
    1813             : 
    1814           2 :   StringRef FirstName = Pipeline->front().Name;
    1815           2 :   if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
    1816             :     return false;
    1817             : 
    1818           4 :   return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
    1819             : }
    1820             : 
    1821             : // Primary pass pipeline description parsing routine for a \c
    1822             : // FunctionPassManager
    1823          13 : bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
    1824             :                                     StringRef PipelineText, bool VerifyEachPass,
    1825             :                                     bool DebugLogging) {
    1826          13 :   auto Pipeline = parsePipelineText(PipelineText);
    1827          26 :   if (!Pipeline || Pipeline->empty())
    1828             :     return false;
    1829             : 
    1830          13 :   StringRef FirstName = Pipeline->front().Name;
    1831          13 :   if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
    1832             :     return false;
    1833             : 
    1834          26 :   return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
    1835          13 :                                    DebugLogging);
    1836             : }
    1837             : 
    1838             : // Primary pass pipeline description parsing routine for a \c LoopPassManager
    1839           4 : bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
    1840             :                                     StringRef PipelineText, bool VerifyEachPass,
    1841             :                                     bool DebugLogging) {
    1842           4 :   auto Pipeline = parsePipelineText(PipelineText);
    1843           8 :   if (!Pipeline || Pipeline->empty())
    1844             :     return false;
    1845             : 
    1846           8 :   return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
    1847             : }
    1848             : 
    1849         905 : bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
    1850             :   // If the pipeline just consists of the word 'default' just replace the AA
    1851             :   // manager with our default one.
    1852             :   if (PipelineText == "default") {
    1853          38 :     AA = buildDefaultAAPipeline();
    1854          19 :     return true;
    1855             :   }
    1856             : 
    1857        1172 :   while (!PipelineText.empty()) {
    1858             :     StringRef Name;
    1859         145 :     std::tie(Name, PipelineText) = PipelineText.split(',');
    1860         145 :     if (!parseAAPassName(AA, Name))
    1861           2 :       return false;
    1862             :   }
    1863             : 
    1864             :   return true;
    1865      299229 : }

Generated by: LCOV version 1.13