blob: 90e26266f8ba886bbd3b82d3da81995034c545af [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
30class CGPassManager : public ModulePass, public PMDataManager {
31
32public:
Devang Patel19974732007-05-03 01:11:54 +000033 static char ID;
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000034 explicit CGPassManager(int Depth)
Devang Patel794fd752007-05-01 21:15:47 +000035 : ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
Devang Patel75f9abf2007-01-17 21:45:01 +000036
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 Patel505f36a2007-02-01 22:09:37 +000051 virtual const char *getPassName() const {
52 return "CallGraph Pass Manager";
53 }
54
Devang Patel75f9abf2007-01-17 21:45:01 +000055 // 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 Patel84da80d2007-02-27 15:00:39 +000071 virtual PassManagerType getPassManagerType() const {
Devang Patel75f9abf2007-01-17 21:45:01 +000072 return PMT_CallGraphPassManager;
73 }
74};
75
Devang Patel19974732007-05-03 01:11:54 +000076char CGPassManager::ID = 0;
Devang Patel75f9abf2007-01-17 21:45:01 +000077/// 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.
79bool CGPassManager::runOnModule(Module &M) {
80 CallGraph &CG = getAnalysis<CallGraph>();
81 bool Changed = doInitialization(CG);
82
Devang Patel75f9abf2007-01-17 21:45:01 +000083 // 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 Korobeynikovbed29462007-04-16 18:10:23 +000088 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel75f9abf2007-01-17 21:45:01 +000089 Pass *P = getContainedPass(Index);
90 AnalysisUsage AnUsage;
91 P->getAnalysisUsage(AnUsage);
92
Devang Patel7f997612007-03-05 20:01:30 +000093 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
Devang Patel75f9abf2007-01-17 21:45:01 +000094 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
95
96 initializeAnalysisImpl(P);
97
Devang Patelf5b17fd2007-01-29 23:29:54 +000098 StartPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +000099 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000100 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
Devang Patel75f9abf2007-01-17 21:45:01 +0000101 else {
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000102 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
103 assert (FPP && "Invalid CGPassManager member");
Devang Patel75f9abf2007-01-17 21:45:01 +0000104
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000105 // 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 Patel6b4af742007-08-10 18:29:32 +0000110 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getNameStart());
Devang Patel7f997612007-03-05 20:01:30 +0000111 Changed |= FPP->runOnFunction(*F);
Devang Patel95ced112007-02-01 22:38:33 +0000112 }
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000113 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000114 }
Devang Patelf5b17fd2007-01-29 23:29:54 +0000115 StopPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000116
117 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000118 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
Devang Patel75f9abf2007-01-17 21:45:01 +0000119 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patel58e0ef12007-07-19 18:02:32 +0000120
121 verifyPreservedAnalysis(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000122 removeNotPreservedAnalysis(P);
123 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000124 removeDeadPasses(P, "", ON_CG_MSG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000125 }
126 }
127 Changed |= doFinalization(CG);
128 return Changed;
129}
130
131/// Initialize CG
132bool CGPassManager::doInitialization(CallGraph &CG) {
133 bool Changed = false;
134 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
135 Pass *P = getContainedPass(Index);
136 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
137 Changed |= CGSP->doInitialization(CG);
138 }
139 return Changed;
140}
141
142/// Finalize CG
143bool CGPassManager::doFinalization(CallGraph &CG) {
144 bool Changed = false;
145 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
146 Pass *P = getContainedPass(Index);
147 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
148 Changed |= CGSP->doFinalization(CG);
149 }
150 return Changed;
151}
152
Devang Pateld9f10c32007-01-23 21:55:17 +0000153/// Assign pass manager to manage this pass.
Devang Patel97fd2432007-01-23 21:52:35 +0000154void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000155 PassManagerType PreferredType) {
Devang Patel97fd2432007-01-23 21:52:35 +0000156 // Find CGPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000157 while (!PMS.empty() &&
158 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
159 PMS.pop();
Devang Patel97fd2432007-01-23 21:52:35 +0000160
161 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
162
163 // Create new Call Graph SCC Pass Manager if it does not exist.
164 if (!CGP) {
165
166 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
167 PMDataManager *PMD = PMS.top();
168
169 // [1] Create new Call Graph Pass Manager
170 CGP = new CGPassManager(PMD->getDepth() + 1);
171
172 // [2] Set up new manager's top level manager
173 PMTopLevelManager *TPM = PMD->getTopLevelManager();
174 TPM->addIndirectPassManager(CGP);
175
176 // [3] Assign manager to manage this new manager. This may create
177 // and push new managers into PMS
178 Pass *P = dynamic_cast<Pass *>(CGP);
Devang Patel25e681a2007-06-21 22:29:02 +0000179 TPM->schedulePass(P);
Devang Patel97fd2432007-01-23 21:52:35 +0000180
181 // [4] Push new manager into PMS
182 PMS.push(CGP);
183 }
184
185 CGP->add(this);
186}
187
Chris Lattner4a810672003-08-31 01:54:59 +0000188/// getAnalysisUsage - For this class, we declare that we require and preserve
189/// the call graph. If the derived class implements this method, it should
190/// always explicitly call the implementation here.
191void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
192 AU.addRequired<CallGraph>();
193 AU.addPreserved<CallGraph>();
194}