[llvm-commits] [llvm] r44204 - in /llvm/trunk: docs/LangRef.html lib/CodeGen/SelectionDAG/LegalizeDAG.cpp lib/Target/X86/X86InstrSSE.td lib/VMCore/ConstantFold.cpp lib/VMCore/Constants.cpp lib/VMCore/Instructions.cpp lib/VMCore/Verifier.cpp test/Feature/newcasts.ll

Nate Begeman natebegeman at mac.com
Fri Nov 16 19:58:36 PST 2007


Author: sampo
Date: Fri Nov 16 21:58:34 2007
New Revision: 44204

URL: http://llvm.org/viewvc/llvm-project?rev=44204&view=rev
Log:
Add support for vectors to int <-> float casts.

Modified:
    llvm/trunk/docs/LangRef.html
    llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
    llvm/trunk/lib/Target/X86/X86InstrSSE.td
    llvm/trunk/lib/VMCore/ConstantFold.cpp
    llvm/trunk/lib/VMCore/Constants.cpp
    llvm/trunk/lib/VMCore/Instructions.cpp
    llvm/trunk/lib/VMCore/Verifier.cpp
    llvm/trunk/test/Feature/newcasts.ll

Modified: llvm/trunk/docs/LangRef.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/LangRef.html?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/docs/LangRef.html (original)
+++ llvm/trunk/docs/LangRef.html Fri Nov 16 21:58:34 2007
@@ -1511,23 +1511,31 @@
 
   <dt><b><tt>fptoui ( CST to TYPE )</tt></b></dt>
   <dd>Convert a floating point constant to the corresponding unsigned integer
-  constant. TYPE must be an integer type. CST must be floating point. If the 
-  value won't fit in the integer type, the results are undefined.</dd>
+  constant. TYPE must be a scalar or vector integer type. CST must be of scalar
+  or vector floating point type. Both CST and TYPE must be scalars, or vectors
+  of the same number of elements. If the  value won't fit in the integer type,
+  the results are undefined.</dd>
 
   <dt><b><tt>fptosi ( CST to TYPE )</tt></b></dt>
   <dd>Convert a floating point constant to the corresponding signed integer
-  constant. TYPE must be an integer type. CST must be floating point. If the 
-  value won't fit in the integer type, the results are undefined.</dd>
+  constant.  TYPE must be a scalar or vector integer type. CST must be of scalar
+  or vector floating point type. Both CST and TYPE must be scalars, or vectors
+  of the same number of elements. If the  value won't fit in the integer type,
+  the results are undefined.</dd>
 
   <dt><b><tt>uitofp ( CST to TYPE )</tt></b></dt>
   <dd>Convert an unsigned integer constant to the corresponding floating point
-  constant. TYPE must be floating point. CST must be of integer type. If the
-  value won't fit in the floating point type, the results are undefined.</dd>
+  constant. TYPE must be a scalar or vector floating point type. CST must be of
+  scalar or vector integer type. Both CST and TYPE must be scalars, or vectors
+  of the same number of elements. If the value won't fit in the floating point 
+  type, the results are undefined.</dd>
 
   <dt><b><tt>sitofp ( CST to TYPE )</tt></b></dt>
   <dd>Convert a signed integer constant to the corresponding floating point
-  constant. TYPE must be floating point. CST must be of integer type. If the
-  value won't fit in the floating point type, the results are undefined.</dd>
+  constant. TYPE must be a scalar or vector floating point type. CST must be of
+  scalar or vector integer type. Both CST and TYPE must be scalars, or vectors
+  of the same number of elements. If the value won't fit in the floating point 
+  type, the results are undefined.</dd>
 
   <dt><b><tt>ptrtoint ( CST to TYPE )</tt></b></dt>
   <dd>Convert a pointer typed constant to the corresponding integer constant
@@ -3136,8 +3144,10 @@
 
 <h5>Arguments:</h5>
 <p>The '<tt>fptoui</tt>' instruction takes a value to cast, which must be a 
-<a href="#t_floating">floating point</a> value, and a type to cast it to, which
-must be an <a href="#t_integer">integer</a> type.</p>
+scalar or vector <a href="#t_floating">floating point</a> value, and a type 
+to cast it to <tt>ty2</tt>, which must be an <a href="#t_integer">integer</a> 
+type. If <tt>ty</tt> is a vector floating point type, <tt>ty2</tt> must be a
+vector integer type with the same number of elements as <tt>ty</tt></p>
 
 <h5>Semantics:</h5>
 <p> The '<tt>fptoui</tt>' instruction converts its 
