2010-07-20 21:23:55 +02:00
|
|
|
//===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the PassRegistry, with which passes are registered on
|
|
|
|
// initialization, and supports the PassManager in dependency resolution.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/PassRegistry.h"
|
2010-07-21 01:41:56 +02:00
|
|
|
#include "llvm/PassSupport.h"
|
2010-07-20 23:22:24 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Mutex.h"
|
2010-09-07 21:16:25 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2010-09-13 20:47:42 +02:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2010-09-07 21:16:25 +02:00
|
|
|
#include <vector>
|
2010-07-20 23:22:24 +02:00
|
|
|
|
2010-07-21 01:41:56 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-07 22:48:10 +02:00
|
|
|
// FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
|
2010-07-20 23:22:24 +02:00
|
|
|
// Unfortunately, passes are registered with static ctors, and having
|
|
|
|
// llvm_shutdown clear this map prevents successful ressurection after
|
|
|
|
// llvm_shutdown is run. Ideally we should find a solution so that we don't
|
|
|
|
// leak the map, AND can still resurrect after shutdown.
|
2010-09-07 22:48:10 +02:00
|
|
|
static ManagedStatic<PassRegistry> PassRegistryObj;
|
|
|
|
PassRegistry *PassRegistry::getPassRegistry() {
|
|
|
|
return &*PassRegistryObj;
|
2010-07-20 23:22:24 +02:00
|
|
|
}
|
2010-07-20 21:23:55 +02:00
|
|
|
|
2010-09-17 01:44:50 +02:00
|
|
|
static ManagedStatic<sys::SmartMutex<true> > Lock;
|
2010-09-17 01:32:35 +02:00
|
|
|
|
2010-09-07 21:16:25 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PassRegistryImpl
|
|
|
|
//
|
|
|
|
|
2010-10-22 19:35:07 +02:00
|
|
|
namespace {
|
2010-09-07 21:16:25 +02:00
|
|
|
struct PassRegistryImpl {
|
|
|
|
/// PassInfoMap - Keep track of the PassInfo object for each registered pass.
|
|
|
|
typedef DenseMap<const void*, const PassInfo*> MapType;
|
|
|
|
MapType PassInfoMap;
|
|
|
|
|
|
|
|
typedef StringMap<const PassInfo*> StringMapType;
|
|
|
|
StringMapType PassInfoStringMap;
|
|
|
|
|
|
|
|
/// AnalysisGroupInfo - Keep track of information for each analysis group.
|
|
|
|
struct AnalysisGroupInfo {
|
|
|
|
SmallPtrSet<const PassInfo *, 8> Implementations;
|
|
|
|
};
|
|
|
|
DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
|
|
|
|
|
2010-10-21 00:22:30 +02:00
|
|
|
std::vector<const PassInfo*> ToFree;
|
2010-09-07 21:16:25 +02:00
|
|
|
std::vector<PassRegistrationListener*> Listeners;
|
|
|
|
};
|
2010-10-22 19:35:07 +02:00
|
|
|
} // end anonymous namespace
|
2010-09-07 21:16:25 +02:00
|
|
|
|
|
|
|
void *PassRegistry::getImpl() const {
|
|
|
|
if (!pImpl)
|
|
|
|
pImpl = new PassRegistryImpl();
|
|
|
|
return pImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Accessors
|
|
|
|
//
|
|
|
|
|
2010-09-07 22:48:10 +02:00
|
|
|
PassRegistry::~PassRegistry() {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 22:48:10 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
|
2010-10-21 00:22:30 +02:00
|
|
|
|
|
|
|
for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(),
|
|
|
|
E = Impl->ToFree.end(); I != E; ++I)
|
|
|
|
delete *I;
|
|
|
|
|
2010-10-12 02:19:24 +02:00
|
|
|
delete Impl;
|
2010-09-07 22:48:10 +02:00
|
|
|
pImpl = 0;
|
|
|
|
}
|
|
|
|
|
2010-08-06 20:33:48 +02:00
|
|
|
const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
|
|
|
|
return I != Impl->PassInfoMap.end() ? I->second : 0;
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
PassRegistryImpl::StringMapType::const_iterator
|
|
|
|
I = Impl->PassInfoStringMap.find(Arg);
|
|
|
|
return I != Impl->PassInfoStringMap.end() ? I->second : 0;
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
2010-07-21 01:41:56 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Registration mechanism
|
|
|
|
//
|
|
|
|
|
2010-10-21 00:22:30 +02:00
|
|
|
void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
2010-07-20 21:23:55 +02:00
|
|
|
bool Inserted =
|
2010-09-07 21:16:25 +02:00
|
|
|
Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
|
2010-07-20 21:23:55 +02:00
|
|
|
assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
|
2010-09-07 21:16:25 +02:00
|
|
|
Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
|
2010-07-21 01:41:56 +02:00
|
|
|
|
|
|
|
// Notify any listeners.
|
|
|
|
for (std::vector<PassRegistrationListener*>::iterator
|
2010-09-07 21:16:25 +02:00
|
|
|
I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
|
2010-07-21 01:41:56 +02:00
|
|
|
(*I)->passRegistered(&PI);
|
2010-10-21 00:22:30 +02:00
|
|
|
|
|
|
|
if (ShouldFree) Impl->ToFree.push_back(&PI);
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::unregisterPass(const PassInfo &PI) {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
PassRegistryImpl::MapType::iterator I =
|
|
|
|
Impl->PassInfoMap.find(PI.getTypeInfo());
|
|
|
|
assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
|
2010-07-20 21:23:55 +02:00
|
|
|
|
|
|
|
// Remove pass from the map.
|
2010-09-07 21:16:25 +02:00
|
|
|
Impl->PassInfoMap.erase(I);
|
|
|
|
Impl->PassInfoStringMap.erase(PI.getPassArgument());
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::enumerateWith(PassRegistrationListener *L) {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
|
|
|
|
E = Impl->PassInfoMap.end(); I != E; ++I)
|
2010-07-20 21:23:55 +02:00
|
|
|
L->passEnumerate(I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Analysis Group Mechanisms.
|
2010-08-06 20:33:48 +02:00
|
|
|
void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
|
|
|
|
const void *PassID,
|
2010-07-21 19:52:45 +02:00
|
|
|
PassInfo& Registeree,
|
2010-10-21 00:22:30 +02:00
|
|
|
bool isDefault,
|
|
|
|
bool ShouldFree) {
|
2010-07-21 19:52:45 +02:00
|
|
|
PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
|
|
|
|
if (InterfaceInfo == 0) {
|
|
|
|
// First reference to Interface, register it now.
|
|
|
|
registerPass(Registeree);
|
|
|
|
InterfaceInfo = &Registeree;
|
|
|
|
}
|
|
|
|
assert(Registeree.isAnalysisGroup() &&
|
|
|
|
"Trying to join an analysis group that is a normal pass!");
|
|
|
|
|
|
|
|
if (PassID) {
|
|
|
|
PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
|
|
|
|
assert(ImplementationInfo &&
|
|
|
|
"Must register pass before adding to AnalysisGroup!");
|
|
|
|
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-17 01:32:35 +02:00
|
|
|
|
2010-07-21 19:52:45 +02:00
|
|
|
// Make sure we keep track of the fact that the implementation implements
|
|
|
|
// the interface.
|
|
|
|
ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
|
|
|
|
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
PassRegistryImpl::AnalysisGroupInfo &AGI =
|
|
|
|
Impl->AnalysisGroupInfoMap[InterfaceInfo];
|
2010-07-21 19:52:45 +02:00
|
|
|
assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
|
|
|
|
"Cannot add a pass to the same analysis group more than once!");
|
|
|
|
AGI.Implementations.insert(ImplementationInfo);
|
|
|
|
if (isDefault) {
|
|
|
|
assert(InterfaceInfo->getNormalCtor() == 0 &&
|
|
|
|
"Default implementation for analysis group already specified!");
|
|
|
|
assert(ImplementationInfo->getNormalCtor() &&
|
|
|
|
"Cannot specify pass as default if it does not have a default ctor");
|
|
|
|
InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
|
|
|
|
}
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
2010-10-21 00:22:30 +02:00
|
|
|
|
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
if (ShouldFree) Impl->ToFree.push_back(&Registeree);
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
2010-07-21 01:41:56 +02:00
|
|
|
|
|
|
|
void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
Impl->Listeners.push_back(L);
|
2010-07-21 01:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
|
2010-09-17 01:44:50 +02:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-16 01:03:33 +02:00
|
|
|
|
2010-09-07 22:48:10 +02:00
|
|
|
// NOTE: This is necessary, because removeRegistrationListener() can be called
|
|
|
|
// as part of the llvm_shutdown sequence. Since we have no control over the
|
|
|
|
// order of that sequence, we need to gracefully handle the case where the
|
|
|
|
// PassRegistry is destructed before the object that triggers this call.
|
|
|
|
if (!pImpl) return;
|
|
|
|
|
2010-09-07 21:16:25 +02:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
2010-07-21 01:41:56 +02:00
|
|
|
std::vector<PassRegistrationListener*>::iterator I =
|
2010-09-07 21:16:25 +02:00
|
|
|
std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
|
|
|
|
assert(I != Impl->Listeners.end() &&
|
|
|
|
"PassRegistrationListener not registered!");
|
|
|
|
Impl->Listeners.erase(I);
|
2010-07-21 01:41:56 +02:00
|
|
|
}
|