d91a74dd43689d3d97fc4e571329a5141f35d5f2
[oota-llvm.git] / lib / Analysis / IPA / CallGraphSCCPass.cpp
1 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // CGPassManager
28 //
29 /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
30
31 namespace {
32
33 class CGPassManager : public ModulePass, public PMDataManager {
34 public:
35   static char ID;
36   explicit CGPassManager(int Depth) 
37     : ModulePass(&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     errs().indent(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     return static_cast<Pass *>(PassVector[N]);
70   }
71
72   virtual PassManagerType getPassManagerType() const { 
73     return PMT_CallGraphPassManager; 
74   }
75   
76 private:
77   bool RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC);
78 };
79
80 } // end anonymous namespace.
81
82 char CGPassManager::ID = 0;
83
84 bool CGPassManager::RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC) {
85   bool Changed = false;
86   if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass*>(P)) {
87     StartPassTimer(P);
88     Changed = CGSP->runOnSCC(CurSCC);
89     StopPassTimer(P);
90     return Changed;
91   }
92   
93   StartPassTimer(P);
94   FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
95   assert(FPP && "Invalid CGPassManager member");
96   
97   // Run pass P on all functions in the current SCC.
98   for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) {
99     if (Function *F = CurSCC[i]->getFunction()) {
100       dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
101       Changed |= FPP->runOnFunction(*F);
102     }
103   }
104   StopPassTimer(P);
105   
106   return Changed;
107 }
108
109
110 /// run - Execute all of the passes scheduled for execution.  Keep track of
111 /// whether any of the passes modifies the module, and if so, return true.
112 bool CGPassManager::runOnModule(Module &M) {
113   CallGraph &CG = getAnalysis<CallGraph>();
114   bool Changed = doInitialization(CG);
115
116   std::vector<CallGraphNode*> CurSCC;
117   
118   // Walk the callgraph in bottom-up SCC order.
119   for (scc_iterator<CallGraph*> CGI = scc_begin(&CG), E = scc_end(&CG);
120        CGI != E;) {
121     // Copy the current SCC and increment past it so that the pass can hack
122     // on the SCC if it wants to without invalidating our iterator.
123     CurSCC = *CGI;
124     ++CGI;
125     
126     
127     // Run all passes on current SCC.
128     for (unsigned PassNo = 0, e = getNumContainedPasses();
129          PassNo != e; ++PassNo) {
130       Pass *P = getContainedPass(PassNo);
131
132       dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
133       dumpRequiredSet(P);
134
135       initializeAnalysisImpl(P);
136
137       // Actually run this pass on the current SCC.
138       Changed |= RunPassOnSCC(P, CurSCC);
139
140       if (Changed)
141         dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
142       dumpPreservedSet(P);
143
144       verifyPreservedAnalysis(P);      
145       removeNotPreservedAnalysis(P);
146       recordAvailableAnalysis(P);
147       removeDeadPasses(P, "", ON_CG_MSG);
148     }
149   }
150   Changed |= doFinalization(CG);
151   return Changed;
152 }
153
154 /// Initialize CG
155 bool CGPassManager::doInitialization(CallGraph &CG) {
156   bool Changed = false;
157   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
158     Pass *P = getContainedPass(Index);
159     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
160       Changed |= CGSP->doInitialization(CG);
161     } else {
162       FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
163       assert (FP && "Invalid CGPassManager member");
164       Changed |= FP->doInitialization(CG.getModule());
165     }
166   }
167   return Changed;
168 }
169
170 /// Finalize CG
171 bool CGPassManager::doFinalization(CallGraph &CG) {
172   bool Changed = false;
173   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
174     Pass *P = getContainedPass(Index);
175     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
176       Changed |= CGSP->doFinalization(CG);
177     } else {
178       FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
179       assert (FP && "Invalid CGPassManager member");
180       Changed |= FP->doFinalization(CG.getModule());
181     }
182   }
183   return Changed;
184 }
185
186 /// Assign pass manager to manage this pass.
187 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
188                                          PassManagerType PreferredType) {
189   // Find CGPassManager 
190   while (!PMS.empty() &&
191          PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
192     PMS.pop();
193
194   assert (!PMS.empty() && "Unable to handle Call Graph Pass");
195   CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
196
197   // Create new Call Graph SCC Pass Manager if it does not exist. 
198   if (!CGP) {
199
200     assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
201     PMDataManager *PMD = PMS.top();
202
203     // [1] Create new Call Graph Pass Manager
204     CGP = new CGPassManager(PMD->getDepth() + 1);
205
206     // [2] Set up new manager's top level manager
207     PMTopLevelManager *TPM = PMD->getTopLevelManager();
208     TPM->addIndirectPassManager(CGP);
209
210     // [3] Assign manager to manage this new manager. This may create
211     // and push new managers into PMS
212     Pass *P = dynamic_cast<Pass *>(CGP);
213     TPM->schedulePass(P);
214
215     // [4] Push new manager into PMS
216     PMS.push(CGP);
217   }
218
219   CGP->add(this);
220 }
221
222 /// getAnalysisUsage - For this class, we declare that we require and preserve
223 /// the call graph.  If the derived class implements this method, it should
224 /// always explicitly call the implementation here.
225 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
226   AU.addRequired<CallGraph>();
227   AU.addPreserved<CallGraph>();
228 }