blob: 0097fbdd3117bfcdf781c59487df8df025821e7d [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
Chris Lattnercdd09c22002-01-31 00:45:31 +000016#include "llvm/PassManager.h"
Chris Lattner37c86672002-04-28 20:46:05 +000017#include "PassManagerT.h" // PassManagerT implementation
Chris Lattnercdd09c22002-01-31 00:45:31 +000018#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"
Chris Lattner1b368a02006-12-01 23:27:45 +000021#include "llvm/Support/ManagedStatic.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/TypeInfo.h"
Reid Spencer8baf8e22004-07-04 11:55:37 +000023#include <iostream>
Chris Lattner6e041bd2002-08-21 22:17:09 +000024#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner7e0dbe62002-05-06 19:31:52 +000027//===----------------------------------------------------------------------===//
Chris Lattner7e0dbe62002-05-06 19:31:52 +000028// AnalysisResolver Class Implementation
29//
30
Reid Spencer8edc8be2005-04-25 01:01:35 +000031AnalysisResolver::~AnalysisResolver() {
32}
Chris Lattnercdd09c22002-01-31 00:45:31 +000033void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
34 assert(P->Resolver == 0 && "Pass already in a PassManager!");
35 P->Resolver = AR;
36}
37
Chris Lattner7e0dbe62002-05-06 19:31:52 +000038//===----------------------------------------------------------------------===//
Chris Lattner37c86672002-04-28 20:46:05 +000039// PassManager implementation - The PassManager class is a simple Pimpl class
40// that wraps the PassManagerT template.
41//
Chris Lattnerc47b0812006-01-04 07:47:13 +000042PassManager::PassManager() : PM(new ModulePassManager()) {}
Chris Lattner37c86672002-04-28 20:46:05 +000043PassManager::~PassManager() { delete PM; }
Chris Lattner79e523d2004-09-20 04:47:19 +000044void PassManager::add(Pass *P) {
45 ModulePass *MP = dynamic_cast<ModulePass*>(P);
46 assert(MP && "Not a modulepass?");
47 PM->add(MP);
48}
49bool PassManager::run(Module &M) { return PM->runOnModule(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000050
Brian Gaekeb7a83882003-08-12 17:22:39 +000051//===----------------------------------------------------------------------===//
52// FunctionPassManager implementation - The FunctionPassManager class
53// is a simple Pimpl class that wraps the PassManagerT template. It
54// is like PassManager, but only deals in FunctionPasses.
55//
Misha Brukmanb1c93172005-04-21 23:48:37 +000056FunctionPassManager::FunctionPassManager(ModuleProvider *P) :
Chris Lattnerc47b0812006-01-04 07:47:13 +000057 PM(new FunctionPassManagerT()), MP(P) {}
Brian Gaekeb7a83882003-08-12 17:22:39 +000058FunctionPassManager::~FunctionPassManager() { delete PM; }
59void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +000060void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
Chris Lattner2f779222006-09-04 04:07:39 +000061
62/// doInitialization - Run all of the initializers for the function passes.
63///
64bool FunctionPassManager::doInitialization() {
65 return PM->doInitialization(*MP->getModule());
66}
67
Misha Brukmanb1c93172005-04-21 23:48:37 +000068bool FunctionPassManager::run(Function &F) {
Chris Lattner3d40daa2006-07-06 21:35:01 +000069 std::string errstr;
70 if (MP->materializeFunction(&F, &errstr)) {
Reid Spencer6967cd52004-07-07 21:01:38 +000071 std::cerr << "Error reading bytecode file: " << errstr << "\n";
72 abort();
Reid Spencer6967cd52004-07-07 21:01:38 +000073 }
Chris Lattner2f779222006-09-04 04:07:39 +000074 return PM->runOnFunction(F);
75}
76
77/// doFinalization - Run all of the initializers for the function passes.
78///
79bool FunctionPassManager::doFinalization() {
80 return PM->doFinalization(*MP->getModule());
Misha Brukman56a86422003-10-14 21:38:42 +000081}
Brian Gaekeb7a83882003-08-12 17:22:39 +000082
Chris Lattner37c86672002-04-28 20:46:05 +000083
84//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000085// TimingInfo Class - This class is used to calculate information about the
86// amount of time each pass takes to execute. This only happens with
87// -time-passes is enabled on the command line.
88//
Reid Spencer2e4bfff2004-08-24 17:52:35 +000089bool llvm::TimePassesIsEnabled = false;
90static cl::opt<bool,true>
91EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
Chris Lattnerf5cad152002-07-22 02:10:13 +000092 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000093
Chris Lattnere7e09442003-08-14 05:20:28 +000094// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
95// a non null value (if the -time-passes option is enabled) or it leaves it
96// null. It may be called multiple times.
97void TimingInfo::createTheTimeInfo() {
Reid Spencer2e4bfff2004-08-24 17:52:35 +000098 if (!TimePassesIsEnabled || TheTimeInfo) return;
Chris Lattnere7e09442003-08-14 05:20:28 +000099
100 // Constructed the first time this is called, iff -time-passes is enabled.
101 // This guarantees that the object will be constructed before static globals,
102 // thus it will be destroyed before them.
103 static TimingInfo TTI;
104 TheTimeInfo = &TTI;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000105}
106
Chris Lattner1e4867f2002-07-30 19:51:02 +0000107void PMDebug::PrintArgumentInformation(const Pass *P) {
108 // Print out passes in pass manager...
109 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
110 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
111 PrintArgumentInformation(PM->getContainedPass(i));
112
113 } else { // Normal pass. Print argument information...
114 // Print out arguments for registered passes that are _optimizations_
115 if (const PassInfo *PI = P->getPassInfo())
Chris Lattnerfbb46502006-08-27 22:21:55 +0000116 if (!PI->isAnalysisGroup())
Chris Lattner1e4867f2002-07-30 19:51:02 +0000117 std::cerr << " -" << PI->getPassArgument();
118 }
119}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000120
121void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattner43640d72004-02-29 22:37:04 +0000122 Pass *P, Module *M) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000123 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000124 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000125 << P->getPassName();
Chris Lattner43640d72004-02-29 22:37:04 +0000126 if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n";
127 std::cerr << "'...\n";
128 }
129}
Chris Lattnera454b5b2002-04-28 05:14:06 +0000130
Chris Lattner43640d72004-02-29 22:37:04 +0000131void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
132 Pass *P, Function *F) {
133 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000134 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000135 << P->getPassName();
136 if (F) std::cerr << "' on Function '" << F->getName();
137 std::cerr << "'...\n";
138 }
139}
140
141void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
142 Pass *P, BasicBlock *BB) {
143 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000144 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000145 << P->getPassName();
146 if (BB) std::cerr << "' on BasicBlock '" << BB->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000147 std::cerr << "'...\n";
148 }
149}
150
151void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000152 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000153 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000154 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerb3708e22002-08-30 20:23:45 +0000155 for (unsigned i = 0; i != Set.size(); ++i) {
156 if (i) std::cerr << ",";
157 std::cerr << " " << Set[i]->getPassName();
158 }
Chris Lattnercdd09c22002-01-31 00:45:31 +0000159 std::cerr << "\n";
160 }
161}
162
Chris Lattnercdd09c22002-01-31 00:45:31 +0000163//===----------------------------------------------------------------------===//
164// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000165//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000166
Chris Lattnerc47b0812006-01-04 07:47:13 +0000167void ModulePass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000168 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000169}
Chris Lattner26e4f892002-01-21 07:37:31 +0000170
Chris Lattner9ad77572003-03-21 21:41:02 +0000171bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
172 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
173}
174
Chris Lattner198cf422002-07-30 16:27:02 +0000175// dumpPassStructure - Implement the -debug-passes=Structure option
176void Pass::dumpPassStructure(unsigned Offset) {
177 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
178}
Chris Lattner37104aa2002-04-29 14:57:45 +0000179
Brian Gaeke465a5cc2004-02-28 21:55:18 +0000180// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +0000181//
Chris Lattner071577d2002-07-29 21:02:31 +0000182const char *Pass::getPassName() const {
183 if (const PassInfo *PI = getPassInfo())
184 return PI->getPassName();
185 return typeid(*this).name();
186}
Chris Lattner37104aa2002-04-29 14:57:45 +0000187
Misha Brukman56a86422003-10-14 21:38:42 +0000188// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000189// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000190// implement this method.
191//
Reid Spencer90839362004-12-07 04:03:45 +0000192void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000193 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
194}
195
196// dump - call print(std::cerr);
197void Pass::dump() const {
198 print(std::cerr, 0);
199}
200
Chris Lattnercdd09c22002-01-31 00:45:31 +0000201//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000202// ImmutablePass Implementation
203//
Chris Lattnerc47b0812006-01-04 07:47:13 +0000204void ImmutablePass::addToPassManager(ModulePassManager *PM,
Chris Lattneree0788d2002-09-25 21:59:11 +0000205 AnalysisUsage &AU) {
206 PM->addPass(this, AU);
207}
208
209
210//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000211// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000212//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000213
Chris Lattnerc8e66542002-04-27 06:56:12 +0000214// run - On a module, we run this pass by initializing, runOnFunction'ing once
215// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000216//
Chris Lattner79e523d2004-09-20 04:47:19 +0000217bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000218 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000219
Chris Lattner113f4f42002-06-25 16:13:24 +0000220 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
221 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000222 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000223
Chris Lattnercdd09c22002-01-31 00:45:31 +0000224 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000225}
226
Chris Lattnerc8e66542002-04-27 06:56:12 +0000227// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000228//
Chris Lattner113f4f42002-06-25 16:13:24 +0000229bool FunctionPass::run(Function &F) {
230 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000231
Chris Lattner79e523d2004-09-20 04:47:19 +0000232 bool Changed = doInitialization(*F.getParent());
233 Changed |= runOnFunction(F);
234 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000235}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000236
Chris Lattnerc47b0812006-01-04 07:47:13 +0000237void FunctionPass::addToPassManager(ModulePassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000238 AnalysisUsage &AU) {
239 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000240}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000241
Chris Lattnerc47b0812006-01-04 07:47:13 +0000242void FunctionPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000243 AnalysisUsage &AU) {
244 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000245}
246
247//===----------------------------------------------------------------------===//
248// BasicBlockPass Implementation
249//
250
Chris Lattnerc8e66542002-04-27 06:56:12 +0000251// To run this pass on a function, we simply call runOnBasicBlock once for each
252// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000253//
Chris Lattner113f4f42002-06-25 16:13:24 +0000254bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000255 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000256 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000257 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000258 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000259}
260
261// To run directly on the basic block, we initialize, runOnBasicBlock, then
262// finalize.
263//
Chris Lattner79e523d2004-09-20 04:47:19 +0000264bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000265 Function &F = *BB.getParent();
266 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000267 bool Changed = doInitialization(M);
268 Changed |= doInitialization(F);
269 Changed |= runOnBasicBlock(BB);
270 Changed |= doFinalization(F);
271 Changed |= doFinalization(M);
272 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000273}
274
Chris Lattnerc47b0812006-01-04 07:47:13 +0000275void BasicBlockPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000276 AnalysisUsage &AU) {
277 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000278}
279
Chris Lattnerc47b0812006-01-04 07:47:13 +0000280void BasicBlockPass::addToPassManager(BasicBlockPassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000281 AnalysisUsage &AU) {
282 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000283}
284
Chris Lattner37d3c952002-07-23 18:08:00 +0000285
286//===----------------------------------------------------------------------===//
287// Pass Registration mechanism
288//
Chris Lattner1b368a02006-12-01 23:27:45 +0000289class PassRegistrar {
290 std::map<TypeInfo, PassInfo*> PassInfoMap;
291public:
292
293 const PassInfo *GetPassInfo(const std::type_info &TI) const {
294 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
295 return I != PassInfoMap.end() ? I->second : 0;
296 }
297
298 void RegisterPass(PassInfo &PI) {
299 bool Inserted =
300 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
301 assert(Inserted && "Pass registered multiple times!");
302 }
303
304 void UnregisterPass(PassInfo &PI) {
305 std::map<TypeInfo, PassInfo*>::iterator I =
306 PassInfoMap.find(PI.getTypeInfo());
307 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
308
309 // Remove pass from the map.
310 PassInfoMap.erase(I);
311 }
312
313 void EnumerateWith(PassRegistrationListener *L) {
314 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
315 E = PassInfoMap.end(); I != E; ++I)
316 L->passEnumerate(I->second);
317 }
318};
319
320
321static ManagedStatic<PassRegistrar> PassRegistrarObj;
Chris Lattner37d3c952002-07-23 18:08:00 +0000322static std::vector<PassRegistrationListener*> *Listeners = 0;
323
324// getPassInfo - Return the PassInfo data structure that corresponds to this
325// pass...
326const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000327 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000328 return lookupPassInfo(typeid(*this));
329}
330
331const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000332 return PassRegistrarObj->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000333}
334
Chris Lattnerc66da832006-01-23 01:01:04 +0000335void RegisterPassBase::registerPass() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000336 PassRegistrarObj->RegisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000337
Chris Lattner1b368a02006-12-01 23:27:45 +0000338 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000339 if (Listeners)
340 for (std::vector<PassRegistrationListener*>::iterator
341 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000342 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000343}
344
Chris Lattnerc66da832006-01-23 01:01:04 +0000345void RegisterPassBase::unregisterPass() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000346 PassRegistrarObj->UnregisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000347}
348
Chris Lattner6e041bd2002-08-21 22:17:09 +0000349//===----------------------------------------------------------------------===//
350// Analysis Group Implementation Code
351//===----------------------------------------------------------------------===//
352
353struct AnalysisGroupInfo {
354 const PassInfo *DefaultImpl;
355 std::set<const PassInfo *> Implementations;
356 AnalysisGroupInfo() : DefaultImpl(0) {}
357};
358
359static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
360
361// RegisterAGBase implementation
362//
363RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
364 const std::type_info *Pass, bool isDefault)
Chris Lattnerfbb46502006-08-27 22:21:55 +0000365 : RegisterPassBase(Interface),
Chris Lattnerc66da832006-01-23 01:01:04 +0000366 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000367
Chris Lattner6e041bd2002-08-21 22:17:09 +0000368 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000369 if (InterfaceInfo == 0) {
370 // First reference to Interface, register it now.
371 registerPass();
372 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000373 }
Chris Lattnerfbb46502006-08-27 22:21:55 +0000374 assert(PIObj.isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000375 "Trying to join an analysis group that is a normal pass!");
376
377 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000378 ImplementationInfo = Pass::lookupPassInfo(*Pass);
379 assert(ImplementationInfo &&
380 "Must register pass before adding to AnalysisGroup!");
381
Chris Lattnerb3708e22002-08-30 20:23:45 +0000382 // Make sure we keep track of the fact that the implementation implements
383 // the interface.
384 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
385 IIPI->addInterfaceImplemented(InterfaceInfo);
386
Chris Lattner6e041bd2002-08-21 22:17:09 +0000387 // Lazily allocate to avoid nasty initialization order dependencies
388 if (AnalysisGroupInfoMap == 0)
389 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
390
391 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
392 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
393 "Cannot add a pass to the same analysis group more than once!");
394 AGI.Implementations.insert(ImplementationInfo);
395 if (isDefault) {
396 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
397 "Default implementation for analysis group already specified!");
398 assert(ImplementationInfo->getNormalCtor() &&
399 "Cannot specify pass as default if it does not have a default ctor");
400 AGI.DefaultImpl = ImplementationInfo;
401 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
402 }
403 }
404}
405
406void RegisterAGBase::setGroupName(const char *Name) {
407 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
408 InterfaceInfo->setPassName(Name);
409}
410
411RegisterAGBase::~RegisterAGBase() {
412 if (ImplementationInfo) {
413 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
414 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
415
416 assert(AGI.Implementations.count(ImplementationInfo) &&
417 "Pass not a member of analysis group?");
418
419 if (AGI.DefaultImpl == ImplementationInfo)
420 AGI.DefaultImpl = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000421
Chris Lattner6e041bd2002-08-21 22:17:09 +0000422 AGI.Implementations.erase(ImplementationInfo);
423
424 // Last member of this analysis group? Unregister PassInfo, delete map entry
425 if (AGI.Implementations.empty()) {
426 assert(AGI.DefaultImpl == 0 &&
427 "Default implementation didn't unregister?");
428 AnalysisGroupInfoMap->erase(InterfaceInfo);
429 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
430 delete AnalysisGroupInfoMap;
431 AnalysisGroupInfoMap = 0;
432 }
Chris Lattner6e041bd2002-08-21 22:17:09 +0000433 }
434 }
Chris Lattnerc66da832006-01-23 01:01:04 +0000435
436 if (InterfaceInfo == &PIObj)
437 unregisterPass();
Chris Lattner6e041bd2002-08-21 22:17:09 +0000438}
439
440
Chris Lattner37d3c952002-07-23 18:08:00 +0000441//===----------------------------------------------------------------------===//
442// PassRegistrationListener implementation
443//
444
445// PassRegistrationListener ctor - Add the current object to the list of
446// PassRegistrationListeners...
447PassRegistrationListener::PassRegistrationListener() {
448 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
449 Listeners->push_back(this);
450}
451
452// dtor - Remove object from list of listeners...
453PassRegistrationListener::~PassRegistrationListener() {
454 std::vector<PassRegistrationListener*>::iterator I =
455 std::find(Listeners->begin(), Listeners->end(), this);
456 assert(Listeners && I != Listeners->end() &&
457 "PassRegistrationListener not registered!");
458 Listeners->erase(I);
459
460 if (Listeners->empty()) {
461 delete Listeners;
462 Listeners = 0;
463 }
464}
465
466// enumeratePasses - Iterate over the registered passes, calling the
467// passEnumerate callback on each PassInfo object.
468//
469void PassRegistrationListener::enumeratePasses() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000470 PassRegistrarObj->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000471}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000472
Chris Lattner23d54052006-12-01 22:21:11 +0000473//===----------------------------------------------------------------------===//
474// AnalysisUsage Class Implementation
475//
476
Chris Lattner1b368a02006-12-01 23:27:45 +0000477namespace {
478 struct GetCFGOnlyPasses : public PassRegistrationListener {
479 std::vector<AnalysisID> &CFGOnlyList;
480 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
481
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
504