blob: c317a112f5a6d6c0a8def8a860c54d6341ea89fd [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//
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 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:
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 Patel505f36a2007-02-01 22:09:37 +000049 virtual const char *getPassName() const {
50 return "CallGraph Pass Manager";
51 }
52
Devang Patel75f9abf2007-01-17 21:45:01 +000053 // 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 Patel84da80d2007-02-27 15:00:39 +000069 virtual PassManagerType getPassManagerType() const {
Devang Patel75f9abf2007-01-17 21:45:01 +000070 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.
76bool CGPassManager::runOnModule(Module &M) {
77 CallGraph &CG = getAnalysis<CallGraph>();
78 bool Changed = doInitialization(CG);
79
80 std::string Msg1 = "Executing Pass '";
81 std::string Msg3 = "' Made Modification '";
82
83 // 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
88 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
89
90 Pass *P = getContainedPass(Index);
91 AnalysisUsage AnUsage;
92 P->getAnalysisUsage(AnUsage);
93
94 std::string Msg2 = "' on Call Graph ...\n'";
95 dumpPassInfo(P, Msg1, Msg2);
96 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
97
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))
102 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
103 else {
104 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
105 assert (FPP && "Invalid CGPassManager member");
106
107 // 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();
Devang Patel95ced112007-02-01 22:38:33 +0000111 if (F) {
112 std::string Msg4 = "' on Function '" + F->getName() + "'...\n";
113 dumpPassInfo(P, Msg1, Msg4);
Devang Patel75f9abf2007-01-17 21:45:01 +0000114 Changed |= FPP->runOnFunction(*F);
Devang Patel95ced112007-02-01 22:38:33 +0000115 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000116 }
117 }
Devang Patelf5b17fd2007-01-29 23:29:54 +0000118 StopPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000119
120 if (Changed)
121 dumpPassInfo(P, Msg3, Msg2);
122 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
123
124 removeNotPreservedAnalysis(P);
125 recordAvailableAnalysis(P);
126 removeDeadPasses(P, Msg2);
127 }
128 }
129 Changed |= doFinalization(CG);
130 return Changed;
131}
132
133/// Initialize CG
134bool CGPassManager::doInitialization(CallGraph &CG) {
135 bool Changed = false;
136 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
137 Pass *P = getContainedPass(Index);
138 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
139 Changed |= CGSP->doInitialization(CG);
140 }
141 return Changed;
142}
143
144/// Finalize CG
145bool CGPassManager::doFinalization(CallGraph &CG) {
146 bool Changed = false;
147 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
148 Pass *P = getContainedPass(Index);
149 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
150 Changed |= CGSP->doFinalization(CG);
151 }
152 return Changed;
153}
154
Devang Pateld9f10c32007-01-23 21:55:17 +0000155/// Assign pass manager to manage this pass.
Devang Patel97fd2432007-01-23 21:52:35 +0000156void CallGraphSCCPass::assignPassManager(PMStack &PMS,
157 PassManagerType PreferredType) {
158 // Find CGPassManager
159 while (!PMS.empty()) {
160 if (PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
161 PMS.pop();
162 else;
163 break;
164 }
165
166 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);
184 P->assignPassManager(PMS);
185
186 // [4] Push new manager into PMS
187 PMS.push(CGP);
188 }
189
190 CGP->add(this);
191}
192
Chris Lattner4a810672003-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}