blob: 4a9bf9fa005d61d31956ef47166768f8fb96fbec [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"
Chris Lattner397f4562009-08-23 06:03:38 +000023#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27// CGPassManager
28//
29/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
30
Dan Gohman089efff2008-05-13 00:00:25 +000031namespace {
32
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033class CGPassManager : public ModulePass, public PMDataManager {
34
35public:
36 static char ID;
Dan Gohman34c280e2007-08-01 15:32:29 +000037 explicit CGPassManager(int Depth)
Dan Gohman26f8c272008-09-04 17:05:41 +000038 : ModulePass(&ID), PMDataManager(Depth) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40 /// run - Execute all of the passes scheduled for execution. Keep track of
41 /// whether any of the passes modifies the module, and if so, return true.
42 bool runOnModule(Module &M);
43
Bill Wendlinga60551c2009-02-11 18:19:24 +000044 bool doInitialization(CallGraph &CG);
45 bool doFinalization(CallGraph &CG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47 /// Pass Manager itself does not invalidate any analysis info.
48 void getAnalysisUsage(AnalysisUsage &Info) const {
49 // CGPassManager walks SCC and it needs CallGraph.
50 Info.addRequired<CallGraph>();
51 Info.setPreservesAll();
52 }
53
54 virtual const char *getPassName() const {
55 return "CallGraph Pass Manager";
56 }
57
58 // Print passes managed by this manager
59 void dumpPassStructure(unsigned Offset) {
Chris Lattner397f4562009-08-23 06:03:38 +000060 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
62 Pass *P = getContainedPass(Index);
63 P->dumpPassStructure(Offset + 1);
64 dumpLastUses(P, Offset+1);
65 }
66 }
67
68 Pass *getContainedPass(unsigned N) {
Chris Lattner397f4562009-08-23 06:03:38 +000069 assert(N < PassVector.size() && "Pass number out of range!");
70 return static_cast<Pass *>(PassVector[N]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 }
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>();
Bill Wendlinga60551c2009-02-11 18:19:24 +000085 bool Changed = doInitialization(CG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086
Chris Lattner3dab7152009-08-31 00:19:58 +000087 std::vector<CallGraphNode*> CurSCC;
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 // Walk SCC
Chris Lattner3dab7152009-08-31 00:19:58 +000090 for (scc_iterator<CallGraph*> CGI = scc_begin(&CG), E = scc_end(&CG);
91 CGI != E;) {
92 // Copy the current SCC and increment past it so that the pass can hack
93 // on the SCC if it wants to without invalidating our iterator.
94 CurSCC = *CGI;
95 ++CGI;
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 // Run all passes on current SCC
98 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
99 Pass *P = getContainedPass(Index);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100
101 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
Chris Lattnere1172482008-08-08 15:14:09 +0000102 dumpRequiredSet(P);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103
104 initializeAnalysisImpl(P);
105
106 StartPassTimer(P);
107 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
Chris Lattner3dab7152009-08-31 00:19:58 +0000108 Changed |= CGSP->runOnSCC(CurSCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 else {
110 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
111 assert (FPP && "Invalid CGPassManager member");
112
113 // Run pass P on all functions current SCC
Chris Lattner3dab7152009-08-31 00:19:58 +0000114 for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) {
115 if (Function *F = CurSCC[i]->getFunction()) {
Daniel Dunbar5d3ea962009-07-26 09:48:23 +0000116 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 Changed |= FPP->runOnFunction(*F);
118 }
119 }
120 }
121 StopPassTimer(P);
122
123 if (Changed)
124 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
Chris Lattnere1172482008-08-08 15:14:09 +0000125 dumpPreservedSet(P);
Devang Patela5eb9a32007-07-19 18:02:32 +0000126
127 verifyPreservedAnalysis(P);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 removeNotPreservedAnalysis(P);
129 recordAvailableAnalysis(P);
130 removeDeadPasses(P, "", ON_CG_MSG);
131 }
132 }
Bill Wendlinga60551c2009-02-11 18:19:24 +0000133 Changed |= doFinalization(CG);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 return Changed;
135}
136
137/// Initialize CG
Bill Wendlinga60551c2009-02-11 18:19:24 +0000138bool CGPassManager::doInitialization(CallGraph &CG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 bool Changed = false;
140 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
141 Pass *P = getContainedPass(Index);
Nick Lewycky6f9646c2009-02-13 07:15:53 +0000142 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 Changed |= CGSP->doInitialization(CG);
Nick Lewycky6f9646c2009-02-13 07:15:53 +0000144 } else {
145 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
146 assert (FP && "Invalid CGPassManager member");
147 Changed |= FP->doInitialization(CG.getModule());
148 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 }
150 return Changed;
151}
152
153/// Finalize CG
Bill Wendlinga60551c2009-02-11 18:19:24 +0000154bool CGPassManager::doFinalization(CallGraph &CG) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 bool Changed = false;
156 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
157 Pass *P = getContainedPass(Index);
Nick Lewycky6f9646c2009-02-13 07:15:53 +0000158 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 Changed |= CGSP->doFinalization(CG);
Nick Lewycky6f9646c2009-02-13 07:15:53 +0000160 } else {
161 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
162 assert (FP && "Invalid CGPassManager member");
163 Changed |= FP->doFinalization(CG.getModule());
164 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 }
166 return Changed;
167}
168
169/// Assign pass manager to manage this pass.
170void CallGraphSCCPass::assignPassManager(PMStack &PMS,
171 PassManagerType PreferredType) {
172 // Find CGPassManager
Duncan Sands5e1f0482007-07-19 09:42:01 +0000173 while (!PMS.empty() &&
174 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
175 PMS.pop();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176
Devang Pateld0abb8b2008-05-02 22:13:33 +0000177 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
179
180 // Create new Call Graph SCC Pass Manager if it does not exist.
181 if (!CGP) {
182
183 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
184 PMDataManager *PMD = PMS.top();
185
186 // [1] Create new Call Graph Pass Manager
187 CGP = new CGPassManager(PMD->getDepth() + 1);
188
189 // [2] Set up new manager's top level manager
190 PMTopLevelManager *TPM = PMD->getTopLevelManager();
191 TPM->addIndirectPassManager(CGP);
192
193 // [3] Assign manager to manage this new manager. This may create
194 // and push new managers into PMS
195 Pass *P = dynamic_cast<Pass *>(CGP);
196 TPM->schedulePass(P);
197
198 // [4] Push new manager into PMS
199 PMS.push(CGP);
200 }
201
202 CGP->add(this);
203}
204
205/// getAnalysisUsage - For this class, we declare that we require and preserve
206/// the call graph. If the derived class implements this method, it should
207/// always explicitly call the implementation here.
208void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
209 AU.addRequired<CallGraph>();
210 AU.addPreserved<CallGraph>();
211}