blob: 3880d0a10bb675c28ec64f76beec36c9f9c0b2ba [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 Lattnera10df502004-04-20 21:30:06 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Devang Patel75f9abf2007-01-17 21:45:01 +000025//===----------------------------------------------------------------------===//
26// CGPassManager
27//
28/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
29
Dan Gohman844731a2008-05-13 00:00:25 +000030namespace {
31
Devang Patel75f9abf2007-01-17 21:45:01 +000032class CGPassManager : public ModulePass, public PMDataManager {
33
34public:
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) {
59 llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n";
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) {
68 assert ( N < PassVector.size() && "Pass number out of range!");
69 Pass *FP = static_cast<Pass *>(PassVector[N]);
70 return FP;
71 }
72
Devang Patel84da80d2007-02-27 15:00:39 +000073 virtual PassManagerType getPassManagerType() const {
Devang Patel75f9abf2007-01-17 21:45:01 +000074 return PMT_CallGraphPassManager;
75 }
76};
77
Dan Gohman844731a2008-05-13 00:00:25 +000078}
79
Devang Patel19974732007-05-03 01:11:54 +000080char CGPassManager::ID = 0;
Devang Patel75f9abf2007-01-17 21:45:01 +000081/// run - Execute all of the passes scheduled for execution. Keep track of
82/// whether any of the passes modifies the module, and if so, return true.
83bool CGPassManager::runOnModule(Module &M) {
84 CallGraph &CG = getAnalysis<CallGraph>();
Bill Wendling905c7e92009-02-11 18:19:24 +000085 bool Changed = doInitialization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +000086
Devang Patel75f9abf2007-01-17 21:45:01 +000087 // Walk SCC
88 for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
89 I != E; ++I) {
90
91 // Run all passes on current SCC
Anton Korobeynikovbed29462007-04-16 18:10:23 +000092 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel75f9abf2007-01-17 21:45:01 +000093 Pass *P = getContainedPass(Index);
Devang Patel75f9abf2007-01-17 21:45:01 +000094
Devang Patel7f997612007-03-05 20:01:30 +000095 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +000096 dumpRequiredSet(P);
Devang Patel75f9abf2007-01-17 21:45:01 +000097
98 initializeAnalysisImpl(P);
99
Devang Patelf5b17fd2007-01-29 23:29:54 +0000100 StartPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000101 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000102 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
Devang Patel75f9abf2007-01-17 21:45:01 +0000103 else {
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000104 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
105 assert (FPP && "Invalid CGPassManager member");
Devang Patel75f9abf2007-01-17 21:45:01 +0000106
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000107 // Run pass P on all functions current SCC
108 std::vector<CallGraphNode*> &SCC = *I;
109 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
110 Function *F = SCC[i]->getFunction();
111 if (F) {
Devang Patel6b4af742007-08-10 18:29:32 +0000112 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getNameStart());
Devang Patel7f997612007-03-05 20:01:30 +0000113 Changed |= FPP->runOnFunction(*F);
Devang Patel95ced112007-02-01 22:38:33 +0000114 }
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000115 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000116 }
Devang Patelf5b17fd2007-01-29 23:29:54 +0000117 StopPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000118
119 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000120 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000121 dumpPreservedSet(P);
Devang Patel58e0ef12007-07-19 18:02:32 +0000122
123 verifyPreservedAnalysis(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000124 removeNotPreservedAnalysis(P);
125 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000126 removeDeadPasses(P, "", ON_CG_MSG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000127 }
128 }
Bill Wendling905c7e92009-02-11 18:19:24 +0000129 Changed |= doFinalization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000130 return Changed;
131}
132
133/// Initialize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000134bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000135 bool Changed = false;
136 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
137 Pass *P = getContainedPass(Index);
Nick Lewycky8968a072009-02-13 07:15:53 +0000138 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000139 Changed |= CGSP->doInitialization(CG);
Nick Lewycky8968a072009-02-13 07:15:53 +0000140 } else {
141 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
142 assert (FP && "Invalid CGPassManager member");
143 Changed |= FP->doInitialization(CG.getModule());
144 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000145 }
146 return Changed;
147}
148
149/// Finalize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000150bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000151 bool Changed = false;
152 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
153 Pass *P = getContainedPass(Index);
Nick Lewycky8968a072009-02-13 07:15:53 +0000154 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000155 Changed |= CGSP->doFinalization(CG);
Nick Lewycky8968a072009-02-13 07:15:53 +0000156 } else {
157 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
158 assert (FP && "Invalid CGPassManager member");
159 Changed |= FP->doFinalization(CG.getModule());
160 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000161 }
162 return Changed;
163}
164
Devang Pateld9f10c32007-01-23 21:55:17 +0000165/// Assign pass manager to manage this pass.
Devang Patel97fd2432007-01-23 21:52:35 +0000166void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000167 PassManagerType PreferredType) {
Devang Patel97fd2432007-01-23 21:52:35 +0000168 // Find CGPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000169 while (!PMS.empty() &&
170 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
171 PMS.pop();
Devang Patel97fd2432007-01-23 21:52:35 +0000172
Devang Patel201ebe32008-05-02 22:13:33 +0000173 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
Devang Patel97fd2432007-01-23 21:52:35 +0000174 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
175
176 // Create new Call Graph SCC Pass Manager if it does not exist.
177 if (!CGP) {
178
179 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
180 PMDataManager *PMD = PMS.top();
181
182 // [1] Create new Call Graph Pass Manager
183 CGP = new CGPassManager(PMD->getDepth() + 1);
184
185 // [2] Set up new manager's top level manager
186 PMTopLevelManager *TPM = PMD->getTopLevelManager();
187 TPM->addIndirectPassManager(CGP);
188
189 // [3] Assign manager to manage this new manager. This may create
190 // and push new managers into PMS
191 Pass *P = dynamic_cast<Pass *>(CGP);
Devang Patel25e681a2007-06-21 22:29:02 +0000192 TPM->schedulePass(P);
Devang Patel97fd2432007-01-23 21:52:35 +0000193
194 // [4] Push new manager into PMS
195 PMS.push(CGP);
196 }
197
198 CGP->add(this);
199}
200
Chris Lattner4a810672003-08-31 01:54:59 +0000201/// getAnalysisUsage - For this class, we declare that we require and preserve
202/// the call graph. If the derived class implements this method, it should
203/// always explicitly call the implementation here.
204void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
205 AU.addRequired<CallGraph>();
206 AU.addPreserved<CallGraph>();
207}