2010-07-21 03:23:55 +08: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"
|
2016-08-12 06:21:41 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/PassSupport.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-07-21 05:22:24 +08:00
|
|
|
|
2010-07-21 07:41:56 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-08 04:48:10 +08:00
|
|
|
// FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
|
2010-07-21 05:22:24 +08:00
|
|
|
// Unfortunately, passes are registered with static ctors, and having
|
2011-04-15 13:18:47 +08:00
|
|
|
// llvm_shutdown clear this map prevents successful resurrection after
|
2010-07-21 05:22:24 +08: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-08 04:48:10 +08:00
|
|
|
static ManagedStatic<PassRegistry> PassRegistryObj;
|
|
|
|
PassRegistry *PassRegistry::getPassRegistry() {
|
|
|
|
return &*PassRegistryObj;
|
2010-07-21 05:22:24 +08:00
|
|
|
}
|
2010-07-21 03:23:55 +08:00
|
|
|
|
2010-09-08 03:16:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Accessors
|
|
|
|
//
|
|
|
|
|
2014-10-06 07:59:03 +08:00
|
|
|
PassRegistry::~PassRegistry() {}
|
2010-09-08 04:48:10 +08:00
|
|
|
|
2010-08-07 02:33:48 +08:00
|
|
|
const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
|
2015-03-06 01:53:00 +08:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
2014-06-13 00:06:51 +08:00
|
|
|
MapType::const_iterator I = PassInfoMap.find(TI);
|
|
|
|
return I != PassInfoMap.end() ? I->second : nullptr;
|
2010-07-21 03:23:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
|
2015-03-06 01:53:00 +08:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
2014-06-13 00:06:51 +08:00
|
|
|
StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
|
|
|
|
return I != PassInfoStringMap.end() ? I->second : nullptr;
|
2010-07-21 03:23:55 +08:00
|
|
|
}
|
|
|
|
|
2010-07-21 07:41:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass Registration mechanism
|
|
|
|
//
|
|
|
|
|
2010-10-21 06:22:30 +08:00
|
|
|
void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
|
2014-06-13 00:06:51 +08:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2010-07-21 03:23:55 +08:00
|
|
|
bool Inserted =
|
2014-10-06 07:59:03 +08:00
|
|
|
PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
|
2011-01-06 05:50:21 +08:00
|
|
|
assert(Inserted && "Pass registered multiple times!");
|
|
|
|
(void)Inserted;
|
2014-06-13 00:06:51 +08:00
|
|
|
PassInfoStringMap[PI.getPassArgument()] = &PI;
|
2014-10-06 07:59:03 +08:00
|
|
|
|
2010-07-21 07:41:56 +08:00
|
|
|
// Notify any listeners.
|
2014-10-06 08:06:48 +08:00
|
|
|
for (auto *Listener : Listeners)
|
|
|
|
Listener->passRegistered(&PI);
|
2014-10-06 07:59:03 +08:00
|
|
|
|
|
|
|
if (ShouldFree)
|
|
|
|
ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
|
2010-07-21 03:23:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::enumerateWith(PassRegistrationListener *L) {
|
2014-06-13 00:06:51 +08:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
2014-10-06 08:06:48 +08:00
|
|
|
for (auto PassInfoPair : PassInfoMap)
|
|
|
|
L->passEnumerate(PassInfoPair.second);
|
2010-07-21 03:23:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Analysis Group Mechanisms.
|
2014-10-06 07:59:03 +08:00
|
|
|
void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
|
2010-08-07 02:33:48 +08:00
|
|
|
const void *PassID,
|
2014-10-06 07:59:03 +08:00
|
|
|
PassInfo &Registeree, bool isDefault,
|
2010-10-21 06:22:30 +08:00
|
|
|
bool ShouldFree) {
|
2014-10-06 07:59:03 +08:00
|
|
|
PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
|
2014-04-09 14:08:46 +08:00
|
|
|
if (!InterfaceInfo) {
|
2010-07-22 01:52:45 +08:00
|
|
|
// First reference to Interface, register it now.
|
|
|
|
registerPass(Registeree);
|
|
|
|
InterfaceInfo = &Registeree;
|
|
|
|
}
|
2014-10-06 07:59:03 +08:00
|
|
|
assert(Registeree.isAnalysisGroup() &&
|
2010-07-22 01:52:45 +08:00
|
|
|
"Trying to join an analysis group that is a normal pass!");
|
|
|
|
|
|
|
|
if (PassID) {
|
2014-10-06 07:59:03 +08:00
|
|
|
PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
|
2010-07-22 01:52:45 +08:00
|
|
|
assert(ImplementationInfo &&
|
|
|
|
"Must register pass before adding to AnalysisGroup!");
|
|
|
|
|
2014-06-13 00:06:51 +08:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2014-10-06 07:59:03 +08:00
|
|
|
|
2010-07-22 01:52:45 +08:00
|
|
|
// Make sure we keep track of the fact that the implementation implements
|
|
|
|
// the interface.
|
|
|
|
ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
|
|
|
|
|
|
|
|
if (isDefault) {
|
2014-04-15 14:32:26 +08:00
|
|
|
assert(InterfaceInfo->getNormalCtor() == nullptr &&
|
2010-07-22 01:52:45 +08:00
|
|
|
"Default implementation for analysis group already specified!");
|
2014-10-06 07:59:03 +08:00
|
|
|
assert(
|
|
|
|
ImplementationInfo->getNormalCtor() &&
|
|
|
|
"Cannot specify pass as default if it does not have a default ctor");
|
2010-07-22 01:52:45 +08:00
|
|
|
InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
|
2014-01-17 05:44:34 +08:00
|
|
|
InterfaceInfo->setTargetMachineCtor(
|
|
|
|
ImplementationInfo->getTargetMachineCtor());
|
2010-07-22 01:52:45 +08:00
|
|
|
}
|
2010-07-21 03:23:55 +08:00
|
|
|
}
|
2014-10-06 07:59:03 +08:00
|
|
|
|
2014-04-15 23:17:14 +08:00
|
|
|
if (ShouldFree)
|
2014-06-13 00:06:51 +08:00
|
|
|
ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
|
2010-07-21 03:23:55 +08:00
|
|
|
}
|
2010-07-21 07:41:56 +08:00
|
|
|
|
|
|
|
void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
|
2014-06-13 00:06:51 +08:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
|
|
|
Listeners.push_back(L);
|
2010-07-21 07:41:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
|
2014-06-13 00:06:51 +08:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2014-10-06 07:59:03 +08:00
|
|
|
|
2016-08-12 06:21:41 +08:00
|
|
|
auto I = find(Listeners, L);
|
2014-06-13 00:06:51 +08:00
|
|
|
Listeners.erase(I);
|
2010-07-21 07:41:56 +08:00
|
|
|
}
|