X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FPass.cpp;h=dc49ac6aeecafb01ae6bfe718f98c5c2a07fbe80;hb=f28265402130eb03762d9a6333fd8f87765a8875;hp=40a3b7d17fe05c0427cd670066e89134aa040b6a;hpb=31f8499e83dc4dccbb57ea7e76d1fd49b7010d0c;p=oota-llvm.git diff --git a/lib/VMCore/Pass.cpp b/lib/VMCore/Pass.cpp index 40a3b7d17fe..dc49ac6aeec 100644 --- a/lib/VMCore/Pass.cpp +++ b/lib/VMCore/Pass.cpp @@ -1,10 +1,10 @@ //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// -// +// // The LLVM Compiler Infrastructure // // This file was developed by the LLVM research group and is distributed under // the University of Illinois Open Source License. See LICENSE.TXT for details. -// +// //===----------------------------------------------------------------------===// // // This file implements the LLVM Pass infrastructure. It is primarily @@ -17,14 +17,12 @@ #include "PassManagerT.h" // PassManagerT implementation #include "llvm/Module.h" #include "llvm/ModuleProvider.h" -#include "Support/STLExtras.h" -#include "Support/TypeInfo.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/Support/TypeInfo.h" +#include #include using namespace llvm; -// IncludeFile - Stub function used to help linking out. -IncludeFile::IncludeFile(void*) {} - //===----------------------------------------------------------------------===// // AnalysisID Class Implementation // @@ -37,13 +35,15 @@ static std::vector &getCFGOnlyAnalyses() { } void RegisterPassBase::setOnlyUsesCFG() { - getCFGOnlyAnalyses().push_back(PIObj); + getCFGOnlyAnalyses().push_back(&PIObj); } //===----------------------------------------------------------------------===// // AnalysisResolver Class Implementation // +AnalysisResolver::~AnalysisResolver() { +} void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { assert(P->Resolver == 0 && "Pass already in a PassManager!"); P->Resolver = AR; @@ -75,26 +75,32 @@ void AnalysisUsage::setPreservesCFG() { // PassManager implementation - The PassManager class is a simple Pimpl class // that wraps the PassManagerT template. // -PassManager::PassManager() : PM(new PassManagerT()) {} +PassManager::PassManager() : PM(new ModulePassManager()) {} PassManager::~PassManager() { delete PM; } -void PassManager::add(Pass *P) { PM->add(P); } -bool PassManager::run(Module &M) { return PM->run(M); } +void PassManager::add(Pass *P) { + ModulePass *MP = dynamic_cast(P); + assert(MP && "Not a modulepass?"); + PM->add(MP); +} +bool PassManager::run(Module &M) { return PM->runOnModule(M); } //===----------------------------------------------------------------------===// // FunctionPassManager implementation - The FunctionPassManager class // is a simple Pimpl class that wraps the PassManagerT template. It // is like PassManager, but only deals in FunctionPasses. // -FunctionPassManager::FunctionPassManager(ModuleProvider *P) : - PM(new PassManagerT()), MP(P) {} +FunctionPassManager::FunctionPassManager(ModuleProvider *P) : + PM(new FunctionPassManagerT()), MP(P) {} FunctionPassManager::~FunctionPassManager() { delete PM; } void FunctionPassManager::add(FunctionPass *P) { PM->add(P); } void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); } -bool FunctionPassManager::run(Function &F) { - Function *mF = MP->getModule()->getNamedFunction(F.getName()); - assert((&F == mF) && "ModuleProvider does not contain this function!"); - MP->materializeFunction(&F); - return PM->run(F); +bool FunctionPassManager::run(Function &F) { + std::string errstr; + if (MP->materializeFunction(&F, &errstr)) { + std::cerr << "Error reading bytecode file: " << errstr << "\n"; + abort(); + } + return PM->run(F); } @@ -103,15 +109,16 @@ bool FunctionPassManager::run(Function &F) { // amount of time each pass takes to execute. This only happens with // -time-passes is enabled on the command line. // -static cl::opt -EnableTiming("time-passes", +bool llvm::TimePassesIsEnabled = false; +static cl::opt +EnableTiming("time-passes", cl::location(TimePassesIsEnabled), cl::desc("Time each pass, printing elapsed time for each on exit")); // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to // a non null value (if the -time-passes option is enabled) or it leaves it // null. It may be called multiple times. void TimingInfo::createTheTimeInfo() { - if (!EnableTiming || TheTimeInfo) return; + if (!TimePassesIsEnabled || TheTimeInfo) return; // Constructed the first time this is called, iff -time-passes is enabled. // This guarantees that the object will be constructed before static globals, @@ -135,22 +142,31 @@ void PMDebug::PrintArgumentInformation(const Pass *P) { } void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, - Pass *P, Annotable *V) { + Pass *P, Module *M) { if (PassDebugging >= Executions) { - std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" + std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" << P->getPassName(); - if (V) { - std::cerr << "' on "; - - if (dynamic_cast(V)) { - std::cerr << "Module\n"; return; - } else if (Function *F = dynamic_cast(V)) - std::cerr << "Function '" << F->getName(); - else if (BasicBlock *BB = dynamic_cast(V)) - std::cerr << "BasicBlock '" << BB->getName(); - else if (Value *Val = dynamic_cast(V)) - std::cerr << typeid(*Val).name() << " '" << Val->getName(); - } + if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n"; + std::cerr << "'...\n"; + } +} + +void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, + Pass *P, Function *F) { + if (PassDebugging >= Executions) { + std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" + << P->getPassName(); + if (F) std::cerr << "' on Function '" << F->getName(); + std::cerr << "'...\n"; + } +} + +void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, + Pass *P, BasicBlock *BB) { + if (PassDebugging >= Executions) { + std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" + << P->getPassName(); + if (BB) std::cerr << "' on BasicBlock '" << BB->getName(); std::cerr << "'...\n"; } } @@ -171,7 +187,7 @@ void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, // Pass Implementation // -void Pass::addToPassManager(PassManagerT *PM, AnalysisUsage &AU) { +void ModulePass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) { PM->addPass(this, AU); } @@ -184,7 +200,7 @@ void Pass::dumpPassStructure(unsigned Offset) { std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; } -// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass. +// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass. // const char *Pass::getPassName() const { if (const PassInfo *PI = getPassInfo()) @@ -196,7 +212,7 @@ const char *Pass::getPassName() const { // to print out the contents of an analysis. Otherwise it is not necessary to // implement this method. // -void Pass::print(std::ostream &O) const { +void Pass::print(std::ostream &O,const Module*) const { O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; } @@ -208,7 +224,7 @@ void Pass::dump() const { //===----------------------------------------------------------------------===// // ImmutablePass Implementation // -void ImmutablePass::addToPassManager(PassManagerT *PM, +void ImmutablePass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) { PM->addPass(this, AU); } @@ -221,13 +237,13 @@ void ImmutablePass::addToPassManager(PassManagerT *PM, // run - On a module, we run this pass by initializing, runOnFunction'ing once // for every function in the module, then by finalizing. // -bool FunctionPass::run(Module &M) { +bool FunctionPass::runOnModule(Module &M) { bool Changed = doInitialization(M); - + for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) if (!I->isExternal()) // Passes are not run on external functions! Changed |= runOnFunction(*I); - + return Changed | doFinalization(M); } @@ -236,16 +252,17 @@ bool FunctionPass::run(Module &M) { bool FunctionPass::run(Function &F) { if (F.isExternal()) return false;// Passes are not run on external functions! - return doInitialization(*F.getParent()) | runOnFunction(F) - | doFinalization(*F.getParent()); + bool Changed = doInitialization(*F.getParent()); + Changed |= runOnFunction(F); + return Changed | doFinalization(*F.getParent()); } -void FunctionPass::addToPassManager(PassManagerT *PM, +void FunctionPass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) { PM->addPass(this, AU); } -void FunctionPass::addToPassManager(PassManagerT *PM, +void FunctionPass::addToPassManager(FunctionPassManagerT *PM, AnalysisUsage &AU) { PM->addPass(this, AU); } @@ -267,19 +284,23 @@ bool BasicBlockPass::runOnFunction(Function &F) { // To run directly on the basic block, we initialize, runOnBasicBlock, then // finalize. // -bool BasicBlockPass::run(BasicBlock &BB) { +bool BasicBlockPass::runPass(BasicBlock &BB) { Function &F = *BB.getParent(); Module &M = *F.getParent(); - return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) | - doFinalization(F) | doFinalization(M); + bool Changed = doInitialization(M); + Changed |= doInitialization(F); + Changed |= runOnBasicBlock(BB); + Changed |= doFinalization(F); + Changed |= doFinalization(M); + return Changed; } -void BasicBlockPass::addToPassManager(PassManagerT *PM, +void BasicBlockPass::addToPassManager(FunctionPassManagerT *PM, AnalysisUsage &AU) { PM->addPass(this, AU); } -void BasicBlockPass::addToPassManager(PassManagerT *PM, +void BasicBlockPass::addToPassManager(BasicBlockPassManager *PM, AnalysisUsage &AU) { PM->addPass(this, AU); } @@ -304,26 +325,25 @@ const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) { return (I != PassInfoMap->end()) ? I->second : 0; } -void RegisterPassBase::registerPass(PassInfo *PI) { +void RegisterPassBase::registerPass() { if (PassInfoMap == 0) PassInfoMap = new std::map(); - assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && + assert(PassInfoMap->find(PIObj.getTypeInfo()) == PassInfoMap->end() && "Pass already registered!"); - PIObj = PI; - PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); + PassInfoMap->insert(std::make_pair(TypeInfo(PIObj.getTypeInfo()), &PIObj)); // Notify any listeners... if (Listeners) for (std::vector::iterator I = Listeners->begin(), E = Listeners->end(); I != E; ++I) - (*I)->passRegistered(PI); + (*I)->passRegistered(&PIObj); } -void RegisterPassBase::unregisterPass(PassInfo *PI) { +void RegisterPassBase::unregisterPass() { assert(PassInfoMap && "Pass registered but not in map!"); std::map::iterator I = - PassInfoMap->find(PI->getTypeInfo()); + PassInfoMap->find(PIObj.getTypeInfo()); assert(I != PassInfoMap->end() && "Pass registered but not in map!"); // Remove pass from the map... @@ -337,10 +357,7 @@ void RegisterPassBase::unregisterPass(PassInfo *PI) { if (Listeners) for (std::vector::iterator I = Listeners->begin(), E = Listeners->end(); I != E; ++I) - (*I)->passUnregistered(PI); - - // Delete the PassInfo object itself... - delete PI; + (*I)->passUnregistered(&PIObj); } //===----------------------------------------------------------------------===// @@ -359,14 +376,14 @@ static std::map *AnalysisGroupInfoMap = 0; // RegisterAGBase::RegisterAGBase(const std::type_info &Interface, const std::type_info *Pass, bool isDefault) - : ImplementationInfo(0), isDefaultImplementation(isDefault) { + : RegisterPassBase(Interface, PassInfo::AnalysisGroup), + ImplementationInfo(0), isDefaultImplementation(isDefault) { InterfaceInfo = const_cast(Pass::lookupPassInfo(Interface)); - if (InterfaceInfo == 0) { // First reference to Interface, add it now. - InterfaceInfo = // Create the new PassInfo for the interface... - new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0); - registerPass(InterfaceInfo); - PIObj = 0; + if (InterfaceInfo == 0) { + // First reference to Interface, register it now. + registerPass(); + InterfaceInfo = &PIObj; } assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup && "Trying to join an analysis group that is a normal pass!"); @@ -415,7 +432,7 @@ RegisterAGBase::~RegisterAGBase() { if (AGI.DefaultImpl == ImplementationInfo) AGI.DefaultImpl = 0; - + AGI.Implementations.erase(ImplementationInfo); // Last member of this analysis group? Unregister PassInfo, delete map entry @@ -427,10 +444,11 @@ RegisterAGBase::~RegisterAGBase() { delete AnalysisGroupInfoMap; AnalysisGroupInfoMap = 0; } - - unregisterPass(InterfaceInfo); } } + + if (InterfaceInfo == &PIObj) + unregisterPass(); } @@ -468,3 +486,4 @@ void PassRegistrationListener::enumeratePasses() { E = PassInfoMap->end(); I != E; ++I) passEnumerate(I->second); } +