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/System/Mutex.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/StringMap.h"
27 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
28 // Unfortunately, passes are registered with static ctors, and having
29 // llvm_shutdown clear this map prevents successful ressurection after
30 // llvm_shutdown is run. Ideally we should find a solution so that we don't
31 // leak the map, AND can still resurrect after shutdown.
32 static ManagedStatic<PassRegistry> PassRegistryObj;
33 PassRegistry *PassRegistry::getPassRegistry() {
34 return &*PassRegistryObj;
37 static ManagedStatic<sys::SmartMutex<true> > Lock;
39 //===----------------------------------------------------------------------===//
43 struct PassRegistryImpl {
44 /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
45 typedef DenseMap<const void*, const PassInfo*> MapType;
48 typedef StringMap<const PassInfo*> StringMapType;
49 StringMapType PassInfoStringMap;
51 /// AnalysisGroupInfo - Keep track of information for each analysis group.
52 struct AnalysisGroupInfo {
53 SmallPtrSet<const PassInfo *, 8> Implementations;
55 DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
57 std::vector<PassRegistrationListener*> Listeners;
60 void *PassRegistry::getImpl() const {
62 pImpl = new PassRegistryImpl();
66 //===----------------------------------------------------------------------===//
70 PassRegistry::~PassRegistry() {
71 sys::SmartScopedLock<true> Guard(*Lock);
72 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
77 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
78 sys::SmartScopedLock<true> Guard(*Lock);
79 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
80 PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
81 return I != Impl->PassInfoMap.end() ? I->second : 0;
84 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
85 sys::SmartScopedLock<true> Guard(*Lock);
86 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
87 PassRegistryImpl::StringMapType::const_iterator
88 I = Impl->PassInfoStringMap.find(Arg);
89 return I != Impl->PassInfoStringMap.end() ? I->second : 0;
92 //===----------------------------------------------------------------------===//
93 // Pass Registration mechanism
96 void PassRegistry::registerPass(const PassInfo &PI) {
97 sys::SmartScopedLock<true> Guard(*Lock);
98 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
100 Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
101 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
102 Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
104 // Notify any listeners.
105 for (std::vector<PassRegistrationListener*>::iterator
106 I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
107 (*I)->passRegistered(&PI);
110 void PassRegistry::unregisterPass(const PassInfo &PI) {
111 sys::SmartScopedLock<true> Guard(*Lock);
112 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
113 PassRegistryImpl::MapType::iterator I =
114 Impl->PassInfoMap.find(PI.getTypeInfo());
115 assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
117 // Remove pass from the map.
118 Impl->PassInfoMap.erase(I);
119 Impl->PassInfoStringMap.erase(PI.getPassArgument());
122 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
123 sys::SmartScopedLock<true> Guard(*Lock);
124 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
125 for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
126 E = Impl->PassInfoMap.end(); I != E; ++I)
127 L->passEnumerate(I->second);
131 /// Analysis Group Mechanisms.
132 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
134 PassInfo& Registeree,
136 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
137 if (InterfaceInfo == 0) {
138 // First reference to Interface, register it now.
139 registerPass(Registeree);
140 InterfaceInfo = &Registeree;
142 assert(Registeree.isAnalysisGroup() &&
143 "Trying to join an analysis group that is a normal pass!");
146 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
147 assert(ImplementationInfo &&
148 "Must register pass before adding to AnalysisGroup!");
150 sys::SmartScopedLock<true> Guard(*Lock);
152 // Make sure we keep track of the fact that the implementation implements
154 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
156 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
157 PassRegistryImpl::AnalysisGroupInfo &AGI =
158 Impl->AnalysisGroupInfoMap[InterfaceInfo];
159 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
160 "Cannot add a pass to the same analysis group more than once!");
161 AGI.Implementations.insert(ImplementationInfo);
163 assert(InterfaceInfo->getNormalCtor() == 0 &&
164 "Default implementation for analysis group already specified!");
165 assert(ImplementationInfo->getNormalCtor() &&
166 "Cannot specify pass as default if it does not have a default ctor");
167 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
172 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
173 sys::SmartScopedLock<true> Guard(*Lock);
174 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
175 Impl->Listeners.push_back(L);
178 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
179 sys::SmartScopedLock<true> Guard(*Lock);
181 // NOTE: This is necessary, because removeRegistrationListener() can be called
182 // as part of the llvm_shutdown sequence. Since we have no control over the
183 // order of that sequence, we need to gracefully handle the case where the
184 // PassRegistry is destructed before the object that triggers this call.
187 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
188 std::vector<PassRegistrationListener*>::iterator I =
189 std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
190 assert(I != Impl->Listeners.end() &&
191 "PassRegistrationListener not registered!");
192 Impl->Listeners.erase(I);