blob: 112f9c2e5df747891685e3a66078c116e8f4fa21 [file] [log] [blame]
Chris Lattnerf7e95942003-08-31 01:54:59 +00001//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf7e95942003-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 Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/SCCIterator.h"
Devang Patel48537a02007-01-17 21:45:01 +000021#include "llvm/PassManagers.h"
Devang Patelb8a29bd2007-02-01 22:38:33 +000022#include "llvm/Function.h"
Chris Lattner8d083812004-04-20 21:30:06 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Devang Patel48537a02007-01-17 21:45:01 +000025//===----------------------------------------------------------------------===//
26// CGPassManager
27//
28/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
29
Dan Gohmand78c4002008-05-13 00:00:25 +000030namespace {
31
Devang Patel48537a02007-01-17 21:45:01 +000032class CGPassManager : public ModulePass, public PMDataManager {
33
34public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000035 static char ID;
Dan Gohman34d442f2007-08-01 15:32:29 +000036 explicit CGPassManager(int Depth)
Devang Patel09f162c2007-05-01 21:15:47 +000037 : ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel48537a02007-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
43 bool doInitialization(CallGraph &CG);
44 bool doFinalization(CallGraph &CG);
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 Patelf7fea8a2007-02-01 22:09:37 +000053 virtual const char *getPassName() const {
54 return "CallGraph Pass Manager";
55 }
56
Devang Patel48537a02007-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 Patel28349ab2007-02-27 15:00:39 +000073 virtual PassManagerType getPassManagerType() const {
Devang Patel48537a02007-01-17 21:45:01 +000074 return PMT_CallGraphPassManager;
75 }
76};
77
Dan Gohmand78c4002008-05-13 00:00:25 +000078}
79
Devang Patel8c78a0b2007-05-03 01:11:54 +000080char CGPassManager::ID = 0;
Devang Patel48537a02007-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>();
85 bool Changed = doInitialization(CG);
86
Devang Patel48537a02007-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 Korobeynikovfb801512007-04-16 18:10:23 +000092 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel48537a02007-01-17 21:45:01 +000093 Pass *P = getContainedPass(Index);
94 AnalysisUsage AnUsage;
95 P->getAnalysisUsage(AnUsage);
96
Devang Patel003a5592007-03-05 20:01:30 +000097 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
Devang Patel48537a02007-01-17 21:45:01 +000098 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
99
100 initializeAnalysisImpl(P);
101
Devang Patelb64c4332007-01-29 23:29:54 +0000102 StartPassTimer(P);
Devang Patel48537a02007-01-17 21:45:01 +0000103 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000104 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
Devang Patel48537a02007-01-17 21:45:01 +0000105 else {
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000106 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
107 assert (FPP && "Invalid CGPassManager member");
Devang Patel48537a02007-01-17 21:45:01 +0000108
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000109 // Run pass P on all functions current SCC
110 std::vector<CallGraphNode*> &SCC = *I;
111 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
112 Function *F = SCC[i]->getFunction();
113 if (F) {
Devang Pateld305c402007-08-10 18:29:32 +0000114 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getNameStart());
Devang Patel003a5592007-03-05 20:01:30 +0000115 Changed |= FPP->runOnFunction(*F);
Devang Patelb8a29bd2007-02-01 22:38:33 +0000116 }
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000117 }
Devang Patel48537a02007-01-17 21:45:01 +0000118 }
Devang Patelb64c4332007-01-29 23:29:54 +0000119 StopPassTimer(P);
Devang Patel48537a02007-01-17 21:45:01 +0000120
121 if (Changed)
Devang Patel003a5592007-03-05 20:01:30 +0000122 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
Devang Patel48537a02007-01-17 21:45:01 +0000123 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patela273d1c2007-07-19 18:02:32 +0000124
125 verifyPreservedAnalysis(P);
Devang Patel48537a02007-01-17 21:45:01 +0000126 removeNotPreservedAnalysis(P);
127 recordAvailableAnalysis(P);
Devang Patel003a5592007-03-05 20:01:30 +0000128 removeDeadPasses(P, "", ON_CG_MSG);
Devang Patel48537a02007-01-17 21:45:01 +0000129 }
130 }
131 Changed |= doFinalization(CG);
132 return Changed;
133}
134
135/// Initialize CG
136bool CGPassManager::doInitialization(CallGraph &CG) {
137 bool Changed = false;
138 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
139 Pass *P = getContainedPass(Index);
140 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
141 Changed |= CGSP->doInitialization(CG);
142 }
143 return Changed;
144}
145
146/// Finalize CG
147bool CGPassManager::doFinalization(CallGraph &CG) {
148 bool Changed = false;
149 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
150 Pass *P = getContainedPass(Index);
151 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
152 Changed |= CGSP->doFinalization(CG);
153 }
154 return Changed;
155}
156
Devang Patel020f4f22007-01-23 21:55:17 +0000157/// Assign pass manager to manage this pass.
Devang Patel1f8200b2007-01-23 21:52:35 +0000158void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +0000159 PassManagerType PreferredType) {
Devang Patel1f8200b2007-01-23 21:52:35 +0000160 // Find CGPassManager
Duncan Sands60f28bf2007-07-19 09:42:01 +0000161 while (!PMS.empty() &&
162 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
163 PMS.pop();
Devang Patel1f8200b2007-01-23 21:52:35 +0000164
Devang Patelab8cace2008-05-02 22:13:33 +0000165 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
Devang Patel1f8200b2007-01-23 21:52:35 +0000166 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
167
168 // Create new Call Graph SCC Pass Manager if it does not exist.
169 if (!CGP) {
170
171 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
172 PMDataManager *PMD = PMS.top();
173
174 // [1] Create new Call Graph Pass Manager
175 CGP = new CGPassManager(PMD->getDepth() + 1);
176
177 // [2] Set up new manager's top level manager
178 PMTopLevelManager *TPM = PMD->getTopLevelManager();
179 TPM->addIndirectPassManager(CGP);
180
181 // [3] Assign manager to manage this new manager. This may create
182 // and push new managers into PMS
183 Pass *P = dynamic_cast<Pass *>(CGP);
Devang Patel703de8f2007-06-21 22:29:02 +0000184 TPM->schedulePass(P);
Devang Patel1f8200b2007-01-23 21:52:35 +0000185
186 // [4] Push new manager into PMS
187 PMS.push(CGP);
188 }
189
190 CGP->add(this);
191}
192
Chris Lattnerf7e95942003-08-31 01:54:59 +0000193/// getAnalysisUsage - For this class, we declare that we require and preserve
194/// the call graph. If the derived class implements this method, it should
195/// always explicitly call the implementation here.
196void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
197 AU.addRequired<CallGraph>();
198 AU.addPreserved<CallGraph>();
199}