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/IR/Function.h" 17 #include "llvm/PassSupport.h" 18 #include "llvm/Support/Compiler.h" 19 #include "llvm/Support/ManagedStatic.h" 20 #include "llvm/Support/RWMutex.h" 21 #include <vector> 22 23 using namespace llvm; 24 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; 33 } 34 35 //===----------------------------------------------------------------------===// 36 // Accessors 37 // 38 39 PassRegistry::~PassRegistry() { 40 } 41 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; 46 } 47 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; 52 } 53 54 //===----------------------------------------------------------------------===// 55 // Pass Registration mechanism 56 // 57 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 (std::vector<PassRegistrationListener*>::iterator 68 I = Listeners.begin(), E = Listeners.end(); I != E; ++I) 69 (*I)->passRegistered(&PI); 70 71 if (ShouldFree) ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); 72 } 73 74 void PassRegistry::unregisterPass(const PassInfo &PI) { 75 sys::SmartScopedWriter<true> Guard(Lock); 76 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo()); 77 assert(I != PassInfoMap.end() && "Pass registered but not in map!"); 78 79 // Remove pass from the map. 80 PassInfoMap.erase(I); 81 PassInfoStringMap.erase(PI.getPassArgument()); 82 } 83 84 void PassRegistry::enumerateWith(PassRegistrationListener *L) { 85 sys::SmartScopedReader<true> Guard(Lock); 86 for (auto I = PassInfoMap.begin(), E = PassInfoMap.end(); I != E; ++I) 87 L->passEnumerate(I->second); 88 } 89 90 91 /// Analysis Group Mechanisms. 92 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 93 const void *PassID, 94 PassInfo& Registeree, 95 bool isDefault, 96 bool ShouldFree) { 97 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID)); 98 if (!InterfaceInfo) { 99 // First reference to Interface, register it now. 100 registerPass(Registeree); 101 InterfaceInfo = &Registeree; 102 } 103 assert(Registeree.isAnalysisGroup() && 104 "Trying to join an analysis group that is a normal pass!"); 105 106 if (PassID) { 107 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID)); 108 assert(ImplementationInfo && 109 "Must register pass before adding to AnalysisGroup!"); 110 111 sys::SmartScopedWriter<true> Guard(Lock); 112 113 // Make sure we keep track of the fact that the implementation implements 114 // the interface. 115 ImplementationInfo->addInterfaceImplemented(InterfaceInfo); 116 117 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo]; 118 assert(AGI.Implementations.count(ImplementationInfo) == 0 && 119 "Cannot add a pass to the same analysis group more than once!"); 120 AGI.Implementations.insert(ImplementationInfo); 121 if (isDefault) { 122 assert(InterfaceInfo->getNormalCtor() == nullptr && 123 "Default implementation for analysis group already specified!"); 124 assert(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()); 129 } 130 } 131 132 if (ShouldFree) 133 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree)); 134 } 135 136 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { 137 sys::SmartScopedWriter<true> Guard(Lock); 138 Listeners.push_back(L); 139 } 140 141 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { 142 sys::SmartScopedWriter<true> Guard(Lock); 143 144 auto I = std::find(Listeners.begin(), Listeners.end(), L); 145 Listeners.erase(I); 146 } 147