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