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