mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-07-27 12:26:08 +00:00
Simplify code by eliminating need to hang onto constant pool references
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@440 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
#include "llvm/Analysis/Expressions.h"
|
#include "llvm/Analysis/Expressions.h"
|
||||||
#include "llvm/Optimizations/ConstantHandling.h"
|
#include "llvm/Optimizations/ConstantHandling.h"
|
||||||
#include "llvm/ConstantPool.h"
|
|
||||||
#include "llvm/Method.h"
|
#include "llvm/Method.h"
|
||||||
#include "llvm/BasicBlock.h"
|
#include "llvm/BasicBlock.h"
|
||||||
|
|
||||||
@@ -18,30 +17,23 @@ using namespace analysis;
|
|||||||
|
|
||||||
class DefVal {
|
class DefVal {
|
||||||
const ConstPoolInt * const Val;
|
const ConstPoolInt * const Val;
|
||||||
ConstantPool &CP;
|
|
||||||
const Type * const Ty;
|
const Type * const Ty;
|
||||||
protected:
|
protected:
|
||||||
inline DefVal(const ConstPoolInt *val, ConstantPool &cp, const Type *ty)
|
inline DefVal(const ConstPoolInt *val, const Type *ty) : Val(val), Ty(ty) {}
|
||||||
: Val(val), CP(cp), Ty(ty) {}
|
|
||||||
public:
|
public:
|
||||||
inline const Type *getType() const { return Ty; }
|
inline const Type *getType() const { return Ty; }
|
||||||
inline ConstantPool &getCP() const { return CP; }
|
|
||||||
inline const ConstPoolInt *getVal() const { return Val; }
|
inline const ConstPoolInt *getVal() const { return Val; }
|
||||||
inline operator const ConstPoolInt * () const { return Val; }
|
inline operator const ConstPoolInt * () const { return Val; }
|
||||||
inline const ConstPoolInt *operator->() const { return Val; }
|
inline const ConstPoolInt *operator->() const { return Val; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DefZero : public DefVal {
|
struct DefZero : public DefVal {
|
||||||
inline DefZero(const ConstPoolInt *val, ConstantPool &cp, const Type *ty)
|
inline DefZero(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
|
||||||
: DefVal(val, cp, ty) {}
|
inline DefZero(const ConstPoolInt *val) : DefVal(val, val->getType()) {}
|
||||||
inline DefZero(const ConstPoolInt *val)
|
|
||||||
: DefVal(val, (ConstantPool&)val->getParent()->getConstantPool(),
|
|
||||||
val->getType()) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DefOne : public DefVal {
|
struct DefOne : public DefVal {
|
||||||
inline DefOne(const ConstPoolInt *val, ConstantPool &cp, const Type *ty)
|
inline DefOne(const ConstPoolInt *val, const Type *ty) : DefVal(val, ty) {}
|
||||||
: DefVal(val, cp, ty) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -50,24 +42,14 @@ struct DefOne : public DefVal {
|
|||||||
// the constant pool. If it is, it is quickly recycled, otherwise a new one
|
// the constant pool. If it is, it is quickly recycled, otherwise a new one
|
||||||
// is allocated and added to the constant pool.
|
// is allocated and added to the constant pool.
|
||||||
//
|
//
|
||||||
static ConstPoolInt *getIntegralConstant(ConstantPool &CP, unsigned char V,
|
static ConstPoolInt *getIntegralConstant(unsigned char V, const Type *Ty) {
|
||||||
const Type *Ty) {
|
return ConstPoolInt::get(Ty, V);
|
||||||
// FIXME: Lookup prexisting constant in table!
|
|
||||||
|
|
||||||
ConstPoolInt *CPI = ConstPoolInt::get(Ty, V);
|
|
||||||
CP.insert(CPI);
|
|
||||||
return CPI;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static ConstPoolInt *getUnsignedConstant(ConstantPool &CP, uint64_t V,
|
static ConstPoolInt *getUnsignedConstant(uint64_t V, const Type *Ty) {
|
||||||
const Type *Ty) {
|
|
||||||
// FIXME: Lookup prexisting constant in table!
|
|
||||||
if (Ty->isPointerType()) Ty = Type::ULongTy;
|
if (Ty->isPointerType()) Ty = Type::ULongTy;
|
||||||
|
|
||||||
ConstPoolInt *CPI;
|
return Ty->isSigned() ? ConstPoolSInt::get(Ty, V) : ConstPoolUInt::get(Ty, V);
|
||||||
CPI = Ty->isSigned() ? new ConstPoolSInt(Ty, V) : new ConstPoolUInt(Ty, V);
|
|
||||||
CP.insert(CPI);
|
|
||||||
return CPI;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add - Helper function to make later code simpler. Basically it just adds
|
// Add - Helper function to make later code simpler. Basically it just adds
|
||||||
@@ -82,7 +64,7 @@ static ConstPoolInt *getUnsignedConstant(ConstantPool &CP, uint64_t V,
|
|||||||
// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
|
// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
|
||||||
// is false, a null return value indicates a value of 0.
|
// is false, a null return value indicates a value of 0.
|
||||||
//
|
//
|
||||||
static const ConstPoolInt *Add(ConstantPool &CP, const ConstPoolInt *Arg1,
|
static const ConstPoolInt *Add(const ConstPoolInt *Arg1,
|
||||||
const ConstPoolInt *Arg2, bool DefOne) {
|
const ConstPoolInt *Arg2, bool DefOne) {
|
||||||
assert(Arg1 && Arg2 && "No null arguments should exist now!");
|
assert(Arg1 && Arg2 && "No null arguments should exist now!");
|
||||||
assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
|
assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
|
||||||
@@ -101,28 +83,25 @@ static const ConstPoolInt *Add(ConstantPool &CP, const ConstPoolInt *Arg1,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CP.insert(ResultI);
|
|
||||||
return ResultI;
|
return ResultI;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const ConstPoolInt *operator+(const DefZero &L, const DefZero &R) {
|
inline const ConstPoolInt *operator+(const DefZero &L, const DefZero &R) {
|
||||||
if (L == 0) return R;
|
if (L == 0) return R;
|
||||||
if (R == 0) return L;
|
if (R == 0) return L;
|
||||||
return Add(L.getCP(), L, R, false);
|
return Add(L, R, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const ConstPoolInt *operator+(const DefOne &L, const DefOne &R) {
|
inline const ConstPoolInt *operator+(const DefOne &L, const DefOne &R) {
|
||||||
if (L == 0) {
|
if (L == 0) {
|
||||||
if (R == 0)
|
if (R == 0)
|
||||||
return getIntegralConstant(L.getCP(), 2, L.getType());
|
return getIntegralConstant(2, L.getType());
|
||||||
else
|
else
|
||||||
return Add(L.getCP(), getIntegralConstant(L.getCP(), 1, L.getType()),
|
return Add(getIntegralConstant(1, L.getType()), R, true);
|
||||||
R, true);
|
|
||||||
} else if (R == 0) {
|
} else if (R == 0) {
|
||||||
return Add(L.getCP(), L,
|
return Add(L, getIntegralConstant(1, L.getType()), true);
|
||||||
getIntegralConstant(L.getCP(), 1, L.getType()), true);
|
|
||||||
}
|
}
|
||||||
return Add(L.getCP(), L, R, true);
|
return Add(L, R, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -138,7 +117,7 @@ inline const ConstPoolInt *operator+(const DefOne &L, const DefOne &R) {
|
|||||||
// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
|
// 3. If DefOne is true, a null return value indicates a value of 1, if DefOne
|
||||||
// is false, a null return value indicates a value of 0.
|
// is false, a null return value indicates a value of 0.
|
||||||
//
|
//
|
||||||
inline const ConstPoolInt *Mul(ConstantPool &CP, const ConstPoolInt *Arg1,
|
inline const ConstPoolInt *Mul(const ConstPoolInt *Arg1,
|
||||||
const ConstPoolInt *Arg2, bool DefOne = false) {
|
const ConstPoolInt *Arg2, bool DefOne = false) {
|
||||||
assert(Arg1 && Arg2 && "No null arguments should exist now!");
|
assert(Arg1 && Arg2 && "No null arguments should exist now!");
|
||||||
assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
|
assert(Arg1->getType() == Arg2->getType() && "Types must be compatible!");
|
||||||
@@ -157,18 +136,17 @@ inline const ConstPoolInt *Mul(ConstantPool &CP, const ConstPoolInt *Arg1,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
CP.insert(ResultI);
|
|
||||||
return ResultI;
|
return ResultI;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const ConstPoolInt *operator*(const DefZero &L, const DefZero &R) {
|
inline const ConstPoolInt *operator*(const DefZero &L, const DefZero &R) {
|
||||||
if (L == 0 || R == 0) return 0;
|
if (L == 0 || R == 0) return 0;
|
||||||
return Mul(L.getCP(), L, R, false);
|
return Mul(L, R, false);
|
||||||
}
|
}
|
||||||
inline const ConstPoolInt *operator*(const DefOne &L, const DefZero &R) {
|
inline const ConstPoolInt *operator*(const DefOne &L, const DefZero &R) {
|
||||||
if (R == 0) return getIntegralConstant(L.getCP(), 0, L.getType());
|
if (R == 0) return getIntegralConstant(0, L.getType());
|
||||||
if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
|
if (L == 0) return R->equalsInt(1) ? 0 : R.getVal();
|
||||||
return Mul(L.getCP(), L, R, false);
|
return Mul(L, R, false);
|
||||||
}
|
}
|
||||||
inline const ConstPoolInt *operator*(const DefZero &L, const DefOne &R) {
|
inline const ConstPoolInt *operator*(const DefZero &L, const DefOne &R) {
|
||||||
return R*L;
|
return R*L;
|
||||||
@@ -201,7 +179,6 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Instruction *I = Expr->castInstructionAsserting();
|
Instruction *I = Expr->castInstructionAsserting();
|
||||||
ConstantPool &CP = I->getParent()->getParent()->getConstantPool();
|
|
||||||
const Type *Ty = I->getType();
|
const Type *Ty = I->getType();
|
||||||
|
|
||||||
switch (I->getOpcode()) { // Handle each instruction type seperately
|
switch (I->getOpcode()) { // Handle each instruction type seperately
|
||||||
@@ -214,15 +191,15 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
|
|||||||
switch (Left.ExprTy) {
|
switch (Left.ExprTy) {
|
||||||
case ExprType::Constant:
|
case ExprType::Constant:
|
||||||
return ExprType(Right.Scale, Right.Var,
|
return ExprType(Right.Scale, Right.Var,
|
||||||
DefZero(Right.Offset,CP,Ty) + DefZero(Left.Offset, CP,Ty));
|
DefZero(Right.Offset, Ty) + DefZero(Left.Offset, Ty));
|
||||||
case ExprType::Linear: // RHS side must be linear or scaled
|
case ExprType::Linear: // RHS side must be linear or scaled
|
||||||
case ExprType::ScaledLinear: // RHS must be scaled
|
case ExprType::ScaledLinear: // RHS must be scaled
|
||||||
if (Left.Var != Right.Var) // Are they the same variables?
|
if (Left.Var != Right.Var) // Are they the same variables?
|
||||||
return ExprType(I); // if not, we don't know anything!
|
return ExprType(I); // if not, we don't know anything!
|
||||||
|
|
||||||
return ExprType(DefOne(Left.Scale ,CP,Ty) + DefOne(Right.Scale , CP,Ty),
|
return ExprType( DefOne(Left.Scale , Ty) + DefOne(Right.Scale , Ty),
|
||||||
Left.Var,
|
Left.Var,
|
||||||
DefZero(Left.Offset,CP,Ty) + DefZero(Right.Offset, CP,Ty));
|
DefZero(Left.Offset, Ty) + DefZero(Right.Offset, Ty));
|
||||||
}
|
}
|
||||||
} // end case Instruction::Add
|
} // end case Instruction::Add
|
||||||
|
|
||||||
@@ -234,11 +211,10 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
|
|||||||
assert(Right.Offset->getType() == Type::UByteTy &&
|
assert(Right.Offset->getType() == Type::UByteTy &&
|
||||||
"Shift amount must always be a unsigned byte!");
|
"Shift amount must always be a unsigned byte!");
|
||||||
uint64_t ShiftAmount = ((ConstPoolUInt*)Right.Offset)->getValue();
|
uint64_t ShiftAmount = ((ConstPoolUInt*)Right.Offset)->getValue();
|
||||||
ConstPoolInt *Multiplier = getUnsignedConstant(CP, 1ULL << ShiftAmount, Ty);
|
ConstPoolInt *Multiplier = getUnsignedConstant(1ULL << ShiftAmount, Ty);
|
||||||
|
|
||||||
return ExprType(DefOne(Left.Scale, CP, Ty) * Multiplier,
|
return ExprType(DefOne(Left.Scale, Ty) * Multiplier, Left.Var,
|
||||||
Left.Var,
|
DefZero(Left.Offset, Ty) * Multiplier);
|
||||||
DefZero(Left.Offset, CP, Ty) * Multiplier);
|
|
||||||
} // end case Instruction::Shl
|
} // end case Instruction::Shl
|
||||||
|
|
||||||
case Instruction::Mul: {
|
case Instruction::Mul: {
|
||||||
@@ -252,9 +228,8 @@ ExprType analysis::ClassifyExpression(Value *Expr) {
|
|||||||
|
|
||||||
const ConstPoolInt *Offs = Left.Offset;
|
const ConstPoolInt *Offs = Left.Offset;
|
||||||
if (Offs == 0) return ExprType();
|
if (Offs == 0) return ExprType();
|
||||||
return ExprType(DefOne(Right.Scale, CP, Ty) * Offs,
|
return ExprType( DefOne(Right.Scale , Ty) * Offs, Right.Var,
|
||||||
Right.Var,
|
DefZero(Right.Offset, Ty) * Offs);
|
||||||
DefZero(Right.Offset, CP, Ty) * Offs);
|
|
||||||
} // end case Instruction::Mul
|
} // end case Instruction::Mul
|
||||||
|
|
||||||
case Instruction::Cast: {
|
case Instruction::Cast: {
|
||||||
|
Reference in New Issue
Block a user