Fix some nasty callgraph dangling pointer problems in
[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   std::vector<CallGraphNode*> CurSCC;
88   
89   // Walk SCC
90   for (scc_iterator<CallGraph*> CGI = scc_begin(&CG), E = scc_end(&CG);
91        CGI != E;) {
92     // Copy the current SCC and increment past it so that the pass can hack
93     // on the SCC if it wants to without invalidating our iterator.
94     CurSCC = *CGI;
95     ++CGI;
96     
97     // Run all passes on current SCC
98     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
99       Pass *P = getContainedPass(Index);
100
101       dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
102       dumpRequiredSet(P);
103
104       initializeAnalysisImpl(P);
105
106       StartPassTimer(P);
107       if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
108         Changed |= CGSP->runOnSCC(CurSCC);
109       else {
110         FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
111         assert (FPP && "Invalid CGPassManager member");
112
113         // Run pass P on all functions current SCC
114         for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) {
115           if (Function *F = CurSCC[i]->getFunction()) {
116             dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
117             Changed |= FPP->runOnFunction(*F);
118           }
119         }
120       }
121       StopPassTimer(P);
122
123       if (Changed)
124         dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
125       dumpPreservedSet(P);
126
127       verifyPreservedAnalysis(P);      
128       removeNotPreservedAnalysis(P);
129       recordAvailableAnalysis(P);
130       removeDeadPasses(P, "", ON_CG_MSG);
131     }
132   }
133   Changed |= doFinalization(CG);
134   return Changed;
135 }
136
137 /// Initialize CG
138 bool CGPassManager::doInitialization(CallGraph &CG) {
139   bool Changed = false;
140   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
141     Pass *P = getContainedPass(Index);
142     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
143       Changed |= CGSP->doInitialization(CG);
144     } else {
145       FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
146       assert (FP && "Invalid CGPassManager member");
147       Changed |= FP->doInitialization(CG.getModule());
148     }
149   }
150   return Changed;
151 }
152
153 /// Finalize CG
154 bool CGPassManager::doFinalization(CallGraph &CG) {
155   bool Changed = false;
156   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
157     Pass *P = getContainedPass(Index);
158     if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
159       Changed |= CGSP->doFinalization(CG);
160     } else {
161       FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
162       assert (FP && "Invalid CGPassManager member");
163       Changed |= FP->doFinalization(CG.getModule());
164     }
165   }
166   return Changed;
167 }
168
169 /// Assign pass manager to manage this pass.
170 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
171                                          PassManagerType PreferredType) {
172   // Find CGPassManager 
173   while (!PMS.empty() &&
174          PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
175     PMS.pop();
176
177   assert (!PMS.empty() && "Unable to handle Call Graph Pass");
178   CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
179
180   // Create new Call Graph SCC Pass Manager if it does not exist. 
181   if (!CGP) {
182
183     assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
184     PMDataManager *PMD = PMS.top();
185
186     // [1] Create new Call Graph Pass Manager
187     CGP = new CGPassManager(PMD->getDepth() + 1);
188
189     // [2] Set up new manager's top level manager
190     PMTopLevelManager *TPM = PMD->getTopLevelManager();
191     TPM->addIndirectPassManager(CGP);
192
193     // [3] Assign manager to manage this new manager. This may create
194     // and push new managers into PMS
195     Pass *P = dynamic_cast<Pass *>(CGP);
196     TPM->schedulePass(P);
197
198     // [4] Push new manager into PMS
199     PMS.push(CGP);
200   }
201
202   CGP->add(this);
203 }
204
205 /// getAnalysisUsage - For this class, we declare that we require and preserve
206 /// the call graph.  If the derived class implements this method, it should
207 /// always explicitly call the implementation here.
208 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
209   AU.addRequired<CallGraph>();
210   AU.addPreserved<CallGraph>();
211 }