Change Pass::print to take a raw ostream instead of std::ostream,
[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
35 public:
36   static char ID;
37   explicit CGPassManager(int Depth) 
38     : ModulePass(&ID), PMDataManager(Depth) { }
39
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
44   bool doInitialization(CallGraph &CG);
45   bool doFinalization(CallGraph &CG);
46
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) {
60     errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
61     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) {
69     assert(N < PassVector.size() && "Pass number out of range!");
70     return static_cast<Pass *>(PassVector[N]);
71   }
72
73   virtual PassManagerType getPassManagerType() const { 
74     return PMT_CallGraphPassManager; 
75   }
76 };
77
78 }
79
80 char 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.
83 bool 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
95       dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
96       dumpRequiredSet(P);
97
98       initializeAnalysisImpl(P);
99
100       StartPassTimer(P);
101       if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
102         Changed |= CGSP->runOnSCC(*I);   // TODO : What if CG is changed ?
103       else {
104         FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
105         assert (FPP && "Invalid CGPassManager member");
106
107         // Run pass P on all functions current SCC
108         std::vector<CallGraphNode*> &SCC = *I;
109         for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
110           Function *F = SCC[i]->getFunction();
111           if (F) {
112             dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
113             Changed |= FPP->runOnFunction(*F);
114           }
115         }
116       }
117       StopPassTimer(P);
118
119       if (Changed)
120         dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
121       dumpPreservedSet(P);
122
123       verifyPreservedAnalysis(P);      
124       removeNotPreservedAnalysis(P);
125       recordAvailableAnalysis(P);
126       removeDeadPasses(P, "", ON_CG_MSG);
127     }
128   }
129   Changed |= doFinalization(CG);
130   return Changed;
131 }
132
133 /// Initialize CG
134 bool CGPassManager::doInitialization(CallGraph &CG) {
135   bool Changed = false;
136   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
137     Pass *P = getContainedPass(Index);
138     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
139       Changed |= CGSP->doInitialization(CG);
140     } else {
141       FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
142       assert (FP && "Invalid CGPassManager member");
143       Changed |= FP->doInitialization(CG.getModule());
144     }
145   }
146   return Changed;
147 }
148
149 /// Finalize CG
150 bool CGPassManager::doFinalization(CallGraph &CG) {
151   bool Changed = false;
152   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
153     Pass *P = getContainedPass(Index);
154     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
155       Changed |= CGSP->doFinalization(CG);
156     } else {
157       FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
158       assert (FP && "Invalid CGPassManager member");
159       Changed |= FP->doFinalization(CG.getModule());
160     }
161   }
162   return Changed;
163 }
164
165 /// Assign pass manager to manage this pass.
166 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
167                                          PassManagerType PreferredType) {
168   // Find CGPassManager 
169   while (!PMS.empty() &&
170          PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
171     PMS.pop();
172
173   assert (!PMS.empty() && "Unable to handle Call Graph Pass");
174   CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
175
176   // Create new Call Graph SCC Pass Manager if it does not exist. 
177   if (!CGP) {
178
179     assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
180     PMDataManager *PMD = PMS.top();
181
182     // [1] Create new Call Graph Pass Manager
183     CGP = new CGPassManager(PMD->getDepth() + 1);
184
185     // [2] Set up new manager's top level manager
186     PMTopLevelManager *TPM = PMD->getTopLevelManager();
187     TPM->addIndirectPassManager(CGP);
188
189     // [3] Assign manager to manage this new manager. This may create
190     // and push new managers into PMS
191     Pass *P = dynamic_cast<Pass *>(CGP);
192     TPM->schedulePass(P);
193
194     // [4] Push new manager into PMS
195     PMS.push(CGP);
196   }
197
198   CGP->add(this);
199 }
200
201 /// getAnalysisUsage - For this class, we declare that we require and preserve
202 /// the call graph.  If the derived class implements this method, it should
203 /// always explicitly call the implementation here.
204 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
205   AU.addRequired<CallGraph>();
206   AU.addPreserved<CallGraph>();
207 }