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 {
37 explicit CGPassManager(int Depth)
38 : ModulePass(&ID), PMDataManager(Depth) { }
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);
44 bool doInitialization(CallGraph &CG);
45 bool doFinalization(CallGraph &CG);
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();
54 virtual const char *getPassName() const {
55 return "CallGraph Pass Manager";
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);
68 Pass *getContainedPass(unsigned N) {
69 assert(N < PassVector.size() && "Pass number out of range!");
70 return static_cast<Pass *>(PassVector[N]);
73 virtual PassManagerType getPassManagerType() const {
74 return PMT_CallGraphPassManager;
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);
87 std::vector<CallGraphNode*> CurSCC;
90 for (scc_iterator<CallGraph*> CGI = scc_begin(&CG), E = scc_end(&CG);
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.
97 // Run all passes on current SCC
98 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
99 Pass *P = getContainedPass(Index);
101 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
104 initializeAnalysisImpl(P);
107 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
108 Changed |= CGSP->runOnSCC(CurSCC);
110 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
111 assert (FPP && "Invalid CGPassManager member");
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);
124 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
127 verifyPreservedAnalysis(P);
128 removeNotPreservedAnalysis(P);
129 recordAvailableAnalysis(P);
130 removeDeadPasses(P, "", ON_CG_MSG);
133 Changed |= doFinalization(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);
145 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
146 assert (FP && "Invalid CGPassManager member");
147 Changed |= FP->doInitialization(CG.getModule());
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);
161 FPPassManager *FP = dynamic_cast<FPPassManager *>(P);
162 assert (FP && "Invalid CGPassManager member");
163 Changed |= FP->doFinalization(CG.getModule());
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)
177 assert (!PMS.empty() && "Unable to handle Call Graph Pass");
178 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
180 // Create new Call Graph SCC Pass Manager if it does not exist.
183 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
184 PMDataManager *PMD = PMS.top();
186 // [1] Create new Call Graph Pass Manager
187 CGP = new CGPassManager(PMD->getDepth() + 1);
189 // [2] Set up new manager's top level manager
190 PMTopLevelManager *TPM = PMD->getTopLevelManager();
191 TPM->addIndirectPassManager(CGP);
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);
198 // [4] Push new manager into PMS
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>();