@@ -3169,11 +3179,12 @@
 <a href="#t_floating">floating point</a> <tt>value</tt> to type <tt>ty2</tt>.
 </p>
 
-
 <h5>Arguments:</h5>
 <p> The '<tt>fptosi</tt>' instruction takes a value to cast, which must be a 
-<a href="#t_floating">floating point</a> value, and a type to cast it to, which 
-must also be an <a href="#t_integer">integer</a> type.</p>
+scalar or vector <a href="#t_floating">floating point</a> value, and a type 
+to cast it to <tt>ty2</tt>, which must be an <a href="#t_integer">integer</a> 
+type. If <tt>ty</tt> is a vector floating point type, <tt>ty2</tt> must be a
+vector integer type with the same number of elements as <tt>ty</tt></p>
 
 <h5>Semantics:</h5>
 <p>The '<tt>fptosi</tt>' instruction converts its 
@@ -3204,18 +3215,18 @@
 <p>The '<tt>uitofp</tt>' instruction regards <tt>value</tt> as an unsigned
 integer and converts that value to the <tt>ty2</tt> type.</p>
 
-
 <h5>Arguments:</h5>
-<p>The '<tt>uitofp</tt>' instruction takes a value to cast, which must be an
-<a href="#t_integer">integer</a> value, and a type to cast it to, which must 
-be a <a href="#t_floating">floating point</a> type.</p>
+<p>The '<tt>uitofp</tt>' instruction takes a value to cast, which must be a
+scalar or vector <a href="#t_integer">integer</a> value, and a type to cast it
+to <tt>ty2</tt>, which must be an <a href="#t_floating">floating point</a> 
+type. If <tt>ty</tt> is a vector integer type, <tt>ty2</tt> must be a vector
+floating point type with the same number of elements as <tt>ty</tt></p>
 
 <h5>Semantics:</h5>
 <p>The '<tt>uitofp</tt>' instruction interprets its operand as an unsigned
 integer quantity and converts it to the corresponding floating point value. If
 the value cannot fit in the floating point value, the results are undefined.</p>
 
-
 <h5>Example:</h5>
 <pre>
   %X = uitofp i32 257 to float         <i>; yields float:257.0</i>
@@ -3239,9 +3250,11 @@
 integer and converts that value to the <tt>ty2</tt> type.</p>
 
 <h5>Arguments:</h5>
-<p>The '<tt>sitofp</tt>' instruction takes a value to cast, which must be an
-<a href="#t_integer">integer</a> value, and a type to cast it to, which must be
-a <a href="#t_floating">floating point</a> type.</p>
+<p>The '<tt>sitofp</tt>' instruction takes a value to cast, which must be a
+scalar or vector <a href="#t_integer">integer</a> value, and a type to cast it
+to <tt>ty2</tt>, which must be an <a href="#t_floating">floating point</a> 
+type. If <tt>ty</tt> is a vector integer type, <tt>ty2</tt> must be a vector
+floating point type with the same number of elements as <tt>ty</tt></p>
 
 <h5>Semantics:</h5>
 <p>The '<tt>sitofp</tt>' instruction interprets its operand as a signed

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp Fri Nov 16 21:58:34 2007
@@ -6365,7 +6365,11 @@
   case ISD::FABS:
   case ISD::FSQRT:
   case ISD::FSIN:
-  case ISD::FCOS: {
+  case ISD::FCOS:
+  case ISD::FP_TO_SINT:
+  case ISD::FP_TO_UINT:
+  case ISD::SINT_TO_FP:
+  case ISD::UINT_TO_FP: {
     SDOperand L, H;
     SplitVectorOp(Node->getOperand(0), L, H);
 

Modified: llvm/trunk/lib/Target/X86/X86InstrSSE.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86InstrSSE.td?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/lib/Target/X86/X86InstrSSE.td (original)
+++ llvm/trunk/lib/Target/X86/X86InstrSSE.td Fri Nov 16 21:58:34 2007
@@ -2967,6 +2967,12 @@
                   (memopv2i64 addr:$src2))),
           (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
 
+// vector -> vector casts
+def : Pat<(v4f32 (sint_to_fp (v4i32 VR128:$src))),
+          (Int_CVTDQ2PSrr VR128:$src)>, Requires<[HasSSE2]>;
+def : Pat<(v4i32 (fp_to_sint (v4f32 VR128:$src))),
+          (Int_CVTTPS2DQrr VR128:$src)>, Requires<[HasSSE2]>;
+
 // Use movaps / movups for SSE integer load / store (one byte shorter).
 def : Pat<(alignedloadv4i32 addr:$src),
           (MOVAPSrm addr:$src)>, Requires<[HasSSE1]>;

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Fri Nov 16 21:58:34 2007
@@ -202,6 +202,15 @@
       APInt Val(DestBitWidth, 2, x);
       return ConstantInt::get(Val);
     }
