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