03db7f940b62dee1be4f533786ab5b79fe760fe3
[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   static char ID;
34   explicit CGPassManager(int Depth) 
35     : ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
36
37   /// run - Execute all of the passes scheduled for execution.  Keep track of
38   /// whether any of the passes modifies the module, and if so, return true.
39   bool runOnModule(Module &M);
40
41   bool doInitialization(CallGraph &CG);
42   bool doFinalization(CallGraph &CG);
43
44   /// Pass Manager itself does not invalidate any analysis info.
45   void getAnalysisUsage(AnalysisUsage &Info) const {
46     // CGPassManager walks SCC and it needs CallGraph.
47     Info.addRequired<CallGraph>();
48     Info.setPreservesAll();
49   }
50
51   virtual const char *getPassName() const {
52     return "CallGraph Pass Manager";
53   }
54
55   // Print passes managed by this manager
56   void dumpPassStructure(unsigned Offset) {
57     llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n";
58     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
59       Pass *P = getContainedPass(Index);
60       P->dumpPassStructure(Offset + 1);
61       dumpLastUses(P, Offset+1);
62     }
63   }
64
65   Pass *getContainedPass(unsigned N) {
66     assert ( N < PassVector.size() && "Pass number out of range!");
67     Pass *FP = static_cast<Pass *>(PassVector[N]);
68     return FP;
69   }
70
71   virtual PassManagerType getPassManagerType() const { 
72     return PMT_CallGraphPassManager; 
73   }
74 };
75
76 char CGPassManager::ID = 0;
77 /// run - Execute all of the passes scheduled for execution.  Keep track of
78 /// whether any of the passes modifies the module, and if so, return true.
79 bool CGPassManager::runOnModule(Module &M) {
80   CallGraph &CG = getAnalysis<CallGraph>();
81   bool Changed = doInitialization(CG);
82
83   // Walk SCC
84   for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
85        I != E; ++I) {
86
87     // Run all passes on current SCC
88     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
89       Pass *P = getContainedPass(Index);
90       AnalysisUsage AnUsage;
91       P->getAnalysisUsage(AnUsage);
92
93       dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
94       dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
95
96       initializeAnalysisImpl(P);
97
98       StartPassTimer(P);
99       if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
100         Changed |= CGSP->runOnSCC(*I);   // TODO : What if CG is changed ?
101       else {
102         FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
103         assert (FPP && "Invalid CGPassManager member");
104
105         // Run pass P on all functions current SCC
106         std::vector<CallGraphNode*> &SCC = *I;
107         for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
108           Function *F = SCC[i]->getFunction();
109           if (F) {
110             dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getNameStart());
111             Changed |= FPP->runOnFunction(*F);
112           }
113         }
114       }
115       StopPassTimer(P);
116
117       if (Changed)
118         dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
119       dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
120
121       verifyPreservedAnalysis(P);      
122       removeNotPreservedAnalysis(P);
123       recordAvailableAnalysis(P);
124       removeDeadPasses(P, "", ON_CG_MSG);
125     }
126   }
127   Changed |= doFinalization(CG);
128   return Changed;
129 }
130
131 /// Initialize CG
132 bool CGPassManager::doInitialization(CallGraph &CG) {
133   bool Changed = false;
134   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
135     Pass *P = getContainedPass(Index);
136     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) 
137       Changed |= CGSP->doInitialization(CG);
138   }
139   return Changed;
140 }
141
142 /// Finalize CG
143 bool CGPassManager::doFinalization(CallGraph &CG) {
144   bool Changed = false;
145   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
146     Pass *P = getContainedPass(Index);
147     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) 
148       Changed |= CGSP->doFinalization(CG);
149   }
150   return Changed;
151 }
152
153 /// Assign pass manager to manage this pass.
154 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
155                                          PassManagerType PreferredType) {
156   // Find CGPassManager 
157   while (!PMS.empty() &&
158          PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
159     PMS.pop();
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     TPM->schedulePass(P);
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 }