ff9ac928b9698283b64b8c65d2f93a8aa3d2cf19
[oota-llvm.git] / lib / VMCore / Pass.cpp
1 //===- 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
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"
16 #include <typeinfo>
17 #include <iostream>
18
19 // Source of unique analysis ID #'s.
20 unsigned AnalysisID::NextID = 0;
21
22 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
23   assert(P->Resolver == 0 && "Pass already in a PassManager!");
24   P->Resolver = AR;
25 }
26
27 //===----------------------------------------------------------------------===//
28 // PassManager implementation - The PassManager class is a simple Pimpl class
29 // that wraps the PassManagerT template.
30 //
31 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
32 PassManager::~PassManager() { delete PM; }
33 void PassManager::add(Pass *P) { PM->add(P); }
34 bool PassManager::run(Module *M) { return PM->run(M); }
35
36
37 //===----------------------------------------------------------------------===//
38 // Pass debugging information.  Often it is useful to find out what pass is
39 // running when a crash occurs in a utility.  When this library is compiled with
40 // debugging on, a command line option (--debug-pass) is enabled that causes the
41 // pass name to be printed before it executes.
42 //
43
44 // Different debug levels that can be enabled...
45 enum PassDebugLevel {
46   None, PassStructure, PassExecutions, PassDetails
47 };
48
49 static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
50   "Print PassManager debugging information",
51   clEnumVal(None          , "disable debug output"),
52   clEnumVal(PassStructure , "print pass structure before run()"),
53   clEnumVal(PassExecutions, "print pass name before it is executed"),
54   clEnumVal(PassDetails   , "print pass details when it is executed"), 0); 
55
56 void PMDebug::PrintPassStructure(Pass *P) {
57   if (PassDebugging >= PassStructure)
58     P->dumpPassStructure();
59 }
60
61 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
62                                    Pass *P, Annotable *V) {
63   if (PassDebugging >= PassExecutions) {
64     std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" 
65               << typeid(*P).name();
66     if (V) {
67       std::cerr << "' on ";
68
69       if (dynamic_cast<Module*>(V)) {
70         std::cerr << "Module\n"; return;
71       } else if (Function *F = dynamic_cast<Function*>(V))
72         std::cerr << "Function '" << F->getName();
73       else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
74         std::cerr << "BasicBlock '" << BB->getName();
75       else if (Value *Val = dynamic_cast<Value*>(V))
76         std::cerr << typeid(*Val).name() << " '" << Val->getName();
77     }
78     std::cerr << "'...\n";
79   }
80 }
81
82 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
83                                    Pass *P, const std::vector<AnalysisID> &Set){
84   if (PassDebugging >= PassDetails && !Set.empty()) {
85     std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
86     for (unsigned i = 0; i != Set.size(); ++i) {
87       Pass *P = Set[i].createPass();   // Good thing this is just debug code...
88       std::cerr << "  " << typeid(*P).name();
89       delete P;
90     }
91     std::cerr << "\n";
92   }
93 }
94
95 // dumpPassStructure - Implement the -debug-passes=PassStructure option
96 void Pass::dumpPassStructure(unsigned Offset = 0) {
97   std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
98 }
99
100
101 //===----------------------------------------------------------------------===//
102 // Pass Implementation
103 //
104
105 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
106   PM->addPass(this, AU);
107 }
108
109 //===----------------------------------------------------------------------===//
110 // FunctionPass Implementation
111 //
112
113 // run - On a module, we run this pass by initializing, runOnFunction'ing once
114 // for every function in the module, then by finalizing.
115 //
116 bool FunctionPass::run(Module *M) {
117   bool Changed = doInitialization(M);
118   
119   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
120     if (!(*I)->isExternal())      // Passes are not run on external functions!
121     Changed |= runOnFunction(*I);
122   
123   return Changed | doFinalization(M);
124 }
125
126 // run - On a function, we simply initialize, run the function, then finalize.
127 //
128 bool FunctionPass::run(Function *F) {
129   if (F->isExternal()) return false;// Passes are not run on external functions!
130
131   return doInitialization(F->getParent()) | runOnFunction(F)
132        | doFinalization(F->getParent());
133 }
134
135 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
136                                     AnalysisUsage &AU) {
137   PM->addPass(this, AU);
138 }
139
140 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
141                                     AnalysisUsage &AU) {
142   PM->addPass(this, AU);
143 }
144
145 // doesNotModifyCFG - This function should be called by our subclasses to
146 // implement the getAnalysisUsage virtual function, iff they do not:
147 //
148 //  1. Add or remove basic blocks from the function
149 //  2. Modify terminator instructions in any way.
150 //
151 // This function annotates the AnalysisUsage info object to say that analyses
152 // that only depend on the CFG are preserved by this pass.
153 //
154 void FunctionPass::doesNotModifyCFG(AnalysisUsage &Info) {
155
156 }
157
158
159 //===----------------------------------------------------------------------===//
160 // BasicBlockPass Implementation
161 //
162
163 // To run this pass on a function, we simply call runOnBasicBlock once for each
164 // function.
165 //
166 bool BasicBlockPass::runOnFunction(Function *F) {
167   bool Changed = false;
168   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
169     Changed |= runOnBasicBlock(*I);
170   return Changed;
171 }
172
173 // To run directly on the basic block, we initialize, runOnBasicBlock, then
174 // finalize.
175 //
176 bool BasicBlockPass::run(BasicBlock *BB) {
177   Module *M = BB->getParent()->getParent();
178   return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
179 }
180
181 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
182                                       AnalysisUsage &AU) {
183   PM->addPass(this, AU);
184 }
185
186 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
187                                       AnalysisUsage &AU) {
188   PM->addPass(this, AU);
189 }
190