blob: 24f14f804bf23b767deaebb69d0ca51f99a5fe79 [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 Lattner37c86672002-04-28 20:46:05 +000014#include <typeinfo>
Chris Lattnere2eb99e2002-04-29 04:04:29 +000015#include <stdio.h>
Chris Lattner6a33d6f2002-08-01 19:33:09 +000016#include <sys/resource.h>
17#include <sys/unistd.h>
Chris Lattnerd013ba92002-01-23 05:49:41 +000018
Chris Lattner7e0dbe62002-05-06 19:31:52 +000019//===----------------------------------------------------------------------===//
20// AnalysisID Class Implementation
21//
22
Chris Lattner198cf422002-07-30 16:27:02 +000023static std::vector<const PassInfo*> CFGOnlyAnalyses;
Chris Lattnercdd09c22002-01-31 00:45:31 +000024
Chris Lattner198cf422002-07-30 16:27:02 +000025void RegisterPassBase::setPreservesCFG() {
26 CFGOnlyAnalyses.push_back(PIObj);
Chris Lattner7e0dbe62002-05-06 19:31:52 +000027}
28
29//===----------------------------------------------------------------------===//
30// AnalysisResolver Class Implementation
31//
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//===----------------------------------------------------------------------===//
39// AnalysisUsage Class Implementation
40//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000041
42// preservesCFG - This function should be called to by the pass, iff they do
43// not:
44//
45// 1. Add or remove basic blocks from the function
46// 2. Modify terminator instructions in any way.
47//
48// This function annotates the AnalysisUsage info object to say that analyses
49// that only depend on the CFG are preserved by this pass.
50//
51void AnalysisUsage::preservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000052 // Since this transformation doesn't modify the CFG, it preserves all analyses
53 // that only depend on the CFG (like dominators, loop info, etc...)
54 //
55 Preserved.insert(Preserved.end(),
56 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000057}
58
59
Chris Lattner37c86672002-04-28 20:46:05 +000060//===----------------------------------------------------------------------===//
61// PassManager implementation - The PassManager class is a simple Pimpl class
62// that wraps the PassManagerT template.
63//
64PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
65PassManager::~PassManager() { delete PM; }
66void PassManager::add(Pass *P) { PM->add(P); }
Chris Lattner113f4f42002-06-25 16:13:24 +000067bool PassManager::run(Module &M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000068
Chris Lattner37c86672002-04-28 20:46:05 +000069
70//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000071// TimingInfo Class - This class is used to calculate information about the
72// amount of time each pass takes to execute. This only happens with
73// -time-passes is enabled on the command line.
74//
Chris Lattnerf5cad152002-07-22 02:10:13 +000075static cl::opt<bool>
76EnableTiming("time-passes",
77 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000078
Chris Lattner6a33d6f2002-08-01 19:33:09 +000079static TimeRecord getTimeRecord() {
80 static unsigned long PageSize = 0;
81
82 if (PageSize == 0) {
83#ifdef _SC_PAGE_SIZE
84 PageSize = sysconf(_SC_PAGE_SIZE);
85#else
86#ifdef _SC_PAGESIZE
87 PageSize = sysconf(_SC_PAGESIZE);
88#else
89 PageSize = getpagesize();
90#endif
91#endif
92 }
93
94 struct rusage RU;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000095 struct timeval T;
96 gettimeofday(&T, 0);
Chris Lattner6a33d6f2002-08-01 19:33:09 +000097 if (getrusage(RUSAGE_SELF, &RU)) {
98 perror("getrusage call failed: -time-passes info incorrect!");
99 }
100
101 TimeRecord Result;
102 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
103 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
104 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
105 Result.MaxRSS = RU.ru_maxrss*PageSize;
106
107 return Result;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000108}
109
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000110void TimeRecord::passStart(const TimeRecord &T) {
111 Elapsed -= T.Elapsed;
112 UserTime -= T.UserTime;
113 SystemTime -= T.SystemTime;
114 RSSTemp = T.MaxRSS;
115}
116
117void TimeRecord::passEnd(const TimeRecord &T) {
118 Elapsed += T.Elapsed;
119 UserTime += T.UserTime;
120 SystemTime += T.SystemTime;
121 RSSTemp = T.MaxRSS - RSSTemp;
122 MaxRSS = std::max(MaxRSS, RSSTemp);
123}
124
Chris Lattner5ec216b2002-08-19 15:43:33 +0000125static void printVal(double Val, double Total) {
126 if (Total < 1e-7) // Avoid dividing by zero...
Chris Lattnerca5afe72002-08-19 20:42:12 +0000127 fprintf(stderr, " ----- ");
Chris Lattner5ec216b2002-08-19 15:43:33 +0000128 else
129 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
130}
131
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000132void TimeRecord::print(const char *PassName, const TimeRecord &Total) const {
Chris Lattner5ec216b2002-08-19 15:43:33 +0000133 printVal(UserTime, Total.UserTime);
134 printVal(SystemTime, Total.SystemTime);
135 printVal(UserTime+SystemTime, Total.UserTime+Total.SystemTime);
136 printVal(Elapsed, Total.Elapsed);
137
138 fprintf(stderr, " ");
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000139
140 if (Total.MaxRSS)
141 std::cerr << MaxRSS << "\t";
142 std::cerr << PassName << "\n";
143}
144
145
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000146// Create method. If Timing is enabled, this creates and returns a new timing
147// object, otherwise it returns null.
148//
149TimingInfo *TimingInfo::create() {
150 return EnableTiming ? new TimingInfo() : 0;
151}
152
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000153void TimingInfo::passStarted(Pass *P) {
154 TimingData[P].passStart(getTimeRecord());
155}
156void TimingInfo::passEnded(Pass *P) {
157 TimingData[P].passEnd(getTimeRecord());
158}
159void TimeRecord::sum(const TimeRecord &TR) {
160 Elapsed += TR.Elapsed;
161 UserTime += TR.UserTime;
162 SystemTime += TR.SystemTime;
163 MaxRSS += TR.MaxRSS;
164}
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000165
166// TimingDtor - Print out information about timing information
167TimingInfo::~TimingInfo() {
168 // Iterate over all of the data, converting it into the dual of the data map,
169 // so that the data is sorted by amount of time taken, instead of pointer.
170 //
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000171 std::vector<std::pair<TimeRecord, Pass*> > Data;
172 TimeRecord Total;
173 for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(),
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000174 E = TimingData.end(); I != E; ++I)
175 // Throw out results for "grouping" pass managers...
176 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
177 Data.push_back(std::make_pair(I->second, I->first));
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000178 Total.sum(I->second);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000179 }
180
181 // Sort the data by time as the primary key, in reverse order...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000182 std::sort(Data.begin(), Data.end(),
183 std::greater<std::pair<TimeRecord, Pass*> >());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000184
185 // Print out timing header...
Anand Shukla8c377892002-06-25 22:07:38 +0000186 std::cerr << std::string(79, '=') << "\n"
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000187 << " ... Pass execution timing report ...\n"
188 << std::string(79, '=') << "\n Total Execution Time: "
189 << (Total.UserTime+Total.SystemTime) << " seconds ("
190 << Total.Elapsed << " wall clock)\n\n ---User Time--- "
191 << "--System Time-- --User+System-- ---Wall Time---";
192
193 if (Total.MaxRSS)
194 std::cerr << " ---Mem---";
195 std::cerr << " --- Pass Name ---\n";
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000196
197 // Loop through all of the timing data, printing it out...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000198 for (unsigned i = 0, e = Data.size(); i != e; ++i)
199 Data[i].first.print(Data[i].second->getPassName(), Total);
200
201 Total.print("TOTAL", Total);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000202}
203
204
Chris Lattner1e4867f2002-07-30 19:51:02 +0000205void PMDebug::PrintArgumentInformation(const Pass *P) {
206 // Print out passes in pass manager...
207 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
208 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
209 PrintArgumentInformation(PM->getContainedPass(i));
210
211 } else { // Normal pass. Print argument information...
212 // Print out arguments for registered passes that are _optimizations_
213 if (const PassInfo *PI = P->getPassInfo())
214 if (PI->getPassType() & PassInfo::Optimization)
215 std::cerr << " -" << PI->getPassArgument();
216 }
217}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000218
219void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000220 Pass *P, Annotable *V) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000221 if (PassDebugging >= Executions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000222 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000223 << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000224 if (V) {
225 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000226
227 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000228 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000229 } else if (Function *F = dynamic_cast<Function*>(V))
230 std::cerr << "Function '" << F->getName();
231 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
232 std::cerr << "BasicBlock '" << BB->getName();
233 else if (Value *Val = dynamic_cast<Value*>(V))
234 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000235 }
236 std::cerr << "'...\n";
237 }
238}
239
240void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000241 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000242 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000243 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattner198cf422002-07-30 16:27:02 +0000244 for (unsigned i = 0; i != Set.size(); ++i)
245 std::cerr << " " << Set[i]->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000246 std::cerr << "\n";
247 }
248}
249
Chris Lattnercdd09c22002-01-31 00:45:31 +0000250//===----------------------------------------------------------------------===//
251// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000252//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000253
Chris Lattnerc8e66542002-04-27 06:56:12 +0000254void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
255 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000256}
Chris Lattner26e4f892002-01-21 07:37:31 +0000257
Chris Lattner198cf422002-07-30 16:27:02 +0000258// dumpPassStructure - Implement the -debug-passes=Structure option
259void Pass::dumpPassStructure(unsigned Offset) {
260 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
261}
Chris Lattner37104aa2002-04-29 14:57:45 +0000262
263// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
264//
Chris Lattner071577d2002-07-29 21:02:31 +0000265const char *Pass::getPassName() const {
266 if (const PassInfo *PI = getPassInfo())
267 return PI->getPassName();
268 return typeid(*this).name();
269}
Chris Lattner37104aa2002-04-29 14:57:45 +0000270
Chris Lattner26750072002-07-27 01:12:17 +0000271// print - Print out the internal state of the pass. This is called by Analyse
272// to print out the contents of an analysis. Otherwise it is not neccesary to
273// implement this method.
274//
275void Pass::print(std::ostream &O) const {
276 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
277}
278
279// dump - call print(std::cerr);
280void Pass::dump() const {
281 print(std::cerr, 0);
282}
283
Chris Lattnercdd09c22002-01-31 00:45:31 +0000284//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000285// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000286//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000287
Chris Lattnerc8e66542002-04-27 06:56:12 +0000288// run - On a module, we run this pass by initializing, runOnFunction'ing once
289// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000290//
Chris Lattner113f4f42002-06-25 16:13:24 +0000291bool FunctionPass::run(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000292 bool Changed = doInitialization(M);
293
Chris Lattner113f4f42002-06-25 16:13:24 +0000294 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
295 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000296 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000297
298 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000299}
300
Chris Lattnerc8e66542002-04-27 06:56:12 +0000301// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000302//
Chris Lattner113f4f42002-06-25 16:13:24 +0000303bool FunctionPass::run(Function &F) {
304 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000305
Chris Lattner113f4f42002-06-25 16:13:24 +0000306 return doInitialization(*F.getParent()) | runOnFunction(F)
307 | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000308}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000309
Chris Lattnerc8e66542002-04-27 06:56:12 +0000310void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
311 AnalysisUsage &AU) {
312 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000313}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000314
Chris Lattnerc8e66542002-04-27 06:56:12 +0000315void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
316 AnalysisUsage &AU) {
317 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000318}
319
320//===----------------------------------------------------------------------===//
321// BasicBlockPass Implementation
322//
323
Chris Lattnerc8e66542002-04-27 06:56:12 +0000324// To run this pass on a function, we simply call runOnBasicBlock once for each
325// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000326//
Chris Lattner113f4f42002-06-25 16:13:24 +0000327bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000328 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000329 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000330 Changed |= runOnBasicBlock(*I);
331 return Changed;
332}
333
334// To run directly on the basic block, we initialize, runOnBasicBlock, then
335// finalize.
336//
Chris Lattner113f4f42002-06-25 16:13:24 +0000337bool BasicBlockPass::run(BasicBlock &BB) {
338 Module &M = *BB.getParent()->getParent();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000339 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
340}
341
Chris Lattner57698e22002-03-26 18:01:55 +0000342void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000343 AnalysisUsage &AU) {
344 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000345}
346
347void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000348 AnalysisUsage &AU) {
349 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000350}
351
Chris Lattner37d3c952002-07-23 18:08:00 +0000352
353//===----------------------------------------------------------------------===//
354// Pass Registration mechanism
355//
356static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
357static std::vector<PassRegistrationListener*> *Listeners = 0;
358
359// getPassInfo - Return the PassInfo data structure that corresponds to this
360// pass...
361const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000362 if (PassInfoCache) return PassInfoCache;
363 if (PassInfoMap == 0) return 0;
364 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(typeid(*this));
365 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000366}
367
368void RegisterPassBase::registerPass(PassInfo *PI) {
369 if (PassInfoMap == 0)
370 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
371
372 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
373 "Pass already registered!");
374 PIObj = PI;
375 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
376
377 // Notify any listeners...
378 if (Listeners)
379 for (std::vector<PassRegistrationListener*>::iterator
380 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
381 (*I)->passRegistered(PI);
382}
383
384RegisterPassBase::~RegisterPassBase() {
385 assert(PassInfoMap && "Pass registered but not in map!");
386 std::map<TypeInfo, PassInfo*>::iterator I =
387 PassInfoMap->find(PIObj->getTypeInfo());
388 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
389
390 // Remove pass from the map...
391 PassInfoMap->erase(I);
392 if (PassInfoMap->empty()) {
393 delete PassInfoMap;
394 PassInfoMap = 0;
395 }
396
397 // Notify any listeners...
398 if (Listeners)
399 for (std::vector<PassRegistrationListener*>::iterator
400 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
401 (*I)->passUnregistered(PIObj);
402
403 // Delete the PassInfo object itself...
404 delete PIObj;
405}
406
407
408
409//===----------------------------------------------------------------------===//
410// PassRegistrationListener implementation
411//
412
413// PassRegistrationListener ctor - Add the current object to the list of
414// PassRegistrationListeners...
415PassRegistrationListener::PassRegistrationListener() {
416 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
417 Listeners->push_back(this);
418}
419
420// dtor - Remove object from list of listeners...
421PassRegistrationListener::~PassRegistrationListener() {
422 std::vector<PassRegistrationListener*>::iterator I =
423 std::find(Listeners->begin(), Listeners->end(), this);
424 assert(Listeners && I != Listeners->end() &&
425 "PassRegistrationListener not registered!");
426 Listeners->erase(I);
427
428 if (Listeners->empty()) {
429 delete Listeners;
430 Listeners = 0;
431 }
432}
433
434// enumeratePasses - Iterate over the registered passes, calling the
435// passEnumerate callback on each PassInfo object.
436//
437void PassRegistrationListener::enumeratePasses() {
438 if (PassInfoMap)
439 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
440 E = PassInfoMap->end(); I != E; ++I)
441 passEnumerate(I->second);
442}