2010-07-21 03:23:55 +08:00
|
|
|
//===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-07-21 03:23:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2020-04-26 19:57:57 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include "llvm/PassInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-09-07 07:05:38 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
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
|
|
|
|
//
|
|
|
|
|
2017-09-07 07:05:38 +08:00
|
|
|
PassRegistry::~PassRegistry() = default;
|
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);
|
2020-12-30 11:23:24 +08:00
|
|
|
return PassInfoMap.lookup(TI);
|
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);
|
2020-12-30 11:23:24 +08:00
|
|
|
return PassInfoStringMap.lookup(Arg);
|
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());
|
|
|
|
}
|
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
|
|
|
|
2017-09-07 07:05:38 +08:00
|
|
|
auto I = llvm::find(Listeners, L);
|
2014-06-13 00:06:51 +08:00
|
|
|
Listeners.erase(I);
|
2010-07-21 07:41:56 +08:00
|
|
|
}
|