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