1 //===- PassRegistry.cpp - Pass Registration Implementation ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the PassRegistry, with which passes are registered on
11 // initialization, and supports the PassManager in dependency resolution.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
20 #include <cassert>
21 #include <memory>
22 #include <utility>
23
24 using namespace llvm;
25
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;
getPassRegistry()32 PassRegistry *PassRegistry::getPassRegistry() {
33 return &*PassRegistryObj;
34 }
35
36 //===----------------------------------------------------------------------===//
37 // Accessors
38 //
39
40 PassRegistry::~PassRegistry() = default;
41
getPassInfo(const void * TI) const42 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;
46 }
47
getPassInfo(StringRef Arg) const48 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;
52 }
53
54 //===----------------------------------------------------------------------===//
55 // Pass Registration mechanism
56 //
57
registerPass(const PassInfo & PI,bool ShouldFree)58 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
59 sys::SmartScopedWriter<true> Guard(Lock);
60 bool Inserted =
61 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
62 assert(Inserted && "Pass registered multiple times!");
63 (void)Inserted;
64 PassInfoStringMap[PI.getPassArgument()] = &PI;
65
66 // Notify any listeners.
67 for (auto *Listener : Listeners)
68 Listener->passRegistered(&PI);
69
70 if (ShouldFree)
71 ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
72 }
73
enumerateWith(PassRegistrationListener * L)74 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
75 sys::SmartScopedReader<true> Guard(Lock);
76 for (auto PassInfoPair : PassInfoMap)
77 L->passEnumerate(PassInfoPair.second);
78 }
79
80 /// Analysis Group Mechanisms.
registerAnalysisGroup(const void * InterfaceID,const void * PassID,PassInfo & Registeree,bool isDefault,bool ShouldFree)81 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
82 const void *PassID,
83 PassInfo &Registeree, bool isDefault,
84 bool ShouldFree) {
85 PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
86 if (!InterfaceInfo) {
87 // First reference to Interface, register it now.
88 registerPass(Registeree);
89 InterfaceInfo = &Registeree;
90 }
91 assert(Registeree.isAnalysisGroup() &&
92 "Trying to join an analysis group that is a normal pass!");
93
94 if (PassID) {
95 PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
96 assert(ImplementationInfo &&
97 "Must register pass before adding to AnalysisGroup!");
98
99 sys::SmartScopedWriter<true> Guard(Lock);
100
101 // Make sure we keep track of the fact that the implementation implements
102 // the interface.
103 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
104
105 if (isDefault) {
106 assert(InterfaceInfo->getNormalCtor() == nullptr &&
107 "Default implementation for analysis group already specified!");
108 assert(
109 ImplementationInfo->getNormalCtor() &&
110 "Cannot specify pass as default if it does not have a default ctor");
111 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
112 }
113 }
114
115 if (ShouldFree)
116 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
117 }
118
addRegistrationListener(PassRegistrationListener * L)119 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
120 sys::SmartScopedWriter<true> Guard(Lock);
121 Listeners.push_back(L);
122 }
123
removeRegistrationListener(PassRegistrationListener * L)124 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
125 sys::SmartScopedWriter<true> Guard(Lock);
126
127 auto I = llvm::find(Listeners, L);
128 Listeners.erase(I);
129 }
130