1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PassRegistry, with which passes are registered on
11 // initialization, and supports the PassManager in dependency resolution.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/PassRegistry.h"
16 #include "llvm/PassSupport.h"
17 #include "llvm/Support/ManagedStatic.h"
21 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
22 // Unfortunately, passes are registered with static ctors, and having
23 // llvm_shutdown clear this map prevents successful resurrection after
24 // llvm_shutdown is run. Ideally we should find a solution so that we don't
25 // leak the map, AND can still resurrect after shutdown.
26 static ManagedStatic<PassRegistry> PassRegistryObj;
27 PassRegistry *PassRegistry::getPassRegistry() {
28 return &*PassRegistryObj;
31 //===----------------------------------------------------------------------===//
35 PassRegistry::~PassRegistry() {}
37 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
38 sys::SmartScopedReader<true> Guard(Lock);
39 MapType::const_iterator I = PassInfoMap.find(TI);
40 return I != PassInfoMap.end() ? I->second : nullptr;
43 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
44 sys::SmartScopedReader<true> Guard(Lock);
45 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
46 return I != PassInfoStringMap.end() ? I->second : nullptr;
49 //===----------------------------------------------------------------------===//
50 // Pass Registration mechanism
53 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
54 sys::SmartScopedWriter<true> Guard(Lock);
56 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
57 assert(Inserted && "Pass registered multiple times!");
59 PassInfoStringMap[PI.getPassArgument()] = &PI;
61 // Notify any listeners.
62 for (auto *Listener : Listeners)
63 Listener->passRegistered(&PI);
66 ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
69 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
70 sys::SmartScopedReader<true> Guard(Lock);
71 for (auto PassInfoPair : PassInfoMap)
72 L->passEnumerate(PassInfoPair.second);
75 /// Analysis Group Mechanisms.
76 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
78 PassInfo &Registeree, bool isDefault,
80 PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
82 // First reference to Interface, register it now.
83 registerPass(Registeree);
84 InterfaceInfo = &Registeree;
86 assert(Registeree.isAnalysisGroup() &&
87 "Trying to join an analysis group that is a normal pass!");
90 PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
91 assert(ImplementationInfo &&
92 "Must register pass before adding to AnalysisGroup!");
94 sys::SmartScopedWriter<true> Guard(Lock);
96 // Make sure we keep track of the fact that the implementation implements
98 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
101 assert(InterfaceInfo->getNormalCtor() == nullptr &&
102 "Default implementation for analysis group already specified!");
104 ImplementationInfo->getNormalCtor() &&
105 "Cannot specify pass as default if it does not have a default ctor");
106 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
107 InterfaceInfo->setTargetMachineCtor(
108 ImplementationInfo->getTargetMachineCtor());
113 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
116 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
117 sys::SmartScopedWriter<true> Guard(Lock);
118 Listeners.push_back(L);
121 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
122 sys::SmartScopedWriter<true> Guard(Lock);
124 auto I = std::find(Listeners.begin(), Listeners.end(), L);