blob: 21dba56aad728725c2558fda00eb1098840d9e96 [file] [log] [blame]
Owen Anderson660466e2010-07-20 19:23:55 +00001//===- 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 Anderson7fc9fe72010-07-20 23:41:56 +000016#include "llvm/PassSupport.h"
Owen Anderson41540612010-07-20 21:22:24 +000017#include "llvm/Support/Compiler.h"
18#include "llvm/Support/ManagedStatic.h"
19
Owen Anderson7fc9fe72010-07-20 23:41:56 +000020using namespace llvm;
21
Owen Anderson41540612010-07-20 21:22:24 +000022static PassRegistry *PassRegistryObj = 0;
23PassRegistry *PassRegistry::getPassRegistry() {
24 // Use double-checked locking to safely initialize the registrar when
25 // we're running in multithreaded mode.
26 PassRegistry* tmp = PassRegistryObj;
27 if (llvm_is_multithreaded()) {
28 sys::MemoryFence();
29 if (!tmp) {
30 llvm_acquire_global_lock();
31 tmp = PassRegistryObj;
32 if (!tmp) {
33 tmp = new PassRegistry();
34 sys::MemoryFence();
35 PassRegistryObj = tmp;
36 }
37 llvm_release_global_lock();
38 }
39 } else if (!tmp) {
40 PassRegistryObj = new PassRegistry();
41 }
42
43 return PassRegistryObj;
44}
45
46namespace {
47
48// FIXME: We use ManagedCleanup to erase the pass registrar on shutdown.
49// Unfortunately, passes are registered with static ctors, and having
50// llvm_shutdown clear this map prevents successful ressurection after
51// llvm_shutdown is run. Ideally we should find a solution so that we don't
52// leak the map, AND can still resurrect after shutdown.
53void cleanupPassRegistry(void*) {
54 if (PassRegistryObj) {
55 delete PassRegistryObj;
56 PassRegistryObj = 0;
57 }
58}
59ManagedCleanup<&cleanupPassRegistry> registryCleanup ATTRIBUTE_USED;
60
61}
Owen Anderson660466e2010-07-20 19:23:55 +000062
Owen Andersona7aed182010-08-06 18:33:48 +000063const PassInfo *PassRegistry::getPassInfo(const void *TI) const {
Owen Anderson660466e2010-07-20 19:23:55 +000064 sys::SmartScopedLock<true> Guard(Lock);
65 MapType::const_iterator I = PassInfoMap.find(TI);
66 return I != PassInfoMap.end() ? I->second : 0;
67}
68
69const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
70 sys::SmartScopedLock<true> Guard(Lock);
71 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
72 return I != PassInfoStringMap.end() ? I->second : 0;
73}
74
Owen Anderson7fc9fe72010-07-20 23:41:56 +000075//===----------------------------------------------------------------------===//
76// Pass Registration mechanism
77//
78
Owen Anderson660466e2010-07-20 19:23:55 +000079void PassRegistry::registerPass(const PassInfo &PI) {
80 sys::SmartScopedLock<true> Guard(Lock);
81 bool Inserted =
82 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
83 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
84 PassInfoStringMap[PI.getPassArgument()] = &PI;
Owen Anderson7fc9fe72010-07-20 23:41:56 +000085
86 // Notify any listeners.
87 for (std::vector<PassRegistrationListener*>::iterator
88 I = Listeners.begin(), E = Listeners.end(); I != E; ++I)
89 (*I)->passRegistered(&PI);
Owen Anderson660466e2010-07-20 19:23:55 +000090}
91
92void PassRegistry::unregisterPass(const PassInfo &PI) {
93 sys::SmartScopedLock<true> Guard(Lock);
94 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
95 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
96
97 // Remove pass from the map.
98 PassInfoMap.erase(I);
99 PassInfoStringMap.erase(PI.getPassArgument());
100}
101
102void PassRegistry::enumerateWith(PassRegistrationListener *L) {
103 sys::SmartScopedLock<true> Guard(Lock);
104 for (MapType::const_iterator I = PassInfoMap.begin(),
105 E = PassInfoMap.end(); I != E; ++I)
106 L->passEnumerate(I->second);
107}
108
109
110/// Analysis Group Mechanisms.
Owen Andersona7aed182010-08-06 18:33:48 +0000111void PassRegistry::registerAnalysisGroup(const void *InterfaceID,
112 const void *PassID,
Owen Anderson845b14e2010-07-21 17:52:45 +0000113 PassInfo& Registeree,
Owen Anderson660466e2010-07-20 19:23:55 +0000114 bool isDefault) {
Owen Anderson845b14e2010-07-21 17:52:45 +0000115 PassInfo *InterfaceInfo = const_cast<PassInfo*>(getPassInfo(InterfaceID));
116 if (InterfaceInfo == 0) {
117 // First reference to Interface, register it now.
118 registerPass(Registeree);
119 InterfaceInfo = &Registeree;
120 }
121 assert(Registeree.isAnalysisGroup() &&
122 "Trying to join an analysis group that is a normal pass!");
123
124 if (PassID) {
125 PassInfo *ImplementationInfo = const_cast<PassInfo*>(getPassInfo(PassID));
126 assert(ImplementationInfo &&
127 "Must register pass before adding to AnalysisGroup!");
128
129 // Make sure we keep track of the fact that the implementation implements
130 // the interface.
131 ImplementationInfo->addInterfaceImplemented(InterfaceInfo);
132
133 sys::SmartScopedLock<true> Guard(Lock);
134 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
135 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
136 "Cannot add a pass to the same analysis group more than once!");
137 AGI.Implementations.insert(ImplementationInfo);
138 if (isDefault) {
139 assert(InterfaceInfo->getNormalCtor() == 0 &&
140 "Default implementation for analysis group already specified!");
141 assert(ImplementationInfo->getNormalCtor() &&
142 "Cannot specify pass as default if it does not have a default ctor");
143 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
144 }
Owen Anderson660466e2010-07-20 19:23:55 +0000145 }
146}
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000147
148void PassRegistry::addRegistrationListener(PassRegistrationListener *L) {
149 sys::SmartScopedLock<true> Guard(Lock);
150 Listeners.push_back(L);
151}
152
153void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) {
154 sys::SmartScopedLock<true> Guard(Lock);
155 std::vector<PassRegistrationListener*>::iterator I =
156 std::find(Listeners.begin(), Listeners.end(), L);
157 assert(I != Listeners.end() && "PassRegistrationListener not registered!");
158 Listeners.erase(I);
159}