Implement createIntegerCast and createFPCast factory methods for handling
authorReid Spencer <rspencer@reidspencer.com>
Tue, 12 Dec 2006 00:49:44 +0000 (00:49 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Tue, 12 Dec 2006 00:49:44 +0000 (00:49 +0000)
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
lib/VMCore/Instructions.cpp

index eaf56a301cdc843995b4144a386ab8b15c7ef363..6fa9f19f634884280c942d633737ddfce9bf2e34 100644 (file)
@@ -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)
index 57dd5a56b8ab7871741fc585f590c73c8bde0262..07a44ee750f0568dde0c9748f88bcfde8047f6af 100644 (file)
@@ -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: