diff options
| author | Reid Spencer <rspencer@reidspencer.com> | 2006-12-04 20:17:56 +0000 |
|---|---|---|
| committer | Reid Spencer <rspencer@reidspencer.com> | 2006-12-04 20:17:56 +0000 |
| commit | 5c14088844effd2a9ec483cc4b71be4dbacdaa57 (patch) | |
| tree | 1d2f5cfd6fe7a19406980148ab11e9908daffe1a /llvm/lib/VMCore | |
| parent | 332109374a628563c43036561c49a0cee0447237 (diff) | |
| download | bcm5719-llvm-5c14088844effd2a9ec483cc4b71be4dbacdaa57.tar.gz bcm5719-llvm-5c14088844effd2a9ec483cc4b71be4dbacdaa57.zip | |
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
Diffstat (limited to 'llvm/lib/VMCore')
| -rw-r--r-- | llvm/lib/VMCore/Constants.cpp | 22 | ||||
| -rw-r--r-- | llvm/lib/VMCore/Instructions.cpp | 48 |
2 files changed, 68 insertions, 2 deletions
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<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); } 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: |

