blob: 7ec7a87d2ca5edf21cefa7520198811a0ef70723 [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"
Chris Lattnera10df502004-04-20 21:30:06 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Devang Patel75f9abf2007-01-17 21:45:01 +000024//===----------------------------------------------------------------------===//
25// CGPassManager
26//
27/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
28
29class CGPassManager : public ModulePass, public PMDataManager {
30
31public:
32 CGPassManager(int Depth) : PMDataManager(Depth) { }
33
34 /// run - Execute all of the passes scheduled for execution. Keep track of
35 /// whether any of the passes modifies the module, and if so, return true.
36 bool runOnModule(Module &M);
37
38 bool doInitialization(CallGraph &CG);
39 bool doFinalization(CallGraph &CG);
40
41 /// Pass Manager itself does not invalidate any analysis info.
42 void getAnalysisUsage(AnalysisUsage &Info) const {
43 // CGPassManager walks SCC and it needs CallGraph.
44 Info.addRequired<CallGraph>();
45 Info.setPreservesAll();
46 }
47
48 // Print passes managed by this manager
49 void dumpPassStructure(unsigned Offset) {
50 llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n";
51 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
52 Pass *P = getContainedPass(Index);
53 P->dumpPassStructure(Offset + 1);
54 dumpLastUses(P, Offset+1);
55 }
56 }
57
58 Pass *getContainedPass(unsigned N) {
59 assert ( N < PassVector.size() && "Pass number out of range!");
60 Pass *FP = static_cast<Pass *>(PassVector[N]);
61 return FP;
62 }
63
64 virtual PassManagerType getPassManagerType() {
65 return PMT_CallGraphPassManager;
66 }
67};
68
69/// run - Execute all of the passes scheduled for execution. Keep track of
70/// whether any of the passes modifies the module, and if so, return true.
71bool CGPassManager::runOnModule(Module &M) {
72 CallGraph &CG = getAnalysis<CallGraph>();
73 bool Changed = doInitialization(CG);
74
75 std::string Msg1 = "Executing Pass '";
76 std::string Msg3 = "' Made Modification '";
77
78 // Walk SCC
79 for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
80 I != E; ++I) {
81
82 // Run all passes on current SCC
83 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
84
85 Pass *P = getContainedPass(Index);
86 AnalysisUsage AnUsage;
87 P->getAnalysisUsage(AnUsage);
88
89 std::string Msg2 = "' on Call Graph ...\n'";
90 dumpPassInfo(P, Msg1, Msg2);
91 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
92
93 initializeAnalysisImpl(P);
94
95 // if (TheTimeInfo) TheTimeInfo->passStarted(P);
96 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
97 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
98 else {
99 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
100 assert (FPP && "Invalid CGPassManager member");
101
102 // Run pass P on all functions current SCC
103 std::vector<CallGraphNode*> &SCC = *I;
104 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
105 Function *F = SCC[i]->getFunction();
106 if (F)
107 Changed |= FPP->runOnFunction(*F);
108 }
109 }
110 // if (TheTimeInfo) TheTimeInfo->passEnded(MP);
111
112 if (Changed)
113 dumpPassInfo(P, Msg3, Msg2);
114 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
115
116 removeNotPreservedAnalysis(P);
117 recordAvailableAnalysis(P);
118 removeDeadPasses(P, Msg2);
119 }
120 }
121 Changed |= doFinalization(CG);
122 return Changed;
123}
124
125/// Initialize CG
126bool CGPassManager::doInitialization(CallGraph &CG) {
127 bool Changed = false;
128 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
129 Pass *P = getContainedPass(Index);
130 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
131 Changed |= CGSP->doInitialization(CG);
132 }
133 return Changed;
134}
135
136/// Finalize CG
137bool CGPassManager::doFinalization(CallGraph &CG) {
138 bool Changed = false;
139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
140 Pass *P = getContainedPass(Index);
141 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
142 Changed |= CGSP->doFinalization(CG);
143 }
144 return Changed;
145}
146
Chris Lattner4a810672003-08-31 01:54:59 +0000147/// getAnalysisUsage - For this class, we declare that we require and preserve
148/// the call graph. If the derived class implements this method, it should
149/// always explicitly call the implementation here.
150void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
151 AU.addRequired<CallGraph>();
152 AU.addPreserved<CallGraph>();
153}
154
Chris Lattnerb12914b2004-09-20 04:48:05 +0000155bool CallGraphSCCPass::runOnModule(Module &M) {
Chris Lattner4a810672003-08-31 01:54:59 +0000156 CallGraph &CG = getAnalysis<CallGraph>();
Chris Lattner72035992004-04-20 21:52:26 +0000157 bool Changed = doInitialization(CG);
Chris Lattner55b2eb32003-08-31 20:01:57 +0000158 for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
Chris Lattner4a810672003-08-31 01:54:59 +0000159 I != E; ++I)
Chris Lattner6c5fd8e2003-08-31 19:35:16 +0000160 Changed = runOnSCC(*I);
Chris Lattner72035992004-04-20 21:52:26 +0000161 return Changed | doFinalization(CG);
Chris Lattner4a810672003-08-31 01:54:59 +0000162}