llvm.org GIT mirror llvm / b348d18
Add support for vectors to int <-> float casts. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44204 91177308-0d34-0410-b5e6-96231b3b80d8 Nate Begeman 12 years ago
8 changed file(s) with 142 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
15101510
15111511
fptoui ( CST to TYPE )
15121512
Convert a floating point constant to the corresponding unsigned integer
1513 constant. TYPE must be an integer type. CST must be floating point. If the
1514 value won't fit in the integer type, the results are undefined.
1513 constant. TYPE must be a scalar or vector integer type. CST must be of scalar
1514 or vector floating point type. Both CST and TYPE must be scalars, or vectors
1515 of the same number of elements. If the value won't fit in the integer type,
1516 the results are undefined.
15151517
15161518
fptosi ( CST to TYPE )
15171519
Convert a floating point constant to the corresponding signed integer
1518 constant. TYPE must be an integer type. CST must be floating point. If the
1519 value won't fit in the integer type, the results are undefined.
1520 constant. TYPE must be a scalar or vector integer type. CST must be of scalar
1521 or vector floating point type. Both CST and TYPE must be scalars, or vectors
1522 of the same number of elements. If the value won't fit in the integer type,
1523 the results are undefined.
15201524
15211525
uitofp ( CST to TYPE )
15221526
Convert an unsigned integer constant to the corresponding floating point
1523 constant. TYPE must be floating point. CST must be of integer type. If the
1524 value won't fit in the floating point type, the results are undefined.
1527 constant. TYPE must be a scalar or vector floating point type. CST must be of
1528 scalar or vector integer type. Both CST and TYPE must be scalars, or vectors
1529 of the same number of elements. If the value won't fit in the floating point
1530 type, the results are undefined.
15251531
15261532
sitofp ( CST to TYPE )
15271533
Convert a signed integer constant to the corresponding floating point
1528 constant. TYPE must be floating point. CST must be of integer type. If the
1529 value won't fit in the floating point type, the results are undefined.
1534 constant. TYPE must be a scalar or vector floating point type. CST must be of
1535 scalar or vector integer type. Both CST and TYPE must be scalars, or vectors
1536 of the same number of elements. If the value won't fit in the floating point
1537 type, the results are undefined.
15301538
15311539
ptrtoint ( CST to TYPE )
15321540
Convert a pointer typed constant to the corresponding integer constant
31353143
31363144
Arguments:
31373145

The 'fptoui' instruction takes a value to cast, which must be a

3138 floating point value, and a type to cast it to, which
3139 must be an integer type.

3146 scalar or vector floating point value, and a type
3147 to cast it to ty2, which must be an integer
3148 type. If ty is a vector floating point type, ty2 must be a
3149 vector integer type with the same number of elements as ty

31403150
31413151
Semantics:
31423152

The 'fptoui' instruction converts its

31683178 floating point value to type ty2.
31693179

31703180
3171
31723181
Arguments:
31733182

The 'fptosi' instruction takes a value to cast, which must be a

3174 floating point value, and a type to cast it to, which
3175 must also be an integer type.

3183 scalar or vector floating point value, and a type
3184 to cast it to ty2, which must be an integer
3185 type. If ty is a vector floating point type, ty2 must be a
3186 vector integer type with the same number of elements as ty

31763187
31773188
Semantics:
31783189

The 'fptosi' instruction converts its

32033214

The 'uitofp' instruction regards value as an unsigned

32043215 integer and converts that value to the ty2 type.

32053216
3206
3207
Arguments:
3208

The 'uitofp' instruction takes a value to cast, which must be an

3209 integer value, and a type to cast it to, which must
3210 be a floating point type.>
3217
Arguments:>
3218

The 'uitofp' instruction takes a value to cast, which must be a

3219 scalar or vector integer value, and a type to cast it
3220 to ty2, which must be an floating point
3221 type. If ty is a vector integer type, ty2 must be a vector
3222 floating point type with the same number of elements as ty

32113223
32123224
Semantics:
32133225

The 'uitofp' instruction interprets its operand as an unsigned

32143226 integer quantity and converts it to the corresponding floating point value. If
32153227 the value cannot fit in the floating point value, the results are undefined.

32163228
3217
32183229
Example:
32193230

                  
                
32203231 %X = uitofp i32 257 to float ; yields float:257.0
32383249 integer and converts that value to the ty2 type.

32393250
32403251
Arguments:
3241

The 'sitofp' instruction takes a value to cast, which must be an

3242 integer value, and a type to cast it to, which must be
3243 a floating point type.

3252

The 'sitofp' instruction takes a value to cast, which must be a

3253 scalar or vector integer value, and a type to cast it
3254 to ty2, which must be an floating point
3255 type. If ty is a vector integer type, ty2 must be a vector
3256 floating point type with the same number of elements as ty

32443257
32453258
Semantics:
32463259

The 'sitofp' instruction interprets its operand as a signed

