LLVM  9.0.0svn
PassRegistry.cpp
Go to the documentation of this file.
1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the PassRegistry, with which passes are registered on
10 // initialization, and supports the PassManager in dependency resolution.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/PassRegistry.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/PassInfo.h"
17 #include "llvm/PassSupport.h"
19 #include <cassert>
20 #include <memory>
21 #include <utility>
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 resurrection 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.
32  return &*PassRegistryObj;
33 }
34 
35 //===----------------------------------------------------------------------===//
36 // Accessors
37 //
38 
39 PassRegistry::~PassRegistry() = default;
40 
41 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
42  sys::SmartScopedReader<true> Guard(Lock);
43  MapType::const_iterator I = PassInfoMap.find(TI);
44  return I != PassInfoMap.end() ? I->second : nullptr;
45 }
46 
48  sys::SmartScopedReader<true> Guard(Lock);
49  StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
50  return I != PassInfoStringMap.end() ? I->second : nullptr;
51 }
52 
53 //===----------------------------------------------------------------------===//
54 // Pass Registration mechanism
55 //
56 
57 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
58  sys::SmartScopedWriter<true> Guard(Lock);
59  bool Inserted =
60  PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
61  assert(Inserted && "Pass registered multiple times!");
62  (void)Inserted;
63  PassInfoStringMap[PI.getPassArgument()] = &PI;
64 
65  // Notify any listeners.
66  for (auto *Listener : Listeners)
67  Listener->passRegistered(&PI);
68 
69  if (ShouldFree)
70  ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
71 }
72 
74  sys::SmartScopedReader<true> Guard(Lock);
75  for (auto PassInfoPair : PassInfoMap)
76  L->passEnumerate(PassInfoPair.second);
77 }
78 
79 /// Analysis Group Mechanisms.
80 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
81  const void *PassID,
82  PassInfo &Registeree, bool isDefault,
83  bool ShouldFree) {
84  PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
85  if (!InterfaceInfo) {
86  // First reference to Interface, register it now.
87  registerPass(Registeree);
88  InterfaceInfo = &Registeree;
89  }
90  assert(Registeree.isAnalysisGroup() &&
91  "Trying to join an analysis group that is a normal pass!");
92 
93  if (PassID) {
94  PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
95  assert(ImplementationInfo &&
96  "Must register pass before adding to AnalysisGroup!");
97 
98  sys::SmartScopedWriter<true> Guard(Lock);
99 
100  // Make sure we keep track of the fact that the implementation implements
101  // the interface.
102  ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
103 
104  if (isDefault) {
105  assert(InterfaceInfo->getNormalCtor() == nullptr &&
106  "Default implementation for analysis group already specified!");
107  assert(
108  ImplementationInfo->getNormalCtor() &&
109  "Cannot specify pass as default if it does not have a default ctor");
110  InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
111  }
112  }
113 
114  if (ShouldFree)
115  ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
116 }
117 
119  sys::SmartScopedWriter<true> Guard(Lock);
120  Listeners.push_back(L);
121 }
122 
124  sys::SmartScopedWriter<true> Guard(Lock);
125 
126  auto I = llvm::find(Listeners, L);
127  Listeners.erase(I);
128 }
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:91
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener&#39;s pass...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator find(StringRef Key)
Definition: StringMap.h:332
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:221
void addInterfaceImplemented(const PassInfo *ItfPI)
addInterfaceImplemented - This method is called when this pass is registered as a member of an analys...
Definition: PassInfo.h:107
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:88
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:145
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:161
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:71
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:176
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:210
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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:30
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1206
static ManagedStatic< PassRegistry > PassRegistryObj
bool isAnalysisGroup() const
isAnalysisGroup - Return true if this is an analysis group, not a normal pass.
Definition: PassInfo.h:78
#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:196
iterator end()
Definition: DenseMap.h:108
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:67
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:79
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:317