From 54bbdb46777455fd2386dfcdacc555ca76098151 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 23 Jul 2002 18:08:00 +0000 Subject: [PATCH] Add support for pass registration git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3018 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Pass.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/lib/VMCore/Pass.cpp b/lib/VMCore/Pass.cpp index 4b29371fff8..2480d70bb2f 100644 --- a/lib/VMCore/Pass.cpp +++ b/lib/VMCore/Pass.cpp @@ -11,6 +11,7 @@ #include "llvm/Module.h" #include "Support/STLExtras.h" #include "Support/CommandLine.h" +#include "Support/TypeInfo.h" #include #include #include @@ -284,3 +285,95 @@ void BasicBlockPass::addToPassManager(PassManagerT *PM, PM->addPass(this, AU); } + +//===----------------------------------------------------------------------===// +// Pass Registration mechanism +// +static std::map *PassInfoMap = 0; +static std::vector *Listeners = 0; + +// getPassInfo - Return the PassInfo data structure that corresponds to this +// pass... +const PassInfo *Pass::getPassInfo() const { + assert(PassInfoMap && "PassInfoMap not constructed yet??"); + std::map::iterator I = + PassInfoMap->find(typeid(*this)); + assert(I != PassInfoMap->end() && "Pass has not been registered!"); + return I->second; +} + +void RegisterPassBase::registerPass(PassInfo *PI) { + if (PassInfoMap == 0) + PassInfoMap = new std::map(); + + assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && + "Pass already registered!"); + PIObj = PI; + PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); + + // Notify any listeners... + if (Listeners) + for (std::vector::iterator + I = Listeners->begin(), E = Listeners->end(); I != E; ++I) + (*I)->passRegistered(PI); +} + +RegisterPassBase::~RegisterPassBase() { + assert(PassInfoMap && "Pass registered but not in map!"); + std::map::iterator I = + PassInfoMap->find(PIObj->getTypeInfo()); + assert(I != PassInfoMap->end() && "Pass registered but not in map!"); + + // Remove pass from the map... + PassInfoMap->erase(I); + if (PassInfoMap->empty()) { + delete PassInfoMap; + PassInfoMap = 0; + } + + // Notify any listeners... + if (Listeners) + for (std::vector::iterator + I = Listeners->begin(), E = Listeners->end(); I != E; ++I) + (*I)->passUnregistered(PIObj); + + // Delete the PassInfo object itself... + delete PIObj; +} + + + +//===----------------------------------------------------------------------===// +// PassRegistrationListener implementation +// + +// PassRegistrationListener ctor - Add the current object to the list of +// PassRegistrationListeners... +PassRegistrationListener::PassRegistrationListener() { + if (!Listeners) Listeners = new std::vector(); + Listeners->push_back(this); +} + +// dtor - Remove object from list of listeners... +PassRegistrationListener::~PassRegistrationListener() { + std::vector::iterator I = + std::find(Listeners->begin(), Listeners->end(), this); + assert(Listeners && I != Listeners->end() && + "PassRegistrationListener not registered!"); + Listeners->erase(I); + + if (Listeners->empty()) { + delete Listeners; + Listeners = 0; + } +} + +// enumeratePasses - Iterate over the registered passes, calling the +// passEnumerate callback on each PassInfo object. +// +void PassRegistrationListener::enumeratePasses() { + if (PassInfoMap) + for (std::map::iterator I = PassInfoMap->begin(), + E = PassInfoMap->end(); I != E; ++I) + passEnumerate(I->second); +} -- 2.34.1