blob: 9fed64754395385009cd5172495137992998cc7c [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"
21#include "llvm/Support/TypeInfo.h"
Reid Spencer8baf8e22004-07-04 11:55:37 +000022#include <iostream>
Chris Lattner6e041bd2002-08-21 22:17:09 +000023#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner675e7a92002-08-21 23:51:51 +000026// IncludeFile - Stub function used to help linking out.
27IncludeFile::IncludeFile(void*) {}
28
Chris Lattner7e0dbe62002-05-06 19:31:52 +000029//===----------------------------------------------------------------------===//
30// AnalysisID Class Implementation
31//
32
Chris Lattnerab16a652003-10-13 05:33:01 +000033// getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it
34// initializer order independent.
35static std::vector<const PassInfo*> &getCFGOnlyAnalyses() {
36 static std::vector<const PassInfo*> CFGOnlyAnalyses;
37 return CFGOnlyAnalyses;
38}
Chris Lattnercdd09c22002-01-31 00:45:31 +000039
Chris Lattnerdd99f5b2003-10-12 18:52:12 +000040void RegisterPassBase::setOnlyUsesCFG() {
Chris Lattnerc66da832006-01-23 01:01:04 +000041 getCFGOnlyAnalyses().push_back(&PIObj);
Chris Lattner7e0dbe62002-05-06 19:31:52 +000042}
43
44//===----------------------------------------------------------------------===//
45// AnalysisResolver Class Implementation
46//
47
Reid Spencer8edc8be2005-04-25 01:01:35 +000048AnalysisResolver::~AnalysisResolver() {
49}
Chris Lattnercdd09c22002-01-31 00:45:31 +000050void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
51 assert(P->Resolver == 0 && "Pass already in a PassManager!");
52 P->Resolver = AR;
53}
54
Chris Lattner7e0dbe62002-05-06 19:31:52 +000055//===----------------------------------------------------------------------===//
56// AnalysisUsage Class Implementation
57//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000058
Chris Lattner820d9712002-10-21 20:00:28 +000059// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattneree2ff5d2002-04-28 21:25:41 +000060// not:
61//
62// 1. Add or remove basic blocks from the function
63// 2. Modify terminator instructions in any way.
64//
65// This function annotates the AnalysisUsage info object to say that analyses
66// that only depend on the CFG are preserved by this pass.
67//
Chris Lattner820d9712002-10-21 20:00:28 +000068void AnalysisUsage::setPreservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000069 // Since this transformation doesn't modify the CFG, it preserves all analyses
70 // that only depend on the CFG (like dominators, loop info, etc...)
71 //
72 Preserved.insert(Preserved.end(),
Chris Lattnerab16a652003-10-13 05:33:01 +000073 getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000074}
75
76
Chris Lattner37c86672002-04-28 20:46:05 +000077//===----------------------------------------------------------------------===//
78// PassManager implementation - The PassManager class is a simple Pimpl class
79// that wraps the PassManagerT template.
80//
Chris Lattnerc47b0812006-01-04 07:47:13 +000081PassManager::PassManager() : PM(new ModulePassManager()) {}
Chris Lattner37c86672002-04-28 20:46:05 +000082PassManager::~PassManager() { delete PM; }
Chris Lattner79e523d2004-09-20 04:47:19 +000083void PassManager::add(Pass *P) {
84 ModulePass *MP = dynamic_cast<ModulePass*>(P);
85 assert(MP && "Not a modulepass?");
86 PM->add(MP);
87}
88bool PassManager::run(Module &M) { return PM->runOnModule(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000089
Brian Gaekeb7a83882003-08-12 17:22:39 +000090//===----------------------------------------------------------------------===//
91// FunctionPassManager implementation - The FunctionPassManager class
92// is a simple Pimpl class that wraps the PassManagerT template. It
93// is like PassManager, but only deals in FunctionPasses.
94//
Misha Brukmanb1c93172005-04-21 23:48:37 +000095FunctionPassManager::FunctionPassManager(ModuleProvider *P) :
Chris Lattnerc47b0812006-01-04 07:47:13 +000096 PM(new FunctionPassManagerT()), MP(P) {}
Brian Gaekeb7a83882003-08-12 17:22:39 +000097FunctionPassManager::~FunctionPassManager() { delete PM; }
98void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +000099void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000100bool FunctionPassManager::run(Function &F) {
Reid Spencer6967cd52004-07-07 21:01:38 +0000101 try {
102 MP->materializeFunction(&F);
103 } catch (std::string& errstr) {
104 std::cerr << "Error reading bytecode file: " << errstr << "\n";
105 abort();
106 } catch (...) {
Misha Brukman42331552004-07-07 21:22:05 +0000107 std::cerr << "Error reading bytecode file!\n";
Reid Spencer6967cd52004-07-07 21:01:38 +0000108 abort();
109 }
Chris Lattner79e523d2004-09-20 04:47:19 +0000110 return PM->run(F);
Misha Brukman56a86422003-10-14 21:38:42 +0000111}
Brian Gaekeb7a83882003-08-12 17:22:39 +0000112
Chris Lattner37c86672002-04-28 20:46:05 +0000113
114//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000115// TimingInfo Class - This class is used to calculate information about the
116// amount of time each pass takes to execute. This only happens with
117// -time-passes is enabled on the command line.
118//
Reid Spencer2e4bfff2004-08-24 17:52:35 +0000119bool llvm::TimePassesIsEnabled = false;
120static cl::opt<bool,true>
121EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
Chris Lattnerf5cad152002-07-22 02:10:13 +0000122 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000123
Chris Lattnere7e09442003-08-14 05:20:28 +0000124// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
125// a non null value (if the -time-passes option is enabled) or it leaves it
126// null. It may be called multiple times.
127void TimingInfo::createTheTimeInfo() {
Reid Spencer2e4bfff2004-08-24 17:52:35 +0000128 if (!TimePassesIsEnabled || TheTimeInfo) return;
Chris Lattnere7e09442003-08-14 05:20:28 +0000129
130 // Constructed the first time this is called, iff -time-passes is enabled.
131 // This guarantees that the object will be constructed before static globals,
132 // thus it will be destroyed before them.
133 static TimingInfo TTI;
134 TheTimeInfo = &TTI;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000135}
136
Chris Lattner1e4867f2002-07-30 19:51:02 +0000137void PMDebug::PrintArgumentInformation(const Pass *P) {
138 // Print out passes in pass manager...
139 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
140 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
141 PrintArgumentInformation(PM->getContainedPass(i));
142
143 } else { // Normal pass. Print argument information...
144 // Print out arguments for registered passes that are _optimizations_
145 if (const PassInfo *PI = P->getPassInfo())
146 if (PI->getPassType() & PassInfo::Optimization)
147 std::cerr << " -" << PI->getPassArgument();
148 }
149}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000150
151void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattner43640d72004-02-29 22:37:04 +0000152 Pass *P, Module *M) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000153 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000155 << P->getPassName();
Chris Lattner43640d72004-02-29 22:37:04 +0000156 if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n";
157 std::cerr << "'...\n";
158 }
159}
Chris Lattnera454b5b2002-04-28 05:14:06 +0000160
Chris Lattner43640d72004-02-29 22:37:04 +0000161void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
162 Pass *P, Function *F) {
163 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000164 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000165 << P->getPassName();
166 if (F) std::cerr << "' on Function '" << F->getName();
167 std::cerr << "'...\n";
168 }
169}
170
171void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
172 Pass *P, BasicBlock *BB) {
173 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000174 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000175 << P->getPassName();
176 if (BB) std::cerr << "' on BasicBlock '" << BB->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000177 std::cerr << "'...\n";
178 }
179}
180
181void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000182 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000183 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000184 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerb3708e22002-08-30 20:23:45 +0000185 for (unsigned i = 0; i != Set.size(); ++i) {
186 if (i) std::cerr << ",";
187 std::cerr << " " << Set[i]->getPassName();
188 }
Chris Lattnercdd09c22002-01-31 00:45:31 +0000189 std::cerr << "\n";
190 }
191}
192
Chris Lattnercdd09c22002-01-31 00:45:31 +0000193//===----------------------------------------------------------------------===//
194// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000195//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000196
Chris Lattnerc47b0812006-01-04 07:47:13 +0000197void ModulePass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000198 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000199}
Chris Lattner26e4f892002-01-21 07:37:31 +0000200
Chris Lattner9ad77572003-03-21 21:41:02 +0000201bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
202 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
203}
204
Chris Lattner198cf422002-07-30 16:27:02 +0000205// dumpPassStructure - Implement the -debug-passes=Structure option
206void Pass::dumpPassStructure(unsigned Offset) {
207 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
208}
Chris Lattner37104aa2002-04-29 14:57:45 +0000209
Brian Gaeke465a5cc2004-02-28 21:55:18 +0000210// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +0000211//
Chris Lattner071577d2002-07-29 21:02:31 +0000212const char *Pass::getPassName() const {
213 if (const PassInfo *PI = getPassInfo())
214 return PI->getPassName();
215 return typeid(*this).name();
216}
Chris Lattner37104aa2002-04-29 14:57:45 +0000217
Misha Brukman56a86422003-10-14 21:38:42 +0000218// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000219// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000220// implement this method.
221//
Reid Spencer90839362004-12-07 04:03:45 +0000222void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000223 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
224}
225
226// dump - call print(std::cerr);
227void Pass::dump() const {
228 print(std::cerr, 0);
229}
230
Chris Lattnercdd09c22002-01-31 00:45:31 +0000231//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000232// ImmutablePass Implementation
233//
Chris Lattnerc47b0812006-01-04 07:47:13 +0000234void ImmutablePass::addToPassManager(ModulePassManager *PM,
Chris Lattneree0788d2002-09-25 21:59:11 +0000235 AnalysisUsage &AU) {
236 PM->addPass(this, AU);
237}
238
239
240//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000241// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000242//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000243
Chris Lattnerc8e66542002-04-27 06:56:12 +0000244// run - On a module, we run this pass by initializing, runOnFunction'ing once
245// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000246//
Chris Lattner79e523d2004-09-20 04:47:19 +0000247bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000248 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000249
Chris Lattner113f4f42002-06-25 16:13:24 +0000250 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
251 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000252 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000253
Chris Lattnercdd09c22002-01-31 00:45:31 +0000254 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000255}
256
Chris Lattnerc8e66542002-04-27 06:56:12 +0000257// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000258//
Chris Lattner113f4f42002-06-25 16:13:24 +0000259bool FunctionPass::run(Function &F) {
260 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000261
Chris Lattner79e523d2004-09-20 04:47:19 +0000262 bool Changed = doInitialization(*F.getParent());
263 Changed |= runOnFunction(F);
264 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000265}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000266
Chris Lattnerc47b0812006-01-04 07:47:13 +0000267void FunctionPass::addToPassManager(ModulePassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000268 AnalysisUsage &AU) {
269 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000270}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000271
Chris Lattnerc47b0812006-01-04 07:47:13 +0000272void FunctionPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000273 AnalysisUsage &AU) {
274 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000275}
276
277//===----------------------------------------------------------------------===//
278// BasicBlockPass Implementation
279//
280
Chris Lattnerc8e66542002-04-27 06:56:12 +0000281// To run this pass on a function, we simply call runOnBasicBlock once for each
282// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000283//
Chris Lattner113f4f42002-06-25 16:13:24 +0000284bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000285 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000286 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000287 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000288 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000289}
290
291// To run directly on the basic block, we initialize, runOnBasicBlock, then
292// finalize.
293//
Chris Lattner79e523d2004-09-20 04:47:19 +0000294bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000295 Function &F = *BB.getParent();
296 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000297 bool Changed = doInitialization(M);
298 Changed |= doInitialization(F);
299 Changed |= runOnBasicBlock(BB);
300 Changed |= doFinalization(F);
301 Changed |= doFinalization(M);
302 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000303}
304
Chris Lattnerc47b0812006-01-04 07:47:13 +0000305void BasicBlockPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000306 AnalysisUsage &AU) {
307 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000308}
309
Chris Lattnerc47b0812006-01-04 07:47:13 +0000310void BasicBlockPass::addToPassManager(BasicBlockPassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000311 AnalysisUsage &AU) {
312 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000313}
314
Chris Lattner37d3c952002-07-23 18:08:00 +0000315
316//===----------------------------------------------------------------------===//
317// Pass Registration mechanism
318//
319static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
320static std::vector<PassRegistrationListener*> *Listeners = 0;
321
322// getPassInfo - Return the PassInfo data structure that corresponds to this
323// pass...
324const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000325 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000326 return lookupPassInfo(typeid(*this));
327}
328
329const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner071577d2002-07-29 21:02:31 +0000330 if (PassInfoMap == 0) return 0;
Chris Lattner4b169632002-08-21 17:08:37 +0000331 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
Chris Lattner071577d2002-07-29 21:02:31 +0000332 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000333}
334
Chris Lattnerc66da832006-01-23 01:01:04 +0000335void RegisterPassBase::registerPass() {
Chris Lattner37d3c952002-07-23 18:08:00 +0000336 if (PassInfoMap == 0)
337 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
338
Chris Lattnerc66da832006-01-23 01:01:04 +0000339 assert(PassInfoMap->find(PIObj.getTypeInfo()) == PassInfoMap->end() &&
Chris Lattner37d3c952002-07-23 18:08:00 +0000340 "Pass already registered!");
Chris Lattnerc66da832006-01-23 01:01:04 +0000341 PassInfoMap->insert(std::make_pair(TypeInfo(PIObj.getTypeInfo()), &PIObj));
Chris Lattner37d3c952002-07-23 18:08:00 +0000342
343 // Notify any listeners...
344 if (Listeners)
345 for (std::vector<PassRegistrationListener*>::iterator
346 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000347 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000348}
349
Chris Lattnerc66da832006-01-23 01:01:04 +0000350void RegisterPassBase::unregisterPass() {
Chris Lattner37d3c952002-07-23 18:08:00 +0000351 assert(PassInfoMap && "Pass registered but not in map!");
352 std::map<TypeInfo, PassInfo*>::iterator I =
Chris Lattnerc66da832006-01-23 01:01:04 +0000353 PassInfoMap->find(PIObj.getTypeInfo());
Chris Lattner37d3c952002-07-23 18:08:00 +0000354 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
355
356 // Remove pass from the map...
357 PassInfoMap->erase(I);
358 if (PassInfoMap->empty()) {
359 delete PassInfoMap;
360 PassInfoMap = 0;
361 }
362
363 // Notify any listeners...
364 if (Listeners)
365 for (std::vector<PassRegistrationListener*>::iterator
366 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000367 (*I)->passUnregistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000368}
369
Chris Lattner6e041bd2002-08-21 22:17:09 +0000370//===----------------------------------------------------------------------===//
371// Analysis Group Implementation Code
372//===----------------------------------------------------------------------===//
373
374struct AnalysisGroupInfo {
375 const PassInfo *DefaultImpl;
376 std::set<const PassInfo *> Implementations;
377 AnalysisGroupInfo() : DefaultImpl(0) {}
378};
379
380static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
381
382// RegisterAGBase implementation
383//
384RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
385 const std::type_info *Pass, bool isDefault)
Chris Lattnerc66da832006-01-23 01:01:04 +0000386 : RegisterPassBase(Interface, PassInfo::AnalysisGroup),
387 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000388
Chris Lattner6e041bd2002-08-21 22:17:09 +0000389 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000390 if (InterfaceInfo == 0) {
391 // First reference to Interface, register it now.
392 registerPass();
393 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000394 }
395 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
396 "Trying to join an analysis group that is a normal pass!");
397
398 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000399 ImplementationInfo = Pass::lookupPassInfo(*Pass);
400 assert(ImplementationInfo &&
401 "Must register pass before adding to AnalysisGroup!");
402
Chris Lattnerb3708e22002-08-30 20:23:45 +0000403 // Make sure we keep track of the fact that the implementation implements
404 // the interface.
405 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
406 IIPI->addInterfaceImplemented(InterfaceInfo);
407
Chris Lattner6e041bd2002-08-21 22:17:09 +0000408 // Lazily allocate to avoid nasty initialization order dependencies
409 if (AnalysisGroupInfoMap == 0)
410 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
411
412 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
413 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
414 "Cannot add a pass to the same analysis group more than once!");
415 AGI.Implementations.insert(ImplementationInfo);
416 if (isDefault) {
417 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
418 "Default implementation for analysis group already specified!");
419 assert(ImplementationInfo->getNormalCtor() &&
420 "Cannot specify pass as default if it does not have a default ctor");
421 AGI.DefaultImpl = ImplementationInfo;
422 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
423 }
424 }
425}
426
427void RegisterAGBase::setGroupName(const char *Name) {
428 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
429 InterfaceInfo->setPassName(Name);
430}
431
432RegisterAGBase::~RegisterAGBase() {
433 if (ImplementationInfo) {
434 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
435 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
436
437 assert(AGI.Implementations.count(ImplementationInfo) &&
438 "Pass not a member of analysis group?");
439
440 if (AGI.DefaultImpl == ImplementationInfo)
441 AGI.DefaultImpl = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442
Chris Lattner6e041bd2002-08-21 22:17:09 +0000443 AGI.Implementations.erase(ImplementationInfo);
444
445 // Last member of this analysis group? Unregister PassInfo, delete map entry
446 if (AGI.Implementations.empty()) {
447 assert(AGI.DefaultImpl == 0 &&
448 "Default implementation didn't unregister?");
449 AnalysisGroupInfoMap->erase(InterfaceInfo);
450 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
451 delete AnalysisGroupInfoMap;
452 AnalysisGroupInfoMap = 0;
453 }
Chris Lattner6e041bd2002-08-21 22:17:09 +0000454 }
455 }
Chris Lattnerc66da832006-01-23 01:01:04 +0000456
457 if (InterfaceInfo == &PIObj)
458 unregisterPass();
Chris Lattner6e041bd2002-08-21 22:17:09 +0000459}
460
461
Chris Lattner37d3c952002-07-23 18:08:00 +0000462//===----------------------------------------------------------------------===//
463// PassRegistrationListener implementation
464//
465
466// PassRegistrationListener ctor - Add the current object to the list of
467// PassRegistrationListeners...
468PassRegistrationListener::PassRegistrationListener() {
469 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
470 Listeners->push_back(this);
471}
472
473// dtor - Remove object from list of listeners...
474PassRegistrationListener::~PassRegistrationListener() {
475 std::vector<PassRegistrationListener*>::iterator I =
476 std::find(Listeners->begin(), Listeners->end(), this);
477 assert(Listeners && I != Listeners->end() &&
478 "PassRegistrationListener not registered!");
479 Listeners->erase(I);
480
481 if (Listeners->empty()) {
482 delete Listeners;
483 Listeners = 0;
484 }
485}
486
487// enumeratePasses - Iterate over the registered passes, calling the
488// passEnumerate callback on each PassInfo object.
489//
490void PassRegistrationListener::enumeratePasses() {
491 if (PassInfoMap)
492 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
493 E = PassInfoMap->end(); I != E; ++I)
494 passEnumerate(I->second);
495}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000496