blob: 13df79390102756576830973a094ef55946f381e [file] [log] [blame]
Chris Lattner26e4f892002-01-21 07:37:31 +00001//===- 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/Pass.h"
10#include "Support/STLExtras.h"
Chris Lattnerd013ba92002-01-23 05:49:41 +000011#include <algorithm>
12
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.
17//
18#ifdef NDEBUG
19// If not debugging, remove the option
20inline static void PrintPassInformation(const char *, Pass *, Value *) { }
21#else
22
23#include "Support/CommandLine.h"
24#include <typeinfo>
25#include <iostream>
26
27// The option is hidden from --help by default
28static cl::Flag PassDebugEnabled("debug-pass",
29 "Print pass names as they are executed by the PassManager", cl::Hidden);
30
31static void PrintPassInformation(const char *Action, Pass *P, Value *V) {
32 if (PassDebugEnabled)
33 std::cerr << Action << " Pass '" << typeid(*P).name() << "' on "
34 << typeid(*V).name() << " '" << V->getName() << "'...\n";
35}
36#endif
37
38
Chris Lattner26e4f892002-01-21 07:37:31 +000039
40PassManager::~PassManager() {
41 for_each(Passes.begin(), Passes.end(), deleter<Pass>);
42}
43
44class BasicBlockPassBatcher : public MethodPass {
45 typedef std::vector<BasicBlockPass*> SubPassesType;
46 SubPassesType SubPasses;
47public:
48 ~BasicBlockPassBatcher() {
49 for_each(SubPasses.begin(), SubPasses.end(), deleter<BasicBlockPass>);
50 }
51
52 void add(BasicBlockPass *P) { SubPasses.push_back(P); }
53
Chris Lattner9c56a282002-01-22 03:30:25 +000054 virtual bool doInitialization(Module *M) {
Chris Lattner26e4f892002-01-21 07:37:31 +000055 bool Changed = false;
56 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
Chris Lattnerd013ba92002-01-23 05:49:41 +000057 I != E; ++I) {
58 PrintPassInformation("Initializing", *I, M);
Chris Lattner26e4f892002-01-21 07:37:31 +000059 Changed |= (*I)->doInitialization(M);
Chris Lattnerd013ba92002-01-23 05:49:41 +000060 }
Chris Lattner26e4f892002-01-21 07:37:31 +000061 return Changed;
62 }
63
64 virtual bool runOnMethod(Method *M) {
65 bool Changed = false;
66
67 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
68 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
Chris Lattnerd013ba92002-01-23 05:49:41 +000069 I != E; ++I) {
70 PrintPassInformation("Executing", *I, *MI);
Chris Lattner26e4f892002-01-21 07:37:31 +000071 Changed |= (*I)->runOnBasicBlock(*MI);
Chris Lattnerd013ba92002-01-23 05:49:41 +000072 }
Chris Lattner26e4f892002-01-21 07:37:31 +000073 return Changed;
74 }
75
76 virtual bool doFinalization(Module *M) {
77 bool Changed = false;
78 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
Chris Lattnerd013ba92002-01-23 05:49:41 +000079 I != E; ++I) {
80 PrintPassInformation("Finalizing", *I, M);
Chris Lattner26e4f892002-01-21 07:37:31 +000081 Changed |= (*I)->doFinalization(M);
Chris Lattnerd013ba92002-01-23 05:49:41 +000082 }
Chris Lattner26e4f892002-01-21 07:37:31 +000083 return Changed;
84 }
85};
86
87class MethodPassBatcher : public Pass {
88 typedef std::vector<MethodPass*> SubPassesType;
89 SubPassesType SubPasses;
90 BasicBlockPassBatcher *BBPBatcher;
91public:
Chris Lattnerd013ba92002-01-23 05:49:41 +000092 inline MethodPassBatcher() : BBPBatcher(0) {}
93
94 inline ~MethodPassBatcher() {
Chris Lattner26e4f892002-01-21 07:37:31 +000095 for_each(SubPasses.begin(), SubPasses.end(), deleter<MethodPass>);
96 }
97
Chris Lattner654b5bc2002-01-22 00:17:48 +000098 void add(BasicBlockPass *BBP) {
99 if (BBPBatcher == 0) {
100 BBPBatcher = new BasicBlockPassBatcher();
101 SubPasses.push_back(BBPBatcher);
102 }
103 BBPBatcher->add(BBP);
104 }
105
Chris Lattner26e4f892002-01-21 07:37:31 +0000106 void add(MethodPass *P) {
107 if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) {
Chris Lattner654b5bc2002-01-22 00:17:48 +0000108 add(BBP);
Chris Lattner26e4f892002-01-21 07:37:31 +0000109 } else {
110 BBPBatcher = 0; // Ensure that passes don't get accidentally reordered
111 SubPasses.push_back(P);
112 }
113 }
114
115 virtual bool run(Module *M) {
116 bool Changed = false;
117 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
Chris Lattnerd013ba92002-01-23 05:49:41 +0000118 I != E; ++I) {
119 PrintPassInformation("Initializing", *I, M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000120 Changed |= (*I)->doInitialization(M);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000121 }
Chris Lattner26e4f892002-01-21 07:37:31 +0000122
123 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
124 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
Chris Lattnerd013ba92002-01-23 05:49:41 +0000125 I != E; ++I) {
126 PrintPassInformation("Executing", *I, M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000127 Changed |= (*I)->runOnMethod(*MI);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000128 }
Chris Lattner26e4f892002-01-21 07:37:31 +0000129
130 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
Chris Lattnerd013ba92002-01-23 05:49:41 +0000131 I != E; ++I) {
132 PrintPassInformation("Finalizing", *I, M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000133 Changed |= (*I)->doFinalization(M);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000134 }
Chris Lattner26e4f892002-01-21 07:37:31 +0000135 return Changed;
136 }
137};
138
Chris Lattner654b5bc2002-01-22 00:17:48 +0000139// add(BasicBlockPass*) - If we know it's a BasicBlockPass, we don't have to do
140// any checking...
141//
142void PassManager::add(BasicBlockPass *BBP) {
143 if (Batcher == 0) // If we don't have a batcher yet, make one now.
144 add((MethodPass*)BBP);
145 else
146 Batcher->add(BBP);
147}
Chris Lattner26e4f892002-01-21 07:37:31 +0000148
149
150// add(MethodPass*) - MethodPass's must be batched together... make sure this
151// happens now.
152//
153void 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);
157 }
158 Batcher->add(MP); // The Batcher will queue them passes up
159}
160
161// add - Add a pass to the PassManager, batching it up as appropriate...
162void PassManager::add(Pass *P) {
163 if (MethodPass *MP = dynamic_cast<MethodPass*>(P)) {
164 add(MP); // Use the methodpass specific code to do the addition
165 } else {
166 Batcher = 0; // Ensure that passes don't get accidentally reordered
167 Passes.push_back(P);
168 }
169}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000170
171
172bool 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);
178 }
179 return MadeChanges;
180}