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"
22 static PassRegistry *PassRegistryObj = 0;
23 PassRegistry *PassRegistry::getPassRegistry() {
24 // Use double-checked locking to safely initialize the registrar when
25 // we're running in multithreaded mode.
26 PassRegistry* tmp = PassRegistryObj;
27 if (llvm_is_multithreaded()) {
30 llvm_acquire_global_lock();
31 tmp = PassRegistryObj;
33 tmp = new PassRegistry();
35 PassRegistryObj = tmp;
37 llvm_release_global_lock();
40 PassRegistryObj = new PassRegistry();
43 return PassRegistryObj;
48 // FIXME: We use ManagedCleanup to erase the pass registrar on shutdown.
49 // Unfortunately, passes are registered with static ctors, and having
50 // llvm_shutdown clear this map prevents successful ressurection after
51 // llvm_shutdown is run. Ideally we should find a solution so that we don't
52 // leak the map, AND can still resurrect after shutdown.
53 void cleanupPassRegistry(void*) {
54 if (PassRegistryObj) {
55 delete PassRegistryObj;
59 ManagedCleanup<&cleanupPassRegistry> registryCleanup ATTRIBUTE_USED;
63 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
64 sys::SmartScopedLock<true> Guard(Lock);
65 MapType::const_iterator I = PassInfoMap.find(TI);
66 return I != PassInfoMap.end() ? I->second : 0;
69 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
70 sys::SmartScopedLock<true> Guard(Lock);
71 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
72 return I != PassInfoStringMap.end() ? I->second : 0;
75 //===----------------------------------------------------------------------===//
76 // Pass Registration mechanism
79 void PassRegistry::registerPass(const PassInfo &PI) {
80 sys::SmartScopedLock<true> Guard(Lock);
82 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
83 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
84 PassInfoStringMap[PI.getPassArgument()] = &PI;
86 // Notify any listeners.
87 for (std::vector<PassRegistrationListener*>::iterator
88 I = Listeners.begin(), E = Listeners.end(); I != E; ++I)
89 (*I)->passRegistered(&PI);
92 void PassRegistry::unregisterPass(const PassInfo &PI) {
93 sys::SmartScopedLock<true> Guard(Lock);
94 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
95 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
97 // Remove pass from the map.
99 PassInfoStringMap.erase(PI.getPassArgument());
102 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
103 sys::SmartScopedLock<true> Guard(Lock);
104 for (MapType::const_iterator I = PassInfoMap.begin(),
105 E = PassInfoMap.end(); I != E; ++I)
106 L->passEnumerate(I->second);
110 /// Analysis Group Mechanisms.
111 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
113 PassInfo& Registeree,
115 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
116 if (InterfaceInfo == 0) {
117 // First reference to Interface, register it now.
118 registerPass(Registeree);
119 InterfaceInfo = &Registeree;
121 assert(Registeree.isAnalysisGroup() &&
122 "Trying to join an analysis group that is a normal pass!");
125 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
126 assert(ImplementationInfo &&
127 "Must register pass before adding to AnalysisGroup!");
129 // Make sure we keep track of the fact that the implementation implements
131 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
133 sys::SmartScopedLock<true> Guard(Lock);
134 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
135 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
136 "Cannot add a pass to the same analysis group more than once!");
137 AGI.Implementations.insert(ImplementationInfo);
139 assert(InterfaceInfo->getNormalCtor() == 0 &&
140 "Default implementation for analysis group already specified!");
141 assert(ImplementationInfo->getNormalCtor() &&
142 "Cannot specify pass as default if it does not have a default ctor");
143 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
148 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
149 sys::SmartScopedLock<true> Guard(Lock);
150 Listeners.push_back(L);
153 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
154 sys::SmartScopedLock<true> Guard(Lock);
155 std::vector<PassRegistrationListener*>::iterator I =
156 std::find(Listeners.begin(), Listeners.end(), L);
157 assert(I != Listeners.end() && "PassRegistrationListener not registered!");