From 539673579ec79b75a95ef9daefc6a8b2fc8552f5 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 20 Jul 2010 23:41:56 +0000 Subject: [PATCH] Move the handling of PassRegistrationListener's to PassRegistry. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@108966 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/PassRegistry.h | 11 +++++++--- include/llvm/PassSupport.h | 8 +++----- lib/VMCore/Pass.cpp | 40 +++---------------------------------- lib/VMCore/PassRegistry.cpp | 25 +++++++++++++++++++++++ 4 files changed, 39 insertions(+), 45 deletions(-) diff --git a/include/llvm/PassRegistry.h b/include/llvm/PassRegistry.h index 1f627d8bfca..7b1b6fc703a 100644 --- a/include/llvm/PassRegistry.h +++ b/include/llvm/PassRegistry.h @@ -17,17 +17,18 @@ #ifndef LLVM_PASSREGISTRY_H #define LLVM_PASSREGISTRY_H -#include "llvm/PassSupport.h" #include "llvm/ADT/StringMap.h" #include "llvm/System/DataTypes.h" #include "llvm/System/Mutex.h" #include #include - -using namespace llvm; +#include namespace llvm { +class PassInfo; +struct PassRegistrationListener; + class PassRegistry { /// Guards the contents of this class. mutable sys::SmartMutex Lock; @@ -44,6 +45,8 @@ class PassRegistry { std::set Implementations; }; std::map AnalysisGroupInfoMap; + + std::vector Listeners; public: static PassRegistry *getPassRegistry(); @@ -60,6 +63,8 @@ public: bool isDefault); void enumerateWith(PassRegistrationListener *L); + void addRegistrationListener(PassRegistrationListener* L); + void removeRegistrationListener(PassRegistrationListener *L); }; } diff --git a/include/llvm/PassSupport.h b/include/llvm/PassSupport.h index b0183513386..4bbafdb8552 100644 --- a/include/llvm/PassSupport.h +++ b/include/llvm/PassSupport.h @@ -22,6 +22,7 @@ #define LLVM_PASS_SUPPORT_H #include "Pass.h" +#include "llvm/PassRegistry.h" namespace llvm { @@ -57,7 +58,7 @@ public: : PassName(name), PassArgument(arg), PassID(pi), IsCFGOnlyPass(isCFGOnly), IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) { - registerPass(); + PassRegistry::getPassRegistry()->registerPass(*this); } /// PassInfo ctor - Do not call this directly, this should only be invoked /// through RegisterPass. This version is for use by analysis groups; it @@ -126,10 +127,6 @@ public: return ItfImpl; } -protected: - void registerPass(); - void unregisterPass(); - private: void operator=(const PassInfo &); // do not implement PassInfo(const PassInfo &); // do not implement @@ -165,6 +162,7 @@ struct RegisterPass : public PassInfo { : PassInfo(Name, PassArg, intptr_t(&passName::ID), PassInfo::NormalCtor_t(callDefaultCtor), CFGOnly, is_analysis) { + } }; diff --git a/lib/VMCore/Pass.cpp b/lib/VMCore/Pass.cpp index 255be38cd6d..75ee17cd5a5 100644 --- a/lib/VMCore/Pass.cpp +++ b/lib/VMCore/Pass.cpp @@ -234,13 +234,6 @@ PassManagerType BasicBlockPass::getPotentialPassManagerType() const { return PMT_BasicBlockPassManager; } -//===----------------------------------------------------------------------===// -// Pass Registration mechanism -// - -static std::vector *Listeners = 0; -static sys::SmartMutex ListenersLock; - // getPassInfo - Return the PassInfo data structure that corresponds to this // pass... const PassInfo *Pass::getPassInfo() const { @@ -255,21 +248,6 @@ const PassInfo *Pass::lookupPassInfo(StringRef Arg) { return PassRegistry::getPassRegistry()->getPassInfo(Arg); } -void PassInfo::registerPass() { - PassRegistry::getPassRegistry()->registerPass(*this); - - // Notify any listeners. - sys::SmartScopedLock Lock(ListenersLock); - if (Listeners) - for (std::vector::iterator - I = Listeners->begin(), E = Listeners->end(); I != E; ++I) - (*I)->passRegistered(this); -} - -void PassInfo::unregisterPass() { - PassRegistry::getPassRegistry()->unregisterPass(*this); -} - Pass *PassInfo::createPass() const { assert((!isAnalysisGroup() || NormalCtor) && "No default implementation found for analysis group!"); @@ -292,7 +270,7 @@ RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID, const_cast(Pass::lookupPassInfo(InterfaceID)); if (InterfaceInfo == 0) { // First reference to Interface, register it now. - registerPass(); + PassRegistry::getPassRegistry()->registerPass(*this); InterfaceInfo = this; } assert(isAnalysisGroup() && @@ -320,24 +298,12 @@ RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID, // PassRegistrationListener ctor - Add the current object to the list of // PassRegistrationListeners... PassRegistrationListener::PassRegistrationListener() { - sys::SmartScopedLock Lock(ListenersLock); - if (!Listeners) Listeners = new std::vector(); - Listeners->push_back(this); + PassRegistry::getPassRegistry()->addRegistrationListener(this); } // dtor - Remove object from list of listeners... PassRegistrationListener::~PassRegistrationListener() { - sys::SmartScopedLock Lock(ListenersLock); - 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; - } + PassRegistry::getPassRegistry()->removeRegistrationListener(this); } // enumeratePasses - Iterate over the registered passes, calling the diff --git a/lib/VMCore/PassRegistry.cpp b/lib/VMCore/PassRegistry.cpp index 18a8cca56fc..140719e12b3 100644 --- a/lib/VMCore/PassRegistry.cpp +++ b/lib/VMCore/PassRegistry.cpp @@ -13,9 +13,12 @@ //===----------------------------------------------------------------------===// #include "llvm/PassRegistry.h" +#include "llvm/PassSupport.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/ManagedStatic.h" +using namespace llvm; + static PassRegistry *PassRegistryObj = 0; PassRegistry *PassRegistry::getPassRegistry() { // Use double-checked locking to safely initialize the registrar when @@ -69,12 +72,21 @@ const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { return I != PassInfoStringMap.end() ? I->second : 0; } +//===----------------------------------------------------------------------===// +// Pass Registration mechanism +// + void PassRegistry::registerPass(const PassInfo &PI) { sys::SmartScopedLock Guard(Lock); bool Inserted = PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second; assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted; PassInfoStringMap[PI.getPassArgument()] = Π + + // Notify any listeners. + for (std::vector::iterator + I = Listeners.begin(), E = Listeners.end(); I != E; ++I) + (*I)->passRegistered(&PI); } void PassRegistry::unregisterPass(const PassInfo &PI) { @@ -112,3 +124,16 @@ void PassRegistry::registerAnalysisGroup(PassInfo *InterfaceInfo, InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); } } + +void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { + sys::SmartScopedLock Guard(Lock); + Listeners.push_back(L); +} + +void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { + sys::SmartScopedLock Guard(Lock); + std::vector::iterator I = + std::find(Listeners.begin(), Listeners.end(), L); + assert(I != Listeners.end() && "PassRegistrationListener not registered!"); + Listeners.erase(I); +} -- 2.34.1