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/Compiler.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/Mutex.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/Function.h"
28 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
29 // Unfortunately, passes are registered with static ctors, and having
30 // llvm_shutdown clear this map prevents successful resurrection after
31 // llvm_shutdown is run. Ideally we should find a solution so that we don't
32 // leak the map, AND can still resurrect after shutdown.
33 static ManagedStatic<PassRegistry> PassRegistryObj;
34 PassRegistry *PassRegistry::getPassRegistry() {
35 return &*PassRegistryObj;
38 static ManagedStatic<sys::SmartMutex<true> > Lock;
40 //===----------------------------------------------------------------------===//
45 struct PassRegistryImpl {
46 /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
47 typedef DenseMap<const void*, const PassInfo*> MapType;
50 typedef StringMap<const PassInfo*> StringMapType;
51 StringMapType PassInfoStringMap;
53 /// AnalysisGroupInfo - Keep track of information for each analysis group.
54 struct AnalysisGroupInfo {
55 SmallPtrSet<const PassInfo *, 8> Implementations;
57 DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
59 std::vector<const PassInfo*> ToFree;
60 std::vector<PassRegistrationListener*> Listeners;
62 } // end anonymous namespace
64 void *PassRegistry::getImpl() const {
66 pImpl = new PassRegistryImpl();
70 //===----------------------------------------------------------------------===//
74 PassRegistry::~PassRegistry() {
75 sys::SmartScopedLock<true> Guard(*Lock);
76 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
78 for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(),
79 E = Impl->ToFree.end(); I != E; ++I)
86 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
87 sys::SmartScopedLock<true> Guard(*Lock);
88 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
89 PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
90 return I != Impl->PassInfoMap.end() ? I->second : 0;
93 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
94 sys::SmartScopedLock<true> Guard(*Lock);
95 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
96 PassRegistryImpl::StringMapType::const_iterator
97 I = Impl->PassInfoStringMap.find(Arg);
98 return I != Impl->PassInfoStringMap.end() ? I->second : 0;
101 //===----------------------------------------------------------------------===//
102 // Pass Registration mechanism
105 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
106 sys::SmartScopedLock<true> Guard(*Lock);
107 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
109 Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
110 assert(Inserted && "Pass registered multiple times!");
112 Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
114 // Notify any listeners.
115 for (std::vector<PassRegistrationListener*>::iterator
116 I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
117 (*I)->passRegistered(&PI);
119 if (ShouldFree) Impl->ToFree.push_back(&PI);
122 void PassRegistry::unregisterPass(const PassInfo &PI) {
123 sys::SmartScopedLock<true> Guard(*Lock);
124 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
125 PassRegistryImpl::MapType::iterator I =
126 Impl->PassInfoMap.find(PI.getTypeInfo());
127 assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
129 // Remove pass from the map.
130 Impl->PassInfoMap.erase(I);
131 Impl->PassInfoStringMap.erase(PI.getPassArgument());
134 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
135 sys::SmartScopedLock<true> Guard(*Lock);
136 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
137 for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
138 E = Impl->PassInfoMap.end(); I != E; ++I)
139 L->passEnumerate(I->second);
143 /// Analysis Group Mechanisms.
144 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
146 PassInfo& Registeree,
149 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
150 if (InterfaceInfo == 0) {
151 // First reference to Interface, register it now.
152 registerPass(Registeree);
153 InterfaceInfo = &Registeree;
155 assert(Registeree.isAnalysisGroup() &&
156 "Trying to join an analysis group that is a normal pass!");
159 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
160 assert(ImplementationInfo &&
161 "Must register pass before adding to AnalysisGroup!");
163 sys::SmartScopedLock<true> Guard(*Lock);
165 // Make sure we keep track of the fact that the implementation implements
167 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
169 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
170 PassRegistryImpl::AnalysisGroupInfo &AGI =
171 Impl->AnalysisGroupInfoMap[InterfaceInfo];
172 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
173 "Cannot add a pass to the same analysis group more than once!");
174 AGI.Implementations.insert(ImplementationInfo);
176 assert(InterfaceInfo->getNormalCtor() == 0 &&
177 "Default implementation for analysis group already specified!");
178 assert(ImplementationInfo->getNormalCtor() &&
179 "Cannot specify pass as default if it does not have a default ctor");
180 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
184 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
185 if (ShouldFree) Impl->ToFree.push_back(&Registeree);
188 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
189 sys::SmartScopedLock<true> Guard(*Lock);
190 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
191 Impl->Listeners.push_back(L);
194 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
195 sys::SmartScopedLock<true> Guard(*Lock);
197 // NOTE: This is necessary, because removeRegistrationListener() can be called
198 // as part of the llvm_shutdown sequence. Since we have no control over the
199 // order of that sequence, we need to gracefully handle the case where the
200 // PassRegistry is destructed before the object that triggers this call.
203 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
204 std::vector<PassRegistrationListener*>::iterator I =
205 std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
206 assert(I != Impl->Listeners.end() &&
207 "PassRegistrationListener not registered!");
208 Impl->Listeners.erase(I);