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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | a10df50 | 2004-04-20 21:30:06 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // CGPassManager |
| 27 | // |
| 28 | /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses. |
| 29 | |
| 30 | class CGPassManager : public ModulePass, public PMDataManager { |
| 31 | |
| 32 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 33 | static char ID; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 34 | CGPassManager(int Depth) |
| 35 | : ModulePass((intptr_t)&ID), PMDataManager(Depth) { } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 36 | |
| 37 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 38 | /// whether any of the passes modifies the module, and if so, return true. |
| 39 | bool runOnModule(Module &M); |
| 40 | |
| 41 | bool doInitialization(CallGraph &CG); |
| 42 | bool doFinalization(CallGraph &CG); |
| 43 | |
| 44 | /// Pass Manager itself does not invalidate any analysis info. |
| 45 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 46 | // CGPassManager walks SCC and it needs CallGraph. |
| 47 | Info.addRequired<CallGraph>(); |
| 48 | Info.setPreservesAll(); |
| 49 | } |
| 50 | |
Devang Patel | 505f36a | 2007-02-01 22:09:37 +0000 | [diff] [blame] | 51 | virtual const char *getPassName() const { |
| 52 | return "CallGraph Pass Manager"; |
| 53 | } |
| 54 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 55 | // Print passes managed by this manager |
| 56 | void dumpPassStructure(unsigned Offset) { |
| 57 | llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n"; |
| 58 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 59 | Pass *P = getContainedPass(Index); |
| 60 | P->dumpPassStructure(Offset + 1); |
| 61 | dumpLastUses(P, Offset+1); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Pass *getContainedPass(unsigned N) { |
| 66 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 67 | Pass *FP = static_cast<Pass *>(PassVector[N]); |
| 68 | return FP; |
| 69 | } |
| 70 | |
Devang Patel | 84da80d | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 71 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 72 | return PMT_CallGraphPassManager; |
| 73 | } |
| 74 | }; |
| 75 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 76 | char CGPassManager::ID = 0; |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 77 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 78 | /// whether any of the passes modifies the module, and if so, return true. |
| 79 | bool CGPassManager::runOnModule(Module &M) { |
| 80 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 81 | bool Changed = doInitialization(CG); |
| 82 | |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 83 | // Walk SCC |
| 84 | for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG); |
| 85 | I != E; ++I) { |
| 86 | |
| 87 | // Run all passes on current SCC |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 88 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 89 | Pass *P = getContainedPass(Index); |
| 90 | AnalysisUsage AnUsage; |
| 91 | P->getAnalysisUsage(AnUsage); |
| 92 | |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 93 | dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, ""); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 94 | dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet()); |
| 95 | |
| 96 | initializeAnalysisImpl(P); |
| 97 | |
Devang Patel | f5b17fd | 2007-01-29 23:29:54 +0000 | [diff] [blame] | 98 | StartPassTimer(P); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 99 | if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 100 | Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ? |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 101 | else { |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 102 | FPPassManager *FPP = dynamic_cast<FPPassManager *>(P); |
| 103 | assert (FPP && "Invalid CGPassManager member"); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 104 | |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 105 | // Run pass P on all functions current SCC |
| 106 | std::vector<CallGraphNode*> &SCC = *I; |
| 107 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 108 | Function *F = SCC[i]->getFunction(); |
| 109 | if (F) { |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 110 | dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName()); |
| 111 | Changed |= FPP->runOnFunction(*F); |
Devang Patel | 95ced11 | 2007-02-01 22:38:33 +0000 | [diff] [blame] | 112 | } |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 113 | } |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 114 | } |
Devang Patel | f5b17fd | 2007-01-29 23:29:54 +0000 | [diff] [blame] | 115 | StopPassTimer(P); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 116 | |
| 117 | if (Changed) |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 118 | dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, ""); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 119 | dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet()); |
| 120 | |
| 121 | removeNotPreservedAnalysis(P); |
| 122 | recordAvailableAnalysis(P); |
Devang Patel | 7f99761 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 123 | removeDeadPasses(P, "", ON_CG_MSG); |
Devang Patel | 75f9abf | 2007-01-17 21:45:01 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | Changed |= doFinalization(CG); |
| 127 | return Changed; |
| 128 | } |
| 129 | |
| 130 | /// Initialize CG |
| 131 | bool CGPassManager::doInitialization(CallGraph &CG) { |
| 132 | bool Changed = false; |
| 133 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 134 | Pass *P = getContainedPass(Index); |
| 135 | if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) |
| 136 | Changed |= CGSP->doInitialization(CG); |
| 137 | } |
| 138 | return Changed; |
| 139 | } |
| 140 | |
| 141 | /// Finalize CG |
| 142 | bool CGPassManager::doFinalization(CallGraph &CG) { |
| 143 | bool Changed = false; |
| 144 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 145 | Pass *P = getContainedPass(Index); |
| 146 | if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) |
| 147 | Changed |= CGSP->doFinalization(CG); |
| 148 | } |
| 149 | return Changed; |
| 150 | } |
| 151 | |
Devang Patel | d9f10c3 | 2007-01-23 21:55:17 +0000 | [diff] [blame] | 152 | /// Assign pass manager to manage this pass. |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 153 | void CallGraphSCCPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | bed2946 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 154 | PassManagerType PreferredType) { |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 155 | // Find CGPassManager |
| 156 | while (!PMS.empty()) { |
| 157 | if (PMS.top()->getPassManagerType() > PMT_CallGraphPassManager) |
| 158 | PMS.pop(); |
| 159 | else; |
| 160 | break; |
| 161 | } |
| 162 | |
| 163 | CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top()); |
| 164 | |
| 165 | // Create new Call Graph SCC Pass Manager if it does not exist. |
| 166 | if (!CGP) { |
| 167 | |
| 168 | assert (!PMS.empty() && "Unable to create Call Graph Pass Manager"); |
| 169 | PMDataManager *PMD = PMS.top(); |
| 170 | |
| 171 | // [1] Create new Call Graph Pass Manager |
| 172 | CGP = new CGPassManager(PMD->getDepth() + 1); |
| 173 | |
| 174 | // [2] Set up new manager's top level manager |
| 175 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 176 | TPM->addIndirectPassManager(CGP); |
| 177 | |
| 178 | // [3] Assign manager to manage this new manager. This may create |
| 179 | // and push new managers into PMS |
| 180 | Pass *P = dynamic_cast<Pass *>(CGP); |
Devang Patel | 25e681a | 2007-06-21 22:29:02 +0000 | [diff] [blame^] | 181 | TPM->schedulePass(P); |
Devang Patel | 97fd243 | 2007-01-23 21:52:35 +0000 | [diff] [blame] | 182 | |
| 183 | // [4] Push new manager into PMS |
| 184 | PMS.push(CGP); |
| 185 | } |
| 186 | |
| 187 | CGP->add(this); |
| 188 | } |
| 189 | |
Chris Lattner | 4a81067 | 2003-08-31 01:54:59 +0000 | [diff] [blame] | 190 | /// getAnalysisUsage - For this class, we declare that we require and preserve |
| 191 | /// the call graph. If the derived class implements this method, it should |
| 192 | /// always explicitly call the implementation here. |
| 193 | void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 194 | AU.addRequired<CallGraph>(); |
| 195 | AU.addPreserved<CallGraph>(); |
| 196 | } |