2010-07-20 19:23:55 +00: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-09-07 19:16:25 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2010-09-13 18:47:42 +00:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/PassSupport.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/Mutex.h"
|
2010-09-07 19:16:25 +00:00
|
|
|
#include <vector>
|
2010-07-20 21:22:24 +00:00
|
|
|
|
2010-07-20 23:41:56 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-07 20:48:10 +00:00
|
|
|
// FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
|
2010-07-20 21:22:24 +00:00
|
|
|
// Unfortunately, passes are registered with static ctors, and having
|
2011-04-15 05:18:47 +00:00
|
|
|
// llvm_shutdown clear this map prevents successful resurrection after
|
2010-07-20 21:22:24 +00:00
|
|
|
// 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 20:48:10 +00:00
|
|
|
static ManagedStatic<PassRegistry> PassRegistryObj;
|
|
|
|
PassRegistry *PassRegistry::getPassRegistry() {
|
|
|
|
return &*PassRegistryObj;
|
2010-07-20 21:22:24 +00:00
|
|
|
}
|
2010-07-20 19:23:55 +00:00
|
|
|
|
2010-09-16 23:44:50 +00:00
|
|
|
static ManagedStatic<sys::SmartMutex<true> > Lock;
|
2010-09-16 23:32:35 +00:00
|
|
|
|
2010-09-07 19:16:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PassRegistryImpl
|
|
|
|
//
|
|
|
|
|
2010-10-22 17:35:07 +00:00
|
|
|
namespace {
|
2010-09-07 19:16:25 +00: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-20 22:22:30 +00:00
|
|
|
std::vector<const PassInfo*> ToFree;
|
2010-09-07 19:16:25 +00:00
|
|
|
std::vector<PassRegistrationListener*> Listeners;
|
|
|
|
};
|
2010-10-22 17:35:07 +00:00
|
|
|
} // end anonymous namespace
|
2010-09-07 19:16:25 +00:00
|
|
|
|
|
|
|
void *PassRegistry::getImpl() const {
|
|
|
|
if (!pImpl)
|
|
|
|
pImpl = new PassRegistryImpl();
|
|
|
|
return pImpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Accessors
|
|
|
|
//
|
|
|
|
|
2010-09-07 20:48:10 +00:00
|
|
|
PassRegistry::~PassRegistry() {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 20:48:10 +00:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
|
2010-10-20 22:22:30 +00:00
|
|
|
|
|
|
|
for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(),
|
|
|
|
E = Impl->ToFree.end(); I != E; ++I)
|
|
|
|
delete *I;
|
|
|
|
|
2010-10-12 00:19:24 +00:00
|
|
|
delete Impl;
|
2010-09-07 20:48:10 +00:00
|
|
|
pImpl = 0;
|
|
|
|
}
|
|
|
|
|
2010-08-06 18:33:48 +00:00
|
|
|
const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 19:16:25 +00: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 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 19:16:25 +00: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 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
2010-07-20 23:41:56 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Registration mechanism
|
|
|
|
//
|
|
|
|
|
2010-10-20 22:22:30 +00:00
|
|
|
void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 19:16:25 +00:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
2010-07-20 19:23:55 +00:00
|
|
|
bool Inserted =
|
2010-09-07 19:16:25 +00:00
|
|
|
Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
|
2011-01-05 21:50:21 +00:00
|
|
|
assert(Inserted && "Pass registered multiple times!");
|
|
|
|
(void)Inserted;
|
2010-09-07 19:16:25 +00:00
|
|
|
Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
|
2010-07-20 23:41:56 +00:00
|
|
|
|
|
|
|
// Notify any listeners.
|
|
|
|
for (std::vector<PassRegistrationListener*>::iterator
|
2010-09-07 19:16:25 +00:00
|
|
|
I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
|
2010-07-20 23:41:56 +00:00
|
|
|
(*I)->passRegistered(&PI);
|
2010-10-20 22:22:30 +00:00
|
|
|
|
|
|
|
if (ShouldFree) Impl->ToFree.push_back(&PI);
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::unregisterPass(const PassInfo &PI) {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 19:16:25 +00: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 19:23:55 +00:00
|
|
|
|
|
|
|
// Remove pass from the map.
|
2010-09-07 19:16:25 +00:00
|
|
|
Impl->PassInfoMap.erase(I);
|
|
|
|
Impl->PassInfoStringMap.erase(PI.getPassArgument());
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::enumerateWith(PassRegistrationListener *L) {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 19:16:25 +00: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 19:23:55 +00:00
|
|
|
L->passEnumerate(I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Analysis Group Mechanisms.
|
2010-08-06 18:33:48 +00:00
|
|
|
void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
|
|
|
|
const void *PassID,
|
2010-07-21 17:52:45 +00:00
|
|
|
PassInfo& Registeree,
|
2010-10-20 22:22:30 +00:00
|
|
|
bool isDefault,
|
|
|
|
bool ShouldFree) {
|
2010-07-21 17:52:45 +00: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-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-16 23:32:35 +00:00
|
|
|
|
2010-07-21 17:52:45 +00:00
|
|
|
// Make sure we keep track of the fact that the implementation implements
|
|
|
|
// the interface.
|
|
|
|
ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
|
|
|
|
|
2010-09-07 19:16:25 +00:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
PassRegistryImpl::AnalysisGroupInfo &AGI =
|
|
|
|
Impl->AnalysisGroupInfoMap[InterfaceInfo];
|
2010-07-21 17:52:45 +00: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 19:23:55 +00:00
|
|
|
}
|
2010-10-20 22:22:30 +00:00
|
|
|
|
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
if (ShouldFree) Impl->ToFree.push_back(&Registeree);
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
2010-07-20 23:41:56 +00:00
|
|
|
|
|
|
|
void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-07 19:16:25 +00:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
|
|
|
Impl->Listeners.push_back(L);
|
2010-07-20 23:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
|
2010-09-16 23:44:50 +00:00
|
|
|
sys::SmartScopedLock<true> Guard(*Lock);
|
2010-09-15 23:03:33 +00:00
|
|
|
|
2010-09-07 20:48:10 +00: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 19:16:25 +00:00
|
|
|
PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
|
2010-07-20 23:41:56 +00:00
|
|
|
std::vector<PassRegistrationListener*>::iterator I =
|
2010-09-07 19:16:25 +00:00
|
|
|
std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
|
|
|
|
assert(I != Impl->Listeners.end() &&
|
|
|
|
"PassRegistrationListener not registered!");
|
|
|
|
Impl->Listeners.erase(I);
|
2010-07-20 23:41:56 +00:00
|
|
|
}
|