From 848414e49c7600e3002a4366de52d03a9638b327 Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Mon, 4 Dec 2006 20:17:56 +0000 Subject: [PATCH] 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 --- include/llvm/Constants.h | 18 ++++++++++++++ include/llvm/InstrTypes.h | 48 +++++++++++++++++++++++++++++++++++++ lib/VMCore/Constants.cpp | 22 +++++++++++++++-- lib/VMCore/Instructions.cpp | 48 +++++++++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) diff --git a/include/llvm/Constants.h b/include/llvm/Constants.h index d4744d4d72e..47255de655b 100644 --- a/include/llvm/Constants.h +++ b/include/llvm/Constants.h @@ -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 diff --git a/include/llvm/InstrTypes.h b/include/llvm/InstrTypes.h index 07d13da59d5..0878a9c4b5a 100644 --- a/include/llvm/InstrTypes.h +++ b/include/llvm/InstrTypes.h @@ -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( diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 17a5eaeabf3..966108748b8 100644 --- a/lib/VMCore/Constants.cpp +++ b/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/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index de1ebced7bc..8c1f47d8afb 100644 --- a/lib/VMCore/Instructions.cpp +++ b/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: