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