blob: 39da8fbe87ac7cbabad43dc480b9efd4fc7ec042 [file] [log] [blame]
Chris Lattnerab16a652003-10-13 05:33:01 +00001//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner26e4f892002-01-21 07:37:31 +00009//
10// This file implements the LLVM Pass infrastructure. It is primarily
11// responsible with ensuring that passes are executed and batched together
12// optimally.
13//
14//===----------------------------------------------------------------------===//
15
Dan Gohman643b3a02008-05-23 20:40:06 +000016#include "llvm/Pass.h"
Chris Lattnercdd09c22002-01-31 00:45:31 +000017#include "llvm/PassManager.h"
18#include "llvm/Module.h"
Misha Brukman56a86422003-10-14 21:38:42 +000019#include "llvm/ModuleProvider.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
Devang Patel1155fdf2009-10-22 19:36:54 +000021#include "llvm/ADT/StringMap.h"
David Greenedfe4ad72010-01-05 01:30:04 +000022#include "llvm/Support/Debug.h"
Chris Lattner1b368a02006-12-01 23:27:45 +000023#include "llvm/Support/ManagedStatic.h"
Chris Lattner13626022009-08-23 06:03:38 +000024#include "llvm/Support/raw_ostream.h"
Owen Andersoneae97722009-06-17 21:16:20 +000025#include "llvm/System/Atomic.h"
Owen Andersonecdab542009-06-24 00:25:42 +000026#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000027#include "llvm/System/Threading.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000028#include <algorithm>
Dan Gohman99885692008-03-21 23:51:57 +000029#include <map>
Chris Lattner6e041bd2002-08-21 22:17:09 +000030#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Chris Lattner7e0dbe62002-05-06 19:31:52 +000033//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000034// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000035//
Chris Lattnercdd09c22002-01-31 00:45:31 +000036
Devang Patelf5a994e2006-12-22 22:49:00 +000037// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000038Pass::~Pass() {
39 delete Resolver;
40}
41
42// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000043ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000044
Dan Gohman1cfbfc82009-12-14 19:43:09 +000045PassManagerType ModulePass::getPotentialPassManagerType() const {
46 return PMT_ModulePassManager;
47}
48
Chris Lattner9ad77572003-03-21 21:41:02 +000049bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Duncan Sands5a913d62009-01-28 13:14:17 +000050 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000051}
52
Chris Lattner198cf422002-07-30 16:27:02 +000053// dumpPassStructure - Implement the -debug-passes=Structure option
54void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000055 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000056}
Chris Lattner37104aa2002-04-29 14:57:45 +000057
Dan Gohman94f57c52008-03-14 18:27:04 +000058/// getPassName - Return a nice clean name for a pass. This usually
59/// implemented in terms of the name that is registered by one of the
60/// Registration templates, but can be overloaded directly.
61///
Chris Lattner071577d2002-07-29 21:02:31 +000062const char *Pass::getPassName() const {
63 if (const PassInfo *PI = getPassInfo())
64 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000065 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000066}
Chris Lattner37104aa2002-04-29 14:57:45 +000067
Dan Gohman1cfbfc82009-12-14 19:43:09 +000068void Pass::preparePassManager(PMStack &) {
69 // By default, don't do anything.
70}
71
72PassManagerType Pass::getPotentialPassManagerType() const {
73 // Default implementation.
74 return PMT_Unknown;
75}
76
77void Pass::getAnalysisUsage(AnalysisUsage &) const {
78 // By default, no analysis results are used, all are invalidated.
79}
80
81void Pass::releaseMemory() {
82 // By default, don't do anything.
83}
84
85void Pass::verifyAnalysis() const {
86 // By default, don't do anything.
87}
88
Misha Brukman56a86422003-10-14 21:38:42 +000089// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000090// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000091// implement this method.
92//
Chris Lattner13626022009-08-23 06:03:38 +000093void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000094 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
95}
96
Bill Wendling22e978a2006-12-07 20:04:42 +000097// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000098void Pass::dump() const {
David Greenedfe4ad72010-01-05 01:30:04 +000099 print(dbgs(), 0);
Chris Lattner26750072002-07-27 01:12:17 +0000100}
101
Chris Lattnercdd09c22002-01-31 00:45:31 +0000102//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000103// ImmutablePass Implementation
104//
Devang Patelf5a994e2006-12-22 22:49:00 +0000105// Force out-of-line virtual method.
106ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000107
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000108void ImmutablePass::initializePass() {
109 // By default, don't do anything.
110}
111
Chris Lattneree0788d2002-09-25 21:59:11 +0000112//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000113// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000114//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000115
Chris Lattnerc8e66542002-04-27 06:56:12 +0000116// run - On a module, we run this pass by initializing, runOnFunction'ing once
117// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000118//
Chris Lattner79e523d2004-09-20 04:47:19 +0000119bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000120 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000121
Chris Lattner113f4f42002-06-25 16:13:24 +0000122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000123 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000124 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000125
Chris Lattnercdd09c22002-01-31 00:45:31 +0000126 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000127}
128
Chris Lattnerc8e66542002-04-27 06:56:12 +0000129// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130//
Chris Lattner113f4f42002-06-25 16:13:24 +0000131bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +0000132 // Passes are not run on external functions!
133 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000134
Chris Lattner79e523d2004-09-20 04:47:19 +0000135 bool Changed = doInitialization(*F.getParent());
136 Changed |= runOnFunction(F);
137 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000138}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000139
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000140bool FunctionPass::doInitialization(Module &) {
141 // By default, don't do anything.
142 return false;
143}
144
145bool FunctionPass::doFinalization(Module &) {
146 // By default, don't do anything.
147 return false;
148}
149
150PassManagerType FunctionPass::getPotentialPassManagerType() const {
151 return PMT_FunctionPassManager;
152}
153
Chris Lattnercdd09c22002-01-31 00:45:31 +0000154//===----------------------------------------------------------------------===//
155// BasicBlockPass Implementation
156//
157
Chris Lattnerc8e66542002-04-27 06:56:12 +0000158// To run this pass on a function, we simply call runOnBasicBlock once for each
159// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000160//
Chris Lattner113f4f42002-06-25 16:13:24 +0000161bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000162 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000163 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000164 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000165 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000166}
167
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000168bool BasicBlockPass::doInitialization(Module &) {
169 // By default, don't do anything.
170 return false;
171}
172
173bool BasicBlockPass::doInitialization(Function &) {
174 // By default, don't do anything.
175 return false;
176}
177
178bool BasicBlockPass::doFinalization(Function &) {
179 // By default, don't do anything.
180 return false;
181}
182
183bool BasicBlockPass::doFinalization(Module &) {
184 // By default, don't do anything.
185 return false;
186}
187
188PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
189 return PMT_BasicBlockPassManager;
190}
191
Chris Lattner37d3c952002-07-23 18:08:00 +0000192//===----------------------------------------------------------------------===//
193// Pass Registration mechanism
194//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000195namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000196class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000197 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
198 /// pass.
Dan Gohman0479aa52008-05-13 02:05:11 +0000199 typedef std::map<intptr_t, const PassInfo*> MapType;
200 MapType PassInfoMap;
Dan Gohman09984272009-10-08 17:00:02 +0000201
202 typedef StringMap<const PassInfo*> StringMapType;
203 StringMapType PassInfoStringMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000204
205 /// AnalysisGroupInfo - Keep track of information for each analysis group.
206 struct AnalysisGroupInfo {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000207 std::set<const PassInfo *> Implementations;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000208 };
209
210 /// AnalysisGroupInfoMap - Information for each analysis group.
211 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
212
Chris Lattner1b368a02006-12-01 23:27:45 +0000213public:
214
Devang Patel0f88f762007-05-02 20:38:25 +0000215 const PassInfo *GetPassInfo(intptr_t TI) const {
Dan Gohman0479aa52008-05-13 02:05:11 +0000216 MapType::const_iterator I = PassInfoMap.find(TI);
Chris Lattner1b368a02006-12-01 23:27:45 +0000217 return I != PassInfoMap.end() ? I->second : 0;
218 }
219
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000220 const PassInfo *GetPassInfo(StringRef Arg) const {
Dan Gohman09984272009-10-08 17:00:02 +0000221 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
222 return I != PassInfoStringMap.end() ? I->second : 0;
223 }
224
Dan Gohman0479aa52008-05-13 02:05:11 +0000225 void RegisterPass(const PassInfo &PI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000226 bool Inserted =
Devang Patel0f88f762007-05-02 20:38:25 +0000227 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
Chris Lattner106b0462008-06-21 19:47:03 +0000228 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
Dan Gohman09984272009-10-08 17:00:02 +0000229 PassInfoStringMap[PI.getPassArgument()] = &PI;
Chris Lattner1b368a02006-12-01 23:27:45 +0000230 }
231
Dan Gohman0479aa52008-05-13 02:05:11 +0000232 void UnregisterPass(const PassInfo &PI) {
233 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000234 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
235
236 // Remove pass from the map.
237 PassInfoMap.erase(I);
Dan Gohman09984272009-10-08 17:00:02 +0000238 PassInfoStringMap.erase(PI.getPassArgument());
Chris Lattner1b368a02006-12-01 23:27:45 +0000239 }
240
241 void EnumerateWith(PassRegistrationListener *L) {
Dan Gohman0479aa52008-05-13 02:05:11 +0000242 for (MapType::const_iterator I = PassInfoMap.begin(),
Chris Lattner1b368a02006-12-01 23:27:45 +0000243 E = PassInfoMap.end(); I != E; ++I)
244 L->passEnumerate(I->second);
245 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000246
247
248 /// Analysis Group Mechanisms.
249 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
250 const PassInfo *ImplementationInfo,
251 bool isDefault) {
252 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
253 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
254 "Cannot add a pass to the same analysis group more than once!");
255 AGI.Implementations.insert(ImplementationInfo);
256 if (isDefault) {
Dan Gohman07d0a552009-08-29 23:34:14 +0000257 assert(InterfaceInfo->getNormalCtor() == 0 &&
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000258 "Default implementation for analysis group already specified!");
259 assert(ImplementationInfo->getNormalCtor() &&
260 "Cannot specify pass as default if it does not have a default ctor");
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000261 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
262 }
263 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000264};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000265}
Chris Lattner1b368a02006-12-01 23:27:45 +0000266
Chris Lattner37d3c952002-07-23 18:08:00 +0000267static std::vector<PassRegistrationListener*> *Listeners = 0;
Owen Andersonecdab542009-06-24 00:25:42 +0000268static sys::SmartMutex<true> ListenersLock;
Chris Lattner37d3c952002-07-23 18:08:00 +0000269
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000270// FIXME: This should use ManagedStatic to manage the pass registrar.
271// Unfortunately, we can't do this, because passes are registered with static
272// ctors, and having llvm_shutdown clear this map prevents successful
273// ressurection after llvm_shutdown is run.
274static PassRegistrar *getPassRegistrar() {
275 static PassRegistrar *PassRegistrarObj = 0;
Owen Andersoneae97722009-06-17 21:16:20 +0000276
277 // Use double-checked locking to safely initialize the registrar when
278 // we're running in multithreaded mode.
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000279 PassRegistrar* tmp = PassRegistrarObj;
Owen Anderson81241a32009-06-19 17:45:12 +0000280 if (llvm_is_multithreaded()) {
281 sys::MemoryFence();
282 if (!tmp) {
Owen Andersoneae97722009-06-17 21:16:20 +0000283 llvm_acquire_global_lock();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000284 tmp = PassRegistrarObj;
285 if (!tmp) {
286 tmp = new PassRegistrar();
Owen Andersoneae97722009-06-17 21:16:20 +0000287 sys::MemoryFence();
288 PassRegistrarObj = tmp;
289 }
290 llvm_release_global_lock();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000291 }
Owen Anderson81241a32009-06-19 17:45:12 +0000292 } else if (!tmp) {
293 PassRegistrarObj = new PassRegistrar();
Nick Lewycky87c4a052009-06-18 03:01:42 +0000294 }
Owen Anderson81241a32009-06-19 17:45:12 +0000295
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000296 return PassRegistrarObj;
297}
298
Chris Lattner37d3c952002-07-23 18:08:00 +0000299// getPassInfo - Return the PassInfo data structure that corresponds to this
300// pass...
301const PassInfo *Pass::getPassInfo() const {
Devang Patel0f88f762007-05-02 20:38:25 +0000302 return lookupPassInfo(PassID);
Chris Lattner4b169632002-08-21 17:08:37 +0000303}
304
Devang Patel0f88f762007-05-02 20:38:25 +0000305const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000306 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000307}
308
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000309const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Dan Gohman09984272009-10-08 17:00:02 +0000310 return getPassRegistrar()->GetPassInfo(Arg);
311}
312
Dan Gohman0479aa52008-05-13 02:05:11 +0000313void PassInfo::registerPass() {
314 getPassRegistrar()->RegisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000315
Chris Lattner1b368a02006-12-01 23:27:45 +0000316 // Notify any listeners.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000317 sys::SmartScopedLock<true> Lock(ListenersLock);
Chris Lattner37d3c952002-07-23 18:08:00 +0000318 if (Listeners)
319 for (std::vector<PassRegistrationListener*>::iterator
320 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Dan Gohman0479aa52008-05-13 02:05:11 +0000321 (*I)->passRegistered(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000322}
323
Dan Gohman0479aa52008-05-13 02:05:11 +0000324void PassInfo::unregisterPass() {
325 getPassRegistrar()->UnregisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000326}
327
Chris Lattner6e041bd2002-08-21 22:17:09 +0000328//===----------------------------------------------------------------------===//
329// Analysis Group Implementation Code
330//===----------------------------------------------------------------------===//
331
Chris Lattner6e041bd2002-08-21 22:17:09 +0000332// RegisterAGBase implementation
333//
Dan Gohman0479aa52008-05-13 02:05:11 +0000334RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
Devang Patel0f88f762007-05-02 20:38:25 +0000335 intptr_t PassID, bool isDefault)
Dan Gohman07d0a552009-08-29 23:34:14 +0000336 : PassInfo(Name, InterfaceID) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000337
Dan Gohman07d0a552009-08-29 23:34:14 +0000338 PassInfo *InterfaceInfo =
339 const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
Chris Lattnerc66da832006-01-23 01:01:04 +0000340 if (InterfaceInfo == 0) {
341 // First reference to Interface, register it now.
342 registerPass();
Dan Gohman0479aa52008-05-13 02:05:11 +0000343 InterfaceInfo = this;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000344 }
Dan Gohman0479aa52008-05-13 02:05:11 +0000345 assert(isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000346 "Trying to join an analysis group that is a normal pass!");
347
Devang Patel0f88f762007-05-02 20:38:25 +0000348 if (PassID) {
Dan Gohman07d0a552009-08-29 23:34:14 +0000349 const PassInfo *ImplementationInfo = Pass::lookupPassInfo(PassID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000350 assert(ImplementationInfo &&
351 "Must register pass before adding to AnalysisGroup!");
352
Chris Lattnerb3708e22002-08-30 20:23:45 +0000353 // Make sure we keep track of the fact that the implementation implements
354 // the interface.
355 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
356 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000357
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000358 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000359 }
360}
361
Chris Lattner6e041bd2002-08-21 22:17:09 +0000362
Chris Lattner37d3c952002-07-23 18:08:00 +0000363//===----------------------------------------------------------------------===//
364// PassRegistrationListener implementation
365//
366
367// PassRegistrationListener ctor - Add the current object to the list of
368// PassRegistrationListeners...
369PassRegistrationListener::PassRegistrationListener() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000370 sys::SmartScopedLock<true> Lock(ListenersLock);
Chris Lattner37d3c952002-07-23 18:08:00 +0000371 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
372 Listeners->push_back(this);
373}
374
375// dtor - Remove object from list of listeners...
376PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000377 sys::SmartScopedLock<true> Lock(ListenersLock);
Chris Lattner37d3c952002-07-23 18:08:00 +0000378 std::vector<PassRegistrationListener*>::iterator I =
379 std::find(Listeners->begin(), Listeners->end(), this);
380 assert(Listeners && I != Listeners->end() &&
381 "PassRegistrationListener not registered!");
382 Listeners->erase(I);
383
384 if (Listeners->empty()) {
385 delete Listeners;
386 Listeners = 0;
387 }
388}
389
390// enumeratePasses - Iterate over the registered passes, calling the
391// passEnumerate callback on each PassInfo object.
392//
393void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000394 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000395}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000396
Chris Lattner23d54052006-12-01 22:21:11 +0000397//===----------------------------------------------------------------------===//
398// AnalysisUsage Class Implementation
399//
400
Chris Lattner1b368a02006-12-01 23:27:45 +0000401namespace {
402 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000403 typedef AnalysisUsage::VectorType VectorType;
404 VectorType &CFGOnlyList;
405 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000406
407 void passEnumerate(const PassInfo *P) {
408 if (P->isCFGOnlyPass())
409 CFGOnlyList.push_back(P);
410 }
411 };
412}
413
Chris Lattner23d54052006-12-01 22:21:11 +0000414// setPreservesCFG - This function should be called to by the pass, iff they do
415// not:
416//
417// 1. Add or remove basic blocks from the function
418// 2. Modify terminator instructions in any way.
419//
420// This function annotates the AnalysisUsage info object to say that analyses
421// that only depend on the CFG are preserved by this pass.
422//
423void AnalysisUsage::setPreservesCFG() {
424 // Since this transformation doesn't modify the CFG, it preserves all analyses
425 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000426 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000427}
428
429