Don't acquire the mutex during the destructor of PassRegistry.
[oota-llvm.git] / lib / IR / 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/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/PassSupport.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/Mutex.h"
24 #include "llvm/Support/RWMutex.h"
25 #include <vector>
26
27 using namespace llvm;
28
29 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
30 // Unfortunately, passes are registered with static ctors, and having
31 // llvm_shutdown clear this map prevents successful resurrection after
32 // llvm_shutdown is run.  Ideally we should find a solution so that we don't
33 // leak the map, AND can still resurrect after shutdown.
34 static ManagedStatic<PassRegistry> PassRegistryObj;
35 PassRegistry *PassRegistry::getPassRegistry() {
36   return &*PassRegistryObj;
37 }
38
39 static ManagedStatic<sys::SmartRWMutex<true> > Lock;
40
41 //===----------------------------------------------------------------------===//
42 // PassRegistryImpl
43 //
44
45 namespace {
46 struct PassRegistryImpl {
47   /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
48   typedef DenseMap<const void*, const PassInfo*> MapType;
49   MapType PassInfoMap;
50   
51   typedef StringMap<const PassInfo*> StringMapType;
52   StringMapType PassInfoStringMap;
53   
54   /// AnalysisGroupInfo - Keep track of information for each analysis group.
55   struct AnalysisGroupInfo {
56     SmallPtrSet<const PassInfo *, 8> Implementations;
57   };
58   DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
59   
60   std::vector<std::unique_ptr<const PassInfo>> ToFree;
61   std::vector<PassRegistrationListener*> Listeners;
62 };
63 } // end anonymous namespace
64
65 void *PassRegistry::getImpl() const {
66   if (!pImpl)
67     pImpl = new PassRegistryImpl();
68   return pImpl;
69 }
70
71 //===----------------------------------------------------------------------===//
72 // Accessors
73 //
74
75 PassRegistry::~PassRegistry() {
76   // Don't acquire the mutex here.  This is destroyed during static execution of
77   // static destructors, after llvm_shutdown() has been called, so all instances
78   // of all ManagedStatics (including the Mutex), will have been destroyed as
79   // well.
80   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
81   delete Impl;
82   pImpl = nullptr;
83 }
84
85 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
86   sys::SmartScopedReader<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 : nullptr;
90 }
91
92 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
93   sys::SmartScopedReader<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 : nullptr;
98 }
99
100 //===----------------------------------------------------------------------===//
101 // Pass Registration mechanism
102 //
103
104 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
105   sys::SmartScopedWriter<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!");
110   (void)Inserted;
111   Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
112   
113   // Notify any listeners.
114   for (std::vector<PassRegistrationListener*>::iterator
115        I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
116     (*I)->passRegistered(&PI);
117   
118   if (ShouldFree) Impl->ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
119 }
120
121 void PassRegistry::unregisterPass(const PassInfo &PI) {
122   sys::SmartScopedWriter<true> Guard(*Lock);
123   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
124   PassRegistryImpl::MapType::iterator I = 
125     Impl->PassInfoMap.find(PI.getTypeInfo());
126   assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
127   
128   // Remove pass from the map.
129   Impl->PassInfoMap.erase(I);
130   Impl->PassInfoStringMap.erase(PI.getPassArgument());
131 }
132
133 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
134   sys::SmartScopedReader<true> Guard(*Lock);
135   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
136   for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
137        E = Impl->PassInfoMap.end(); I != E; ++I)
138     L->passEnumerate(I->second);
139 }
140
141
142 /// Analysis Group Mechanisms.
143 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 
144                                          const void *PassID,
145                                          PassInfo& Registeree,
146                                          bool isDefault,
147                                          bool ShouldFree) {
148   PassInfo *InterfaceInfo =  const_cast<PassInfo*>(getPassInfo(InterfaceID));
149   if (!InterfaceInfo) {
150     // First reference to Interface, register it now.
151     registerPass(Registeree);
152     InterfaceInfo = &Registeree;
153   }
154   assert(Registeree.isAnalysisGroup() && 
155          "Trying to join an analysis group that is a normal pass!");
156
157   if (PassID) {
158     PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
159     assert(ImplementationInfo &&
160            "Must register pass before adding to AnalysisGroup!");
161
162     sys::SmartScopedWriter<true> Guard(*Lock);
163     
164     // Make sure we keep track of the fact that the implementation implements
165     // the interface.
166     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
167
168     PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
169     PassRegistryImpl::AnalysisGroupInfo &AGI =
170       Impl->AnalysisGroupInfoMap[InterfaceInfo];
171     assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
172            "Cannot add a pass to the same analysis group more than once!");
173     AGI.Implementations.insert(ImplementationInfo);
174     if (isDefault) {
175       assert(InterfaceInfo->getNormalCtor() == nullptr &&
176              "Default implementation for analysis group already specified!");
177       assert(ImplementationInfo->getNormalCtor() &&
178            "Cannot specify pass as default if it does not have a default ctor");
179       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
180       InterfaceInfo->setTargetMachineCtor(
181           ImplementationInfo->getTargetMachineCtor());
182     }
183   }
184   
185   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
186   if (ShouldFree)
187     Impl->ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
188 }
189
190 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
191   sys::SmartScopedWriter<true> Guard(*Lock);
192   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
193   Impl->Listeners.push_back(L);
194 }
195
196 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
197   sys::SmartScopedWriter<true> Guard(*Lock);
198   
199   // NOTE: This is necessary, because removeRegistrationListener() can be called
200   // as part of the llvm_shutdown sequence.  Since we have no control over the
201   // order of that sequence, we need to gracefully handle the case where the
202   // PassRegistry is destructed before the object that triggers this call.
203   if (!pImpl) return;
204   
205   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
206   std::vector<PassRegistrationListener*>::iterator I =
207     std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
208   assert(I != Impl->Listeners.end() &&
209          "PassRegistrationListener not registered!");
210   Impl->Listeners.erase(I);
211 }