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/Function.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.
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::FunctionVal:
62 std::cerr << "Function '" << 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";
92 //===----------------------------------------------------------------------===//
93 // Pass Implementation
96 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisSet &Required,
97 AnalysisSet &Destroyed, AnalysisSet &Provided) {
98 PM->addPass(this, Required, Destroyed, Provided);
101 //===----------------------------------------------------------------------===//
102 // MethodPass Implementation
105 // run - On a module, we run this pass by initializing, ronOnMethod'ing once
106 // for every method in the module, then by finalizing.
108 bool MethodPass::run(Module *M) {
109 bool Changed = doInitialization(M);
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);
115 return Changed | doFinalization(M);
118 // run - On a method, we simply initialize, run the method, then finalize.
120 bool MethodPass::run(Function *F) {
121 if (F->isExternal()) return false; // Passes are not run on external methods!
123 return doInitialization(F->getParent()) | runOnMethod(F)
124 | doFinalization(F->getParent());
127 void MethodPass::addToPassManager(PassManagerT<Module> *PM,
128 AnalysisSet &Required, AnalysisSet &Destroyed,
129 AnalysisSet &Provided) {
130 PM->addPass(this, Required, Destroyed, Provided);
133 void MethodPass::addToPassManager(PassManagerT<Function> *PM,
134 AnalysisSet &Required, AnalysisSet &Destroyed,
135 AnalysisSet &Provided) {
136 PM->addPass(this, Required, Destroyed, Provided);
139 //===----------------------------------------------------------------------===//
140 // BasicBlockPass Implementation
143 // To run this pass on a method, we simply call runOnBasicBlock once for each
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);
153 // To run directly on the basic block, we initialize, runOnBasicBlock, then
156 bool BasicBlockPass::run(BasicBlock *BB) {
157 Module *M = BB->getParent()->getParent();
158 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
161 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
162 AnalysisSet &Required,
163 AnalysisSet &Destroyed,
164 AnalysisSet &Provided) {
165 PM->addPass(this, Required, Destroyed, Provided);
168 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
169 AnalysisSet &Required,
170 AnalysisSet &Destroyed,
171 AnalysisSet &Provided) {
172 PM->addPass(this, Required, Destroyed, Provided);