1 //===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
3 // The LLVM Compiler Infrastructure
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.
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"
25 //===----------------------------------------------------------------------===//
28 /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
30 class CGPassManager : public ModulePass, public PMDataManager {
34 CGPassManager(int Depth)
35 : ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
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);
41 bool doInitialization(CallGraph &CG);
42 bool doFinalization(CallGraph &CG);
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();
51 virtual const char *getPassName() const {
52 return "CallGraph Pass Manager";
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);
65 Pass *getContainedPass(unsigned N) {
66 assert ( N < PassVector.size() && "Pass number out of range!");
67 Pass *FP = static_cast<Pass *>(PassVector[N]);
71 virtual PassManagerType getPassManagerType() const {
72 return PMT_CallGraphPassManager;
76 const int 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);
84 for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
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);
93 dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
94 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
96 initializeAnalysisImpl(P);
99 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
100 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
102 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
103 assert (FPP && "Invalid CGPassManager member");
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();
110 dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getName());
111 Changed |= FPP->runOnFunction(*F);
118 dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
119 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
121 removeNotPreservedAnalysis(P);
122 recordAvailableAnalysis(P);
123 removeDeadPasses(P, "", ON_CG_MSG);
126 Changed |= doFinalization(CG);
131 bool CGPassManager::doInitialization(CallGraph &CG) {
132 bool Changed = false;
133 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
134 Pass *P = getContainedPass(Index);
135 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
136 Changed |= CGSP->doInitialization(CG);
142 bool CGPassManager::doFinalization(CallGraph &CG) {
143 bool Changed = false;
144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
145 Pass *P = getContainedPass(Index);
146 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
147 Changed |= CGSP->doFinalization(CG);
152 /// Assign pass manager to manage this pass.
153 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
154 PassManagerType PreferredType) {
155 // Find CGPassManager
156 while (!PMS.empty()) {
157 if (PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
163 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
165 // Create new Call Graph SCC Pass Manager if it does not exist.
168 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
169 PMDataManager *PMD = PMS.top();
171 // [1] Create new Call Graph Pass Manager
172 CGP = new CGPassManager(PMD->getDepth() + 1);
174 // [2] Set up new manager's top level manager
175 PMTopLevelManager *TPM = PMD->getTopLevelManager();
176 TPM->addIndirectPassManager(CGP);
178 // [3] Assign manager to manage this new manager. This may create
179 // and push new managers into PMS
180 Pass *P = dynamic_cast<Pass *>(CGP);
181 P->assignPassManager(PMS);
183 // [4] Push new manager into PMS
190 /// getAnalysisUsage - For this class, we declare that we require and preserve
191 /// the call graph. If the derived class implements this method, it should
192 /// always explicitly call the implementation here.
193 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
194 AU.addRequired<CallGraph>();
195 AU.addPreserved<CallGraph>();