LLVM  16.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"
18 #include <cassert>
19 #include <memory>
20 #include <utility>
21 
22 using namespace llvm;
23 
25  static PassRegistry PassRegistryObj;
26  return &PassRegistryObj;
27 }
28 
29 //===----------------------------------------------------------------------===//
30 // Accessors
31 //
32 
33 PassRegistry::~PassRegistry() = default;
34 
35 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
36  sys::SmartScopedReader<true> Guard(Lock);
37  return PassInfoMap.lookup(TI);
38 }
39 
41  sys::SmartScopedReader<true> Guard(Lock);
42  return PassInfoStringMap.lookup(Arg);
43 }
44 
45 //===----------------------------------------------------------------------===//
46 // Pass Registration mechanism
47 //
48 
49 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
50  sys::SmartScopedWriter<true> Guard(Lock);
51  bool Inserted =
52  PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
53  assert(Inserted && "Pass registered multiple times!");
54  (void)Inserted;
55  PassInfoStringMap[PI.getPassArgument()] = &PI;
56 
57  // Notify any listeners.
58  for (auto *Listener : Listeners)
59  Listener->passRegistered(&PI);
60 
61  if (ShouldFree)
62  ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
63 }
64 
66  sys::SmartScopedReader<true> Guard(Lock);
67  for (auto PassInfoPair : PassInfoMap)
68  L->passEnumerate(PassInfoPair.second);
69 }
70 
71 /// Analysis Group Mechanisms.
72 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
73  const void *PassID,
74  PassInfo &Registeree, bool isDefault,
75  bool ShouldFree) {
76  PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
77  if (!InterfaceInfo) {
78  // First reference to Interface, register it now.
79  registerPass(Registeree);
80  InterfaceInfo = &Registeree;
81  }
82  assert(Registeree.isAnalysisGroup() &&
83  "Trying to join an analysis group that is a normal pass!");
84 
85  if (PassID) {
86  PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
87  assert(ImplementationInfo &&
88  "Must register pass before adding to AnalysisGroup!");
89 
90  sys::SmartScopedWriter<true> Guard(Lock);
91 
92  // Make sure we keep track of the fact that the implementation implements
93  // the interface.
94  ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
95 
96  if (isDefault) {
97  assert(InterfaceInfo->getNormalCtor() == nullptr &&
98  "Default implementation for analysis group already specified!");
99  assert(
100  ImplementationInfo->getNormalCtor() &&
101  "Cannot specify pass as default if it does not have a default ctor");
102  InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
103  }
104  }
105 
106  if (ShouldFree)
107  ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
108 }
109 
111  sys::SmartScopedWriter<true> Guard(Lock);
112  Listeners.push_back(L);
113 }
114 
116  sys::SmartScopedWriter<true> Guard(Lock);
117 
118  auto I = llvm::find(Listeners, L);
119  Listeners.erase(I);
120 }
llvm::sys::SmartScopedWriter
std::lock_guard< SmartRWMutex< mt_only > > SmartScopedWriter
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:178
llvm::PassInfo::getTypeInfo
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:71
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:65
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:186
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
llvm::PassRegistry::removeRegistrationListener
void removeRegistrationListener(PassRegistrationListener *L)
removeRegistrationListener - Unregister a PassRegistrationListener so that it no longer receives pass...
Definition: PassRegistry.cpp:115
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:1610
llvm::sys::SmartScopedReader
const std::shared_lock< SmartRWMutex< mt_only > > SmartScopedReader
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:161
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:58
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:233
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:50
llvm::PassRegistry::addRegistrationListener
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
Definition: PassRegistry.cpp:110
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:35
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:72
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:49