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