Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 1 | //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 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/CallGraphSCCPass.h" |
| 19 | #include "llvm/Analysis/CallGraph.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SCCIterator.h" |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 21 | #include "llvm/PassManagers.h" |
Devang Patel | 95ced11 | 2007-02-01 22:38:33 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | a10df50 | 2004-04-20 21:30:06 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // CGPassManager |
| 28 | // |
| 29 | /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses. |
| 30 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 33 | class CGPassManager : public ModulePass, public PMDataManager { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 34 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 35 | static char ID; |
Dan Gohman | c2bbfc1 | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 36 | explicit CGPassManager(int Depth) |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 37 | : ModulePass(&ID), PMDataManager(Depth) { } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 38 | |
| 39 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 40 | /// whether any of the passes modifies the module, and if so, return true. |
| 41 | bool runOnModule(Module &M); |
| 42 | |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 43 | bool doInitialization(CallGraph &CG); |
| 44 | bool doFinalization(CallGraph &CG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 45 | |
| 46 | /// Pass Manager itself does not invalidate any analysis info. |
| 47 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 48 | // CGPassManager walks SCC and it needs CallGraph. |
| 49 | Info.addRequired<CallGraph>(); |
| 50 | Info.setPreservesAll(); |
| 51 | } |
| 52 | |
Devang Patel | 505f36a | 2007-02-01 22:09:37 +0000 | [diff] [blame] | 53 | virtual const char *getPassName() const { |
| 54 | return "CallGraph Pass Manager"; |
| 55 | } |
| 56 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 57 | // Print passes managed by this manager |
| 58 | void dumpPassStructure(unsigned Offset) { |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 59 | errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n"; |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 60 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 61 | Pass *P = getContainedPass(Index); |
| 62 | P->dumpPassStructure(Offset + 1); |
| 63 | dumpLastUses(P, Offset+1); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | Pass *getContainedPass(unsigned N) { |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 68 | assert(N < PassVector.size() && "Pass number out of range!"); |
| 69 | return static_cast<Pass *>(PassVector[N]); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Devang Patel | 84da80d | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 72 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 73 | return PMT_CallGraphPassManager; |
| 74 | } |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame^] | 75 | |
| 76 | private: |
| 77 | bool RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame^] | 80 | } // end anonymous namespace. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 81 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 82 | char CGPassManager::ID = 0; |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame^] | 83 | |
| 84 | bool CGPassManager::RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC) { |
| 85 | bool Changed = false; |
| 86 | if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass*>(P)) { |
| 87 | StartPassTimer(P); |
| 88 | Changed = CGSP->runOnSCC(CurSCC); |
| 89 | StopPassTimer(P); |
| 90 | return Changed; |
| 91 | } |
| 92 | |
| 93 | StartPassTimer(P); |
| 94 | FPPassManager *FPP = dynamic_cast<FPPassManager *>(P); |
| 95 | assert(FPP && "Invalid CGPassManager member"); |
| 96 | |
| 97 | // Run pass P on all functions in the current SCC. |
| 98 | for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) { |
| 99 | if (Function *F = CurSCC[i]->getFunction()) { |
| 100 | dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); |
| 101 | Changed |= FPP->runOnFunction(*F); |
| 102 | } |
| 103 | } |
| 104 | StopPassTimer(P); |
| 105 | |
| 106 | return Changed; |
| 107 | } |
| 108 | |
| 109 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 110 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 111 | /// whether any of the passes modifies the module, and if so, return true. |
| 112 | bool CGPassManager::runOnModule(Module &M) { |
| 113 | CallGraph &CG = getAnalysis<CallGraph>(); |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 114 | bool Changed = doInitialization(CG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 5095e3d | 2009-08-31 00:19:58 +0000 | [diff] [blame] | 116 | std::vector<CallGraphNode*> CurSCC; |
| 117 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame^] | 118 | // Walk the callgraph in bottom-up SCC order. |
Chris Lattner | 5095e3d | 2009-08-31 00:19:58 +0000 | [diff] [blame] | 119 | for (scc_iterator<CallGraph*> CGI = scc_begin(&CG), E = scc_end(&CG); |
| 120 | CGI != E;) { |
| 121 | // Copy the current SCC and increment past it so that the pass can hack |
| 122 | // on the SCC if it wants to without invalidating our iterator. |
| 123 | CurSCC = *CGI; |
| 124 | ++CGI; |
| 125 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame^] | 126 | |
| 127 | // Run all passes on current SCC. |
| 128 | for (unsigned PassNo = 0, e = getNumContainedPasses(); |
| 129 | PassNo != e; ++PassNo) { |
| 130 | Pass *P = getContainedPass(PassNo); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 131 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 132 | dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, ""); |
Chris Lattner | 0dabb7e | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 133 | dumpRequiredSet(P); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 134 | |
| 135 | initializeAnalysisImpl(P); |
| 136 | |
Chris Lattner | f3a1c15 | 2009-08-31 06:01:21 +0000 | [diff] [blame^] | 137 | // Actually run this pass on the current SCC. |
| 138 | Changed |= RunPassOnSCC(P, CurSCC); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 139 | |
| 140 | if (Changed) |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 141 | dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); |
Chris Lattner | 0dabb7e | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 142 | dumpPreservedSet(P); |
Devang Patel | 58e0ef1 | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 143 | |
| 144 | verifyPreservedAnalysis(P); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 145 | removeNotPreservedAnalysis(P); |
| 146 | recordAvailableAnalysis(P); |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 147 | removeDeadPasses(P, "", ON_CG_MSG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 150 | Changed |= doFinalization(CG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 151 | return Changed; |
| 152 | } |
| 153 | |
| 154 | /// Initialize CG |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 155 | bool CGPassManager::doInitialization(CallGraph &CG) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 156 | bool Changed = false; |
| 157 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 158 | Pass *P = getContainedPass(Index); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 159 | if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 160 | Changed |= CGSP->doInitialization(CG); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 161 | } else { |
| 162 | FPPassManager *FP = dynamic_cast<FPPassManager *>(P); |
| 163 | assert (FP && "Invalid CGPassManager member"); |
| 164 | Changed |= FP->doInitialization(CG.getModule()); |
| 165 | } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 166 | } |
| 167 | return Changed; |
| 168 | } |
| 169 | |
| 170 | /// Finalize CG |
Bill Wendling | 905c7e9 | 2009-02-11 18:19:24 +0000 | [diff] [blame] | 171 | bool CGPassManager::doFinalization(CallGraph &CG) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 172 | bool Changed = false; |
| 173 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 174 | Pass *P = getContainedPass(Index); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 175 | if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 176 | Changed |= CGSP->doFinalization(CG); |
Nick Lewycky | 8968a07 | 2009-02-13 07:15:53 +0000 | [diff] [blame] | 177 | } else { |
| 178 | FPPassManager *FP = dynamic_cast<FPPassManager *>(P); |
| 179 | assert (FP && "Invalid CGPassManager member"); |
| 180 | Changed |= FP->doFinalization(CG.getModule()); |
| 181 | } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 182 | } |
| 183 | return Changed; |
| 184 | } |
| 185 | |
Devang Patel | d9f10c3 | 2007-01-23 21:55:17 +0000 | [diff] [blame] | 186 | /// Assign pass manager to manage this pass. |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 187 | void CallGraphSCCPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 188 | PassManagerType PreferredType) { |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 189 | // Find CGPassManager |
Duncan Sands | 20d824b | 2007-07-19 09:42:01 +0000 | [diff] [blame] | 190 | while (!PMS.empty() && |
| 191 | PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) |
| 192 | PMS.pop(); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 193 | |
Devang Patel | 201ebe3 | 2008-05-02 22:13:33 +0000 | [diff] [blame] | 194 | assert (!PMS.empty() && "Unable to handle Call Graph Pass"); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 195 | CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top()); |
| 196 | |
| 197 | // Create new Call Graph SCC Pass Manager if it does not exist. |
| 198 | if (!CGP) { |
| 199 | |
| 200 | assert (!PMS.empty() && "Unable to create Call Graph Pass Manager"); |
| 201 | PMDataManager *PMD = PMS.top(); |
| 202 | |
| 203 | // [1] Create new Call Graph Pass Manager |
| 204 | CGP = new CGPassManager(PMD->getDepth() + 1); |
| 205 | |
| 206 | // [2] Set up new manager's top level manager |
| 207 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 208 | TPM->addIndirectPassManager(CGP); |
| 209 | |
| 210 | // [3] Assign manager to manage this new manager. This may create |
| 211 | // and push new managers into PMS |
| 212 | Pass *P = dynamic_cast<Pass *>(CGP); |
Devang Patel | 25e681a | 2007-06-21 22:29:02 +0000 | [diff] [blame] | 213 | TPM->schedulePass(P); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 214 | |
| 215 | // [4] Push new manager into PMS |
| 216 | PMS.push(CGP); |
| 217 | } |
| 218 | |
| 219 | CGP->add(this); |
| 220 | } |
| 221 | |
Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 222 | /// getAnalysisUsage - For this class, we declare that we require and preserve |
| 223 | /// the call graph. If the derived class implements this method, it should |
| 224 | /// always explicitly call the implementation here. |
| 225 | void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 226 | AU.addRequired<CallGraph>(); |
| 227 | AU.addPreserved<CallGraph>(); |
| 228 | } |