blob: efd98af0f443ce4acfc56b53af0c5f3e0f4ff953 [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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/STLExtras.h"
Devang Patel1155fdf2009-10-22 19:36:54 +000020#include "llvm/ADT/StringMap.h"
David Greene9b063df2010-04-02 23:17:14 +000021#include "llvm/Assembly/PrintModulePass.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 Lattner5264fce2010-01-22 06:29:25 +000024#include "llvm/Support/PassNameParser.h"
Chris Lattner13626022009-08-23 06:03:38 +000025#include "llvm/Support/raw_ostream.h"
Owen Andersoneae97722009-06-17 21:16:20 +000026#include "llvm/System/Atomic.h"
Owen Andersonecdab542009-06-24 00:25:42 +000027#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000028#include "llvm/System/Threading.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000029#include <algorithm>
Dan Gohman99885692008-03-21 23:51:57 +000030#include <map>
Chris Lattner6e041bd2002-08-21 22:17:09 +000031#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner7e0dbe62002-05-06 19:31:52 +000034//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000035// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000036//
Chris Lattnercdd09c22002-01-31 00:45:31 +000037
Dan Gohmanffdee302010-06-21 18:46:45 +000038Pass::Pass(PassKind K, intptr_t pid) : Resolver(0), PassID(pid), Kind(K) {
39 assert(pid && "pid cannot be 0");
40}
41
42Pass::Pass(PassKind K, const void *pid)
43 : Resolver(0), PassID((intptr_t)pid), Kind(K) {
44 assert(pid && "pid cannot be 0");
45}
46
Devang Patelf5a994e2006-12-22 22:49:00 +000047// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000048Pass::~Pass() {
49 delete Resolver;
50}
51
52// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000053ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000054
David Greene9b063df2010-04-02 23:17:14 +000055Pass *ModulePass::createPrinterPass(raw_ostream &O,
56 const std::string &Banner) const {
57 return createPrintModulePass(&O, false, Banner);
58}
59
Dan Gohman1cfbfc82009-12-14 19:43:09 +000060PassManagerType ModulePass::getPotentialPassManagerType() const {
61 return PMT_ModulePassManager;
62}
63
Chris Lattner9ad77572003-03-21 21:41:02 +000064bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Duncan Sands5a913d62009-01-28 13:14:17 +000065 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000066}
67
Chris Lattner198cf422002-07-30 16:27:02 +000068// dumpPassStructure - Implement the -debug-passes=Structure option
69void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000070 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000071}
Chris Lattner37104aa2002-04-29 14:57:45 +000072
Dan Gohman94f57c52008-03-14 18:27:04 +000073/// getPassName - Return a nice clean name for a pass. This usually
74/// implemented in terms of the name that is registered by one of the
75/// Registration templates, but can be overloaded directly.
76///
Chris Lattner071577d2002-07-29 21:02:31 +000077const char *Pass::getPassName() const {
78 if (const PassInfo *PI = getPassInfo())
79 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000080 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000081}
Chris Lattner37104aa2002-04-29 14:57:45 +000082
Dan Gohman1cfbfc82009-12-14 19:43:09 +000083void Pass::preparePassManager(PMStack &) {
84 // By default, don't do anything.
85}
86
87PassManagerType Pass::getPotentialPassManagerType() const {
88 // Default implementation.
89 return PMT_Unknown;
90}
91
92void Pass::getAnalysisUsage(AnalysisUsage &) const {
93 // By default, no analysis results are used, all are invalidated.
94}
95
96void Pass::releaseMemory() {
97 // By default, don't do anything.
98}
99
100void Pass::verifyAnalysis() const {
101 // By default, don't do anything.
102}
103
Dan Gohmanffdee302010-06-21 18:46:45 +0000104void *Pass::getAdjustedAnalysisPointer(const PassInfo *) {
105 return this;
106}
107
108ImmutablePass *Pass::getAsImmutablePass() {
109 return 0;
110}
111
112PMDataManager *Pass::getAsPMDataManager() {
113 return 0;
114}
115
116void Pass::setResolver(AnalysisResolver *AR) {
117 assert(!Resolver && "Resolver is already set");
118 Resolver = AR;
119}
120
Misha Brukman56a86422003-10-14 21:38:42 +0000121// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000122// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000123// implement this method.
124//
Chris Lattner13626022009-08-23 06:03:38 +0000125void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000126 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
127}
128
Bill Wendling22e978a2006-12-07 20:04:42 +0000129// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000130void Pass::dump() const {
David Greenedfe4ad72010-01-05 01:30:04 +0000131 print(dbgs(), 0);
Chris Lattner26750072002-07-27 01:12:17 +0000132}
133
Chris Lattnercdd09c22002-01-31 00:45:31 +0000134//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000135// ImmutablePass Implementation
136//
Devang Patelf5a994e2006-12-22 22:49:00 +0000137// Force out-of-line virtual method.
138ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000139
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000140void ImmutablePass::initializePass() {
141 // By default, don't do anything.
142}
143
Chris Lattneree0788d2002-09-25 21:59:11 +0000144//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000145// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000146//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000147
David Greene9b063df2010-04-02 23:17:14 +0000148Pass *FunctionPass::createPrinterPass(raw_ostream &O,
149 const std::string &Banner) const {
150 return createPrintFunctionPass(Banner, &O);
151}
152
Chris Lattnerc8e66542002-04-27 06:56:12 +0000153// run - On a module, we run this pass by initializing, runOnFunction'ing once
154// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000155//
Chris Lattner79e523d2004-09-20 04:47:19 +0000156bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000157 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158
Chris Lattner113f4f42002-06-25 16:13:24 +0000159 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000160 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000161 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000162
Chris Lattnercdd09c22002-01-31 00:45:31 +0000163 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000164}
165
Chris Lattnerc8e66542002-04-27 06:56:12 +0000166// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000167//
Chris Lattner113f4f42002-06-25 16:13:24 +0000168bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +0000169 // Passes are not run on external functions!
170 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000171
Chris Lattner79e523d2004-09-20 04:47:19 +0000172 bool Changed = doInitialization(*F.getParent());
173 Changed |= runOnFunction(F);
174 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000175}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000176
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000177bool FunctionPass::doInitialization(Module &) {
178 // By default, don't do anything.
179 return false;
180}
181
182bool FunctionPass::doFinalization(Module &) {
183 // By default, don't do anything.
184 return false;
185}
186
187PassManagerType FunctionPass::getPotentialPassManagerType() const {
188 return PMT_FunctionPassManager;
189}
190
Chris Lattnercdd09c22002-01-31 00:45:31 +0000191//===----------------------------------------------------------------------===//
192// BasicBlockPass Implementation
193//
194
David Greene9b063df2010-04-02 23:17:14 +0000195Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
196 const std::string &Banner) const {
197
198 llvm_unreachable("BasicBlockPass printing unsupported.");
199 return 0;
200}
201
Chris Lattnerc8e66542002-04-27 06:56:12 +0000202// To run this pass on a function, we simply call runOnBasicBlock once for each
203// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000204//
Chris Lattner113f4f42002-06-25 16:13:24 +0000205bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000206 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000207 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000208 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000209 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000210}
211
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000212bool BasicBlockPass::doInitialization(Module &) {
213 // By default, don't do anything.
214 return false;
215}
216
217bool BasicBlockPass::doInitialization(Function &) {
218 // By default, don't do anything.
219 return false;
220}
221
222bool BasicBlockPass::doFinalization(Function &) {
223 // By default, don't do anything.
224 return false;
225}
226
227bool BasicBlockPass::doFinalization(Module &) {
228 // By default, don't do anything.
229 return false;
230}
231
232PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
233 return PMT_BasicBlockPassManager;
234}
235
Chris Lattner37d3c952002-07-23 18:08:00 +0000236//===----------------------------------------------------------------------===//
237// Pass Registration mechanism
238//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000239namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000240class PassRegistrar {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000241 /// Guards the contents of this class.
242 mutable sys::SmartMutex<true> Lock;
243
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000244 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
245 /// pass.
Dan Gohman0479aa52008-05-13 02:05:11 +0000246 typedef std::map<intptr_t, const PassInfo*> MapType;
247 MapType PassInfoMap;
Dan Gohman09984272009-10-08 17:00:02 +0000248
249 typedef StringMap<const PassInfo*> StringMapType;
250 StringMapType PassInfoStringMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000251
252 /// AnalysisGroupInfo - Keep track of information for each analysis group.
253 struct AnalysisGroupInfo {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000254 std::set<const PassInfo *> Implementations;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000255 };
256
257 /// AnalysisGroupInfoMap - Information for each analysis group.
258 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
259
Chris Lattner1b368a02006-12-01 23:27:45 +0000260public:
261
Devang Patel0f88f762007-05-02 20:38:25 +0000262 const PassInfo *GetPassInfo(intptr_t TI) const {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000263 sys::SmartScopedLock<true> Guard(Lock);
Dan Gohman0479aa52008-05-13 02:05:11 +0000264 MapType::const_iterator I = PassInfoMap.find(TI);
Chris Lattner1b368a02006-12-01 23:27:45 +0000265 return I != PassInfoMap.end() ? I->second : 0;
266 }
267
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000268 const PassInfo *GetPassInfo(StringRef Arg) const {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000269 sys::SmartScopedLock<true> Guard(Lock);
Dan Gohman09984272009-10-08 17:00:02 +0000270 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
271 return I != PassInfoStringMap.end() ? I->second : 0;
272 }
273
Dan Gohman0479aa52008-05-13 02:05:11 +0000274 void RegisterPass(const PassInfo &PI) {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000275 sys::SmartScopedLock<true> Guard(Lock);
Chris Lattner1b368a02006-12-01 23:27:45 +0000276 bool Inserted =
Devang Patel0f88f762007-05-02 20:38:25 +0000277 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
Chris Lattner106b0462008-06-21 19:47:03 +0000278 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
Dan Gohman09984272009-10-08 17:00:02 +0000279 PassInfoStringMap[PI.getPassArgument()] = &PI;
Chris Lattner1b368a02006-12-01 23:27:45 +0000280 }
281
Dan Gohman0479aa52008-05-13 02:05:11 +0000282 void UnregisterPass(const PassInfo &PI) {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000283 sys::SmartScopedLock<true> Guard(Lock);
Dan Gohman0479aa52008-05-13 02:05:11 +0000284 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000285 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
286
287 // Remove pass from the map.
288 PassInfoMap.erase(I);
Dan Gohman09984272009-10-08 17:00:02 +0000289 PassInfoStringMap.erase(PI.getPassArgument());
Chris Lattner1b368a02006-12-01 23:27:45 +0000290 }
291
292 void EnumerateWith(PassRegistrationListener *L) {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000293 sys::SmartScopedLock<true> Guard(Lock);
Dan Gohman0479aa52008-05-13 02:05:11 +0000294 for (MapType::const_iterator I = PassInfoMap.begin(),
Chris Lattner1b368a02006-12-01 23:27:45 +0000295 E = PassInfoMap.end(); I != E; ++I)
296 L->passEnumerate(I->second);
297 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000298
299
300 /// Analysis Group Mechanisms.
301 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
302 const PassInfo *ImplementationInfo,
303 bool isDefault) {
Jeffrey Yasskin01f88a92010-02-13 00:03:17 +0000304 sys::SmartScopedLock<true> Guard(Lock);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000305 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
306 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
307 "Cannot add a pass to the same analysis group more than once!");
308 AGI.Implementations.insert(ImplementationInfo);
309 if (isDefault) {
Dan Gohman07d0a552009-08-29 23:34:14 +0000310 assert(InterfaceInfo->getNormalCtor() == 0 &&
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000311 "Default implementation for analysis group already specified!");
312 assert(ImplementationInfo->getNormalCtor() &&
313 "Cannot specify pass as default if it does not have a default ctor");
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000314 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
315 }
316 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000317};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000318}
Chris Lattner1b368a02006-12-01 23:27:45 +0000319
Chris Lattner37d3c952002-07-23 18:08:00 +0000320static std::vector<PassRegistrationListener*> *Listeners = 0;
Owen Andersonecdab542009-06-24 00:25:42 +0000321static sys::SmartMutex<true> ListenersLock;
Chris Lattner37d3c952002-07-23 18:08:00 +0000322
Owen Andersonee3570f2010-04-06 04:20:48 +0000323static PassRegistrar *PassRegistrarObj = 0;
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000324static PassRegistrar *getPassRegistrar() {
Owen Andersoneae97722009-06-17 21:16:20 +0000325 // Use double-checked locking to safely initialize the registrar when
326 // we're running in multithreaded mode.
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000327 PassRegistrar* tmp = PassRegistrarObj;
Owen Anderson81241a32009-06-19 17:45:12 +0000328 if (llvm_is_multithreaded()) {
329 sys::MemoryFence();
330 if (!tmp) {
Owen Andersoneae97722009-06-17 21:16:20 +0000331 llvm_acquire_global_lock();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000332 tmp = PassRegistrarObj;
333 if (!tmp) {
334 tmp = new PassRegistrar();
Owen Andersoneae97722009-06-17 21:16:20 +0000335 sys::MemoryFence();
336 PassRegistrarObj = tmp;
337 }
338 llvm_release_global_lock();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000339 }
Owen Anderson81241a32009-06-19 17:45:12 +0000340 } else if (!tmp) {
341 PassRegistrarObj = new PassRegistrar();
Nick Lewycky87c4a052009-06-18 03:01:42 +0000342 }
Owen Anderson81241a32009-06-19 17:45:12 +0000343
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000344 return PassRegistrarObj;
345}
346
Dan Gohman345356e2010-04-15 16:23:27 +0000347namespace {
348
Owen Andersonee3570f2010-04-06 04:20:48 +0000349// FIXME: We use ManagedCleanup to erase the pass registrar on shutdown.
350// Unfortunately, passes are registered with static ctors, and having
351// llvm_shutdown clear this map prevents successful ressurection after
352// llvm_shutdown is run. Ideally we should find a solution so that we don't
353// leak the map, AND can still resurrect after shutdown.
354void cleanupPassRegistrar(void*) {
355 if (PassRegistrarObj) {
356 delete PassRegistrarObj;
357 PassRegistrarObj = 0;
358 }
359}
Eric Christopher206d7ce2010-04-16 04:02:04 +0000360ManagedCleanup<&cleanupPassRegistrar> registrarCleanup ATTRIBUTE_USED;
Owen Andersonee3570f2010-04-06 04:20:48 +0000361
Dan Gohman345356e2010-04-15 16:23:27 +0000362}
363
Chris Lattner37d3c952002-07-23 18:08:00 +0000364// getPassInfo - Return the PassInfo data structure that corresponds to this
365// pass...
366const PassInfo *Pass::getPassInfo() const {
Devang Patel0f88f762007-05-02 20:38:25 +0000367 return lookupPassInfo(PassID);
Chris Lattner4b169632002-08-21 17:08:37 +0000368}
369
Devang Patel0f88f762007-05-02 20:38:25 +0000370const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000371 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000372}
373
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000374const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Dan Gohman09984272009-10-08 17:00:02 +0000375 return getPassRegistrar()->GetPassInfo(Arg);
376}
377
Dan Gohman0479aa52008-05-13 02:05:11 +0000378void PassInfo::registerPass() {
379 getPassRegistrar()->RegisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000380
Chris Lattner1b368a02006-12-01 23:27:45 +0000381 // Notify any listeners.
Owen Anderson5c96ef72009-07-07 18:33:04 +0000382 sys::SmartScopedLock<true> Lock(ListenersLock);
Chris Lattner37d3c952002-07-23 18:08:00 +0000383 if (Listeners)
384 for (std::vector<PassRegistrationListener*>::iterator
385 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Dan Gohman0479aa52008-05-13 02:05:11 +0000386 (*I)->passRegistered(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000387}
388
Dan Gohman0479aa52008-05-13 02:05:11 +0000389void PassInfo::unregisterPass() {
390 getPassRegistrar()->UnregisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000391}
392
Dan Gohmanffdee302010-06-21 18:46:45 +0000393Pass *PassInfo::createPass() const {
394 assert((!isAnalysisGroup() || NormalCtor) &&
395 "No default implementation found for analysis group!");
396 assert(NormalCtor &&
397 "Cannot call createPass on PassInfo without default ctor!");
398 return NormalCtor();
399}
400
Chris Lattner6e041bd2002-08-21 22:17:09 +0000401//===----------------------------------------------------------------------===//
402// Analysis Group Implementation Code
403//===----------------------------------------------------------------------===//
404
Chris Lattner6e041bd2002-08-21 22:17:09 +0000405// RegisterAGBase implementation
406//
Dan Gohman0479aa52008-05-13 02:05:11 +0000407RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
Devang Patel0f88f762007-05-02 20:38:25 +0000408 intptr_t PassID, bool isDefault)
Dan Gohman07d0a552009-08-29 23:34:14 +0000409 : PassInfo(Name, InterfaceID) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000410
Dan Gohman07d0a552009-08-29 23:34:14 +0000411 PassInfo *InterfaceInfo =
412 const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
Chris Lattnerc66da832006-01-23 01:01:04 +0000413 if (InterfaceInfo == 0) {
414 // First reference to Interface, register it now.
415 registerPass();
Dan Gohman0479aa52008-05-13 02:05:11 +0000416 InterfaceInfo = this;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000417 }
Dan Gohman0479aa52008-05-13 02:05:11 +0000418 assert(isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000419 "Trying to join an analysis group that is a normal pass!");
420
Devang Patel0f88f762007-05-02 20:38:25 +0000421 if (PassID) {
Dan Gohman07d0a552009-08-29 23:34:14 +0000422 const PassInfo *ImplementationInfo = Pass::lookupPassInfo(PassID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000423 assert(ImplementationInfo &&
424 "Must register pass before adding to AnalysisGroup!");
425
Chris Lattnerb3708e22002-08-30 20:23:45 +0000426 // Make sure we keep track of the fact that the implementation implements
427 // the interface.
428 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
429 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000430
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000431 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000432 }
433}
434
Chris Lattner6e041bd2002-08-21 22:17:09 +0000435
Chris Lattner37d3c952002-07-23 18:08:00 +0000436//===----------------------------------------------------------------------===//
437// PassRegistrationListener implementation
438//
439
440// PassRegistrationListener ctor - Add the current object to the list of
441// PassRegistrationListeners...
442PassRegistrationListener::PassRegistrationListener() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000443 sys::SmartScopedLock<true> Lock(ListenersLock);
Chris Lattner37d3c952002-07-23 18:08:00 +0000444 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
445 Listeners->push_back(this);
446}
447
448// dtor - Remove object from list of listeners...
449PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000450 sys::SmartScopedLock<true> Lock(ListenersLock);
Chris Lattner37d3c952002-07-23 18:08:00 +0000451 std::vector<PassRegistrationListener*>::iterator I =
452 std::find(Listeners->begin(), Listeners->end(), this);
453 assert(Listeners && I != Listeners->end() &&
454 "PassRegistrationListener not registered!");
455 Listeners->erase(I);
456
457 if (Listeners->empty()) {
458 delete Listeners;
459 Listeners = 0;
460 }
461}
462
463// enumeratePasses - Iterate over the registered passes, calling the
464// passEnumerate callback on each PassInfo object.
465//
466void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000467 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000468}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000469
Chris Lattner5264fce2010-01-22 06:29:25 +0000470PassNameParser::~PassNameParser() {}
471
Chris Lattner23d54052006-12-01 22:21:11 +0000472//===----------------------------------------------------------------------===//
473// AnalysisUsage Class Implementation
474//
475
Chris Lattner1b368a02006-12-01 23:27:45 +0000476namespace {
477 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000478 typedef AnalysisUsage::VectorType VectorType;
479 VectorType &CFGOnlyList;
480 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000481
482 void passEnumerate(const PassInfo *P) {
483 if (P->isCFGOnlyPass())
484 CFGOnlyList.push_back(P);
485 }
486 };
487}
488
Chris Lattner23d54052006-12-01 22:21:11 +0000489// setPreservesCFG - This function should be called to by the pass, iff they do
490// not:
491//
492// 1. Add or remove basic blocks from the function
493// 2. Modify terminator instructions in any way.
494//
495// This function annotates the AnalysisUsage info object to say that analyses
496// that only depend on the CFG are preserved by this pass.
497//
498void AnalysisUsage::setPreservesCFG() {
499 // Since this transformation doesn't modify the CFG, it preserves all analyses
500 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000501 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000502}
503
Dan Gohmanffdee302010-06-21 18:46:45 +0000504AnalysisUsage &AnalysisUsage::addRequiredID(AnalysisID ID) {
505 assert(ID && "Pass class not registered!");
506 Required.push_back(ID);
507 return *this;
508}
Chris Lattner23d54052006-12-01 22:21:11 +0000509
Dan Gohmanffdee302010-06-21 18:46:45 +0000510AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(AnalysisID ID) {
511 assert(ID && "Pass class not registered!");
512 Required.push_back(ID);
513 RequiredTransitive.push_back(ID);
514 return *this;
515}