Implement new cast creation functions for both instructions and constant
authorReid Spencer <rspencer@reidspencer.com>
Mon, 4 Dec 2006 20:17:56 +0000 (20:17 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Mon, 4 Dec 2006 20:17:56 +0000 (20:17 +0000)
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
include/llvm/InstrTypes.h
lib/VMCore/Constants.cpp
lib/VMCore/Instructions.cpp

index d4744d4d72ee02dbaf81d3a82c108fd704470288..47255de655bea07737c9d18b42b17de39937d640 100644 (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
index 07d13da59d5206967922b6b304a3e5bfd87ded49..0878a9c4b5a4764e05cb2d9bf6ec4ba37e8c9599 100644 (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(
index 17a5eaeabf3f832999b3f7b5fd236d2e15e9c4ba..966108748b834c0cdfe55166f6ff75df53b51a26 100644 (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);
 }
 
index de1ebced7bcc4f6b74d0e23fb29131f095b44db5..8c1f47d8afb360c0928e22b6bb19c8f7951ef9f4 100644 (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: