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