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"
24 //===----------------------------------------------------------------------===//
27 /// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
29 class CGPassManager : public ModulePass, public PMDataManager {
32 CGPassManager(int Depth) : PMDataManager(Depth) { }
34 /// run - Execute all of the passes scheduled for execution. Keep track of
35 /// whether any of the passes modifies the module, and if so, return true.
36 bool runOnModule(Module &M);
38 bool doInitialization(CallGraph &CG);
39 bool doFinalization(CallGraph &CG);
41 /// Pass Manager itself does not invalidate any analysis info.
42 void getAnalysisUsage(AnalysisUsage &Info) const {
43 // CGPassManager walks SCC and it needs CallGraph.
44 Info.addRequired<CallGraph>();
45 Info.setPreservesAll();
48 // Print passes managed by this manager
49 void dumpPassStructure(unsigned Offset) {
50 llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n";
51 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
52 Pass *P = getContainedPass(Index);
53 P->dumpPassStructure(Offset + 1);
54 dumpLastUses(P, Offset+1);
58 Pass *getContainedPass(unsigned N) {
59 assert ( N < PassVector.size() && "Pass number out of range!");
60 Pass *FP = static_cast<Pass *>(PassVector[N]);
64 virtual PassManagerType getPassManagerType() {
65 return PMT_CallGraphPassManager;
69 /// run - Execute all of the passes scheduled for execution. Keep track of
70 /// whether any of the passes modifies the module, and if so, return true.
71 bool CGPassManager::runOnModule(Module &M) {
72 CallGraph &CG = getAnalysis<CallGraph>();
73 bool Changed = doInitialization(CG);
75 std::string Msg1 = "Executing Pass '";
76 std::string Msg3 = "' Made Modification '";
79 for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
82 // Run all passes on current SCC
83 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
85 Pass *P = getContainedPass(Index);
86 AnalysisUsage AnUsage;
87 P->getAnalysisUsage(AnUsage);
89 std::string Msg2 = "' on Call Graph ...\n'";
90 dumpPassInfo(P, Msg1, Msg2);
91 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
93 initializeAnalysisImpl(P);
95 // if (TheTimeInfo) TheTimeInfo->passStarted(P);
96 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
97 Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
99 FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
100 assert (FPP && "Invalid CGPassManager member");
102 // Run pass P on all functions current SCC
103 std::vector<CallGraphNode*> &SCC = *I;
104 for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
105 Function *F = SCC[i]->getFunction();
107 Changed |= FPP->runOnFunction(*F);
110 // if (TheTimeInfo) TheTimeInfo->passEnded(MP);
113 dumpPassInfo(P, Msg3, Msg2);
114 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
116 removeNotPreservedAnalysis(P);
117 recordAvailableAnalysis(P);
118 removeDeadPasses(P, Msg2);
121 Changed |= doFinalization(CG);
126 bool CGPassManager::doInitialization(CallGraph &CG) {
127 bool Changed = false;
128 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
129 Pass *P = getContainedPass(Index);
130 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
131 Changed |= CGSP->doInitialization(CG);
137 bool CGPassManager::doFinalization(CallGraph &CG) {
138 bool Changed = false;
139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
140 Pass *P = getContainedPass(Index);
141 if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
142 Changed |= CGSP->doFinalization(CG);
147 /// Assign pass manager to manage this pass.
148 void CallGraphSCCPass::assignPassManager(PMStack &PMS,
149 PassManagerType PreferredType) {
150 // Find CGPassManager
151 while (!PMS.empty()) {
152 if (PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
158 CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
160 // Create new Call Graph SCC Pass Manager if it does not exist.
163 assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
164 PMDataManager *PMD = PMS.top();
166 // [1] Create new Call Graph Pass Manager
167 CGP = new CGPassManager(PMD->getDepth() + 1);
169 // [2] Set up new manager's top level manager
170 PMTopLevelManager *TPM = PMD->getTopLevelManager();
171 TPM->addIndirectPassManager(CGP);
173 // [3] Assign manager to manage this new manager. This may create
174 // and push new managers into PMS
175 Pass *P = dynamic_cast<Pass *>(CGP);
176 P->assignPassManager(PMS);
178 // [4] Push new manager into PMS
185 /// getAnalysisUsage - For this class, we declare that we require and preserve
186 /// the call graph. If the derived class implements this method, it should
187 /// always explicitly call the implementation here.
188 void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
189 AU.addRequired<CallGraph>();
190 AU.addPreserved<CallGraph>();