LLVM  6.0.0svn
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/STLExtras.h"
17 #include "llvm/PassSupport.h"
19 
20 using namespace llvm;
21 
22 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
23 // Unfortunately, passes are registered with static ctors, and having
24 // llvm_shutdown clear this map prevents successful resurrection after
25 // llvm_shutdown is run. Ideally we should find a solution so that we don't
26 // leak the map, AND can still resurrect after shutdown.
29  return &*PassRegistryObj;
30 }
31 
32 //===----------------------------------------------------------------------===//
33 // Accessors
34 //
35 
37 
38 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
39  sys::SmartScopedReader<true> Guard(Lock);
40  MapType::const_iterator I = PassInfoMap.find(TI);
41  return I != PassInfoMap.end() ? I->second : nullptr;
42 }
43 
45  sys::SmartScopedReader<true> Guard(Lock);
46  StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
47  return I != PassInfoStringMap.end() ? I->second : nullptr;
48 }
49 
50 //===----------------------------------------------------------------------===//
51 // Pass Registration mechanism
52 //
53 
54 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
55  sys::SmartScopedWriter<true> Guard(Lock);
56  bool Inserted =
57  PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
58  assert(Inserted && "Pass registered multiple times!");
59  (void)Inserted;
60  PassInfoStringMap[PI.getPassArgument()] = &PI;
61 
62  // Notify any listeners.
63  for (auto *Listener : Listeners)
64  Listener->passRegistered(&PI);
65 
66  if (ShouldFree)
67  ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
68 }
69 
71  sys::SmartScopedReader<true> Guard(Lock);
72  for (auto PassInfoPair : PassInfoMap)
73  L->passEnumerate(PassInfoPair.second);
74 }
75 
76 /// Analysis Group Mechanisms.
77 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
78  const void *PassID,
79  PassInfo &Registeree, bool isDefault,
80  bool ShouldFree) {
81  PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
82  if (!InterfaceInfo) {
83  // First reference to Interface, register it now.
84  registerPass(Registeree);
85  InterfaceInfo = &Registeree;
86  }
87  assert(Registeree.isAnalysisGroup() &&
88  "Trying to join an analysis group that is a normal pass!");
89 
90  if (PassID) {
91  PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
92  assert(ImplementationInfo &&
93  "Must register pass before adding to AnalysisGroup!");
94 
95  sys::SmartScopedWriter<true> Guard(Lock);
96 
97  // Make sure we keep track of the fact that the implementation implements
98  // the interface.
99  ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
100 
101  if (isDefault) {
102  assert(InterfaceInfo->getNormalCtor() == nullptr &&
103  "Default implementation for analysis group already specified!");
104  assert(
105  ImplementationInfo->getNormalCtor() &&
106  "Cannot specify pass as default if it does not have a default ctor");
107  InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
108  }
109  }
110 
111  if (ShouldFree)
112  ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
113 }
114 
116  sys::SmartScopedWriter<true> Guard(Lock);
117  Listeners.push_back(L);
118 }
119 
121  sys::SmartScopedWriter<true> Guard(Lock);
122 
123  auto I = find(Listeners, L);
124  Listeners.erase(I);
125 }
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass&#39; corresponding PassInfo, indexed by the pass&#39; type identifier (&MyPass::...
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()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void setNormalCtor(NormalCtor_t Ctor)
Definition: PassInfo.h:95
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener&#39;s pass...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
iterator find(StringRef Key)
Definition: StringMap.h:337
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:176
void addInterfaceImplemented(const PassInfo *ItfPI)
addInterfaceImplemented - This method is called when this pass is registered as a member of an analys...
Definition: PassInfo.h:112
void removeRegistrationListener(PassRegistrationListener *L)
removeRegistrationListener - Unregister a PassRegistrationListener so that it no longer receives pass...
NormalCtor_t getNormalCtor() const
getNormalCtor - Return a pointer to a function, that when called, creates an instance of the pass and...
Definition: PassInfo.h:92
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:146
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:162
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:73
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:131
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:217
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:32
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:839
static ManagedStatic< PassRegistry > PassRegistryObj
bool isAnalysisGroup() const
isAnalysisGroup - Return true if this is an analysis group, not a normal pass.
Definition: PassInfo.h:81
#define I(x, y, z)
Definition: MD5.cpp:58
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:200
iterator end()
Definition: DenseMap.h:73
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to &#39;opt&#39; that will cause this pas...
Definition: PassInfo.h:69
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:40
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
void registerPass(const PassInfo &PI, bool ShouldFree=false)
registerPass - Register a pass (by means of its PassInfo) with the registry.
iterator end()
Definition: StringMap.h:322