Fix PR7972, in which the PassRegistry was being leaked. As part of this,
[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/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include <vector>
22
23 using namespace llvm;
24
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;
33 }
34
35 //===----------------------------------------------------------------------===//
36 // PassRegistryImpl
37 //
38
39 struct PassRegistryImpl {
40   /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
41   typedef DenseMap<const void*, const PassInfo*> MapType;
42   MapType PassInfoMap;
43   
44   typedef StringMap<const PassInfo*> StringMapType;
45   StringMapType PassInfoStringMap;
46   
47   /// AnalysisGroupInfo - Keep track of information for each analysis group.
48   struct AnalysisGroupInfo {
49     SmallPtrSet<const PassInfo *, 8> Implementations;
50   };
51   DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap;
52   
53   std::vector<PassRegistrationListener*> Listeners;
54 };
55
56 void *PassRegistry::getImpl() const {
57   if (!pImpl)
58     pImpl = new PassRegistryImpl();
59   return pImpl;
60 }
61
62 //===----------------------------------------------------------------------===//
63 // Accessors
64 //
65
66 PassRegistry::~PassRegistry() {
67   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
68   if (Impl) delete Impl;
69   pImpl = 0;
70 }
71
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;
76 }
77
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;
83 }
84
85 //===----------------------------------------------------------------------===//
86 // Pass Registration mechanism
87 //
88
89 void PassRegistry::registerPass(const PassInfo &PI) {
90   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
91   bool Inserted =
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;
95   
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);
100 }
101
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!");
107   
108   // Remove pass from the map.
109   Impl->PassInfoMap.erase(I);
110   Impl->PassInfoStringMap.erase(PI.getPassArgument());
111 }
112
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);
118 }
119
120
121 /// Analysis Group Mechanisms.
122 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 
123                                          const void *PassID,
124                                          PassInfo& Registeree,
125                                          bool isDefault) {
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;
131   }
132   assert(Registeree.isAnalysisGroup() && 
133          "Trying to join an analysis group that is a normal pass!");
134
135   if (PassID) {
136     PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
137     assert(ImplementationInfo &&
138            "Must register pass before adding to AnalysisGroup!");
139
140     // Make sure we keep track of the fact that the implementation implements
141     // the interface.
142     ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
143
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);
150     if (isDefault) {
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());
156     }
157   }
158 }
159
160 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
161   PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
162   Impl->Listeners.push_back(L);
163 }
164
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.
170   if (!pImpl) return;
171   
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);
178 }