mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-12 02:33:33 +00:00
Add new Pass infrastructure and some examples
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@836 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
3524fc2197
commit
1bffea0341
39
include/llvm/Transforms/ChangeAllocations.h
Normal file
39
include/llvm/Transforms/ChangeAllocations.h
Normal file
@ -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
|
20
include/llvm/Transforms/HoistPHIConstants.h
Normal file
20
include/llvm/Transforms/HoistPHIConstants.h
Normal file
@ -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
|
200
include/llvm/Transforms/Pass.h
Normal file
200
include/llvm/Transforms/Pass.h
Normal file
@ -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
|
||||
|
35
include/llvm/Transforms/PrintModulePass.h
Normal file
35
include/llvm/Transforms/PrintModulePass.h
Normal file
@ -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
|
61
lib/Transforms/HoistPHIConstants.cpp
Normal file
61
lib/Transforms/HoistPHIConstants.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
//===- llvm/Transforms/HoistPHIConstants.h - Normalize PHI nodes ------------=//
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Transforms/HoistPHIConstants.h"
|
||||
#include "llvm/iOther.h"
|
||||
#include "llvm/BasicBlock.h"
|
||||
#include "llvm/Method.h"
|
||||
#include <map>
|
||||
|
||||
typedef pair<BasicBlock *, Value*> BBConstTy;
|
||||
typedef map<BBConstTy, CastInst *> CachedCopyMap;
|
||||
|
||||
static Value *NormalizePhiOperand(PHINode *PN, Value *CPV,
|
||||
BasicBlock *Pred, CachedCopyMap &CopyCache) {
|
||||
|
||||
// Check if we've already inserted a copy for this constant in Pred
|
||||
// Note that `copyCache[Pred]' will create an empty vector the first time
|
||||
//
|
||||
CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV));
|
||||
if (CCI != CopyCache.end()) return CCI->second;
|
||||
|
||||
// Create a copy instruction and add it to the cache...
|
||||
CastInst *Inst = new CastInst(CPV, CPV->getType());
|
||||
CopyCache.insert(make_pair(BBConstTy(Pred, CPV), Inst));
|
||||
|
||||
// Insert the copy just before the terminator inst of the predecessor BB
|
||||
assert(Pred->getTerminator() && "Degenerate BB encountered!");
|
||||
Pred->getInstList().insert(Pred->getInstList().end()-1, Inst);
|
||||
|
||||
return Inst;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Entry point for normalizing constant args in PHIs
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool HoistPHIConstants::doPerMethodWork(Method *M) {
|
||||
CachedCopyMap Cache;
|
||||
|
||||
for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI)
|
||||
for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) {
|
||||
Instruction *Inst = *II;
|
||||
if (!isa<PHINode>(Inst)) break; // All PHIs occur at top of BB!
|
||||
|
||||
PHINode *PN = cast<PHINode>(Inst);
|
||||
for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
|
||||
Value *Op = PN->getIncomingValue(i);
|
||||
if (isa<ConstPoolVal>(Op))
|
||||
PN->setIncomingValue(i,
|
||||
NormalizePhiOperand(PN, Op, PN->getIncomingBlock(i), Cache));
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
121
lib/Transforms/Utils/LowerAllocations.cpp
Normal file
121
lib/Transforms/Utils/LowerAllocations.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
//===- llvm/Transforms/LowerAllocations.h - Remove Malloc & Free Insts ------=//
|
||||
//
|
||||
// This file implements 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.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Transforms/LowerAllocations.h"
|
||||
#include "llvm/Target/TargetData.h"
|
||||
#include "llvm/DerivedTypes.h"
|
||||
#include "llvm/iMemory.h"
|
||||
#include "llvm/iOther.h"
|
||||
#include "llvm/SymbolTable.h"
|
||||
|
||||
// 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 LowerAllocations::doPassInitializationVirt(Module *M) {
|
||||
const MethodType *MallocType =
|
||||
MethodType::get(PointerType::get(Type::UByteTy),
|
||||
vector<const Type*>(1, Type::UIntTy), false);
|
||||
|
||||
SymbolTable *SymTab = M->getSymbolTableSure();
|
||||
|
||||
// Check for a definition of malloc
|
||||
if (Value *V = SymTab->lookup(PointerType::get(MallocType), "malloc")) {
|
||||
MallocMeth = cast<Method>(V); // Yup, got it
|
||||
} else { // Nope, add one
|
||||
M->getMethodList().push_back(MallocMeth = new Method(MallocType, "malloc"));
|
||||
}
|
||||
|
||||
const MethodType *FreeType =
|
||||
MethodType::get(Type::VoidTy,
|
||||
vector<const Type*>(1, PointerType::get(Type::UByteTy)),
|
||||
false);
|
||||
|
||||
// Check for a definition of free
|
||||
if (Value *V = SymTab->lookup(PointerType::get(FreeType), "free")) {
|
||||
FreeMeth = cast<Method>(V); // Yup, got it
|
||||
} else { // Nope, add one
|
||||
M->getMethodList().push_back(FreeMeth = new Method(FreeType, "free"));
|
||||
}
|
||||
|
||||
return false; // Always successful
|
||||
}
|
||||
|
||||
// doPerMethodWork - This method does the actual work of converting
|
||||
// instructions over, assuming that the pass has already been initialized.
|
||||
//
|
||||
bool LowerAllocations::doPerMethodWorkVirt(Method *M) {
|
||||
assert(MallocMeth && FreeMeth && M && "Pass not initialized!");
|
||||
|
||||
// Loop over all of the instructions, looking for malloc or free instructions
|
||||
for (Method::iterator BBI = M->begin(), BBE = M->end(); BBI != BBE; ++BBI) {
|
||||
BasicBlock *BB = *BBI;
|
||||
for (unsigned i = 0; i < BB->size(); ++i) {
|
||||
BasicBlock::InstListType &BBIL = BB->getInstList();
|
||||
if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) {
|
||||
BBIL.remove(BBIL.begin()+i); // remove the malloc instr...
|
||||
|
||||
const Type *AllocTy = cast<PointerType>(MI->getType())->getValueType();
|
||||
|
||||
// If the user is allocating an unsized array with a dynamic size arg,
|
||||
// start by getting the size of one element.
|
||||
//
|
||||
if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy))
|
||||
if (ATy->isUnsized()) AllocTy = ATy->getElementType();
|
||||
|
||||
// Get the number of bytes to be allocated for one element of the
|
||||
// requested type...
|
||||
unsigned Size = DataLayout.getTypeSize(AllocTy);
|
||||
|
||||
// malloc(type) becomes sbyte *malloc(constint)
|
||||
Value *MallocArg = ConstPoolUInt::get(Type::UIntTy, Size);
|
||||
if (MI->getNumOperands() && Size == 1) {
|
||||
MallocArg = MI->getOperand(0); // Operand * 1 = Operand
|
||||
} else if (MI->getNumOperands()) {
|
||||
// Multiply it by the array size if neccesary...
|
||||
MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0),
|
||||
MallocArg);
|
||||
BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg));
|
||||
}
|
||||
|
||||
// Create the call to Malloc...
|
||||
CallInst *MCall = new CallInst(MallocMeth,
|
||||
vector<Value*>(1, MallocArg));
|
||||
BBIL.insert(BBIL.begin()+i, MCall);
|
||||
|
||||
// Create a cast instruction to convert to the right type...
|
||||
CastInst *MCast = new CastInst(MCall, MI->getType());
|
||||
BBIL.insert(BBIL.begin()+i+1, MCast);
|
||||
|
||||
// Replace all uses of the old malloc inst with the cast inst
|
||||
MI->replaceAllUsesWith(MCast);
|
||||
delete MI; // Delete the malloc inst
|
||||
} else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) {
|
||||
BBIL.remove(BB->getInstList().begin()+i);
|
||||
|
||||
// Cast the argument to free into a ubyte*...
|
||||
CastInst *MCast = new CastInst(FI->getOperand(0),
|
||||
PointerType::get(Type::UByteTy));
|
||||
BBIL.insert(BBIL.begin()+i, MCast);
|
||||
|
||||
// Insert a call to the free function...
|
||||
CallInst *FCall = new CallInst(FreeMeth,
|
||||
vector<Value*>(1, MCast));
|
||||
BBIL.insert(BBIL.begin()+i+1, FCall);
|
||||
|
||||
// Delete the old free instruction
|
||||
delete FI;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false; // Always successful
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user