LLVM  4.0.0
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  InterfaceInfo->setTargetMachineCtor(
109  ImplementationInfo->getTargetMachineCtor());
110  }
111  }
112 
113  if (ShouldFree)
114  ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
115 }
116 
118  sys::SmartScopedWriter<true> Guard(Lock);
119  Listeners.push_back(L);
120 }
121 
123  sys::SmartScopedWriter<true> Guard(Lock);
124 
125  auto I = find(Listeners, L);
126  Listeners.erase(I);
127 }
MachineLoop * L
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:100
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
TargetMachineCtor_t getTargetMachineCtor() const
getTargetMachineCtor - Return a pointer to a function, that when called with a TargetMachine, creates an instance of the pass and returns it.
Definition: PassInfo.h:109
iterator find(StringRef Key)
Definition: StringMap.h:315
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:78
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:172
void addInterfaceImplemented(const PassInfo *ItfPI)
addInterfaceImplemented - This method is called when this pass is registered as a member of an analys...
Definition: PassInfo.h:127
bool isAnalysisGroup() const
isAnalysisGroup - Return true if this is an analysis group, not a normal pass.
Definition: PassInfo.h:86
void removeRegistrationListener(PassRegistrationListener *L)
removeRegistrationListener - Unregister a PassRegistrationListener so that it no longer receives pass...
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:149
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:165
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to 'opt' that will cause this pas...
Definition: PassInfo.h:74
void setTargetMachineCtor(TargetMachineCtor_t Ctor)
Definition: PassInfo.h:110
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:227
NormalCtor_t getNormalCtor() const
getNormalCtor - Return a pointer to a function, that when called, creates an instance of the pass and...
Definition: PassInfo.h:97
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:757
static ManagedStatic< PassRegistry > PassRegistryObj
#define I(x, y, z)
Definition: MD5.cpp:54
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:210
iterator end()
Definition: DenseMap.h:69
iterator find(const KeyT &Val)
Definition: DenseMap.h:127
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass' type identifier (&MyPass::...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
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:63
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:305