Allow the PassRegistry mutex to be lazily initialized, and clean up the global namesp...
[oota-llvm.git] / lib / VMCore / PassRegistry.cpp
1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the PassRegistry, with which passes are registered on
11 // initialization, and supports the PassManager in dependency resolution.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
23 #include <vector>
24
25 using namespace llvm;
26
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;
35 }
36
37 static ManagedStatic<sys::SmartMutex<true> > Lock;
38
39 //===----------------------------------------------------------------------===//
40 // PassRegistryImpl
41 //
42
43 struct PassRegistryImpl {
44   /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
45   typedef DenseMap<const void*, const PassInfo*> MapType;
46   MapType PassInfoMap;
47   
48   typedef StringMap<const PassInfo*> StringMapType;
49   StringMapType PassInfoStringMap;
50   
51   /// AnalysisGroupInfo - Keep track of information for each analysis group.
52   struct AnalysisGroupInfo {
53     SmallPtrSet<const PassInfo *, 8> Implementations;
54   };
55   DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
56   
57   std::vector<PassRegistrationListener*> Listeners;
58 };
59
60 void *PassRegistry::getImpl() const {
61   if (!pImpl)
62     pImpl = new PassRegistryImpl();
63   return pImpl;
64 }
65
66 //===----------------------------------------------------------------------===//
67 // Accessors
68 //
69
70 PassRegistry::~PassRegistry() {
71   sys::SmartScopedLock<true> Guard(*Lock);
72   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
73   if (Impl) delete Impl;
74   pImpl = 0;
75 }
76
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;
82 }
83
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;
90 }
91
92 //===----------------------------------------------------------------------===//
93 // Pass Registration mechanism
94 //
95
96 void PassRegistry::registerPass(const PassInfo &PI) {
97   sys::SmartScopedLock<true> Guard(*Lock);
98   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
99   bool Inserted =
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;
103   
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);
108 }
109
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!");
116   
117   // Remove pass from the map.
118   Impl->PassInfoMap.erase(I);
119   Impl->PassInfoStringMap.erase(PI.getPassArgument());
120 }
121
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);
128 }
129
130
131 /// Analysis Group Mechanisms.
132 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 
133                                          const void *PassID,
134                                          PassInfo& Registeree,
135                                          bool isDefault) {
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;
141   }
142   assert(Registeree.isAnalysisGroup() && 
143          "Trying to join an analysis group that is a normal pass!");
144
145   if (PassID) {
146     PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
147     assert(ImplementationInfo &&
148            "Must register pass before adding to AnalysisGroup!");
149
150     sys::SmartScopedLock<true> Guard(*Lock);
151     
152     // Make sure we keep track of the fact that the implementation implements
153     // the interface.
154     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
155
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);
162     if (isDefault) {
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());
168     }
169   }
170 }
171
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);
176 }
177
178 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
179   sys::SmartScopedLock<true> Guard(*Lock);
180   
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.
185   if (!pImpl) return;
186   
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);
193 }