13df79390102756576830973a094ef55946f381e
[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/Pass.h"
10 #include "Support/STLExtras.h"
11 #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
20 inline 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
28 static cl::Flag PassDebugEnabled("debug-pass",
29   "Print pass names as they are executed by the PassManager", cl::Hidden);
30
31 static 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
39
40 PassManager::~PassManager() {
41   for_each(Passes.begin(), Passes.end(), deleter<Pass>);
42 }
43
44 class BasicBlockPassBatcher : public MethodPass {
45   typedef std::vector<BasicBlockPass*> SubPassesType;
46   SubPassesType SubPasses;
47 public:
48   ~BasicBlockPassBatcher() {
49     for_each(SubPasses.begin(), SubPasses.end(), deleter<BasicBlockPass>);
50   }
51
52   void add(BasicBlockPass *P) { SubPasses.push_back(P); }
53
54   virtual bool doInitialization(Module *M) {
55     bool Changed = false;
56     for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
57          I != E; ++I) {
58       PrintPassInformation("Initializing", *I, M);
59       Changed |= (*I)->doInitialization(M);
60     }
61     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();
69            I != E; ++I) {
70         PrintPassInformation("Executing", *I, *MI);
71         Changed |= (*I)->runOnBasicBlock(*MI);
72       }
73     return Changed;
74   }
75
76   virtual bool doFinalization(Module *M) {
77     bool Changed = false;
78     for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
79          I != E; ++I) {
80       PrintPassInformation("Finalizing", *I, M);
81       Changed |= (*I)->doFinalization(M);
82     }
83     return Changed;
84   }
85 };
86
87 class MethodPassBatcher : public Pass {
88   typedef std::vector<MethodPass*> SubPassesType;
89   SubPassesType SubPasses;
90   BasicBlockPassBatcher *BBPBatcher;
91 public:
92   inline MethodPassBatcher() : BBPBatcher(0) {}
93
94   inline ~MethodPassBatcher() {
95     for_each(SubPasses.begin(), SubPasses.end(), deleter<MethodPass>);
96   }
97
98   void add(BasicBlockPass *BBP) {
99     if (BBPBatcher == 0) {
100       BBPBatcher = new BasicBlockPassBatcher();
101       SubPasses.push_back(BBPBatcher);
102     }
103     BBPBatcher->add(BBP);
104   }
105
106   void add(MethodPass *P) {
107     if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) {
108       add(BBP);
109     } 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();
118          I != E; ++I) {
119       PrintPassInformation("Initializing", *I, M);
120       Changed |= (*I)->doInitialization(M);
121     }
122
123     for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
124       for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
125            I != E; ++I) {
126         PrintPassInformation("Executing", *I, M);
127         Changed |= (*I)->runOnMethod(*MI);
128       }
129
130     for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
131          I != E; ++I) {
132       PrintPassInformation("Finalizing", *I, M);
133       Changed |= (*I)->doFinalization(M);
134     }
135     return Changed;
136   }
137 };
138
139 // add(BasicBlockPass*) - If we know it's a BasicBlockPass, we don't have to do
140 // any checking...
141 //
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);
145   else
146     Batcher->add(BBP);
147 }
148
149
150 // add(MethodPass*) - MethodPass's must be batched together... make sure this
151 // happens now.
152 //
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);
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...
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
165   } else {
166     Batcher = 0;  // Ensure that passes don't get accidentally reordered
167     Passes.push_back(P);
168   }
169 }
170
171
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);
178   }
179   return MadeChanges;
180 }