41a87d60ad647c2d9aa56d315e0f8735815d6197
[oota-llvm.git] / lib / VMCore / Pass.cpp
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM Pass infrastructure.  It is primarily
11 // responsible with ensuring that passes are executed and batched together
12 // optimally.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Pass.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/PassRegistry.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/Assembly/PrintModulePass.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/PassNameParser.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/System/Atomic.h"
26 #include "llvm/System/Mutex.h"
27 #include "llvm/System/Threading.h"
28 using namespace llvm;
29
30 //===----------------------------------------------------------------------===//
31 // Pass Implementation
32 //
33
34 Pass::Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
35
36 // Force out-of-line virtual method.
37 Pass::~Pass() { 
38   delete Resolver; 
39 }
40
41 // Force out-of-line virtual method.
42 ModulePass::~ModulePass() { }
43
44 Pass *ModulePass::createPrinterPass(raw_ostream &O,
45                                     const std::string &Banner) const {
46   return createPrintModulePass(&O, false, Banner);
47 }
48
49 PassManagerType ModulePass::getPotentialPassManagerType() const {
50   return PMT_ModulePassManager;
51 }
52
53 bool Pass::mustPreserveAnalysisID(char &AID) const {
54   return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
55 }
56
57 // dumpPassStructure - Implement the -debug-passes=Structure option
58 void Pass::dumpPassStructure(unsigned Offset) {
59   dbgs().indent(Offset*2) << getPassName() << "\n";
60 }
61
62 /// getPassName - Return a nice clean name for a pass.  This usually
63 /// implemented in terms of the name that is registered by one of the
64 /// Registration templates, but can be overloaded directly.
65 ///
66 const char *Pass::getPassName() const {
67   AnalysisID AID =  getPassID();
68   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
69   if (PI)
70     return PI->getPassName();
71   return "Unnamed pass: implement Pass::getPassName()";
72 }
73
74 void Pass::preparePassManager(PMStack &) {
75   // By default, don't do anything.
76 }
77
78 PassManagerType Pass::getPotentialPassManagerType() const {
79   // Default implementation.
80   return PMT_Unknown; 
81 }
82
83 void Pass::getAnalysisUsage(AnalysisUsage &) const {
84   // By default, no analysis results are used, all are invalidated.
85 }
86
87 void Pass::releaseMemory() {
88   // By default, don't do anything.
89 }
90
91 void Pass::verifyAnalysis() const {
92   // By default, don't do anything.
93 }
94
95 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
96   return this;
97 }
98
99 ImmutablePass *Pass::getAsImmutablePass() {
100   return 0;
101 }
102
103 PMDataManager *Pass::getAsPMDataManager() {
104   return 0;
105 }
106
107 void Pass::setResolver(AnalysisResolver *AR) {
108   assert(!Resolver && "Resolver is already set");
109   Resolver = AR;
110 }
111
112 // print - Print out the internal state of the pass.  This is called by Analyze
113 // to print out the contents of an analysis.  Otherwise it is not necessary to
114 // implement this method.
115 //
116 void Pass::print(raw_ostream &O,const Module*) const {
117   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
118 }
119
120 // dump - call print(cerr);
121 void Pass::dump() const {
122   print(dbgs(), 0);
123 }
124
125 //===----------------------------------------------------------------------===//
126 // ImmutablePass Implementation
127 //
128 // Force out-of-line virtual method.
129 ImmutablePass::~ImmutablePass() { }
130
131 void ImmutablePass::initializePass() {
132   // By default, don't do anything.
133 }
134
135 //===----------------------------------------------------------------------===//
136 // FunctionPass Implementation
137 //
138
139 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
140                                       const std::string &Banner) const {
141   return createPrintFunctionPass(Banner, &O);
142 }
143
144 // run - On a module, we run this pass by initializing, runOnFunction'ing once
145 // for every function in the module, then by finalizing.
146 //
147 bool FunctionPass::runOnModule(Module &M) {
148   bool Changed = doInitialization(M);
149
150   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
151     if (!I->isDeclaration())      // Passes are not run on external functions!
152     Changed |= runOnFunction(*I);
153
154   return Changed | doFinalization(M);
155 }
156
157 // run - On a function, we simply initialize, run the function, then finalize.
158 //
159 bool FunctionPass::run(Function &F) {
160   // Passes are not run on external functions!
161   if (F.isDeclaration()) return false;
162
163   bool Changed = doInitialization(*F.getParent());
164   Changed |= runOnFunction(F);
165   return Changed | doFinalization(*F.getParent());
166 }
167
168 bool FunctionPass::doInitialization(Module &) {
169   // By default, don't do anything.
170   return false;
171 }
172
173 bool FunctionPass::doFinalization(Module &) {
174   // By default, don't do anything.
175   return false;
176 }
177
178 PassManagerType FunctionPass::getPotentialPassManagerType() const {
179   return PMT_FunctionPassManager;
180 }
181
182 //===----------------------------------------------------------------------===//
183 // BasicBlockPass Implementation
184 //
185
186 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
187                                         const std::string &Banner) const {
188   
189   llvm_unreachable("BasicBlockPass printing unsupported.");
190   return 0;
191 }
192
193 // To run this pass on a function, we simply call runOnBasicBlock once for each
194 // function.
195 //
196 bool BasicBlockPass::runOnFunction(Function &F) {
197   bool Changed = doInitialization(F);
198   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
199     Changed |= runOnBasicBlock(*I);
200   return Changed | doFinalization(F);
201 }
202
203 bool BasicBlockPass::doInitialization(Module &) {
204   // By default, don't do anything.
205   return false;
206 }
207
208 bool BasicBlockPass::doInitialization(Function &) {
209   // By default, don't do anything.
210   return false;
211 }
212
213 bool BasicBlockPass::doFinalization(Function &) {
214   // By default, don't do anything.
215   return false;
216 }
217
218 bool BasicBlockPass::doFinalization(Module &) {
219   // By default, don't do anything.
220   return false;
221 }
222
223 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
224   return PMT_BasicBlockPassManager; 
225 }
226
227 const PassInfo *Pass::lookupPassInfo(const void *TI) {
228   return PassRegistry::getPassRegistry()->getPassInfo(TI);
229 }
230
231 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
232   return PassRegistry::getPassRegistry()->getPassInfo(Arg);
233 }
234
235 Pass *PassInfo::createPass() const {
236   assert((!isAnalysisGroup() || NormalCtor) &&
237          "No default implementation found for analysis group!");
238   assert(NormalCtor &&
239          "Cannot call createPass on PassInfo without default ctor!");
240   return NormalCtor();
241 }
242
243 //===----------------------------------------------------------------------===//
244 //                  Analysis Group Implementation Code
245 //===----------------------------------------------------------------------===//
246
247 // RegisterAGBase implementation
248 //
249 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
250                                const void *PassID, bool isDefault)
251     : PassInfo(Name, InterfaceID) {
252   PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
253                                                          *this, isDefault);
254 }
255
256
257 //===----------------------------------------------------------------------===//
258 // PassRegistrationListener implementation
259 //
260
261 // PassRegistrationListener ctor - Add the current object to the list of
262 // PassRegistrationListeners...
263 PassRegistrationListener::PassRegistrationListener() {
264   PassRegistry::getPassRegistry()->addRegistrationListener(this);
265 }
266
267 // dtor - Remove object from list of listeners...
268 PassRegistrationListener::~PassRegistrationListener() {
269   PassRegistry::getPassRegistry()->removeRegistrationListener(this);
270 }
271
272 // enumeratePasses - Iterate over the registered passes, calling the
273 // passEnumerate callback on each PassInfo object.
274 //
275 void PassRegistrationListener::enumeratePasses() {
276   PassRegistry::getPassRegistry()->enumerateWith(this);
277 }
278
279 PassNameParser::~PassNameParser() {}
280
281 //===----------------------------------------------------------------------===//
282 //   AnalysisUsage Class Implementation
283 //
284
285 namespace {
286   struct GetCFGOnlyPasses : public PassRegistrationListener {
287     typedef AnalysisUsage::VectorType VectorType;
288     VectorType &CFGOnlyList;
289     GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
290     
291     void passEnumerate(const PassInfo *P) {
292       if (P->isCFGOnlyPass())
293         CFGOnlyList.push_back(P->getTypeInfo());
294     }
295   };
296 }
297
298 // setPreservesCFG - This function should be called to by the pass, iff they do
299 // not:
300 //
301 //  1. Add or remove basic blocks from the function
302 //  2. Modify terminator instructions in any way.
303 //
304 // This function annotates the AnalysisUsage info object to say that analyses
305 // that only depend on the CFG are preserved by this pass.
306 //
307 void AnalysisUsage::setPreservesCFG() {
308   // Since this transformation doesn't modify the CFG, it preserves all analyses
309   // that only depend on the CFG (like dominators, loop info, etc...)
310   GetCFGOnlyPasses(Preserved).enumeratePasses();
311 }
312
313 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
314   const PassInfo *PI = Pass::lookupPassInfo(Arg);
315   // If the pass exists, preserve it. Otherwise silently do nothing.
316   if (PI) Preserved.push_back(PI->getTypeInfo());
317   return *this;
318 }
319
320 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
321   Required.push_back(ID);
322   return *this;
323 }
324
325 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
326   Required.push_back(&ID);
327   return *this;
328 }
329
330 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
331   Required.push_back(&ID);
332   RequiredTransitive.push_back(&ID);
333   return *this;
334 }