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 "llvm/BasicBlock.h"
13 #include "Support/STLExtras.h"
16 // Source of unique analysis ID #'s.
17 unsigned AnalysisID::NextID = 0;
19 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
20 assert(P->Resolver == 0 && "Pass already in a PassManager!");
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.
31 #include "Support/CommandLine.h"
35 // Different debug levels that can be enabled...
37 None, PassStructure, PassExecutions, PassDetails
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);
47 void PMDebug::PrintPassStructure(Pass *P) {
48 if (PassDebugging >= PassStructure)
49 P->dumpPassStructure();
52 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
54 if (PassDebugging >= PassExecutions) {
55 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
59 switch (V->getValueType()) {
60 case Value::ModuleVal:
61 std::cerr << "Module\n"; return;
62 case Value::MethodVal:
63 std::cerr << "Method '" << V->getName(); break;
64 case Value::BasicBlockVal:
65 std::cerr << "BasicBlock '" << V->getName(); break;
67 std::cerr << typeid(*V).name() << " '" << V->getName(); break;
70 std::cerr << "'...\n";
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();
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";
94 //===----------------------------------------------------------------------===//
95 // Pass Implementation
98 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
99 AnalysisSet &Destroyed, AnalysisSet &Provided) {
100 PM->addPass(this, Required, Destroyed, Provided);
103 //===----------------------------------------------------------------------===//
104 // MethodPass Implementation
107 // run - On a module, we run this pass by initializing, ronOnMethod'ing once
108 // for every method in the module, then by finalizing.
110 bool MethodPass::run(Module *M) {
111 bool Changed = doInitialization(M);
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);
117 return Changed | doFinalization(M);
120 // run - On a method, we simply initialize, run the method, then finalize.
122 bool MethodPass::run(Method *M) {
123 if (M->isExternal()) return false; // Passes are not run on external methods!
125 return doInitialization(M->getParent()) | runOnMethod(M)
126 | doFinalization(M->getParent());
129 void MethodPass::addToPassManager(PassManagerT<Module> *PM,
130 AnalysisSet &Required, AnalysisSet &Destroyed,
131 AnalysisSet &Provided) {
132 PM->addPass(this, Required, Destroyed, Provided);
135 void MethodPass::addToPassManager(PassManagerT<Method> *PM,
136 AnalysisSet &Required, AnalysisSet &Destroyed,
137 AnalysisSet &Provided) {
138 PM->addPass(this, Required, Destroyed, Provided);
141 //===----------------------------------------------------------------------===//
142 // BasicBlockPass Implementation
145 // To run this pass on a method, we simply call runOnBasicBlock once for each
148 bool BasicBlockPass::runOnMethod(Method *M) {
149 bool Changed = false;
150 for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
151 Changed |= runOnBasicBlock(*I);
155 // To run directly on the basic block, we initialize, runOnBasicBlock, then
158 bool BasicBlockPass::run(BasicBlock *BB) {
159 Module *M = BB->getParent()->getParent();
160 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
163 void BasicBlockPass::addToPassManager(PassManagerT<Method> *PM,
164 AnalysisSet &Required,
165 AnalysisSet &Destroyed,
166 AnalysisSet &Provided) {
167 PM->addPass(this, Required, Destroyed, Provided);
170 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
171 AnalysisSet &Required,
172 AnalysisSet &Destroyed,
173 AnalysisSet &Provided) {
174 PM->addPass(this, Required, Destroyed, Provided);