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,
52 Pass *P, Annotable *V) {
53 if (PassDebugging >= PassExecutions) {
54 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
59 if (dynamic_cast<Module*>(V)) {
60 std::cerr << "Module\n"; return;
61 } else if (Function *F = dynamic_cast<Function*>(V))
62 std::cerr << "Function '" << F->getName();
63 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
64 std::cerr << "BasicBlock '" << BB->getName();
65 else if (Value *Val = dynamic_cast<Value*>(V))
66 std::cerr << typeid(*Val).name() << " '" << Val->getName();
68 std::cerr << "'...\n";
72 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
73 Pass *P, const std::vector<AnalysisID> &Set){
74 if (PassDebugging >= PassDetails && !Set.empty()) {
75 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
76 for (unsigned i = 0; i != Set.size(); ++i) {
77 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
78 std::cerr << " " << typeid(*P).name();
85 // dumpPassStructure - Implement the -debug-passes=PassStructure option
86 void Pass::dumpPassStructure(unsigned Offset = 0) {
87 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
91 //===----------------------------------------------------------------------===//
92 // Pass Implementation
95 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
96 PM->addPass(this, AU);
99 //===----------------------------------------------------------------------===//
100 // FunctionPass Implementation
103 // run - On a module, we run this pass by initializing, runOnFunction'ing once
104 // for every function in the module, then by finalizing.
106 bool FunctionPass::run(Module *M) {
107 bool Changed = doInitialization(M);
109 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
110 if (!(*I)->isExternal()) // Passes are not run on external functions!
111 Changed |= runOnFunction(*I);
113 return Changed | doFinalization(M);
116 // run - On a function, we simply initialize, run the function, then finalize.
118 bool FunctionPass::run(Function *F) {
119 if (F->isExternal()) return false;// Passes are not run on external functions!
121 return doInitialization(F->getParent()) | runOnFunction(F)
122 | doFinalization(F->getParent());
125 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
127 PM->addPass(this, AU);
130 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
132 PM->addPass(this, AU);
135 //===----------------------------------------------------------------------===//
136 // BasicBlockPass Implementation
139 // To run this pass on a function, we simply call runOnBasicBlock once for each
142 bool BasicBlockPass::runOnFunction(Function *F) {
143 bool Changed = false;
144 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
145 Changed |= runOnBasicBlock(*I);
149 // To run directly on the basic block, we initialize, runOnBasicBlock, then
152 bool BasicBlockPass::run(BasicBlock *BB) {
153 Module *M = BB->getParent()->getParent();
154 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
157 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
159 PM->addPass(this, AU);
162 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
164 PM->addPass(this, AU);