blob: 0344dd63b335197924686ba77d42a6b3a6d66c08 [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 Lattner57698e22002-03-26 18:01:55 +000012#include "llvm/Function.h"
Chris Lattner60a65912002-02-12 21:07:25 +000013#include "llvm/BasicBlock.h"
Chris Lattner26e4f892002-01-21 07:37:31 +000014#include "Support/STLExtras.h"
Chris Lattner37c86672002-04-28 20:46:05 +000015#include "Support/CommandLine.h"
16#include <typeinfo>
17#include <iostream>
Chris Lattnere2eb99e2002-04-29 04:04:29 +000018#include <sys/time.h>
19#include <stdio.h>
Chris Lattnerd013ba92002-01-23 05:49:41 +000020
Chris Lattnercdd09c22002-01-31 00:45:31 +000021// Source of unique analysis ID #'s.
22unsigned AnalysisID::NextID = 0;
23
24void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
25 assert(P->Resolver == 0 && "Pass already in a PassManager!");
26 P->Resolver = AR;
27}
28
Chris Lattneree2ff5d2002-04-28 21:25:41 +000029
30// preservesCFG - This function should be called to by the pass, iff they do
31// not:
32//
33// 1. Add or remove basic blocks from the function
34// 2. Modify terminator instructions in any way.
35//
36// This function annotates the AnalysisUsage info object to say that analyses
37// that only depend on the CFG are preserved by this pass.
38//
39void AnalysisUsage::preservesCFG() {
40 // FIXME: implement preservesCFG
41}
42
43
Chris Lattner37c86672002-04-28 20:46:05 +000044//===----------------------------------------------------------------------===//
45// PassManager implementation - The PassManager class is a simple Pimpl class
46// that wraps the PassManagerT template.
47//
48PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
49PassManager::~PassManager() { delete PM; }
50void PassManager::add(Pass *P) { PM->add(P); }
51bool PassManager::run(Module *M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000052
Chris Lattner37c86672002-04-28 20:46:05 +000053
54//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000055// TimingInfo Class - This class is used to calculate information about the
56// amount of time each pass takes to execute. This only happens with
57// -time-passes is enabled on the command line.
58//
59static cl::Flag EnableTiming("time-passes", "Time each pass, printing elapsed"
60 " time for each on exit");
61
62static double getTime() {
63 struct timeval T;
64 gettimeofday(&T, 0);
65 return T.tv_sec + T.tv_usec/1000000.0;
66}
67
68// Create method. If Timing is enabled, this creates and returns a new timing
69// object, otherwise it returns null.
70//
71TimingInfo *TimingInfo::create() {
72 return EnableTiming ? new TimingInfo() : 0;
73}
74
75void TimingInfo::passStarted(Pass *P) { TimingData[P] -= getTime(); }
76void TimingInfo::passEnded(Pass *P) { TimingData[P] += getTime(); }
77
78// TimingDtor - Print out information about timing information
79TimingInfo::~TimingInfo() {
80 // Iterate over all of the data, converting it into the dual of the data map,
81 // so that the data is sorted by amount of time taken, instead of pointer.
82 //
83 std::vector<pair<double, Pass*> > Data;
84 double TotalTime = 0;
85 for (std::map<Pass*, double>::iterator I = TimingData.begin(),
86 E = TimingData.end(); I != E; ++I)
87 // Throw out results for "grouping" pass managers...
88 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
89 Data.push_back(std::make_pair(I->second, I->first));
90 TotalTime += I->second;
91 }
92
93 // Sort the data by time as the primary key, in reverse order...
94 std::sort(Data.begin(), Data.end(), greater<pair<double, Pass*> >());
95
96 // Print out timing header...
97 cerr << std::string(79, '=') << "\n"
98 << " ... Pass execution timing report ...\n"
99 << std::string(79, '=') << "\n Total Execution Time: " << TotalTime
100 << " seconds\n\n % Time: Seconds:\tPass Name (mangled):\n";
101
102 // Loop through all of the timing data, printing it out...
103 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
104 fprintf(stderr, " %6.2f%% %fs\t%s\n", Data[i].first*100 / TotalTime,
105 Data[i].first, typeid(*Data[i].second).name());
106 }
107 cerr << " 100.00% " << TotalTime << "s\tTOTAL\n"
108 << std::string(79, '=') << "\n";
109}
110
111
112//===----------------------------------------------------------------------===//
Chris Lattnerd013ba92002-01-23 05:49:41 +0000113// Pass debugging information. Often it is useful to find out what pass is
114// running when a crash occurs in a utility. When this library is compiled with
115// debugging on, a command line option (--debug-pass) is enabled that causes the
116// pass name to be printed before it executes.
117//
Chris Lattnerd013ba92002-01-23 05:49:41 +0000118
Chris Lattnercdd09c22002-01-31 00:45:31 +0000119// Different debug levels that can be enabled...
120enum PassDebugLevel {
121 None, PassStructure, PassExecutions, PassDetails
122};
Chris Lattnerd013ba92002-01-23 05:49:41 +0000123
Chris Lattnercdd09c22002-01-31 00:45:31 +0000124static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
125 "Print PassManager debugging information",
126 clEnumVal(None , "disable debug output"),
127 clEnumVal(PassStructure , "print pass structure before run()"),
128 clEnumVal(PassExecutions, "print pass name before it is executed"),
129 clEnumVal(PassDetails , "print pass details when it is executed"), 0);
130
131void PMDebug::PrintPassStructure(Pass *P) {
132 if (PassDebugging >= PassStructure)
133 P->dumpPassStructure();
134}
135
136void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000137 Pass *P, Annotable *V) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000138 if (PassDebugging >= PassExecutions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000139 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattnercdd09c22002-01-31 00:45:31 +0000140 << typeid(*P).name();
141 if (V) {
142 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000143
144 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000145 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000146 } else if (Function *F = dynamic_cast<Function*>(V))
147 std::cerr << "Function '" << F->getName();
148 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
149 std::cerr << "BasicBlock '" << BB->getName();
150 else if (Value *Val = dynamic_cast<Value*>(V))
151 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000152 }
153 std::cerr << "'...\n";
154 }
155}
156
157void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000158 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnercdd09c22002-01-31 00:45:31 +0000159 if (PassDebugging >= PassDetails && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000160 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerc8e66542002-04-27 06:56:12 +0000161 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000162 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
163 std::cerr << " " << typeid(*P).name();
164 delete P;
165 }
166 std::cerr << "\n";
167 }
168}
169
170// dumpPassStructure - Implement the -debug-passes=PassStructure option
171void Pass::dumpPassStructure(unsigned Offset = 0) {
172 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +0000173}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000174
175
Chris Lattnercdd09c22002-01-31 00:45:31 +0000176//===----------------------------------------------------------------------===//
177// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000178//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000179
Chris Lattnerc8e66542002-04-27 06:56:12 +0000180void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
181 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000182}
Chris Lattner26e4f892002-01-21 07:37:31 +0000183
Chris Lattnercdd09c22002-01-31 00:45:31 +0000184//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000185// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000186//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000187
Chris Lattnerc8e66542002-04-27 06:56:12 +0000188// run - On a module, we run this pass by initializing, runOnFunction'ing once
189// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000190//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000191bool FunctionPass::run(Module *M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000192 bool Changed = doInitialization(M);
193
194 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc8e66542002-04-27 06:56:12 +0000195 if (!(*I)->isExternal()) // Passes are not run on external functions!
196 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000197
198 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000199}
200
Chris Lattnerc8e66542002-04-27 06:56:12 +0000201// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000202//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000203bool FunctionPass::run(Function *F) {
204 if (F->isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000205
Chris Lattnerc8e66542002-04-27 06:56:12 +0000206 return doInitialization(F->getParent()) | runOnFunction(F)
Chris Lattner57698e22002-03-26 18:01:55 +0000207 | doFinalization(F->getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000208}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000209
Chris Lattnerc8e66542002-04-27 06:56:12 +0000210void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
211 AnalysisUsage &AU) {
212 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000213}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000214
Chris Lattnerc8e66542002-04-27 06:56:12 +0000215void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
216 AnalysisUsage &AU) {
217 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000218}
219
220//===----------------------------------------------------------------------===//
221// BasicBlockPass Implementation
222//
223
Chris Lattnerc8e66542002-04-27 06:56:12 +0000224// To run this pass on a function, we simply call runOnBasicBlock once for each
225// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000226//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000227bool BasicBlockPass::runOnFunction(Function *F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000228 bool Changed = false;
Chris Lattner57698e22002-03-26 18:01:55 +0000229 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000230 Changed |= runOnBasicBlock(*I);
231 return Changed;
232}
233
234// To run directly on the basic block, we initialize, runOnBasicBlock, then
235// finalize.
236//
237bool BasicBlockPass::run(BasicBlock *BB) {
238 Module *M = BB->getParent()->getParent();
239 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
240}
241
Chris Lattner57698e22002-03-26 18:01:55 +0000242void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000243 AnalysisUsage &AU) {
244 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000245}
246
247void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000248 AnalysisUsage &AU) {
249 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000250}
251