Index: lib/Target/PowerPC/PPCISelLowering.cpp =================================================================== --- lib/Target/PowerPC/PPCISelLowering.cpp +++ lib/Target/PowerPC/PPCISelLowering.cpp @@ -6924,6 +6924,11 @@ SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG, const SDLoc &dl) const { + + // It is legal to truncate and convert f128 to integer types. + if (EnableQuadPrecision && (Op->getOperand(0).getValueType() == MVT::f128)) + return Op; + // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on // PPC (the libcall is not available). if (Op.getOperand(0).getValueType() == MVT::ppcf128) { Index: lib/Target/PowerPC/PPCInstrVSX.td =================================================================== --- lib/Target/PowerPC/PPCInstrVSX.td +++ lib/Target/PowerPC/PPCInstrVSX.td @@ -2506,8 +2506,12 @@ // Truncate & Convert QP -> (Un)Signed (D)Word (dword[1] is set to zero) def XSCVQPSDZ : X_VT5_XO5_VB5<63, 25, 836, "xscvqpsdz", []>; + def : Pat<(i64 (fp_to_sint f128:$src)), + (i64 (MFVRD (XSCVQPSDZ $src)))>; def XSCVQPSWZ : X_VT5_XO5_VB5<63, 9, 836, "xscvqpswz", []>; def XSCVQPUDZ : X_VT5_XO5_VB5<63, 17, 836, "xscvqpudz", []>; + def : Pat<(i64 (fp_to_uint f128:$src)), + (i64 (MFVRD (XSCVQPUDZ $src)))>; def XSCVQPUWZ : X_VT5_XO5_VB5<63, 1, 836, "xscvqpuwz", []>; // Convert (Un)Signed DWord -> QP @@ -3150,6 +3154,12 @@ def : Pat<(f128 (uint_to_fp ScalarLoads.ZELi8)), (f128 (XSCVUDQP (LXSIBZX xoaddr:$src)))>; + // Truncate & Convert QP -> (Un)Signed DWord in memory + def : Pat<(store (i64 (fp_to_sint f128:$src)), xoaddr:$dst), + (STXSD (COPY_TO_REGCLASS (XSCVQPSDZ $src), VFRC), xoaddr:$dst)>; + def : Pat<(store (i64 (fp_to_uint f128:$src)), xoaddr:$dst), + (STXSD (COPY_TO_REGCLASS (XSCVQPUDZ $src), VFRC), xoaddr:$dst)>; + } // end HasP9Vector, AddedComplexity let Predicates = [HasP9Vector] in { Index: test/CodeGen/PowerPC/f128-truncateNconv.ll =================================================================== --- /dev/null +++ test/CodeGen/PowerPC/f128-truncateNconv.ll @@ -0,0 +1,158 @@ +; RUN: llc -mcpu=pwr9 -mtriple=powerpc64le-unknown-unknown \ +; RUN: -enable-ppc-quad-precision -ppc-vsr-nums-as-vr < %s | FileCheck %s + +@f128Array = global [4 x fp128] [fp128 0xL00000000000000004004C00000000000, + fp128 0xLF000000000000000400808AB851EB851, + fp128 0xL5000000000000000400E0C26324C8366, + fp128 0xL8000000000000000400A24E2E147AE14], + align 16 + +; Function Attrs: norecurse nounwind readonly +define i64 @qpConv2sdw(fp128* nocapture readonly %a) { +entry: + %0 = load fp128, fp128* %a, align 16 + %conv = fptosi fp128 %0 to i64 + ret i64 %conv + +; CHECK-LABEL: qpConv2sdw +; CHECK: lxv [[REG:[0-9]+]], 0(3) +; CHECK-NEXT: xscvqpsdz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: mfvsrd 3, [[CONV]] +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind +define void @qpConv2sdw_02(i64* nocapture %res) local_unnamed_addr #1 { +entry: + %0 = load fp128, fp128* getelementptr inbounds + ([4 x fp128], [4 x fp128]* @f128Array, i64 0, + i64 2), align 16 + %conv = fptosi fp128 %0 to i64 + store i64 %conv, i64* %res, align 8 + ret void + +; CHECK-LABEL: qpConv2sdw_02 +; CHECK: addis [[REG0:[0-9]+]], 2, .LC0@toc@ha +; CHECK: ld [[REG0]], .LC0@toc@l([[REG0]]) +; CHECK: lxv [[REG:[0-9]+]], 32([[REG0]]) +; CHECK-NEXT: xscvqpsdz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: stxsd [[CONV]], 0(3) +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind readonly +define i64 @qpConv2sdw_03(fp128* nocapture readonly %a) { +entry: + %0 = load fp128, fp128* %a, align 16 + %1 = load fp128, fp128* getelementptr inbounds + ([4 x fp128], [4 x fp128]* @f128Array, i64 0, + i64 1), align 16 + %add = fadd fp128 %0, %1 + %conv = fptosi fp128 %add to i64 + ret i64 %conv + +; CHECK-LABEL: qpConv2sdw_03 +; CHECK: addis [[REG0:[0-9]+]], 2, .LC0@toc@ha +; CHECK-DAG: ld [[REG0]], .LC0@toc@l([[REG0]]) +; CHECK-DAG: lxv [[REG1:[0-9]+]], 16([[REG0]]) +; CHECK-DAG: lxv [[REG:[0-9]+]], 0(3) +; CHECK: xsaddqp [[REG]], [[REG]], [[REG1]] +; CHECK-NEXT: xscvqpsdz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: mfvsrd 3, [[CONV]] +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind +define void @qpConv2sdw_04(fp128* nocapture readonly %a, + fp128* nocapture readonly %b, i64* nocapture %res) { +entry: + %0 = load fp128, fp128* %a, align 16 + %1 = load fp128, fp128* %b, align 16 + %add = fadd fp128 %0, %1 + %conv = fptosi fp128 %add to i64 + store i64 %conv, i64* %res, align 8 + ret void + +; CHECK-LABEL: qpConv2sdw_04 +; CHECK-DAG: lxv [[REG1:[0-9]+]], 0(4) +; CHECK-DAG: lxv [[REG:[0-9]+]], 0(3) +; CHECK: xsaddqp [[REG]], [[REG]], [[REG1]] +; CHECK-NEXT: xscvqpsdz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: stxsd [[CONV]], 0(5) +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind readonly +define i64 @qpConv2udw(fp128* nocapture readonly %a) { +entry: + %0 = load fp128, fp128* %a, align 16 + %conv = fptoui fp128 %0 to i64 + ret i64 %conv + +; CHECK-LABEL: qpConv2udw +; CHECK: lxv [[REG:[0-9]+]], 0(3) +; CHECK-NEXT: xscvqpudz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: mfvsrd 3, [[CONV]] +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind +define void @qpConv2udw_02(i64* nocapture %res) { +entry: + %0 = load fp128, fp128* getelementptr inbounds + ([4 x fp128], [4 x fp128]* @f128Array, i64 0, + i64 2), align 16 + %conv = fptoui fp128 %0 to i64 + store i64 %conv, i64* %res, align 8 + ret void + +; CHECK-LABEL: qpConv2udw_02 +; CHECK: addis [[REG0:[0-9]+]], 2, .LC0@toc@ha +; CHECK: ld [[REG0]], .LC0@toc@l([[REG0]]) +; CHECK: lxv [[REG:[0-9]+]], 32([[REG0]]) +; CHECK-NEXT: xscvqpudz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: stxsd [[CONV]], 0(3) +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind readonly +define i64 @qpConv2udw_03(fp128* nocapture readonly %a) { +entry: + %0 = load fp128, fp128* %a, align 16 + %1 = load fp128, fp128* getelementptr inbounds + ([4 x fp128], [4 x fp128]* @f128Array, i64 0, + i64 1), align 16 + %add = fadd fp128 %0, %1 + %conv = fptoui fp128 %add to i64 + ret i64 %conv + +; CHECK-LABEL: qpConv2udw_03 +; CHECK: addis [[REG0:[0-9]+]], 2, .LC0@toc@ha +; CHECK-DAG: ld [[REG0]], .LC0@toc@l([[REG0]]) +; CHECK-DAG: lxv [[REG1:[0-9]+]], 16([[REG0]]) +; CHECK-DAG: lxv [[REG:[0-9]+]], 0(3) +; CHECK: xsaddqp [[REG]], [[REG]], [[REG1]] +; CHECK-NEXT: xscvqpudz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: mfvsrd 3, [[CONV]] +; CHECK-NEXT: blr +} + +; Function Attrs: norecurse nounwind +define void @qpConv2udw_04(fp128* nocapture readonly %a, + fp128* nocapture readonly %b, i64* nocapture %res) { +entry: + %0 = load fp128, fp128* %a, align 16 + %1 = load fp128, fp128* %b, align 16 + %add = fadd fp128 %0, %1 + %conv = fptoui fp128 %add to i64 + store i64 %conv, i64* %res, align 8 + ret void + +; CHECK-LABEL: qpConv2udw_04 +; CHECK-DAG: lxv [[REG1:[0-9]+]], 0(4) +; CHECK-DAG: lxv [[REG:[0-9]+]], 0(3) +; CHECK: xsaddqp [[REG]], [[REG]], [[REG1]] +; CHECK-NEXT: xscvqpudz [[CONV:[0-9]+]], [[REG]] +; CHECK-NEXT: stxsd [[CONV]], 0(5) +; CHECK-NEXT: blr +}