Owen Anderson | ee9886e | 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 | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | 9797e4a | 2017-09-06 23:05:38 +0000 | [diff] [blame] | 17 | #include "llvm/PassInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/PassSupport.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | 9797e4a | 2017-09-06 23:05:38 +0000 | [diff] [blame] | 20 | #include <cassert> |
| 21 | #include <memory> |
| 22 | #include <utility> |
Owen Anderson | aac07ea | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 23 | |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Owen Anderson | 381f17e | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 26 | // FIXME: We use ManagedStatic to erase the pass registrar on shutdown. |
Owen Anderson | aac07ea | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 27 | // Unfortunately, passes are registered with static ctors, and having |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 28 | // llvm_shutdown clear this map prevents successful resurrection after |
Owen Anderson | aac07ea | 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 | 381f17e | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 31 | static ManagedStatic<PassRegistry> PassRegistryObj; |
| 32 | PassRegistry *PassRegistry::getPassRegistry() { |
| 33 | return &*PassRegistryObj; |
Owen Anderson | aac07ea | 2010-07-20 21:22:24 +0000 | [diff] [blame] | 34 | } |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 35 | |
Owen Anderson | 6bcd3a026 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
| 37 | // Accessors |
| 38 | // |
| 39 | |
Eugene Zelenko | 9797e4a | 2017-09-06 23:05:38 +0000 | [diff] [blame] | 40 | PassRegistry::~PassRegistry() = default; |
Owen Anderson | 381f17e | 2010-09-07 20:48:10 +0000 | [diff] [blame] | 41 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 42 | const PassInfo *PassRegistry::getPassInfo(const void *TI) const { |
Erik Eckstein | 8b4cc5e | 2015-03-05 17:53:00 +0000 | [diff] [blame] | 43 | sys::SmartScopedReader<true> Guard(Lock); |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 44 | MapType::const_iterator I = PassInfoMap.find(TI); |
| 45 | return I != PassInfoMap.end() ? I->second : nullptr; |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { |
Erik Eckstein | 8b4cc5e | 2015-03-05 17:53:00 +0000 | [diff] [blame] | 49 | sys::SmartScopedReader<true> Guard(Lock); |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 50 | StringMapType::const_iterator I = PassInfoStringMap.find(Arg); |
| 51 | return I != PassInfoStringMap.end() ? I->second : nullptr; |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | // Pass Registration mechanism |
| 56 | // |
| 57 | |
Owen Anderson | 75f6df2 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 58 | void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 59 | sys::SmartScopedWriter<true> Guard(Lock); |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 60 | bool Inserted = |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 61 | PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second; |
Jakob Stoklund Olesen | 7008f1e | 2011-01-05 21:50:21 +0000 | [diff] [blame] | 62 | assert(Inserted && "Pass registered multiple times!"); |
| 63 | (void)Inserted; |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 64 | PassInfoStringMap[PI.getPassArgument()] = &PI; |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 65 | |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 66 | // Notify any listeners. |
Chandler Carruth | 24a1934 | 2014-10-06 00:06:48 +0000 | [diff] [blame] | 67 | for (auto *Listener : Listeners) |
| 68 | Listener->passRegistered(&PI); |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 69 | |
| 70 | if (ShouldFree) |
| 71 | ToFree.push_back(std::unique_ptr<const PassInfo>(&PI)); |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 74 | void PassRegistry::enumerateWith(PassRegistrationListener *L) { |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 75 | sys::SmartScopedReader<true> Guard(Lock); |
Chandler Carruth | 24a1934 | 2014-10-06 00:06:48 +0000 | [diff] [blame] | 76 | for (auto PassInfoPair : PassInfoMap) |
| 77 | L->passEnumerate(PassInfoPair.second); |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 80 | /// Analysis Group Mechanisms. |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 81 | void PassRegistry::registerAnalysisGroup(const void *InterfaceID, |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 82 | const void *PassID, |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 83 | PassInfo &Registeree, bool isDefault, |
Owen Anderson | 75f6df2 | 2010-10-20 22:22:30 +0000 | [diff] [blame] | 84 | bool ShouldFree) { |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 85 | PassInfo *InterfaceInfo = const_cast<PassInfo *>(getPassInfo(InterfaceID)); |
Craig Topper | ec0f0bc | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 86 | if (!InterfaceInfo) { |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 87 | // First reference to Interface, register it now. |
| 88 | registerPass(Registeree); |
| 89 | InterfaceInfo = &Registeree; |
| 90 | } |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 91 | assert(Registeree.isAnalysisGroup() && |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 92 | "Trying to join an analysis group that is a normal pass!"); |
| 93 | |
| 94 | if (PassID) { |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 95 | PassInfo *ImplementationInfo = const_cast<PassInfo *>(getPassInfo(PassID)); |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 96 | assert(ImplementationInfo && |
| 97 | "Must register pass before adding to AnalysisGroup!"); |
| 98 | |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 99 | sys::SmartScopedWriter<true> Guard(Lock); |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 100 | |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 101 | // Make sure we keep track of the fact that the implementation implements |
| 102 | // the interface. |
| 103 | ImplementationInfo->addInterfaceImplemented(InterfaceInfo); |
| 104 | |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 105 | if (isDefault) { |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 106 | assert(InterfaceInfo->getNormalCtor() == nullptr && |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 107 | "Default implementation for analysis group already specified!"); |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 108 | assert( |
| 109 | ImplementationInfo->getNormalCtor() && |
| 110 | "Cannot specify pass as default if it does not have a default ctor"); |
Owen Anderson | 9650983 | 2010-07-21 17:52:45 +0000 | [diff] [blame] | 111 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 112 | } |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 113 | } |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 114 | |
David Blaikie | 3549f3c | 2014-04-15 15:17:14 +0000 | [diff] [blame] | 115 | if (ShouldFree) |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 116 | ToFree.push_back(std::unique_ptr<const PassInfo>(&Registeree)); |
Owen Anderson | ee9886e | 2010-07-20 19:23:55 +0000 | [diff] [blame] | 117 | } |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 118 | |
| 119 | void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 120 | sys::SmartScopedWriter<true> Guard(Lock); |
| 121 | Listeners.push_back(L); |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 125 | sys::SmartScopedWriter<true> Guard(Lock); |
Chandler Carruth | b3d7f9c | 2014-10-05 23:59:03 +0000 | [diff] [blame] | 126 | |
Eugene Zelenko | 9797e4a | 2017-09-06 23:05:38 +0000 | [diff] [blame] | 127 | auto I = llvm::find(Listeners, L); |
Zachary Turner | 070d532 | 2014-06-12 16:06:51 +0000 | [diff] [blame] | 128 | Listeners.erase(I); |
Owen Anderson | 5396735 | 2010-07-20 23:41:56 +0000 | [diff] [blame] | 129 | } |