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" |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/PassSupport.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 19 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 22 | // FIXME: We use ManagedStatic to erase the pass registrar on shutdown. |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 23 | // Unfortunately, passes are registered with static ctors, and having |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 24 | // llvm_shutdown clear this map prevents successful resurrection after |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 25 | // llvm_shutdown is run. Ideally we should find a solution so that we don't |
| 26 | // leak the map, AND can still resurrect after shutdown. |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 27 | static ManagedStatic<PassRegistry> PassRegistryObj; |
| 28 | PassRegistry *PassRegistry::getPassRegistry() { |
| 29 | return &*PassRegistryObj; |
Owen Anderson | 4154061 | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 30 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 31 | |
Owen Anderson | a74fa15 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
| 33 | // Accessors |
| 34 | // |
| 35 | |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 36 | PassRegistry::~PassRegistry() {} |
Owen Anderson | d12ea00 | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 37 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 38 | const PassInfo *PassRegistry::getPassInfo(const void *TI) const { |
Erik Eckstein | 8c76e66 | 2015-03-05 17:53:00 +0000 | [diff] [blame] | 39 | sys::SmartScopedReader<true> Guard(Lock); |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 40 | MapType::const_iterator I = PassInfoMap.find(TI); |
| 41 | return I != PassInfoMap.end() ? I->second : nullptr; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { |
Erik Eckstein | 8c76e66 | 2015-03-05 17:53:00 +0000 | [diff] [blame] | 45 | sys::SmartScopedReader<true> Guard(Lock); |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 46 | StringMapType::const_iterator I = PassInfoStringMap.find(Arg); |
| 47 | return I != PassInfoStringMap.end() ? I->second : nullptr; |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Pass Registration mechanism |
| 52 | // |
| 53 | |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 54 | void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 55 | sys::SmartScopedWriter<true> Guard(Lock); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 56 | bool Inserted = |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 57 | PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second; |
Jakob Stoklund Olesen | 88b4b27 | 2011-01-05 21:50:21 +0000 | [diff] [blame] | 58 | assert(Inserted && "Pass registered multiple times!"); |
| 59 | (void)Inserted; |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 60 | PassInfoStringMap[PI.getPassArgument()] = &PI; |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 61 | |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 62 | // Notify any listeners. |
Chandler Carruth | 484bc69 | 2014-10-06 00:06:48 +0000 | [diff] [blame] | 63 | for (auto *Listener : Listeners) |
| 64 | Listener->passRegistered(&PI); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 65 | |
| 66 | if (ShouldFree) |
| 67 | ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 70 | void PassRegistry::enumerateWith(PassRegistrationListener *L) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 71 | sys::SmartScopedReader<true> Guard(Lock); |
Chandler Carruth | 484bc69 | 2014-10-06 00:06:48 +0000 | [diff] [blame] | 72 | for (auto PassInfoPair : PassInfoMap) |
| 73 | L->passEnumerate(PassInfoPair.second); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 76 | /// Analysis Group Mechanisms. |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 77 | void PassRegistry::registerAnalysisGroup(const void *InterfaceID, |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 78 | const void *PassID, |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 79 | PassInfo &Registeree, bool isDefault, |
Owen Anderson | 80b8817 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 80 | bool ShouldFree) { |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 81 | PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID)); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 82 | if (!InterfaceInfo) { |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 83 | // First reference to Interface, register it now. |
| 84 | registerPass(Registeree); |
| 85 | InterfaceInfo = &Registeree; |
| 86 | } |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 87 | assert(Registeree.isAnalysisGroup() && |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 88 | "Trying to join an analysis group that is a normal pass!"); |
| 89 | |
| 90 | if (PassID) { |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 91 | PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID)); |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 92 | assert(ImplementationInfo && |
| 93 | "Must register pass before adding to AnalysisGroup!"); |
| 94 | |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 95 | sys::SmartScopedWriter<true> Guard(Lock); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 96 | |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 97 | // Make sure we keep track of the fact that the implementation implements |
| 98 | // the interface. |
| 99 | ImplementationInfo->addInterfaceImplemented(InterfaceInfo); |
| 100 | |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 101 | if (isDefault) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 102 | assert(InterfaceInfo->getNormalCtor() == nullptr && |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 103 | "Default implementation for analysis group already specified!"); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 104 | assert( |
| 105 | ImplementationInfo->getNormalCtor() && |
| 106 | "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] | 107 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
Quentin Colombet | dc0b2ea | 2014-01-16 21:44:34 +0000 | [diff] [blame] | 108 | InterfaceInfo->setTargetMachineCtor( |
| 109 | ImplementationInfo->getTargetMachineCtor()); |
Owen Anderson | 845b14e | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 110 | } |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 111 | } |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 112 | |
David Blaikie | 0afad5e | 2014-04-15 15:17:14 +0000 | [diff] [blame] | 113 | if (ShouldFree) |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 114 | ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree)); |
Owen Anderson | 660466e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 115 | } |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 116 | |
| 117 | void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 118 | sys::SmartScopedWriter<true> Guard(Lock); |
| 119 | Listeners.push_back(L); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 123 | sys::SmartScopedWriter<true> Guard(Lock); |
Chandler Carruth | c34cfb9 | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 124 | |
David Majnemer | 0d955d0 | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 125 | auto I = find(Listeners, L); |
Zachary Turner | 0921f6b | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 126 | Listeners.erase(I); |
Owen Anderson | 7fc9fe7 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 127 | } |