1 //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
3 // This file implements the LLVM Pass infrastructure. It is primarily
4 // responsible with ensuring that passes are executed and batched together
7 //===----------------------------------------------------------------------===//
9 #include "llvm/PassManager.h"
10 #include "PassManagerT.h" // PassManagerT implementation
11 #include "llvm/Module.h"
12 #include "llvm/Function.h"
13 #include "llvm/BasicBlock.h"
14 #include "Support/STLExtras.h"
15 #include "Support/CommandLine.h"
21 // Source of unique analysis ID #'s.
22 unsigned AnalysisID::NextID = 0;
24 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
25 assert(P->Resolver == 0 && "Pass already in a PassManager!");
30 // preservesCFG - This function should be called to by the pass, iff they do
33 // 1. Add or remove basic blocks from the function
34 // 2. Modify terminator instructions in any way.
36 // This function annotates the AnalysisUsage info object to say that analyses
37 // that only depend on the CFG are preserved by this pass.
39 void AnalysisUsage::preservesCFG() {
40 // FIXME: implement preservesCFG
44 //===----------------------------------------------------------------------===//
45 // PassManager implementation - The PassManager class is a simple Pimpl class
46 // that wraps the PassManagerT template.
48 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
49 PassManager::~PassManager() { delete PM; }
50 void PassManager::add(Pass *P) { PM->add(P); }
51 bool PassManager::run(Module *M) { return PM->run(M); }
54 //===----------------------------------------------------------------------===//
55 // 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.
59 static cl::Flag EnableTiming("time-passes", "Time each pass, printing elapsed"
60 " time for each on exit");
62 static double getTime() {
65 return T.tv_sec + T.tv_usec/1000000.0;
68 // Create method. If Timing is enabled, this creates and returns a new timing
69 // object, otherwise it returns null.
71 TimingInfo *TimingInfo::create() {
72 return EnableTiming ? new TimingInfo() : 0;
75 void TimingInfo::passStarted(Pass *P) { TimingData[P] -= getTime(); }
76 void TimingInfo::passEnded(Pass *P) { TimingData[P] += getTime(); }
78 // TimingDtor - Print out information about timing information
79 TimingInfo::~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.
83 std::vector<pair<double, Pass*> > Data;
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;
93 // Sort the data by time as the primary key, in reverse order...
94 std::sort(Data.begin(), Data.end(), greater<pair<double, Pass*> >());
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:\n";
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, Data[i].second->getPassName());
107 cerr << " 100.00% " << TotalTime << "s\tTOTAL\n"
108 << std::string(79, '=') << "\n";
112 //===----------------------------------------------------------------------===//
113 // 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.
119 // Different debug levels that can be enabled...
120 enum PassDebugLevel {
121 None, PassStructure, PassExecutions, PassDetails
124 static 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);
131 void PMDebug::PrintPassStructure(Pass *P) {
132 if (PassDebugging >= PassStructure)
133 P->dumpPassStructure();
136 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
137 Pass *P, Annotable *V) {
138 if (PassDebugging >= PassExecutions) {
139 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
142 std::cerr << "' on ";
144 if (dynamic_cast<Module*>(V)) {
145 std::cerr << "Module\n"; return;
146 } 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();
153 std::cerr << "'...\n";
157 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
158 Pass *P, const std::vector<AnalysisID> &Set){
159 if (PassDebugging >= PassDetails && !Set.empty()) {
160 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
161 for (unsigned i = 0; i != Set.size(); ++i) {
162 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
163 std::cerr << " " << P->getPassName();
170 // dumpPassStructure - Implement the -debug-passes=PassStructure option
171 void Pass::dumpPassStructure(unsigned Offset = 0) {
172 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
176 //===----------------------------------------------------------------------===//
177 // Pass Implementation
180 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
181 PM->addPass(this, AU);
185 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
187 const char *Pass::getPassName() const { return typeid(*this).name(); }
189 //===----------------------------------------------------------------------===//
190 // FunctionPass Implementation
193 // run - On a module, we run this pass by initializing, runOnFunction'ing once
194 // for every function in the module, then by finalizing.
196 bool FunctionPass::run(Module *M) {
197 bool Changed = doInitialization(M);
199 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
200 if (!(*I)->isExternal()) // Passes are not run on external functions!
201 Changed |= runOnFunction(*I);
203 return Changed | doFinalization(M);
206 // run - On a function, we simply initialize, run the function, then finalize.
208 bool FunctionPass::run(Function *F) {
209 if (F->isExternal()) return false;// Passes are not run on external functions!
211 return doInitialization(F->getParent()) | runOnFunction(F)
212 | doFinalization(F->getParent());
215 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
217 PM->addPass(this, AU);
220 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
222 PM->addPass(this, AU);
225 //===----------------------------------------------------------------------===//
226 // BasicBlockPass Implementation
229 // To run this pass on a function, we simply call runOnBasicBlock once for each
232 bool BasicBlockPass::runOnFunction(Function *F) {
233 bool Changed = false;
234 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
235 Changed |= runOnBasicBlock(*I);
239 // To run directly on the basic block, we initialize, runOnBasicBlock, then
242 bool BasicBlockPass::run(BasicBlock *BB) {
243 Module *M = BB->getParent()->getParent();
244 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
247 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
249 PM->addPass(this, AU);
252 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
254 PM->addPass(this, AU);