Home | History | Annotate | Download | only in IPA
      1 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
      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 CallGraphSCCPass class, which is used for passes
     11 // which are implemented as bottom-up traversals on the call graph.  Because
     12 // there may be cycles in the call graph, passes of this type operate on the
     13 // call-graph in SCC order: that is, they process function bottom-up, except for
     14 // recursive functions, which they process all at once.
     15 //
     16 //===----------------------------------------------------------------------===//
     17 
     18 #include "llvm/Analysis/CallGraphSCCPass.h"
     19 #include "llvm/ADT/SCCIterator.h"
     20 #include "llvm/ADT/Statistic.h"
     21 #include "llvm/Analysis/CallGraph.h"
     22 #include "llvm/IR/Function.h"
     23 #include "llvm/IR/IntrinsicInst.h"
     24 #include "llvm/IR/LegacyPassManagers.h"
     25 #include "llvm/IR/LLVMContext.h"
     26 #include "llvm/Support/CommandLine.h"
     27 #include "llvm/Support/Debug.h"
     28 #include "llvm/Support/Timer.h"
     29 #include "llvm/Support/raw_ostream.h"
     30 using namespace llvm;
     31 
     32 #define DEBUG_TYPE "cgscc-passmgr"
     33 
     34 static cl::opt<unsigned>
     35 MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
     36 
     37 STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
     38 
     39 //===----------------------------------------------------------------------===//
     40 // CGPassManager
     41 //
     42 /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
     43 
     44 namespace {
     45 
     46 class CGPassManager : public ModulePass, public PMDataManager {
     47 public:
     48   static char ID;
     49   explicit CGPassManager()
     50     : ModulePass(ID), PMDataManager() { }
     51 
     52   /// run - Execute all of the passes scheduled for execution.  Keep track of
     53   /// whether any of the passes modifies the module, and if so, return true.
     54   bool runOnModule(Module &M) override;
     55 
     56   using ModulePass::doInitialization;
     57   using ModulePass::doFinalization;
     58 
     59   bool doInitialization(CallGraph &CG);
     60   bool doFinalization(CallGraph &CG);
     61 
     62   /// Pass Manager itself does not invalidate any analysis info.
     63   void getAnalysisUsage(AnalysisUsage &Info) const override {
     64     // CGPassManager walks SCC and it needs CallGraph.
     65     Info.addRequired<CallGraphWrapperPass>();
     66     Info.setPreservesAll();
     67   }
     68 
     69   const char *getPassName() const override {
     70     return "CallGraph Pass Manager";
     71   }
     72 
     73   PMDataManager *getAsPMDataManager() override { return this; }
     74   Pass *getAsPass() override { return this; }
     75 
     76   // Print passes managed by this manager
     77   void dumpPassStructure(unsigned Offset) override {
     78     errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
     79     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
     80       Pass *P = getContainedPass(Index);
     81       P->dumpPassStructure(Offset + 1);
     82       dumpLastUses(P, Offset+1);
     83     }
     84   }
     85 
     86   Pass *getContainedPass(unsigned N) {
     87     assert(N < PassVector.size() && "Pass number out of range!");
     88     return static_cast<Pass *>(PassVector[N]);
     89   }
     90 
     91   PassManagerType getPassManagerType() const override {
     92     return PMT_CallGraphPassManager;
     93   }
     94 
     95 private:
     96   bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
     97                          bool &DevirtualizedCall);
     98 
     99   bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
    100                     CallGraph &CG, bool &CallGraphUpToDate,
    101                     bool &DevirtualizedCall);
    102   bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
    103                         bool IsCheckingMode);
    104 };
    105 
    106 } // end anonymous namespace.
    107 
    108 char CGPassManager::ID = 0;
    109 
    110 
    111 bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
    112                                  CallGraph &CG, bool &CallGraphUpToDate,
    113                                  bool &DevirtualizedCall) {
    114   bool Changed = false;
    115   PMDataManager *PM = P->getAsPMDataManager();
    116 
    117   if (!PM) {
    118     CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
    119     if (!CallGraphUpToDate) {
    120       DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
    121       CallGraphUpToDate = true;
    122     }
    123 
    124     {
    125       TimeRegion PassTimer(getPassTimer(CGSP));
    126       Changed = CGSP->runOnSCC(CurSCC);
    127     }
    128 
    129     // After the CGSCCPass is done, when assertions are enabled, use
    130     // RefreshCallGraph to verify that the callgraph was correctly updated.
    131 #ifndef NDEBUG
    132     if (Changed)
    133       RefreshCallGraph(CurSCC, CG, true);
    134 #endif
    135 
    136     return Changed;
    137   }
    138 
    139 
    140   assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
    141          "Invalid CGPassManager member");
    142   FPPassManager *FPP = (FPPassManager*)P;
    143 
    144   // Run pass P on all functions in the current SCC.
    145   for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
    146        I != E; ++I) {
    147     if (Function *F = (*I)->getFunction()) {
    148       dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
    149       {
    150         TimeRegion PassTimer(getPassTimer(FPP));
    151         Changed |= FPP->runOnFunction(*F);
    152       }
    153       F->getContext().yield();
    154     }
    155   }
    156 
    157   // The function pass(es) modified the IR, they may have clobbered the
    158   // callgraph.
    159   if (Changed && CallGraphUpToDate) {
    160     DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
    161                  << P->getPassName() << '\n');
    162     CallGraphUpToDate = false;
    163   }
    164   return Changed;
    165 }
    166 
    167 
    168 /// RefreshCallGraph - Scan the functions in the specified CFG and resync the
    169 /// callgraph with the call sites found in it.  This is used after
    170 /// FunctionPasses have potentially munged the callgraph, and can be used after
    171 /// CallGraphSCC passes to verify that they correctly updated the callgraph.
    172 ///
    173 /// This function returns true if it devirtualized an existing function call,
    174 /// meaning it turned an indirect call into a direct call.  This happens when
    175 /// a function pass like GVN optimizes away stuff feeding the indirect call.
    176 /// This never happens in checking mode.
    177 ///
    178 bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
    179                                      CallGraph &CG, bool CheckingMode) {
    180   DenseMap<Value*, CallGraphNode*> CallSites;
    181 
    182   DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
    183                << " nodes:\n";
    184         for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
    185              I != E; ++I)
    186           (*I)->dump();
    187         );
    188 
    189   bool MadeChange = false;
    190   bool DevirtualizedCall = false;
    191 
    192   // Scan all functions in the SCC.
    193   unsigned FunctionNo = 0;
    194   for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
    195        SCCIdx != E; ++SCCIdx, ++FunctionNo) {
    196     CallGraphNode *CGN = *SCCIdx;
    197     Function *F = CGN->getFunction();
    198     if (!F || F->isDeclaration()) continue;
    199 
    200     // Walk the function body looking for call sites.  Sync up the call sites in
    201     // CGN with those actually in the function.
    202 
    203     // Keep track of the number of direct and indirect calls that were
    204     // invalidated and removed.
    205     unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
    206 
    207     // Get the set of call sites currently in the function.
    208     for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
    209       // If this call site is null, then the function pass deleted the call
    210       // entirely and the WeakVH nulled it out.
    211       if (!I->first ||
    212           // If we've already seen this call site, then the FunctionPass RAUW'd
    213           // one call with another, which resulted in two "uses" in the edge
    214           // list of the same call.
    215           CallSites.count(I->first) ||
    216 
    217           // If the call edge is not from a call or invoke, then the function
    218           // pass RAUW'd a call with another value.  This can happen when
    219           // constant folding happens of well known functions etc.
    220           !CallSite(I->first)) {
    221         assert(!CheckingMode &&
    222                "CallGraphSCCPass did not update the CallGraph correctly!");
    223 
    224         // If this was an indirect call site, count it.
    225         if (!I->second->getFunction())
    226           ++NumIndirectRemoved;
    227         else
    228           ++NumDirectRemoved;
    229 
    230         // Just remove the edge from the set of callees, keep track of whether
    231         // I points to the last element of the vector.
    232         bool WasLast = I + 1 == E;
    233         CGN->removeCallEdge(I);
    234 
    235         // If I pointed to the last element of the vector, we have to bail out:
    236         // iterator checking rejects comparisons of the resultant pointer with
    237         // end.
    238         if (WasLast)
    239           break;
    240         E = CGN->end();
    241         continue;
    242       }
    243 
    244       assert(!CallSites.count(I->first) &&
    245              "Call site occurs in node multiple times");
    246       CallSites.insert(std::make_pair(I->first, I->second));
    247       ++I;
    248     }
    249 
    250     // Loop over all of the instructions in the function, getting the callsites.
    251     // Keep track of the number of direct/indirect calls added.
    252     unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
    253 
    254     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
    255       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
    256         CallSite CS(cast<Value>(I));
    257         if (!CS) continue;
    258         Function *Callee = CS.getCalledFunction();
    259         if (Callee && Callee->isIntrinsic()) continue;
    260 
    261         // If this call site already existed in the callgraph, just verify it
    262         // matches up to expectations and remove it from CallSites.
    263         DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
    264           CallSites.find(CS.getInstruction());
    265         if (ExistingIt != CallSites.end()) {
    266           CallGraphNode *ExistingNode = ExistingIt->second;
    267 
    268           // Remove from CallSites since we have now seen it.
    269           CallSites.erase(ExistingIt);
    270 
    271           // Verify that the callee is right.
    272           if (ExistingNode->getFunction() == CS.getCalledFunction())
    273             continue;
    274 
    275           // If we are in checking mode, we are not allowed to actually mutate
    276           // the callgraph.  If this is a case where we can infer that the
    277           // callgraph is less precise than it could be (e.g. an indirect call
    278           // site could be turned direct), don't reject it in checking mode, and
    279           // don't tweak it to be more precise.
    280           if (CheckingMode && CS.getCalledFunction() &&
    281               ExistingNode->getFunction() == nullptr)
    282             continue;
    283 
    284           assert(!CheckingMode &&
    285                  "CallGraphSCCPass did not update the CallGraph correctly!");
    286 
    287           // If not, we either went from a direct call to indirect, indirect to
    288           // direct, or direct to different direct.
    289           CallGraphNode *CalleeNode;
    290           if (Function *Callee = CS.getCalledFunction()) {
    291             CalleeNode = CG.getOrInsertFunction(Callee);
    292             // Keep track of whether we turned an indirect call into a direct
    293             // one.
    294             if (!ExistingNode->getFunction()) {
    295               DevirtualizedCall = true;
    296               DEBUG(dbgs() << "  CGSCCPASSMGR: Devirtualized call to '"
    297                            << Callee->getName() << "'\n");
    298             }
    299           } else {
    300             CalleeNode = CG.getCallsExternalNode();
    301           }
    302 
    303           // Update the edge target in CGN.
    304           CGN->replaceCallEdge(CS, CS, CalleeNode);
    305           MadeChange = true;
    306           continue;
    307         }
    308 
    309         assert(!CheckingMode &&
    310                "CallGraphSCCPass did not update the CallGraph correctly!");
    311 
    312         // If the call site didn't exist in the CGN yet, add it.
    313         CallGraphNode *CalleeNode;
    314         if (Function *Callee = CS.getCalledFunction()) {
    315           CalleeNode = CG.getOrInsertFunction(Callee);
    316           ++NumDirectAdded;
    317         } else {
    318           CalleeNode = CG.getCallsExternalNode();
    319           ++NumIndirectAdded;
    320         }
    321 
    322         CGN->addCalledFunction(CS, CalleeNode);
    323         MadeChange = true;
    324       }
    325 
    326     // We scanned the old callgraph node, removing invalidated call sites and
    327     // then added back newly found call sites.  One thing that can happen is
    328     // that an old indirect call site was deleted and replaced with a new direct
    329     // call.  In this case, we have devirtualized a call, and CGSCCPM would like
    330     // to iteratively optimize the new code.  Unfortunately, we don't really
    331     // have a great way to detect when this happens.  As an approximation, we
    332     // just look at whether the number of indirect calls is reduced and the
    333     // number of direct calls is increased.  There are tons of ways to fool this
    334     // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
    335     // direct call) but this is close enough.
    336     if (NumIndirectRemoved > NumIndirectAdded &&
    337         NumDirectRemoved < NumDirectAdded)
    338       DevirtualizedCall = true;
    339 
    340     // After scanning this function, if we still have entries in callsites, then
    341     // they are dangling pointers.  WeakVH should save us for this, so abort if
    342     // this happens.
    343     assert(CallSites.empty() && "Dangling pointers found in call sites map");
    344 
    345     // Periodically do an explicit clear to remove tombstones when processing
    346     // large scc's.
    347     if ((FunctionNo & 15) == 15)
    348       CallSites.clear();
    349   }
    350 
    351   DEBUG(if (MadeChange) {
    352           dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
    353           for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
    354             I != E; ++I)
    355               (*I)->dump();
    356           if (DevirtualizedCall)
    357             dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
    358 
    359          } else {
    360            dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
    361          }
    362         );
    363   (void)MadeChange;
    364 
    365   return DevirtualizedCall;
    366 }
    367 
    368 /// RunAllPassesOnSCC -  Execute the body of the entire pass manager on the
    369 /// specified SCC.  This keeps track of whether a function pass devirtualizes
    370 /// any calls and returns it in DevirtualizedCall.
    371 bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
    372                                       bool &DevirtualizedCall) {
    373   bool Changed = false;
    374 
    375   // CallGraphUpToDate - Keep track of whether the callgraph is known to be
    376   // up-to-date or not.  The CGSSC pass manager runs two types of passes:
    377   // CallGraphSCC Passes and other random function passes.  Because other
    378   // random function passes are not CallGraph aware, they may clobber the
    379   // call graph by introducing new calls or deleting other ones.  This flag
    380   // is set to false when we run a function pass so that we know to clean up
    381   // the callgraph when we need to run a CGSCCPass again.
    382   bool CallGraphUpToDate = true;
    383 
    384   // Run all passes on current SCC.
    385   for (unsigned PassNo = 0, e = getNumContainedPasses();
    386        PassNo != e; ++PassNo) {
    387     Pass *P = getContainedPass(PassNo);
    388 
    389     // If we're in -debug-pass=Executions mode, construct the SCC node list,
    390     // otherwise avoid constructing this string as it is expensive.
    391     if (isPassDebuggingExecutionsOrMore()) {
    392       std::string Functions;
    393   #ifndef NDEBUG
    394       raw_string_ostream OS(Functions);
    395       for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
    396            I != E; ++I) {
    397         if (I != CurSCC.begin()) OS << ", ";
    398         (*I)->print(OS);
    399       }
    400       OS.flush();
    401   #endif
    402       dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
    403     }
    404     dumpRequiredSet(P);
    405 
    406     initializeAnalysisImpl(P);
    407 
    408     // Actually run this pass on the current SCC.
    409     Changed |= RunPassOnSCC(P, CurSCC, CG,
    410                             CallGraphUpToDate, DevirtualizedCall);
    411 
    412     if (Changed)
    413       dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
    414     dumpPreservedSet(P);
    415 
    416     verifyPreservedAnalysis(P);
    417     removeNotPreservedAnalysis(P);
    418     recordAvailableAnalysis(P);
    419     removeDeadPasses(P, "", ON_CG_MSG);
    420   }
    421 
    422   // If the callgraph was left out of date (because the last pass run was a
    423   // functionpass), refresh it before we move on to the next SCC.
    424   if (!CallGraphUpToDate)
    425     DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
    426   return Changed;
    427 }
    428 
    429 /// run - Execute all of the passes scheduled for execution.  Keep track of
    430 /// whether any of the passes modifies the module, and if so, return true.
    431 bool CGPassManager::runOnModule(Module &M) {
    432   CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
    433   bool Changed = doInitialization(CG);
    434 
    435   // Walk the callgraph in bottom-up SCC order.
    436   scc_iterator<CallGraph*> CGI = scc_begin(&CG);
    437 
    438   CallGraphSCC CurSCC(&CGI);
    439   while (!CGI.isAtEnd()) {
    440     // Copy the current SCC and increment past it so that the pass can hack
    441     // on the SCC if it wants to without invalidating our iterator.
    442     const std::vector<CallGraphNode *> &NodeVec = *CGI;
    443     CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size());
    444     ++CGI;
    445 
    446     // At the top level, we run all the passes in this pass manager on the
    447     // functions in this SCC.  However, we support iterative compilation in the
    448     // case where a function pass devirtualizes a call to a function.  For
    449     // example, it is very common for a function pass (often GVN or instcombine)
    450     // to eliminate the addressing that feeds into a call.  With that improved
    451     // information, we would like the call to be an inline candidate, infer
    452     // mod-ref information etc.
    453     //
    454     // Because of this, we allow iteration up to a specified iteration count.
    455     // This only happens in the case of a devirtualized call, so we only burn
    456     // compile time in the case that we're making progress.  We also have a hard
    457     // iteration count limit in case there is crazy code.
    458     unsigned Iteration = 0;
    459     bool DevirtualizedCall = false;
    460     do {
    461       DEBUG(if (Iteration)
    462               dbgs() << "  SCCPASSMGR: Re-visiting SCC, iteration #"
    463                      << Iteration << '\n');
    464       DevirtualizedCall = false;
    465       Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
    466     } while (Iteration++ < MaxIterations && DevirtualizedCall);
    467 
    468     if (DevirtualizedCall)
    469       DEBUG(dbgs() << "  CGSCCPASSMGR: Stopped iteration after " << Iteration
    470                    << " times, due to -max-cg-scc-iterations\n");
    471 
    472     if (Iteration > MaxSCCIterations)
    473       MaxSCCIterations = Iteration;
    474 
    475   }
    476   Changed |= doFinalization(CG);
    477   return Changed;
    478 }
    479 
    480 
    481 /// Initialize CG
    482 bool CGPassManager::doInitialization(CallGraph &CG) {
    483   bool Changed = false;
    484   for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
    485     if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
    486       assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
    487              "Invalid CGPassManager member");
    488       Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
    489     } else {
    490       Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
    491     }
    492   }
    493   return Changed;
    494 }
    495 
    496 /// Finalize CG
    497 bool CGPassManager::doFinalization(CallGraph &CG) {
    498   bool Changed = false;
    499   for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
    500     if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
    501       assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
    502              "Invalid CGPassManager member");
    503       Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
    504     } else {
    505       Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
    506     }
    507   }
    508   return Changed;
    509 }
    510 
    511 //===----------------------------------------------------------------------===//
    512 // CallGraphSCC Implementation
    513 //===----------------------------------------------------------------------===//
    514 
    515 /// ReplaceNode - This informs the SCC and the pass manager that the specified
    516 /// Old node has been deleted, and New is to be used in its place.
    517 void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
    518   assert(Old != New && "Should not replace node with self");
    519   for (unsigned i = 0; ; ++i) {
    520     assert(i != Nodes.size() && "Node not in SCC");
    521     if (Nodes[i] != Old) continue;
    522     Nodes[i] = New;
    523     break;
    524   }
    525 
    526   // Update the active scc_iterator so that it doesn't contain dangling
    527   // pointers to the old CallGraphNode.
    528   scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
    529   CGI->ReplaceNode(Old, New);
    530 }
    531 
    532 
    533 //===----------------------------------------------------------------------===//
    534 // CallGraphSCCPass Implementation
    535 //===----------------------------------------------------------------------===//
    536 
    537 /// Assign pass manager to manage this pass.
    538 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
    539                                          PassManagerType PreferredType) {
    540   // Find CGPassManager
    541   while (!PMS.empty() &&
    542          PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
    543     PMS.pop();
    544 
    545   assert(!PMS.empty() && "Unable to handle Call Graph Pass");
    546   CGPassManager *CGP;
    547 
    548   if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
    549     CGP = (CGPassManager*)PMS.top();
    550   else {
    551     // Create new Call Graph SCC Pass Manager if it does not exist.
    552     assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
    553     PMDataManager *PMD = PMS.top();
    554 
    555     // [1] Create new Call Graph Pass Manager
    556     CGP = new CGPassManager();
    557 
    558     // [2] Set up new manager's top level manager
    559     PMTopLevelManager *TPM = PMD->getTopLevelManager();
    560     TPM->addIndirectPassManager(CGP);
    561 
    562     // [3] Assign manager to manage this new manager. This may create
    563     // and push new managers into PMS
    564     Pass *P = CGP;
    565     TPM->schedulePass(P);
    566 
    567     // [4] Push new manager into PMS
    568     PMS.push(CGP);
    569   }
    570 
    571   CGP->add(this);
    572 }
    573 
    574 /// getAnalysisUsage - For this class, we declare that we require and preserve
    575 /// the call graph.  If the derived class implements this method, it should
    576 /// always explicitly call the implementation here.
    577 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
    578   AU.addRequired<CallGraphWrapperPass>();
    579   AU.addPreserved<CallGraphWrapperPass>();
    580 }
    581 
    582 
    583 //===----------------------------------------------------------------------===//
    584 // PrintCallGraphPass Implementation
    585 //===----------------------------------------------------------------------===//
    586 
    587 namespace {
    588   /// PrintCallGraphPass - Print a Module corresponding to a call graph.
    589   ///
    590   class PrintCallGraphPass : public CallGraphSCCPass {
    591     std::string Banner;
    592     raw_ostream &Out;       // raw_ostream to print on.
    593 
    594   public:
    595     static char ID;
    596     PrintCallGraphPass(const std::string &B, raw_ostream &o)
    597       : CallGraphSCCPass(ID), Banner(B), Out(o) {}
    598 
    599     void getAnalysisUsage(AnalysisUsage &AU) const override {
    600       AU.setPreservesAll();
    601     }
    602 
    603     bool runOnSCC(CallGraphSCC &SCC) override {
    604       Out << Banner;
    605       for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
    606         if ((*I)->getFunction())
    607           (*I)->getFunction()->print(Out);
    608         else
    609           Out << "\nPrinting <null> Function\n";
    610       }
    611       return false;
    612     }
    613   };
    614 
    615 } // end anonymous namespace.
    616 
    617 char PrintCallGraphPass::ID = 0;
    618 
    619 Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
    620                                           const std::string &Banner) const {
    621   return new PrintCallGraphPass(Banner, O);
    622 }
    623 
    624