1 //===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=//
3 // This file defines a base class that indicates that a specified class is a
4 // transformation pass implementation.
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.
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.
16 //===----------------------------------------------------------------------===//
21 #include "llvm/Module.h"
22 #include "llvm/Method.h"
24 class MethodPassBatcher;
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.
32 // Destructor - Virtual so we can be subclassed
33 inline virtual ~Pass() {}
35 virtual bool run(Module *M) = 0;
39 //===----------------------------------------------------------------------===//
40 // MethodPass class - This class is used to implement most global optimizations.
41 // Optimizations should subclass this class if they meet the following
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
47 struct MethodPass : public Pass {
48 // doInitialization - Virtual method overridden by subclasses to do
49 // any neccesary per-module initialization.
51 virtual bool doInitialization(Module *M) { return false; }
53 // runOnMethod - Virtual method overriden by subclasses to do the per-method
54 // processing of the pass.
56 virtual bool runOnMethod(Method *M) = 0;
58 // doFinalization - Virtual method overriden by subclasses to do any post
59 // processing needed after all passes have run.
61 virtual bool doFinalization(Module *M) { return false; }
64 virtual bool run(Module *M) {
65 bool Changed = doInitialization(M);
67 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
68 Changed |= runOnMethod(*I);
70 return Changed | doFinalization(M);
74 return doInitialization(M->getParent()) | runOnMethod(M)
75 | doFinalization(M->getParent());
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.
90 struct CFGSafeMethodPass : public MethodPass {
92 // TODO: Differentiation from MethodPass will come later
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.
107 struct BasicBlockPass : public CFGSafeMethodPass {
108 // runOnBasicBlock - Virtual method overriden by subclasses to do the
109 // per-basicblock processing of the pass.
111 virtual bool runOnBasicBlock(BasicBlock *M) = 0;
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);
120 bool run(BasicBlock *BB) {
121 Module *M = BB->getParent()->getParent();
122 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
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.
133 std::vector<Pass*> Passes;
134 MethodPassBatcher *Batcher;
136 PassManager() : Batcher(0) {}
139 // run - Run all of the queued passes on the specified module in an optimal
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.
149 void add(MethodPass *P);
150 void add(BasicBlockPass *P);