blob: 4b29371fff847c18e3e582778f57fd46d7bef9df [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"
14#include <typeinfo>
15#include <iostream>
Chris Lattnere2eb99e2002-04-29 04:04:29 +000016#include <sys/time.h>
17#include <stdio.h>
Chris Lattnerd013ba92002-01-23 05:49:41 +000018
Chris Lattner7e0dbe62002-05-06 19:31:52 +000019//===----------------------------------------------------------------------===//
20// AnalysisID Class Implementation
21//
22
23static std::vector<AnalysisID> CFGOnlyAnalyses;
24
Chris Lattnercdd09c22002-01-31 00:45:31 +000025// Source of unique analysis ID #'s.
26unsigned AnalysisID::NextID = 0;
27
Chris Lattner7e0dbe62002-05-06 19:31:52 +000028AnalysisID::AnalysisID(const AnalysisID &AID, bool DependsOnlyOnCFG) {
29 ID = AID.ID; // Implement the copy ctor part...
30 Constructor = AID.Constructor;
31
32 // If this analysis only depends on the CFG of the function, add it to the CFG
33 // only list...
34 if (DependsOnlyOnCFG)
35 CFGOnlyAnalyses.push_back(AID);
36}
37
38//===----------------------------------------------------------------------===//
39// AnalysisResolver Class Implementation
40//
41
Chris Lattnercdd09c22002-01-31 00:45:31 +000042void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
43 assert(P->Resolver == 0 && "Pass already in a PassManager!");
44 P->Resolver = AR;
45}
46
Chris Lattner7e0dbe62002-05-06 19:31:52 +000047//===----------------------------------------------------------------------===//
48// AnalysisUsage Class Implementation
49//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000050
51// preservesCFG - This function should be called to by the pass, iff they do
52// not:
53//
54// 1. Add or remove basic blocks from the function
55// 2. Modify terminator instructions in any way.
56//
57// This function annotates the AnalysisUsage info object to say that analyses
58// that only depend on the CFG are preserved by this pass.
59//
60void AnalysisUsage::preservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000061 // Since this transformation doesn't modify the CFG, it preserves all analyses
62 // that only depend on the CFG (like dominators, loop info, etc...)
63 //
64 Preserved.insert(Preserved.end(),
65 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000066}
67
68
Chris Lattner37c86672002-04-28 20:46:05 +000069//===----------------------------------------------------------------------===//
70// PassManager implementation - The PassManager class is a simple Pimpl class
71// that wraps the PassManagerT template.
72//
73PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
74PassManager::~PassManager() { delete PM; }
75void PassManager::add(Pass *P) { PM->add(P); }
Chris Lattner113f4f42002-06-25 16:13:24 +000076bool PassManager::run(Module &M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000077
Chris Lattner37c86672002-04-28 20:46:05 +000078
79//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000080// TimingInfo Class - This class is used to calculate information about the
81// amount of time each pass takes to execute. This only happens with
82// -time-passes is enabled on the command line.
83//
Chris Lattnerf5cad152002-07-22 02:10:13 +000084static cl::opt<bool>
85EnableTiming("time-passes",
86 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000087
88static double getTime() {
89 struct timeval T;
90 gettimeofday(&T, 0);
91 return T.tv_sec + T.tv_usec/1000000.0;
92}
93
94// Create method. If Timing is enabled, this creates and returns a new timing
95// object, otherwise it returns null.
96//
97TimingInfo *TimingInfo::create() {
98 return EnableTiming ? new TimingInfo() : 0;
99}
100
101void TimingInfo::passStarted(Pass *P) { TimingData[P] -= getTime(); }
102void TimingInfo::passEnded(Pass *P) { TimingData[P] += getTime(); }
103
104// TimingDtor - Print out information about timing information
105TimingInfo::~TimingInfo() {
106 // Iterate over all of the data, converting it into the dual of the data map,
107 // so that the data is sorted by amount of time taken, instead of pointer.
108 //
Anand Shukla8c377892002-06-25 22:07:38 +0000109 std::vector<std::pair<double, Pass*> > Data;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000110 double TotalTime = 0;
111 for (std::map<Pass*, double>::iterator I = TimingData.begin(),
112 E = TimingData.end(); I != E; ++I)
113 // Throw out results for "grouping" pass managers...
114 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
115 Data.push_back(std::make_pair(I->second, I->first));
116 TotalTime += I->second;
117 }
118
119 // Sort the data by time as the primary key, in reverse order...
Anand Shukla8c377892002-06-25 22:07:38 +0000120 std::sort(Data.begin(), Data.end(), std::greater<std::pair<double, Pass*> >());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000121
122 // Print out timing header...
Anand Shukla8c377892002-06-25 22:07:38 +0000123 std::cerr << std::string(79, '=') << "\n"
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000124 << " ... Pass execution timing report ...\n"
125 << std::string(79, '=') << "\n Total Execution Time: " << TotalTime
Chris Lattner37104aa2002-04-29 14:57:45 +0000126 << " seconds\n\n % Time: Seconds:\tPass Name:\n";
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000127
128 // Loop through all of the timing data, printing it out...
129 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
130 fprintf(stderr, " %6.2f%% %fs\t%s\n", Data[i].first*100 / TotalTime,
Chris Lattner37104aa2002-04-29 14:57:45 +0000131 Data[i].first, Data[i].second->getPassName());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000132 }
Anand Shukla8c377892002-06-25 22:07:38 +0000133 std::cerr << " 100.00% " << TotalTime << "s\tTOTAL\n"
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000134 << std::string(79, '=') << "\n";
135}
136
137
138//===----------------------------------------------------------------------===//
Chris Lattnerd013ba92002-01-23 05:49:41 +0000139// Pass debugging information. Often it is useful to find out what pass is
140// running when a crash occurs in a utility. When this library is compiled with
141// debugging on, a command line option (--debug-pass) is enabled that causes the
142// pass name to be printed before it executes.
143//
Chris Lattnerd013ba92002-01-23 05:49:41 +0000144
Chris Lattnercdd09c22002-01-31 00:45:31 +0000145// Different debug levels that can be enabled...
146enum PassDebugLevel {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000147 None, Structure, Executions, Details
Chris Lattnercdd09c22002-01-31 00:45:31 +0000148};
Chris Lattnerd013ba92002-01-23 05:49:41 +0000149
Chris Lattnerf5cad152002-07-22 02:10:13 +0000150static cl::opt<enum PassDebugLevel>
151PassDebugging("debug-pass", cl::Hidden,
152 cl::desc("Print PassManager debugging information"),
153 cl::values(
154 clEnumVal(None , "disable debug output"),
155 // TODO: add option to print out pass names "PassOptions"
156 clEnumVal(Structure , "print pass structure before run()"),
157 clEnumVal(Executions, "print pass name before it is executed"),
158 clEnumVal(Details , "print pass details when it is executed"),
159 0));
Chris Lattnercdd09c22002-01-31 00:45:31 +0000160
161void PMDebug::PrintPassStructure(Pass *P) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000162 if (PassDebugging >= Structure)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000163 P->dumpPassStructure();
164}
165
166void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000167 Pass *P, Annotable *V) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000168 if (PassDebugging >= Executions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000169 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000170 << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000171 if (V) {
172 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000173
174 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000175 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000176 } else if (Function *F = dynamic_cast<Function*>(V))
177 std::cerr << "Function '" << F->getName();
178 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
179 std::cerr << "BasicBlock '" << BB->getName();
180 else if (Value *Val = dynamic_cast<Value*>(V))
181 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000182 }
183 std::cerr << "'...\n";
184 }
185}
186
187void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000188 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000189 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000190 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerc8e66542002-04-27 06:56:12 +0000191 for (unsigned i = 0; i != Set.size(); ++i) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000192 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
Chris Lattner37104aa2002-04-29 14:57:45 +0000193 std::cerr << " " << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000194 delete P;
195 }
196 std::cerr << "\n";
197 }
198}
199
Chris Lattnerf5cad152002-07-22 02:10:13 +0000200// dumpPassStructure - Implement the -debug-passes=Structure option
Chris Lattnercdd09c22002-01-31 00:45:31 +0000201void Pass::dumpPassStructure(unsigned Offset = 0) {
Chris Lattner37104aa2002-04-29 14:57:45 +0000202 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattnerd013ba92002-01-23 05:49:41 +0000203}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000204
205
Chris Lattnercdd09c22002-01-31 00:45:31 +0000206//===----------------------------------------------------------------------===//
207// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000208//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000209
Chris Lattnerc8e66542002-04-27 06:56:12 +0000210void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
211 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000212}
Chris Lattner26e4f892002-01-21 07:37:31 +0000213
Chris Lattner37104aa2002-04-29 14:57:45 +0000214
215// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
216//
217const char *Pass::getPassName() const { return typeid(*this).name(); }
218
Chris Lattnercdd09c22002-01-31 00:45:31 +0000219//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000220// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000221//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000222
Chris Lattnerc8e66542002-04-27 06:56:12 +0000223// run - On a module, we run this pass by initializing, runOnFunction'ing once
224// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000225//
Chris Lattner113f4f42002-06-25 16:13:24 +0000226bool FunctionPass::run(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000227 bool Changed = doInitialization(M);
228
Chris Lattner113f4f42002-06-25 16:13:24 +0000229 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
230 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000231 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000232
233 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000234}
235
Chris Lattnerc8e66542002-04-27 06:56:12 +0000236// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000237//
Chris Lattner113f4f42002-06-25 16:13:24 +0000238bool FunctionPass::run(Function &F) {
239 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000240
Chris Lattner113f4f42002-06-25 16:13:24 +0000241 return doInitialization(*F.getParent()) | runOnFunction(F)
242 | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000243}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000244
Chris Lattnerc8e66542002-04-27 06:56:12 +0000245void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
246 AnalysisUsage &AU) {
247 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000248}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000249
Chris Lattnerc8e66542002-04-27 06:56:12 +0000250void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
251 AnalysisUsage &AU) {
252 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000253}
254
255//===----------------------------------------------------------------------===//
256// BasicBlockPass Implementation
257//
258
Chris Lattnerc8e66542002-04-27 06:56:12 +0000259// To run this pass on a function, we simply call runOnBasicBlock once for each
260// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000261//
Chris Lattner113f4f42002-06-25 16:13:24 +0000262bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000263 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000264 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000265 Changed |= runOnBasicBlock(*I);
266 return Changed;
267}
268
269// To run directly on the basic block, we initialize, runOnBasicBlock, then
270// finalize.
271//
Chris Lattner113f4f42002-06-25 16:13:24 +0000272bool BasicBlockPass::run(BasicBlock &BB) {
273 Module &M = *BB.getParent()->getParent();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000274 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
275}
276
Chris Lattner57698e22002-03-26 18:01:55 +0000277void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000278 AnalysisUsage &AU) {
279 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000280}
281
282void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000283 AnalysisUsage &AU) {
284 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000285}
286