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/PassSupport.h"
18 #include "llvm/Support/ManagedStatic.h"
22 // FIXME: We use ManagedStatic to erase the pass registrar on shutdown.
23 // Unfortunately, passes are registered with static ctors, and having
24 // llvm_shutdown clear this map prevents successful resurrection after
25 // llvm_shutdown is run. Ideally we should find a solution so that we don't
26 // leak the map, AND can still resurrect after shutdown.
27 static ManagedStatic<PassRegistry> PassRegistryObj;
28 PassRegistry *PassRegistry::getPassRegistry() {
29 return &*PassRegistryObj;
32 //===----------------------------------------------------------------------===//
36 PassRegistry::~PassRegistry() {}
38 const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
39 sys::SmartScopedReader<true> Guard(Lock);
40 MapType::const_iterator I = PassInfoMap.find(TI);
41 return I != PassInfoMap.end() ? I->second : nullptr;
44 const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
45 sys::SmartScopedReader<true> Guard(Lock);
46 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
47 return I != PassInfoStringMap.end() ? I->second : nullptr;
50 //===----------------------------------------------------------------------===//
51 // Pass Registration mechanism
54 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
55 sys::SmartScopedWriter<true> Guard(Lock);
57 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
58 assert(Inserted && "Pass registered multiple times!");
60 PassInfoStringMap[PI.getPassArgument()] = &PI;
62 // Notify any listeners.
63 for (auto *Listener : Listeners)
64 Listener->passRegistered(&PI);
67 ToFree.push_back(std::unique_ptr<const PassInfo>(&PI));
70 void PassRegistry::enumerateWith(PassRegistrationListener *L) {
71 sys::SmartScopedReader<true> Guard(Lock);
72 for (auto PassInfoPair : PassInfoMap)
73 L->passEnumerate(PassInfoPair.second);
76 /// Analysis Group Mechanisms.
77 void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
79 PassInfo &Registeree, bool isDefault,
81 PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID));
83 // First reference to Interface, register it now.
84 registerPass(Registeree);
85 InterfaceInfo = &Registeree;
87 assert(Registeree.isAnalysisGroup() &&
88 "Trying to join an analysis group that is a normal pass!");
91 PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID));
92 assert(ImplementationInfo &&
93 "Must register pass before adding to AnalysisGroup!");
95 sys::SmartScopedWriter<true> Guard(Lock);
97 // Make sure we keep track of the fact that the implementation implements
99 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
102 assert(InterfaceInfo->getNormalCtor() == nullptr &&
103 "Default implementation for analysis group already specified!");
105 ImplementationInfo->getNormalCtor() &&
106 "Cannot specify pass as default if it does not have a default ctor");
107 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
112 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree));
115 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
116 sys::SmartScopedWriter<true> Guard(Lock);
117 Listeners.push_back(L);
120 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
121 sys::SmartScopedWriter<true> Guard(Lock);
123 auto I = find(Listeners, L);