+    if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
+      std::vector<Constant*> res;
+      const VectorType *DestVecTy = cast<VectorType>(DestTy);
+      const Type *DstEltTy = DestVecTy->getElementType();
+      for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
+        res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i),
+                                                  DstEltTy));
+      return ConstantVector::get(DestVecTy, res);
+    }
     return 0; // Can't fold.
   case Instruction::IntToPtr:   //always treated as unsigned
     if (V->isNullValue())       // Is it an integral null value?
@@ -224,6 +233,15 @@
                                    APFloat::rmNearestTiesToEven);
       return ConstantFP::get(DestTy, apf);
     }
+    if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
+      std::vector<Constant*> res;
+      const VectorType *DestVecTy = cast<VectorType>(DestTy);
+      const Type *DstEltTy = DestVecTy->getElementType();
+      for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
+        res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i),
+                                                  DstEltTy));
+      return ConstantVector::get(DestVecTy, res);
+    }
     return 0;
   case Instruction::ZExt:
     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Fri Nov 16 21:58:34 2007
@@ -1643,26 +1643,38 @@
 }
 
 Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
-  assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
-         "This is an illegal i32 to floating point cast!");
+  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
+  bool toVec = Ty->getTypeID() == Type::VectorTyID;
+  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
+  assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
+         "This is an illegal uint to floating point cast!");
   return getFoldedCast(Instruction::UIToFP, C, Ty);
 }
 
 Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
-  assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
+  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
+  bool toVec = Ty->getTypeID() == Type::VectorTyID;
+  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
+  assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
          "This is an illegal sint to floating point cast!");
   return getFoldedCast(Instruction::SIToFP, C, Ty);
 }
 
 Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
-  assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
-         "This is an illegal floating point to i32 cast!");
+  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
+  bool toVec = Ty->getTypeID() == Type::VectorTyID;
+  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
+  assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
+         "This is an illegal floating point to uint cast!");
   return getFoldedCast(Instruction::FPToUI, C, Ty);
 }
 
 Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
-  assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
-         "This is an illegal floating point to i32 cast!");
+  bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
+  bool toVec = Ty->getTypeID() == Type::VectorTyID;
+  assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
+  assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
+         "This is an illegal floating point to sint cast!");
   return getFoldedCast(Instruction::FPToSI, C, Ty);
 }
 

Modified: llvm/trunk/lib/VMCore/Instructions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Instructions.cpp?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Instructions.cpp (original)
+++ llvm/trunk/lib/VMCore/Instructions.cpp Fri Nov 16 21:58:34 2007
@@ -1912,12 +1912,24 @@
     return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && 
       SrcBitSize < DstBitSize;
   case Instruction::UIToFP:
-    return SrcTy->isInteger() && DstTy->isFloatingPoint();
   case Instruction::SIToFP:
+    if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
+      if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
+        return SVTy->getElementType()->isInteger() &&
+               DVTy->getElementType()->isFloatingPoint() &&
+               SVTy->getNumElements() == DVTy->getNumElements();
+      }
+    }
     return SrcTy->isInteger() && DstTy->isFloatingPoint();
   case Instruction::FPToUI:
-    return SrcTy->isFloatingPoint() && DstTy->isInteger();
   case Instruction::FPToSI:
+    if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
+      if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
+        return SVTy->getElementType()->isFloatingPoint() &&
+               DVTy->getElementType()->isInteger() &&
+               SVTy->getNumElements() == DVTy->getNumElements();
+      }
+    }
     return SrcTy->isFloatingPoint() && DstTy->isInteger();
   case Instruction::PtrToInt:
     return isa<PointerType>(SrcTy) && DstTy->isInteger();

