blob: 89b719c0dd89c5c24ee0dcbf737d52857fb250a6 [file] [log] [blame]
Chris Lattner26e4f892002-01-21 07:37:31 +00001//===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
2//
3// This file implements the LLVM Pass infrastructure. It is primarily
4// responsible with ensuring that passes are executed and batched together
5// optimally.
6//
7//===----------------------------------------------------------------------===//
8
Chris Lattnercdd09c22002-01-31 00:45:31 +00009#include "llvm/PassManager.h"
Chris Lattner37c86672002-04-28 20:46:05 +000010#include "PassManagerT.h" // PassManagerT implementation
Chris Lattnercdd09c22002-01-31 00:45:31 +000011#include "llvm/Module.h"
Chris Lattner26e4f892002-01-21 07:37:31 +000012#include "Support/STLExtras.h"
Chris Lattner37d3c952002-07-23 18:08:00 +000013#include "Support/TypeInfo.h"
Chris Lattnere2eb99e2002-04-29 04:04:29 +000014#include <stdio.h>
Chris Lattner6a33d6f2002-08-01 19:33:09 +000015#include <sys/resource.h>
Chris Lattner548002f2002-09-13 14:47:12 +000016#include <sys/time.h>
Chris Lattner6a33d6f2002-08-01 19:33:09 +000017#include <sys/unistd.h>
Chris Lattner6e041bd2002-08-21 22:17:09 +000018#include <set>
Chris Lattnerd013ba92002-01-23 05:49:41 +000019
Chris Lattner675e7a92002-08-21 23:51:51 +000020// IncludeFile - Stub function used to help linking out.
21IncludeFile::IncludeFile(void*) {}
22
Chris Lattner7e0dbe62002-05-06 19:31:52 +000023//===----------------------------------------------------------------------===//
24// AnalysisID Class Implementation
25//
26
Chris Lattner198cf422002-07-30 16:27:02 +000027static std::vector<const PassInfo*> CFGOnlyAnalyses;
Chris Lattnercdd09c22002-01-31 00:45:31 +000028
Chris Lattner198cf422002-07-30 16:27:02 +000029void RegisterPassBase::setPreservesCFG() {
30 CFGOnlyAnalyses.push_back(PIObj);
Chris Lattner7e0dbe62002-05-06 19:31:52 +000031}
32
33//===----------------------------------------------------------------------===//
34// AnalysisResolver Class Implementation
35//
36
Chris Lattnercdd09c22002-01-31 00:45:31 +000037void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
38 assert(P->Resolver == 0 && "Pass already in a PassManager!");
39 P->Resolver = AR;
40}
41
Chris Lattner7e0dbe62002-05-06 19:31:52 +000042//===----------------------------------------------------------------------===//
43// AnalysisUsage Class Implementation
44//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000045
46// preservesCFG - This function should be called to by the pass, iff they do
47// not:
48//
49// 1. Add or remove basic blocks from the function
50// 2. Modify terminator instructions in any way.
51//
52// This function annotates the AnalysisUsage info object to say that analyses
53// that only depend on the CFG are preserved by this pass.
54//
55void AnalysisUsage::preservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000056 // Since this transformation doesn't modify the CFG, it preserves all analyses
57 // that only depend on the CFG (like dominators, loop info, etc...)
58 //
59 Preserved.insert(Preserved.end(),
60 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000061}
62
63
Chris Lattner37c86672002-04-28 20:46:05 +000064//===----------------------------------------------------------------------===//
65// PassManager implementation - The PassManager class is a simple Pimpl class
66// that wraps the PassManagerT template.
67//
68PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
69PassManager::~PassManager() { delete PM; }
70void PassManager::add(Pass *P) { PM->add(P); }
Chris Lattner113f4f42002-06-25 16:13:24 +000071bool PassManager::run(Module &M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000072
Chris Lattner37c86672002-04-28 20:46:05 +000073
74//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000075// TimingInfo Class - This class is used to calculate information about the
76// amount of time each pass takes to execute. This only happens with
77// -time-passes is enabled on the command line.
78//
Chris Lattnerf5cad152002-07-22 02:10:13 +000079static cl::opt<bool>
80EnableTiming("time-passes",
81 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000082
Chris Lattner6a33d6f2002-08-01 19:33:09 +000083static TimeRecord getTimeRecord() {
84 static unsigned long PageSize = 0;
85
86 if (PageSize == 0) {
87#ifdef _SC_PAGE_SIZE
88 PageSize = sysconf(_SC_PAGE_SIZE);
89#else
90#ifdef _SC_PAGESIZE
91 PageSize = sysconf(_SC_PAGESIZE);
92#else
93 PageSize = getpagesize();
94#endif
95#endif
96 }
97
98 struct rusage RU;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000099 struct timeval T;
100 gettimeofday(&T, 0);
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000101 if (getrusage(RUSAGE_SELF, &RU)) {
102 perror("getrusage call failed: -time-passes info incorrect!");
103 }
104
105 TimeRecord Result;
106 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
107 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
108 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
109 Result.MaxRSS = RU.ru_maxrss*PageSize;
110
111 return Result;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000112}
113
Chris Lattnere821d782002-08-20 18:47:53 +0000114bool TimeRecord::operator<(const TimeRecord &TR) const {
115 // Primary sort key is User+System time
116 if (UserTime+SystemTime < TR.UserTime+TR.SystemTime)
117 return true;
118 if (UserTime+SystemTime > TR.UserTime+TR.SystemTime)
119 return false;
120
121 // Secondary sort key is Wall Time
122 return Elapsed < TR.Elapsed;
123}
124
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000125void TimeRecord::passStart(const TimeRecord &T) {
126 Elapsed -= T.Elapsed;
127 UserTime -= T.UserTime;
128 SystemTime -= T.SystemTime;
129 RSSTemp = T.MaxRSS;
130}
131
132void TimeRecord::passEnd(const TimeRecord &T) {
133 Elapsed += T.Elapsed;
134 UserTime += T.UserTime;
135 SystemTime += T.SystemTime;
136 RSSTemp = T.MaxRSS - RSSTemp;
137 MaxRSS = std::max(MaxRSS, RSSTemp);
138}
139
Chris Lattner5ec216b2002-08-19 15:43:33 +0000140static void printVal(double Val, double Total) {
141 if (Total < 1e-7) // Avoid dividing by zero...
Chris Lattnerca5afe72002-08-19 20:42:12 +0000142 fprintf(stderr, " ----- ");
Chris Lattner5ec216b2002-08-19 15:43:33 +0000143 else
144 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
145}
146
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000147void TimeRecord::print(const char *PassName, const TimeRecord &Total) const {
Chris Lattner5ec216b2002-08-19 15:43:33 +0000148 printVal(UserTime, Total.UserTime);
149 printVal(SystemTime, Total.SystemTime);
150 printVal(UserTime+SystemTime, Total.UserTime+Total.SystemTime);
151 printVal(Elapsed, Total.Elapsed);
152
153 fprintf(stderr, " ");
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000154
155 if (Total.MaxRSS)
156 std::cerr << MaxRSS << "\t";
157 std::cerr << PassName << "\n";
158}
159
160
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000161// Create method. If Timing is enabled, this creates and returns a new timing
162// object, otherwise it returns null.
163//
164TimingInfo *TimingInfo::create() {
165 return EnableTiming ? new TimingInfo() : 0;
166}
167
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000168void TimingInfo::passStarted(Pass *P) {
169 TimingData[P].passStart(getTimeRecord());
170}
171void TimingInfo::passEnded(Pass *P) {
172 TimingData[P].passEnd(getTimeRecord());
173}
174void TimeRecord::sum(const TimeRecord &TR) {
175 Elapsed += TR.Elapsed;
176 UserTime += TR.UserTime;
177 SystemTime += TR.SystemTime;
178 MaxRSS += TR.MaxRSS;
179}
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000180
181// TimingDtor - Print out information about timing information
182TimingInfo::~TimingInfo() {
183 // Iterate over all of the data, converting it into the dual of the data map,
184 // so that the data is sorted by amount of time taken, instead of pointer.
185 //
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000186 std::vector<std::pair<TimeRecord, Pass*> > Data;
187 TimeRecord Total;
188 for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(),
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000189 E = TimingData.end(); I != E; ++I)
190 // Throw out results for "grouping" pass managers...
191 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
192 Data.push_back(std::make_pair(I->second, I->first));
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000193 Total.sum(I->second);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000194 }
195
196 // Sort the data by time as the primary key, in reverse order...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000197 std::sort(Data.begin(), Data.end(),
198 std::greater<std::pair<TimeRecord, Pass*> >());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000199
200 // Print out timing header...
Anand Shukla8c377892002-06-25 22:07:38 +0000201 std::cerr << std::string(79, '=') << "\n"
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000202 << " ... Pass execution timing report ...\n"
203 << std::string(79, '=') << "\n Total Execution Time: "
204 << (Total.UserTime+Total.SystemTime) << " seconds ("
205 << Total.Elapsed << " wall clock)\n\n ---User Time--- "
206 << "--System Time-- --User+System-- ---Wall Time---";
207
208 if (Total.MaxRSS)
209 std::cerr << " ---Mem---";
210 std::cerr << " --- Pass Name ---\n";
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000211
212 // Loop through all of the timing data, printing it out...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000213 for (unsigned i = 0, e = Data.size(); i != e; ++i)
214 Data[i].first.print(Data[i].second->getPassName(), Total);
215
216 Total.print("TOTAL", Total);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000217}
218
219
Chris Lattner1e4867f2002-07-30 19:51:02 +0000220void PMDebug::PrintArgumentInformation(const Pass *P) {
221 // Print out passes in pass manager...
222 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
223 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
224 PrintArgumentInformation(PM->getContainedPass(i));
225
226 } else { // Normal pass. Print argument information...
227 // Print out arguments for registered passes that are _optimizations_
228 if (const PassInfo *PI = P->getPassInfo())
229 if (PI->getPassType() & PassInfo::Optimization)
230 std::cerr << " -" << PI->getPassArgument();
231 }
232}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000233
234void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000235 Pass *P, Annotable *V) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000236 if (PassDebugging >= Executions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000237 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000238 << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000239 if (V) {
240 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000241
242 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000243 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000244 } else if (Function *F = dynamic_cast<Function*>(V))
245 std::cerr << "Function '" << F->getName();
246 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
247 std::cerr << "BasicBlock '" << BB->getName();
248 else if (Value *Val = dynamic_cast<Value*>(V))
249 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000250 }
251 std::cerr << "'...\n";
252 }
253}
254
255void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000256 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000257 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000258 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerb3708e22002-08-30 20:23:45 +0000259 for (unsigned i = 0; i != Set.size(); ++i) {
260 if (i) std::cerr << ",";
261 std::cerr << " " << Set[i]->getPassName();
262 }
Chris Lattnercdd09c22002-01-31 00:45:31 +0000263 std::cerr << "\n";
264 }
265}
266
Chris Lattnercdd09c22002-01-31 00:45:31 +0000267//===----------------------------------------------------------------------===//
268// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000269//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000270
Chris Lattnerc8e66542002-04-27 06:56:12 +0000271void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
272 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000273}
Chris Lattner26e4f892002-01-21 07:37:31 +0000274
Chris Lattner198cf422002-07-30 16:27:02 +0000275// dumpPassStructure - Implement the -debug-passes=Structure option
276void Pass::dumpPassStructure(unsigned Offset) {
277 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
278}
Chris Lattner37104aa2002-04-29 14:57:45 +0000279
280// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
281//
Chris Lattner071577d2002-07-29 21:02:31 +0000282const char *Pass::getPassName() const {
283 if (const PassInfo *PI = getPassInfo())
284 return PI->getPassName();
285 return typeid(*this).name();
286}
Chris Lattner37104aa2002-04-29 14:57:45 +0000287
Chris Lattner26750072002-07-27 01:12:17 +0000288// print - Print out the internal state of the pass. This is called by Analyse
289// to print out the contents of an analysis. Otherwise it is not neccesary to
290// implement this method.
291//
292void Pass::print(std::ostream &O) const {
293 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
294}
295
296// dump - call print(std::cerr);
297void Pass::dump() const {
298 print(std::cerr, 0);
299}
300
Chris Lattnercdd09c22002-01-31 00:45:31 +0000301//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000302// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000303//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000304
Chris Lattnerc8e66542002-04-27 06:56:12 +0000305// run - On a module, we run this pass by initializing, runOnFunction'ing once
306// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000307//
Chris Lattner113f4f42002-06-25 16:13:24 +0000308bool FunctionPass::run(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000309 bool Changed = doInitialization(M);
310
Chris Lattner113f4f42002-06-25 16:13:24 +0000311 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
312 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000313 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000314
315 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000316}
317
Chris Lattnerc8e66542002-04-27 06:56:12 +0000318// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000319//
Chris Lattner113f4f42002-06-25 16:13:24 +0000320bool FunctionPass::run(Function &F) {
321 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000322
Chris Lattner113f4f42002-06-25 16:13:24 +0000323 return doInitialization(*F.getParent()) | runOnFunction(F)
324 | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000325}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000326
Chris Lattnerc8e66542002-04-27 06:56:12 +0000327void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
328 AnalysisUsage &AU) {
329 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000330}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000331
Chris Lattnerc8e66542002-04-27 06:56:12 +0000332void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
333 AnalysisUsage &AU) {
334 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000335}
336
337//===----------------------------------------------------------------------===//
338// BasicBlockPass Implementation
339//
340
Chris Lattnerc8e66542002-04-27 06:56:12 +0000341// To run this pass on a function, we simply call runOnBasicBlock once for each
342// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000343//
Chris Lattner113f4f42002-06-25 16:13:24 +0000344bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000345 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000346 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000347 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000348 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000349}
350
351// To run directly on the basic block, we initialize, runOnBasicBlock, then
352// finalize.
353//
Chris Lattner113f4f42002-06-25 16:13:24 +0000354bool BasicBlockPass::run(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000355 Function &F = *BB.getParent();
356 Module &M = *F.getParent();
357 return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) |
358 doFinalization(F) | doFinalization(M);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000359}
360
Chris Lattner57698e22002-03-26 18:01:55 +0000361void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000362 AnalysisUsage &AU) {
363 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000364}
365
366void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000367 AnalysisUsage &AU) {
368 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000369}
370
Chris Lattner37d3c952002-07-23 18:08:00 +0000371
372//===----------------------------------------------------------------------===//
373// Pass Registration mechanism
374//
375static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
376static std::vector<PassRegistrationListener*> *Listeners = 0;
377
378// getPassInfo - Return the PassInfo data structure that corresponds to this
379// pass...
380const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000381 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000382 return lookupPassInfo(typeid(*this));
383}
384
385const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner071577d2002-07-29 21:02:31 +0000386 if (PassInfoMap == 0) return 0;
Chris Lattner4b169632002-08-21 17:08:37 +0000387 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
Chris Lattner071577d2002-07-29 21:02:31 +0000388 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000389}
390
391void RegisterPassBase::registerPass(PassInfo *PI) {
392 if (PassInfoMap == 0)
393 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
394
395 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
396 "Pass already registered!");
397 PIObj = PI;
398 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
399
400 // Notify any listeners...
401 if (Listeners)
402 for (std::vector<PassRegistrationListener*>::iterator
403 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
404 (*I)->passRegistered(PI);
405}
406
Chris Lattner6e041bd2002-08-21 22:17:09 +0000407void RegisterPassBase::unregisterPass(PassInfo *PI) {
Chris Lattner37d3c952002-07-23 18:08:00 +0000408 assert(PassInfoMap && "Pass registered but not in map!");
409 std::map<TypeInfo, PassInfo*>::iterator I =
Chris Lattner6e041bd2002-08-21 22:17:09 +0000410 PassInfoMap->find(PI->getTypeInfo());
Chris Lattner37d3c952002-07-23 18:08:00 +0000411 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
412
413 // Remove pass from the map...
414 PassInfoMap->erase(I);
415 if (PassInfoMap->empty()) {
416 delete PassInfoMap;
417 PassInfoMap = 0;
418 }
419
420 // Notify any listeners...
421 if (Listeners)
422 for (std::vector<PassRegistrationListener*>::iterator
423 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattner6e041bd2002-08-21 22:17:09 +0000424 (*I)->passUnregistered(PI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000425
426 // Delete the PassInfo object itself...
Chris Lattner6e041bd2002-08-21 22:17:09 +0000427 delete PI;
Chris Lattner37d3c952002-07-23 18:08:00 +0000428}
429
Chris Lattner6e041bd2002-08-21 22:17:09 +0000430//===----------------------------------------------------------------------===//
431// Analysis Group Implementation Code
432//===----------------------------------------------------------------------===//
433
434struct AnalysisGroupInfo {
435 const PassInfo *DefaultImpl;
436 std::set<const PassInfo *> Implementations;
437 AnalysisGroupInfo() : DefaultImpl(0) {}
438};
439
440static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
441
442// RegisterAGBase implementation
443//
444RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
445 const std::type_info *Pass, bool isDefault)
446 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
447
Chris Lattner6e041bd2002-08-21 22:17:09 +0000448 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
449 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
450 InterfaceInfo = // Create the new PassInfo for the interface...
451 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
452 registerPass(InterfaceInfo);
453 PIObj = 0;
454 }
455 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
456 "Trying to join an analysis group that is a normal pass!");
457
458 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000459 ImplementationInfo = Pass::lookupPassInfo(*Pass);
460 assert(ImplementationInfo &&
461 "Must register pass before adding to AnalysisGroup!");
462
Chris Lattnerb3708e22002-08-30 20:23:45 +0000463 // Make sure we keep track of the fact that the implementation implements
464 // the interface.
465 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
466 IIPI->addInterfaceImplemented(InterfaceInfo);
467
Chris Lattner6e041bd2002-08-21 22:17:09 +0000468 // Lazily allocate to avoid nasty initialization order dependencies
469 if (AnalysisGroupInfoMap == 0)
470 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
471
472 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
473 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
474 "Cannot add a pass to the same analysis group more than once!");
475 AGI.Implementations.insert(ImplementationInfo);
476 if (isDefault) {
477 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
478 "Default implementation for analysis group already specified!");
479 assert(ImplementationInfo->getNormalCtor() &&
480 "Cannot specify pass as default if it does not have a default ctor");
481 AGI.DefaultImpl = ImplementationInfo;
482 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
483 }
484 }
485}
486
487void RegisterAGBase::setGroupName(const char *Name) {
488 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
489 InterfaceInfo->setPassName(Name);
490}
491
492RegisterAGBase::~RegisterAGBase() {
493 if (ImplementationInfo) {
494 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
495 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
496
497 assert(AGI.Implementations.count(ImplementationInfo) &&
498 "Pass not a member of analysis group?");
499
500 if (AGI.DefaultImpl == ImplementationInfo)
501 AGI.DefaultImpl = 0;
502
503 AGI.Implementations.erase(ImplementationInfo);
504
505 // Last member of this analysis group? Unregister PassInfo, delete map entry
506 if (AGI.Implementations.empty()) {
507 assert(AGI.DefaultImpl == 0 &&
508 "Default implementation didn't unregister?");
509 AnalysisGroupInfoMap->erase(InterfaceInfo);
510 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
511 delete AnalysisGroupInfoMap;
512 AnalysisGroupInfoMap = 0;
513 }
514
515 unregisterPass(InterfaceInfo);
516 }
517 }
518}
519
520
Chris Lattner37d3c952002-07-23 18:08:00 +0000521//===----------------------------------------------------------------------===//
522// PassRegistrationListener implementation
523//
524
525// PassRegistrationListener ctor - Add the current object to the list of
526// PassRegistrationListeners...
527PassRegistrationListener::PassRegistrationListener() {
528 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
529 Listeners->push_back(this);
530}
531
532// dtor - Remove object from list of listeners...
533PassRegistrationListener::~PassRegistrationListener() {
534 std::vector<PassRegistrationListener*>::iterator I =
535 std::find(Listeners->begin(), Listeners->end(), this);
536 assert(Listeners && I != Listeners->end() &&
537 "PassRegistrationListener not registered!");
538 Listeners->erase(I);
539
540 if (Listeners->empty()) {
541 delete Listeners;
542 Listeners = 0;
543 }
544}
545
546// enumeratePasses - Iterate over the registered passes, calling the
547// passEnumerate callback on each PassInfo object.
548//
549void PassRegistrationListener::enumeratePasses() {
550 if (PassInfoMap)
551 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
552 E = PassInfoMap->end(); I != E; ++I)
553 passEnumerate(I->second);
554}