2001-10-15 17:31:51 +00:00
|
|
|
//===- llvm/Transforms/Pass.h - Base class for XForm Passes ------*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This file defines a marker class that indicates that a specified class is a
|
|
|
|
// transformation pass implementation.
|
|
|
|
//
|
|
|
|
// Pass's are designed this way so that it is possible to apply N passes to a
|
|
|
|
// module, by first doing N Pass specific initializations for the module, then
|
|
|
|
// looping over all of the methods in the module, doing method specific work
|
|
|
|
// N times for each method. Like this:
|
|
|
|
//
|
|
|
|
// for_each(Passes.begin(), Passes.end(), doPassInitialization(Module));
|
|
|
|
// for_each(Method *M <- Module->begin(), Module->end())
|
|
|
|
// for_each(Passes.begin(), Passes.end(), doPerMethodWork(M));
|
|
|
|
//
|
|
|
|
// The other way to do things is like this:
|
|
|
|
// for_each(Pass *P <- Passes.begin(), Passes.end()) {
|
|
|
|
// Passes->doPassInitialization(Module)
|
|
|
|
// for_each(Module->begin(), Module->end(), P->doPerMethodWork);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// But this can cause thrashing and poor cache performance, so we don't do it
|
|
|
|
// that way.
|
|
|
|
//
|
|
|
|
// Because a transformation does not see all methods consecutively, it should
|
|
|
|
// be careful about the state that it maintains... another pass may modify a
|
2001-10-18 05:20:53 +00:00
|
|
|
// method between two invocatations of doPerMethodWork.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
|
|
|
// Also, implementations of doMethodWork should not remove any methods from the
|
|
|
|
// module.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TRANSFORMS_PASS_H
|
|
|
|
#define LLVM_TRANSFORMS_PASS_H
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pass interface - Implemented by all 'passes'.
|
|
|
|
//
|
|
|
|
struct Pass {
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// The externally useful entry points
|
|
|
|
//
|
|
|
|
|
|
|
|
// runAllPasses - Run a bunch of passes on the specified module, efficiently.
|
|
|
|
static bool runAllPasses(Module *M, vector<Pass*> &Passes) {
|
2001-10-18 01:32:17 +00:00
|
|
|
bool MadeChanges = false;
|
2001-10-18 05:20:53 +00:00
|
|
|
// Run all of the pass initializers
|
2001-10-15 17:31:51 +00:00
|
|
|
for (unsigned i = 0; i < Passes.size(); ++i)
|
2001-10-18 05:20:53 +00:00
|
|
|
MadeChanges |= Passes[i]->doPassInitialization(M);
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
// Loop over all of the methods, applying all of the passes to them
|
2001-10-18 05:20:53 +00:00
|
|
|
for (unsigned m = 0; m < M->size(); ++m)
|
2001-10-15 17:31:51 +00:00
|
|
|
for (unsigned i = 0; i < Passes.size(); ++i)
|
2001-10-18 05:20:53 +00:00
|
|
|
MadeChanges |= Passes[i]->doPerMethodWork(*(M->begin()+m));
|
|
|
|
|
|
|
|
// Run all of the pass finalizers...
|
|
|
|
for (unsigned i = 0; i < Passes.size(); ++i)
|
|
|
|
MadeChanges |= Passes[i]->doPassFinalization(M);
|
2001-10-18 01:32:17 +00:00
|
|
|
return MadeChanges;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runAllPassesAndFree - Run a bunch of passes on the specified module,
|
|
|
|
// efficiently. When done, delete all of the passes.
|
|
|
|
//
|
|
|
|
static bool runAllPassesAndFree(Module *M, vector<Pass*> &Passes) {
|
|
|
|
// First run all of the passes
|
2001-10-18 01:32:17 +00:00
|
|
|
bool MadeChanges = runAllPasses(M, Passes);
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
// Free all of the passes.
|
|
|
|
for (unsigned i = 0; i < Passes.size(); ++i)
|
|
|
|
delete Passes[i];
|
2001-10-18 01:32:17 +00:00
|
|
|
return MadeChanges;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// run(Module*) - Run this pass on a module and all of the methods contained
|
2001-10-18 01:49:48 +00:00
|
|
|
// within it. Returns true if any of the contained passes returned true.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
|
|
|
bool run(Module *M) {
|
2001-10-18 05:20:53 +00:00
|
|
|
bool MadeChanges = doPassInitialization(M);
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
// Loop over methods in the module. doPerMethodWork could add a method to
|
|
|
|
// the Module, so we have to keep checking for end of method list condition.
|
|
|
|
//
|
2001-10-18 05:20:53 +00:00
|
|
|
for (unsigned m = 0; m < M->size(); ++m)
|
|
|
|
MadeChanges |= doPerMethodWork(*(M->begin()+m));
|
|
|
|
return MadeChanges | doPassFinalization(M);
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run(Method*) - Run this pass on a module and one specific method. Returns
|
|
|
|
// false on success.
|
|
|
|
//
|
|
|
|
bool run(Method *M) {
|
2001-10-18 05:20:53 +00:00
|
|
|
return doPassInitialization(M->getParent()) | doPerMethodWork(M) |
|
|
|
|
doPassFinalization(M->getParent());
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Functions to be implemented by subclasses
|
|
|
|
//
|
|
|
|
|
|
|
|
// Destructor - Virtual so we can be subclassed
|
|
|
|
inline virtual ~Pass() {}
|
|
|
|
|
2001-10-18 05:20:53 +00:00
|
|
|
// doPassInitialization - Virtual method overridden by subclasses to do
|
2001-10-18 01:49:48 +00:00
|
|
|
// any neccesary per-module initialization.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2001-10-18 05:20:53 +00:00
|
|
|
virtual bool doPassInitialization(Module *M) { return false; }
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2001-10-18 05:20:53 +00:00
|
|
|
// doPerMethodWork - Virtual method overriden by subclasses to do the
|
2001-10-18 01:49:48 +00:00
|
|
|
// per-method processing of the pass.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2001-10-18 05:20:53 +00:00
|
|
|
virtual bool doPerMethodWork(Method *M) { return false; }
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2001-10-18 05:20:53 +00:00
|
|
|
// doPassFinalization - Virtual method overriden by subclasses to do any post
|
|
|
|
// processing needed after all passes have run.
|
2001-10-15 17:31:51 +00:00
|
|
|
//
|
2001-10-18 05:20:53 +00:00
|
|
|
virtual bool doPassFinalization(Module *M) { return false; }
|
2001-10-15 17:31:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|