c18ff0adcc5a48fc760961fa175bd95e9cb663cc
[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 "llvm/Module.h"
11 #include "llvm/Method.h"
12 #include "Support/STLExtras.h"
13 #include <algorithm>
14
15 // Source of unique analysis ID #'s.
16 unsigned AnalysisID::NextID = 0;
17
18 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
19   assert(P->Resolver == 0 && "Pass already in a PassManager!");
20   P->Resolver = AR;
21 }
22
23
24 // Pass debugging information.  Often it is useful to find out what pass is
25 // running when a crash occurs in a utility.  When this library is compiled with
26 // debugging on, a command line option (--debug-pass) is enabled that causes the
27 // pass name to be printed before it executes.
28 //
29 #ifndef NDEBUG
30 #include "Support/CommandLine.h"
31 #include <typeinfo>
32 #include <iostream>
33
34 // Different debug levels that can be enabled...
35 enum PassDebugLevel {
36   None, PassStructure, PassExecutions, PassDetails
37 };
38
39 static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
40   "Print PassManager debugging information",
41   clEnumVal(None          , "disable debug output"),
42   clEnumVal(PassStructure , "print pass structure before run()"),
43   clEnumVal(PassExecutions, "print pass name before it is executed"),
44   clEnumVal(PassDetails   , "print pass details when it is executed"), 0); 
45
46 void PMDebug::PrintPassStructure(Pass *P) {
47   if (PassDebugging >= PassStructure)
48     P->dumpPassStructure();
49 }
50
51 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
52                                    Pass *P, Value *V) {
53   if (PassDebugging >= PassExecutions) {
54     std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" 
55               << typeid(*P).name();
56     if (V) {
57       std::cerr << "' on ";
58       switch (V->getValueType()) {
59       case Value::ModuleVal:
60         std::cerr << "Module\n"; return;
61       case Value::MethodVal:
62         std::cerr << "Method '" << V->getName(); break;
63       case Value::BasicBlockVal:
64         std::cerr << "BasicBlock '" << V->getName(); break;
65       default:
66         std::cerr << typeid(*V).name() << " '" << V->getName(); break;
67       }
68     }
69     std::cerr << "'...\n";
70   }
71 }
72
73 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
74                                    Pass *P, const Pass::AnalysisSet &Set) {
75   if (PassDebugging >= PassDetails && !Set.empty()) {
76     std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
77     for (unsigned i = 0; i < Set.size(); ++i) {
78       Pass *P = Set[i].createPass();   // Good thing this is just debug code...
79       std::cerr << "  " << typeid(*P).name();
80       delete P;
81     }
82     std::cerr << "\n";
83   }
84 }
85
86 // dumpPassStructure - Implement the -debug-passes=PassStructure option
87 void Pass::dumpPassStructure(unsigned Offset = 0) {
88   std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
89 }
90 #endif
91
92
93 //===----------------------------------------------------------------------===//
94 // Pass Implementation
95 //
96
97 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
98                             AnalysisSet &Destroyed, AnalysisSet &Provided) {
99   PM->addPass(this, Required, Destroyed, Provided);
100 }
101
102 //===----------------------------------------------------------------------===//
103 // MethodPass Implementation
104 //
105
106 // run - On a module, we run this pass by initializing, ronOnMethod'ing once
107 // for every method in the module, then by finalizing.
108 //
109 bool MethodPass::run(Module *M) {
110   bool Changed = doInitialization(M);
111   
112   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
113     if (!(*I)->isExternal())      // Passes are not run on external methods!
114     Changed |= runOnMethod(*I);
115   
116   return Changed | doFinalization(M);
117 }
118
119 // run - On a method, we simply initialize, run the method, then finalize.
120 //
121 bool MethodPass::run(Method *M) {
122   if (M->isExternal()) return false;  // Passes are not run on external methods!
123
124   return doInitialization(M->getParent()) | runOnMethod(M)
125        | doFinalization(M->getParent());
126 }
127
128 void MethodPass::addToPassManager(PassManagerT<Module> *PM,
129                                   AnalysisSet &Required, AnalysisSet &Destroyed,
130                                   AnalysisSet &Provided) {
131   PM->addPass(this, Required, Destroyed, Provided);
132 }
133
134 void MethodPass::addToPassManager(PassManagerT<Method> *PM,
135                                   AnalysisSet &Required, AnalysisSet &Destroyed,
136                                   AnalysisSet &Provided) {
137   PM->addPass(this, Required, Destroyed, Provided);
138 }
139
140 //===----------------------------------------------------------------------===//
141 // BasicBlockPass Implementation
142 //
143
144 // To run this pass on a method, we simply call runOnBasicBlock once for each
145 // method.
146 //
147 bool BasicBlockPass::runOnMethod(Method *M) {
148   bool Changed = false;
149   for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
150     Changed |= runOnBasicBlock(*I);
151   return Changed;
152 }
153
154 // To run directly on the basic block, we initialize, runOnBasicBlock, then
155 // finalize.
156 //
157 bool BasicBlockPass::run(BasicBlock *BB) {
158   Module *M = BB->getParent()->getParent();
159   return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
160 }
161
162 void BasicBlockPass::addToPassManager(PassManagerT<Method> *PM,
163                                       AnalysisSet &Required,
164                                       AnalysisSet &Destroyed,
165                                       AnalysisSet &Provided) {
166   PM->addPass(this, Required, Destroyed, Provided);
167 }
168
169 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
170                                       AnalysisSet &Required,
171                                       AnalysisSet &Destroyed,
172                                       AnalysisSet &Provided) {
173   PM->addPass(this, Required, Destroyed, Provided);
174 }
175