blob: 112f9c2e5df747891685e3a66078c116e8f4fa21 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
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"
20#include "llvm/ADT/SCCIterator.h"
21#include "llvm/PassManagers.h"
22#include "llvm/Function.h"
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// CGPassManager
27//
28/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
29
Dan Gohman089efff2008-05-13 00:00:25 +000030namespace {
31
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032class CGPassManager : public ModulePass, public PMDataManager {
33
34public:
35 static char ID;
Dan Gohman34c280e2007-08-01 15:32:29 +000036 explicit CGPassManager(int Depth)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037 : ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
38
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
53 virtual const char *getPassName() const {
54 return "CallGraph Pass Manager";
55 }
56
57 // 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
73 virtual PassManagerType getPassManagerType() const {
74 return PMT_CallGraphPassManager;
75 }
76};
77
Dan Gohman089efff2008-05-13 00:00:25 +000078}
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080char CGPassManager::ID = 0;
81/// 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
87 // 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
92 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
93 Pass *P = getContainedPass(Index);
94 AnalysisUsage AnUsage;
95 P->getAnalysisUsage(AnUsage);
96
97 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
98 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
99
100 initializeAnalysisImpl(P);
101
102 StartPassTimer(P);
103 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
104 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
105 else {
106 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
107 assert (FPP && "Invalid CGPassManager member");
108
109 // 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 Patel378b4ff2007-08-10 18:29:32 +0000114 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getNameStart());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 Changed |= FPP->runOnFunction(*F);
116 }
117 }
118 }
119 StopPassTimer(P);
120
121 if (Changed)
122 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
123 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
Devang Patela5eb9a32007-07-19 18:02:32 +0000124
125 verifyPreservedAnalysis(P);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 removeNotPreservedAnalysis(P);
127 recordAvailableAnalysis(P);
128 removeDeadPasses(P, "", ON_CG_MSG);
129 }
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
157/// Assign pass manager to manage this pass.
158void CallGraphSCCPass::assignPassManager(PMStack &PMS,
159 PassManagerType PreferredType) {
160 // Find CGPassManager
Duncan Sands5e1f0482007-07-19 09:42:01 +0000161 while (!PMS.empty() &&
162 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
163 PMS.pop();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164
Devang Pateld0abb8b2008-05-02 22:13:33 +0000165 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
Dan Gohmanf17a25c2007-07-18 16:29:46 +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);
184 TPM->schedulePass(P);
185
186 // [4] Push new manager into PMS
187 PMS.push(CGP);
188 }
189
190 CGP->add(this);
191}
192
193/// 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}