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 //===----------------------------------------------------------------------===//
10 #include "Support/STLExtras.h"
13 // Pass debugging information. Often it is useful to find out what pass is
14 // running when a crash occurs in a utility. When this library is compiled with
15 // debugging on, a command line option (--debug-pass) is enabled that causes the
16 // pass name to be printed before it executes.
19 // If not debugging, remove the option
20 inline static void PrintPassInformation(const char *, Pass *, Value *) { }
23 #include "Support/CommandLine.h"
27 // The option is hidden from --help by default
28 static cl::Flag PassDebugEnabled("debug-pass",
29 "Print pass names as they are executed by the PassManager", cl::Hidden);
31 static void PrintPassInformation(const char *Action, Pass *P, Value *V) {
33 std::cerr << Action << " Pass '" << typeid(*P).name() << "' on "
34 << typeid(*V).name() << " '" << V->getName() << "'...\n";
40 PassManager::~PassManager() {
41 for_each(Passes.begin(), Passes.end(), deleter<Pass>);
44 class BasicBlockPassBatcher : public MethodPass {
45 typedef std::vector<BasicBlockPass*> SubPassesType;
46 SubPassesType SubPasses;
48 ~BasicBlockPassBatcher() {
49 for_each(SubPasses.begin(), SubPasses.end(), deleter<BasicBlockPass>);
52 void add(BasicBlockPass *P) { SubPasses.push_back(P); }
54 virtual bool doInitialization(Module *M) {
56 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
58 PrintPassInformation("Initializing", *I, M);
59 Changed |= (*I)->doInitialization(M);
64 virtual bool runOnMethod(Method *M) {
67 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
68 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
70 PrintPassInformation("Executing", *I, *MI);
71 Changed |= (*I)->runOnBasicBlock(*MI);
76 virtual bool doFinalization(Module *M) {
78 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
80 PrintPassInformation("Finalizing", *I, M);
81 Changed |= (*I)->doFinalization(M);
87 class MethodPassBatcher : public Pass {
88 typedef std::vector<MethodPass*> SubPassesType;
89 SubPassesType SubPasses;
90 BasicBlockPassBatcher *BBPBatcher;
92 inline MethodPassBatcher() : BBPBatcher(0) {}
94 inline ~MethodPassBatcher() {
95 for_each(SubPasses.begin(), SubPasses.end(), deleter<MethodPass>);
98 void add(BasicBlockPass *BBP) {
99 if (BBPBatcher == 0) {
100 BBPBatcher = new BasicBlockPassBatcher();
101 SubPasses.push_back(BBPBatcher);
103 BBPBatcher->add(BBP);
106 void add(MethodPass *P) {
107 if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) {
110 BBPBatcher = 0; // Ensure that passes don't get accidentally reordered
111 SubPasses.push_back(P);
115 virtual bool run(Module *M) {
116 bool Changed = false;
117 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
119 PrintPassInformation("Initializing", *I, M);
120 Changed |= (*I)->doInitialization(M);
123 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
124 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
126 PrintPassInformation("Executing", *I, M);
127 Changed |= (*I)->runOnMethod(*MI);
130 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
132 PrintPassInformation("Finalizing", *I, M);
133 Changed |= (*I)->doFinalization(M);
139 // add(BasicBlockPass*) - If we know it's a BasicBlockPass, we don't have to do
142 void PassManager::add(BasicBlockPass *BBP) {
143 if (Batcher == 0) // If we don't have a batcher yet, make one now.
144 add((MethodPass*)BBP);
150 // add(MethodPass*) - MethodPass's must be batched together... make sure this
153 void PassManager::add(MethodPass *MP) {
154 if (Batcher == 0) { // If we don't have a batcher yet, make one now.
155 Batcher = new MethodPassBatcher();
156 Passes.push_back(Batcher);
158 Batcher->add(MP); // The Batcher will queue them passes up
161 // add - Add a pass to the PassManager, batching it up as appropriate...
162 void PassManager::add(Pass *P) {
163 if (MethodPass *MP = dynamic_cast<MethodPass*>(P)) {
164 add(MP); // Use the methodpass specific code to do the addition
166 Batcher = 0; // Ensure that passes don't get accidentally reordered
172 bool PassManager::run(Module *M) {
173 bool MadeChanges = false;
174 // Run all of the pass initializers
175 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
176 PrintPassInformation("Executing", Passes[i], M);
177 MadeChanges |= Passes[i]->run(M);