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 "PassManagerT.h" // PassManagerT implementation
11 #include "llvm/Module.h"
12 #include "llvm/Function.h"
13 #include "llvm/BasicBlock.h"
14 #include "Support/STLExtras.h"
15 #include "Support/CommandLine.h"
19 // Source of unique analysis ID #'s.
20 unsigned AnalysisID::NextID = 0;
22 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
23 assert(P->Resolver == 0 && "Pass already in a PassManager!");
27 //===----------------------------------------------------------------------===//
28 // PassManager implementation - The PassManager class is a simple Pimpl class
29 // that wraps the PassManagerT template.
31 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
32 PassManager::~PassManager() { delete PM; }
33 void PassManager::add(Pass *P) { PM->add(P); }
34 bool PassManager::run(Module *M) { return PM->run(M); }
37 //===----------------------------------------------------------------------===//
38 // Pass debugging information. Often it is useful to find out what pass is
39 // running when a crash occurs in a utility. When this library is compiled with
40 // debugging on, a command line option (--debug-pass) is enabled that causes the
41 // pass name to be printed before it executes.
44 // Different debug levels that can be enabled...
46 None, PassStructure, PassExecutions, PassDetails
49 static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
50 "Print PassManager debugging information",
51 clEnumVal(None , "disable debug output"),
52 clEnumVal(PassStructure , "print pass structure before run()"),
53 clEnumVal(PassExecutions, "print pass name before it is executed"),
54 clEnumVal(PassDetails , "print pass details when it is executed"), 0);
56 void PMDebug::PrintPassStructure(Pass *P) {
57 if (PassDebugging >= PassStructure)
58 P->dumpPassStructure();
61 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
62 Pass *P, Annotable *V) {
63 if (PassDebugging >= PassExecutions) {
64 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
69 if (dynamic_cast<Module*>(V)) {
70 std::cerr << "Module\n"; return;
71 } else if (Function *F = dynamic_cast<Function*>(V))
72 std::cerr << "Function '" << F->getName();
73 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
74 std::cerr << "BasicBlock '" << BB->getName();
75 else if (Value *Val = dynamic_cast<Value*>(V))
76 std::cerr << typeid(*Val).name() << " '" << Val->getName();
78 std::cerr << "'...\n";
82 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
83 Pass *P, const std::vector<AnalysisID> &Set){
84 if (PassDebugging >= PassDetails && !Set.empty()) {
85 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
86 for (unsigned i = 0; i != Set.size(); ++i) {
87 Pass *P = Set[i].createPass(); // Good thing this is just debug code...
88 std::cerr << " " << typeid(*P).name();
95 // dumpPassStructure - Implement the -debug-passes=PassStructure option
96 void Pass::dumpPassStructure(unsigned Offset = 0) {
97 std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
101 //===----------------------------------------------------------------------===//
102 // Pass Implementation
105 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
106 PM->addPass(this, AU);
109 //===----------------------------------------------------------------------===//
110 // FunctionPass Implementation
113 // run - On a module, we run this pass by initializing, runOnFunction'ing once
114 // for every function in the module, then by finalizing.
116 bool FunctionPass::run(Module *M) {
117 bool Changed = doInitialization(M);
119 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
120 if (!(*I)->isExternal()) // Passes are not run on external functions!
121 Changed |= runOnFunction(*I);
123 return Changed | doFinalization(M);
126 // run - On a function, we simply initialize, run the function, then finalize.
128 bool FunctionPass::run(Function *F) {
129 if (F->isExternal()) return false;// Passes are not run on external functions!
131 return doInitialization(F->getParent()) | runOnFunction(F)
132 | doFinalization(F->getParent());
135 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
137 PM->addPass(this, AU);
140 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
142 PM->addPass(this, AU);
145 // doesNotModifyCFG - This function should be called by our subclasses to
146 // implement the getAnalysisUsage virtual function, iff they do not:
148 // 1. Add or remove basic blocks from the function
149 // 2. Modify terminator instructions in any way.
151 // This function annotates the AnalysisUsage info object to say that analyses
152 // that only depend on the CFG are preserved by this pass.
154 void FunctionPass::doesNotModifyCFG(AnalysisUsage &Info) {
159 //===----------------------------------------------------------------------===//
160 // BasicBlockPass Implementation
163 // To run this pass on a function, we simply call runOnBasicBlock once for each
166 bool BasicBlockPass::runOnFunction(Function *F) {
167 bool Changed = false;
168 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
169 Changed |= runOnBasicBlock(*I);
173 // To run directly on the basic block, we initialize, runOnBasicBlock, then
176 bool BasicBlockPass::run(BasicBlock *BB) {
177 Module *M = BB->getParent()->getParent();
178 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
181 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
183 PM->addPass(this, AU);
186 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
188 PM->addPass(this, AU);