LCOV - code coverage report
Current view: top level - lib/IR - LegacyPassManager.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 694 797 87.1 %
Date: 2018-10-20 13:21:21 Functions: 92 114 80.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
       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             : //
      10             : // This file implements the legacy LLVM Pass Manager infrastructure.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/IR/LegacyPassManager.h"
      15             : #include "llvm/ADT/MapVector.h"
      16             : #include "llvm/ADT/Statistic.h"
      17             : #include "llvm/IR/DiagnosticInfo.h"
      18             : #include "llvm/IR/IRPrintingPasses.h"
      19             : #include "llvm/IR/LLVMContext.h"
      20             : #include "llvm/IR/LegacyPassManagers.h"
      21             : #include "llvm/IR/LegacyPassNameParser.h"
      22             : #include "llvm/IR/Module.h"
      23             : #include "llvm/IR/PassTimingInfo.h"
      24             : #include "llvm/Support/Chrono.h"
      25             : #include "llvm/Support/CommandLine.h"
      26             : #include "llvm/Support/Debug.h"
      27             : #include "llvm/Support/Error.h"
      28             : #include "llvm/Support/ErrorHandling.h"
      29             : #include "llvm/Support/ManagedStatic.h"
      30             : #include "llvm/Support/Mutex.h"
      31             : #include "llvm/Support/Timer.h"
      32             : #include "llvm/Support/raw_ostream.h"
      33             : #include <algorithm>
      34             : #include <unordered_set>
      35             : using namespace llvm;
      36             : using namespace llvm::legacy;
      37             : 
      38             : // See PassManagers.h for Pass Manager infrastructure overview.
      39             : 
      40             : //===----------------------------------------------------------------------===//
      41             : // Pass debugging information.  Often it is useful to find out what pass is
      42             : // running when a crash occurs in a utility.  When this library is compiled with
      43             : // debugging on, a command line option (--debug-pass) is enabled that causes the
      44             : // pass name to be printed before it executes.
      45             : //
      46             : 
      47             : namespace {
      48             : // Different debug levels that can be enabled...
      49             : enum PassDebugLevel {
      50             :   Disabled, Arguments, Structure, Executions, Details
      51             : };
      52             : }
      53             : 
      54             : static cl::opt<enum PassDebugLevel>
      55             : PassDebugging("debug-pass", cl::Hidden,
      56             :                   cl::desc("Print PassManager debugging information"),
      57             :                   cl::values(
      58             :   clEnumVal(Disabled  , "disable debug output"),
      59             :   clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
      60             :   clEnumVal(Structure , "print pass structure before run()"),
      61             :   clEnumVal(Executions, "print pass name before it is executed"),
      62             :   clEnumVal(Details   , "print pass details when it is executed")));
      63             : 
      64             : namespace {
      65             : typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
      66             : PassOptionList;
      67             : }
      68             : 
      69             : // Print IR out before/after specified passes.
      70             : static PassOptionList
      71             : PrintBefore("print-before",
      72             :             llvm::cl::desc("Print IR before specified passes"),
      73             :             cl::Hidden);
      74             : 
      75             : static PassOptionList
      76             : PrintAfter("print-after",
      77             :            llvm::cl::desc("Print IR after specified passes"),
      78             :            cl::Hidden);
      79             : 
      80             : static cl::opt<bool> PrintBeforeAll("print-before-all",
      81             :                                     llvm::cl::desc("Print IR before each pass"),
      82             :                                     cl::init(false), cl::Hidden);
      83             : static cl::opt<bool> PrintAfterAll("print-after-all",
      84             :                                    llvm::cl::desc("Print IR after each pass"),
      85             :                                    cl::init(false), cl::Hidden);
      86             : 
      87             : static cl::opt<bool>
      88             :     PrintModuleScope("print-module-scope",
      89             :                      cl::desc("When printing IR for print-[before|after]{-all} "
      90             :                               "always print a module IR"),
      91             :                      cl::init(false), cl::Hidden);
      92             : 
      93             : static cl::list<std::string>
      94             :     PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
      95             :                    cl::desc("Only print IR for functions whose name "
      96             :                             "match this for all print-[before|after][-all] "
      97             :                             "options"),
      98             :                    cl::CommaSeparated, cl::Hidden);
      99             : 
     100             : /// This is a helper to determine whether to print IR before or
     101             : /// after a pass.
     102             : 
     103         936 : bool llvm::shouldPrintBeforePass() {
     104         936 :   return PrintBeforeAll || !PrintBefore.empty();
     105             : }
     106             : 
     107         936 : bool llvm::shouldPrintAfterPass() {
     108         936 :   return PrintAfterAll || !PrintAfter.empty();
     109             : }
     110             : 
     111     4863612 : static bool ShouldPrintBeforeOrAfterPass(StringRef PassID,
     112             :                                          PassOptionList &PassesToPrint) {
     113     4865793 :   for (auto *PassInf : PassesToPrint) {
     114        2208 :     if (PassInf)
     115             :       if (PassInf->getPassArgument() == PassID) {
     116             :         return true;
     117             :       }
     118             :   }
     119             :   return false;
     120             : }
     121             : 
     122     2432057 : bool llvm::shouldPrintBeforePass(StringRef PassID) {
     123     2432057 :   return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PassID, PrintBefore);
     124             : }
     125             : 
     126     2432113 : bool llvm::shouldPrintAfterPass(StringRef PassID) {
     127     2432113 :   return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PassID, PrintAfter);
     128             : }
     129             : 
     130         530 : bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
     131             : 
     132       16616 : bool llvm::isFunctionInPrintList(StringRef FunctionName) {
     133             :   static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
     134       31063 :                                                         PrintFuncsList.end());
     135       16814 :   return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
     136             : }
     137             : /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
     138             : /// or higher is specified.
     139     1431128 : bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
     140     1431128 :   return PassDebugging >= Executions;
     141             : }
     142             : 
     143          42 : unsigned PMDataManager::initSizeRemarkInfo(
     144             :     Module &M, StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount) {
     145             :   // Only calculate getInstructionCount if the size-info remark is requested.
     146             :   unsigned InstrCount = 0;
     147             : 
     148             :   // Collect instruction counts for every function. We'll use this to emit
     149             :   // per-function size remarks later.
     150         184 :   for (Function &F : M) {
     151         142 :     unsigned FCount = F.getInstructionCount();
     152             : 
     153             :     // Insert a record into FunctionToInstrCount keeping track of the current
     154             :     // size of the function as the first member of a pair. Set the second
     155             :     // member to 0; if the function is deleted by the pass, then when we get
     156             :     // here, we'll be able to let the user know that F no longer contributes to
     157             :     // the module.
     158         284 :     FunctionToInstrCount[F.getName().str()] =
     159             :         std::pair<unsigned, unsigned>(FCount, 0);
     160         142 :     InstrCount += FCount;
     161             :   }
     162          42 :   return InstrCount;
     163             : }
     164             : 
     165          18 : void PMDataManager::emitInstrCountChangedRemark(
     166             :     Pass *P, Module &M, int64_t Delta, unsigned CountBefore,
     167             :     StringMap<std::pair<unsigned, unsigned>> &FunctionToInstrCount,
     168             :     Function *F) {
     169             :   // If it's a pass manager, don't emit a remark. (This hinges on the assumption
     170             :   // that the only passes that return non-null with getAsPMDataManager are pass
     171             :   // managers.) The reason we have to do this is to avoid emitting remarks for
     172             :   // CGSCC passes.
     173          18 :   if (P->getAsPMDataManager())
     174           6 :     return;
     175             : 
     176             :   // Set to true if this isn't a module pass or CGSCC pass.
     177          12 :   bool CouldOnlyImpactOneFunction = (F != nullptr);
     178             : 
     179             :   // Helper lambda that updates the changes to the size of some function.
     180             :   auto UpdateFunctionChanges =
     181             :       [&FunctionToInstrCount](Function &MaybeChangedFn) {
     182             :         // Update the total module count.
     183             :         unsigned FnSize = MaybeChangedFn.getInstructionCount();
     184             :         auto It = FunctionToInstrCount.find(MaybeChangedFn.getName());
     185             : 
     186             :         // If we created a new function, then we need to add it to the map and
     187             :         // say that it changed from 0 instructions to FnSize.
     188             :         if (It == FunctionToInstrCount.end()) {
     189             :           FunctionToInstrCount[MaybeChangedFn.getName()] =
     190             :               std::pair<unsigned, unsigned>(0, FnSize);
     191             :           return;
     192             :         }
     193             :         // Insert the new function size into the second member of the pair. This
     194             :         // tells us whether or not this function changed in size.
     195             :         It->second.second = FnSize;
     196          12 :       };
     197             : 
     198             :   // We need to initially update all of the function sizes.
     199             :   // If no function was passed in, then we're either a module pass or an
     200             :   // CGSCC pass.
     201          12 :   if (!CouldOnlyImpactOneFunction)
     202           3 :     std::for_each(M.begin(), M.end(), UpdateFunctionChanges);
     203             :   else
     204           9 :     UpdateFunctionChanges(*F);
     205             : 
     206             :   // Do we have a function we can use to emit a remark?
     207          12 :   if (!CouldOnlyImpactOneFunction) {
     208             :     // We need a function containing at least one basic block in order to output
     209             :     // remarks. Since it's possible that the first function in the module
     210             :     // doesn't actually contain a basic block, we have to go and find one that's
     211             :     // suitable for emitting remarks.
     212             :     auto It = std::find_if(M.begin(), M.end(),
     213           0 :                           [](const Function &Fn) { return !Fn.empty(); });
     214             : 
     215             :     // Didn't find a function. Quit.
     216           3 :     if (It == M.end())
     217             :       return;
     218             : 
     219             :     // We found a function containing at least one basic block.
     220           3 :     F = &*It;
     221             :   }
     222          12 :   int64_t CountAfter = static_cast<int64_t>(CountBefore) + Delta;
     223          12 :   BasicBlock &BB = *F->begin();
     224             :   OptimizationRemarkAnalysis R("size-info", "IRSizeChange",
     225          24 :                                DiagnosticLocation(), &BB);
     226             :   // FIXME: Move ore namespace to DiagnosticInfo so that we can use it. This
     227             :   // would let us use NV instead of DiagnosticInfoOptimizationBase::Argument.
     228          12 :   R << DiagnosticInfoOptimizationBase::Argument("Pass", P->getPassName())
     229             :     << ": IR instruction count changed from "
     230          24 :     << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore", CountBefore)
     231             :     << " to "
     232          24 :     << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter", CountAfter)
     233             :     << "; Delta: "
     234          60 :     << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", Delta);
     235          12 :   F->getContext().diagnose(R); // Not using ORE for layering reasons.
     236             : 
     237             :   // Emit per-function size change remarks separately.
     238          12 :   std::string PassName = P->getPassName().str();
     239             : 
     240             :   // Helper lambda that emits a remark when the size of a function has changed.
     241             :   auto EmitFunctionSizeChangedRemark = [&FunctionToInstrCount, &F, &BB,
     242             :                                         &PassName](const std::string &Fname) {
     243             :     unsigned FnCountBefore, FnCountAfter;
     244             :     std::pair<unsigned, unsigned> &Change = FunctionToInstrCount[Fname];
     245             :     std::tie(FnCountBefore, FnCountAfter) = Change;
     246             :     int64_t FnDelta = static_cast<int64_t>(FnCountAfter) -
     247             :                       static_cast<int64_t>(FnCountBefore);
     248             : 
     249             :     if (FnDelta == 0)
     250             :       return;
     251             : 
     252             :     // FIXME: We shouldn't use BB for the location here. Unfortunately, because
     253             :     // the function that we're looking at could have been deleted, we can't use
     254             :     // it for the source location. We *want* remarks when a function is deleted
     255             :     // though, so we're kind of stuck here as is. (This remark, along with the
     256             :     // whole-module size change remarks really ought not to have source
     257             :     // locations at all.)
     258             :     OptimizationRemarkAnalysis FR("size-info", "FunctionIRSizeChange",
     259             :                                   DiagnosticLocation(), &BB);
     260             :     FR << DiagnosticInfoOptimizationBase::Argument("Pass", PassName)
     261             :        << ": Function: "
     262             :        << DiagnosticInfoOptimizationBase::Argument("Function", Fname)
     263             :        << ": IR instruction count changed from "
     264             :        << DiagnosticInfoOptimizationBase::Argument("IRInstrsBefore",
     265             :                                                    FnCountBefore)
     266             :        << " to "
     267             :        << DiagnosticInfoOptimizationBase::Argument("IRInstrsAfter",
     268             :                                                    FnCountAfter)
     269             :        << "; Delta: "
     270             :        << DiagnosticInfoOptimizationBase::Argument("DeltaInstrCount", FnDelta);
     271             :     F->getContext().diagnose(FR);
     272             : 
     273             :     // Update the function size.
     274             :     Change.first = FnCountAfter;
     275          12 :   };
     276             : 
     277             :   // Are we looking at more than one function? If so, emit remarks for all of
     278             :   // the functions in the module. Otherwise, only emit one remark.
     279          12 :   if (!CouldOnlyImpactOneFunction)
     280           3 :     std::for_each(FunctionToInstrCount.keys().begin(),
     281             :                   FunctionToInstrCount.keys().end(),
     282           3 :                   EmitFunctionSizeChangedRemark);
     283             :   else
     284          27 :     EmitFunctionSizeChangedRemark(F->getName().str());
     285             : }
     286             : 
     287           0 : void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
     288           0 :   if (!V && !M)
     289           0 :     OS << "Releasing pass '";
     290             :   else
     291           0 :     OS << "Running pass '";
     292             : 
     293           0 :   OS << P->getPassName() << "'";
     294             : 
     295           0 :   if (M) {
     296           0 :     OS << " on module '" << M->getModuleIdentifier() << "'.\n";
     297           0 :     return;
     298             :   }
     299           0 :   if (!V) {
     300             :     OS << '\n';
     301           0 :     return;
     302             :   }
     303             : 
     304           0 :   OS << " on ";
     305           0 :   if (isa<Function>(V))
     306           0 :     OS << "function";
     307           0 :   else if (isa<BasicBlock>(V))
     308           0 :     OS << "basic block";
     309             :   else
     310           0 :     OS << "value";
     311             : 
     312           0 :   OS << " '";
     313           0 :   V->printAsOperand(OS, /*PrintTy=*/false, M);
     314           0 :   OS << "'\n";
     315             : }
     316             : 
     317             : 
     318             : namespace {
     319             : //===----------------------------------------------------------------------===//
     320             : // BBPassManager
     321             : //
     322             : /// BBPassManager manages BasicBlockPass. It batches all the
     323             : /// pass together and sequence them to process one basic block before
     324             : /// processing next basic block.
     325             : class BBPassManager : public PMDataManager, public FunctionPass {
     326             : 
     327             : public:
     328             :   static char ID;
     329             :   explicit BBPassManager()
     330         250 :     : PMDataManager(), FunctionPass(ID) {}
     331             : 
     332             :   /// Execute all of the passes scheduled for execution.  Keep track of
     333             :   /// whether any of the passes modifies the function, and if so, return true.
     334             :   bool runOnFunction(Function &F) override;
     335             : 
     336             :   /// Pass Manager itself does not invalidate any analysis info.
     337         250 :   void getAnalysisUsage(AnalysisUsage &Info) const override {
     338             :     Info.setPreservesAll();
     339         250 :   }
     340             : 
     341             :   bool doInitialization(Module &M) override;
     342             :   bool doInitialization(Function &F);
     343             :   bool doFinalization(Module &M) override;
     344             :   bool doFinalization(Function &F);
     345             : 
     346         250 :   PMDataManager *getAsPMDataManager() override { return this; }
     347           0 :   Pass *getAsPass() override { return this; }
     348             : 
     349           0 :   StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
     350             : 
     351             :   // Print passes managed by this manager
     352           0 :   void dumpPassStructure(unsigned Offset) override {
     353           0 :     dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
     354           0 :     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
     355             :       BasicBlockPass *BP = getContainedPass(Index);
     356           0 :       BP->dumpPassStructure(Offset + 1);
     357           0 :       dumpLastUses(BP, Offset+1);
     358             :     }
     359           0 :   }
     360             : 
     361             :   BasicBlockPass *getContainedPass(unsigned N) {
     362             :     assert(N < PassVector.size() && "Pass number out of range!");
     363        3949 :     BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
     364             :     return BP;
     365             :   }
     366             : 
     367         249 :   PassManagerType getPassManagerType() const override {
     368         249 :     return PMT_BasicBlockPassManager;
     369             :   }
     370             : };
     371             : 
     372             : char BBPassManager::ID = 0;
     373             : } // End anonymous namespace
     374             : 
     375             : namespace llvm {
     376             : namespace legacy {
     377             : //===----------------------------------------------------------------------===//
     378             : // FunctionPassManagerImpl
     379             : //
     380             : /// FunctionPassManagerImpl manages FPPassManagers
     381             : class FunctionPassManagerImpl : public Pass,
     382             :                                 public PMDataManager,
     383             :                                 public PMTopLevelManager {
     384             :   virtual void anchor();
     385             : private:
     386             :   bool wasRun;
     387             : public:
     388             :   static char ID;
     389       23062 :   explicit FunctionPassManagerImpl() :
     390             :     Pass(PT_PassManager, ID), PMDataManager(),
     391       46124 :     PMTopLevelManager(new FPPassManager()), wasRun(false) {}
     392             : 
     393             :   /// \copydoc FunctionPassManager::add()
     394             :   void add(Pass *P) {
     395       71520 :     schedulePass(P);
     396             :   }
     397             : 
     398             :   /// createPrinterPass - Get a function printer pass.
     399           0 :   Pass *createPrinterPass(raw_ostream &O,
     400             :                           const std::string &Banner) const override {
     401           0 :     return createPrintFunctionPass(O, Banner);
     402             :   }
     403             : 
     404             :   // Prepare for running an on the fly pass, freeing memory if needed
     405             :   // from a previous run.
     406             :   void releaseMemoryOnTheFly();
     407             : 
     408             :   /// run - Execute all of the passes scheduled for execution.  Keep track of
     409             :   /// whether any of the passes modifies the module, and if so, return true.
     410             :   bool run(Function &F);
     411             : 
     412             :   /// doInitialization - Run all of the initializers for the function passes.
     413             :   ///
     414             :   bool doInitialization(Module &M) override;
     415             : 
     416             :   /// doFinalization - Run all of the finalizers for the function passes.
     417             :   ///
     418             :   bool doFinalization(Module &M) override;
     419             : 
     420             : 
     421       37495 :   PMDataManager *getAsPMDataManager() override { return this; }
     422           0 :   Pass *getAsPass() override { return this; }
     423       50435 :   PassManagerType getTopLevelPassManagerType() override {
     424       50435 :     return PMT_FunctionPassManager;
     425             :   }
     426             : 
     427             :   /// Pass Manager itself does not invalidate any analysis info.
     428           0 :   void getAnalysisUsage(AnalysisUsage &Info) const override {
     429             :     Info.setPreservesAll();
     430           0 :   }
     431             : 
     432             :   FPPassManager *getContainedManager(unsigned N) {
     433             :     assert(N < PassManagers.size() && "Pass number out of range!");
     434     3701974 :     FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
     435             :     return FP;
     436             :   }
     437             : };
     438             : 
     439           0 : void FunctionPassManagerImpl::anchor() {}
     440             : 
     441             : char FunctionPassManagerImpl::ID = 0;
     442             : } // End of legacy namespace
     443             : } // End of llvm namespace
     444             : 
     445             : namespace {
     446             : //===----------------------------------------------------------------------===//
     447             : // MPPassManager
     448             : //
     449             : /// MPPassManager manages ModulePasses and function pass managers.
     450             : /// It batches all Module passes and function pass managers together and
     451             : /// sequences them to process one module.
     452             : class MPPassManager : public Pass, public PMDataManager {
     453             : public:
     454             :   static char ID;
     455       62392 :   explicit MPPassManager() :
     456       62392 :     Pass(PT_PassManager, ID), PMDataManager() { }
     457             : 
     458             :   // Delete on the fly managers.
     459      186387 :   ~MPPassManager() override {
     460       70009 :     for (auto &OnTheFlyManager : OnTheFlyManagers) {
     461        7880 :       FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
     462        7880 :       delete FPP;
     463             :     }
     464      124258 :   }
     465       62129 : 
     466             :   /// createPrinterPass - Get a module printer pass.
     467             :   Pass *createPrinterPass(raw_ostream &O,
     468             :                           const std::string &Banner) const override {
     469             :     return createPrintModulePass(O, Banner);
     470       62129 :   }
     471      124258 : 
     472       70009 :   /// run - Execute all of the passes scheduled for execution.  Keep track of
     473        7880 :   /// whether any of the passes modifies the module, and if so, return true.
     474        7880 :   bool runOnModule(Module &M);
     475             : 
     476       62129 :   using llvm::Pass::doInitialization;
     477             :   using llvm::Pass::doFinalization;
     478             : 
     479           0 :   /// Pass Manager itself does not invalidate any analysis info.
     480             :   void getAnalysisUsage(AnalysisUsage &Info) const override {
     481           0 :     Info.setPreservesAll();
     482             :   }
     483             : 
     484             :   /// Add RequiredPass into list of lower level passes required by pass P.
     485             :   /// RequiredPass is run on the fly by Pass Manager when P requests it
     486             :   /// through getAnalysis interface.
     487             :   void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
     488             : 
     489             :   /// Return function pass corresponding to PassInfo PI, that is
     490             :   /// required by module pass MP. Instantiate analysis pass, by using
     491             :   /// its runOnFunction() for function F.
     492           0 :   Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
     493             : 
     494           0 :   StringRef getPassName() const override { return "Module Pass Manager"; }
     495             : 
     496             :   PMDataManager *getAsPMDataManager() override { return this; }
     497             :   Pass *getAsPass() override { return this; }
     498             : 
     499             :   // Print passes managed by this manager
     500             :   void dumpPassStructure(unsigned Offset) override {
     501             :     dbgs().indent(Offset*2) << "ModulePass Manager\n";
     502             :     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
     503             :       ModulePass *MP = getContainedPass(Index);
     504             :       MP->dumpPassStructure(Offset + 1);
     505             :       MapVector<Pass *, FunctionPassManagerImpl *>::const_iterator I =
     506           0 :           OnTheFlyManagers.find(MP);
     507             :       if (I != OnTheFlyManagers.end())
     508           0 :         I->second->dumpPassStructure(Offset + 2);
     509     4373156 :       dumpLastUses(MP, Offset+1);
     510             :     }
     511             :   }
     512          38 : 
     513          38 :   ModulePass *getContainedPass(unsigned N) {
     514         346 :     assert(N < PassVector.size() && "Pass number out of range!");
     515             :     return static_cast<ModulePass *>(PassVector[N]);
     516         308 :   }
     517             : 
     518         308 :   PassManagerType getPassManagerType() const override {
     519         308 :     return PMT_ModulePassManager;
     520          30 :   }
     521         308 : 
     522             :  private:
     523          38 :   /// Collection of on the fly FPPassManagers. These managers manage
     524             :   /// function passes that are required by module passes.
     525             :    MapVector<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
     526             : };
     527      537008 : 
     528             : char MPPassManager::ID = 0;
     529             : } // End anonymous namespace
     530      567597 : 
     531      567597 : namespace llvm {
     532             : namespace legacy {
     533             : //===----------------------------------------------------------------------===//
     534             : // PassManagerImpl
     535             : //
     536             : 
     537             : /// PassManagerImpl manages MPPassManagers
     538             : class PassManagerImpl : public Pass,
     539             :                         public PMDataManager,
     540             :                         public PMTopLevelManager {
     541             :   virtual void anchor();
     542             : 
     543             : public:
     544             :   static char ID;
     545             :   explicit PassManagerImpl() :
     546             :     Pass(PT_PassManager, ID), PMDataManager(),
     547             :                               PMTopLevelManager(new MPPassManager()) {}
     548             : 
     549             :   /// \copydoc PassManager::add()
     550             :   void add(Pass *P) {
     551             :     schedulePass(P);
     552             :   }
     553             : 
     554             :   /// createPrinterPass - Get a module printer pass.
     555             :   Pass *createPrinterPass(raw_ostream &O,
     556             :                           const std::string &Banner) const override {
     557       62392 :     return createPrintModulePass(O, Banner);
     558             :   }
     559       62392 : 
     560             :   /// run - Execute all of the passes scheduled for execution.  Keep track of
     561             :   /// whether any of the passes modifies the module, and if so, return true.
     562             :   bool run(Module &M);
     563     2726449 : 
     564             :   using llvm::Pass::doInitialization;
     565             :   using llvm::Pass::doFinalization;
     566             : 
     567           0 :   /// Pass Manager itself does not invalidate any analysis info.
     568             :   void getAnalysisUsage(AnalysisUsage &Info) const override {
     569           0 :     Info.setPreservesAll();
     570             :   }
     571             : 
     572             :   PMDataManager *getAsPMDataManager() override { return this; }
     573             :   Pass *getAsPass() override { return this; }
     574             :   PassManagerType getTopLevelPassManagerType() override {
     575             :     return PMT_ModulePassManager;
     576             :   }
     577             : 
     578             :   MPPassManager *getContainedManager(unsigned N) {
     579             :     assert(N < PassManagers.size() && "Pass number out of range!");
     580           0 :     MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
     581             :     return MP;
     582           0 :   }
     583             : };
     584      383230 : 
     585           0 : void PassManagerImpl::anchor() {}
     586     4120916 : 
     587     4120916 : char PassManagerImpl::ID = 0;
     588             : } // End of legacy namespace
     589             : } // End of llvm namespace
     590             : 
     591             : //===----------------------------------------------------------------------===//
     592      124382 : // PMTopLevelManager implementation
     593             : 
     594             : /// Initialize top level manager. Create first pass manager.
     595             : PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
     596             :   PMDM->setTopLevelManager(this);
     597           0 :   addPassManager(PMDM);
     598             :   activeStack.push(PMDM);
     599             : }
     600             : 
     601             : /// Set pass P as the last user of the given analysis passes.
     602             : void
     603             : PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
     604             :   unsigned PDepth = 0;
     605             :   if (P->getResolver())
     606             :     PDepth = P->getResolver()->getPMDataManager().getDepth();
     607      256362 : 
     608             :   for (Pass *AP : AnalysisPasses) {
     609       85454 :     LastUser[AP] = P;
     610       85454 : 
     611       85454 :     if (P == AP)
     612             :       continue;
     613             : 
     614             :     // Update the last users of passes that are required transitive by AP.
     615    28246499 :     AnalysisUsage *AnUsage = findAnalysisUsage(AP);
     616             :     const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
     617    28246499 :     SmallVector<Pass *, 12> LastUses;
     618    23806902 :     SmallVector<Pass *, 12> LastPMUses;
     619             :     for (AnalysisID ID : IDs) {
     620    42715109 :       Pass *AnalysisPass = findAnalysisPass(ID);
     621    14468638 :       assert(AnalysisPass && "Expected analysis pass to exist.");
     622             :       AnalysisResolver *AR = AnalysisPass->getResolver();
     623    14468509 :       assert(AR && "Expected analysis resolver to exist.");
     624     4108514 :       unsigned APDepth = AR->getPMDataManager().getDepth();
     625             : 
     626             :       if (PDepth == APDepth)
     627    10359995 :         LastUses.push_back(AnalysisPass);
     628             :       else if (PDepth > APDepth)
     629             :         LastPMUses.push_back(AnalysisPass);
     630             :     }
     631    11682589 : 
     632     1322577 :     setLastUser(LastUses, P);
     633             : 
     634     1322571 :     // If this pass has a corresponding pass manager, push higher level
     635             :     // analysis to this pass manager.
     636     1322571 :     if (P->getResolver())
     637             :       setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
     638     1322571 : 
     639     1028586 : 
     640      293985 :     // If AP is the last user of other passes then make P last user of
     641      293985 :     // such passes.
     642             :     for (auto LU : LastUser) {
     643             :       if (LU.second == AP)
     644    10360012 :         // DenseMap iterator is not invalidated here because
     645             :         // this is just updating existing entries.
     646             :         LastUser[LU.first] = P;
     647             :     }
     648    10360034 :   }
     649    10325349 : }
     650             : 
     651             : /// Collect passes whose last user is P
     652             : void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
     653             :                                         Pass *P) {
     654   915046470 :   DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
     655   904686367 :     InversedLastUser.find(P);
     656             :   if (DMI == InversedLastUser.end())
     657             :     return;
     658      884413 : 
     659             :   SmallPtrSet<Pass *, 8> &LU = DMI->second;
     660             :   for (Pass *LUP : LU) {
     661    28246471 :     LastUses.push_back(LUP);
     662             :   }
     663             : 
     664    50747198 : }
     665             : 
     666             : AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
     667    50747198 :   AnalysisUsage *AnUsage = nullptr;
     668    50747225 :   auto DMI = AnUsageMap.find(P);
     669    19438908 :   if (DMI != AnUsageMap.end())
     670             :     AnUsage = DMI->second;
     671             :   else {
     672    81977454 :     // Look up the analysis usage from the pass instance (different instances
     673    50669133 :     // of the same pass can produce different results), but unique the
     674             :     // resulting object to reduce memory usage.  This helps to greatly reduce
     675             :     // memory usage when we have many instances of only a few pass types
     676             :     // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
     677             :     // of dependencies.
     678   125387069 :     AnalysisUsage AU;
     679             :     P->getAnalysisUsage(AU);
     680   125387069 : 
     681   125387298 :     AUFoldingSetNode* Node = nullptr;
     682   120707374 :     FoldingSetNodeID ID;
     683             :     AUFoldingSetNode::Profile(ID, AU);
     684             :     void *IP = nullptr;
     685             :     if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
     686             :       Node = N;
     687             :     else {
     688             :       Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
     689             :       UniqueAnalysisUsages.InsertNode(Node, IP);
     690     4679927 :     }
     691     4679924 :     assert(Node && "cached analysis usage must be non null");
     692             : 
     693             :     AnUsageMap[P] = &Node->AU;
     694             :     AnUsage = &Node->AU;
     695     4679923 :   }
     696     4679931 :   return AnUsage;
     697     4679924 : }
     698             : 
     699             : /// Schedule pass P for execution. Make sure that passes required by
     700             : /// P are run before P is run. Update analysis info maintained by
     701     2044782 : /// the manager. Remove dead passes. This is a recursive function.
     702             : void PMTopLevelManager::schedulePass(Pass *P) {
     703             : 
     704             :   // TODO : Allocate function manager for this pass, other wise required set
     705     4679927 :   // may be inserted into previous function manager
     706             : 
     707             :   // Give pass a chance to prepare the stage.
     708   125387296 :   P->preparePassManager(activeStack);
     709             : 
     710             :   // If P is an analysis pass and it is available then do not
     711             :   // generate the analysis again. Stale analysis info should not be
     712             :   // available at this point.
     713             :   const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
     714     4604563 :   if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
     715             :     // Remove any cached AnalysisUsage information.
     716             :     AnUsageMap.erase(P);
     717             :     delete P;
     718             :     return;
     719             :   }
     720     4604563 : 
     721             :   AnalysisUsage *AnUsage = findAnalysisUsage(P);
     722             : 
     723             :   bool checkAnalysis = true;
     724             :   while (checkAnalysis) {
     725     4604568 :     checkAnalysis = false;
     726     4604561 : 
     727             :     const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
     728       12979 :     for (const AnalysisID ID : RequiredSet) {
     729       12979 : 
     730       12979 :       Pass *AnalysisPass = findAnalysisPass(ID);
     731             :       if (!AnalysisPass) {
     732             :         const PassInfo *PI = findAnalysisPassInfo(ID);
     733     4591584 : 
     734             :         if (!PI) {
     735             :           // Pass P is not in the global PassRegistry
     736     9362147 :           dbgs() << "Pass '"  << P->getPassName() << "' is not initialized." << "\n";
     737             :           dbgs() << "Verify if there is a pass dependency cycle." << "\n";
     738             :           dbgs() << "Required Passes:" << "\n";
     739             :           for (const AnalysisID ID2 : RequiredSet) {
     740    13837477 :             if (ID == ID2)
     741             :               break;
     742     9066915 :             Pass *AnalysisPass2 = findAnalysisPass(ID2);
     743     9066924 :             if (AnalysisPass2) {
     744     1755536 :               dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
     745             :             } else {
     746     1755534 :               dbgs() << "\t"   << "Error: Required pass not found! Possible causes:"  << "\n";
     747             :               dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)"    << "\n";
     748           0 :               dbgs() << "\t\t" << "- Corruption of the global PassRegistry"           << "\n";
     749           0 :             }
     750           0 :           }
     751           0 :         }
     752           0 : 
     753             :         assert(PI && "Expected required passes to be initialized");
     754           0 :         AnalysisPass = PI->createPass();
     755           0 :         if (P->getPotentialPassManagerType () ==
     756           0 :             AnalysisPass->getPotentialPassManagerType())
     757             :           // Schedule analysis pass that is managed by the same pass manager.
     758           0 :           schedulePass(AnalysisPass);
     759           0 :         else if (P->getPotentialPassManagerType () >
     760           0 :                  AnalysisPass->getPotentialPassManagerType()) {
     761             :           // Schedule analysis pass that is managed by a new manager.
     762             :           schedulePass(AnalysisPass);
     763             :           // Recheck analysis passes to ensure that required analyses that
     764             :           // are already checked are still available.
     765             :           checkAnalysis = true;
     766     1755534 :         } else
     767     3511066 :           // Do not schedule this analysis. Lower level analysis
     768     1755530 :           // passes are run on the fly.
     769             :           delete AnalysisPass;
     770     1434617 :       }
     771      320918 :     }
     772      320918 :   }
     773             : 
     774      309156 :   // Now all required passes are available.
     775             :   if (ImmutablePass *IP = P->getAsImmutablePass()) {
     776             :     // P is a immutable pass and it will be managed by this
     777             :     // top level manager. Set up analysis resolver to connect them.
     778             :     PMDataManager *DM = getAsPMDataManager();
     779             :     AnalysisResolver *AR = new AnalysisResolver(*DM);
     780             :     P->setResolver(AR);
     781       11762 :     DM->initializeAnalysisImpl(P);
     782             :     addImmutablePass(IP);
     783             :     DM->recordAvailableAnalysis(IP);
     784             :     return;
     785             :   }
     786             : 
     787     4591593 :   if (PI && !PI->isAnalysis() && shouldPrintBeforePass(PI->getPassArgument())) {
     788             :     Pass *PP = P->createPrinterPass(
     789             :         dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
     790      420725 :     PP->assignPassManager(activeStack, getTopLevelPassManagerType());
     791      420725 :   }
     792      420724 : 
     793      420725 :   // Add the requested pass to the best available pass manager.
     794      420725 :   P->assignPassManager(activeStack, getTopLevelPassManagerType());
     795      420725 : 
     796      420725 :   if (PI && !PI->isAnalysis() && shouldPrintAfterPass(PI->getPassArgument())) {
     797             :     Pass *PP = P->createPrinterPass(
     798             :         dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
     799     4170868 :     PP->assignPassManager(activeStack, getTopLevelPassManagerType());
     800           6 :   }
     801          18 : }
     802           6 : 
     803             : /// Find the pass that implements Analysis AID. Search immutable
     804             : /// passes and all pass managers. If desired pass is not found
     805             : /// then return NULL.
     806     4170868 : Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
     807             :   // For immutable passes we have a direct mapping from ID to pass, so check
     808     4170863 :   // that first.
     809         475 :   if (Pass *P = ImmutablePassMap.lookup(AID))
     810        1425 :     return P;
     811         475 : 
     812             :   // Check pass managers
     813             :   for (PMDataManager *PassManager : PassManagers)
     814             :     if (Pass *P = PassManager->findAnalysisPass(AID, false))
     815             :       return P;
     816             : 
     817             :   // Check other pass managers
     818   110997393 :   for (PMDataManager *IndirectPassManager : IndirectPassManagers)
     819             :     if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
     820             :       return P;
     821   221995079 : 
     822             :   return nullptr;
     823             : }
     824             : 
     825    76440506 : const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
     826    39148743 :   const PassInfo *&PI = AnalysisPassInfos[AID];
     827     1857016 :   if (!PI)
     828             :     PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
     829             :   else
     830   418298589 :     assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
     831   385776298 :            "The pass info pointer changed for an analysis ID!");
     832     4769548 : 
     833             :   return PI;
     834             : }
     835             : 
     836             : void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
     837   112907482 :   P->initializePass();
     838   112907482 :   ImmutablePasses.push_back(P);
     839   112907352 : 
     840    11973196 :   // Add this pass to the map from its analysis ID. We clobber any prior runs
     841             :   // of the pass in the map so that the last one added is the one found when
     842             :   // doing lookups.
     843             :   AnalysisID AID = P->getPassID();
     844             :   ImmutablePassMap[AID] = P;
     845   112907354 : 
     846             :   // Also add any interfaces implemented by the immutable pass to the map for
     847             :   // fast lookup.
     848      420725 :   const PassInfo *PassInf = findAnalysisPassInfo(AID);
     849      420725 :   assert(PassInf && "Expected all immutable passes to be initialized");
     850      420725 :   for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
     851             :     ImmutablePassMap[ImmPI->getTypeInfo()] = P;
     852             : }
     853             : 
     854             : // Print passes managed by this top level manager.
     855      420725 : void PMTopLevelManager::dumpPasses() const {
     856      420725 : 
     857             :   if (PassDebugging < Structure)
     858             :     return;
     859             : 
     860      420725 :   // Print out the immutable passes
     861             :   for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
     862      420725 :     ImmutablePasses[i]->dumpPassStructure(0);
     863           0 :   }
     864      420725 : 
     865             :   // Every class that derives from PMDataManager also derives from Pass
     866             :   // (sometimes indirectly), but there's no inheritance relationship
     867       85196 :   // between PMDataManager and Pass, so we have to getAsPass to get
     868             :   // from a PMDataManager* to a Pass*.
     869       85196 :   for (PMDataManager *Manager : PassManagers)
     870             :     Manager->getAsPass()->dumpPassStructure(1);
     871             : }
     872             : 
     873         378 : void PMTopLevelManager::dumpArguments() const {
     874         604 : 
     875             :   if (PassDebugging < Arguments)
     876             :     return;
     877             : 
     878             :   dbgs() << "Pass Arguments: ";
     879             :   for (ImmutablePass *P : ImmutablePasses)
     880             :     if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
     881         152 :       assert(PI && "Expected all immutable passes to be initialized");
     882          76 :       if (!PI->isAnalysisGroup())
     883             :         dbgs() << " -" << PI->getPassArgument();
     884             :     }
     885       85196 :   for (PMDataManager *PM : PassManagers)
     886             :     PM->dumpPassArguments();
     887       85196 :   dbgs() << "\n";
     888             : }
     889             : 
     890         179 : void PMTopLevelManager::initializeAllAnalysisInfo() {
     891         774 :   for (PMDataManager *PM : PassManagers)
     892         595 :     PM->initializeAnalysisInfo();
     893             : 
     894         595 :   // Initailize other pass managers
     895         595 :   for (PMDataManager *IPM : IndirectPassManagers)
     896             :     IPM->initializeAnalysisInfo();
     897         358 : 
     898         179 :   for (auto LU : LastUser) {
     899         179 :     SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
     900             :     L.insert(LU.first);
     901             :   }
     902      945500 : }
     903     1891000 : 
     904             : /// Destructor
     905             : PMTopLevelManager::~PMTopLevelManager() {
     906             :   for (PMDataManager *PM : PassManagers)
     907     1246376 :     delete PM;
     908             : 
     909             :   for (ImmutablePass *P : ImmutablePasses)
     910     6702699 :     delete P;
     911     5757199 : }
     912     5757200 : 
     913             : //===----------------------------------------------------------------------===//
     914      945500 : // PMDataManager implementation
     915             : 
     916             : /// Augement AvailableAnalysis by adding analysis made available by pass P.
     917      255431 : void PMDataManager::recordAvailableAnalysis(Pass *P) {
     918      170288 :   AnalysisID PI = P->getPassID();
     919       85144 : 
     920             :   AvailableAnalysis[PI] = P;
     921      503950 : 
     922      418806 :   assert(!AvailableAnalysis.empty());
     923       85144 : 
     924           0 :   // This pass is the current implementation of all of the interfaces it
     925             :   // implements as well.
     926             :   const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
     927             :   if (!PInf) return;
     928             :   const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
     929             :   for (unsigned i = 0, e = II.size(); i != e; ++i)
     930           0 :     AvailableAnalysis[II[i]->getTypeInfo()] = P;
     931      255431 : }
     932      170288 : 
     933       85144 : // Return true if P preserves high level analysis used by other
     934             : // passes managed by this manager
     935      503950 : bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
     936      418806 :   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
     937       85144 :   if (AnUsage->getPreservesAll())
     938             :     return true;
     939             : 
     940             :   const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
     941             :   for (Pass *P1 : HigherLevelAnalysis) {
     942             :     if (P1->getAsImmutablePass() == nullptr &&
     943    55422702 :         !is_contained(PreservedSet, P1->getPassID()))
     944    55422702 :       return false;
     945             :   }
     946    55422702 : 
     947             :   return true;
     948             : }
     949             : 
     950             : /// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
     951             : void PMDataManager::verifyPreservedAnalysis(Pass *P) {
     952    55422727 :   // Don't do this unless assertions are enabled.
     953    55422716 : #ifdef NDEBUG
     954             :   return;
     955   101195070 : #endif
     956           0 :   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
     957             :   const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
     958             : 
     959             :   // Verify preserved analysis
     960             :   for (AnalysisID AID : PreservedSet) {
     961       11051 :     if (Pass *AP = findAnalysisPass(AID, true)) {
     962       11051 :       TimeRegion PassTimer(getPassTimer(AP));
     963       11051 :       AP->verifyAnalysis();
     964             :     }
     965             :   }
     966             : }
     967      115107 : 
     968      107128 : /// Remove Analysis not preserved by Pass P
     969      107128 : void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
     970          30 :   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
     971             :   if (AnUsage->getPreservesAll())
     972             :     return;
     973             : 
     974             :   const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
     975             :   for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
     976             :          E = AvailableAnalysis.end(); I != E; ) {
     977    50742155 :     DenseMap<AnalysisID, Pass*>::iterator Info = I++;
     978             :     if (Info->second->getAsImmutablePass() == nullptr &&
     979             :         !is_contained(PreservedSet, Info->first)) {
     980             :       // Remove this analysis
     981             :       if (PassDebugging >= Details) {
     982             :         Pass *S = Info->second;
     983             :         dbgs() << " -- '" <<  P->getPassName() << "' is not preserving '";
     984             :         dbgs() << S->getPassName() << "'\n";
     985             :       }
     986             :       AvailableAnalysis.erase(Info);
     987             :     }
     988             :   }
     989             : 
     990             :   // Check inherited analysis also. If P is not preserving analysis
     991             :   // provided by parent manager then remove it here.
     992             :   for (unsigned Index = 0; Index < PMT_Last; ++Index) {
     993             : 
     994             :     if (!InheritedAnalysis[Index])
     995    55001998 :       continue;
     996    55001998 : 
     997    55001990 :     for (DenseMap<AnalysisID, Pass*>::iterator
     998             :            I = InheritedAnalysis[Index]->begin(),
     999             :            E = InheritedAnalysis[Index]->end(); I != E; ) {
    1000             :       DenseMap<AnalysisID, Pass *>::iterator Info = I++;
    1001    83577605 :       if (Info->second->getAsImmutablePass() == nullptr &&
    1002    83577602 :           !is_contained(PreservedSet, Info->first)) {
    1003             :         // Remove this analysis
    1004    54032048 :         if (PassDebugging >= Details) {
    1005    54032059 :           Pass *S = Info->second;
    1006             :           dbgs() << " -- '" <<  P->getPassName() << "' is not preserving '";
    1007    16152263 :           dbgs() << S->getPassName() << "'\n";
    1008           2 :         }
    1009           2 :         InheritedAnalysis[Index]->erase(Info);
    1010           2 :       }
    1011             :     }
    1012             :   }
    1013             : }
    1014             : 
    1015             : /// Remove analysis passes that are not used any longer
    1016             : void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
    1017             :                                      enum PassDebuggingString DBG_STR) {
    1018   236364356 : 
    1019             :   SmallVector<Pass *, 12> DeadPasses;
    1020   206818802 : 
    1021             :   // If this is a on the fly manager then it does not have TPM.
    1022             :   if (!TPM)
    1023    25258240 :     return;
    1024    55155767 : 
    1025    80414004 :   TPM->collectLastUses(DeadPasses, P);
    1026             : 
    1027    25258237 :   if (PassDebugging >= Details && !DeadPasses.empty()) {
    1028    25258236 :     dbgs() << " -*- '" <<  P->getPassName();
    1029             :     dbgs() << "' is the last user of following pass instances.";
    1030      341250 :     dbgs() << " Free these instances\n";
    1031           1 :   }
    1032           1 : 
    1033           1 :   for (Pass *P : DeadPasses)
    1034             :     freePass(P, Msg, DBG_STR);
    1035      341250 : }
    1036             : 
    1037             : void PMDataManager::freePass(Pass *P, StringRef Msg,
    1038             :                              enum PassDebuggingString DBG_STR) {
    1039             :   dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
    1040             : 
    1041             :   {
    1042    50742817 :     // If the pass crashes releasing memory, remember this.
    1043             :     PassManagerPrettyStackEntry X(P);
    1044             :     TimeRegion PassTimer(getPassTimer(P));
    1045             : 
    1046             :     P->releaseMemory();
    1047             :   }
    1048    50742817 : 
    1049             :   AnalysisID PI = P->getPassID();
    1050             :   if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
    1051    50742817 :     // Remove the pass itself (if it is not already removed).
    1052             :     AvailableAnalysis.erase(PI);
    1053    50742819 : 
    1054           3 :     // Remove all interfaces this pass implements, for which it is also
    1055           3 :     // listed as the available implementation.
    1056           3 :     const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
    1057             :     for (unsigned i = 0, e = II.size(); i != e; ++i) {
    1058             :       DenseMap<AnalysisID, Pass*>::iterator Pos =
    1059   101411934 :         AvailableAnalysis.find(II[i]->getTypeInfo());
    1060    50669109 :       if (Pos != AvailableAnalysis.end() && Pos->second == P)
    1061             :         AvailableAnalysis.erase(Pos);
    1062             :     }
    1063    50671184 :   }
    1064             : }
    1065    50671184 : 
    1066             : /// Add pass P into the PassVector. Update
    1067             : /// AvailableAnalysis appropriately if ProcessAnalysis is true.
    1068             : void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
    1069             :   // This manager is going to manage pass P. Set up analysis resolver
    1070    50671194 :   // to connect them.
    1071             :   AnalysisResolver *AR = new AnalysisResolver(*this);
    1072    50671198 :   P->setResolver(AR);
    1073             : 
    1074             :   // If a FunctionPass F is the last user of ModulePass info M
    1075    50671197 :   // then the F's manager, not F, records itself as a last user of M.
    1076    50671197 :   SmallVector<Pass *, 12> TransferLastUses;
    1077             : 
    1078    46899617 :   if (!ProcessAnalysis) {
    1079             :     // Add pass
    1080             :     PassVector.push_back(P);
    1081             :     return;
    1082             :   }
    1083    93799232 : 
    1084             :   // At the moment, this pass is the last user of all required passes.
    1085           0 :   SmallVector<Pass *, 12> LastUses;
    1086           0 :   SmallVector<Pass *, 8> UsedPasses;
    1087             :   SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
    1088             : 
    1089             :   unsigned PDepth = this->getDepth();
    1090    50671198 : 
    1091             :   collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
    1092             :   for (Pass *PUsed : UsedPasses) {
    1093             :     unsigned RDepth = 0;
    1094     4259203 : 
    1095             :     assert(PUsed->getResolver() && "Analysis Resolver is not set");
    1096             :     PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
    1097     4259203 :     RDepth = DM.getDepth();
    1098     4259204 : 
    1099             :     if (PDepth == RDepth)
    1100             :       LastUses.push_back(PUsed);
    1101             :     else if (PDepth > RDepth) {
    1102             :       // Let the parent claim responsibility of last use
    1103             :       TransferLastUses.push_back(PUsed);
    1104     4259204 :       // Keep track of higher level analysis used by this manager.
    1105             :       HigherLevelAnalysis.push_back(PUsed);
    1106           0 :     } else
    1107             :       llvm_unreachable("Unable to accommodate Used Pass");
    1108             :   }
    1109             : 
    1110             :   // Set P as P's last user until someone starts using P.
    1111             :   // However, if P is a Pass Manager then it does not need
    1112             :   // to record its last user.
    1113             :   if (!P->getAsPMDataManager())
    1114             :     LastUses.push_back(P);
    1115     4259204 :   TPM->setLastUser(LastUses, P);
    1116             : 
    1117     4259204 :   if (!TransferLastUses.empty()) {
    1118    13284973 :     Pass *My_PM = getAsPass();
    1119             :     TPM->setLastUser(TransferLastUses, My_PM);
    1120             :     TransferLastUses.clear();
    1121             :   }
    1122     9025771 : 
    1123     9025771 :   // Now, take care of required analyses that are not available.
    1124             :   for (AnalysisID ID : ReqAnalysisNotAvailable) {
    1125     9025771 :     const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
    1126     3939510 :     Pass *AnalysisPass = PI->createPass();
    1127     5086261 :     this->addLowerLevelRequiredPass(P, AnalysisPass);
    1128             :   }
    1129     5086261 : 
    1130             :   // Take a note of analysis required and made available by this pass.
    1131     5086261 :   // Remove the analysis not preserved by this pass
    1132             :   removeNotPreservedAnalysis(P);
    1133           0 :   recordAvailableAnalysis(P);
    1134             : 
    1135             :   // Add pass
    1136             :   PassVector.push_back(P);
    1137             : }
    1138             : 
    1139     4259202 : 
    1140     4108518 : /// Populate UP with analysis pass that are used or required by
    1141     8518410 : /// pass P and are available. Populate RP_NotAvail with analysis
    1142             : /// pass that are required by pass P but are not available.
    1143     4259201 : void PMDataManager::collectRequiredAndUsedAnalyses(
    1144     3290402 :     SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
    1145     6580804 :     Pass *P) {
    1146             :   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
    1147             : 
    1148             :   for (const auto &UsedID : AnUsage->getUsedSet())
    1149             :     if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
    1150     4270970 :       UP.push_back(AnalysisPass);
    1151       11769 : 
    1152       11769 :   for (const auto &RequiredID : AnUsage->getRequiredSet())
    1153       11769 :     if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
    1154             :       UP.push_back(AnalysisPass);
    1155             :     else
    1156             :       RP_NotAvail.push_back(RequiredID);
    1157             : 
    1158     4259201 :   for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
    1159     4259195 :     if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
    1160             :       UP.push_back(AnalysisPass);
    1161             :     else
    1162     4259190 :       RP_NotAvail.push_back(RequiredID);
    1163             : }
    1164             : 
    1165             : // All Required analyses should be available to the pass as it runs!  Here
    1166             : // we fill in the AnalysisImpls member of the pass so that it can
    1167             : // successfully use the getAnalysis() method to retrieve the
    1168             : // implementations it needs.
    1169     4259206 : //
    1170             : void PMDataManager::initializeAnalysisImpl(Pass *P) {
    1171             :   AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
    1172     4259206 : 
    1173             :   for (const AnalysisID ID : AnUsage->getRequiredSet()) {
    1174     5208030 :     Pass *Impl = findAnalysisPass(ID, true);
    1175      948824 :     if (!Impl)
    1176      259364 :       // This may be analysis pass that is initialized on the fly.
    1177             :       // If that is not the case then it will raise an assert when it is used.
    1178    12614382 :       continue;
    1179     8355178 :     AnalysisResolver *AR = P->getResolver();
    1180     8343410 :     assert(AR && "Analysis Resolver is not set");
    1181             :     AR->addAnalysisImplsPair(ID, Impl);
    1182       11769 :   }
    1183             : }
    1184     4682178 : 
    1185      422975 : /// Find the pass that implements Analysis AID. If desired pass is not found
    1186      422975 : /// then return NULL.
    1187             : Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
    1188           0 : 
    1189     4259203 :   // Check if AvailableAnalysis map has one entry.
    1190             :   DenseMap<AnalysisID, Pass*>::const_iterator I =  AvailableAnalysis.find(AID);
    1191             : 
    1192             :   if (I != AvailableAnalysis.end())
    1193             :     return I->second;
    1194             : 
    1195             :   // Search Parents through TopLevelManager
    1196    51163988 :   if (SearchParent)
    1197    51163988 :     return TPM->findAnalysisPass(AID);
    1198             : 
    1199   150847354 :   return nullptr;
    1200    99683356 : }
    1201    99683364 : 
    1202             : // Print list of passes that are last used by P.
    1203             : void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
    1204             : 
    1205    99492836 :   SmallVector<Pass *, 12> LUses;
    1206             : 
    1207    99492836 :   // If this is a on the fly manager then it does not have TPM.
    1208             :   if (!TPM)
    1209    51163998 :     return;
    1210             : 
    1211             :   TPM->collectLastUses(LUses, P);
    1212             : 
    1213   571483523 :   for (Pass *P : LUses) {
    1214             :     dbgs() << "--" << std::string(Offset*2, ' ');
    1215             :     P->dumpPassStructure(0);
    1216   571483523 :   }
    1217             : }
    1218   571486731 : 
    1219    54426958 : void PMDataManager::dumpPassArguments() const {
    1220             :   for (Pass *P : PassVector) {
    1221             :     if (PMDataManager *PMD = P->getAsPMDataManager())
    1222   517059773 :       PMD->dumpPassArguments();
    1223    98761880 :     else
    1224             :       if (const PassInfo *PI =
    1225             :             TPM->findAnalysisPassInfo(P->getPassID()))
    1226             :         if (!PI->isAnalysisGroup())
    1227             :           dbgs() << " -" << PI->getPassArgument();
    1228             :   }
    1229        4381 : }
    1230             : 
    1231             : void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
    1232             :                                  enum PassDebuggingString S2,
    1233             :                                  StringRef Msg) {
    1234        4381 :   if (PassDebugging < Executions)
    1235             :     return;
    1236             :   dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
    1237        4381 :          << std::string(getDepth() * 2 + 1, ' ');
    1238             :   switch (S1) {
    1239        4381 :   case EXECUTION_MSG:
    1240           0 :     dbgs() << "Executing Pass '" << P->getPassName();
    1241           0 :     break;
    1242             :   case MODIFICATION_MSG:
    1243             :     dbgs() << "Made Modification '" << P->getPassName();
    1244             :     break;
    1245         628 :   case FREEING_MSG:
    1246        9863 :     dbgs() << " Freeing Pass '" << P->getPassName();
    1247        9235 :     break;
    1248         449 :   default:
    1249             :     break;
    1250        8786 :   }
    1251        8786 :   switch (S2) {
    1252        8560 :   case ON_BASICBLOCK_MSG:
    1253        8560 :     dbgs() << "' on BasicBlock '" << Msg << "'...\n";
    1254             :     break;
    1255         628 :   case ON_FUNCTION_MSG:
    1256             :     dbgs() << "' on Function '" << Msg << "'...\n";
    1257   105677887 :     break;
    1258             :   case ON_MODULE_MSG:
    1259             :     dbgs() << "' on Module '"  << Msg << "'...\n";
    1260   105677887 :     break;
    1261             :   case ON_REGION_MSG:
    1262         154 :     dbgs() << "' on Region '"  << Msg << "'...\n";
    1263         154 :     break;
    1264         154 :   case ON_LOOP_MSG:
    1265          71 :     dbgs() << "' on Loop '" << Msg << "'...\n";
    1266          71 :     break;
    1267          71 :   case ON_CG_MSG:
    1268          11 :     dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
    1269          11 :     break;
    1270          11 :   default:
    1271          72 :     break;
    1272          72 :   }
    1273          72 : }
    1274             : 
    1275             : void PMDataManager::dumpRequiredSet(const Pass *P) const {
    1276             :   if (PassDebugging < Details)
    1277         154 :     return;
    1278           0 : 
    1279           0 :   AnalysisUsage analysisUsage;
    1280           0 :   P->getAnalysisUsage(analysisUsage);
    1281         126 :   dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
    1282         126 : }
    1283         126 : 
    1284          20 : void PMDataManager::dumpPreservedSet(const Pass *P) const {
    1285          20 :   if (PassDebugging < Details)
    1286          20 :     return;
    1287           0 : 
    1288           0 :   AnalysisUsage analysisUsage;
    1289           0 :   P->getAnalysisUsage(analysisUsage);
    1290           0 :   dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
    1291           0 : }
    1292           0 : 
    1293           8 : void PMDataManager::dumpUsedSet(const Pass *P) const {
    1294           8 :   if (PassDebugging < Details)
    1295           8 :     return;
    1296             : 
    1297             :   AnalysisUsage analysisUsage;
    1298             :   P->getAnalysisUsage(analysisUsage);
    1299             :   dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
    1300             : }
    1301    50691982 : 
    1302             : void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
    1303    50691972 :                                    const AnalysisUsage::VectorType &Set) const {
    1304             :   assert(PassDebugging >= Details);
    1305          10 :   if (Set.empty())
    1306          10 :     return;
    1307          10 :   dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
    1308             :   for (unsigned i = 0; i != Set.size(); ++i) {
    1309             :     if (i) dbgs() << ',';
    1310    50691587 :     const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
    1311    50691587 :     if (!PInf) {
    1312    50691577 :       // Some preserved passes, such as AliasAnalysis, may not be initialized by
    1313             :       // all drivers.
    1314          10 :       dbgs() << " Uninitialized Pass";
    1315          10 :       continue;
    1316          10 :     }
    1317             :     dbgs() << ' ' << PInf->getPassName();
    1318             :   }
    1319    49340010 :   dbgs() << '\n';
    1320    49340010 : }
    1321    49340000 : 
    1322             : /// Add RequiredPass into list of lower level passes required by pass P.
    1323          10 : /// RequiredPass is run on the fly by Pass Manager when P requests it
    1324          10 : /// through getAnalysis interface.
    1325          10 : /// This should be handled by specific pass manager.
    1326             : void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
    1327             :   if (TPM) {
    1328          30 :     TPM->dumpArguments();
    1329             :     TPM->dumpPasses();
    1330             :   }
    1331          30 : 
    1332             :   // Module Level pass may required Function Level analysis info
    1333          30 :   // (e.g. dominator info). Pass manager uses on the fly function pass manager
    1334          71 :   // to provide this on demand. In that case, in Pass manager terminology,
    1335          61 :   // module level pass is requiring lower level analysis info managed by
    1336         122 :   // lower level pass manager.
    1337          61 : 
    1338             :   // When Pass manager is not able to order required analysis info, Pass manager
    1339             :   // checks whether any lower level manager will be able to provide this
    1340           0 :   // analysis info on demand or not.
    1341           0 : #ifndef NDEBUG
    1342             :   dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
    1343         122 :   dbgs() << "' required by '" << P->getPassName() << "'\n";
    1344             : #endif
    1345          10 :   llvm_unreachable("Unable to schedule pass");
    1346             : }
    1347             : 
    1348             : Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
    1349             :   llvm_unreachable("Unable to find on the fly pass");
    1350             : }
    1351             : 
    1352           0 : // Destructor
    1353           0 : PMDataManager::~PMDataManager() {
    1354           0 :   for (Pass *P : PassVector)
    1355           0 :     delete P;
    1356             : }
    1357             : 
    1358             : //===----------------------------------------------------------------------===//
    1359             : // NOTE: Is this the right place to define this method ?
    1360             : // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
    1361             : Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
    1362             :   return PM.findAnalysisPass(ID, dir);
    1363             : }
    1364             : 
    1365             : Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
    1366             :                                      Function &F) {
    1367             :   return PM.getOnTheFlyPass(P, AnalysisPI, F);
    1368             : }
    1369             : 
    1370             : //===----------------------------------------------------------------------===//
    1371           0 : // BBPassManager implementation
    1372             : 
    1373             : /// Execute all of the passes scheduled for execution by invoking
    1374           0 : /// runOnBasicBlock method.  Keep track of whether any of the passes modifies
    1375           0 : /// the function, and if so, return true.
    1376             : bool BBPassManager::runOnFunction(Function &F) {
    1377             :   if (F.isDeclaration())
    1378             :     return false;
    1379      320139 : 
    1380     4554551 :   bool Changed = doInitialization(F);
    1381     4234412 :   Module &M = *F.getParent();
    1382      320138 : 
    1383           0 :   unsigned InstrCount, BBSize = 0;
    1384             :   StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
    1385             :   bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
    1386           0 :   if (EmitICRemark)
    1387      320139 :     InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
    1388     4554551 : 
    1389     4234412 :   for (BasicBlock &BB : F) {
    1390      320138 :     // Collect the initial size of the basic block.
    1391             :     if (EmitICRemark)
    1392             :       BBSize = BB.size();
    1393             :     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1394             :       BasicBlockPass *BP = getContainedPass(Index);
    1395    37152017 :       bool LocalChanged = false;
    1396    37152017 : 
    1397             :       dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
    1398             :       dumpRequiredSet(BP);
    1399       49879 : 
    1400             :       initializeAnalysisImpl(BP);
    1401       49879 : 
    1402             :       {
    1403             :         // If the pass crashes, remember this.
    1404             :         PassManagerPrettyStackEntry X(BP, BB);
    1405             :         TimeRegion PassTimer(getPassTimer(BP));
    1406             :         LocalChanged |= BP->runOnBasicBlock(BB);
    1407             :         if (EmitICRemark) {
    1408             :           unsigned NewSize = BB.size();
    1409             :           // Update the size of the basic block, emit a remark, and update the
    1410        1173 :           // size of the module.
    1411        1173 :           if (NewSize != BBSize) {
    1412             :             int64_t Delta =
    1413             :                 static_cast<int64_t>(NewSize) - static_cast<int64_t>(BBSize);
    1414             :             emitInstrCountChangedRemark(BP, M, Delta, InstrCount,
    1415        1173 :                                         FunctionToInstrCount, &F);
    1416             :             InstrCount = static_cast<int64_t>(InstrCount) + Delta;
    1417             :             BBSize = NewSize;
    1418        1173 :           }
    1419        1173 :         }
    1420        1173 :       }
    1421           0 : 
    1422             :       Changed |= LocalChanged;
    1423        2526 :       if (LocalChanged)
    1424             :         dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
    1425        1353 :                      BB.getName());
    1426           0 :       dumpPreservedSet(BP);
    1427        2706 :       dumpUsedSet(BP);
    1428             : 
    1429             :       verifyPreservedAnalysis(BP);
    1430             :       removeNotPreservedAnalysis(BP);
    1431        1353 :       recordAvailableAnalysis(BP);
    1432        1353 :       removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
    1433             :     }
    1434        1353 :   }
    1435             : 
    1436             :   return doFinalization(F) || Changed;
    1437             : }
    1438             : 
    1439        1353 : // Implement doInitialization and doFinalization
    1440        1353 : bool BBPassManager::doInitialization(Module &M) {
    1441        1353 :   bool Changed = false;
    1442           0 : 
    1443             :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
    1444             :     Changed |= getContainedPass(Index)->doInitialization(M);
    1445           0 : 
    1446           0 :   return Changed;
    1447           0 : }
    1448           0 : 
    1449             : bool BBPassManager::doFinalization(Module &M) {
    1450           0 :   bool Changed = false;
    1451             : 
    1452             :   for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
    1453             :     Changed |= getContainedPass(Index)->doFinalization(M);
    1454             : 
    1455             :   return Changed;
    1456        1353 : }
    1457        1353 : 
    1458          69 : bool BBPassManager::doInitialization(Function &F) {
    1459             :   bool Changed = false;
    1460        1353 : 
    1461        1353 :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1462             :     BasicBlockPass *BP = getContainedPass(Index);
    1463        1353 :     Changed |= BP->doInitialization(F);
    1464        1353 :   }
    1465        1353 : 
    1466        1353 :   return Changed;
    1467             : }
    1468             : 
    1469             : bool BBPassManager::doFinalization(Function &F) {
    1470        1173 :   bool Changed = false;
    1471             : 
    1472             :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1473             :     BasicBlockPass *BP = getContainedPass(Index);
    1474         250 :     Changed |= BP->doFinalization(F);
    1475             :   }
    1476             : 
    1477         500 :   return Changed;
    1478         250 : }
    1479             : 
    1480         250 : 
    1481             : //===----------------------------------------------------------------------===//
    1482             : // FunctionPassManager implementation
    1483         243 : 
    1484             : /// Create new Function pass manager
    1485             : FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
    1486         486 :   FPM = new FunctionPassManagerImpl();
    1487         486 :   // FPM is the top level manager.
    1488             :   FPM->setTopLevelManager(FPM);
    1489         243 : 
    1490             :   AnalysisResolver *AR = new AnalysisResolver(*FPM);
    1491             :   FPM->setResolver(AR);
    1492             : }
    1493             : 
    1494             : FunctionPassManager::~FunctionPassManager() {
    1495        2346 :   delete FPM;
    1496             : }
    1497        1173 : 
    1498             : void FunctionPassManager::add(Pass *P) {
    1499             :   FPM->add(P);
    1500             : }
    1501             : 
    1502             : /// run - Execute all of the passes scheduled for execution.  Keep
    1503             : /// track of whether any of the passes modifies the function, and if
    1504             : /// so, return true.
    1505             : ///
    1506        2346 : bool FunctionPassManager::run(Function &F) {
    1507             :   handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
    1508        1173 :     report_fatal_error("Error reading bitcode file: " + EIB.message());
    1509             :   });
    1510             :   return FPM->run(F);
    1511             : }
    1512             : 
    1513             : 
    1514             : /// doInitialization - Run all of the initializers for the function passes.
    1515             : ///
    1516             : bool FunctionPassManager::doInitialization() {
    1517             :   return FPM->doInitialization(*M);
    1518             : }
    1519       15147 : 
    1520       15147 : /// doFinalization - Run all of the finalizers for the function passes.
    1521             : ///
    1522       15147 : bool FunctionPassManager::doFinalization() {
    1523             :   return FPM->doFinalization(*M);
    1524       15147 : }
    1525       15147 : 
    1526       15147 : //===----------------------------------------------------------------------===//
    1527             : // FunctionPassManagerImpl implementation
    1528       15320 : //
    1529       15135 : bool FunctionPassManagerImpl::doInitialization(Module &M) {
    1530       15320 :   bool Changed = false;
    1531         185 : 
    1532             :   dumpArguments();
    1533         185 :   dumpPasses();
    1534       15135 : 
    1535       15135 :   for (ImmutablePass *ImPass : getImmutablePasses())
    1536       15135 :     Changed |= ImPass->doInitialization(M);
    1537             : 
    1538       59751 :   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
    1539       59751 :     Changed |= getContainedManager(Index)->doInitialization(M);
    1540       59751 : 
    1541             :   return Changed;
    1542             : }
    1543             : 
    1544             : bool FunctionPassManagerImpl::doFinalization(Module &M) {
    1545             :   bool Changed = false;
    1546      833430 : 
    1547      833430 :   for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
    1548             :     Changed |= getContainedManager(Index)->doFinalization(M);
    1549             : 
    1550      833430 :   for (ImmutablePass *ImPass : getImmutablePasses())
    1551             :     Changed |= ImPass->doFinalization(M);
    1552             : 
    1553             :   return Changed;
    1554             : }
    1555             : 
    1556       15114 : /// cleanup - After running all passes, clean up pass manager cache.
    1557       15114 : void FPPassManager::cleanup() {
    1558             :  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1559             :     FunctionPass *FP = getContainedPass(Index);
    1560             :     AnalysisResolver *AR = FP->getResolver();
    1561             :     assert(AR && "Analysis Resolver is not set");
    1562       15106 :     AR->clearAnalysisImpls();
    1563       15106 :  }
    1564             : }
    1565             : 
    1566             : void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
    1567             :   if (!wasRun)
    1568             :     return;
    1569       23005 :   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
    1570             :     FPPassManager *FPPM = getContainedManager(Index);
    1571             :     for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
    1572       23005 :       FPPM->getContainedPass(Index)->releaseMemory();
    1573       23005 :     }
    1574             :   }
    1575       60440 :   wasRun = false;
    1576       37435 : }
    1577             : 
    1578       46010 : // Execute all the passes managed by this top level manager.
    1579       23005 : // Return true if any function is modified by a pass.
    1580             : bool FunctionPassManagerImpl::run(Function &F) {
    1581       23005 :   bool Changed = false;
    1582             : 
    1583             :   initializeAllAnalysisInfo();
    1584       22986 :   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
    1585             :     Changed |= getContainedManager(Index)->runOnFunction(F);
    1586             :     F.getContext().yield();
    1587       45972 :   }
    1588       45972 : 
    1589             :   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
    1590       60379 :     getContainedManager(Index)->cleanup();
    1591       37393 : 
    1592             :   wasRun = true;
    1593       22986 :   return Changed;
    1594             : }
    1595             : 
    1596             : //===----------------------------------------------------------------------===//
    1597      883301 : // FPPassManager implementation
    1598     2119396 : 
    1599             : char FPPassManager::ID = 0;
    1600     1236095 : /// Print passes managed by this manager
    1601             : void FPPassManager::dumpPassStructure(unsigned Offset) {
    1602             :   dbgs().indent(Offset*2) << "FunctionPass Manager\n";
    1603             :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1604      883301 :     FunctionPass *FP = getContainedPass(Index);
    1605             :     FP->dumpPassStructure(Offset + 1);
    1606       57759 :     dumpLastUses(FP, Offset+1);
    1607       57759 :   }
    1608             : }
    1609       99758 : 
    1610             : 
    1611      115345 : /// Execute all of the passes scheduled for execution by invoking
    1612       65466 : /// runOnFunction method.  Keep track of whether any of the passes modifies
    1613             : /// the function, and if so, return true.
    1614             : bool FPPassManager::runOnFunction(Function &F) {
    1615       49879 :   if (F.isDeclaration())
    1616             :     return false;
    1617             : 
    1618             :   bool Changed = false;
    1619             :   Module &M = *F.getParent();
    1620      883309 :   // Collect inherited analysis from Module level pass manager.
    1621             :   populateInheritedAnalysis(TPM->activeStack);
    1622             : 
    1623      883309 :   unsigned InstrCount, FunctionSize = 0;
    1624     1766610 :   StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
    1625      883309 :   bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
    1626      883301 :   // Collect the initial size of the module.
    1627             :   if (EmitICRemark) {
    1628             :     InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
    1629     1766602 :     FunctionSize = F.getInstructionCount();
    1630      883301 :   }
    1631             : 
    1632      883301 :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1633      883301 :     FunctionPass *FP = getContainedPass(Index);
    1634             :     bool LocalChanged = false;
    1635             : 
    1636             :     dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
    1637             :     dumpRequiredSet(FP);
    1638             : 
    1639             :     initializeAnalysisImpl(FP);
    1640             : 
    1641         134 :     {
    1642         134 :       PassManagerPrettyStackEntry X(FP, F);
    1643        4059 :       TimeRegion PassTimer(getPassTimer(FP));
    1644             :       LocalChanged |= FP->runOnFunction(F);
    1645        3925 :       if (EmitICRemark) {
    1646        3925 :         unsigned NewSize = F.getInstructionCount();
    1647             : 
    1648         134 :         // Update the size of the function, emit a remark, and update the size
    1649             :         // of the module.
    1650             :         if (NewSize != FunctionSize) {
    1651             :           int64_t Delta = static_cast<int64_t>(NewSize) -
    1652             :                           static_cast<int64_t>(FunctionSize);
    1653             :           emitInstrCountChangedRemark(FP, M, Delta, InstrCount,
    1654     2413409 :                                       FunctionToInstrCount, &F);
    1655     2413409 :           InstrCount = static_cast<int64_t>(InstrCount) + Delta;
    1656             :           FunctionSize = NewSize;
    1657             :         }
    1658             :       }
    1659     2036532 :     }
    1660             : 
    1661     2036532 :     Changed |= LocalChanged;
    1662             :     if (LocalChanged)
    1663             :       dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
    1664     2036342 :     dumpPreservedSet(FP);
    1665     2036532 :     dumpUsedSet(FP);
    1666             : 
    1667     2036532 :     verifyPreservedAnalysis(FP);
    1668          25 :     removeNotPreservedAnalysis(FP);
    1669          25 :     recordAvailableAnalysis(FP);
    1670             :     removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
    1671             :   }
    1672    51107257 :   return Changed;
    1673             : }
    1674             : 
    1675             : bool FPPassManager::runOnModule(Module &M) {
    1676    49070915 :   bool Changed = false;
    1677    49070921 : 
    1678             :   for (Function &F : M)
    1679    49070920 :     Changed |= runOnFunction(F);
    1680             : 
    1681             :   return Changed;
    1682             : }
    1683    49070935 : 
    1684    49070935 : bool FPPassManager::doInitialization(Module &M) {
    1685    49070729 :   bool Changed = false;
    1686         277 : 
    1687             :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
    1688             :     Changed |= getContainedPass(Index)->doInitialization(M);
    1689             : 
    1690         277 :   return Changed;
    1691          18 : }
    1692           9 : 
    1693           9 : bool FPPassManager::doFinalization(Module &M) {
    1694             :   bool Changed = false;
    1695           9 : 
    1696             :   for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
    1697             :     Changed |= getContainedPass(Index)->doFinalization(M);
    1698             : 
    1699             :   return Changed;
    1700             : }
    1701    49070733 : 
    1702    49070733 : //===----------------------------------------------------------------------===//
    1703     4832333 : // MPPassManager implementation
    1704    49070733 : 
    1705    49070723 : /// Execute all of the passes scheduled for execution by invoking
    1706             : /// runOnModule method.  Keep track of whether any of the passes modifies
    1707    49070723 : /// the module, and if so, return true.
    1708    49070722 : bool
    1709    49070714 : MPPassManager::runOnModule(Module &M) {
    1710    49070718 :   bool Changed = false;
    1711             : 
    1712             :   // Initialize on-the-fly passes
    1713             :   for (auto &OnTheFlyManager : OnTheFlyManagers) {
    1714             :     FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
    1715       81437 :     Changed |= FPP->doInitialization(M);
    1716             :   }
    1717             : 
    1718     1506179 :   // Initialize module passes
    1719     1424915 :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
    1720             :     Changed |= getContainedPass(Index)->doInitialization(M);
    1721       81264 : 
    1722             :   unsigned InstrCount, ModuleCount = 0;
    1723             :   StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
    1724      110416 :   bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
    1725             :   // Collect the initial size of the module.
    1726             :   if (EmitICRemark) {
    1727     3992669 :     InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
    1728     3882256 :     ModuleCount = InstrCount;
    1729             :   }
    1730      110413 : 
    1731             :   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
    1732             :     ModulePass *MP = getContainedPass(Index);
    1733      109996 :     bool LocalChanged = false;
    1734             : 
    1735             :     dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
    1736     3971378 :     dumpRequiredSet(MP);
    1737     7722784 : 
    1738             :     initializeAnalysisImpl(MP);
    1739      109986 : 
    1740             :     {
    1741             :       PassManagerPrettyStackEntry X(MP, M);
    1742             :       TimeRegion PassTimer(getPassTimer(MP));
    1743             : 
    1744             :       LocalChanged |= MP->runOnModule(M);
    1745             :       if (EmitICRemark) {
    1746             :         // Update the size of the module.
    1747             :         ModuleCount = M.getInstructionCount();
    1748             :         if (ModuleCount != InstrCount) {
    1749       62191 :           int64_t Delta = static_cast<int64_t>(ModuleCount) -
    1750             :                           static_cast<int64_t>(InstrCount);
    1751             :           emitInstrCountChangedRemark(MP, M, Delta, InstrCount,
    1752             :                                       FunctionToInstrCount);
    1753       70082 :           InstrCount = ModuleCount;
    1754        7891 :         }
    1755        7891 :       }
    1756             :     }
    1757             : 
    1758             :     Changed |= LocalChanged;
    1759      330449 :     if (LocalChanged)
    1760      268267 :       dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
    1761             :                    M.getModuleIdentifier());
    1762             :     dumpPreservedSet(MP);
    1763       61985 :     dumpUsedSet(MP);
    1764       62182 : 
    1765             :     verifyPreservedAnalysis(MP);
    1766       62182 :     removeNotPreservedAnalysis(MP);
    1767          10 :     recordAvailableAnalysis(MP);
    1768             :     removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
    1769             :   }
    1770             : 
    1771      330120 :   // Finalize module passes
    1772             :   for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
    1773             :     Changed |= getContainedPass(Index)->doFinalization(M);
    1774             : 
    1775      536250 :   // Finalize on-the-fly passes
    1776      268125 :   for (auto &OnTheFlyManager : OnTheFlyManagers) {
    1777             :     FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
    1778      268125 :     // We don't know when is the last time an on-the-fly pass is run,
    1779             :     // so we need to releaseMemory / finalize here
    1780             :     FPP->releaseMemoryOnTheFly();
    1781             :     Changed |= FPP->doFinalization(M);
    1782      268125 :   }
    1783             : 
    1784      268125 :   return Changed;
    1785      267938 : }
    1786             : 
    1787          23 : /// Add RequiredPass into list of lower level passes required by pass P.
    1788          23 : /// RequiredPass is run on the fly by Pass Manager when P requests it
    1789          14 : /// through getAnalysis interface.
    1790           7 : void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
    1791           7 :   assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
    1792             :          "Unable to handle Pass that requires lower level Analysis pass");
    1793             :   assert((P->getPotentialPassManagerType() <
    1794             :           RequiredPass->getPotentialPassManagerType()) &&
    1795             :          "Unable to handle Pass that requires lower level Analysis pass");
    1796             :   if (!RequiredPass)
    1797             :     return;
    1798      267938 : 
    1799      267938 :   FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
    1800       56610 :   if (!FPP) {
    1801             :     FPP = new FunctionPassManagerImpl();
    1802      267938 :     // FPP is the top level manager.
    1803      267938 :     FPP->setTopLevelManager(FPP);
    1804             : 
    1805      267938 :     OnTheFlyManagers[P] = FPP;
    1806      267938 :   }
    1807      267938 :   const PassInfo *RequiredPassPI =
    1808      267938 :       TPM->findAnalysisPassInfo(RequiredPass->getPassID());
    1809             : 
    1810             :   Pass *FoundPass = nullptr;
    1811             :   if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
    1812      329405 :     FoundPass =
    1813      534840 :       ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
    1814             :   }
    1815             :   if (!FoundPass) {
    1816       69865 :     FoundPass = RequiredPass;
    1817        7880 :     // This should be guaranteed to add RequiredPass to the passmanager given
    1818             :     // that we checked for an available analysis above.
    1819             :     FPP->add(RequiredPass);
    1820        7880 :   }
    1821        7880 :   // Register P as the last user of FoundPass or RequiredPass.
    1822             :   SmallVector<Pass *, 1> LU;
    1823             :   LU.push_back(FoundPass);
    1824       61985 :   FPP->setLastUser(LU,  P);
    1825             : }
    1826             : 
    1827             : /// Return function pass corresponding to PassInfo PI, that is
    1828             : /// required by module pass MP. Instantiate analysis pass, by using
    1829             : /// its runOnFunction() for function F.
    1830       11769 : Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
    1831             :   FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
    1832             :   assert(FPP && "Unable to find on the fly pass");
    1833             : 
    1834             :   FPP->releaseMemoryOnTheFly();
    1835             :   FPP->run(F);
    1836       11769 :   return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
    1837           0 : }
    1838             : 
    1839       11769 : 
    1840       11769 : //===----------------------------------------------------------------------===//
    1841        7915 : // PassManagerImpl implementation
    1842             : 
    1843        7915 : //
    1844             : /// run - Execute all of the passes scheduled for execution.  Keep track of
    1845        7915 : /// whether any of the passes modifies the module, and if so, return true.
    1846             : bool PassManagerImpl::run(Module &M) {
    1847             :   bool Changed = false;
    1848       11769 : 
    1849             :   dumpArguments();
    1850       11769 :   dumpPasses();
    1851       11769 : 
    1852       11768 :   for (ImmutablePass *ImPass : getImmutablePasses())
    1853       11768 :     Changed |= ImPass->doInitialization(M);
    1854             : 
    1855       11769 :   initializeAllAnalysisInfo();
    1856       11769 :   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
    1857             :     Changed |= getContainedManager(Index)->runOnModule(M);
    1858             :     M.getContext().yield();
    1859             :   }
    1860             : 
    1861             :   for (ImmutablePass *ImPass : getImmutablePasses())
    1862             :     Changed |= ImPass->doFinalization(M);
    1863       11769 : 
    1864       23538 :   return Changed;
    1865             : }
    1866             : 
    1867             : //===----------------------------------------------------------------------===//
    1868             : // PassManager implementation
    1869             : 
    1870       49879 : /// Create new pass manager
    1871       49879 : PassManager::PassManager() {
    1872             :   PM = new PassManagerImpl();
    1873             :   // PM is the top level manager
    1874       49879 :   PM->setTopLevelManager(PM);
    1875       49879 : }
    1876       49879 : 
    1877             : PassManager::~PassManager() {
    1878             :   delete PM;
    1879             : }
    1880             : 
    1881             : void PassManager::add(Pass *P) {
    1882             :   PM->add(P);
    1883             : }
    1884             : 
    1885             : /// run - Execute all of the passes scheduled for execution.  Keep track of
    1886       62191 : /// whether any of the passes modifies the module, and if so, return true.
    1887             : bool PassManager::run(Module &M) {
    1888             :   return PM->run(M);
    1889       62191 : }
    1890       62191 : 
    1891             : //===----------------------------------------------------------------------===//
    1892      444939 : // PMStack implementation
    1893      382748 : //
    1894             : 
    1895       62191 : // Pop Pass Manager from the stack and clear its analysis info.
    1896      124176 : void PMStack::pop() {
    1897       62191 : 
    1898       61985 :   PMDataManager *Top = this->top();
    1899             :   Top->initializeAnalysisInfo();
    1900             : 
    1901      443064 :   S.pop_back();
    1902      381079 : }
    1903             : 
    1904       61985 : // Push PM on the stack and set its top level manager.
    1905             : void PMStack::push(PMDataManager *PM) {
    1906             :   assert(PM && "Unable to push. Pass Manager expected");
    1907             :   assert(PM->getDepth()==0 && "Pass Manager depth set too early");
    1908             : 
    1909             :   if (!this->empty()) {
    1910             :     assert(PM->getPassManagerType() > this->top()->getPassManagerType()
    1911       62392 :            && "pushing bad pass manager to PMStack");
    1912       62392 :     PMTopLevelManager *TPM = this->top()->getTopLevelManager();
    1913             : 
    1914       62392 :     assert(TPM && "Unable to find top level manager");
    1915       62392 :     TPM->addIndirectPassManager(PM);
    1916             :     PM->setTopLevelManager(TPM);
    1917       62137 :     PM->setDepth(this->top()->getDepth()+1);
    1918       62129 :   } else {
    1919       62137 :     assert((PM->getPassManagerType() == PMT_ModulePassManager
    1920           8 :            || PM->getPassManagerType() == PMT_FunctionPassManager)
    1921             :            && "pushing bad pass manager to PMStack");
    1922           8 :     PM->setDepth(1);
    1923       62129 :   }
    1924       62129 : 
    1925       62129 :   S.push_back(PM);
    1926             : }
    1927     2726449 : 
    1928     2726449 : // Dump content of the pass manager stack.
    1929     2726443 : LLVM_DUMP_METHOD void PMStack::dump() const {
    1930             :   for (PMDataManager *Manager : S)
    1931             :     dbgs() << Manager->getAsPass()->getPassName() << ' ';
    1932             : 
    1933       62191 :   if (!S.empty())
    1934       62191 :     dbgs() << '\n';
    1935             : }
    1936             : 
    1937             : /// Find appropriate Module Pass Manager in the PM Stack and
    1938             : /// add self into that manager.
    1939             : void ModulePass::assignPassManager(PMStack &PMS,
    1940             :                                    PassManagerType PreferredType) {
    1941             :   // Find Module Pass Manager
    1942      115874 :   while (!PMS.empty()) {
    1943             :     PassManagerType TopPMType = PMS.top()->getPassManagerType();
    1944             :     if (TopPMType == PreferredType)
    1945             :       break; // We found desired pass manager
    1946             :     else if (TopPMType > PMT_ModulePassManager)
    1947             :       PMS.pop();    // Pop children pass managers
    1948      115874 :     else
    1949             :       break;
    1950             :   }
    1951      236140 :   assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
    1952             :   PMS.top()->add(this);
    1953             : }
    1954             : 
    1955      236140 : /// Find appropriate Function Pass Manager or Call Graph Pass Manager
    1956             : /// in the PM Stack and add self into that manager.
    1957             : void FunctionPass::assignPassManager(PMStack &PMS,
    1958      150686 :                                      PassManagerType PreferredType) {
    1959             : 
    1960             :   // Find Function Pass Manager
    1961      150686 :   while (!PMS.empty()) {
    1962      150686 :     if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
    1963      150686 :       PMS.pop();
    1964             :     else
    1965             :       break;
    1966             :   }
    1967             : 
    1968       85454 :   // Create new Function Pass Manager if needed.
    1969             :   FPPassManager *FPP;
    1970             :   if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
    1971      236140 :     FPP = (FPPassManager *)PMS.top();
    1972      236140 :   } else {
    1973             :     assert(!PMS.empty() && "Unable to create Function Pass Manager");
    1974             :     PMDataManager *PMD = PMS.top();
    1975           0 : 
    1976           0 :     // [1] Create new Function Pass Manager
    1977           0 :     FPP = new FPPassManager();
    1978             :     FPP->populateInheritedAnalysis(PMS);
    1979           0 : 
    1980           0 :     // [2] Set up new manager's top level manager
    1981           0 :     PMTopLevelManager *TPM = PMD->getTopLevelManager();
    1982             :     TPM->addIndirectPassManager(FPP);
    1983             : 
    1984             :     // [3] Assign manager to manage this new manager. This may create
    1985      274539 :     // and push new managers into PMS
    1986             :     FPP->assignPassManager(PMS, PMD->getPassManagerType());
    1987             : 
    1988      346702 :     // [4] Push new manager into PMS
    1989      346702 :     PMS.push(FPP);
    1990      346702 :   }
    1991             : 
    1992       72163 :   // Assign FPP as the manager of this pass.
    1993       72163 :   FPP->add(this);
    1994             : }
    1995             : 
    1996             : /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
    1997             : /// in the PM Stack and add self into that manager.
    1998      274539 : void BasicBlockPass::assignPassManager(PMStack &PMS,
    1999      274539 :                                        PassManagerType PreferredType) {
    2000             :   BBPassManager *BBP;
    2001             : 
    2002             :   // Basic Pass Manager is a leaf pass manager. It does not handle
    2003     3883951 :   // any other pass manager.
    2004             :   if (!PMS.empty() &&
    2005             :       PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
    2006             :     BBP = (BBPassManager *)PMS.top();
    2007     3925672 :   } else {
    2008     3925672 :     // If leaf manager is not Basic Block Pass manager then create new
    2009       41721 :     // basic Block Pass manager.
    2010             :     assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
    2011             :     PMDataManager *PMD = PMS.top();
    2012             : 
    2013             :     // [1] Create new Basic Block Manager
    2014             :     BBP = new BBPassManager();
    2015             : 
    2016     3883951 :     // [2] Set up new manager's top level manager
    2017     3796348 :     // Basic Block Pass Manager does not live by itself
    2018             :     PMTopLevelManager *TPM = PMD->getTopLevelManager();
    2019             :     TPM->addIndirectPassManager(BBP);
    2020             : 
    2021             :     // [3] Assign manager to manage this new manager. This may create
    2022             :     // and push new managers into PMS
    2023       87603 :     BBP->assignPassManager(PMS, PreferredType);
    2024             : 
    2025             :     // [4] Push new manager into PMS
    2026             :     PMS.push(BBP);
    2027       87603 :   }
    2028       87603 : 
    2029             :   // Assign BBP as the manager of this pass.
    2030             :   BBP->add(this);
    2031             : }
    2032       87603 : 
    2033             : PassManagerBase::~PassManagerBase() {}

Generated by: LCOV version 1.13