From 6d81a7d2aefd769267efe40e5a78f9427039e8b0 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Tue, 12 Dec 2006 00:49:44 +0000 Subject: [PATCH] Implement createIntegerCast and createFPCast factory methods for handling integer and floating point cast creation. createIntegerCast generates ZExt/SExt, BitCast or Trunc. createFPCast generates FPExt, Bitcast, or FPTrunc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32456 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/InstrTypes.h | 34 ++++++++++++++++++++++++ lib/VMCore/Instructions.cpp | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/include/llvm/InstrTypes.h b/include/llvm/InstrTypes.h index eaf56a301cd..6fa9f19f634 100644 --- a/include/llvm/InstrTypes.h +++ b/include/llvm/InstrTypes.h @@ -339,6 +339,40 @@ public: Instruction *InsertBefore = 0 ///< Place to insert the instruction ); + /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts. + static CastInst *createIntegerCast( + Value *S, ///< The pointer value to be casted (operand 0) + const Type *Ty, ///< The type to which cast should be made + bool isSigned, ///< Whether to regard S as signed or not + const std::string &Name = "", ///< Name for the instruction + Instruction *InsertBefore = 0 ///< Place to insert the instruction + ); + + /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts. + static CastInst *createIntegerCast( + Value *S, ///< The integer value to be casted (operand 0) + const Type *Ty, ///< The integer type to which operand is casted + bool isSigned, ///< Whether to regard S as signed or not + const std::string &Name, ///< The name for the instruction + BasicBlock *InsertAtEnd ///< The block to insert the instruction into + ); + + /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts + static CastInst *createFPCast( + Value *S, ///< The floating point value to be casted + const Type *Ty, ///< The floating point type to cast to + const std::string &Name = "", ///< Name for the instruction + Instruction *InsertBefore = 0 ///< Place to insert the instruction + ); + + /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts + static CastInst *createFPCast( + Value *S, ///< The floating point value to be casted + const Type *Ty, ///< The floating point type to cast to + 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) diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 57dd5a56b8a..07a44ee750f 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -1580,6 +1580,58 @@ CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, return create(Instruction::BitCast, S, Ty, Name, InsertBefore); } +CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, + bool isSigned, const std::string &Name, + Instruction *InsertBefore) { + 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 create(opcode, C, Ty, Name, InsertBefore); +} + +CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, + bool isSigned, const std::string &Name, + BasicBlock *InsertAtEnd) { + 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 create(opcode, C, Ty, Name, InsertAtEnd); +} + +CastInst *CastInst::createFPCast(Value *C, const Type *Ty, + const std::string &Name, + Instruction *InsertBefore) { + 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 create(opcode, C, Ty, Name, InsertBefore); +} + +CastInst *CastInst::createFPCast(Value *C, const Type *Ty, + const std::string &Name, + BasicBlock *InsertAtEnd) { + 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 create(opcode, C, 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: