2003-08-31 03:54:59 +02:00
|
|
|
//===- CallGraphSCCPass.cpp - Pass that operates BU on call graph ---------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-31 03:54:59 +02:00
|
|
|
//
|
|
|
|
// This file implements the CallGraphSCCPass class, which is used for passes
|
|
|
|
// which are implemented as bottom-up traversals on the call graph. Because
|
|
|
|
// there may be cycles in the call graph, passes of this type operate on the
|
|
|
|
// call-graph in SCC order: that is, they process function bottom-up, except for
|
|
|
|
// recursive functions, which they process all at once.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CallGraphSCCPass.h"
|
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2007-01-17 22:45:01 +01:00
|
|
|
#include "llvm/PassManagers.h"
|
2007-02-01 23:38:33 +01:00
|
|
|
#include "llvm/Function.h"
|
2004-04-20 23:30:06 +02:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2007-01-17 22:45:01 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CGPassManager
|
|
|
|
//
|
|
|
|
/// CGPassManager manages FPPassManagers and CalLGraphSCCPasses.
|
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
namespace {
|
|
|
|
|
2007-01-17 22:45:01 +01:00
|
|
|
class CGPassManager : public ModulePass, public PMDataManager {
|
|
|
|
|
|
|
|
public:
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID;
|
2007-08-01 17:32:29 +02:00
|
|
|
explicit CGPassManager(int Depth)
|
2007-05-01 23:15:47 +02:00
|
|
|
: ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
|
2007-01-17 22:45:01 +01:00
|
|
|
|
|
|
|
/// run - Execute all of the passes scheduled for execution. Keep track of
|
|
|
|
/// whether any of the passes modifies the module, and if so, return true.
|
|
|
|
bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
bool doInitialization(CallGraph &CG);
|
|
|
|
bool doFinalization(CallGraph &CG);
|
|
|
|
|
|
|
|
/// Pass Manager itself does not invalidate any analysis info.
|
|
|
|
void getAnalysisUsage(AnalysisUsage &Info) const {
|
|
|
|
// CGPassManager walks SCC and it needs CallGraph.
|
|
|
|
Info.addRequired<CallGraph>();
|
|
|
|
Info.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2007-02-01 23:09:37 +01:00
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "CallGraph Pass Manager";
|
|
|
|
}
|
|
|
|
|
2007-01-17 22:45:01 +01:00
|
|
|
// Print passes managed by this manager
|
|
|
|
void dumpPassStructure(unsigned Offset) {
|
|
|
|
llvm::cerr << std::string(Offset*2, ' ') << "Call Graph SCC Pass Manager\n";
|
|
|
|
for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
|
|
|
|
Pass *P = getContainedPass(Index);
|
|
|
|
P->dumpPassStructure(Offset + 1);
|
|
|
|
dumpLastUses(P, Offset+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *getContainedPass(unsigned N) {
|
|
|
|
assert ( N < PassVector.size() && "Pass number out of range!");
|
|
|
|
Pass *FP = static_cast<Pass *>(PassVector[N]);
|
|
|
|
return FP;
|
|
|
|
}
|
|
|
|
|
2007-02-27 16:00:39 +01:00
|
|
|
virtual PassManagerType getPassManagerType() const {
|
2007-01-17 22:45:01 +01:00
|
|
|
return PMT_CallGraphPassManager;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-05-13 02:00:25 +02:00
|
|
|
}
|
|
|
|
|
2007-05-03 03:11:54 +02:00
|
|
|
char CGPassManager::ID = 0;
|
2007-01-17 22:45:01 +01:00
|
|
|
/// run - Execute all of the passes scheduled for execution. Keep track of
|
|
|
|
/// whether any of the passes modifies the module, and if so, return true.
|
|
|
|
bool CGPassManager::runOnModule(Module &M) {
|
|
|
|
CallGraph &CG = getAnalysis<CallGraph>();
|
|
|
|
bool Changed = doInitialization(CG);
|
|
|
|
|
|
|
|
// Walk SCC
|
|
|
|
for (scc_iterator<CallGraph*> I = scc_begin(&CG), E = scc_end(&CG);
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
// Run all passes on current SCC
|
2007-04-16 20:10:23 +02:00
|
|
|
for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
|
2007-01-17 22:45:01 +01:00
|
|
|
Pass *P = getContainedPass(Index);
|
|
|
|
AnalysisUsage AnUsage;
|
|
|
|
P->getAnalysisUsage(AnUsage);
|
|
|
|
|
2007-03-05 21:01:30 +01:00
|
|
|
dumpPassInfo(P, EXECUTION_MSG, ON_CG_MSG, "");
|
2007-01-17 22:45:01 +01:00
|
|
|
dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
|
|
|
|
|
|
|
|
initializeAnalysisImpl(P);
|
|
|
|
|
2007-01-30 00:29:54 +01:00
|
|
|
StartPassTimer(P);
|
2007-01-17 22:45:01 +01:00
|
|
|
if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
|
2007-04-16 20:10:23 +02:00
|
|
|
Changed |= CGSP->runOnSCC(*I); // TODO : What if CG is changed ?
|
2007-01-17 22:45:01 +01:00
|
|
|
else {
|
2007-04-16 20:10:23 +02:00
|
|
|
FPPassManager *FPP = dynamic_cast<FPPassManager *>(P);
|
|
|
|
assert (FPP && "Invalid CGPassManager member");
|
|
|
|
|
|
|
|
// Run pass P on all functions current SCC
|
|
|
|
std::vector<CallGraphNode*> &SCC = *I;
|
|
|
|
for (unsigned i = 0, e = SCC.size(); i != e; ++i) {
|
|
|
|
Function *F = SCC[i]->getFunction();
|
|
|
|
if (F) {
|
2007-08-10 20:29:32 +02:00
|
|
|
dumpPassInfo(P, EXECUTION_MSG, ON_FUNCTION_MSG, F->getNameStart());
|
2007-03-05 21:01:30 +01:00
|
|
|
Changed |= FPP->runOnFunction(*F);
|
2007-02-01 23:38:33 +01:00
|
|
|
}
|
2007-04-16 20:10:23 +02:00
|
|
|
}
|
2007-01-17 22:45:01 +01:00
|
|
|
}
|
2007-01-30 00:29:54 +01:00
|
|
|
StopPassTimer(P);
|
2007-01-17 22:45:01 +01:00
|
|
|
|
|
|
|
if (Changed)
|
2007-03-05 21:01:30 +01:00
|
|
|
dumpPassInfo(P, MODIFICATION_MSG, ON_CG_MSG, "");
|
2007-01-17 22:45:01 +01:00
|
|
|
dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
|
2007-07-19 20:02:32 +02:00
|
|
|
|
|
|
|
verifyPreservedAnalysis(P);
|
2007-01-17 22:45:01 +01:00
|
|
|
removeNotPreservedAnalysis(P);
|
|
|
|
recordAvailableAnalysis(P);
|
2007-03-05 21:01:30 +01:00
|
|
|
removeDeadPasses(P, "", ON_CG_MSG);
|
2007-01-17 22:45:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Changed |= doFinalization(CG);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialize CG
|
|
|
|
bool CGPassManager::doInitialization(CallGraph &CG) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
|
|
|
|
Pass *P = getContainedPass(Index);
|
|
|
|
if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
|
|
|
|
Changed |= CGSP->doInitialization(CG);
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finalize CG
|
|
|
|
bool CGPassManager::doFinalization(CallGraph &CG) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
|
|
|
|
Pass *P = getContainedPass(Index);
|
|
|
|
if (CallGraphSCCPass *CGSP = dynamic_cast<CallGraphSCCPass *>(P))
|
|
|
|
Changed |= CGSP->doFinalization(CG);
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2007-01-23 22:55:17 +01:00
|
|
|
/// Assign pass manager to manage this pass.
|
2007-01-23 22:52:35 +01:00
|
|
|
void CallGraphSCCPass::assignPassManager(PMStack &PMS,
|
2007-04-16 20:10:23 +02:00
|
|
|
PassManagerType PreferredType) {
|
2007-01-23 22:52:35 +01:00
|
|
|
// Find CGPassManager
|
2007-07-19 11:42:01 +02:00
|
|
|
while (!PMS.empty() &&
|
|
|
|
PMS.top()->getPassManagerType() > PMT_CallGraphPassManager)
|
|
|
|
PMS.pop();
|
2007-01-23 22:52:35 +01:00
|
|
|
|
2008-05-03 00:13:33 +02:00
|
|
|
assert (!PMS.empty() && "Unable to handle Call Graph Pass");
|
2007-01-23 22:52:35 +01:00
|
|
|
CGPassManager *CGP = dynamic_cast<CGPassManager *>(PMS.top());
|
|
|
|
|
|
|
|
// Create new Call Graph SCC Pass Manager if it does not exist.
|
|
|
|
if (!CGP) {
|
|
|
|
|
|
|
|
assert (!PMS.empty() && "Unable to create Call Graph Pass Manager");
|
|
|
|
PMDataManager *PMD = PMS.top();
|
|
|
|
|
|
|
|
// [1] Create new Call Graph Pass Manager
|
|
|
|
CGP = new CGPassManager(PMD->getDepth() + 1);
|
|
|
|
|
|
|
|
// [2] Set up new manager's top level manager
|
|
|
|
PMTopLevelManager *TPM = PMD->getTopLevelManager();
|
|
|
|
TPM->addIndirectPassManager(CGP);
|
|
|
|
|
|
|
|
// [3] Assign manager to manage this new manager. This may create
|
|
|
|
// and push new managers into PMS
|
|
|
|
Pass *P = dynamic_cast<Pass *>(CGP);
|
2007-06-22 00:29:02 +02:00
|
|
|
TPM->schedulePass(P);
|
2007-01-23 22:52:35 +01:00
|
|
|
|
|
|
|
// [4] Push new manager into PMS
|
|
|
|
PMS.push(CGP);
|
|
|
|
}
|
|
|
|
|
|
|
|
CGP->add(this);
|
|
|
|
}
|
|
|
|
|
2003-08-31 03:54:59 +02:00
|
|
|
/// getAnalysisUsage - For this class, we declare that we require and preserve
|
|
|
|
/// the call graph. If the derived class implements this method, it should
|
|
|
|
/// always explicitly call the implementation here.
|
|
|
|
void CallGraphSCCPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<CallGraph>();
|
|
|
|
AU.addPreserved<CallGraph>();
|
|
|
|
}
|