LLVM API Documentation

CallGraphSCCPass.cpp
Go to the documentation of this file.
00001 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the CallGraphSCCPass class, which is used for passes
00011 // which are implemented as bottom-up traversals on the call graph.  Because
00012 // there may be cycles in the call graph, passes of this type operate on the
00013 // call-graph in SCC order: that is, they process function bottom-up, except for
00014 // recursive functions, which they process all at once.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "llvm/Analysis/CallGraphSCCPass.h"
00019 #include "llvm/ADT/SCCIterator.h"
00020 #include "llvm/ADT/Statistic.h"
00021 #include "llvm/Analysis/CallGraph.h"
00022 #include "llvm/IR/Function.h"
00023 #include "llvm/IR/IntrinsicInst.h"
00024 #include "llvm/IR/LegacyPassManagers.h"
00025 #include "llvm/IR/LLVMContext.h"
00026 #include "llvm/Support/CommandLine.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/Timer.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 using namespace llvm;
00031 
00032 #define DEBUG_TYPE "cgscc-passmgr"
00033 
00034 static cl::opt<unsigned> 
00035 MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4));
00036 
00037 STATISTIC(MaxSCCIterations, "Maximum CGSCCPassMgr iterations on one SCC");
00038 
00039 //===----------------------------------------------------------------------===//
00040 // CGPassManager
00041 //
00042 /// CGPassManager manages FPPassManagers and CallGraphSCCPasses.
00043 
00044 namespace {
00045 
00046 class CGPassManager : public ModulePass, public PMDataManager {
00047 public:
00048   static char ID;
00049   explicit CGPassManager() 
00050     : ModulePass(ID), PMDataManager() { }
00051 
00052   /// run - Execute all of the passes scheduled for execution.  Keep track of
00053   /// whether any of the passes modifies the module, and if so, return true.
00054   bool runOnModule(Module &M) override;
00055 
00056   using ModulePass::doInitialization;
00057   using ModulePass::doFinalization;
00058 
00059   bool doInitialization(CallGraph &CG);
00060   bool doFinalization(CallGraph &CG);
00061 
00062   /// Pass Manager itself does not invalidate any analysis info.
00063   void getAnalysisUsage(AnalysisUsage &Info) const override {
00064     // CGPassManager walks SCC and it needs CallGraph.
00065     Info.addRequired<CallGraphWrapperPass>();
00066     Info.setPreservesAll();
00067   }
00068 
00069   const char *getPassName() const override {
00070     return "CallGraph Pass Manager";
00071   }
00072 
00073   PMDataManager *getAsPMDataManager() override { return this; }
00074   Pass *getAsPass() override { return this; }
00075 
00076   // Print passes managed by this manager
00077   void dumpPassStructure(unsigned Offset) override {
00078     errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
00079     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00080       Pass *P = getContainedPass(Index);
00081       P->dumpPassStructure(Offset + 1);
00082       dumpLastUses(P, Offset+1);
00083     }
00084   }
00085 
00086   Pass *getContainedPass(unsigned N) {
00087     assert(N < PassVector.size() && "Pass number out of range!");
00088     return static_cast<Pass *>(PassVector[N]);
00089   }
00090 
00091   PassManagerType getPassManagerType() const override {
00092     return PMT_CallGraphPassManager; 
00093   }
00094   
00095 private:
00096   bool RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
00097                          bool &DevirtualizedCall);
00098   
00099   bool RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
00100                     CallGraph &CG, bool &CallGraphUpToDate,
00101                     bool &DevirtualizedCall);
00102   bool RefreshCallGraph(CallGraphSCC &CurSCC, CallGraph &CG,
00103                         bool IsCheckingMode);
00104 };
00105 
00106 } // end anonymous namespace.
00107 
00108 char CGPassManager::ID = 0;
00109 
00110 
00111 bool CGPassManager::RunPassOnSCC(Pass *P, CallGraphSCC &CurSCC,
00112                                  CallGraph &CG, bool &CallGraphUpToDate,
00113                                  bool &DevirtualizedCall) {
00114   bool Changed = false;
00115   PMDataManager *PM = P->getAsPMDataManager();
00116 
00117   if (!PM) {
00118     CallGraphSCCPass *CGSP = (CallGraphSCCPass*)P;
00119     if (!CallGraphUpToDate) {
00120       DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
00121       CallGraphUpToDate = true;
00122     }
00123 
00124     {
00125       TimeRegion PassTimer(getPassTimer(CGSP));
00126       Changed = CGSP->runOnSCC(CurSCC);
00127     }
00128     
00129     // After the CGSCCPass is done, when assertions are enabled, use
00130     // RefreshCallGraph to verify that the callgraph was correctly updated.
00131 #ifndef NDEBUG
00132     if (Changed)
00133       RefreshCallGraph(CurSCC, CG, true);
00134 #endif
00135     
00136     return Changed;
00137   }
00138   
00139   
00140   assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
00141          "Invalid CGPassManager member");
00142   FPPassManager *FPP = (FPPassManager*)P;
00143   
00144   // Run pass P on all functions in the current SCC.
00145   for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
00146        I != E; ++I) {
00147     if (Function *F = (*I)->getFunction()) {
00148       dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
00149       {
00150         TimeRegion PassTimer(getPassTimer(FPP));
00151         Changed |= FPP->runOnFunction(*F);
00152       }
00153       F->getContext().yield();
00154     }
00155   }
00156   
00157   // The function pass(es) modified the IR, they may have clobbered the
00158   // callgraph.
00159   if (Changed && CallGraphUpToDate) {
00160     DEBUG(dbgs() << "CGSCCPASSMGR: Pass Dirtied SCC: "
00161                  << P->getPassName() << '\n');
00162     CallGraphUpToDate = false;
00163   }
00164   return Changed;
00165 }
00166 
00167 
00168 /// RefreshCallGraph - Scan the functions in the specified CFG and resync the
00169 /// callgraph with the call sites found in it.  This is used after
00170 /// FunctionPasses have potentially munged the callgraph, and can be used after
00171 /// CallGraphSCC passes to verify that they correctly updated the callgraph.
00172 ///
00173 /// This function returns true if it devirtualized an existing function call,
00174 /// meaning it turned an indirect call into a direct call.  This happens when
00175 /// a function pass like GVN optimizes away stuff feeding the indirect call.
00176 /// This never happens in checking mode.
00177 ///
00178 bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
00179                                      CallGraph &CG, bool CheckingMode) {
00180   DenseMap<Value*, CallGraphNode*> CallSites;
00181   
00182   DEBUG(dbgs() << "CGSCCPASSMGR: Refreshing SCC with " << CurSCC.size()
00183                << " nodes:\n";
00184         for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
00185              I != E; ++I)
00186           (*I)->dump();
00187         );
00188 
00189   bool MadeChange = false;
00190   bool DevirtualizedCall = false;
00191   
00192   // Scan all functions in the SCC.
00193   unsigned FunctionNo = 0;
00194   for (CallGraphSCC::iterator SCCIdx = CurSCC.begin(), E = CurSCC.end();
00195        SCCIdx != E; ++SCCIdx, ++FunctionNo) {
00196     CallGraphNode *CGN = *SCCIdx;
00197     Function *F = CGN->getFunction();
00198     if (!F || F->isDeclaration()) continue;
00199     
00200     // Walk the function body looking for call sites.  Sync up the call sites in
00201     // CGN with those actually in the function.
00202 
00203     // Keep track of the number of direct and indirect calls that were
00204     // invalidated and removed.
00205     unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
00206     
00207     // Get the set of call sites currently in the function.
00208     for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
00209       // If this call site is null, then the function pass deleted the call
00210       // entirely and the WeakVH nulled it out.  
00211       if (!I->first ||
00212           // If we've already seen this call site, then the FunctionPass RAUW'd
00213           // one call with another, which resulted in two "uses" in the edge
00214           // list of the same call.
00215           CallSites.count(I->first) ||
00216 
00217           // If the call edge is not from a call or invoke, then the function
00218           // pass RAUW'd a call with another value.  This can happen when
00219           // constant folding happens of well known functions etc.
00220           !CallSite(I->first)) {
00221         assert(!CheckingMode &&
00222                "CallGraphSCCPass did not update the CallGraph correctly!");
00223         
00224         // If this was an indirect call site, count it.
00225         if (!I->second->getFunction())
00226           ++NumIndirectRemoved;
00227         else 
00228           ++NumDirectRemoved;
00229         
00230         // Just remove the edge from the set of callees, keep track of whether
00231         // I points to the last element of the vector.
00232         bool WasLast = I + 1 == E;
00233         CGN->removeCallEdge(I);
00234         
00235         // If I pointed to the last element of the vector, we have to bail out:
00236         // iterator checking rejects comparisons of the resultant pointer with
00237         // end.
00238         if (WasLast)
00239           break;
00240         E = CGN->end();
00241         continue;
00242       }
00243       
00244       assert(!CallSites.count(I->first) &&
00245              "Call site occurs in node multiple times");
00246       CallSites.insert(std::make_pair(I->first, I->second));
00247       ++I;
00248     }
00249     
00250     // Loop over all of the instructions in the function, getting the callsites.
00251     // Keep track of the number of direct/indirect calls added.
00252     unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
00253     
00254     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
00255       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
00256         CallSite CS(cast<Value>(I));
00257         if (!CS) continue;
00258         Function *Callee = CS.getCalledFunction();
00259         if (Callee && Callee->isIntrinsic()) continue;
00260         
00261         // If this call site already existed in the callgraph, just verify it
00262         // matches up to expectations and remove it from CallSites.
00263         DenseMap<Value*, CallGraphNode*>::iterator ExistingIt =
00264           CallSites.find(CS.getInstruction());
00265         if (ExistingIt != CallSites.end()) {
00266           CallGraphNode *ExistingNode = ExistingIt->second;
00267 
00268           // Remove from CallSites since we have now seen it.
00269           CallSites.erase(ExistingIt);
00270           
00271           // Verify that the callee is right.
00272           if (ExistingNode->getFunction() == CS.getCalledFunction())
00273             continue;
00274           
00275           // If we are in checking mode, we are not allowed to actually mutate
00276           // the callgraph.  If this is a case where we can infer that the
00277           // callgraph is less precise than it could be (e.g. an indirect call
00278           // site could be turned direct), don't reject it in checking mode, and
00279           // don't tweak it to be more precise.
00280           if (CheckingMode && CS.getCalledFunction() &&
00281               ExistingNode->getFunction() == nullptr)
00282             continue;
00283           
00284           assert(!CheckingMode &&
00285                  "CallGraphSCCPass did not update the CallGraph correctly!");
00286           
00287           // If not, we either went from a direct call to indirect, indirect to
00288           // direct, or direct to different direct.
00289           CallGraphNode *CalleeNode;
00290           if (Function *Callee = CS.getCalledFunction()) {
00291             CalleeNode = CG.getOrInsertFunction(Callee);
00292             // Keep track of whether we turned an indirect call into a direct
00293             // one.
00294             if (!ExistingNode->getFunction()) {
00295               DevirtualizedCall = true;
00296               DEBUG(dbgs() << "  CGSCCPASSMGR: Devirtualized call to '"
00297                            << Callee->getName() << "'\n");
00298             }
00299           } else {
00300             CalleeNode = CG.getCallsExternalNode();
00301           }
00302 
00303           // Update the edge target in CGN.
00304           CGN->replaceCallEdge(CS, CS, CalleeNode);
00305           MadeChange = true;
00306           continue;
00307         }
00308         
00309         assert(!CheckingMode &&
00310                "CallGraphSCCPass did not update the CallGraph correctly!");
00311 
00312         // If the call site didn't exist in the CGN yet, add it.
00313         CallGraphNode *CalleeNode;
00314         if (Function *Callee = CS.getCalledFunction()) {
00315           CalleeNode = CG.getOrInsertFunction(Callee);
00316           ++NumDirectAdded;
00317         } else {
00318           CalleeNode = CG.getCallsExternalNode();
00319           ++NumIndirectAdded;
00320         }
00321         
00322         CGN->addCalledFunction(CS, CalleeNode);
00323         MadeChange = true;
00324       }
00325     
00326     // We scanned the old callgraph node, removing invalidated call sites and
00327     // then added back newly found call sites.  One thing that can happen is
00328     // that an old indirect call site was deleted and replaced with a new direct
00329     // call.  In this case, we have devirtualized a call, and CGSCCPM would like
00330     // to iteratively optimize the new code.  Unfortunately, we don't really
00331     // have a great way to detect when this happens.  As an approximation, we
00332     // just look at whether the number of indirect calls is reduced and the
00333     // number of direct calls is increased.  There are tons of ways to fool this
00334     // (e.g. DCE'ing an indirect call and duplicating an unrelated block with a
00335     // direct call) but this is close enough.
00336     if (NumIndirectRemoved > NumIndirectAdded &&
00337         NumDirectRemoved < NumDirectAdded)
00338       DevirtualizedCall = true;
00339     
00340     // After scanning this function, if we still have entries in callsites, then
00341     // they are dangling pointers.  WeakVH should save us for this, so abort if
00342     // this happens.
00343     assert(CallSites.empty() && "Dangling pointers found in call sites map");
00344     
00345     // Periodically do an explicit clear to remove tombstones when processing
00346     // large scc's.
00347     if ((FunctionNo & 15) == 15)
00348       CallSites.clear();
00349   }
00350 
00351   DEBUG(if (MadeChange) {
00352           dbgs() << "CGSCCPASSMGR: Refreshed SCC is now:\n";
00353           for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
00354             I != E; ++I)
00355               (*I)->dump();
00356           if (DevirtualizedCall)
00357             dbgs() << "CGSCCPASSMGR: Refresh devirtualized a call!\n";
00358 
00359          } else {
00360            dbgs() << "CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
00361          }
00362         );
00363   (void)MadeChange;
00364 
00365   return DevirtualizedCall;
00366 }
00367 
00368 /// RunAllPassesOnSCC -  Execute the body of the entire pass manager on the
00369 /// specified SCC.  This keeps track of whether a function pass devirtualizes
00370 /// any calls and returns it in DevirtualizedCall.
00371 bool CGPassManager::RunAllPassesOnSCC(CallGraphSCC &CurSCC, CallGraph &CG,
00372                                       bool &DevirtualizedCall) {
00373   bool Changed = false;
00374   
00375   // CallGraphUpToDate - Keep track of whether the callgraph is known to be
00376   // up-to-date or not.  The CGSSC pass manager runs two types of passes:
00377   // CallGraphSCC Passes and other random function passes.  Because other
00378   // random function passes are not CallGraph aware, they may clobber the
00379   // call graph by introducing new calls or deleting other ones.  This flag
00380   // is set to false when we run a function pass so that we know to clean up
00381   // the callgraph when we need to run a CGSCCPass again.
00382   bool CallGraphUpToDate = true;
00383 
00384   // Run all passes on current SCC.
00385   for (unsigned PassNo = 0, e = getNumContainedPasses();
00386        PassNo != e; ++PassNo) {
00387     Pass *P = getContainedPass(PassNo);
00388     
00389     // If we're in -debug-pass=Executions mode, construct the SCC node list,
00390     // otherwise avoid constructing this string as it is expensive.
00391     if (isPassDebuggingExecutionsOrMore()) {
00392       std::string Functions;
00393   #ifndef NDEBUG
00394       raw_string_ostream OS(Functions);
00395       for (CallGraphSCC::iterator I = CurSCC.begin(), E = CurSCC.end();
00396            I != E; ++I) {
00397         if (I != CurSCC.begin()) OS << ", ";
00398         (*I)->print(OS);
00399       }
00400       OS.flush();
00401   #endif
00402       dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, Functions);
00403     }
00404     dumpRequiredSet(P);
00405     
00406     initializeAnalysisImpl(P);
00407     
00408     // Actually run this pass on the current SCC.
00409     Changed |= RunPassOnSCC(P, CurSCC, CG,
00410                             CallGraphUpToDate, DevirtualizedCall);
00411     
00412     if (Changed)
00413       dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
00414     dumpPreservedSet(P);
00415     
00416     verifyPreservedAnalysis(P);      
00417     removeNotPreservedAnalysis(P);
00418     recordAvailableAnalysis(P);
00419     removeDeadPasses(P, "", ON_CG_MSG);
00420   }
00421   
00422   // If the callgraph was left out of date (because the last pass run was a
00423   // functionpass), refresh it before we move on to the next SCC.
00424   if (!CallGraphUpToDate)
00425     DevirtualizedCall |= RefreshCallGraph(CurSCC, CG, false);
00426   return Changed;
00427 }
00428 
00429 /// run - Execute all of the passes scheduled for execution.  Keep track of
00430 /// whether any of the passes modifies the module, and if so, return true.
00431 bool CGPassManager::runOnModule(Module &M) {
00432   CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
00433   bool Changed = doInitialization(CG);
00434   
00435   // Walk the callgraph in bottom-up SCC order.
00436   scc_iterator<CallGraph*> CGI = scc_begin(&CG);
00437 
00438   CallGraphSCC CurSCC(&CGI);
00439   while (!CGI.isAtEnd()) {
00440     // Copy the current SCC and increment past it so that the pass can hack
00441     // on the SCC if it wants to without invalidating our iterator.
00442     const std::vector<CallGraphNode *> &NodeVec = *CGI;
00443     CurSCC.initialize(NodeVec.data(), NodeVec.data() + NodeVec.size());
00444     ++CGI;
00445     
00446     // At the top level, we run all the passes in this pass manager on the
00447     // functions in this SCC.  However, we support iterative compilation in the
00448     // case where a function pass devirtualizes a call to a function.  For
00449     // example, it is very common for a function pass (often GVN or instcombine)
00450     // to eliminate the addressing that feeds into a call.  With that improved
00451     // information, we would like the call to be an inline candidate, infer
00452     // mod-ref information etc.
00453     //
00454     // Because of this, we allow iteration up to a specified iteration count.
00455     // This only happens in the case of a devirtualized call, so we only burn
00456     // compile time in the case that we're making progress.  We also have a hard
00457     // iteration count limit in case there is crazy code.
00458     unsigned Iteration = 0;
00459     bool DevirtualizedCall = false;
00460     do {
00461       DEBUG(if (Iteration)
00462               dbgs() << "  SCCPASSMGR: Re-visiting SCC, iteration #"
00463                      << Iteration << '\n');
00464       DevirtualizedCall = false;
00465       Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
00466     } while (Iteration++ < MaxIterations && DevirtualizedCall);
00467     
00468     if (DevirtualizedCall)
00469       DEBUG(dbgs() << "  CGSCCPASSMGR: Stopped iteration after " << Iteration
00470                    << " times, due to -max-cg-scc-iterations\n");
00471     
00472     if (Iteration > MaxSCCIterations)
00473       MaxSCCIterations = Iteration;
00474     
00475   }
00476   Changed |= doFinalization(CG);
00477   return Changed;
00478 }
00479 
00480 
00481 /// Initialize CG
00482 bool CGPassManager::doInitialization(CallGraph &CG) {
00483   bool Changed = false;
00484   for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {  
00485     if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
00486       assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
00487              "Invalid CGPassManager member");
00488       Changed |= ((FPPassManager*)PM)->doInitialization(CG.getModule());
00489     } else {
00490       Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doInitialization(CG);
00491     }
00492   }
00493   return Changed;
00494 }
00495 
00496 /// Finalize CG
00497 bool CGPassManager::doFinalization(CallGraph &CG) {
00498   bool Changed = false;
00499   for (unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {  
00500     if (PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
00501       assert(PM->getPassManagerType() == PMT_FunctionPassManager &&
00502              "Invalid CGPassManager member");
00503       Changed |= ((FPPassManager*)PM)->doFinalization(CG.getModule());
00504     } else {
00505       Changed |= ((CallGraphSCCPass*)getContainedPass(i))->doFinalization(CG);
00506     }
00507   }
00508   return Changed;
00509 }
00510 
00511 //===----------------------------------------------------------------------===//
00512 // CallGraphSCC Implementation
00513 //===----------------------------------------------------------------------===//
00514 
00515 /// ReplaceNode - This informs the SCC and the pass manager that the specified
00516 /// Old node has been deleted, and New is to be used in its place.
00517 void CallGraphSCC::ReplaceNode(CallGraphNode *Old, CallGraphNode *New) {
00518   assert(Old != New && "Should not replace node with self");
00519   for (unsigned i = 0; ; ++i) {
00520     assert(i != Nodes.size() && "Node not in SCC");
00521     if (Nodes[i] != Old) continue;
00522     Nodes[i] = New;
00523     break;
00524   }
00525   
00526   // Update the active scc_iterator so that it doesn't contain dangling
00527   // pointers to the old CallGraphNode.
00528   scc_iterator<CallGraph*> *CGI = (scc_iterator<CallGraph*>*)Context;
00529   CGI->ReplaceNode(Old, New);
00530 }
00531 
00532 
00533 //===----------------------------------------------------------------------===//
00534 // CallGraphSCCPass Implementation
00535 //===----------------------------------------------------------------------===//
00536 
00537 /// Assign pass manager to manage this pass.
00538 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
00539                                          PassManagerType PreferredType) {
00540   // Find CGPassManager 
00541   while (!PMS.empty() &&
00542          PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
00543     PMS.pop();
00544 
00545   assert(!PMS.empty() && "Unable to handle Call Graph Pass");
00546   CGPassManager *CGP;
00547   
00548   if (PMS.top()->getPassManagerType() == PMT_CallGraphPassManager)
00549     CGP = (CGPassManager*)PMS.top();
00550   else {
00551     // Create new Call Graph SCC Pass Manager if it does not exist. 
00552     assert(!PMS.empty() && "Unable to create Call Graph Pass Manager");
00553     PMDataManager *PMD = PMS.top();
00554 
00555     // [1] Create new Call Graph Pass Manager
00556     CGP = new CGPassManager();
00557 
00558     // [2] Set up new manager's top level manager
00559     PMTopLevelManager *TPM = PMD->getTopLevelManager();
00560     TPM->addIndirectPassManager(CGP);
00561 
00562     // [3] Assign manager to manage this new manager. This may create
00563     // and push new managers into PMS
00564     Pass *P = CGP;
00565     TPM->schedulePass(P);
00566 
00567     // [4] Push new manager into PMS
00568     PMS.push(CGP);
00569   }
00570 
00571   CGP->add(this);
00572 }
00573 
00574 /// getAnalysisUsage - For this class, we declare that we require and preserve
00575 /// the call graph.  If the derived class implements this method, it should
00576 /// always explicitly call the implementation here.
00577 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
00578   AU.addRequired<CallGraphWrapperPass>();
00579   AU.addPreserved<CallGraphWrapperPass>();
00580 }
00581 
00582 
00583 //===----------------------------------------------------------------------===//
00584 // PrintCallGraphPass Implementation
00585 //===----------------------------------------------------------------------===//
00586 
00587 namespace {
00588   /// PrintCallGraphPass - Print a Module corresponding to a call graph.
00589   ///
00590   class PrintCallGraphPass : public CallGraphSCCPass {
00591     std::string Banner;
00592     raw_ostream &Out;       // raw_ostream to print on.
00593     
00594   public:
00595     static char ID;
00596     PrintCallGraphPass(const std::string &B, raw_ostream &o)
00597       : CallGraphSCCPass(ID), Banner(B), Out(o) {}
00598 
00599     void getAnalysisUsage(AnalysisUsage &AU) const override {
00600       AU.setPreservesAll();
00601     }
00602 
00603     bool runOnSCC(CallGraphSCC &SCC) override {
00604       Out << Banner;
00605       for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
00606         if ((*I)->getFunction())
00607           (*I)->getFunction()->print(Out);
00608         else
00609           Out << "\nPrinting <null> Function\n";
00610       }
00611       return false;
00612     }
00613   };
00614   
00615 } // end anonymous namespace.
00616 
00617 char PrintCallGraphPass::ID = 0;
00618 
00619 Pass *CallGraphSCCPass::createPrinterPass(raw_ostream &O,
00620                                           const std::string &Banner) const {
00621   return new PrintCallGraphPass(Banner, O);
00622 }
00623