2001-10-18 20:19:09 +00:00
|
|
|
//===- llvm/Pass.h - Base class for XForm Passes -----------------*- C++ -*--=//
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2001-10-18 20:19:09 +00:00
|
|
|
// This file defines a base class that indicates that a specified class is a
|
2001-10-15 17:31:51 +00:00
|
|
|
// transformation pass implementation.
|
|
|
|
//
|
2002-01-21 07:31:00 +00:00
|
|
|
// Pass's are designed this way so that it is possible to run passes in a cache
|
|
|
|
// and organizationally optimal order without having to specify it at the front
|
|
|
|
// end. This allows arbitrary passes to be strung together and have them
|
|
|
|
// executed as effeciently as possible.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2002-01-21 07:31:00 +00:00
|
|
|
// Passes should extend one of the classes below, depending on the guarantees
|
|
|
|
// that it can make about what will be modified as it is run. For example, most
|
|
|
|
// global optimizations should derive from MethodPass, because they do not add
|
|
|
|
// or delete methods, they operate on the internals of the method.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-10-18 20:19:09 +00:00
|
|
|
#ifndef LLVM_PASS_H
|
|
|
|
#define LLVM_PASS_H
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
class MethodPassBatcher;
|
|
|
|
|
2001-10-15 17:31:51 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-01-21 07:31:00 +00:00
|
|
|
// Pass interface - Implemented by all 'passes'. Subclass this if you are an
|
|
|
|
// interprocedural optimization or you do not fit into any of the more
|
|
|
|
// constrained passes described below.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
|
|
|
struct Pass {
|
2002-01-21 07:31:00 +00:00
|
|
|
// Destructor - Virtual so we can be subclassed
|
|
|
|
inline virtual ~Pass() {}
|
|
|
|
|
|
|
|
virtual bool run(Module *M) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MethodPass class - This class is used to implement most global optimizations.
|
|
|
|
// Optimizations should subclass this class if they meet the following
|
|
|
|
// constraints:
|
|
|
|
// 1. Optimizations are organized globally, ie a method at a time
|
|
|
|
// 2. Optimizing a method does not cause the addition or removal of any methods
|
|
|
|
// in the module
|
|
|
|
//
|
|
|
|
struct MethodPass : public Pass {
|
|
|
|
// doInitialization - Virtual method overridden by subclasses to do
|
|
|
|
// any neccesary per-module initialization.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2002-01-21 07:31:00 +00:00
|
|
|
virtual bool doInitialization(Module *M) { return false; }
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
// runOnMethod - Virtual method overriden by subclasses to do the per-method
|
|
|
|
// processing of the pass.
|
|
|
|
//
|
|
|
|
virtual bool runOnMethod(Method *M) = 0;
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
// doFinalization - Virtual method overriden by subclasses to do any post
|
|
|
|
// processing needed after all passes have run.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2002-01-21 07:31:00 +00:00
|
|
|
virtual bool doFinalization(Module *M) { return false; }
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
virtual bool run(Module *M) {
|
|
|
|
bool Changed = doInitialization(M);
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
Changed |= runOnMethod(*I);
|
|
|
|
|
|
|
|
return Changed | doFinalization(M);
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
bool run(Method *M) {
|
|
|
|
return doInitialization(M->getParent()) | runOnMethod(M)
|
|
|
|
| doFinalization(M->getParent());
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
2002-01-21 07:31:00 +00:00
|
|
|
};
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CFGSafeMethodPass class - This class is used to implement global
|
|
|
|
// optimizations that do not modify the CFG of a method. Optimizations should
|
|
|
|
// subclass this class if they meet the following constraints:
|
|
|
|
// 1. Optimizations are global, operating on a method at a time.
|
|
|
|
// 2. Optimizations do not modify the CFG of the contained method, by adding,
|
|
|
|
// removing, or changing the order of basic blocks in a method.
|
|
|
|
// 3. Optimizations conform to all of the contstraints of MethodPass's.
|
|
|
|
//
|
|
|
|
struct CFGSafeMethodPass : public MethodPass {
|
|
|
|
|
|
|
|
// TODO: Differentiation from MethodPass will come later
|
|
|
|
|
|
|
|
};
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BasicBlockPass class - This class is used to implement most local
|
|
|
|
// optimizations. Optimizations should subclass this class if they
|
|
|
|
// meet the following constraints:
|
|
|
|
// 1. Optimizations are local, operating on either a basic block or
|
|
|
|
// instruction at a time.
|
|
|
|
// 2. Optimizations do not modify the CFG of the contained method, or any
|
|
|
|
// other basic block in the method.
|
|
|
|
// 3. Optimizations conform to all of the contstraints of CFGSafeMethodPass's.
|
|
|
|
//
|
|
|
|
struct BasicBlockPass : public CFGSafeMethodPass {
|
|
|
|
// runOnBasicBlock - Virtual method overriden by subclasses to do the
|
|
|
|
// per-basicblock processing of the pass.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2002-01-21 07:31:00 +00:00
|
|
|
virtual bool runOnBasicBlock(BasicBlock *M) = 0;
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-01-21 07:31:00 +00:00
|
|
|
virtual bool runOnMethod(Method *M) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Method::iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
Changed |= runOnBasicBlock(*I);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool run(BasicBlock *BB) {
|
|
|
|
Module *M = BB->getParent()->getParent();
|
|
|
|
return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PassManager - Container object for passes. The PassManager destructor
|
|
|
|
// deletes all passes contained inside of the PassManager, so you shouldn't
|
|
|
|
// delete passes manually, and all passes should be dynamically allocated.
|
|
|
|
//
|
|
|
|
class PassManager {
|
|
|
|
std::vector<Pass*> Passes;
|
|
|
|
MethodPassBatcher *Batcher;
|
|
|
|
public:
|
|
|
|
PassManager() : Batcher(0) {}
|
|
|
|
~PassManager();
|
|
|
|
|
2002-01-23 05:46:22 +00:00
|
|
|
// run - Run all of the queued passes on the specified module in an optimal
|
|
|
|
// way.
|
|
|
|
bool run(Module *M);
|
2002-01-21 07:31:00 +00:00
|
|
|
|
|
|
|
// add - Add a pass to the queue of passes to run. This passes ownership of
|
|
|
|
// the Pass to the PassManager. When the PassManager is destroyed, the pass
|
|
|
|
// will be destroyed as well, so there is no need to delete the pass. Also,
|
|
|
|
// all passes MUST be new'd.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2002-01-21 07:31:00 +00:00
|
|
|
void add(Pass *P);
|
|
|
|
void add(MethodPass *P);
|
|
|
|
void add(BasicBlockPass *P);
|
2001-10-15 17:31:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|