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/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/StringMap.h"
26 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
27 // Unfortunately, passes are registered with static ctors, and having
28 // llvm_shutdown clear this map prevents successful ressurection after
29 // llvm_shutdown is run. Ideally we should find a solution so that we don't
30 // leak the map, AND can still resurrect after shutdown.
31 static ManagedStatic<PassRegistry> PassRegistryObj;
32 PassRegistry *PassRegistry::getPassRegistry() {
33 return &*PassRegistryObj;
36 //===----------------------------------------------------------------------===//
40 struct PassRegistryImpl {
41 /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
42 typedef DenseMap<const void*, const PassInfo*> MapType;
45 typedef StringMap<const PassInfo*> StringMapType;
46 StringMapType PassInfoStringMap;
48 /// AnalysisGroupInfo - Keep track of information for each analysis group.
49 struct AnalysisGroupInfo {
50 SmallPtrSet<const PassInfo *, 8> Implementations;
52 DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
54 std::vector<PassRegistrationListener*> Listeners;
57 void *PassRegistry::getImpl() const {
59 pImpl = new PassRegistryImpl();
63 //===----------------------------------------------------------------------===//
67 PassRegistry::~PassRegistry() {
68 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
69 if (Impl) delete Impl;
73 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
74 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
75 PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
76 return I != Impl->PassInfoMap.end() ? I->second : 0;
79 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
80 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
81 PassRegistryImpl::StringMapType::const_iterator
82 I = Impl->PassInfoStringMap.find(Arg);
83 return I != Impl->PassInfoStringMap.end() ? I->second : 0;
86 //===----------------------------------------------------------------------===//
87 // Pass Registration mechanism
90 void PassRegistry::registerPass(const PassInfo &PI) {
91 sys::SmartScopedLock<true> Guard(Lock);
92 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
94 Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
95 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
96 Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
98 // Notify any listeners.
99 for (std::vector<PassRegistrationListener*>::iterator
100 I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
101 (*I)->passRegistered(&PI);
104 void PassRegistry::unregisterPass(const PassInfo &PI) {
105 sys::SmartScopedLock<true> Guard(Lock);
106 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
107 PassRegistryImpl::MapType::iterator I =
108 Impl->PassInfoMap.find(PI.getTypeInfo());
109 assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
111 // Remove pass from the map.
112 Impl->PassInfoMap.erase(I);
113 Impl->PassInfoStringMap.erase(PI.getPassArgument());
116 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
117 sys::SmartScopedLock<true> Guard(Lock);
118 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
119 for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
120 E = Impl->PassInfoMap.end(); I != E; ++I)
121 L->passEnumerate(I->second);
125 /// Analysis Group Mechanisms.
126 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
128 PassInfo& Registeree,
130 sys::SmartScopedLock<true> Guard(Lock);
131 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
132 if (InterfaceInfo == 0) {
133 // First reference to Interface, register it now.
134 registerPass(Registeree);
135 InterfaceInfo = &Registeree;
137 assert(Registeree.isAnalysisGroup() &&
138 "Trying to join an analysis group that is a normal pass!");
141 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
142 assert(ImplementationInfo &&
143 "Must register pass before adding to AnalysisGroup!");
145 // Make sure we keep track of the fact that the implementation implements
147 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
149 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
150 PassRegistryImpl::AnalysisGroupInfo &AGI =
151 Impl->AnalysisGroupInfoMap[InterfaceInfo];
152 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
153 "Cannot add a pass to the same analysis group more than once!");
154 AGI.Implementations.insert(ImplementationInfo);
156 assert(InterfaceInfo->getNormalCtor() == 0 &&
157 "Default implementation for analysis group already specified!");
158 assert(ImplementationInfo->getNormalCtor() &&
159 "Cannot specify pass as default if it does not have a default ctor");
160 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
165 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
166 sys::SmartScopedLock<true> Guard(Lock);
167 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
168 Impl->Listeners.push_back(L);
171 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
172 sys::SmartScopedLock<true> Guard(Lock);
174 // NOTE: This is necessary, because removeRegistrationListener() can be called
175 // as part of the llvm_shutdown sequence. Since we have no control over the
176 // order of that sequence, we need to gracefully handle the case where the
177 // PassRegistry is destructed before the object that triggers this call.
180 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
181 std::vector<PassRegistrationListener*>::iterator I =
182 std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
183 assert(I != Impl->Listeners.end() &&
184 "PassRegistrationListener not registered!");
185 Impl->Listeners.erase(I);