1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the PassRegistry, with which passes are registered on
10 // initialization, and supports the PassManager in dependency resolution.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/PassRegistry.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/PassInfo.h"
17 #include "llvm/PassSupport.h"
18 #include "llvm/Support/ManagedStatic.h"
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.
30 static ManagedStatic
<PassRegistry
> PassRegistryObj
;
31 PassRegistry
*PassRegistry::getPassRegistry() {
32 return &*PassRegistryObj
;
35 //===----------------------------------------------------------------------===//
39 PassRegistry::~PassRegistry() = default;
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;
47 const PassInfo
*PassRegistry::getPassInfo(StringRef Arg
) const {
48 sys::SmartScopedReader
<true> Guard(Lock
);
49 StringMapType::const_iterator I
= PassInfoStringMap
.find(Arg
);
50 return I
!= PassInfoStringMap
.end() ? I
->second
: nullptr;
53 //===----------------------------------------------------------------------===//
54 // Pass Registration mechanism
57 void PassRegistry::registerPass(const PassInfo
&PI
, bool ShouldFree
) {
58 sys::SmartScopedWriter
<true> Guard(Lock
);
60 PassInfoMap
.insert(std::make_pair(PI
.getTypeInfo(), &PI
)).second
;
61 assert(Inserted
&& "Pass registered multiple times!");
63 PassInfoStringMap
[PI
.getPassArgument()] = &PI
;
65 // Notify any listeners.
66 for (auto *Listener
: Listeners
)
67 Listener
->passRegistered(&PI
);
70 ToFree
.push_back(std::unique_ptr
<const PassInfo
>(&PI
));
73 void PassRegistry::enumerateWith(PassRegistrationListener
*L
) {
74 sys::SmartScopedReader
<true> Guard(Lock
);
75 for (auto PassInfoPair
: PassInfoMap
)
76 L
->passEnumerate(PassInfoPair
.second
);
79 /// Analysis Group Mechanisms.
80 void PassRegistry::registerAnalysisGroup(const void *InterfaceID
,
82 PassInfo
&Registeree
, bool isDefault
,
84 PassInfo
*InterfaceInfo
= const_cast<PassInfo
*>(getPassInfo(InterfaceID
));
86 // First reference to Interface, register it now.
87 registerPass(Registeree
);
88 InterfaceInfo
= &Registeree
;
90 assert(Registeree
.isAnalysisGroup() &&
91 "Trying to join an analysis group that is a normal pass!");
94 PassInfo
*ImplementationInfo
= const_cast<PassInfo
*>(getPassInfo(PassID
));
95 assert(ImplementationInfo
&&
96 "Must register pass before adding to AnalysisGroup!");
98 sys::SmartScopedWriter
<true> Guard(Lock
);
100 // Make sure we keep track of the fact that the implementation implements
102 ImplementationInfo
->addInterfaceImplemented(InterfaceInfo
);
105 assert(InterfaceInfo
->getNormalCtor() == nullptr &&
106 "Default implementation for analysis group already specified!");
108 ImplementationInfo
->getNormalCtor() &&
109 "Cannot specify pass as default if it does not have a default ctor");
110 InterfaceInfo
->setNormalCtor(ImplementationInfo
->getNormalCtor());
115 ToFree
.push_back(std::unique_ptr
<const PassInfo
>(&Registeree
));
118 void PassRegistry::addRegistrationListener(PassRegistrationListener
*L
) {
119 sys::SmartScopedWriter
<true> Guard(Lock
);
120 Listeners
.push_back(L
);
123 void PassRegistry::removeRegistrationListener(PassRegistrationListener
*L
) {
124 sys::SmartScopedWriter
<true> Guard(Lock
);
126 auto I
= llvm::find(Listeners
, L
);