Pull run() into Pass.cpp
[oota-llvm.git] / include / llvm / Pass.h
1 //===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=//
2 //
3 // This file defines a base class that indicates that a specified class is a
4 // transformation pass implementation.
5 //
6 // Pass's are designed this way so that it is possible to run passes in a cache
7 // and organizationally optimal order without having to specify it at the front
8 // end.  This allows arbitrary passes to be strung together and have them
9 // executed as effeciently as possible.
10 //
11 // Passes should extend one of the classes below, depending on the guarantees
12 // that it can make about what will be modified as it is run.  For example, most
13 // global optimizations should derive from MethodPass, because they do not add
14 // or delete methods, they operate on the internals of the method.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_PASS_H
19 #define LLVM_PASS_H
20
21 #include "llvm/Module.h"
22 #include "llvm/Method.h"
23
24 class MethodPassBatcher;
25
26 //===----------------------------------------------------------------------===//
27 // Pass interface - Implemented by all 'passes'.  Subclass this if you are an
28 // interprocedural optimization or you do not fit into any of the more
29 // constrained passes described below.
30 //
31 struct Pass {
32   // Destructor - Virtual so we can be subclassed
33   inline virtual ~Pass() {}
34
35   virtual bool run(Module *M) = 0;
36 };
37
38
39 //===----------------------------------------------------------------------===//
40 // MethodPass class - This class is used to implement most global optimizations.
41 // Optimizations should subclass this class if they meet the following
42 // constraints:
43 //  1. Optimizations are organized globally, ie a method at a time
44 //  2. Optimizing a method does not cause the addition or removal of any methods
45 //     in the module
46 //
47 struct MethodPass : public Pass {
48   // doInitialization - Virtual method overridden by subclasses to do
49   // any neccesary per-module initialization.
50   //
51   virtual bool doInitialization(Module *M) { return false; }
52
53   // runOnMethod - Virtual method overriden by subclasses to do the per-method
54   // processing of the pass.
55   //
56   virtual bool runOnMethod(Method *M) = 0;
57
58   // doFinalization - Virtual method overriden by subclasses to do any post
59   // processing needed after all passes have run.
60   //
61   virtual bool doFinalization(Module *M) { return false; }
62
63
64   virtual bool run(Module *M) {
65     bool Changed = doInitialization(M);
66
67     for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
68       Changed |= runOnMethod(*I);
69
70     return Changed | doFinalization(M);
71   }
72
73  bool run(Method *M) {
74    return doInitialization(M->getParent()) | runOnMethod(M)
75         | doFinalization(M->getParent());
76   }
77 };
78
79
80
81 //===----------------------------------------------------------------------===//
82 // CFGSafeMethodPass class - This class is used to implement global
83 // optimizations that do not modify the CFG of a method.  Optimizations should
84 // subclass this class if they meet the following constraints:
85 //   1. Optimizations are global, operating on a method at a time.
86 //   2. Optimizations do not modify the CFG of the contained method, by adding,
87 //      removing, or changing the order of basic blocks in a method.
88 //   3. Optimizations conform to all of the contstraints of MethodPass's.
89 //
90 struct CFGSafeMethodPass : public MethodPass {
91
92   // TODO: Differentiation from MethodPass will come later
93
94 };
95
96
97 //===----------------------------------------------------------------------===//
98 // BasicBlockPass class - This class is used to implement most local
99 // optimizations.  Optimizations should subclass this class if they
100 // meet the following constraints:
101 //   1. Optimizations are local, operating on either a basic block or
102 //      instruction at a time.
103 //   2. Optimizations do not modify the CFG of the contained method, or any
104 //      other basic block in the method.
105 //   3. Optimizations conform to all of the contstraints of CFGSafeMethodPass's.
106 //
107 struct BasicBlockPass : public CFGSafeMethodPass {
108   // runOnBasicBlock - Virtual method overriden by subclasses to do the
109   // per-basicblock processing of the pass.
110   //
111   virtual bool runOnBasicBlock(BasicBlock *M) = 0;
112
113   virtual bool runOnMethod(Method *M) {
114     bool Changed = false;
115     for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
116       Changed |= runOnBasicBlock(*I);
117     return Changed;
118   }
119
120   bool run(BasicBlock *BB) {
121     Module *M = BB->getParent()->getParent();
122     return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
123   }
124 };
125
126
127 //===----------------------------------------------------------------------===//
128 // PassManager - Container object for passes.  The PassManager destructor
129 // deletes all passes contained inside of the PassManager, so you shouldn't 
130 // delete passes manually, and all passes should be dynamically allocated.
131 //
132 class PassManager {
133   std::vector<Pass*> Passes;
134   MethodPassBatcher *Batcher;
135 public:
136   PassManager() : Batcher(0) {}
137   ~PassManager();
138
139   // run - Run all of the queued passes on the specified module in an optimal
140   // way.
141   bool run(Module *M);
142
143   // add - Add a pass to the queue of passes to run.  This passes ownership of
144   // the Pass to the PassManager.  When the PassManager is destroyed, the pass
145   // will be destroyed as well, so there is no need to delete the pass.  Also,
146   // all passes MUST be new'd.
147   //
148   void add(Pass *P);
149   void add(MethodPass *P);
150   void add(BasicBlockPass *P);
151 };
152
153 #endif