Eliminate the PromoteInstance class, incorporating it into the PromotePass
[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 "llvm/Module.h"
11 #include "llvm/Function.h"
12 #include "llvm/BasicBlock.h"
13 #include "Support/STLExtras.h"
14 #include <algorithm>
15
16 // Source of unique analysis ID #'s.
17 unsigned AnalysisID::NextID = 0;
18
19 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
20   assert(P->Resolver == 0 && "Pass already in a PassManager!");
21   P->Resolver = AR;
22 }
23
24
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.
29 //
30 #include "Support/CommandLine.h"
31 #include <typeinfo>
32 #include <iostream>
33
34 // Different debug levels that can be enabled...
35 enum PassDebugLevel {
36   None, PassStructure, PassExecutions, PassDetails
37 };
38
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); 
45
46 void PMDebug::PrintPassStructure(Pass *P) {
47   if (PassDebugging >= PassStructure)
48     P->dumpPassStructure();
49 }
50
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 << " '" 
55               << typeid(*P).name();
56     if (V) {
57       std::cerr << "' on ";
58
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();
67     }
68     std::cerr << "'...\n";
69   }
70 }
71
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();
79       delete P;
80     }
81     std::cerr << "\n";
82   }
83 }
84
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";
88 }
89
90
91 //===----------------------------------------------------------------------===//
92 // Pass Implementation
93 //
94
95 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
96   PM->addPass(this, AU);
97 }
98
99 //===----------------------------------------------------------------------===//
100 // FunctionPass Implementation
101 //
102
103 // run - On a module, we run this pass by initializing, runOnFunction'ing once
104 // for every function in the module, then by finalizing.
105 //
106 bool FunctionPass::run(Module *M) {
107   bool Changed = doInitialization(M);
108   
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);
112   
113   return Changed | doFinalization(M);
114 }
115
116 // run - On a function, we simply initialize, run the function, then finalize.
117 //
118 bool FunctionPass::run(Function *F) {
119   if (F->isExternal()) return false;// Passes are not run on external functions!
120
121   return doInitialization(F->getParent()) | runOnFunction(F)
122        | doFinalization(F->getParent());
123 }
124
125 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
126                                     AnalysisUsage &AU) {
127   PM->addPass(this, AU);
128 }
129
130 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
131                                     AnalysisUsage &AU) {
132   PM->addPass(this, AU);
133 }
134
135 //===----------------------------------------------------------------------===//
136 // BasicBlockPass Implementation
137 //
138
139 // To run this pass on a function, we simply call runOnBasicBlock once for each
140 // function.
141 //
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);
146   return Changed;
147 }
148
149 // To run directly on the basic block, we initialize, runOnBasicBlock, then
150 // finalize.
151 //
152 bool BasicBlockPass::run(BasicBlock *BB) {
153   Module *M = BB->getParent()->getParent();
154   return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
155 }
156
157 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
158                                       AnalysisUsage &AU) {
159   PM->addPass(this, AU);
160 }
161
162 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
163                                       AnalysisUsage &AU) {
164   PM->addPass(this, AU);
165 }
166