1 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
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"
26 //===----------------------------------------------------------------------===//
29 /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
33 class CGPassManager : public ModulePass, public PMDataManager {
36 explicit CGPassManager(int Depth)
37 : ModulePass(&ID), PMDataManager(Depth) { }
39 /// run - Execute all of the passes scheduled for execution. Keep track of
40 /// whether any of the passes modifies the module, and if so, return true.
41 bool runOnModule(Module &M);
43 bool doInitialization(CallGraph &CG);
44 bool doFinalization(CallGraph &CG);
46 /// Pass Manager itself does not invalidate any analysis info.
47 void getAnalysisUsage(AnalysisUsage &Info) const {
48 // CGPassManager walks SCC and it needs CallGraph.
49 Info.addRequired<CallGraph>();
50 Info.setPreservesAll();
53 virtual const char *getPassName() const {
54 return "CallGraph Pass Manager";
57 // Print passes managed by this manager
58 void dumpPassStructure(unsigned Offset) {
59 errs().indent(Offset*2) << "Call Graph SCC Pass Manager\n";
60 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
61 Pass *P = getContainedPass(Index);
62 P->dumpPassStructure(Offset + 1);
63 dumpLastUses(P, Offset+1);
67 Pass *getContainedPass(unsigned N) {
68 assert(N < PassVector.size() && "Pass number out of range!");
69 return static_cast<Pass *>(PassVector[N]);
72 virtual PassManagerType getPassManagerType() const {
73 return PMT_CallGraphPassManager;
77 bool RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC);
80 } // end anonymous namespace.
82 char CGPassManager::ID = 0;
84 bool CGPassManager::RunPassOnSCC(Pass *P, std::vector<CallGraphNode*> &CurSCC) {
86 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass*>(P)) {
88 Changed = CGSP->runOnSCC(CurSCC);
94 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
95 assert(FPP && "Invalid CGPassManager member");
97 // Run pass P on all functions in the current SCC.
98 for (unsigned i = 0, e = CurSCC.size(); i != e; ++i) {
99 if (Function *F = CurSCC[i]->getFunction()) {
100 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
101 Changed |= FPP->runOnFunction(*F);
110 /// run - Execute all of the passes scheduled for execution. Keep track of
111 /// whether any of the passes modifies the module, and if so, return true.
112 bool CGPassManager::runOnModule(Module &M) {
113 CallGraph &CG = getAnalysis<CallGraph>();
114 bool Changed = doInitialization(CG);
116 std::vector<CallGraphNode*> CurSCC;
118 // Walk the callgraph in bottom-up SCC order.
119 for (scc_iterator<CallGraph*> CGI = scc_begin(&CG), E = scc_end(&CG);
121 // Copy the current SCC and increment past it so that the pass can hack
122 // on the SCC if it wants to without invalidating our iterator.
127 // Run all passes on current SCC.
128 for (unsigned PassNo = 0, e = getNumContainedPasses();
129 PassNo != e; ++PassNo) {
130 Pass *P = getContainedPass(PassNo);
132 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
135 initializeAnalysisImpl(P);
137 // Actually run this pass on the current SCC.
138 Changed |= RunPassOnSCC(P, CurSCC);
141 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
144 verifyPreservedAnalysis(P);
145 removeNotPreservedAnalysis(P);
146 recordAvailableAnalysis(P);
147 removeDeadPasses(P, "", ON_CG_MSG);
150 Changed |= doFinalization(CG);
155 bool CGPassManager::doInitialization(CallGraph &CG) {
156 bool Changed = false;
157 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
158 Pass *P = getContainedPass(Index);
159 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
160 Changed |= CGSP->doInitialization(CG);
162 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
163 assert (FP && "Invalid CGPassManager member");
164 Changed |= FP->doInitialization(CG.getModule());
171 bool CGPassManager::doFinalization(CallGraph &CG) {
172 bool Changed = false;
173 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
174 Pass *P = getContainedPass(Index);
175 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P)) {
176 Changed |= CGSP->doFinalization(CG);
178 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
179 assert (FP && "Invalid CGPassManager member");
180 Changed |= FP->doFinalization(CG.getModule());
186 /// Assign pass manager to manage this pass.
187 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
188 PassManagerType PreferredType) {
189 // Find CGPassManager
190 while (!PMS.empty() &&
191 PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
194 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
195 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
197 // Create new Call Graph SCC Pass Manager if it does not exist.
200 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
201 PMDataManager *PMD = PMS.top();
203 // [1] Create new Call Graph Pass Manager
204 CGP = new CGPassManager(PMD->getDepth() + 1);
206 // [2] Set up new manager's top level manager
207 PMTopLevelManager *TPM = PMD->getTopLevelManager();
208 TPM->addIndirectPassManager(CGP);
210 // [3] Assign manager to manage this new manager. This may create
211 // and push new managers into PMS
212 Pass *P = dynamic_cast<Pass *>(CGP);
213 TPM->schedulePass(P);
215 // [4] Push new manager into PMS
222 /// getAnalysisUsage - For this class, we declare that we require and preserve
223 /// the call graph. If the derived class implements this method, it should
224 /// always explicitly call the implementation here.
225 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
226 AU.addRequired<CallGraph>();
227 AU.addPreserved<CallGraph>();