Modified: llvm/trunk/lib/VMCore/Verifier.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Verifier.cpp?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Verifier.cpp (original)
+++ llvm/trunk/lib/VMCore/Verifier.cpp Fri Nov 16 21:58:34 2007
@@ -709,8 +709,16 @@
   const Type *SrcTy = I.getOperand(0)->getType();
   const Type *DestTy = I.getType();
 
-  Assert1(SrcTy->isInteger(),"UInt2FP source must be integral", &I);
-  Assert1(DestTy->isFloatingPoint(),"UInt2FP result must be FP", &I);
+  bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
+  bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
+
+  Assert1(SrcVec == DstVec,"UIToFP source and dest must both be vector or scalar", &I);
+  Assert1(SrcTy->isIntOrIntVector(),"UIToFP source must be integer or integer vector", &I);
+  Assert1(DestTy->isFPOrFPVector(),"UIToFP result must be FP or FP vector", &I);
+
+  if (SrcVec && DstVec)
+    Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
+            "UIToFP source and dest vector length mismatch", &I);
 
   visitInstruction(I);
 }
@@ -720,8 +728,16 @@
   const Type *SrcTy = I.getOperand(0)->getType();
   const Type *DestTy = I.getType();
 
-  Assert1(SrcTy->isInteger(),"SInt2FP source must be integral", &I);
-  Assert1(DestTy->isFloatingPoint(),"SInt2FP result must be FP", &I);
+  bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
+  bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
+
+  Assert1(SrcVec == DstVec,"SIToFP source and dest must both be vector or scalar", &I);
+  Assert1(SrcTy->isIntOrIntVector(),"SIToFP source must be integer or integer vector", &I);
+  Assert1(DestTy->isFPOrFPVector(),"SIToFP result must be FP or FP vector", &I);
+
+  if (SrcVec && DstVec)
+    Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
+            "SIToFP source and dest vector length mismatch", &I);
 
   visitInstruction(I);
 }
@@ -731,8 +747,16 @@
   const Type *SrcTy = I.getOperand(0)->getType();
   const Type *DestTy = I.getType();
 
-  Assert1(SrcTy->isFloatingPoint(),"FP2UInt source must be FP", &I);
-  Assert1(DestTy->isInteger(),"FP2UInt result must be integral", &I);
+  bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
+  bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
+
+  Assert1(SrcVec == DstVec,"FPToUI source and dest must both be vector or scalar", &I);
+  Assert1(SrcTy->isFPOrFPVector(),"FPToUI source must be FP or FP vector", &I);
+  Assert1(DestTy->isIntOrIntVector(),"FPToUI result must be integer or integer vector", &I);
+
+  if (SrcVec && DstVec)
+    Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
+            "FPToUI source and dest vector length mismatch", &I);
 
   visitInstruction(I);
 }
@@ -742,8 +766,16 @@
   const Type *SrcTy = I.getOperand(0)->getType();
   const Type *DestTy = I.getType();
 
-  Assert1(SrcTy->isFloatingPoint(),"FPToSI source must be FP", &I);
-  Assert1(DestTy->isInteger(),"FP2ToI result must be integral", &I);
+  bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
+  bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
+
+  Assert1(SrcVec == DstVec,"FPToSI source and dest must both be vector or scalar", &I);
+  Assert1(SrcTy->isFPOrFPVector(),"FPToSI source must be FP or FP vector", &I);
+  Assert1(DestTy->isIntOrIntVector(),"FPToSI result must be integer or integer vector", &I);
+
+  if (SrcVec && DstVec)
+    Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
+            "FPToSI source and dest vector length mismatch", &I);
 
   visitInstruction(I);
 }

Modified: llvm/trunk/test/Feature/newcasts.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Feature/newcasts.ll?rev=44204&r1=44203&r2=44204&view=diff

==============================================================================
--- llvm/trunk/test/Feature/newcasts.ll (original)
+++ llvm/trunk/test/Feature/newcasts.ll Fri Nov 16 21:58:34 2007
@@ -15,6 +15,11 @@
   %k = bitcast i32 %a to float
   %l = inttoptr i16 %x to i32*
   %m = ptrtoint i32* %l to i64
+  %n = insertelement <4 x i32> undef, i32 %a, i32 0
+  %o = sitofp <4 x i32> %n to <4 x float>
+  %p = uitofp <4 x i32> %n to <4 x float>
+  %q = fptosi <4 x float> %p to <4 x i32>
+  %r = fptoui <4 x float> %p to <4 x i32>
   ret void
 }
 





More information about the llvm-commits mailing list