Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 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" |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 16 | #include "llvm/PassSupport.h" |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/ManagedStatic.h" |
| 19 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 22 | static PassRegistry *PassRegistryObj = 0; |
| 23 | PassRegistry *PassRegistry::getPassRegistry() { |
| 24 | // Use double-checked locking to safely initialize the registrar when |
| 25 | // we're running in multithreaded mode. |
| 26 | PassRegistry* tmp = PassRegistryObj; |
| 27 | if (llvm_is_multithreaded()) { |
| 28 | sys::MemoryFence(); |
| 29 | if (!tmp) { |
| 30 | llvm_acquire_global_lock(); |
| 31 | tmp = PassRegistryObj; |
| 32 | if (!tmp) { |
| 33 | tmp = new PassRegistry(); |
| 34 | sys::MemoryFence(); |
| 35 | PassRegistryObj = tmp; |
| 36 | } |
| 37 | llvm_release_global_lock(); |
| 38 | } |
| 39 | } else if (!tmp) { |
| 40 | PassRegistryObj = new PassRegistry(); |
| 41 | } |
| 42 | |
| 43 | return PassRegistryObj; |
| 44 | } |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | // FIXME: We use ManagedCleanup to erase the pass registrar on shutdown. |
| 49 | // Unfortunately, passes are registered with static ctors, and having |
| 50 | // llvm_shutdown clear this map prevents successful ressurection after |
| 51 | // llvm_shutdown is run. Ideally we should find a solution so that we don't |
| 52 | // leak the map, AND can still resurrect after shutdown. |
| 53 | void cleanupPassRegistry(void*) { |
| 54 | if (PassRegistryObj) { |
| 55 | delete PassRegistryObj; |
| 56 | PassRegistryObj = 0; |
| 57 | } |
| 58 | } |
| 59 | ManagedCleanup<&cleanupPassRegistry> registryCleanup ATTRIBUTE_USED; |
| 60 | |
| 61 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 62 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame^] | 63 | const PassInfo *PassRegistry::getPassInfo(const void *TI) const { |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 64 | sys::SmartScopedLock<true> Guard(Lock); |
| 65 | MapType::const_iterator I = PassInfoMap.find(TI); |
| 66 | return I != PassInfoMap.end() ? I->second : 0; |
| 67 | } |
| 68 | |
| 69 | const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { |
| 70 | sys::SmartScopedLock<true> Guard(Lock); |
| 71 | StringMapType::const_iterator I = PassInfoStringMap.find(Arg); |
| 72 | return I != PassInfoStringMap.end() ? I->second : 0; |
| 73 | } |
| 74 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
| 76 | // Pass Registration mechanism |
| 77 | // |
| 78 | |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 79 | void PassRegistry::registerPass(const PassInfo &PI) { |
| 80 | sys::SmartScopedLock<true> Guard(Lock); |
| 81 | bool Inserted = |
| 82 | PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second; |
| 83 | assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted; |
| 84 | PassInfoStringMap[PI.getPassArgument()] = &PI; |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 85 | |
| 86 | // Notify any listeners. |
| 87 | for (std::vector<PassRegistrationListener*>::iterator |
| 88 | I = Listeners.begin(), E = Listeners.end(); I != E; ++I) |
| 89 | (*I)->passRegistered(&PI); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void PassRegistry::unregisterPass(const PassInfo &PI) { |
| 93 | sys::SmartScopedLock<true> Guard(Lock); |
| 94 | MapType::iterator I = PassInfoMap.find(PI.getTypeInfo()); |
| 95 | assert(I != PassInfoMap.end() && "Pass registered but not in map!"); |
| 96 | |
| 97 | // Remove pass from the map. |
| 98 | PassInfoMap.erase(I); |
| 99 | PassInfoStringMap.erase(PI.getPassArgument()); |
| 100 | } |
| 101 | |
| 102 | void PassRegistry::enumerateWith(PassRegistrationListener *L) { |
| 103 | sys::SmartScopedLock<true> Guard(Lock); |
| 104 | for (MapType::const_iterator I = PassInfoMap.begin(), |
| 105 | E = PassInfoMap.end(); I != E; ++I) |
| 106 | L->passEnumerate(I->second); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /// Analysis Group Mechanisms. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame^] | 111 | void PassRegistry::registerAnalysisGroup(const void *InterfaceID, |
| 112 | const void *PassID, |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 113 | PassInfo& Registeree, |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 114 | bool isDefault) { |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 115 | PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID)); |
| 116 | if (InterfaceInfo == 0) { |
| 117 | // First reference to Interface, register it now. |
| 118 | registerPass(Registeree); |
| 119 | InterfaceInfo = &Registeree; |
| 120 | } |
| 121 | assert(Registeree.isAnalysisGroup() && |
| 122 | "Trying to join an analysis group that is a normal pass!"); |
| 123 | |
| 124 | if (PassID) { |
| 125 | PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID)); |
| 126 | assert(ImplementationInfo && |
| 127 | "Must register pass before adding to AnalysisGroup!"); |
| 128 | |
| 129 | // Make sure we keep track of the fact that the implementation implements |
| 130 | // the interface. |
| 131 | ImplementationInfo->addInterfaceImplemented(InterfaceInfo); |
| 132 | |
| 133 | sys::SmartScopedLock<true> Guard(Lock); |
| 134 | AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo]; |
| 135 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 136 | "Cannot add a pass to the same analysis group more than once!"); |
| 137 | AGI.Implementations.insert(ImplementationInfo); |
| 138 | if (isDefault) { |
| 139 | assert(InterfaceInfo->getNormalCtor() == 0 && |
| 140 | "Default implementation for analysis group already specified!"); |
| 141 | assert(ImplementationInfo->getNormalCtor() && |
| 142 | "Cannot specify pass as default if it does not have a default ctor"); |
| 143 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 144 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 147 | |
| 148 | void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { |
| 149 | sys::SmartScopedLock<true> Guard(Lock); |
| 150 | Listeners.push_back(L); |
| 151 | } |
| 152 | |
| 153 | void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { |
| 154 | sys::SmartScopedLock<true> Guard(Lock); |
| 155 | std::vector<PassRegistrationListener*>::iterator I = |
| 156 | std::find(Listeners.begin(), Listeners.end(), L); |
| 157 | assert(I != Listeners.end() && "PassRegistrationListener not registered!"); |
| 158 | Listeners.erase(I); |
| 159 | } |