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/Optional.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/PassSupport.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/RWMutex.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() {}
42 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
43 // We don't need thread synchronization after the PassRegistry is locked
44 // (that means: is read-only).
45 Optional<sys::SmartScopedReader<true>> Guard;
49 MapType::const_iterator I = PassInfoMap.find(TI);
50 return I != PassInfoMap.end() ? I->second : nullptr;
53 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
54 // We don't need thread synchronization after the PassRegistry is locked
55 // (that means: is read-only).
56 Optional<sys::SmartScopedReader<true>> Guard;
60 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
61 return I != PassInfoStringMap.end() ? I->second : nullptr;
64 //===----------------------------------------------------------------------===//
65 // Pass Registration mechanism
68 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
70 assert(!locked && "Trying to register a pass in a locked PassRegistry");
72 sys::SmartScopedWriter<true> Guard(Lock);
74 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
75 assert(Inserted && "Pass registered multiple times!");
77 PassInfoStringMap[PI.getPassArgument()] = &PI;
79 // Notify any listeners.
80 for (auto *Listener : Listeners)
81 Listener->passRegistered(&PI);
84 ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
86 assert(!locked && "PassRegistry locked during registering a pass");
89 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
90 sys::SmartScopedReader<true> Guard(Lock);
91 for (auto PassInfoPair : PassInfoMap)
92 L->passEnumerate(PassInfoPair.second);
95 /// Analysis Group Mechanisms.
96 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
98 PassInfo &Registeree, bool isDefault,
100 PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
101 if (!InterfaceInfo) {
102 // First reference to Interface, register it now.
103 registerPass(Registeree);
104 InterfaceInfo = &Registeree;
106 assert(Registeree.isAnalysisGroup() &&
107 "Trying to join an analysis group that is a normal pass!");
110 PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
111 assert(ImplementationInfo &&
112 "Must register pass before adding to AnalysisGroup!");
114 sys::SmartScopedWriter<true> Guard(Lock);
116 // Make sure we keep track of the fact that the implementation implements
118 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
121 assert(InterfaceInfo->getNormalCtor() == nullptr &&
122 "Default implementation for analysis group already specified!");
124 ImplementationInfo->getNormalCtor() &&
125 "Cannot specify pass as default if it does not have a default ctor");
126 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
127 InterfaceInfo->setTargetMachineCtor(
128 ImplementationInfo->getTargetMachineCtor());
133 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
136 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
137 sys::SmartScopedWriter<true> Guard(Lock);
138 Listeners.push_back(L);
141 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
142 sys::SmartScopedWriter<true> Guard(Lock);
144 auto I = std::find(Listeners.begin(), Listeners.end(), L);