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" |
Erik Eckstein | 8c38b8b | 2015-03-04 18:57:11 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Optional.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/PassSupport.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/ManagedStatic.h" |
Chad Rosier | 673a7db | 2013-07-03 18:38:08 +0000 | [diff] [blame] | 21 | #include "llvm/Support/RWMutex.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 |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 28 | // llvm_shutdown clear this map prevents successful resurrection after |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 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 | // Accessors |
| 38 | // |
| 39 | |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 40 | PassRegistry::~PassRegistry() {} |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 41 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 42 | const PassInfo *PassRegistry::getPassInfo(const void *TI) const { |
Erik Eckstein | 8c38b8b | 2015-03-04 18:57:11 +0000 | [diff] [blame] | 43 | // We don't need thread synchronization after the PassRegistry is locked |
| 44 | // (that means: is read-only). |
| 45 | Optional<sys::SmartScopedReader<true>> Guard; |
| 46 | if (!locked) |
| 47 | Guard.emplace(Lock); |
| 48 | |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 49 | MapType::const_iterator I = PassInfoMap.find(TI); |
| 50 | return I != PassInfoMap.end() ? I->second : nullptr; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { |
Erik Eckstein | 8c38b8b | 2015-03-04 18:57:11 +0000 | [diff] [blame] | 54 | // We don't need thread synchronization after the PassRegistry is locked |
| 55 | // (that means: is read-only). |
| 56 | Optional<sys::SmartScopedReader<true>> Guard; |
| 57 | if (!locked) |
| 58 | Guard.emplace(Lock); |
| 59 | |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 60 | StringMapType::const_iterator I = PassInfoStringMap.find(Arg); |
| 61 | return I != PassInfoStringMap.end() ? I->second : nullptr; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // Pass Registration mechanism |
| 66 | // |
| 67 | |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 68 | void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { |
Erik Eckstein | 8c38b8b | 2015-03-04 18:57:11 +0000 | [diff] [blame] | 69 | |
| 70 | assert(!locked && "Trying to register a pass in a locked PassRegistry"); |
| 71 | |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 72 | sys::SmartScopedWriter<true> Guard(Lock); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 73 | bool Inserted = |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 74 | PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second; |
Jakob Stoklund Olesen | 88b4b27 | 2011-01-05 21:50:21 +0000 | [diff] [blame] | 75 | assert(Inserted && "Pass registered multiple times!"); |
| 76 | (void)Inserted; |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 77 | PassInfoStringMap[PI.getPassArgument()] = &PI; |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 78 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 79 | // Notify any listeners. |
Chandler Carruth | 484bc69 | 2014-10-06 00:06:48 +0000 | [diff] [blame] | 80 | for (auto *Listener : Listeners) |
| 81 | Listener->passRegistered(&PI); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 82 | |
| 83 | if (ShouldFree) |
| 84 | ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); |
Erik Eckstein | 8c38b8b | 2015-03-04 18:57:11 +0000 | [diff] [blame] | 85 | |
| 86 | assert(!locked && "PassRegistry locked during registering a pass"); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 89 | void PassRegistry::enumerateWith(PassRegistrationListener *L) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 90 | sys::SmartScopedReader<true> Guard(Lock); |
Chandler Carruth | 484bc69 | 2014-10-06 00:06:48 +0000 | [diff] [blame] | 91 | for (auto PassInfoPair : PassInfoMap) |
| 92 | L->passEnumerate(PassInfoPair.second); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 95 | /// Analysis Group Mechanisms. |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 96 | void PassRegistry::registerAnalysisGroup(const void *InterfaceID, |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 97 | const void *PassID, |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 98 | PassInfo &Registeree, bool isDefault, |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 99 | bool ShouldFree) { |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 100 | PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID)); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 101 | if (!InterfaceInfo) { |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 102 | // First reference to Interface, register it now. |
| 103 | registerPass(Registeree); |
| 104 | InterfaceInfo = &Registeree; |
| 105 | } |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 106 | assert(Registeree.isAnalysisGroup() && |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 107 | "Trying to join an analysis group that is a normal pass!"); |
| 108 | |
| 109 | if (PassID) { |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 110 | PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID)); |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 111 | assert(ImplementationInfo && |
| 112 | "Must register pass before adding to AnalysisGroup!"); |
| 113 | |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 114 | sys::SmartScopedWriter<true> Guard(Lock); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 115 | |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 116 | // Make sure we keep track of the fact that the implementation implements |
| 117 | // the interface. |
| 118 | ImplementationInfo->addInterfaceImplemented(InterfaceInfo); |
| 119 | |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 120 | if (isDefault) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 121 | assert(InterfaceInfo->getNormalCtor() == nullptr && |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 122 | "Default implementation for analysis group already specified!"); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 123 | assert( |
| 124 | ImplementationInfo->getNormalCtor() && |
| 125 | "Cannot specify pass as default if it does not have a default ctor"); |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 126 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
Quentin Colombet | dc0b2ea | 2014-01-16 21:44:34 +0000 | [diff] [blame] | 127 | InterfaceInfo->setTargetMachineCtor( |
| 128 | ImplementationInfo->getTargetMachineCtor()); |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 129 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 130 | } |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 131 | |
David Blaikie | 0afad5e | 2014-04-15 15:17:14 +0000 | [diff] [blame] | 132 | if (ShouldFree) |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 133 | ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree)); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 134 | } |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 135 | |
| 136 | void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 137 | sys::SmartScopedWriter<true> Guard(Lock); |
| 138 | Listeners.push_back(L); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 142 | sys::SmartScopedWriter<true> Guard(Lock); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 143 | |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 144 | auto I = std::find(Listeners.begin(), Listeners.end(), L); |
| 145 | Listeners.erase(I); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 146 | } |