blob: a3d03f13b3db3deedc12d6fc33e3235b8eb53163 [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"
11
12PassManager::~PassManager() {
13 for_each(Passes.begin(), Passes.end(), deleter<Pass>);
14}
15
16class BasicBlockPassBatcher : public MethodPass {
17 typedef std::vector<BasicBlockPass*> SubPassesType;
18 SubPassesType SubPasses;
19public:
20 ~BasicBlockPassBatcher() {
21 for_each(SubPasses.begin(), SubPasses.end(), deleter<BasicBlockPass>);
22 }
23
24 void add(BasicBlockPass *P) { SubPasses.push_back(P); }
25
26 virtual bool doPassInitialization(Module *M) {
27 bool Changed = false;
28 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
29 I != E; ++I)
30 Changed |= (*I)->doInitialization(M);
31 return Changed;
32 }
33
34 virtual bool runOnMethod(Method *M) {
35 bool Changed = false;
36
37 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
38 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
39 I != E; ++I)
40 Changed |= (*I)->runOnBasicBlock(*MI);
41 return Changed;
42 }
43
44 virtual bool doFinalization(Module *M) {
45 bool Changed = false;
46 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
47 I != E; ++I)
48 Changed |= (*I)->doFinalization(M);
49
50 return Changed;
51 }
52};
53
54class MethodPassBatcher : public Pass {
55 typedef std::vector<MethodPass*> SubPassesType;
56 SubPassesType SubPasses;
57 BasicBlockPassBatcher *BBPBatcher;
58public:
59 ~MethodPassBatcher() {
60 for_each(SubPasses.begin(), SubPasses.end(), deleter<MethodPass>);
61 }
62
Chris Lattner654b5bc2002-01-22 00:17:48 +000063 void add(BasicBlockPass *BBP) {
64 if (BBPBatcher == 0) {
65 BBPBatcher = new BasicBlockPassBatcher();
66 SubPasses.push_back(BBPBatcher);
67 }
68 BBPBatcher->add(BBP);
69 }
70
Chris Lattner26e4f892002-01-21 07:37:31 +000071 void add(MethodPass *P) {
72 if (BasicBlockPass *BBP = dynamic_cast<BasicBlockPass*>(P)) {
Chris Lattner654b5bc2002-01-22 00:17:48 +000073 add(BBP);
Chris Lattner26e4f892002-01-21 07:37:31 +000074 } else {
75 BBPBatcher = 0; // Ensure that passes don't get accidentally reordered
76 SubPasses.push_back(P);
77 }
78 }
79
80 virtual bool run(Module *M) {
81 bool Changed = false;
82 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
83 I != E; ++I)
84 Changed |= (*I)->doInitialization(M);
85
86 for (Module::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI)
87 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
88 I != E; ++I)
89 Changed |= (*I)->runOnMethod(*MI);
90
91 for (SubPassesType::iterator I = SubPasses.begin(), E = SubPasses.end();
92 I != E; ++I)
93 Changed |= (*I)->doFinalization(M);
94
95 return Changed;
96 }
97};
98
Chris Lattner654b5bc2002-01-22 00:17:48 +000099// add(BasicBlockPass*) - If we know it's a BasicBlockPass, we don't have to do
100// any checking...
101//
102void PassManager::add(BasicBlockPass *BBP) {
103 if (Batcher == 0) // If we don't have a batcher yet, make one now.
104 add((MethodPass*)BBP);
105 else
106 Batcher->add(BBP);
107}
Chris Lattner26e4f892002-01-21 07:37:31 +0000108
109
110// add(MethodPass*) - MethodPass's must be batched together... make sure this
111// happens now.
112//
113void PassManager::add(MethodPass *MP) {
114 if (Batcher == 0) { // If we don't have a batcher yet, make one now.
115 Batcher = new MethodPassBatcher();
116 Passes.push_back(Batcher);
117 }
118 Batcher->add(MP); // The Batcher will queue them passes up
119}
120
121// add - Add a pass to the PassManager, batching it up as appropriate...
122void PassManager::add(Pass *P) {
123 if (MethodPass *MP = dynamic_cast<MethodPass*>(P)) {
124 add(MP); // Use the methodpass specific code to do the addition
125 } else {
126 Batcher = 0; // Ensure that passes don't get accidentally reordered
127 Passes.push_back(P);
128 }
129}