63646364 case ISD::FABS:
63656365 case ISD::FSQRT:
63666366 case ISD::FSIN:
6367 case ISD::FCOS: {
6367 case ISD::FCOS:
6368 case ISD::FP_TO_SINT:
6369 case ISD::FP_TO_UINT:
6370 case ISD::SINT_TO_FP:
6371 case ISD::UINT_TO_FP: {
63686372 SDOperand L, H;
63696373 SplitVectorOp(Node->getOperand(0), L, H);
63706374
29662966 (memopv2i64 addr:$src2))),
29672967 (PANDNrm VR128:$src1, addr:$src2)>, Requires<[HasSSE2]>;
29682968
2969 // vector -> vector casts
2970 def : Pat<(v4f32 (sint_to_fp (v4i32 VR128:$src))),
2971 (Int_CVTDQ2PSrr VR128:$src)>, Requires<[HasSSE2]>;
2972 def : Pat<(v4i32 (fp_to_sint (v4f32 VR128:$src))),
2973 (Int_CVTTPS2DQrr VR128:$src)>, Requires<[HasSSE2]>;
2974
29692975 // Use movaps / movups for SSE integer load / store (one byte shorter).
29702976 def : Pat<(alignedloadv4i32 addr:$src),
29712977 (MOVAPSrm addr:$src)>, Requires<[HasSSE1]>;
201201 APInt Val(DestBitWidth, 2, x);
202202 return ConstantInt::get(Val);
203203 }
204 if (const ConstantVector *CV = dyn_cast(V)) {
205 std::vector res;
206 const VectorType *DestVecTy = cast(DestTy);
207 const Type *DstEltTy = DestVecTy->getElementType();
208 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
209 res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i),
210 DstEltTy));
211 return ConstantVector::get(DestVecTy, res);
212 }
204213 return 0; // Can't fold.
205214 case Instruction::IntToPtr: //always treated as unsigned
206215 if (V->isNullValue()) // Is it an integral null value?
222231 opc==Instruction::SIToFP,
223232 APFloat::rmNearestTiesToEven);
224233 return ConstantFP::get(DestTy, apf);
234 }
235 if (const ConstantVector *CV = dyn_cast(V)) {
236 std::vector res;
237 const VectorType *DestVecTy = cast(DestTy);
238 const Type *DstEltTy = DestVecTy->getElementType();
239 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
240 res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i),
241 DstEltTy));
242 return ConstantVector::get(DestVecTy, res);
225243 }
226244 return 0;
227245 case Instruction::ZExt:
16421642 }
16431643
16441644 Constant *ConstantExpr::getUIToFP(Constant *C, const Type *Ty) {
1645 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
1646 "This is an illegal i32 to floating point cast!");
1645 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1646 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1647 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1648 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
1649 "This is an illegal uint to floating point cast!");
16471650 return getFoldedCast(Instruction::UIToFP, C, Ty);
16481651 }
16491652
16501653 Constant *ConstantExpr::getSIToFP(Constant *C, const Type *Ty) {
1651 assert(C->getType()->isInteger() && Ty->isFloatingPoint() &&
1654 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1655 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1656 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1657 assert(C->getType()->isIntOrIntVector() && Ty->isFPOrFPVector() &&
16521658 "This is an illegal sint to floating point cast!");
16531659 return getFoldedCast(Instruction::SIToFP, C, Ty);
16541660 }
16551661
16561662 Constant *ConstantExpr::getFPToUI(Constant *C, const Type *Ty) {
1657 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
1658 "This is an illegal floating point to i32 cast!");
1663 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1664 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1665 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1666 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1667 "This is an illegal floating point to uint cast!");
16591668 return getFoldedCast(Instruction::FPToUI, C, Ty);
16601669 }
16611670
16621671 Constant *ConstantExpr::getFPToSI(Constant *C, const Type *Ty) {
1663 assert(C->getType()->isFloatingPoint() && Ty->isInteger() &&
1664 "This is an illegal floating point to i32 cast!");
1672 bool fromVec = C->getType()->getTypeID() == Type::VectorTyID;
1673 bool toVec = Ty->getTypeID() == Type::VectorTyID;
1674 assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
1675 assert(C->getType()->isFPOrFPVector() && Ty->isIntOrIntVector() &&
1676 "This is an illegal floating point to sint cast!");
16651677 return getFoldedCast(Instruction::FPToSI, C, Ty);
16661678 }
16671679
19111911 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
19121912 SrcBitSize < DstBitSize;
19131913 case Instruction::UIToFP:
1914 return SrcTy->isInteger() && DstTy->isFloatingPoint();
19151914 case Instruction::SIToFP:
1915 if (const VectorType *SVTy = dyn_cast(SrcTy)) {
1916 if (const VectorType *DVTy = dyn_cast(DstTy)) {
1917 return SVTy->getElementType()->isInteger() &&
1918 DVTy->getElementType()->isFloatingPoint() &&
1919 SVTy->getNumElements() == DVTy->getNumElements();
1920 }
1921 }
19161922 return SrcTy->isInteger() && DstTy->isFloatingPoint();
19171923 case Instruction::FPToUI:
1918 return SrcTy->isFloatingPoint() && DstTy->isInteger();
19191924 case Instruction::FPToSI:
1925 if (const VectorType *SVTy = dyn_cast(SrcTy)) {
1926 if (const VectorType *DVTy = dyn_cast(DstTy)) {
1927 return SVTy->getElementType()->isFloatingPoint() &&
1928 DVTy->getElementType()->isInteger() &&
1929 SVTy->getNumElements() == DVTy->getNumElements();
1930 }
1931 }
19201932 return SrcTy->isFloatingPoint() && DstTy->isInteger();
19211933 case Instruction::PtrToInt:
19221934 return isa(SrcTy) && DstTy->isInteger();
708708 const Type *SrcTy = I.getOperand(0)->getType();
709709 const Type *DestTy = I.getType();
710710
711 Assert1(SrcTy->isInteger(),"UInt2FP source must be integral", &I);
712 Assert1(DestTy->isFloatingPoint(),"UInt2FP result must be FP", &I);
711 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
712 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
713
714 Assert1(SrcVec == DstVec,"UIToFP source and dest must both be vector or scalar", &I);
715 Assert1(SrcTy->isIntOrIntVector(),"UIToFP source must be integer or integer vector", &I);
716 Assert1(DestTy->isFPOrFPVector(),"UIToFP result must be FP or FP vector", &I);
717
718 if (SrcVec && DstVec)
719 Assert1(cast(SrcTy)->getNumElements() == cast(DestTy)->getNumElements(),
720 "UIToFP source and dest vector length mismatch", &I);
713721
714722 visitInstruction(I);
715723 }
719727 const Type *SrcTy = I.getOperand(0)->getType();
720728 const Type *DestTy = I.getType();
721729
722 Assert1(SrcTy->isInteger(),"SInt2FP source must be integral", &I);
723 Assert1(DestTy->isFloatingPoint(),"SInt2FP result must be FP", &I);
730 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
731 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
732
733 Assert1(SrcVec == DstVec,"SIToFP source and dest must both be vector or scalar", &I);
734 Assert1(SrcTy->isIntOrIntVector(),"SIToFP source must be integer or integer vector", &I);
735 Assert1(DestTy->isFPOrFPVector(),"SIToFP result must be FP or FP vector", &I);
736
737 if (SrcVec && DstVec)
738 Assert1(cast(SrcTy)->getNumElements() == cast(DestTy)->getNumElements(),
739 "SIToFP source and dest vector length mismatch", &I);
724740
725741 visitInstruction(I);
726742 }
730746 const Type *SrcTy = I.getOperand(0)->getType();
731747 const Type *DestTy = I.getType();
732748
733 Assert1(SrcTy->isFloatingPoint(),"FP2UInt source must be FP", &I);
734 Assert1(DestTy->isInteger(),"FP2UInt result must be integral", &I);
749 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
750 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
751
752 Assert1(SrcVec == DstVec,"FPToUI source and dest must both be vector or scalar", &I);
753 Assert1(SrcTy->isFPOrFPVector(),"FPToUI source must be FP or FP vector", &I);
754 Assert1(DestTy->isIntOrIntVector(),"FPToUI result must be integer or integer vector", &I);
755
756 if (SrcVec && DstVec)
757 Assert1(cast(SrcTy)->getNumElements() == cast(DestTy)->getNumElements(),
758 "FPToUI source and dest vector length mismatch", &I);
735759
736760 visitInstruction(I);
737761 }
741765 const Type *SrcTy = I.getOperand(0)->getType();
742766 const Type *DestTy = I.getType();
743767
744 Assert1(SrcTy->isFloatingPoint(),"FPToSI source must be FP", &I);
745 Assert1(DestTy->isInteger(),"FP2ToI result must be integral", &I);
768 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
769 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
770
771 Assert1(SrcVec == DstVec,"FPToSI source and dest must both be vector or scalar", &I);
772 Assert1(SrcTy->isFPOrFPVector(),"FPToSI source must be FP or FP vector", &I);
773 Assert1(DestTy->isIntOrIntVector(),"FPToSI result must be integer or integer vector", &I);
774
775 if (SrcVec && DstVec)
776 Assert1(cast(SrcTy)->getNumElements() == cast(DestTy)->getNumElements(),
777 "FPToSI source and dest vector length mismatch", &I);
746778
747779 visitInstruction(I);
748780 }
1414 %k = bitcast i32 %a to float
1515 %l = inttoptr i16 %x to i32*
1616 %m = ptrtoint i32* %l to i64
17 %n = insertelement <4 x i32> undef, i32 %a, i32 0
18 %o = sitofp <4 x i32> %n to <4 x float>
19 %p = uitofp <4 x i32> %n to <4 x float>
20 %q = fptosi <4 x float> %p to <4 x i32>
21 %r = fptoui <4 x float> %p to <4 x i32>
1722 ret void
1823 }
1924