LLVM  13.0.0git
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/Pass.h"
17 #include "llvm/PassInfo.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  return PassInfoMap.lookup(TI);
44 }
45 
47  sys::SmartScopedReader<true> Guard(Lock);
48  return PassInfoStringMap.lookup(Arg);
49 }
50 
51 //===----------------------------------------------------------------------===//
52 // Pass Registration mechanism
53 //
54 
55 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
56  sys::SmartScopedWriter<true> Guard(Lock);
57  bool Inserted =
58  PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
59  assert(Inserted && "Pass registered multiple times!");
60  (void)Inserted;
61  PassInfoStringMap[PI.getPassArgument()] = &PI;
62 
63  // Notify any listeners.
64  for (auto *Listener : Listeners)
65  Listener->passRegistered(&PI);
66 
67  if (ShouldFree)
68  ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
69 }
70 
72  sys::SmartScopedReader<true> Guard(Lock);
73  for (auto PassInfoPair : PassInfoMap)
74  L->passEnumerate(PassInfoPair.second);
75 }
76 
77 /// Analysis Group Mechanisms.
78 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
79  const void *PassID,
80  PassInfo &Registeree, bool isDefault,
81  bool ShouldFree) {
82  PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
83  if (!InterfaceInfo) {
84  // First reference to Interface, register it now.
85  registerPass(Registeree);
86  InterfaceInfo = &Registeree;
87  }
88  assert(Registeree.isAnalysisGroup() &&
89  "Trying to join an analysis group that is a normal pass!");
90 
91  if (PassID) {
92  PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
93  assert(ImplementationInfo &&
94  "Must register pass before adding to AnalysisGroup!");
95 
96  sys::SmartScopedWriter<true> Guard(Lock);
97 
98  // Make sure we keep track of the fact that the implementation implements
99  // the interface.
100  ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
101 
102  if (isDefault) {
103  assert(InterfaceInfo->getNormalCtor() == nullptr &&
104  "Default implementation for analysis group already specified!");
105  assert(
106  ImplementationInfo->getNormalCtor() &&
107  "Cannot specify pass as default if it does not have a default ctor");
108  InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
109  }
110  }
111 
112  if (ShouldFree)
113  ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
114 }
115 
117  sys::SmartScopedWriter<true> Guard(Lock);
118  Listeners.push_back(L);
119 }
120 
122  sys::SmartScopedWriter<true> Guard(Lock);
123 
124  auto I = llvm::find(Listeners, L);
125  Listeners.erase(I);
126 }
llvm::sys::SmartScopedWriter
std::lock_guard< SmartRWMutex< mt_only > > SmartScopedWriter
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:184
llvm::PassInfo::getTypeInfo
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:71
llvm
Definition: AllocatorList.h:23
llvm::DenseMapBase::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:197
Pass.h
llvm::PassRegistry::enumerateWith
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
Definition: PassRegistry.cpp:71
ManagedStatic.h
llvm::PassInfo
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
STLExtras.h
PassRegistry.h
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:205
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
PassRegistryObj
static ManagedStatic< PassRegistry > PassRegistryObj
Definition: PassRegistry.cpp:30
llvm::PassRegistry::removeRegistrationListener
void removeRegistrationListener(PassRegistrationListener *L)
removeRegistrationListener - Unregister a PassRegistrationListener so that it no longer receives pass...
Definition: PassRegistry.cpp:121
llvm::PassInfo::isAnalysisGroup
bool isAnalysisGroup() const
isAnalysisGroup - Return true if this is an analysis group, not a normal pass.
Definition: PassInfo.h:78
llvm::PassInfo::setNormalCtor
void setNormalCtor(NormalCtor_t Ctor)
Definition: PassInfo.h:91
llvm::find
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1502
llvm::sys::SmartScopedReader
const std::shared_lock< SmartRWMutex< mt_only > > SmartScopedReader
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:167
llvm::PassInfo::getPassArgument
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to 'opt' that will cause this pas...
Definition: PassInfo.h:67
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::StringMap::lookup
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: StringMap.h:234
llvm::PassRegistrationListener
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:200
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::PassInfo::getNormalCtor
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
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::PassRegistry::addRegistrationListener
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
Definition: PassRegistry.cpp:116
llvm::PassRegistry::~PassRegistry
~PassRegistry()
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:207
llvm::PassInfo::addInterfaceImplemented
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
PassInfo.h
llvm::PassRegistry::getPassInfo
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass' type identifier (&MyPass::...
Definition: PassRegistry.cpp:41
llvm::PassRegistrationListener::passEnumerate
virtual void passEnumerate(const PassInfo *)
passEnumerate - Callback function invoked when someone calls enumeratePasses on this PassRegistration...
Definition: PassSupport.h:214
llvm::PassRegistry::registerAnalysisGroup
void registerAnalysisGroup(const void *InterfaceID, const void *PassID, PassInfo &Registeree, bool isDefault, bool ShouldFree=false)
registerAnalysisGroup - Register an analysis group (or a pass implementing
Definition: PassRegistry.cpp:78
llvm::PassRegistry::registerPass
void registerPass(const PassInfo &PI, bool ShouldFree=false)
registerPass - Register a pass (by means of its PassInfo) with the registry.
Definition: PassRegistry.cpp:55