diff options
author | Chris Lattner <sabre@nondot.org> | 2001-10-15 17:31:51 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2001-10-15 17:31:51 +0000 |
commit | 1bffea0341968b3b2b0106c745d4602b6804e62f (patch) | |
tree | c76072d785eecda413d6e12bcf7ce41f965f5e9d /include | |
parent | 3524fc2197c17edcea786a9bb0e00246438dba90 (diff) | |
download | external_llvm-1bffea0341968b3b2b0106c745d4602b6804e62f.zip external_llvm-1bffea0341968b3b2b0106c745d4602b6804e62f.tar.gz external_llvm-1bffea0341968b3b2b0106c745d4602b6804e62f.tar.bz2 |
Add new Pass infrastructure and some examples
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@836 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/Transforms/ChangeAllocations.h | 39 | ||||
-rw-r--r-- | include/llvm/Transforms/HoistPHIConstants.h | 20 | ||||
-rw-r--r-- | include/llvm/Transforms/Pass.h | 200 | ||||
-rw-r--r-- | include/llvm/Transforms/PrintModulePass.h | 35 |
4 files changed, 294 insertions, 0 deletions
diff --git a/include/llvm/Transforms/ChangeAllocations.h b/include/llvm/Transforms/ChangeAllocations.h new file mode 100644 index 0000000..08339cd --- /dev/null +++ b/include/llvm/Transforms/ChangeAllocations.h @@ -0,0 +1,39 @@ +//===- llvm/Transforms/LowerAllocations.h - Remove Malloc & Free -*- C++ -*--=// +// +// This file defines the interface to a pass that lowers malloc and free +// instructions to calls to %malloc & %free functions. This transformation is +// a target dependant tranformation because we depend on the size of data types +// and alignment constraints. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_LOWERALLOCATIONS_H +#define LLVM_TRANSFORMS_LOWERALLOCATIONS_H + +#include "llvm/Transforms/Pass.h" +class TargetData; + +class LowerAllocations : public ConcretePass<LowerAllocations> { + Method *MallocMeth; // Methods in the module we are processing + Method *FreeMeth; // Initialized by doPassInitializationVirt + + const TargetData &DataLayout; +public: + inline LowerAllocations(const TargetData &TD) : DataLayout(TD) { + MallocMeth = FreeMeth = 0; + } + + // doPassInitialization - For the lower allocations pass, this ensures that a + // module contains a declaration for a malloc and a free function. + // + // This function is always successful. + // + bool doPassInitializationVirt(Module *M); + + // doPerMethodWork - This method does the actual work of converting + // instructions over, assuming that the pass has already been initialized. + // + bool doPerMethodWorkVirt(Method *M); +}; + +#endif diff --git a/include/llvm/Transforms/HoistPHIConstants.h b/include/llvm/Transforms/HoistPHIConstants.h new file mode 100644 index 0000000..d15ead9 --- /dev/null +++ b/include/llvm/Transforms/HoistPHIConstants.h @@ -0,0 +1,20 @@ +//===- llvm/Transforms/HoistPHIConstants.h - Normalize PHI nodes -*- C++ -*--=// +// +// HoistPHIConstants - Remove literal constants that are arguments of PHI nodes +// by inserting cast instructions in the preceeding basic blocks, and changing +// constant references into references of the casted value. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_HOISTPHICONSTANTS_H +#define LLVM_TRANSFORMS_HOISTPHICONSTANTS_H + +#include "llvm/Transforms/Pass.h" + +struct HoistPHIConstants : public StatelessPass<HoistPHIConstants> { + // doPerMethodWork - This method does the work. Always successful. + // + static bool doPerMethodWork(Method *M); +}; + +#endif diff --git a/include/llvm/Transforms/Pass.h b/include/llvm/Transforms/Pass.h new file mode 100644 index 0000000..c6ba84e --- /dev/null +++ b/include/llvm/Transforms/Pass.h @@ -0,0 +1,200 @@ +//===- 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 +// method between two invokacations of doPerMethodWork. +// +// 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) { + for (unsigned i = 0; i < Passes.size(); ++i) + if (Passes[i]->doPassInitializationVirt(M)) return true; + + // Loop over all of the methods, applying all of the passes to them + for (Module::iterator I = M->begin(); I != M->end(); ++I) + for (unsigned i = 0; i < Passes.size(); ++i) + if (Passes[i]->doPerMethodWorkVirt(*I)) return true; + return false; + } + + // 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 + bool Result = runAllPasses(M, Passes); + + // Free all of the passes. + for (unsigned i = 0; i < Passes.size(); ++i) + delete Passes[i]; + return Result; + } + + + // run(Module*) - Run this pass on a module and all of the methods contained + // within it. Returns false on success. + // + bool run(Module *M) { + if (doPassInitializationVirt(M)) return true; + + // 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. + // + for (Module::iterator I = M->begin(); I != M->end(); ++I) + if (doPerMethodWorkVirt(*I)) return true; + return false; + } + + // run(Method*) - Run this pass on a module and one specific method. Returns + // false on success. + // + bool run(Method *M) { + if (doPassInitializationVirt(M->getParent())) return true; + return doPerMethodWorkVirt(M); + } + + + //===--------------------------------------------------------------------===// + // Functions to be implemented by subclasses + // + + // Destructor - Virtual so we can be subclassed + inline virtual ~Pass() {} + + // doPassInitializationVirt - Virtual method overridden by subclasses to do + // any neccesary per-module initialization. Returns false on success. + // + virtual bool doPassInitializationVirt(Module *M) = 0; + + // doPerMethodWorkVirt - Virtual method overriden by subclasses to do the + // per-method processing of the pass. Returns false on success. + // + virtual bool doPerMethodWorkVirt(Method *M) = 0; +}; + + +//===----------------------------------------------------------------------===// +// ConcretePass<t> class - This is used by implementations of passes to fill in +// boiler plate code. SubClass should be a concrete class that is derived from +// ConcretePass. +// +// Deriving from this class is good because if new methods are added in the +// future, code for your pass won't have to change to stub out the unused +// functionality. +// +template<class SubClass> +struct ConcretePass : public Pass { + + // doPassInitializationVirt - Default to success. + virtual bool doPassInitializationVirt(Module *M) { return false; } + + // doPerMethodWorkVirt - Default to success. + virtual bool doPerMethodWorkVirt(Method *M) { return false; } +}; + + + +//===----------------------------------------------------------------------===// +// StatelessPass<t> class - This is used by implementations of passes to fill in +// boiler plate code. Subclassing this class indicates that a class has no +// state to keep around, so it's safe to invoke static versions of functions. +// This can be more efficient that using virtual function dispatch all of the +// time. +// +// SubClass should be a concrete class that is derived from StatelessPass. +// +template<class SubClass> +struct StatelessPass : public ConcretePass<SubClass> { + + //===--------------------------------------------------------------------===// + // The externally useful entry points - These are specialized to avoid the + // overhead of virtual method invokations if + // + // run(Module*) - Run this pass on a module and all of the methods contained + // within it. Returns false on success. + // + static bool run(Module *M) { + if (doPassInitialization(M->getParent())) return true; + + // 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. + // + for (Module::iterator I = M->begin(); I != M->end(); ++I) + if (doPerMethodWork(*I)) return true; + return false; + } + + // run(Method*) - Run this pass on a module and one specific method. Returns + // false on success. + // + static bool run(Method *M) { + if (doPassInitialization(M->getParent())) return true; + return doPerMethodWork(M); + } + + //===--------------------------------------------------------------------===// + // Default static method implementations, these should be defined in SubClass + + static bool doPassInitialization(Module *M) { return false; } + static bool doPerMethodWork(Method *M) { return false; } + + + //===--------------------------------------------------------------------===// + // Virtual method forwarders... + + // doPassInitializationVirt - For a StatelessPass, default to implementing in + // terms of the static method. + // + virtual bool doPassInitializationVirt(Module *M) { + return SubClass::doPassInitialization(M); + } + + // doPerMethodWorkVirt - For a StatelessPass, default to implementing in + // terms of the static method. + // + virtual bool doPerMethodWorkVirt(Method *M) { + return SubClass::doPerMethodWork(M); + } +}; + +#endif + diff --git a/include/llvm/Transforms/PrintModulePass.h b/include/llvm/Transforms/PrintModulePass.h new file mode 100644 index 0000000..90cd369 --- /dev/null +++ b/include/llvm/Transforms/PrintModulePass.h @@ -0,0 +1,35 @@ +//===- llvm/Transforms/PrintModulePass.h - Printing Pass ---------*- C++ -*--=// +// +// This file defines a simple pass to print out methods of a module as they are +// processed. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_PRINTMODULE_H +#define LLVM_TRANSFORMS_PRINTMODULE_H + +#include "llvm/Transforms/Pass.h" +#include "llvm/Assembly/Writer.h" + +class PrintModulePass : public ConcretePass<PrintModulePass> { + string Banner; // String to print before each method + ostream *Out; // ostream to print on + bool DeleteStream; // Delete the ostream in our dtor? +public: + inline PrintModulePass(const string &B, ostream *o = &cout, bool DS = false) + : Banner(B), Out(o), DeleteStream(DS) {} + + ~PrintModulePass() { + if (DeleteStream) delete Out; + } + + // doPerMethodWork - This pass just prints a banner followed by the method as + // it's processed. + // + bool doPerMethodWorkVirt(Method *M) { + (*Out) << Banner << M; + return false; + } +}; + +#endif |