1 //===- llvm/Transforms/Pass.h - Base class for XForm Passes ------*- C++ -*--=//
3 // This file defines a marker 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 apply N passes to a
7 // module, by first doing N Pass specific initializations for the module, then
8 // looping over all of the methods in the module, doing method specific work
9 // N times for each method. Like this:
11 // for_each(Passes.begin(), Passes.end(), doPassInitialization(Module));
12 // for_each(Method *M <- Module->begin(), Module->end())
13 // for_each(Passes.begin(), Passes.end(), doPerMethodWork(M));
15 // The other way to do things is like this:
16 // for_each(Pass *P <- Passes.begin(), Passes.end()) {
17 // Passes->doPassInitialization(Module)
18 // for_each(Module->begin(), Module->end(), P->doPerMethodWork);
21 // But this can cause thrashing and poor cache performance, so we don't do it
24 // Because a transformation does not see all methods consecutively, it should
25 // be careful about the state that it maintains... another pass may modify a
26 // method between two invokacations of doPerMethodWork.
28 // Also, implementations of doMethodWork should not remove any methods from the
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_TRANSFORMS_PASS_H
34 #define LLVM_TRANSFORMS_PASS_H
36 #include "llvm/Module.h"
37 #include "llvm/Method.h"
39 //===----------------------------------------------------------------------===//
40 // Pass interface - Implemented by all 'passes'.
43 //===--------------------------------------------------------------------===//
44 // The externally useful entry points
47 // runAllPasses - Run a bunch of passes on the specified module, efficiently.
48 static bool runAllPasses(Module *M, vector<Pass*> &Passes) {
49 for (unsigned i = 0; i < Passes.size(); ++i)
50 if (Passes[i]->doPassInitializationVirt(M)) return true;
52 // Loop over all of the methods, applying all of the passes to them
53 for (Module::iterator I = M->begin(); I != M->end(); ++I)
54 for (unsigned i = 0; i < Passes.size(); ++i)
55 if (Passes[i]->doPerMethodWorkVirt(*I)) return true;
59 // runAllPassesAndFree - Run a bunch of passes on the specified module,
60 // efficiently. When done, delete all of the passes.
62 static bool runAllPassesAndFree(Module *M, vector<Pass*> &Passes) {
63 // First run all of the passes
64 bool Result = runAllPasses(M, Passes);
66 // Free all of the passes.
67 for (unsigned i = 0; i < Passes.size(); ++i)
73 // run(Module*) - Run this pass on a module and all of the methods contained
74 // within it. Returns false on success.
77 if (doPassInitializationVirt(M)) return true;
79 // Loop over methods in the module. doPerMethodWork could add a method to
80 // the Module, so we have to keep checking for end of method list condition.
82 for (Module::iterator I = M->begin(); I != M->end(); ++I)
83 if (doPerMethodWorkVirt(*I)) return true;
87 // run(Method*) - Run this pass on a module and one specific method. Returns
91 if (doPassInitializationVirt(M->getParent())) return true;
92 return doPerMethodWorkVirt(M);
96 //===--------------------------------------------------------------------===//
97 // Functions to be implemented by subclasses
100 // Destructor - Virtual so we can be subclassed
101 inline virtual ~Pass() {}
103 // doPassInitializationVirt - Virtual method overridden by subclasses to do
104 // any neccesary per-module initialization. Returns false on success.
106 virtual bool doPassInitializationVirt(Module *M) = 0;
108 // doPerMethodWorkVirt - Virtual method overriden by subclasses to do the
109 // per-method processing of the pass. Returns false on success.
111 virtual bool doPerMethodWorkVirt(Method *M) = 0;
115 //===----------------------------------------------------------------------===//
116 // ConcretePass class - This is used by implementations of passes to fill in
117 // boiler plate code.
119 // Deriving from this class is good because if new methods are added in the
120 // future, code for your pass won't have to change to stub out the unused
123 struct ConcretePass : public Pass {
125 // doPassInitializationVirt - Default to success.
126 virtual bool doPassInitializationVirt(Module *M) { return false; }
128 // doPerMethodWorkVirt - Default to success.
129 virtual bool doPerMethodWorkVirt(Method *M) { return false; }
134 //===----------------------------------------------------------------------===//
135 // StatelessPass<t> class - This is used by implementations of passes to fill in
136 // boiler plate code. Subclassing this class indicates that a class has no
137 // state to keep around, so it's safe to invoke static versions of functions.
138 // This can be more efficient that using virtual function dispatch all of the
141 // SubClass should be a concrete class that is derived from StatelessPass.
143 template<class SubClass>
144 struct StatelessPass : public ConcretePass {
146 //===--------------------------------------------------------------------===//
147 // The externally useful entry points - These are specialized to avoid the
148 // overhead of virtual method invokations if
150 // run(Module*) - Run this pass on a module and all of the methods contained
151 // within it. Returns false on success.
153 static bool run(Module *M) {
154 if (doPassInitialization(M->getParent())) return true;
156 // Loop over methods in the module. doPerMethodWork could add a method to
157 // the Module, so we have to keep checking for end of method list condition.
159 for (Module::iterator I = M->begin(); I != M->end(); ++I)
160 if (doPerMethodWork(*I)) return true;
164 // run(Method*) - Run this pass on a module and one specific method. Returns
167 static bool run(Method *M) {
168 if (doPassInitialization(M->getParent())) return true;
169 return doPerMethodWork(M);
172 //===--------------------------------------------------------------------===//
173 // Default static method implementations, these should be defined in SubClass
175 static bool doPassInitialization(Module *M) { return false; }
176 static bool doPerMethodWork(Method *M) { return false; }
179 //===--------------------------------------------------------------------===//
180 // Virtual method forwarders...
182 // doPassInitializationVirt - For a StatelessPass, default to implementing in
183 // terms of the static method.
185 virtual bool doPassInitializationVirt(Module *M) {
186 return SubClass::doPassInitialization(M);
189 // doPerMethodWorkVirt - For a StatelessPass, default to implementing in
190 // terms of the static method.
192 virtual bool doPerMethodWorkVirt(Method *M) {
193 return SubClass::doPerMethodWork(M);