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"
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 ressurection 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 struct PassRegistryImpl {
40 /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
41 typedef DenseMap<const void*, const PassInfo*> MapType;
44 typedef StringMap<const PassInfo*> StringMapType;
45 StringMapType PassInfoStringMap;
47 /// AnalysisGroupInfo - Keep track of information for each analysis group.
48 struct AnalysisGroupInfo {
49 SmallPtrSet<const PassInfo *, 8> Implementations;
51 DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
53 std::vector<PassRegistrationListener*> Listeners;
56 void *PassRegistry::getImpl() const {
58 pImpl = new PassRegistryImpl();
62 //===----------------------------------------------------------------------===//
66 PassRegistry::~PassRegistry() {
67 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
68 if (Impl) delete Impl;
72 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
73 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
74 PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
75 return I != Impl->PassInfoMap.end() ? I->second : 0;
78 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
79 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
80 PassRegistryImpl::StringMapType::const_iterator
81 I = Impl->PassInfoStringMap.find(Arg);
82 return I != Impl->PassInfoStringMap.end() ? I->second : 0;
85 //===----------------------------------------------------------------------===//
86 // Pass Registration mechanism
89 void PassRegistry::registerPass(const PassInfo &PI) {
90 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
92 Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
93 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
94 Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
96 // Notify any listeners.
97 for (std::vector<PassRegistrationListener*>::iterator
98 I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
99 (*I)->passRegistered(&PI);
102 void PassRegistry::unregisterPass(const PassInfo &PI) {
103 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
104 PassRegistryImpl::MapType::iterator I =
105 Impl->PassInfoMap.find(PI.getTypeInfo());
106 assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
108 // Remove pass from the map.
109 Impl->PassInfoMap.erase(I);
110 Impl->PassInfoStringMap.erase(PI.getPassArgument());
113 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
114 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
115 for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
116 E = Impl->PassInfoMap.end(); I != E; ++I)
117 L->passEnumerate(I->second);
121 /// Analysis Group Mechanisms.
122 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
124 PassInfo& Registeree,
126 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
127 if (InterfaceInfo == 0) {
128 // First reference to Interface, register it now.
129 registerPass(Registeree);
130 InterfaceInfo = &Registeree;
132 assert(Registeree.isAnalysisGroup() &&
133 "Trying to join an analysis group that is a normal pass!");
136 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
137 assert(ImplementationInfo &&
138 "Must register pass before adding to AnalysisGroup!");
140 // Make sure we keep track of the fact that the implementation implements
142 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
144 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
145 PassRegistryImpl::AnalysisGroupInfo &AGI =
146 Impl->AnalysisGroupInfoMap[InterfaceInfo];
147 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
148 "Cannot add a pass to the same analysis group more than once!");
149 AGI.Implementations.insert(ImplementationInfo);
151 assert(InterfaceInfo->getNormalCtor() == 0 &&
152 "Default implementation for analysis group already specified!");
153 assert(ImplementationInfo->getNormalCtor() &&
154 "Cannot specify pass as default if it does not have a default ctor");
155 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
160 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
161 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
162 Impl->Listeners.push_back(L);
165 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
166 // NOTE: This is necessary, because removeRegistrationListener() can be called
167 // as part of the llvm_shutdown sequence. Since we have no control over the
168 // order of that sequence, we need to gracefully handle the case where the
169 // PassRegistry is destructed before the object that triggers this call.
172 PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
173 std::vector<PassRegistrationListener*>::iterator I =
174 std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
175 assert(I != Impl->Listeners.end() &&
176 "PassRegistrationListener not registered!");
177 Impl->Listeners.erase(I);