LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PassRegistry.cpp
Go to the documentation of this file.
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"
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.
36  return &*PassRegistryObj;
37 }
38 
40 
41 //===----------------------------------------------------------------------===//
42 // PassRegistryImpl
43 //
44 
45 namespace {
46 struct PassRegistryImpl {
47  /// PassInfoMap - Keep track of the PassInfo object for each registered pass.
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  };
59 
60  std::vector<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 
77  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl);
78 
79  for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(),
80  E = Impl->ToFree.end(); I != E; ++I)
81  delete *I;
82 
83  delete Impl;
84  pImpl = 0;
85 }
86 
87 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
89  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
90  PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI);
91  return I != Impl->PassInfoMap.end() ? I->second : 0;
92 }
93 
96  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
98  I = Impl->PassInfoStringMap.find(Arg);
99  return I != Impl->PassInfoStringMap.end() ? I->second : 0;
100 }
101 
102 //===----------------------------------------------------------------------===//
103 // Pass Registration mechanism
104 //
105 
106 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
108  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
109  bool Inserted =
110  Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
111  assert(Inserted && "Pass registered multiple times!");
112  (void)Inserted;
113  Impl->PassInfoStringMap[PI.getPassArgument()] = &PI;
114 
115  // Notify any listeners.
116  for (std::vector<PassRegistrationListener*>::iterator
117  I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I)
118  (*I)->passRegistered(&PI);
119 
120  if (ShouldFree) Impl->ToFree.push_back(&PI);
121 }
122 
125  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
127  Impl->PassInfoMap.find(PI.getTypeInfo());
128  assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!");
129 
130  // Remove pass from the map.
131  Impl->PassInfoMap.erase(I);
132  Impl->PassInfoStringMap.erase(PI.getPassArgument());
133 }
134 
137  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
138  for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(),
139  E = Impl->PassInfoMap.end(); I != E; ++I)
140  L->passEnumerate(I->second);
141 }
142 
143 
144 /// Analysis Group Mechanisms.
145 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
146  const void *PassID,
147  PassInfo& Registeree,
148  bool isDefault,
149  bool ShouldFree) {
150  PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
151  if (InterfaceInfo == 0) {
152  // First reference to Interface, register it now.
153  registerPass(Registeree);
154  InterfaceInfo = &Registeree;
155  }
156  assert(Registeree.isAnalysisGroup() &&
157  "Trying to join an analysis group that is a normal pass!");
158 
159  if (PassID) {
160  PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
161  assert(ImplementationInfo &&
162  "Must register pass before adding to AnalysisGroup!");
163 
165 
166  // Make sure we keep track of the fact that the implementation implements
167  // the interface.
168  ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
169 
170  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
171  PassRegistryImpl::AnalysisGroupInfo &AGI =
172  Impl->AnalysisGroupInfoMap[InterfaceInfo];
173  assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
174  "Cannot add a pass to the same analysis group more than once!");
175  AGI.Implementations.insert(ImplementationInfo);
176  if (isDefault) {
177  assert(InterfaceInfo->getNormalCtor() == 0 &&
178  "Default implementation for analysis group already specified!");
179  assert(ImplementationInfo->getNormalCtor() &&
180  "Cannot specify pass as default if it does not have a default ctor");
181  InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
182  }
183  }
184 
185  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
186  if (ShouldFree) Impl->ToFree.push_back(&Registeree);
187 }
188 
191  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
192  Impl->Listeners.push_back(L);
193 }
194 
197 
198  // NOTE: This is necessary, because removeRegistrationListener() can be called
199  // as part of the llvm_shutdown sequence. Since we have no control over the
200  // order of that sequence, we need to gracefully handle the case where the
201  // PassRegistry is destructed before the object that triggers this call.
202  if (!pImpl) return;
203 
204  PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl());
205  std::vector<PassRegistrationListener*>::iterator I =
206  std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
207  assert(I != Impl->Listeners.end() &&
208  "PassRegistrationListener not registered!");
209  Impl->Listeners.erase(I);
210 }
void registerAnalysisGroup(const void *InterfaceID, const void *PassID, PassInfo &Registeree, bool isDefault, bool ShouldFree=false)
registerAnalysisGroup - Register an analysis group (or a pass implementing
static PassRegistry * getPassRegistry()
void setNormalCtor(NormalCtor_t Ctor)
Definition: PassSupport.h:106
void enumerateWith(PassRegistrationListener *L)
const void * getTypeInfo() const
Definition: PassSupport.h:82
void addInterfaceImplemented(const PassInfo *ItfPI)
Definition: PassSupport.h:117
bool isAnalysisGroup() const
Definition: PassSupport.h:92
void removeRegistrationListener(PassRegistrationListener *L)
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:144
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:159
virtual void passEnumerate(const PassInfo *)
Definition: PassSupport.h:335
void unregisterPass(const PassInfo &PI)
NormalCtor_t getNormalCtor() const
Definition: PassSupport.h:103
static ManagedStatic< sys::SmartRWMutex< true > > Lock
void addRegistrationListener(PassRegistrationListener *L)
const char * getPassArgument() const
Definition: PassSupport.h:78
static ManagedStatic< PassRegistry > PassRegistryObj
#define I(x, y, z)
Definition: MD5.cpp:54
const PassInfo * getPassInfo(const void *TI) const
void registerPass(const PassInfo &PI, bool ShouldFree=false)