Implement getIntegerCast and getFPCast for ConstantExpr. These are similar
authorReid Spencer <rspencer@reidspencer.com>
Tue, 12 Dec 2006 00:51:07 +0000 (00:51 +0000)
committerReid Spencer <rspencer@reidspencer.com>
Tue, 12 Dec 2006 00:51:07 +0000 (00:51 +0000)
to the createIntegerCast and createFPCast for CastInst instructions.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32457 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
lib/VMCore/Constants.cpp

index 04cad0b1b7ea13fafd237b2b3fb8e12d99d7ba8f..f06d15e26d703bb2849a796e0ceb024f268713bc 100644 (file)
@@ -559,6 +559,19 @@ public:
     const Type *Ty ///< The type to which cast should be made
   );
 
+  /// @brief Create a ZExt, Bitcast or Trunc for integer -> integer casts
+  static Constant *getIntegerCast(
+    Constant *C,    ///< The integer constant to be casted 
+    const Type *Ty, ///< The integer type to cast to
+    bool isSigned   ///< Whether C should be treated as signed or not
+  );
+
+  /// @brief Create a FPExt, Bitcast or FPTrunc for fp -> fp casts
+  static Constant *getFPCast(
+    Constant *C,    ///< The integer constant to be casted 
+    const Type *Ty ///< The integer type to cast to
+  );
+
   static Constant *getCast(Constant *C, const Type *Ty);
 
   /// @brief Return true if this is a convert constant expression
index b59341d9272f3aa807993e5b668395388fc50c3b..2aa0524dfd0565abb0827e8326dec291a558269c 100644 (file)
@@ -1514,6 +1514,29 @@ Constant *ConstantExpr::getPointerCast(Constant *S, const Type *Ty) {
   return getCast(Instruction::BitCast, S, Ty);
 }
 
+Constant *ConstantExpr::getIntegerCast(Constant *C, const Type *Ty, 
+                                       bool isSigned) {
+  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 getCast(opcode, C, Ty);
+}
+
+Constant *ConstantExpr::getFPCast(Constant *C, const Type *Ty) {
+  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 getCast(opcode, 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");