blob: 06e77e7fe16ba0d305eeccd0c0ac02b8b95e3384 [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 Lattner37c86672002-04-28 20:46:05 +000013#include "Support/CommandLine.h"
Chris Lattner37d3c952002-07-23 18:08:00 +000014#include "Support/TypeInfo.h"
Chris Lattner37c86672002-04-28 20:46:05 +000015#include <typeinfo>
16#include <iostream>
Chris Lattnere2eb99e2002-04-29 04:04:29 +000017#include <sys/time.h>
18#include <stdio.h>
Chris Lattnerd013ba92002-01-23 05:49:41 +000019
Chris Lattner7e0dbe62002-05-06 19:31:52 +000020//===----------------------------------------------------------------------===//
21// AnalysisID Class Implementation
22//
23
24static std::vector<AnalysisID> CFGOnlyAnalyses;
Chris Lattner26750072002-07-27 01:12:17 +000025#if 0
Chris Lattnercdd09c22002-01-31 00:45:31 +000026// Source of unique analysis ID #'s.
27unsigned AnalysisID::NextID = 0;
28
Chris Lattner7e0dbe62002-05-06 19:31:52 +000029AnalysisID::AnalysisID(const AnalysisID &AID, bool DependsOnlyOnCFG) {
30 ID = AID.ID; // Implement the copy ctor part...
31 Constructor = AID.Constructor;
32
33 // If this analysis only depends on the CFG of the function, add it to the CFG
34 // only list...
35 if (DependsOnlyOnCFG)
36 CFGOnlyAnalyses.push_back(AID);
37}
Chris Lattner26750072002-07-27 01:12:17 +000038#endif
Chris Lattner7e0dbe62002-05-06 19:31:52 +000039
40//===----------------------------------------------------------------------===//
41// AnalysisResolver Class Implementation
42//
43
Chris Lattnercdd09c22002-01-31 00:45:31 +000044void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
45 assert(P->Resolver == 0 && "Pass already in a PassManager!");
46 P->Resolver = AR;
47}
48
Chris Lattner7e0dbe62002-05-06 19:31:52 +000049//===----------------------------------------------------------------------===//
50// AnalysisUsage Class Implementation
51//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000052
53// preservesCFG - This function should be called to by the pass, iff they do
54// not:
55//
56// 1. Add or remove basic blocks from the function
57// 2. Modify terminator instructions in any way.
58//
59// This function annotates the AnalysisUsage info object to say that analyses
60// that only depend on the CFG are preserved by this pass.
61//
62void AnalysisUsage::preservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000063 // Since this transformation doesn't modify the CFG, it preserves all analyses
64 // that only depend on the CFG (like dominators, loop info, etc...)
65 //
66 Preserved.insert(Preserved.end(),
67 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000068}
69
70
Chris Lattner37c86672002-04-28 20:46:05 +000071//===----------------------------------------------------------------------===//
72// PassManager implementation - The PassManager class is a simple Pimpl class
73// that wraps the PassManagerT template.
74//
75PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
76PassManager::~PassManager() { delete PM; }
77void PassManager::add(Pass *P) { PM->add(P); }
Chris Lattner113f4f42002-06-25 16:13:24 +000078bool PassManager::run(Module &M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000079
Chris Lattner37c86672002-04-28 20:46:05 +000080
81//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000082// TimingInfo Class - This class is used to calculate information about the
83// amount of time each pass takes to execute. This only happens with
84// -time-passes is enabled on the command line.
85//
Chris Lattnerf5cad152002-07-22 02:10:13 +000086static cl::opt<bool>
87EnableTiming("time-passes",
88 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000089
90static double getTime() {
91 struct timeval T;
92 gettimeofday(&T, 0);
93 return T.tv_sec + T.tv_usec/1000000.0;
94}
95
96// Create method. If Timing is enabled, this creates and returns a new timing
97// object, otherwise it returns null.
98//
99TimingInfo *TimingInfo::create() {
100 return EnableTiming ? new TimingInfo() : 0;
101}
102
103void TimingInfo::passStarted(Pass *P) { TimingData[P] -= getTime(); }
104void TimingInfo::passEnded(Pass *P) { TimingData[P] += getTime(); }
105
106// TimingDtor - Print out information about timing information
107TimingInfo::~TimingInfo() {
108 // Iterate over all of the data, converting it into the dual of the data map,
109 // so that the data is sorted by amount of time taken, instead of pointer.
110 //
Anand Shukla8c377892002-06-25 22:07:38 +0000111 std::vector<std::pair<double, Pass*> > Data;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000112 double TotalTime = 0;
113 for (std::map<Pass*, double>::iterator I = TimingData.begin(),
114 E = TimingData.end(); I != E; ++I)
115 // Throw out results for "grouping" pass managers...
116 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
117 Data.push_back(std::make_pair(I->second, I->first));
118 TotalTime += I->second;
119 }
120
121 // Sort the data by time as the primary key, in reverse order...
Anand Shukla8c377892002-06-25 22:07:38 +0000122 std::sort(Data.begin(), Data.end(), std::greater<std::pair<double, Pass*> >());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000123
124 // Print out timing header...
Anand Shukla8c377892002-06-25 22:07:38 +0000125 std::cerr << std::string(79, '=') << "\n"
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000126 << " ... Pass execution timing report ...\n"
127 << std::string(79, '=') << "\n Total Execution Time: " << TotalTime
Chris Lattner37104aa2002-04-29 14:57:45 +0000128 << " seconds\n\n % Time: Seconds:\tPass Name:\n";
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000129
130 // Loop through all of the timing data, printing it out...
131 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
132 fprintf(stderr, " %6.2f%% %fs\t%s\n", Data[i].first*100 / TotalTime,
Chris Lattner37104aa2002-04-29 14:57:45 +0000133 Data[i].first, Data[i].second->getPassName());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000134 }
Anand Shukla8c377892002-06-25 22:07:38 +0000135 std::cerr << " 100.00% " << TotalTime << "s\tTOTAL\n"
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000136 << std::string(79, '=') << "\n";
137}
138
139
140//===----------------------------------------------------------------------===//
Chris Lattnerd013ba92002-01-23 05:49:41 +0000141// Pass debugging information. Often it is useful to find out what pass is
142// running when a crash occurs in a utility. When this library is compiled with
143// debugging on, a command line option (--debug-pass) is enabled that causes the
144// pass name to be printed before it executes.
145//
Chris Lattnerd013ba92002-01-23 05:49:41 +0000146
Chris Lattnercdd09c22002-01-31 00:45:31 +0000147// Different debug levels that can be enabled...
148enum PassDebugLevel {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000149 None, Structure, Executions, Details
Chris Lattnercdd09c22002-01-31 00:45:31 +0000150};
Chris Lattnerd013ba92002-01-23 05:49:41 +0000151
Chris Lattnerf5cad152002-07-22 02:10:13 +0000152static cl::opt<enum PassDebugLevel>
153PassDebugging("debug-pass", cl::Hidden,
154 cl::desc("Print PassManager debugging information"),
155 cl::values(
156 clEnumVal(None , "disable debug output"),
157 // TODO: add option to print out pass names "PassOptions"
158 clEnumVal(Structure , "print pass structure before run()"),
159 clEnumVal(Executions, "print pass name before it is executed"),
160 clEnumVal(Details , "print pass details when it is executed"),
161 0));
Chris Lattnercdd09c22002-01-31 00:45:31 +0000162
163void PMDebug::PrintPassStructure(Pass *P) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000164 if (PassDebugging >= Structure)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000165 P->dumpPassStructure();
166}
167
168void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000169 Pass *P, Annotable *V) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000170 if (PassDebugging >= Executions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000171 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000172 << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000173 if (V) {
174 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000175
176 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000177 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000178 } else if (Function *F = dynamic_cast<Function*>(V))
179 std::cerr << "Function '" << F->getName();
180 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
181 std::cerr << "BasicBlock '" << BB->getName();
182 else if (Value *Val = dynamic_cast<Value*>(V))
183 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000184 }
185 std::cerr << "'...\n";
186 }
187}
188
189void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000190 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000191 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000192 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerc8e66542002-04-27 06:56:12 +0000193 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattner26750072002-07-27 01:12:17 +0000194 // FIXME: This can use the local pass map!
195 Pass *P = Set[i]->createPass(); // Good thing this is just debug code...
Chris Lattner37104aa2002-04-29 14:57:45 +0000196 std::cerr << " " << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000197 delete P;
198 }
199 std::cerr << "\n";
200 }
201}
202
Chris Lattnerf5cad152002-07-22 02:10:13 +0000203// dumpPassStructure - Implement the -debug-passes=Structure option
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000204void Pass::dumpPassStructure(unsigned Offset) {
Chris Lattner37104aa2002-04-29 14:57:45 +0000205 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +0000206}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000207
208
Chris Lattnercdd09c22002-01-31 00:45:31 +0000209//===----------------------------------------------------------------------===//
210// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000211//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000212
Chris Lattnerc8e66542002-04-27 06:56:12 +0000213void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
214 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000215}
Chris Lattner26e4f892002-01-21 07:37:31 +0000216
Chris Lattner37104aa2002-04-29 14:57:45 +0000217
218// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
219//
Chris Lattner071577d2002-07-29 21:02:31 +0000220const char *Pass::getPassName() const {
221 if (const PassInfo *PI = getPassInfo())
222 return PI->getPassName();
223 return typeid(*this).name();
224}
Chris Lattner37104aa2002-04-29 14:57:45 +0000225
Chris Lattner26750072002-07-27 01:12:17 +0000226// print - Print out the internal state of the pass. This is called by Analyse
227// to print out the contents of an analysis. Otherwise it is not neccesary to
228// implement this method.
229//
230void Pass::print(std::ostream &O) const {
231 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
232}
233
234// dump - call print(std::cerr);
235void Pass::dump() const {
236 print(std::cerr, 0);
237}
238
Chris Lattnercdd09c22002-01-31 00:45:31 +0000239//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000240// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000241//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000242
Chris Lattnerc8e66542002-04-27 06:56:12 +0000243// run - On a module, we run this pass by initializing, runOnFunction'ing once
244// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000245//
Chris Lattner113f4f42002-06-25 16:13:24 +0000246bool FunctionPass::run(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000247 bool Changed = doInitialization(M);
248
Chris Lattner113f4f42002-06-25 16:13:24 +0000249 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
250 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000251 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000252
253 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000254}
255
Chris Lattnerc8e66542002-04-27 06:56:12 +0000256// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000257//
Chris Lattner113f4f42002-06-25 16:13:24 +0000258bool FunctionPass::run(Function &F) {
259 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000260
Chris Lattner113f4f42002-06-25 16:13:24 +0000261 return doInitialization(*F.getParent()) | runOnFunction(F)
262 | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000263}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000264
Chris Lattnerc8e66542002-04-27 06:56:12 +0000265void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
266 AnalysisUsage &AU) {
267 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000268}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000269
Chris Lattnerc8e66542002-04-27 06:56:12 +0000270void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
271 AnalysisUsage &AU) {
272 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000273}
274
275//===----------------------------------------------------------------------===//
276// BasicBlockPass Implementation
277//
278
Chris Lattnerc8e66542002-04-27 06:56:12 +0000279// To run this pass on a function, we simply call runOnBasicBlock once for each
280// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000281//
Chris Lattner113f4f42002-06-25 16:13:24 +0000282bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000283 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000284 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000285 Changed |= runOnBasicBlock(*I);
286 return Changed;
287}
288
289// To run directly on the basic block, we initialize, runOnBasicBlock, then
290// finalize.
291//
Chris Lattner113f4f42002-06-25 16:13:24 +0000292bool BasicBlockPass::run(BasicBlock &BB) {
293 Module &M = *BB.getParent()->getParent();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000294 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
295}
296
Chris Lattner57698e22002-03-26 18:01:55 +0000297void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000298 AnalysisUsage &AU) {
299 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000300}
301
302void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *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 Lattner37d3c952002-07-23 18:08:00 +0000307
308//===----------------------------------------------------------------------===//
309// Pass Registration mechanism
310//
311static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
312static std::vector<PassRegistrationListener*> *Listeners = 0;
313
314// getPassInfo - Return the PassInfo data structure that corresponds to this
315// pass...
316const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000317 if (PassInfoCache) return PassInfoCache;
318 if (PassInfoMap == 0) return 0;
319 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(typeid(*this));
320 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000321}
322
323void RegisterPassBase::registerPass(PassInfo *PI) {
324 if (PassInfoMap == 0)
325 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
326
327 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
328 "Pass already registered!");
329 PIObj = PI;
330 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
331
332 // Notify any listeners...
333 if (Listeners)
334 for (std::vector<PassRegistrationListener*>::iterator
335 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
336 (*I)->passRegistered(PI);
337}
338
339RegisterPassBase::~RegisterPassBase() {
340 assert(PassInfoMap && "Pass registered but not in map!");
341 std::map<TypeInfo, PassInfo*>::iterator I =
342 PassInfoMap->find(PIObj->getTypeInfo());
343 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
344
345 // Remove pass from the map...
346 PassInfoMap->erase(I);
347 if (PassInfoMap->empty()) {
348 delete PassInfoMap;
349 PassInfoMap = 0;
350 }
351
352 // Notify any listeners...
353 if (Listeners)
354 for (std::vector<PassRegistrationListener*>::iterator
355 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
356 (*I)->passUnregistered(PIObj);
357
358 // Delete the PassInfo object itself...
359 delete PIObj;
360}
361
362
363
364//===----------------------------------------------------------------------===//
365// PassRegistrationListener implementation
366//
367
368// PassRegistrationListener ctor - Add the current object to the list of
369// PassRegistrationListeners...
370PassRegistrationListener::PassRegistrationListener() {
371 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
372 Listeners->push_back(this);
373}
374
375// dtor - Remove object from list of listeners...
376PassRegistrationListener::~PassRegistrationListener() {
377 std::vector<PassRegistrationListener*>::iterator I =
378 std::find(Listeners->begin(), Listeners->end(), this);
379 assert(Listeners && I != Listeners->end() &&
380 "PassRegistrationListener not registered!");
381 Listeners->erase(I);
382
383 if (Listeners->empty()) {
384 delete Listeners;
385 Listeners = 0;
386 }
387}
388
389// enumeratePasses - Iterate over the registered passes, calling the
390// passEnumerate callback on each PassInfo object.
391//
392void PassRegistrationListener::enumeratePasses() {
393 if (PassInfoMap)
394 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
395 E = PassInfoMap->end(); I != E; ++I)
396 passEnumerate(I->second);
397}