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"
|
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"
|
2013-07-03 18:38:08 +00:00
|
|
|
#include "llvm/Support/RWMutex.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-07 19:16:25 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Accessors
|
|
|
|
//
|
|
|
|
|
2010-09-07 20:48:10 +00:00
|
|
|
PassRegistry::~PassRegistry() {
|
|
|
|
}
|
|
|
|
|
2010-08-06 18:33:48 +00:00
|
|
|
const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
|
|
|
MapType::const_iterator I = PassInfoMap.find(TI);
|
|
|
|
return I != PassInfoMap.end() ? I->second : nullptr;
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
|
|
|
StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
|
|
|
|
return I != PassInfoStringMap.end() ? I->second : nullptr;
|
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) {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2010-07-20 19:23:55 +00:00
|
|
|
bool Inserted =
|
2014-06-12 16:06:51 +00:00
|
|
|
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;
|
2014-06-12 16:06:51 +00:00
|
|
|
PassInfoStringMap[PI.getPassArgument()] = &PI;
|
2010-07-20 23:41:56 +00:00
|
|
|
|
|
|
|
// Notify any listeners.
|
|
|
|
for (std::vector<PassRegistrationListener*>::iterator
|
2014-06-12 16:06:51 +00:00
|
|
|
I = Listeners.begin(), E = Listeners.end(); I != E; ++I)
|
2010-07-20 23:41:56 +00:00
|
|
|
(*I)->passRegistered(&PI);
|
2010-10-20 22:22:30 +00:00
|
|
|
|
2014-06-12 16:06:51 +00:00
|
|
|
if (ShouldFree) ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::unregisterPass(const PassInfo &PI) {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
|
|
|
MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
|
|
|
|
assert(I != PassInfoMap.end() && "Pass registered but not in map!");
|
2010-07-20 19:23:55 +00:00
|
|
|
|
|
|
|
// Remove pass from the map.
|
2014-06-12 16:06:51 +00:00
|
|
|
PassInfoMap.erase(I);
|
|
|
|
PassInfoStringMap.erase(PI.getPassArgument());
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::enumerateWith(PassRegistrationListener *L) {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
|
|
|
for (auto I = PassInfoMap.begin(), E = 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));
|
2014-04-09 06:08:46 +00:00
|
|
|
if (!InterfaceInfo) {
|
2010-07-21 17:52:45 +00:00
|
|
|
// 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!");
|
|
|
|
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedWriter<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);
|
|
|
|
|
2014-06-12 16:06:51 +00:00
|
|
|
AnalysisGroupInfo &AGI = 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) {
|
2014-04-15 06:32:26 +00:00
|
|
|
assert(InterfaceInfo->getNormalCtor() == nullptr &&
|
2010-07-21 17:52:45 +00:00
|
|
|
"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());
|
2014-01-16 21:44:34 +00:00
|
|
|
InterfaceInfo->setTargetMachineCtor(
|
|
|
|
ImplementationInfo->getTargetMachineCtor());
|
2010-07-21 17:52:45 +00:00
|
|
|
}
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
2010-10-20 22:22:30 +00:00
|
|
|
|
2014-04-15 15:17:14 +00:00
|
|
|
if (ShouldFree)
|
2014-06-12 16:06:51 +00:00
|
|
|
ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
|
2010-07-20 19:23:55 +00:00
|
|
|
}
|
2010-07-20 23:41:56 +00:00
|
|
|
|
|
|
|
void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
|
|
|
Listeners.push_back(L);
|
2010-07-20 23:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
|
2014-06-12 16:06:51 +00:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2010-09-07 20:48:10 +00:00
|
|
|
|
2014-06-12 16:06:51 +00:00
|
|
|
auto I = std::find(Listeners.begin(), Listeners.end(), L);
|
|
|
|
Listeners.erase(I);
|
2010-07-20 23:41:56 +00:00
|
|
|
}
|