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" |
Owen Anderson | d878827 | 2010-09-16 23:32:35 +0000 | [diff] [blame] | 19 | #include "llvm/System/Mutex.h" |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #include "llvm/ADT/SmallPtrSet.h" |
Owen Anderson | 0bf2a92 | 2010-09-13 18:47:42 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 23 | #include <vector> |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 24 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 27 | // FIXME: We use ManagedStatic to erase the pass registrar on shutdown. |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 28 | // Unfortunately, passes are registered with static ctors, and having |
| 29 | // llvm_shutdown clear this map prevents successful ressurection after |
| 30 | // llvm_shutdown is run. Ideally we should find a solution so that we don't |
| 31 | // leak the map, AND can still resurrect after shutdown. |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 32 | static ManagedStatic<PassRegistry> PassRegistryObj; |
| 33 | PassRegistry *PassRegistry::getPassRegistry() { |
| 34 | return &*PassRegistryObj; |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 35 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 36 | |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 37 | static ManagedStatic<sys::SmartMutex<true> > Lock; |
Owen Anderson | d878827 | 2010-09-16 23:32:35 +0000 | [diff] [blame] | 38 | |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // PassRegistryImpl |
| 41 | // |
| 42 | |
Benjamin Kramer | 9192e7a | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 43 | namespace { |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 44 | struct PassRegistryImpl { |
| 45 | /// PassInfoMap - Keep track of the PassInfo object for each registered pass. |
| 46 | typedef DenseMap<const void*, const PassInfo*> MapType; |
| 47 | MapType PassInfoMap; |
| 48 | |
| 49 | typedef StringMap<const PassInfo*> StringMapType; |
| 50 | StringMapType PassInfoStringMap; |
| 51 | |
| 52 | /// AnalysisGroupInfo - Keep track of information for each analysis group. |
| 53 | struct AnalysisGroupInfo { |
| 54 | SmallPtrSet<const PassInfo *, 8> Implementations; |
| 55 | }; |
| 56 | DenseMap<const PassInfo*, AnalysisGroupInfo> AnalysisGroupInfoMap; |
| 57 | |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 58 | std::vector<const PassInfo*> ToFree; |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 59 | std::vector<PassRegistrationListener*> Listeners; |
| 60 | }; |
Benjamin Kramer | 9192e7a | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 61 | } // end anonymous namespace |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 62 | |
| 63 | void *PassRegistry::getImpl() const { |
| 64 | if (!pImpl) |
| 65 | pImpl = new PassRegistryImpl(); |
| 66 | return pImpl; |
| 67 | } |
| 68 | |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // Accessors |
| 71 | // |
| 72 | |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 73 | PassRegistry::~PassRegistry() { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 74 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 75 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(pImpl); |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 76 | |
| 77 | for (std::vector<const PassInfo*>::iterator I = Impl->ToFree.begin(), |
| 78 | E = Impl->ToFree.end(); I != E; ++I) |
| 79 | delete *I; |
| 80 | |
Dan Gohman | cb85497 | 2010-10-12 00:19:24 +0000 | [diff] [blame] | 81 | delete Impl; |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 82 | pImpl = 0; |
| 83 | } |
| 84 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 85 | const PassInfo *PassRegistry::getPassInfo(const void *TI) const { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 86 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 87 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 88 | PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.find(TI); |
| 89 | return I != Impl->PassInfoMap.end() ? I->second : 0; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 93 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 94 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 95 | PassRegistryImpl::StringMapType::const_iterator |
| 96 | I = Impl->PassInfoStringMap.find(Arg); |
| 97 | return I != Impl->PassInfoStringMap.end() ? I->second : 0; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 100 | //===----------------------------------------------------------------------===// |
| 101 | // Pass Registration mechanism |
| 102 | // |
| 103 | |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 104 | void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 105 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 106 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 107 | bool Inserted = |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 108 | Impl->PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 109 | assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted; |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 110 | Impl->PassInfoStringMap[PI.getPassArgument()] = &PI; |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 111 | |
| 112 | // Notify any listeners. |
| 113 | for (std::vector<PassRegistrationListener*>::iterator |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 114 | I = Impl->Listeners.begin(), E = Impl->Listeners.end(); I != E; ++I) |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 115 | (*I)->passRegistered(&PI); |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 116 | |
| 117 | if (ShouldFree) Impl->ToFree.push_back(&PI); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void PassRegistry::unregisterPass(const PassInfo &PI) { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 121 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 122 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 123 | PassRegistryImpl::MapType::iterator I = |
| 124 | Impl->PassInfoMap.find(PI.getTypeInfo()); |
| 125 | assert(I != Impl->PassInfoMap.end() && "Pass registered but not in map!"); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 126 | |
| 127 | // Remove pass from the map. |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 128 | Impl->PassInfoMap.erase(I); |
| 129 | Impl->PassInfoStringMap.erase(PI.getPassArgument()); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void PassRegistry::enumerateWith(PassRegistrationListener *L) { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 133 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 134 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 135 | for (PassRegistryImpl::MapType::const_iterator I = Impl->PassInfoMap.begin(), |
| 136 | E = Impl->PassInfoMap.end(); I != E; ++I) |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 137 | L->passEnumerate(I->second); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /// Analysis Group Mechanisms. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 142 | void PassRegistry::registerAnalysisGroup(const void *InterfaceID, |
| 143 | const void *PassID, |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 144 | PassInfo& Registeree, |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 145 | bool isDefault, |
| 146 | bool ShouldFree) { |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 147 | PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID)); |
| 148 | if (InterfaceInfo == 0) { |
| 149 | // First reference to Interface, register it now. |
| 150 | registerPass(Registeree); |
| 151 | InterfaceInfo = &Registeree; |
| 152 | } |
| 153 | assert(Registeree.isAnalysisGroup() && |
| 154 | "Trying to join an analysis group that is a normal pass!"); |
| 155 | |
| 156 | if (PassID) { |
| 157 | PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID)); |
| 158 | assert(ImplementationInfo && |
| 159 | "Must register pass before adding to AnalysisGroup!"); |
| 160 | |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 161 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | d878827 | 2010-09-16 23:32:35 +0000 | [diff] [blame] | 162 | |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 163 | // Make sure we keep track of the fact that the implementation implements |
| 164 | // the interface. |
| 165 | ImplementationInfo->addInterfaceImplemented(InterfaceInfo); |
| 166 | |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 167 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 168 | PassRegistryImpl::AnalysisGroupInfo &AGI = |
| 169 | Impl->AnalysisGroupInfoMap[InterfaceInfo]; |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 170 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 171 | "Cannot add a pass to the same analysis group more than once!"); |
| 172 | AGI.Implementations.insert(ImplementationInfo); |
| 173 | if (isDefault) { |
| 174 | assert(InterfaceInfo->getNormalCtor() == 0 && |
| 175 | "Default implementation for analysis group already specified!"); |
| 176 | assert(ImplementationInfo->getNormalCtor() && |
| 177 | "Cannot specify pass as default if it does not have a default ctor"); |
| 178 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 179 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 180 | } |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 181 | |
| 182 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 183 | if (ShouldFree) Impl->ToFree.push_back(&Registeree); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 184 | } |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 185 | |
| 186 | void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 187 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 188 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
| 189 | Impl->Listeners.push_back(L); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { |
Owen Anderson | c2fcd610 | 2010-09-16 23:44:50 +0000 | [diff] [blame] | 193 | sys::SmartScopedLock<true> Guard(*Lock); |
Owen Anderson | d475497 | 2010-09-15 23:03:33 +0000 | [diff] [blame] | 194 | |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 195 | // NOTE: This is necessary, because removeRegistrationListener() can be called |
| 196 | // as part of the llvm_shutdown sequence. Since we have no control over the |
| 197 | // order of that sequence, we need to gracefully handle the case where the |
| 198 | // PassRegistry is destructed before the object that triggers this call. |
| 199 | if (!pImpl) return; |
| 200 | |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 201 | PassRegistryImpl *Impl = static_cast<PassRegistryImpl*>(getImpl()); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 202 | std::vector<PassRegistrationListener*>::iterator I = |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 203 | std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L); |
| 204 | assert(I != Impl->Listeners.end() && |
| 205 | "PassRegistrationListener not registered!"); |
| 206 | Impl->Listeners.erase(I); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 207 | } |