1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

Implement getIntegerCast and getFPCast for ConstantExpr. These are similar

to the createIntegerCast and createFPCast for CastInst instructions.

llvm-svn: 32457
This commit is contained in:
Reid Spencer 2006-12-12 00:51:07 +00:00
parent a82b285a69
commit 3fbfed6e2c
2 changed files with 36 additions and 0 deletions

View File

@ -559,6 +559,19 @@ public:
const Type *Ty ///< The type to which cast should be made
);
/// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
static Constant *getIntegerCast(
Constant *C, ///< The integer constant to be casted
const Type *Ty, ///< The integer type to cast to
bool isSigned ///< Whether C should be treated as signed or not
);
/// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
static Constant *getFPCast(
Constant *C, ///< The integer constant to be casted
const Type *Ty ///< The integer type to cast to
);
static Constant *getCast(Constant *C, const Type *Ty);
/// @brief Return true if this is a convert constant expression

View File

@ -1514,6 +1514,29 @@ Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
return getCast(Instruction::BitCast, S, Ty);
}
Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty,
bool isSigned) {
assert(C->getType()->isIntegral() && Ty->isIntegral() && "Invalid cast");
unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
unsigned DstBits = Ty->getPrimitiveSizeInBits();
Instruction::CastOps opcode =
(SrcBits == DstBits ? Instruction::BitCast :
(SrcBits > DstBits ? Instruction::Trunc :
(isSigned ? Instruction::SExt : Instruction::ZExt)));
return getCast(opcode, C, Ty);
}
Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
"Invalid cast");
unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
unsigned DstBits = Ty->getPrimitiveSizeInBits();
Instruction::CastOps opcode =
(SrcBits == DstBits ? Instruction::BitCast :
(SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
return getCast(opcode, C, Ty);
}
Constant *ConstantExpr::getTrunc(Constant *C, const Type *Ty) {
assert(C->getType()->isInteger() && "Trunc operand must be integer");
assert(Ty->isIntegral() && "Trunc produces only integral");