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/IR/Function.h"
17 #include "llvm/PassSupport.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/RWMutex.h"
25 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
26 // Unfortunately, passes are registered with static ctors, and having
27 // llvm_shutdown clear this map prevents successful resurrection after
28 // llvm_shutdown is run. Ideally we should find a solution so that we don't
29 // leak the map, AND can still resurrect after shutdown.
30 static ManagedStatic<PassRegistry> PassRegistryObj;
31 PassRegistry *PassRegistry::getPassRegistry() {
32 return &*PassRegistryObj;
35 //===----------------------------------------------------------------------===//
39 PassRegistry::~PassRegistry() {}
41 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
42 sys::SmartScopedReader<true> Guard(Lock);
43 MapType::const_iterator I = PassInfoMap.find(TI);
44 return I != PassInfoMap.end() ? I->second : nullptr;
47 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
48 sys::SmartScopedReader<true> Guard(Lock);
49 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
50 return I != PassInfoStringMap.end() ? I->second : nullptr;
53 //===----------------------------------------------------------------------===//
54 // Pass Registration mechanism
57 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
58 sys::SmartScopedWriter<true> Guard(Lock);
60 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
61 assert(Inserted && "Pass registered multiple times!");
63 PassInfoStringMap[PI.getPassArgument()] = &PI;
65 // Notify any listeners.
66 for (std::vector<PassRegistrationListener *>::iterator I = Listeners.begin(),
69 (*I)->passRegistered(&PI);
72 ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
75 void PassRegistry::unregisterPass(const PassInfo &PI) {
76 sys::SmartScopedWriter<true> Guard(Lock);
77 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
78 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
80 // Remove pass from the map.
82 PassInfoStringMap.erase(PI.getPassArgument());
85 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
86 sys::SmartScopedReader<true> Guard(Lock);
87 for (auto I = PassInfoMap.begin(), E = PassInfoMap.end(); I != E; ++I)
88 L->passEnumerate(I->second);
91 /// Analysis Group Mechanisms.
92 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
94 PassInfo &Registeree, bool isDefault,
96 PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
98 // First reference to Interface, register it now.
99 registerPass(Registeree);
100 InterfaceInfo = &Registeree;
102 assert(Registeree.isAnalysisGroup() &&
103 "Trying to join an analysis group that is a normal pass!");
106 PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
107 assert(ImplementationInfo &&
108 "Must register pass before adding to AnalysisGroup!");
110 sys::SmartScopedWriter<true> Guard(Lock);
112 // Make sure we keep track of the fact that the implementation implements
114 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
116 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
117 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
118 "Cannot add a pass to the same analysis group more than once!");
119 AGI.Implementations.insert(ImplementationInfo);
121 assert(InterfaceInfo->getNormalCtor() == nullptr &&
122 "Default implementation for analysis group already specified!");
124 ImplementationInfo->getNormalCtor() &&
125 "Cannot specify pass as default if it does not have a default ctor");
126 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
127 InterfaceInfo->setTargetMachineCtor(
128 ImplementationInfo->getTargetMachineCtor());
133 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
136 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
137 sys::SmartScopedWriter<true> Guard(Lock);
138 Listeners.push_back(L);
141 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
142 sys::SmartScopedWriter<true> Guard(Lock);
144 auto I = std::find(Listeners.begin(), Listeners.end(), L);