Fix Bug: test/Regression/Other/2002-04-07-InfConstant.ll
[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/Function.h"
12 #include "llvm/BasicBlock.h"
13 #include "Support/STLExtras.h"
14 #include <algorithm>
15
16 // Source of unique analysis ID #'s.
17 unsigned AnalysisID::NextID = 0;
18
19 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
20   assert(P->Resolver == 0 && "Pass already in a PassManager!");
21   P->Resolver = AR;
22 }
23
24
25 // Pass debugging information.  Often it is useful to find out what pass is
26 // running when a crash occurs in a utility.  When this library is compiled with
27 // debugging on, a command line option (--debug-pass) is enabled that causes the
28 // pass name to be printed before it executes.
29 //
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::FunctionVal:
62         std::cerr << "Function '" << 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
91
92 //===----------------------------------------------------------------------===//
93 // Pass Implementation
94 //
95
96 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
97                             AnalysisSet &Destroyed, AnalysisSet &Provided) {
98   PM->addPass(this, Required, Destroyed, Provided);
99 }
100
101 //===----------------------------------------------------------------------===//
102 // MethodPass Implementation
103 //
104
105 // run - On a module, we run this pass by initializing, ronOnMethod'ing once
106 // for every method in the module, then by finalizing.
107 //
108 bool MethodPass::run(Module *M) {
109   bool Changed = doInitialization(M);
110   
111   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
112     if (!(*I)->isExternal())      // Passes are not run on external methods!
113     Changed |= runOnMethod(*I);
114   
115   return Changed | doFinalization(M);
116 }
117
118 // run - On a method, we simply initialize, run the method, then finalize.
119 //
120 bool MethodPass::run(Function *F) {
121   if (F->isExternal()) return false;  // Passes are not run on external methods!
122
123   return doInitialization(F->getParent()) | runOnMethod(F)
124        | doFinalization(F->getParent());
125 }
126
127 void MethodPass::addToPassManager(PassManagerT<Module> *PM,
128                                   AnalysisSet &Required, AnalysisSet &Destroyed,
129                                   AnalysisSet &Provided) {
130   PM->addPass(this, Required, Destroyed, Provided);
131 }
132
133 void MethodPass::addToPassManager(PassManagerT<Function> *PM,
134                                   AnalysisSet &Required, AnalysisSet &Destroyed,
135                                   AnalysisSet &Provided) {
136   PM->addPass(this, Required, Destroyed, Provided);
137 }
138
139 //===----------------------------------------------------------------------===//
140 // BasicBlockPass Implementation
141 //
142
143 // To run this pass on a method, we simply call runOnBasicBlock once for each
144 // method.
145 //
146 bool BasicBlockPass::runOnMethod(Function *F) {
147   bool Changed = false;
148   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
149     Changed |= runOnBasicBlock(*I);
150   return Changed;
151 }
152
153 // To run directly on the basic block, we initialize, runOnBasicBlock, then
154 // finalize.
155 //
156 bool BasicBlockPass::run(BasicBlock *BB) {
157   Module *M = BB->getParent()->getParent();
158   return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
159 }
160
161 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
162                                       AnalysisSet &Required,
163                                       AnalysisSet &Destroyed,
164                                       AnalysisSet &Provided) {
165   PM->addPass(this, Required, Destroyed, Provided);
166 }
167
168 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
169                                       AnalysisSet &Required,
170                                       AnalysisSet &Destroyed,
171                                       AnalysisSet &Provided) {
172   PM->addPass(this, Required, Destroyed, Provided);
173 }
174