2010-07-20 21:23:55 +02:00
|
|
|
//===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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-20 21:23:55 +02: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 00:21:41 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2020-04-26 13:57:57 +02:00
|
|
|
#include "llvm/Pass.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include "llvm/PassInfo.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2017-09-07 01:05:38 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
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
|
2011-04-15 07:18:47 +02:00
|
|
|
// llvm_shutdown clear this map prevents successful resurrection after
|
2010-07-20 23:22:24 +02: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 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-07 21:16:25 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Accessors
|
|
|
|
//
|
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
PassRegistry::~PassRegistry() = default;
|
2010-09-07 22:48:10 +02:00
|
|
|
|
2010-08-06 20:33:48 +02:00
|
|
|
const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
|
2015-03-05 18:53:00 +01:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
2020-12-30 04:23:24 +01:00
|
|
|
return PassInfoMap.lookup(TI);
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
|
2015-03-05 18:53:00 +01:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
2020-12-30 04:23:24 +01:00
|
|
|
return PassInfoStringMap.lookup(Arg);
|
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) {
|
2014-06-12 18:06:51 +02:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2010-07-20 21:23:55 +02:00
|
|
|
bool Inserted =
|
2014-10-06 01:59:03 +02:00
|
|
|
PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
|
2011-01-05 22:50:21 +01:00
|
|
|
assert(Inserted && "Pass registered multiple times!");
|
|
|
|
(void)Inserted;
|
2014-06-12 18:06:51 +02:00
|
|
|
PassInfoStringMap[PI.getPassArgument()] = &PI;
|
2014-10-06 01:59:03 +02:00
|
|
|
|
2010-07-21 01:41:56 +02:00
|
|
|
// Notify any listeners.
|
2014-10-06 02:06:48 +02:00
|
|
|
for (auto *Listener : Listeners)
|
|
|
|
Listener->passRegistered(&PI);
|
2014-10-06 01:59:03 +02:00
|
|
|
|
|
|
|
if (ShouldFree)
|
|
|
|
ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::enumerateWith(PassRegistrationListener *L) {
|
2014-06-12 18:06:51 +02:00
|
|
|
sys::SmartScopedReader<true> Guard(Lock);
|
2014-10-06 02:06:48 +02:00
|
|
|
for (auto PassInfoPair : PassInfoMap)
|
|
|
|
L->passEnumerate(PassInfoPair.second);
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Analysis Group Mechanisms.
|
2014-10-06 01:59:03 +02:00
|
|
|
void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
|
2010-08-06 20:33:48 +02:00
|
|
|
const void *PassID,
|
2014-10-06 01:59:03 +02:00
|
|
|
PassInfo &Registeree, bool isDefault,
|
2010-10-21 00:22:30 +02:00
|
|
|
bool ShouldFree) {
|
2014-10-06 01:59:03 +02:00
|
|
|
PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
|
2014-04-09 08:08:46 +02:00
|
|
|
if (!InterfaceInfo) {
|
2010-07-21 19:52:45 +02:00
|
|
|
// First reference to Interface, register it now.
|
|
|
|
registerPass(Registeree);
|
|
|
|
InterfaceInfo = &Registeree;
|
|
|
|
}
|
2014-10-06 01:59:03 +02:00
|
|
|
assert(Registeree.isAnalysisGroup() &&
|
2010-07-21 19:52:45 +02:00
|
|
|
"Trying to join an analysis group that is a normal pass!");
|
|
|
|
|
|
|
|
if (PassID) {
|
2014-10-06 01:59:03 +02:00
|
|
|
PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
|
2010-07-21 19:52:45 +02:00
|
|
|
assert(ImplementationInfo &&
|
|
|
|
"Must register pass before adding to AnalysisGroup!");
|
|
|
|
|
2014-06-12 18:06:51 +02:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2014-10-06 01:59:03 +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);
|
|
|
|
|
|
|
|
if (isDefault) {
|
2014-04-15 08:32:26 +02:00
|
|
|
assert(InterfaceInfo->getNormalCtor() == nullptr &&
|
2010-07-21 19:52:45 +02:00
|
|
|
"Default implementation for analysis group already specified!");
|
2014-10-06 01:59:03 +02:00
|
|
|
assert(
|
|
|
|
ImplementationInfo->getNormalCtor() &&
|
|
|
|
"Cannot specify pass as default if it does not have a default ctor");
|
2010-07-21 19:52:45 +02:00
|
|
|
InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
|
|
|
|
}
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
2014-10-06 01:59:03 +02:00
|
|
|
|
2014-04-15 17:17:14 +02:00
|
|
|
if (ShouldFree)
|
2014-06-12 18:06:51 +02:00
|
|
|
ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
|
2010-07-20 21:23:55 +02:00
|
|
|
}
|
2010-07-21 01:41:56 +02:00
|
|
|
|
|
|
|
void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
|
2014-06-12 18:06:51 +02:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
|
|
|
Listeners.push_back(L);
|
2010-07-21 01:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
|
2014-06-12 18:06:51 +02:00
|
|
|
sys::SmartScopedWriter<true> Guard(Lock);
|
2014-10-06 01:59:03 +02:00
|
|
|
|
2017-09-07 01:05:38 +02:00
|
|
|
auto I = llvm::find(Listeners, L);
|
2014-06-12 18:06:51 +02:00
|
|
|
Listeners.erase(I);
|
2010-07-21 01:41:56 +02:00
|
|
|
}
|