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/PassInfo.h"
18 #include "llvm/PassSupport.h"
20 #include <cassert>
21 #include <memory>
22 #include <utility>
23 
24 using namespace llvm;
25 
26 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
27 // Unfortunately, passes are registered with static ctors, and having
28 // llvm_shutdown clear this map prevents successful resurrection after
29 // llvm_shutdown is run. Ideally we should find a solution so that we don't
30 // leak the map, AND can still resurrect after shutdown.
33  return &*PassRegistryObj;
34 }
35 
36 //===----------------------------------------------------------------------===//
37 // Accessors
38 //
39 
40 PassRegistry::~PassRegistry() = default;
41 
42 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
43  sys::SmartScopedReader<true> Guard(Lock);
44  MapType::const_iterator I = PassInfoMap.find(TI);
45  return I != PassInfoMap.end() ? I->second : nullptr;
46 }
47 
49  sys::SmartScopedReader<true> Guard(Lock);
50  StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
51  return I != PassInfoStringMap.end() ? I->second : nullptr;
52 }
53 
54 //===----------------------------------------------------------------------===//
55 // Pass Registration mechanism
56 //
57 
58 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
59  sys::SmartScopedWriter<true> Guard(Lock);
60  bool Inserted =
61  PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
62  assert(Inserted && "Pass registered multiple times!");
63  (void)Inserted;
64  PassInfoStringMap[PI.getPassArgument()] = &PI;
65 
66  // Notify any listeners.
67  for (auto *Listener : Listeners)
68  Listener->passRegistered(&PI);
69 
70  if (ShouldFree)
71  ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
72 }
73 
75  sys::SmartScopedReader<true> Guard(Lock);
76  for (auto PassInfoPair : PassInfoMap)
77  L->passEnumerate(PassInfoPair.second);
78 }
79 
80 /// Analysis Group Mechanisms.
81 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
82  const void *PassID,
83  PassInfo &Registeree, bool isDefault,
84  bool ShouldFree) {
85  PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
86  if (!InterfaceInfo) {
87  // First reference to Interface, register it now.
88  registerPass(Registeree);
89  InterfaceInfo = &Registeree;
90  }
91  assert(Registeree.isAnalysisGroup() &&
92  "Trying to join an analysis group that is a normal pass!");
93 
94  if (PassID) {
95  PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
96  assert(ImplementationInfo &&
97  "Must register pass before adding to AnalysisGroup!");
98 
99  sys::SmartScopedWriter<true> Guard(Lock);
100 
101  // Make sure we keep track of the fact that the implementation implements
102  // the interface.
103  ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
104 
105  if (isDefault) {
106  assert(InterfaceInfo->getNormalCtor() == nullptr &&
107  "Default implementation for analysis group already specified!");
108  assert(
109  ImplementationInfo->getNormalCtor() &&
110  "Cannot specify pass as default if it does not have a default ctor");
111  InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
112  }
113  }
114 
115  if (ShouldFree)
116  ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
117 }
118 
120  sys::SmartScopedWriter<true> Guard(Lock);
121  Listeners.push_back(L);
122 }
123 
125  sys::SmartScopedWriter<true> Guard(Lock);
126 
127  auto I = llvm::find(Listeners, L);
128  Listeners.erase(I);
129 }
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:92
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:191
void addInterfaceImplemented(const PassInfo *ItfPI)
addInterfaceImplemented - This method is called when this pass is registered as a member of an analys...
Definition: PassInfo.h:108
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:89
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:72
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:146
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:211
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:31
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:788
amdgpu Simplify well known AMD library false Value Value * Arg
static ManagedStatic< PassRegistry > PassRegistryObj
bool isAnalysisGroup() const
isAnalysisGroup - Return true if this is an analysis group, not a normal pass.
Definition: PassInfo.h:79
#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:197
iterator end()
Definition: DenseMap.h:79
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:68
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:39
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