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