Move FunctionPass::doesNotModifyCFG to AnalysisUsage::preservesCFG()
[oota-llvm.git] / lib / VMCore / Pass.cpp
1 //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
2 //
3 // This file implements the LLVM Pass infrastructure.  It is primarily
4 // responsible with ensuring that passes are executed and batched together
5 // optimally.
6 //
7 //===----------------------------------------------------------------------===//
8
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"
16 #include <typeinfo>
17 #include <iostream>
18
19 // Source of unique analysis ID #'s.
20 unsigned AnalysisID::NextID = 0;
21
22 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
23   assert(P->Resolver == 0 && "Pass already in a PassManager!");
24   P->Resolver = AR;
25 }
26
27
28 // preservesCFG - This function should be called to by the pass, iff they do
29 // not:
30 //
31 //  1. Add or remove basic blocks from the function
32 //  2. Modify terminator instructions in any way.
33 //
34 // This function annotates the AnalysisUsage info object to say that analyses
35 // that only depend on the CFG are preserved by this pass.
36 //
37 void AnalysisUsage::preservesCFG() {
38   // FIXME: implement preservesCFG
39 }
40
41
42 //===----------------------------------------------------------------------===//
43 // PassManager implementation - The PassManager class is a simple Pimpl class
44 // that wraps the PassManagerT template.
45 //
46 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
47 PassManager::~PassManager() { delete PM; }
48 void PassManager::add(Pass *P) { PM->add(P); }
49 bool PassManager::run(Module *M) { return PM->run(M); }
50
51
52 //===----------------------------------------------------------------------===//
53 // Pass debugging information.  Often it is useful to find out what pass is
54 // running when a crash occurs in a utility.  When this library is compiled with
55 // debugging on, a command line option (--debug-pass) is enabled that causes the
56 // pass name to be printed before it executes.
57 //
58
59 // Different debug levels that can be enabled...
60 enum PassDebugLevel {
61   None, PassStructure, PassExecutions, PassDetails
62 };
63
64 static cl::Enum<enum PassDebugLevel> PassDebugging("debug-pass", cl::Hidden,
65   "Print PassManager debugging information",
66   clEnumVal(None          , "disable debug output"),
67   clEnumVal(PassStructure , "print pass structure before run()"),
68   clEnumVal(PassExecutions, "print pass name before it is executed"),
69   clEnumVal(PassDetails   , "print pass details when it is executed"), 0); 
70
71 void PMDebug::PrintPassStructure(Pass *P) {
72   if (PassDebugging >= PassStructure)
73     P->dumpPassStructure();
74 }
75
76 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
77                                    Pass *P, Annotable *V) {
78   if (PassDebugging >= PassExecutions) {
79     std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" 
80               << typeid(*P).name();
81     if (V) {
82       std::cerr << "' on ";
83
84       if (dynamic_cast<Module*>(V)) {
85         std::cerr << "Module\n"; return;
86       } else if (Function *F = dynamic_cast<Function*>(V))
87         std::cerr << "Function '" << F->getName();
88       else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
89         std::cerr << "BasicBlock '" << BB->getName();
90       else if (Value *Val = dynamic_cast<Value*>(V))
91         std::cerr << typeid(*Val).name() << " '" << Val->getName();
92     }
93     std::cerr << "'...\n";
94   }
95 }
96
97 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
98                                    Pass *P, const std::vector<AnalysisID> &Set){
99   if (PassDebugging >= PassDetails && !Set.empty()) {
100     std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
101     for (unsigned i = 0; i != Set.size(); ++i) {
102       Pass *P = Set[i].createPass();   // Good thing this is just debug code...
103       std::cerr << "  " << typeid(*P).name();
104       delete P;
105     }
106     std::cerr << "\n";
107   }
108 }
109
110 // dumpPassStructure - Implement the -debug-passes=PassStructure option
111 void Pass::dumpPassStructure(unsigned Offset = 0) {
112   std::cerr << std::string(Offset*2, ' ') << typeid(*this).name() << "\n";
113 }
114
115
116 //===----------------------------------------------------------------------===//
117 // Pass Implementation
118 //
119
120 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
121   PM->addPass(this, AU);
122 }
123
124 //===----------------------------------------------------------------------===//
125 // FunctionPass Implementation
126 //
127
128 // run - On a module, we run this pass by initializing, runOnFunction'ing once
129 // for every function in the module, then by finalizing.
130 //
131 bool FunctionPass::run(Module *M) {
132   bool Changed = doInitialization(M);
133   
134   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
135     if (!(*I)->isExternal())      // Passes are not run on external functions!
136     Changed |= runOnFunction(*I);
137   
138   return Changed | doFinalization(M);
139 }
140
141 // run - On a function, we simply initialize, run the function, then finalize.
142 //
143 bool FunctionPass::run(Function *F) {
144   if (F->isExternal()) return false;// Passes are not run on external functions!
145
146   return doInitialization(F->getParent()) | runOnFunction(F)
147        | doFinalization(F->getParent());
148 }
149
150 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
151                                     AnalysisUsage &AU) {
152   PM->addPass(this, AU);
153 }
154
155 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
156                                     AnalysisUsage &AU) {
157   PM->addPass(this, AU);
158 }
159
160 //===----------------------------------------------------------------------===//
161 // BasicBlockPass Implementation
162 //
163
164 // To run this pass on a function, we simply call runOnBasicBlock once for each
165 // function.
166 //
167 bool BasicBlockPass::runOnFunction(Function *F) {
168   bool Changed = false;
169   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
170     Changed |= runOnBasicBlock(*I);
171   return Changed;
172 }
173
174 // To run directly on the basic block, we initialize, runOnBasicBlock, then
175 // finalize.
176 //
177 bool BasicBlockPass::run(BasicBlock *BB) {
178   Module *M = BB->getParent()->getParent();
179   return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
180 }
181
182 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
183                                       AnalysisUsage &AU) {
184   PM->addPass(this, AU);
185 }
186
187 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
188                                       AnalysisUsage &AU) {
189   PM->addPass(this, AU);
190 }
191