1 //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
3 // This file implements the LLVM Pass infrastructure. It is primarily
4 // responsible with ensuring that passes are executed and batched together
7 //===----------------------------------------------------------------------===//
9 #include "llvm/PassManager.h"
10 #include "llvm/Module.h"
11 #include "llvm/Method.h"
12 #include "Support/STLExtras.h"
15 // Source of unique analysis ID #'s.
16 unsigned AnalysisID::NextID = 0;
18 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
19 assert(P->Resolver == 0 && "Pass already in a PassManager!");
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.
30 #include "Support/CommandLine.h"
34 // Different debug levels that can be enabled...
36 None, PassStructure, PassExecutions, PassDetails
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);
46 void PMDebug::PrintPassStructure(Pass *P) {
47 if (PassDebugging >= PassStructure)
48 P->dumpPassStructure();
51 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
53 if (PassDebugging >= PassExecutions) {
54 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
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;
66 std::cerr << typeid(*V).name() << " '" << V->getName(); break;
69 std::cerr << "'...\n";
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();
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";
93 //===----------------------------------------------------------------------===//
94 // Pass Implementation
97 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
98 AnalysisSet &Destroyed, AnalysisSet &Provided) {
99 PM->addPass(this, Required, Destroyed, Provided);
102 //===----------------------------------------------------------------------===//
103 // MethodPass Implementation
106 // run - On a module, we run this pass by initializing, ronOnMethod'ing once
107 // for every method in the module, then by finalizing.
109 bool MethodPass::run(Module *M) {
110 bool Changed = doInitialization(M);
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);
116 return Changed | doFinalization(M);
119 // run - On a method, we simply initialize, run the method, then finalize.
121 bool MethodPass::run(Method *M) {
122 if (M->isExternal()) return false; // Passes are not run on external methods!
124 return doInitialization(M->getParent()) | runOnMethod(M)
125 | doFinalization(M->getParent());
128 void MethodPass::addToPassManager(PassManagerT<Module> *PM,
129 AnalysisSet &Required, AnalysisSet &Destroyed,
130 AnalysisSet &Provided) {
131 PM->addPass(this, Required, Destroyed, Provided);
134 void MethodPass::addToPassManager(PassManagerT<Method> *PM,
135 AnalysisSet &Required, AnalysisSet &Destroyed,
136 AnalysisSet &Provided) {
137 PM->addPass(this, Required, Destroyed, Provided);
140 //===----------------------------------------------------------------------===//
141 // BasicBlockPass Implementation
144 // To run this pass on a method, we simply call runOnBasicBlock once for each
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);
154 // To run directly on the basic block, we initialize, runOnBasicBlock, then
157 bool BasicBlockPass::run(BasicBlock *BB) {
158 Module *M = BB->getParent()->getParent();
159 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
162 void BasicBlockPass::addToPassManager(PassManagerT<Method> *PM,
163 AnalysisSet &Required,
164 AnalysisSet &Destroyed,
165 AnalysisSet &Provided) {
166 PM->addPass(this, Required, Destroyed, Provided);
169 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
170 AnalysisSet &Required,
171 AnalysisSet &Destroyed,
172 AnalysisSet &Provided) {
173 PM->addPass(this, Required, Destroyed, Provided);