1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PassRegistry, with which passes are registered on
11 // initialization, and supports the PassManager in dependency resolution.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/PassRegistry.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/PassInfo.h"
18 #include "llvm/PassSupport.h"
19 #include "llvm/Support/ManagedStatic.h"
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.
31 static ManagedStatic
<PassRegistry
> PassRegistryObj
;
32 PassRegistry
*PassRegistry::getPassRegistry() {
33 return &*PassRegistryObj
;
36 //===----------------------------------------------------------------------===//
40 PassRegistry::~PassRegistry() = default;
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;
48 const PassInfo
*PassRegistry::getPassInfo(StringRef Arg
) const {
49 sys::SmartScopedReader
<true> Guard(Lock
);
50 StringMapType::const_iterator I
= PassInfoStringMap
.find(Arg
);
51 return I
!= PassInfoStringMap
.end() ? I
->second
: nullptr;
54 //===----------------------------------------------------------------------===//
55 // Pass Registration mechanism
58 void PassRegistry::registerPass(const PassInfo
&PI
, bool ShouldFree
) {
59 sys::SmartScopedWriter
<true> Guard(Lock
);
61 PassInfoMap
.insert(std::make_pair(PI
.getTypeInfo(), &PI
)).second
;
62 assert(Inserted
&& "Pass registered multiple times!");
64 PassInfoStringMap
[PI
.getPassArgument()] = &PI
;
66 // Notify any listeners.
67 for (auto *Listener
: Listeners
)
68 Listener
->passRegistered(&PI
);
71 ToFree
.push_back(std::unique_ptr
<const PassInfo
>(&PI
));
74 void PassRegistry::enumerateWith(PassRegistrationListener
*L
) {
75 sys::SmartScopedReader
<true> Guard(Lock
);
76 for (auto PassInfoPair
: PassInfoMap
)
77 L
->passEnumerate(PassInfoPair
.second
);
80 /// Analysis Group Mechanisms.
81 void PassRegistry::registerAnalysisGroup(const void *InterfaceID
,
83 PassInfo
&Registeree
, bool isDefault
,
85 PassInfo
*InterfaceInfo
= const_cast<PassInfo
*>(getPassInfo(InterfaceID
));
87 // First reference to Interface, register it now.
88 registerPass(Registeree
);
89 InterfaceInfo
= &Registeree
;
91 assert(Registeree
.isAnalysisGroup() &&
92 "Trying to join an analysis group that is a normal pass!");
95 PassInfo
*ImplementationInfo
= const_cast<PassInfo
*>(getPassInfo(PassID
));
96 assert(ImplementationInfo
&&
97 "Must register pass before adding to AnalysisGroup!");
99 sys::SmartScopedWriter
<true> Guard(Lock
);
101 // Make sure we keep track of the fact that the implementation implements
103 ImplementationInfo
->addInterfaceImplemented(InterfaceInfo
);
106 assert(InterfaceInfo
->getNormalCtor() == nullptr &&
107 "Default implementation for analysis group already specified!");
109 ImplementationInfo
->getNormalCtor() &&
110 "Cannot specify pass as default if it does not have a default ctor");
111 InterfaceInfo
->setNormalCtor(ImplementationInfo
->getNormalCtor());
116 ToFree
.push_back(std::unique_ptr
<const PassInfo
>(&Registeree
));
119 void PassRegistry::addRegistrationListener(PassRegistrationListener
*L
) {
120 sys::SmartScopedWriter
<true> Guard(Lock
);
121 Listeners
.push_back(L
);
124 void PassRegistry::removeRegistrationListener(PassRegistrationListener
*L
) {
125 sys::SmartScopedWriter
<true> Guard(Lock
);
127 auto I
= llvm::find(Listeners
, L
);