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
This commit is contained in:
Owen Anderson 2010-07-20 23:41:56 +00:00
parent 1154f426d7
commit 539673579e
4 changed files with 39 additions and 45 deletions

View File

@ -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 <map>
#include <set>
using namespace llvm;
#include <vector>
namespace llvm {
class PassInfo;
struct PassRegistrationListener;
class PassRegistry {
/// Guards the contents of this class.
mutable sys::SmartMutex<true> Lock;
@ -44,6 +45,8 @@ class PassRegistry {
std::set<const PassInfo *> Implementations;
};
std::map<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
std::vector<PassRegistrationListener*> Listeners;
public:
static PassRegistry *getPassRegistry();
@ -60,6 +63,8 @@ public:
bool isDefault);
void enumerateWith(PassRegistrationListener *L);
void addRegistrationListener(PassRegistrationListener* L);
void removeRegistrationListener(PassRegistrationListener *L);
};
}

View File

@ -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<passName>),
CFGOnly, is_analysis) {
}
};

View File

@ -234,13 +234,6 @@ PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
return PMT_BasicBlockPassManager;
}
//===----------------------------------------------------------------------===//
// Pass Registration mechanism
//
static std::vector<PassRegistrationListener*> *Listeners = 0;
static sys::SmartMutex<true> 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<true> Lock(ListenersLock);
if (Listeners)
for (std::vector<PassRegistrationListener*>::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<PassInfo*>(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<true> Lock(ListenersLock);
if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
Listeners->push_back(this);
PassRegistry::getPassRegistry()->addRegistrationListener(this);
}
// dtor - Remove object from list of listeners...
PassRegistrationListener::~PassRegistrationListener() {
sys::SmartScopedLock<true> Lock(ListenersLock);
std::vector<PassRegistrationListener*>::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

View File

@ -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<true> Guard(Lock);
bool Inserted =
PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
PassInfoStringMap[PI.getPassArgument()] = &PI;
// Notify any listeners.
for (std::vector<PassRegistrationListener*>::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<true> Guard(Lock);
Listeners.push_back(L);
}
void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
sys::SmartScopedLock<true> Guard(Lock);
std::vector<PassRegistrationListener*>::iterator I =
std::find(Listeners.begin(), Listeners.end(), L);
assert(I != Listeners.end() && "PassRegistrationListener not registered!");
Listeners.erase(I);
}