LLVM 19.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
22using namespace llvm;
23
25 static PassRegistry PassRegistryObj;
26 return &PassRegistryObj;
27}
28
29//===----------------------------------------------------------------------===//
30// Accessors
31//
32
34
35const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
37 return PassInfoMap.lookup(TI);
38}
39
42 return PassInfoStringMap.lookup(Arg);
43}
44
45//===----------------------------------------------------------------------===//
46// Pass Registration mechanism
47//
48
49void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
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
67 for (auto PassInfoPair : PassInfoMap)
68 L->passEnumerate(PassInfoPair.second);
69}
70
71/// Analysis Group Mechanisms.
72void 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
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
112 Listeners.push_back(L);
113}
114
117
118 auto I = llvm::find(Listeners, L);
119 Listeners.erase(I);
120}
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
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:202
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
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
bool isAnalysisGroup() const
isAnalysisGroup - Return true if this is an analysis group, not a normal pass.
Definition: PassInfo.h:78
void setNormalCtor(NormalCtor_t Ctor)
Definition: PassInfo.h:91
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
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to 'opt' that will cause this pas...
Definition: PassInfo.h:67
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass... TODO : Rename
Definition: PassInfo.h:71
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
void removeRegistrationListener(PassRegistrationListener *L)
removeRegistrationListener - Unregister a PassRegistrationListener so that it no longer receives pass...
void registerPass(const PassInfo &PI, bool ShouldFree=false)
registerPass - Register a pass (by means of its PassInfo) with the registry.
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass' 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
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:254
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
const std::shared_lock< SmartRWMutex< mt_only > > SmartScopedReader
ScopedReader - RAII acquisition of a reader lock.
Definition: RWMutex.h:158
std::lock_guard< SmartRWMutex< mt_only > > SmartScopedWriter
ScopedWriter - RAII acquisition of a writer lock.
Definition: RWMutex.h:175
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:1751
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:215