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