blob: a7160f1f1ee85652460d674b5152fcdf8557f0c2 [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 Lattner7e0dbe62002-05-06 19:31:52 +000026//===----------------------------------------------------------------------===//
27// AnalysisID Class Implementation
28//
29
Chris Lattnerab16a652003-10-13 05:33:01 +000030// getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it
31// initializer order independent.
32static std::vector<const PassInfo*> &getCFGOnlyAnalyses() {
33 static std::vector<const PassInfo*> CFGOnlyAnalyses;
34 return CFGOnlyAnalyses;
35}
Chris Lattnercdd09c22002-01-31 00:45:31 +000036
Chris Lattnerdd99f5b2003-10-12 18:52:12 +000037void RegisterPassBase::setOnlyUsesCFG() {
Chris Lattnerc66da832006-01-23 01:01:04 +000038 getCFGOnlyAnalyses().push_back(&PIObj);
Chris Lattner7e0dbe62002-05-06 19:31:52 +000039}
40
41//===----------------------------------------------------------------------===//
42// AnalysisResolver Class Implementation
43//
44
Reid Spencer8edc8be2005-04-25 01:01:35 +000045AnalysisResolver::~AnalysisResolver() {
46}
Chris Lattnercdd09c22002-01-31 00:45:31 +000047void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
48 assert(P->Resolver == 0 && "Pass already in a PassManager!");
49 P->Resolver = AR;
50}
51
Chris Lattner7e0dbe62002-05-06 19:31:52 +000052//===----------------------------------------------------------------------===//
53// AnalysisUsage Class Implementation
54//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000055
Chris Lattner820d9712002-10-21 20:00:28 +000056// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattneree2ff5d2002-04-28 21:25:41 +000057// not:
58//
59// 1. Add or remove basic blocks from the function
60// 2. Modify terminator instructions in any way.
61//
62// This function annotates the AnalysisUsage info object to say that analyses
63// that only depend on the CFG are preserved by this pass.
64//
Chris Lattner820d9712002-10-21 20:00:28 +000065void AnalysisUsage::setPreservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000066 // Since this transformation doesn't modify the CFG, it preserves all analyses
67 // that only depend on the CFG (like dominators, loop info, etc...)
68 //
69 Preserved.insert(Preserved.end(),
Chris Lattnerab16a652003-10-13 05:33:01 +000070 getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000071}
72
73
Chris Lattner37c86672002-04-28 20:46:05 +000074//===----------------------------------------------------------------------===//
75// PassManager implementation - The PassManager class is a simple Pimpl class
76// that wraps the PassManagerT template.
77//
Chris Lattnerc47b0812006-01-04 07:47:13 +000078PassManager::PassManager() : PM(new ModulePassManager()) {}
Chris Lattner37c86672002-04-28 20:46:05 +000079PassManager::~PassManager() { delete PM; }
Chris Lattner79e523d2004-09-20 04:47:19 +000080void PassManager::add(Pass *P) {
81 ModulePass *MP = dynamic_cast<ModulePass*>(P);
82 assert(MP && "Not a modulepass?");
83 PM->add(MP);
84}
85bool PassManager::run(Module &M) { return PM->runOnModule(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000086
Brian Gaekeb7a83882003-08-12 17:22:39 +000087//===----------------------------------------------------------------------===//
88// FunctionPassManager implementation - The FunctionPassManager class
89// is a simple Pimpl class that wraps the PassManagerT template. It
90// is like PassManager, but only deals in FunctionPasses.
91//
Misha Brukmanb1c93172005-04-21 23:48:37 +000092FunctionPassManager::FunctionPassManager(ModuleProvider *P) :
Chris Lattnerc47b0812006-01-04 07:47:13 +000093 PM(new FunctionPassManagerT()), MP(P) {}
Brian Gaekeb7a83882003-08-12 17:22:39 +000094FunctionPassManager::~FunctionPassManager() { delete PM; }
95void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +000096void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
Misha Brukmanb1c93172005-04-21 23:48:37 +000097bool FunctionPassManager::run(Function &F) {
Reid Spencer6967cd52004-07-07 21:01:38 +000098 try {
99 MP->materializeFunction(&F);
100 } catch (std::string& errstr) {
101 std::cerr << "Error reading bytecode file: " << errstr << "\n";
102 abort();
103 } catch (...) {
Misha Brukman42331552004-07-07 21:22:05 +0000104 std::cerr << "Error reading bytecode file!\n";
Reid Spencer6967cd52004-07-07 21:01:38 +0000105 abort();
106 }
Chris Lattner79e523d2004-09-20 04:47:19 +0000107 return PM->run(F);
Misha Brukman56a86422003-10-14 21:38:42 +0000108}
Brian Gaekeb7a83882003-08-12 17:22:39 +0000109
Chris Lattner37c86672002-04-28 20:46:05 +0000110
111//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000112// TimingInfo Class - This class is used to calculate information about the
113// amount of time each pass takes to execute. This only happens with
114// -time-passes is enabled on the command line.
115//
Reid Spencer2e4bfff2004-08-24 17:52:35 +0000116bool llvm::TimePassesIsEnabled = false;
117static cl::opt<bool,true>
118EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
Chris Lattnerf5cad152002-07-22 02:10:13 +0000119 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000120
Chris Lattnere7e09442003-08-14 05:20:28 +0000121// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
122// a non null value (if the -time-passes option is enabled) or it leaves it
123// null. It may be called multiple times.
124void TimingInfo::createTheTimeInfo() {
Reid Spencer2e4bfff2004-08-24 17:52:35 +0000125 if (!TimePassesIsEnabled || TheTimeInfo) return;
Chris Lattnere7e09442003-08-14 05:20:28 +0000126
127 // Constructed the first time this is called, iff -time-passes is enabled.
128 // This guarantees that the object will be constructed before static globals,
129 // thus it will be destroyed before them.
130 static TimingInfo TTI;
131 TheTimeInfo = &TTI;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000132}
133
Chris Lattner1e4867f2002-07-30 19:51:02 +0000134void PMDebug::PrintArgumentInformation(const Pass *P) {
135 // Print out passes in pass manager...
136 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
137 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
138 PrintArgumentInformation(PM->getContainedPass(i));
139
140 } else { // Normal pass. Print argument information...
141 // Print out arguments for registered passes that are _optimizations_
142 if (const PassInfo *PI = P->getPassInfo())
143 if (PI->getPassType() & PassInfo::Optimization)
144 std::cerr << " -" << PI->getPassArgument();
145 }
146}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000147
148void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattner43640d72004-02-29 22:37:04 +0000149 Pass *P, Module *M) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000150 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000151 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000152 << P->getPassName();
Chris Lattner43640d72004-02-29 22:37:04 +0000153 if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n";
154 std::cerr << "'...\n";
155 }
156}
Chris Lattnera454b5b2002-04-28 05:14:06 +0000157
Chris Lattner43640d72004-02-29 22:37:04 +0000158void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
159 Pass *P, Function *F) {
160 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000161 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000162 << P->getPassName();
163 if (F) std::cerr << "' on Function '" << F->getName();
164 std::cerr << "'...\n";
165 }
166}
167
168void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
169 Pass *P, BasicBlock *BB) {
170 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000171 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000172 << P->getPassName();
173 if (BB) std::cerr << "' on BasicBlock '" << BB->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000174 std::cerr << "'...\n";
175 }
176}
177
178void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000179 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000180 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000181 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerb3708e22002-08-30 20:23:45 +0000182 for (unsigned i = 0; i != Set.size(); ++i) {
183 if (i) std::cerr << ",";
184 std::cerr << " " << Set[i]->getPassName();
185 }
Chris Lattnercdd09c22002-01-31 00:45:31 +0000186 std::cerr << "\n";
187 }
188}
189
Chris Lattnercdd09c22002-01-31 00:45:31 +0000190//===----------------------------------------------------------------------===//
191// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000192//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000193
Chris Lattnerc47b0812006-01-04 07:47:13 +0000194void ModulePass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000195 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000196}
Chris Lattner26e4f892002-01-21 07:37:31 +0000197
Chris Lattner9ad77572003-03-21 21:41:02 +0000198bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
199 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
200}
201
Chris Lattner198cf422002-07-30 16:27:02 +0000202// dumpPassStructure - Implement the -debug-passes=Structure option
203void Pass::dumpPassStructure(unsigned Offset) {
204 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
205}
Chris Lattner37104aa2002-04-29 14:57:45 +0000206
Brian Gaeke465a5cc2004-02-28 21:55:18 +0000207// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +0000208//
Chris Lattner071577d2002-07-29 21:02:31 +0000209const char *Pass::getPassName() const {
210 if (const PassInfo *PI = getPassInfo())
211 return PI->getPassName();
212 return typeid(*this).name();
213}
Chris Lattner37104aa2002-04-29 14:57:45 +0000214
Misha Brukman56a86422003-10-14 21:38:42 +0000215// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000216// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000217// implement this method.
218//
Reid Spencer90839362004-12-07 04:03:45 +0000219void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000220 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
221}
222
223// dump - call print(std::cerr);
224void Pass::dump() const {
225 print(std::cerr, 0);
226}
227
Chris Lattnercdd09c22002-01-31 00:45:31 +0000228//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000229// ImmutablePass Implementation
230//
Chris Lattnerc47b0812006-01-04 07:47:13 +0000231void ImmutablePass::addToPassManager(ModulePassManager *PM,
Chris Lattneree0788d2002-09-25 21:59:11 +0000232 AnalysisUsage &AU) {
233 PM->addPass(this, AU);
234}
235
236
237//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000238// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000239//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000240
Chris Lattnerc8e66542002-04-27 06:56:12 +0000241// run - On a module, we run this pass by initializing, runOnFunction'ing once
242// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000243//
Chris Lattner79e523d2004-09-20 04:47:19 +0000244bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000245 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000246
Chris Lattner113f4f42002-06-25 16:13:24 +0000247 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
248 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000249 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000250
Chris Lattnercdd09c22002-01-31 00:45:31 +0000251 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000252}
253
Chris Lattnerc8e66542002-04-27 06:56:12 +0000254// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000255//
Chris Lattner113f4f42002-06-25 16:13:24 +0000256bool FunctionPass::run(Function &F) {
257 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000258
Chris Lattner79e523d2004-09-20 04:47:19 +0000259 bool Changed = doInitialization(*F.getParent());
260 Changed |= runOnFunction(F);
261 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000262}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000263
Chris Lattnerc47b0812006-01-04 07:47:13 +0000264void FunctionPass::addToPassManager(ModulePassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000265 AnalysisUsage &AU) {
266 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000267}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000268
Chris Lattnerc47b0812006-01-04 07:47:13 +0000269void FunctionPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000270 AnalysisUsage &AU) {
271 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000272}
273
274//===----------------------------------------------------------------------===//
275// BasicBlockPass Implementation
276//
277
Chris Lattnerc8e66542002-04-27 06:56:12 +0000278// To run this pass on a function, we simply call runOnBasicBlock once for each
279// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000280//
Chris Lattner113f4f42002-06-25 16:13:24 +0000281bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000282 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000283 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000284 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000285 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000286}
287
288// To run directly on the basic block, we initialize, runOnBasicBlock, then
289// finalize.
290//
Chris Lattner79e523d2004-09-20 04:47:19 +0000291bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000292 Function &F = *BB.getParent();
293 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000294 bool Changed = doInitialization(M);
295 Changed |= doInitialization(F);
296 Changed |= runOnBasicBlock(BB);
297 Changed |= doFinalization(F);
298 Changed |= doFinalization(M);
299 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000300}
301
Chris Lattnerc47b0812006-01-04 07:47:13 +0000302void BasicBlockPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000303 AnalysisUsage &AU) {
304 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000305}
306
Chris Lattnerc47b0812006-01-04 07:47:13 +0000307void BasicBlockPass::addToPassManager(BasicBlockPassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000308 AnalysisUsage &AU) {
309 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000310}
311
Chris Lattner37d3c952002-07-23 18:08:00 +0000312
313//===----------------------------------------------------------------------===//
314// Pass Registration mechanism
315//
316static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
317static std::vector<PassRegistrationListener*> *Listeners = 0;
318
319// getPassInfo - Return the PassInfo data structure that corresponds to this
320// pass...
321const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000322 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000323 return lookupPassInfo(typeid(*this));
324}
325
326const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner071577d2002-07-29 21:02:31 +0000327 if (PassInfoMap == 0) return 0;
Chris Lattner4b169632002-08-21 17:08:37 +0000328 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
Chris Lattner071577d2002-07-29 21:02:31 +0000329 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000330}
331
Chris Lattnerc66da832006-01-23 01:01:04 +0000332void RegisterPassBase::registerPass() {
Chris Lattner37d3c952002-07-23 18:08:00 +0000333 if (PassInfoMap == 0)
334 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
335
Chris Lattnerc66da832006-01-23 01:01:04 +0000336 assert(PassInfoMap->find(PIObj.getTypeInfo()) == PassInfoMap->end() &&
Chris Lattner37d3c952002-07-23 18:08:00 +0000337 "Pass already registered!");
Chris Lattnerc66da832006-01-23 01:01:04 +0000338 PassInfoMap->insert(std::make_pair(TypeInfo(PIObj.getTypeInfo()), &PIObj));
Chris Lattner37d3c952002-07-23 18:08:00 +0000339
340 // Notify any listeners...
341 if (Listeners)
342 for (std::vector<PassRegistrationListener*>::iterator
343 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000344 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000345}
346
Chris Lattnerc66da832006-01-23 01:01:04 +0000347void RegisterPassBase::unregisterPass() {
Chris Lattner37d3c952002-07-23 18:08:00 +0000348 assert(PassInfoMap && "Pass registered but not in map!");
349 std::map<TypeInfo, PassInfo*>::iterator I =
Chris Lattnerc66da832006-01-23 01:01:04 +0000350 PassInfoMap->find(PIObj.getTypeInfo());
Chris Lattner37d3c952002-07-23 18:08:00 +0000351 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
352
353 // Remove pass from the map...
354 PassInfoMap->erase(I);
355 if (PassInfoMap->empty()) {
356 delete PassInfoMap;
357 PassInfoMap = 0;
358 }
359
360 // Notify any listeners...
361 if (Listeners)
362 for (std::vector<PassRegistrationListener*>::iterator
363 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000364 (*I)->passUnregistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000365}
366
Chris Lattner6e041bd2002-08-21 22:17:09 +0000367//===----------------------------------------------------------------------===//
368// Analysis Group Implementation Code
369//===----------------------------------------------------------------------===//
370
371struct AnalysisGroupInfo {
372 const PassInfo *DefaultImpl;
373 std::set<const PassInfo *> Implementations;
374 AnalysisGroupInfo() : DefaultImpl(0) {}
375};
376
377static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
378
379// RegisterAGBase implementation
380//
381RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
382 const std::type_info *Pass, bool isDefault)
Chris Lattnerc66da832006-01-23 01:01:04 +0000383 : RegisterPassBase(Interface, PassInfo::AnalysisGroup),
384 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000385
Chris Lattner6e041bd2002-08-21 22:17:09 +0000386 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000387 if (InterfaceInfo == 0) {
388 // First reference to Interface, register it now.
389 registerPass();
390 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000391 }
392 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
393 "Trying to join an analysis group that is a normal pass!");
394
395 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000396 ImplementationInfo = Pass::lookupPassInfo(*Pass);
397 assert(ImplementationInfo &&
398 "Must register pass before adding to AnalysisGroup!");
399
Chris Lattnerb3708e22002-08-30 20:23:45 +0000400 // Make sure we keep track of the fact that the implementation implements
401 // the interface.
402 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
403 IIPI->addInterfaceImplemented(InterfaceInfo);
404
Chris Lattner6e041bd2002-08-21 22:17:09 +0000405 // Lazily allocate to avoid nasty initialization order dependencies
406 if (AnalysisGroupInfoMap == 0)
407 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
408
409 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
410 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
411 "Cannot add a pass to the same analysis group more than once!");
412 AGI.Implementations.insert(ImplementationInfo);
413 if (isDefault) {
414 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
415 "Default implementation for analysis group already specified!");
416 assert(ImplementationInfo->getNormalCtor() &&
417 "Cannot specify pass as default if it does not have a default ctor");
418 AGI.DefaultImpl = ImplementationInfo;
419 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
420 }
421 }
422}
423
424void RegisterAGBase::setGroupName(const char *Name) {
425 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
426 InterfaceInfo->setPassName(Name);
427}
428
429RegisterAGBase::~RegisterAGBase() {
430 if (ImplementationInfo) {
431 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
432 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
433
434 assert(AGI.Implementations.count(ImplementationInfo) &&
435 "Pass not a member of analysis group?");
436
437 if (AGI.DefaultImpl == ImplementationInfo)
438 AGI.DefaultImpl = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000439
Chris Lattner6e041bd2002-08-21 22:17:09 +0000440 AGI.Implementations.erase(ImplementationInfo);
441
442 // Last member of this analysis group? Unregister PassInfo, delete map entry
443 if (AGI.Implementations.empty()) {
444 assert(AGI.DefaultImpl == 0 &&
445 "Default implementation didn't unregister?");
446 AnalysisGroupInfoMap->erase(InterfaceInfo);
447 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
448 delete AnalysisGroupInfoMap;
449 AnalysisGroupInfoMap = 0;
450 }
Chris Lattner6e041bd2002-08-21 22:17:09 +0000451 }
452 }
Chris Lattnerc66da832006-01-23 01:01:04 +0000453
454 if (InterfaceInfo == &PIObj)
455 unregisterPass();
Chris Lattner6e041bd2002-08-21 22:17:09 +0000456}
457
458
Chris Lattner37d3c952002-07-23 18:08:00 +0000459//===----------------------------------------------------------------------===//
460// PassRegistrationListener implementation
461//
462
463// PassRegistrationListener ctor - Add the current object to the list of
464// PassRegistrationListeners...
465PassRegistrationListener::PassRegistrationListener() {
466 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
467 Listeners->push_back(this);
468}
469
470// dtor - Remove object from list of listeners...
471PassRegistrationListener::~PassRegistrationListener() {
472 std::vector<PassRegistrationListener*>::iterator I =
473 std::find(Listeners->begin(), Listeners->end(), this);
474 assert(Listeners && I != Listeners->end() &&
475 "PassRegistrationListener not registered!");
476 Listeners->erase(I);
477
478 if (Listeners->empty()) {
479 delete Listeners;
480 Listeners = 0;
481 }
482}
483
484// enumeratePasses - Iterate over the registered passes, calling the
485// passEnumerate callback on each PassInfo object.
486//
487void PassRegistrationListener::enumeratePasses() {
488 if (PassInfoMap)
489 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
490 E = PassInfoMap->end(); I != E; ++I)
491 passEnumerate(I->second);
492}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000493