From 5c14088844effd2a9ec483cc4b71be4dbacdaa57 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Mon, 4 Dec 2006 20:17:56 +0000 Subject: 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. llvm-svn: 32191 --- llvm/lib/VMCore/Constants.cpp | 22 ++++++++++++++++-- llvm/lib/VMCore/Instructions.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) (limited to 'llvm/lib/VMCore') diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 17a5eaeabf3..966108748b8 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -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(SrcTy) == isa(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); } diff --git a/llvm/lib/VMCore/Instructions.cpp b/llvm/lib/VMCore/Instructions.cpp index de1ebced7bc..8c1f47d8afb 100644 --- a/llvm/lib/VMCore/Instructions.cpp +++ b/llvm/lib/VMCore/Instructions.cpp @@ -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: -- cgit v1.2.3