blob: 4a9bf9fa005d61d31956ef47166768f8fb96fbec [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 Lattner45cfe542009-08-23 06:03:38 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnera10df502004-04-20 21:30:06 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Devang Patel75f9abf2007-01-17 21:45:01 +000026//===----------------------------------------------------------------------===//
27// CGPassManager
28//
29/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
30
Dan Gohman844731a2008-05-13 00:00:25 +000031namespace {
32
Devang Patel75f9abf2007-01-17 21:45:01 +000033class CGPassManager : public ModulePass, public PMDataManager {
34
35public:
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID;
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000037 explicit CGPassManager(int Depth)
Dan Gohmanae73dc12008-09-04 17:05:41 +000038 : ModulePass(&ID), PMDataManager(Depth) { }
Devang Patel75f9abf2007-01-17 21:45:01 +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 Wendling905c7e92009-02-11 18:19:24 +000044 bool doInitialization(CallGraph &CG);
45 bool doFinalization(CallGraph &CG);
Devang Patel75f9abf2007-01-17 21:45:01 +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
Devang Patel505f36a2007-02-01 22:09:37 +000054 virtual const char *getPassName() const {
55 return "CallGraph Pass Manager";
56 }
57
Devang Patel75f9abf2007-01-17 21:45:01 +000058 // Print passes managed by this manager
59 void dumpPassStructure(unsigned Offset) {
Chris Lattner45cfe542009-08-23 06:03:38 +000060 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
Devang Patel75f9abf2007-01-17 21:45:01 +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 Lattner45cfe542009-08-23 06:03:38 +000069 assert(N < PassVector.size() && "Pass number out of range!");
70 return static_cast<Pass *>(PassVector[N]);
Devang Patel75f9abf2007-01-17 21:45:01 +000071 }
72
Devang Patel84da80d2007-02-27 15:00:39 +000073 virtual PassManagerType getPassManagerType() const {
Devang Patel75f9abf2007-01-17 21:45:01 +000074 return PMT_CallGraphPassManager;
75 }
76};
77
Dan Gohman844731a2008-05-13 00:00:25 +000078}
79
Devang Patel19974732007-05-03 01:11:54 +000080char CGPassManager::ID = 0;
Devang Patel75f9abf2007-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>();
Bill Wendling905c7e92009-02-11 18:19:24 +000085 bool Changed = doInitialization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +000086
Chris Lattner5095e3d2009-08-31 00:19:58 +000087 std::vector<CallGraphNode*> CurSCC;
88
Devang Patel75f9abf2007-01-17 21:45:01 +000089 // Walk SCC
Chris Lattner5095e3d2009-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
Devang Patel75f9abf2007-01-17 21:45:01 +000097 // Run all passes on current SCC
Anton Korobeynikovbed29462007-04-16 18:10:23 +000098 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
Devang Patel75f9abf2007-01-17 21:45:01 +000099 Pass *P = getContainedPass(Index);
Devang Patel75f9abf2007-01-17 21:45:01 +0000100
Devang Patel7f997612007-03-05 20:01:30 +0000101 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000102 dumpRequiredSet(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000103
104 initializeAnalysisImpl(P);
105
Devang Patelf5b17fd2007-01-29 23:29:54 +0000106 StartPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000107 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
Chris Lattner5095e3d2009-08-31 00:19:58 +0000108 Changed |= CGSP->runOnSCC(CurSCC);
Devang Patel75f9abf2007-01-17 21:45:01 +0000109 else {
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000110 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
111 assert (FPP && "Invalid CGPassManager member");
Devang Patel75f9abf2007-01-17 21:45:01 +0000112
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000113 // Run pass P on all functions current SCC
Chris Lattner5095e3d2009-08-31 00:19:58 +0000114 for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) {
115 if (Function *F = CurSCC[i]->getFunction()) {
Daniel Dunbar460f6562009-07-26 09:48:23 +0000116 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
Devang Patel7f997612007-03-05 20:01:30 +0000117 Changed |= FPP->runOnFunction(*F);
Devang Patel95ced112007-02-01 22:38:33 +0000118 }
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000119 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000120 }
Devang Patelf5b17fd2007-01-29 23:29:54 +0000121 StopPassTimer(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000122
123 if (Changed)
Devang Patel7f997612007-03-05 20:01:30 +0000124 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
Chris Lattner0dabb7e2008-08-08 15:14:09 +0000125 dumpPreservedSet(P);
Devang Patel58e0ef12007-07-19 18:02:32 +0000126
127 verifyPreservedAnalysis(P);
Devang Patel75f9abf2007-01-17 21:45:01 +0000128 removeNotPreservedAnalysis(P);
129 recordAvailableAnalysis(P);
Devang Patel7f997612007-03-05 20:01:30 +0000130 removeDeadPasses(P, "", ON_CG_MSG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000131 }
132 }
Bill Wendling905c7e92009-02-11 18:19:24 +0000133 Changed |= doFinalization(CG);
Devang Patel75f9abf2007-01-17 21:45:01 +0000134 return Changed;
135}
136
137/// Initialize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000138bool CGPassManager::doInitialization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000139 bool Changed = false;
140 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
141 Pass *P = getContainedPass(Index);
Nick Lewycky8968a072009-02-13 07:15:53 +0000142 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000143 Changed |= CGSP->doInitialization(CG);
Nick Lewycky8968a072009-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 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000149 }
150 return Changed;
151}
152
153/// Finalize CG
Bill Wendling905c7e92009-02-11 18:19:24 +0000154bool CGPassManager::doFinalization(CallGraph &CG) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000155 bool Changed = false;
156 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
157 Pass *P = getContainedPass(Index);
Nick Lewycky8968a072009-02-13 07:15:53 +0000158 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
Devang Patel75f9abf2007-01-17 21:45:01 +0000159 Changed |= CGSP->doFinalization(CG);
Nick Lewycky8968a072009-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 }
Devang Patel75f9abf2007-01-17 21:45:01 +0000165 }
166 return Changed;
167}
168
Devang Pateld9f10c32007-01-23 21:55:17 +0000169/// Assign pass manager to manage this pass.
Devang Patel97fd2432007-01-23 21:52:35 +0000170void CallGraphSCCPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000171 PassManagerType PreferredType) {
Devang Patel97fd2432007-01-23 21:52:35 +0000172 // Find CGPassManager
Duncan Sands20d824b2007-07-19 09:42:01 +0000173 while (!PMS.empty() &&
174 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
175 PMS.pop();
Devang Patel97fd2432007-01-23 21:52:35 +0000176
Devang Patel201ebe32008-05-02 22:13:33 +0000177 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
Devang Patel97fd2432007-01-23 21:52:35 +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);
Devang Patel25e681a2007-06-21 22:29:02 +0000196 TPM->schedulePass(P);
Devang Patel97fd2432007-01-23 21:52:35 +0000197
198 // [4] Push new manager into PMS
199 PMS.push(CGP);
200 }
201
202 CGP->add(this);
203}
204
Chris Lattner4a810672003-08-31 01:54:59 +0000205/// 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}