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/PassSupport.h" 18 #include "llvm/Support/ManagedStatic.h" 19 20 using namespace llvm; 21 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; 30 } 31 32 //===----------------------------------------------------------------------===// 33 // Accessors 34 // 35 36 PassRegistry::~PassRegistry() {} 37 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; 42 } 43 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; 48 } 49 50 //===----------------------------------------------------------------------===// 51 // Pass Registration mechanism 52 // 53 54 void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { 55 sys::SmartScopedWriter<true> Guard(Lock); 56 bool Inserted = 57 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second; 58 assert(Inserted && "Pass registered multiple times!"); 59 (void)Inserted; 60 PassInfoStringMap[PI.getPassArgument()] = &PI; 61 62 // Notify any listeners. 63 for (auto *Listener : Listeners) 64 Listener->passRegistered(&PI); 65 66 if (ShouldFree) 67 ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); 68 } 69 70 void PassRegistry::enumerateWith(PassRegistrationListener *L) { 71 sys::SmartScopedReader<true> Guard(Lock); 72 for (auto PassInfoPair : PassInfoMap) 73 L->passEnumerate(PassInfoPair.second); 74 } 75 76 /// Analysis Group Mechanisms. 77 void PassRegistry::registerAnalysisGroup(const void *InterfaceID, 78 const void *PassID, 79 PassInfo &Registeree, bool isDefault, 80 bool ShouldFree) { 81 PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID)); 82 if (!InterfaceInfo) { 83 // First reference to Interface, register it now. 84 registerPass(Registeree); 85 InterfaceInfo = &Registeree; 86 } 87 assert(Registeree.isAnalysisGroup() && 88 "Trying to join an analysis group that is a normal pass!"); 89 90 if (PassID) { 91 PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID)); 92 assert(ImplementationInfo && 93 "Must register pass before adding to AnalysisGroup!"); 94 95 sys::SmartScopedWriter<true> Guard(Lock); 96 97 // Make sure we keep track of the fact that the implementation implements 98 // the interface. 99 ImplementationInfo->addInterfaceImplemented(InterfaceInfo); 100 101 if (isDefault) { 102 assert(InterfaceInfo->getNormalCtor() == nullptr && 103 "Default implementation for analysis group already specified!"); 104 assert( 105 ImplementationInfo->getNormalCtor() && 106 "Cannot specify pass as default if it does not have a default ctor"); 107 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); 108 InterfaceInfo->setTargetMachineCtor( 109 ImplementationInfo->getTargetMachineCtor()); 110 } 111 } 112 113 if (ShouldFree) 114 ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree)); 115 } 116 117 void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { 118 sys::SmartScopedWriter<true> Guard(Lock); 119 Listeners.push_back(L); 120 } 121 122 void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { 123 sys::SmartScopedWriter<true> Guard(Lock); 124 125 auto I = find(Listeners, L); 126 Listeners.erase(I); 127 } 128