Pretty print pass manager
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 // CGPassManager
26 //
27 /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
28
29 class CGPassManager : public ModulePass, public PMDataManager {
30
31 public:
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   virtual const char *getPassName() const {
49     return "CallGraph Pass Manager";
50   }
51
52   // Print passes managed by this manager
53   void dumpPassStructure(unsigned Offset) {
54     llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n";
55     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
56       Pass *P = getContainedPass(Index);
57       P->dumpPassStructure(Offset + 1);
58       dumpLastUses(P, Offset+1);
59     }
60   }
61
62   Pass *getContainedPass(unsigned N) {
63     assert ( N < PassVector.size() && "Pass number out of range!");
64     Pass *FP = static_cast<Pass *>(PassVector[N]);
65     return FP;
66   }
67
68   virtual PassManagerType getPassManagerType() { 
69     return PMT_CallGraphPassManager; 
70   }
71 };
72
73 /// run - Execute all of the passes scheduled for execution.  Keep track of
74 /// whether any of the passes modifies the module, and if so, return true.
75 bool CGPassManager::runOnModule(Module &M) {
76   CallGraph &CG = getAnalysis<CallGraph>();
77   bool Changed = doInitialization(CG);
78
79   std::string Msg1 = "Executing Pass '";
80   std::string Msg3 = "' Made Modification '";
81
82   // Walk SCC
83   for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
84        I != E; ++I) {
85
86     // Run all passes on current SCC
87     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
88
89       Pass *P = getContainedPass(Index);
90       AnalysisUsage AnUsage;
91       P->getAnalysisUsage(AnUsage);
92
93       std::string Msg2 = "' on Call Graph ...\n'";
94       dumpPassInfo(P, Msg1, Msg2);
95       dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
96
97       initializeAnalysisImpl(P);
98
99       StartPassTimer(P);
100       if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
101         Changed |= CGSP->runOnSCC(*I);   // TODO : What if CG is changed ?
102       else {
103         FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
104         assert (FPP && "Invalid CGPassManager member");
105
106         // Run pass P on all functions current SCC
107         std::vector<CallGraphNode*> &SCC = *I;
108         for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
109           Function *F = SCC[i]->getFunction();
110           if (F) 
111             Changed |= FPP->runOnFunction(*F);
112         }
113       }
114       StopPassTimer(P);
115
116       if (Changed)
117         dumpPassInfo(P, Msg3, Msg2);
118       dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
119       
120       removeNotPreservedAnalysis(P);
121       recordAvailableAnalysis(P);
122       removeDeadPasses(P, Msg2);
123     }
124   }
125   Changed |= doFinalization(CG);
126   return Changed;
127 }
128
129 /// Initialize CG
130 bool CGPassManager::doInitialization(CallGraph &CG) {
131   bool Changed = false;
132   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
133     Pass *P = getContainedPass(Index);
134     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) 
135       Changed |= CGSP->doInitialization(CG);
136   }
137   return Changed;
138 }
139
140 /// Finalize CG
141 bool CGPassManager::doFinalization(CallGraph &CG) {
142   bool Changed = false;
143   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
144     Pass *P = getContainedPass(Index);
145     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) 
146       Changed |= CGSP->doFinalization(CG);
147   }
148   return Changed;
149 }
150
151 /// Assign pass manager to manage this pass.
152 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
153                                          PassManagerType PreferredType) {
154   // Find CGPassManager 
155   while (!PMS.empty()) {
156     if (PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
157       PMS.pop();
158     else;
159     break;
160   }
161
162   CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
163
164   // Create new Call Graph SCC Pass Manager if it does not exist. 
165   if (!CGP) {
166
167     assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
168     PMDataManager *PMD = PMS.top();
169
170     // [1] Create new Call Graph Pass Manager
171     CGP = new CGPassManager(PMD->getDepth() + 1);
172
173     // [2] Set up new manager's top level manager
174     PMTopLevelManager *TPM = PMD->getTopLevelManager();
175     TPM->addIndirectPassManager(CGP);
176
177     // [3] Assign manager to manage this new manager. This may create
178     // and push new managers into PMS
179     Pass *P = dynamic_cast<Pass *>(CGP);
180     P->assignPassManager(PMS);
181
182     // [4] Push new manager into PMS
183     PMS.push(CGP);
184   }
185
186   CGP->add(this);
187 }
188
189 /// getAnalysisUsage - For this class, we declare that we require and preserve
190 /// the call graph.  If the derived class implements this method, it should
191 /// always explicitly call the implementation here.
192 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
193   AU.addRequired<CallGraph>();
194   AU.addPreserved<CallGraph>();
195 }