blob: d91a74dd43689d3d97fc4e571329a5141f35d5f2 [file] [log] [blame]
Chris Lattner4a810672003-08-31 01:54:59 +00001//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4a810672003-08-31 01:54:59 +00009//
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 Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/SCCIterator.h"
Devang Patel75f9abf2007-01-17 21:45:01 +000021#include "llvm/PassManagers.h"
Devang Patel95ced112007-02-01 22:38:33 +000022#include "llvm/Function.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnera10df502004-04-20 21:30:06 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Devang Patel75f9abf2007-01-17 21:45:01 +000026//===----------------------------------------------------------------------===//
27// CGPassManager
28//
29/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
30
Dan Gohman844731a2008-05-13 00:00:25 +000031namespace {
32
Devang Patel75f9abf2007-01-17 21:45:01 +000033class CGPassManager : public ModulePass, public PMDataManager {
Devang Patel75f9abf2007-01-17 21:45:01 +000034public:
Devang Patel19974732007-05-03 01:11:54 +000035 static char ID;
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000036 explicit CGPassManager(int Depth)
Dan Gohmanae73dc12008-09-04 17:05:41 +000037 : ModulePass(&ID), PMDataManager(Depth) { }
Devang Patel75f9abf2007-01-17 21:45:01 +000038
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 Wendling905c7e92009-02-11 18:19:24 +000043 bool doInitialization(CallGraph &CG);
44 bool doFinalization(CallGraph &CG);
Devang Patel75f9abf2007-01-17 21:45:01 +000045
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 Patel505f36a2007-02-01 22:09:37 +000053 virtual const char *getPassName() const {
54 return "CallGraph Pass Manager";
55 }
56
Devang Patel75f9abf2007-01-17 21:45:01 +000057 // Print passes managed by this manager
58 void dumpPassStructure(unsigned Offset) {
Chris Lattner45cfe542009-08-23 06:03:38 +000059 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Devang Patel75f9abf2007-01-17 21:45:01 +000060 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 Lattner45cfe542009-08-23 06:03:38 +000068 assert(N < PassVector.size() && "Pass number out of range!");
69 return static_cast<Pass *>(PassVector[N]);
Devang Patel75f9abf2007-01-17 21:45:01 +000070 }
71
Devang Patel84da80d2007-02-27 15:00:39 +000072 virtual PassManagerType getPassManagerType() const {
Devang Patel75f9abf2007-01-17 21:45:01 +000073 return PMT_CallGraphPassManager;
74 }
Chris Lattnerf3a1c152009-08-31 06:01:21 +000075
76private:
77 bool RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC);
Devang Patel75f9abf2007-01-17 21:45:01 +000078};
79
Chris Lattnerf3a1c152009-08-31 06:01:21 +000080} // end anonymous namespace.
Dan Gohman844731a2008-05-13 00:00:25 +000081
Devang Patel19974732007-05-03 01:11:54 +000082char CGPassManager::ID = 0;
Chris Lattnerf3a1c152009-08-31 06:01:21 +000083
84bool 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 Patel75f9abf2007-01-17 21:45:01 +0000110/// 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.
112bool CGPassManager::runOnModule(Module &M) {
113 CallGraph &CG = getAnalysis<CallGraph>();
Bill Wendling905c7e92009-02-11 18:19:24 +0000114 bool Changed = doInitialization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000115
Chris Lattner5095e3d2009-08-31 00:19:58 +0000116 std::vector<CallGraphNode*> CurSCC;
117
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000118 // Walk the callgraph in bottom-up SCC order.
Chris Lattner5095e3d2009-08-31 00:19:58 +0000119 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 Lattnerf3a1c152009-08-31 06:01:21 +0000126
127 // Run all passes on current SCC.
128 for (unsigned PassNo = 0, e = getNumContainedPasses();
129 PassNo != e; ++PassNo) {
130 Pass *P = getContainedPass(PassNo);
Devang Patel75f9abf2007-01-17 21:45:01 +0000131
Devang Patel7f997612007-03-05 20:01:30 +0000132 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000133 dumpRequiredSet(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000134
135 initializeAnalysisImpl(P);
136
Chris Lattnerf3a1c152009-08-31 06:01:21 +0000137 // Actually run this pass on the current SCC.
138 Changed |= RunPassOnSCC(P, CurSCC);
Devang Patel75f9abf2007-01-17 21:45:01 +0000139
140 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000141 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000142 dumpPreservedSet(P);
Devang Patel58e0ef12007-07-19 18:02:32 +0000143
144 verifyPreservedAnalysis(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000145 removeNotPreservedAnalysis(P);
146 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000147 removeDeadPasses(P, "", ON_CG_MSG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000148 }
149 }
Bill Wendling905c7e92009-02-11 18:19:24 +0000150 Changed |= doFinalization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000151 return Changed;
152}
153
154/// Initialize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000155bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000156 bool Changed = false;
157 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
158 Pass *P = getContainedPass(Index);
Nick Lewycky8968a072009-02-13 07:15:53 +0000159 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000160 Changed |= CGSP->doInitialization(CG);
Nick Lewycky8968a072009-02-13 07:15:53 +0000161 } else {
162 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
163 assert (FP && "Invalid CGPassManager member");
164 Changed |= FP->doInitialization(CG.getModule());
165 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000166 }
167 return Changed;
168}
169
170/// Finalize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000171bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000172 bool Changed = false;
173 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
174 Pass *P = getContainedPass(Index);
Nick Lewycky8968a072009-02-13 07:15:53 +0000175 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000176 Changed |= CGSP->doFinalization(CG);
Nick Lewycky8968a072009-02-13 07:15:53 +0000177 } else {
178 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
179 assert (FP && "Invalid CGPassManager member");
180 Changed |= FP->doFinalization(CG.getModule());
181 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000182 }
183 return Changed;
184}
185
Devang Pateld9f10c32007-01-23 21:55:17 +0000186/// Assign pass manager to manage this pass.
Devang Patel97fd2432007-01-23 21:52:35 +0000187void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000188 PassManagerType PreferredType) {
Devang Patel97fd2432007-01-23 21:52:35 +0000189 // Find CGPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000190 while (!PMS.empty() &&
191 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
192 PMS.pop();
Devang Patel97fd2432007-01-23 21:52:35 +0000193
Devang Patel201ebe32008-05-02 22:13:33 +0000194 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
Devang Patel97fd2432007-01-23 21:52:35 +0000195 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 Patel25e681a2007-06-21 22:29:02 +0000213 TPM->schedulePass(P);
Devang Patel97fd2432007-01-23 21:52:35 +0000214
215 // [4] Push new manager into PMS
216 PMS.push(CGP);
217 }
218
219 CGP->add(this);
220}
221
Chris Lattner4a810672003-08-31 01:54:59 +0000222/// 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.
225void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
226 AU.addRequired<CallGraph>();
227 AU.addPreserved<CallGraph>();
228}