Make some symbols static, move classes into anonymous namespaces.
[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 namespace {
44 struct PassRegistryImpl {
45   /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
46   typedef DenseMap<const void*, const PassInfo*> MapType;
47   MapType PassInfoMap;
48   
49   typedef StringMap<const PassInfo*> StringMapType;
50   StringMapType PassInfoStringMap;
51   
52   /// AnalysisGroupInfo - Keep track of information for each analysis group.
53   struct AnalysisGroupInfo {
54     SmallPtrSet<const PassInfo *, 8> Implementations;
55   };
56   DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
57   
58   std::vector<const PassInfo*> ToFree;
59   std::vector<PassRegistrationListener*> Listeners;
60 };
61 } // end anonymous namespace
62
63 void *PassRegistry::getImpl() const {
64   if (!pImpl)
65     pImpl = new PassRegistryImpl();
66   return pImpl;
67 }
68
69 //===----------------------------------------------------------------------===//
70 // Accessors
71 //
72
73 PassRegistry::~PassRegistry() {
74   sys::SmartScopedLock<true> Guard(*Lock);
75   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
76   
77   for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(),
78        E = Impl->ToFree.end(); I != E; ++I)
79     delete *I;
80   
81   delete Impl;
82   pImpl = 0;
83 }
84
85 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
86   sys::SmartScopedLock<true> Guard(*Lock);
87   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
88   PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
89   return I != Impl->PassInfoMap.end() ? I->second : 0;
90 }
91
92 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
93   sys::SmartScopedLock<true> Guard(*Lock);
94   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
95   PassRegistryImpl::StringMapType::const_iterator
96     I = Impl->PassInfoStringMap.find(Arg);
97   return I != Impl->PassInfoStringMap.end() ? I->second : 0;
98 }
99
100 //===----------------------------------------------------------------------===//
101 // Pass Registration mechanism
102 //
103
104 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
105   sys::SmartScopedLock<true> Guard(*Lock);
106   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
107   bool Inserted =
108     Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
109   assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
110   Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
111   
112   // Notify any listeners.
113   for (std::vector<PassRegistrationListener*>::iterator
114        I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
115     (*I)->passRegistered(&PI);
116   
117   if (ShouldFree) Impl->ToFree.push_back(&PI);
118 }
119
120 void PassRegistry::unregisterPass(const PassInfo &PI) {
121   sys::SmartScopedLock<true> Guard(*Lock);
122   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
123   PassRegistryImpl::MapType::iterator I = 
124     Impl->PassInfoMap.find(PI.getTypeInfo());
125   assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
126   
127   // Remove pass from the map.
128   Impl->PassInfoMap.erase(I);
129   Impl->PassInfoStringMap.erase(PI.getPassArgument());
130 }
131
132 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
133   sys::SmartScopedLock<true> Guard(*Lock);
134   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
135   for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
136        E = Impl->PassInfoMap.end(); I != E; ++I)
137     L->passEnumerate(I->second);
138 }
139
140
141 /// Analysis Group Mechanisms.
142 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 
143                                          const void *PassID,
144                                          PassInfo& Registeree,
145                                          bool isDefault,
146                                          bool ShouldFree) {
147   PassInfo *InterfaceInfo =  const_cast<PassInfo*>(getPassInfo(InterfaceID));
148   if (InterfaceInfo == 0) {
149     // First reference to Interface, register it now.
150     registerPass(Registeree);
151     InterfaceInfo = &Registeree;
152   }
153   assert(Registeree.isAnalysisGroup() && 
154          "Trying to join an analysis group that is a normal pass!");
155
156   if (PassID) {
157     PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
158     assert(ImplementationInfo &&
159            "Must register pass before adding to AnalysisGroup!");
160
161     sys::SmartScopedLock<true> Guard(*Lock);
162     
163     // Make sure we keep track of the fact that the implementation implements
164     // the interface.
165     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
166
167     PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
168     PassRegistryImpl::AnalysisGroupInfo &AGI =
169       Impl->AnalysisGroupInfoMap[InterfaceInfo];
170     assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
171            "Cannot add a pass to the same analysis group more than once!");
172     AGI.Implementations.insert(ImplementationInfo);
173     if (isDefault) {
174       assert(InterfaceInfo->getNormalCtor() == 0 &&
175              "Default implementation for analysis group already specified!");
176       assert(ImplementationInfo->getNormalCtor() &&
177            "Cannot specify pass as default if it does not have a default ctor");
178       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
179     }
180   }
181   
182   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
183   if (ShouldFree) Impl->ToFree.push_back(&Registeree);
184 }
185
186 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
187   sys::SmartScopedLock<true> Guard(*Lock);
188   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
189   Impl->Listeners.push_back(L);
190 }
191
192 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
193   sys::SmartScopedLock<true> Guard(*Lock);
194   
195   // NOTE: This is necessary, because removeRegistrationListener() can be called
196   // as part of the llvm_shutdown sequence.  Since we have no control over the
197   // order of that sequence, we need to gracefully handle the case where the
198   // PassRegistry is destructed before the object that triggers this call.
199   if (!pImpl) return;
200   
201   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
202   std::vector<PassRegistrationListener*>::iterator I =
203     std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
204   assert(I != Impl->Listeners.end() &&
205          "PassRegistrationListener not registered!");
206   Impl->Listeners.erase(I);
207 }