Implement new cast creation functions for both instructions and constant

expressions. These will get used to reduce clutter as we replace various
calls to createInferredCast and getCast.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32191 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2006-12-04 20:17:56 +00:00
parent 4d42fcebe3
commit 848414e49c
4 changed files with 134 additions and 2 deletions

View File

@ -541,6 +541,24 @@ public:
const Type *Ty ///< The type to which the constant is converted
);
// @brief Create a ZExt or BitCast cast constant expression
static Constant *getZExtOrBitCast(
Constant *C, ///< The constant to zext or bitcast
const Type *Ty ///< The type to zext or bitcast C to
);
// @brief Create a SExt or BitCast cast constant expression
static Constant *getSExtOrBitCast(
Constant *C, ///< The constant to zext or bitcast
const Type *Ty ///< The type to zext or bitcast C to
);
// @brief Create a Trunc or BitCast cast constant expression
static Constant *getTruncOrBitCast(
Constant *C, ///< The constant to zext or bitcast
const Type *Ty ///< The type to zext or bitcast C to
);
// This method uses the CastInst::getCastOpcode method to infer the
// cast opcode to use.
// @brief Get a ConstantExpr Conversion operator that casts C to Ty

View File

@ -299,6 +299,54 @@ public:
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
);
/// @brief Create a ZExt or BitCast cast instruction
static CastInst *createZExtOrBitCast(
Value *S, ///< The value to be casted (operand 0)
const Type *Ty, ///< The type to which cast should be made
const std::string &Name = "", ///< Name for the instruction
Instruction *InsertBefore = 0 ///< Place to insert the instruction
);
/// @brief Create a ZExt or BitCast cast instruction
static CastInst *createZExtOrBitCast(
Value *S, ///< The value to be casted (operand 0)
const Type *Ty, ///< The type to which operand is casted
const std::string &Name, ///< The name for the instruction
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
);
/// @brief Create a SExt or BitCast cast instruction
static CastInst *createSExtOrBitCast(
Value *S, ///< The value to be casted (operand 0)
const Type *Ty, ///< The type to which cast should be made
const std::string &Name = "", ///< Name for the instruction
Instruction *InsertBefore = 0 ///< Place to insert the instruction
);
/// @brief Create a SExt or BitCast cast instruction
static CastInst *createSExtOrBitCast(
Value *S, ///< The value to be casted (operand 0)
const Type *Ty, ///< The type to which operand is casted
const std::string &Name, ///< The name for the instruction
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
);
/// @brief Create a Trunc or BitCast cast instruction
static CastInst *createTruncOrBitCast(
Value *S, ///< The value to be casted (operand 0)
const Type *Ty, ///< The type to which cast should be made
const std::string &Name = "", ///< Name for the instruction
Instruction *InsertBefore = 0 ///< Place to insert the instruction
);
/// @brief Create a Trunc or BitCast cast instruction
static CastInst *createTruncOrBitCast(
Value *S, ///< The value to be casted (operand 0)
const Type *Ty, ///< The type to which operand is casted
const std::string &Name, ///< The name for the instruction
BasicBlock *InsertAtEnd ///< The block to insert the instruction into
);
/// Returns the opcode necessary to cast Val into Ty using usual casting
/// rules.
static Instruction::CastOps getCastOpcode(

View File

@ -1534,6 +1534,24 @@ Constant *ConstantExpr::getCast(unsigned oc, Constant *C, const Type *Ty) {
return 0;
}
Constant *ConstantExpr::getZExtOrBitCast(Constant *C, const Type *Ty) {
if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return getCast(Instruction::BitCast, C, Ty);
return getCast(Instruction::ZExt, C, Ty);
}
Constant *ConstantExpr::getSExtOrBitCast(Constant *C, const Type *Ty) {
if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return getCast(Instruction::BitCast, C, Ty);
return getCast(Instruction::SExt, C, Ty);
}
Constant *ConstantExpr::getTruncOrBitCast(Constant *C, const Type *Ty) {
if (C->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return getCast(Instruction::BitCast, C, Ty);
return getCast(Instruction::Trunc, 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");
@ -1616,14 +1634,14 @@ Constant *ConstantExpr::getBitCast(Constant *C, const Type *DstTy) {
// can't cast pointers to anything but pointers.
const Type *SrcTy = C->getType();
assert((isa<PointerType>(SrcTy) == isa<PointerType>(DstTy)) &&
"Bitcast cannot cast pointer to non-pointer and vice versa");
"BitCast cannot cast pointer to non-pointer and vice versa");
// Now we know we're not dealing with mismatched pointer casts (ptr->nonptr
// or nonptr->ptr). For all the other types, the cast is okay if source and
// destination bit widths are identical.
unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
assert(SrcBitSize == DstBitSize && "Bitcast requies types of same width");
assert(SrcBitSize == DstBitSize && "BitCast requies types of same width");
return getFoldedCast(Instruction::BitCast, C, DstTy);
}

View File

@ -1500,6 +1500,54 @@ CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
return 0;
}
CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
const std::string &Name,
Instruction *InsertBefore) {
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
}
CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
const std::string &Name,
BasicBlock *InsertAtEnd) {
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
}
CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
const std::string &Name,
Instruction *InsertBefore) {
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
return create(Instruction::SExt, S, Ty, Name, InsertBefore);
}
CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
const std::string &Name,
BasicBlock *InsertAtEnd) {
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
}
CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
const std::string &Name,
Instruction *InsertBefore) {
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
}
CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
const std::string &Name,
BasicBlock *InsertAtEnd) {
if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
}
// Provide a way to get a "cast" where the cast opcode is inferred from the
// types and size of the operand. This, basically, is a parallel of the
// logic in the checkCast function below. This axiom should hold: