[llvm] [SystemZ] Don't scalarize truncating vector fp to uint conversions. (PR #205311)
Jonas Paulsson via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 23 03:18:15 PDT 2026
https://github.com/JonPsson1 created https://github.com/llvm/llvm-project/pull/205311
It should be better to use vector fp to uint conversions followed by vperm
(and vpdi) than scalarization.
This however only affected three benchmarks (15 files), so it may not be worth having. However, imagick actually showed an improvement (2.5%), but this benchmark has been "volatile" in the past, so I would like to try this again after rebasing on top of the new scheduling strategy to see what it looks like then.
This was discovered when investigating cost functions with LoopVectorizer / LoopUnroller.
>From 31252f3de5558308df815a57da7c142740aea717 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Sun, 21 Jun 2026 12:24:36 +0200
Subject: [PATCH 1/2] DAGCombine for vector fp_to_uint
---
.../Target/SystemZ/SystemZISelLowering.cpp | 47 ++
llvm/lib/Target/SystemZ/SystemZISelLowering.h | 1 +
llvm/test/CodeGen/SystemZ/vec-conv-04.ll | 437 ++++++++++++++++++
3 files changed, 485 insertions(+)
create mode 100644 llvm/test/CodeGen/SystemZ/vec-conv-04.ll
diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
index e56a449cd455d..1f805812b32cf 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
@@ -787,6 +787,7 @@ SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
ISD::FP_EXTEND,
ISD::SINT_TO_FP,
ISD::UINT_TO_FP,
+ ISD::FP_TO_UINT,
ISD::STRICT_FP_EXTEND,
ISD::FCOPYSIGN,
ISD::BSWAP,
@@ -8586,6 +8587,51 @@ SDValue SystemZTargetLowering::combineINT_TO_FP(
return SDValue();
}
+SDValue SystemZTargetLowering::combineFP_TO_UINT(
+ SDNode *N, DAGCombinerInfo &DCI) const {
+ if (DCI.Level != BeforeLegalizeTypes)
+ return SDValue();
+ SelectionDAG &DAG = DCI.DAG;
+ LLVMContext &Ctx = *DAG.getContext();
+ SDLoc DL(N);
+ EVT OutVT = N->getValueType(0);
+ Type *OutLLVMTy = OutVT.getTypeForEVT(Ctx);
+ SDValue Op = N->getOperand(0);
+ unsigned OutScalarBits = OutVT.getScalarSizeInBits();
+ unsigned InScalarBits = Op->getValueType(0).getScalarSizeInBits();
+ // Do a vector conversion followed by a permute to avoid expansion into
+ // scalar conversions.
+ if (OutLLVMTy->isVectorTy() && OutScalarBits < InScalarBits &&
+ InScalarBits <= 64 && isPowerOf2_32(InScalarBits) &&
+ isPowerOf2_32(OutScalarBits)) {
+ // Vector conversion to same bitwidth.
+ unsigned NumElts = cast<FixedVectorType>(OutLLVMTy)->getNumElements();
+ EVT ConvVT = EVT::getVectorVT(Ctx, EVT::getIntegerVT(Ctx, InScalarBits),
+ NumElts);
+ SDValue ConvOp = DAG.getNode(ISD::FP_TO_UINT, DL, ConvVT, Op);
+
+ // Bitcast to narrow elements vector type.
+ unsigned NumTruncElts = ConvVT.getSizeInBits() / OutScalarBits;
+ EVT TruncEltVT = OutVT.getVectorElementType();
+ EVT TruncVecVT = EVT::getVectorVT(Ctx, TruncEltVT, NumTruncElts);
+ SDValue BCast = DAG.getBitcast(TruncVecVT, ConvOp);
+
+ // Build the result vector.
+ unsigned EltFac = InScalarBits / OutScalarBits;
+ SmallVector<SDValue, SystemZ::VectorBytes> Ops;
+ unsigned I = 0;
+ for (; I < NumElts ; ++I)
+ Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, TruncEltVT,
+ BCast, DAG.getConstant(I * EltFac, DL, MVT::i32)));
+ for (; I < NumTruncElts; ++I)
+ Ops.push_back(DAG.getUNDEF(TruncEltVT));
+ SDValue BV = DAG.getBuildVector(TruncVecVT, DL, Ops);
+ return DAG.getExtractSubvector(DL, OutVT, BV, 0);
+ }
+
+ return SDValue();
+}
+
SDValue SystemZTargetLowering::combineFCOPYSIGN(
SDNode *N, DAGCombinerInfo &DCI) const {
SelectionDAG &DAG = DCI.DAG;
@@ -9424,6 +9470,7 @@ SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
case ISD::FP_EXTEND: return combineFP_EXTEND(N, DCI);
case ISD::SINT_TO_FP:
case ISD::UINT_TO_FP: return combineINT_TO_FP(N, DCI);
+ case ISD::FP_TO_UINT: return combineFP_TO_UINT(N, DCI);
case ISD::FCOPYSIGN: return combineFCOPYSIGN(N, DCI);
case ISD::BSWAP: return combineBSWAP(N, DCI);
case ISD::SETCC: return combineSETCC(N, DCI);
diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.h b/llvm/lib/Target/SystemZ/SystemZISelLowering.h
index 7facd3a27d97c..5f092bb8a1204 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.h
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.h
@@ -416,6 +416,7 @@ class SystemZTargetLowering : public TargetLowering {
SDValue combineFP_ROUND(SDNode *N, DAGCombinerInfo &DCI) const;
SDValue combineFP_EXTEND(SDNode *N, DAGCombinerInfo &DCI) const;
SDValue combineINT_TO_FP(SDNode *N, DAGCombinerInfo &DCI) const;
+ SDValue combineFP_TO_UINT(SDNode *N, DAGCombinerInfo &DCI) const;
SDValue combineFCOPYSIGN(SDNode *N, DAGCombinerInfo &DCI) const;
SDValue combineBSWAP(SDNode *N, DAGCombinerInfo &DCI) const;
SDValue combineSETCC(SDNode *N, DAGCombinerInfo &DCI) const;
diff --git a/llvm/test/CodeGen/SystemZ/vec-conv-04.ll b/llvm/test/CodeGen/SystemZ/vec-conv-04.ll
new file mode 100644
index 0000000000000..ee246fa97b4f8
--- /dev/null
+++ b/llvm/test/CodeGen/SystemZ/vec-conv-04.ll
@@ -0,0 +1,437 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; Test truncating vector conversions from fp to unsigned.
+;
+; RUN: llc < %s -mtriple=s390x-linux-gnu -mcpu=z17 | FileCheck %s
+
+define <1 x i8> @fun0(<1 x float> %V) {
+; CHECK-LABEL: fun0:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v24, %v24, 0, 5
+; CHECK-NEXT: br %r14
+ %Res = fptoui <1 x float> %V to <1 x i8>
+ ret <1 x i8> %Res
+}
+
+define <2 x i8> @fun1(<2 x float> %V) {
+; CHECK-LABEL: fun1:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v24, 0, 5
+; CHECK-NEXT: vrepih %v1, 4
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <2 x float> %V to <2 x i8>
+ ret <2 x i8> %Res
+}
+
+define <4 x i8> @fun2(<4 x float> %V) {
+; CHECK-LABEL: fun2:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI2_0
+; CHECK-NEXT: vl %v1, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <4 x float> %V to <4 x i8>
+ ret <4 x i8> %Res
+}
+
+define <8 x i8> @fun3(<8 x float> %V) {
+; CHECK-LABEL: fun3:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v26, 0, 5
+; CHECK-NEXT: vclfeb %v1, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI3_0
+; CHECK-NEXT: vl %v2, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v1, %v0, %v2
+; CHECK-NEXT: br %r14
+ %Res = fptoui <8 x float> %V to <8 x i8>
+ ret <8 x i8> %Res
+}
+
+define <16 x i8> @fun4(<16 x float> %V) {
+; CHECK-LABEL: fun4:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v30, 0, 5
+; CHECK-NEXT: vclfeb %v1, %v28, 0, 5
+; CHECK-NEXT: vclfeb %v2, %v26, 0, 5
+; CHECK-NEXT: vclfeb %v3, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI4_0
+; CHECK-NEXT: larl %r2, .LCPI4_1
+; CHECK-NEXT: vl %v4, 0(%r1), 3
+; CHECK-NEXT: vl %v5, 0(%r2), 3
+; CHECK-NEXT: vperm %v0, %v1, %v0, %v4
+; CHECK-NEXT: vperm %v1, %v3, %v2, %v5
+; CHECK-NEXT: vpdi %v24, %v1, %v0, 1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <16 x float> %V to <16 x i8>
+ ret <16 x i8> %Res
+}
+
+define <32 x i8> @fun5(<32 x float> %V) {
+; CHECK-LABEL: fun5:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v6, %v30, 0, 5
+; CHECK-NEXT: vclfeb %v7, %v28, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI5_0
+; CHECK-NEXT: vl %v16, 0(%r1), 3
+; CHECK-NEXT: vclfeb %v0, %v27, 0, 5
+; CHECK-NEXT: vclfeb %v1, %v25, 0, 5
+; CHECK-NEXT: vclfeb %v2, %v31, 0, 5
+; CHECK-NEXT: vclfeb %v3, %v29, 0, 5
+; CHECK-NEXT: vclfeb %v4, %v26, 0, 5
+; CHECK-NEXT: vclfeb %v5, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI5_1
+; CHECK-NEXT: vperm %v6, %v7, %v6, %v16
+; CHECK-NEXT: vl %v7, 0(%r1), 3
+; CHECK-NEXT: vperm %v4, %v5, %v4, %v7
+; CHECK-NEXT: vperm %v2, %v3, %v2, %v16
+; CHECK-NEXT: vperm %v0, %v1, %v0, %v7
+; CHECK-NEXT: vpdi %v24, %v4, %v6, 1
+; CHECK-NEXT: vpdi %v26, %v0, %v2, 1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <32 x float> %V to <32 x i8>
+ ret <32 x i8> %Res
+}
+
+define <1 x i16> @fun6(<1 x float> %V) {
+; CHECK-LABEL: fun6:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v24, %v24, 0, 5
+; CHECK-NEXT: br %r14
+ %Res = fptoui <1 x float> %V to <1 x i16>
+ ret <1 x i16> %Res
+}
+
+define <2 x i16> @fun7(<2 x float> %V) {
+; CHECK-LABEL: fun7:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI7_0
+; CHECK-NEXT: vl %v1, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <2 x float> %V to <2 x i16>
+ ret <2 x i16> %Res
+}
+
+define <4 x i16> @fun8(<4 x float> %V) {
+; CHECK-LABEL: fun8:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI8_0
+; CHECK-NEXT: vl %v1, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <4 x float> %V to <4 x i16>
+ ret <4 x i16> %Res
+}
+
+define <8 x i16> @fun9(<8 x float> %V) {
+; CHECK-LABEL: fun9:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v1, %v26, 0, 5
+; CHECK-NEXT: vclfeb %v2, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI9_0
+; CHECK-NEXT: vl %v0, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v2, %v1, %v0
+; CHECK-NEXT: br %r14
+ %Res = fptoui <8 x float> %V to <8 x i16>
+ ret <8 x i16> %Res
+}
+
+define <16 x i16> @fun10(<16 x float> %V) {
+; CHECK-LABEL: fun10:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclfeb %v0, %v30, 0, 5
+; CHECK-NEXT: vclfeb %v1, %v28, 0, 5
+; CHECK-NEXT: vclfeb %v2, %v26, 0, 5
+; CHECK-NEXT: vclfeb %v3, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI10_0
+; CHECK-NEXT: vl %v4, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v3, %v2, %v4
+; CHECK-NEXT: vperm %v26, %v1, %v0, %v4
+; CHECK-NEXT: br %r14
+ %Res = fptoui <16 x float> %V to <16 x i16>
+ ret <16 x i16> %Res
+}
+
+define <1 x i8> @fun11(<1 x double> %V) {
+; CHECK-LABEL: fun11:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v24, %v24, 0, 5
+; CHECK-NEXT: br %r14
+ %Res = fptoui <1 x double> %V to <1 x i8>
+ ret <1 x i8> %Res
+}
+
+define <2 x i8> @fun12(<2 x double> %V) {
+; CHECK-LABEL: fun12:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v24, 0, 5
+; CHECK-NEXT: vrepih %v1, 8
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <2 x double> %V to <2 x i8>
+ ret <2 x i8> %Res
+}
+
+define <4 x i8> @fun13(<4 x double> %V) {
+; CHECK-LABEL: fun13:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v1, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI13_0
+; CHECK-NEXT: vl %v2, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v1, %v0, %v2
+; CHECK-NEXT: br %r14
+ %Res = fptoui <4 x double> %V to <4 x i8>
+ ret <4 x i8> %Res
+}
+
+define <16 x i8> @fun15(<16 x double> %V) {
+; CHECK-LABEL: fun15:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v31, 0, 5
+; CHECK-NEXT: vclgdb %v1, %v29, 0, 5
+; CHECK-NEXT: vclgdb %v2, %v27, 0, 5
+; CHECK-NEXT: vclgdb %v3, %v25, 0, 5
+; CHECK-NEXT: vclgdb %v4, %v30, 0, 5
+; CHECK-NEXT: vclgdb %v5, %v28, 0, 5
+; CHECK-NEXT: vclgdb %v6, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v7, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI14_0
+; CHECK-NEXT: larl %r2, .LCPI14_1
+; CHECK-NEXT: larl %r3, .LCPI14_2
+; CHECK-NEXT: larl %r4, .LCPI14_3
+; CHECK-NEXT: vl %v16, 0(%r1), 3
+; CHECK-NEXT: vl %v17, 0(%r2), 3
+; CHECK-NEXT: vl %v18, 0(%r3), 3
+; CHECK-NEXT: vl %v19, 0(%r4), 3
+; CHECK-NEXT: larl %r1, .LCPI14_4
+; CHECK-NEXT: vperm %v0, %v1, %v0, %v16
+; CHECK-NEXT: vperm %v1, %v3, %v2, %v17
+; CHECK-NEXT: vperm %v2, %v5, %v4, %v18
+; CHECK-NEXT: vperm %v3, %v7, %v6, %v19
+; CHECK-NEXT: vl %v4, 0(%r1), 3
+; CHECK-NEXT: vmrlg %v0, %v1, %v0
+; CHECK-NEXT: vmrhg %v1, %v3, %v2
+; CHECK-NEXT: vperm %v24, %v1, %v0, %v4
+; CHECK-NEXT: br %r14
+ %Res = fptoui <16 x double> %V to <16 x i8>
+ ret <16 x i8> %Res
+}
+
+define <32 x i8> @fun16(<32 x double> %V) {
+; CHECK-LABEL: fun16:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v22, %v31, 0, 5
+; CHECK-NEXT: vclgdb %v23, %v29, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI15_0
+; CHECK-NEXT: vclgdb %v21, %v25, 0, 5
+; CHECK-NEXT: vl %v25, 0(%r1), 3
+; CHECK-NEXT: vclgdb %v20, %v27, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI15_1
+; CHECK-NEXT: vl %v0, 256(%r15), 3
+; CHECK-NEXT: vl %v1, 272(%r15), 3
+; CHECK-NEXT: vl %v2, 224(%r15), 3
+; CHECK-NEXT: vl %v3, 240(%r15), 3
+; CHECK-NEXT: vl %v4, 192(%r15), 3
+; CHECK-NEXT: vperm %v22, %v23, %v22, %v25
+; CHECK-NEXT: vl %v23, 0(%r1), 3
+; CHECK-NEXT: vl %v5, 208(%r15), 3
+; CHECK-NEXT: vl %v6, 160(%r15), 3
+; CHECK-NEXT: vl %v7, 176(%r15), 3
+; CHECK-NEXT: vclgdb %v18, %v30, 0, 5
+; CHECK-NEXT: vclgdb %v19, %v28, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI15_2
+; CHECK-NEXT: vclgdb %v3, %v3, 0, 5
+; CHECK-NEXT: vclgdb %v2, %v2, 0, 5
+; CHECK-NEXT: vclgdb %v1, %v1, 0, 5
+; CHECK-NEXT: vclgdb %v0, %v0, 0, 5
+; CHECK-NEXT: vperm %v20, %v21, %v20, %v23
+; CHECK-NEXT: vl %v21, 0(%r1), 3
+; CHECK-NEXT: vclgdb %v7, %v7, 0, 5
+; CHECK-NEXT: vclgdb %v6, %v6, 0, 5
+; CHECK-NEXT: vclgdb %v5, %v5, 0, 5
+; CHECK-NEXT: vclgdb %v4, %v4, 0, 5
+; CHECK-NEXT: vclgdb %v16, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v17, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI15_3
+; CHECK-NEXT: vmrlg %v20, %v20, %v22
+; CHECK-NEXT: vperm %v18, %v19, %v18, %v21
+; CHECK-NEXT: vl %v19, 0(%r1), 3
+; CHECK-NEXT: vperm %v0, %v0, %v1, %v25
+; CHECK-NEXT: larl %r1, .LCPI15_4
+; CHECK-NEXT: vperm %v1, %v2, %v3, %v23
+; CHECK-NEXT: vperm %v16, %v17, %v16, %v19
+; CHECK-NEXT: vl %v17, 0(%r1), 3
+; CHECK-NEXT: vmrlg %v0, %v1, %v0
+; CHECK-NEXT: vmrhg %v16, %v16, %v18
+; CHECK-NEXT: vperm %v1, %v4, %v5, %v21
+; CHECK-NEXT: vperm %v2, %v6, %v7, %v19
+; CHECK-NEXT: vmrhg %v1, %v2, %v1
+; CHECK-NEXT: vperm %v24, %v16, %v20, %v17
+; CHECK-NEXT: vperm %v26, %v1, %v0, %v17
+; CHECK-NEXT: br %r14
+ %Res = fptoui <32 x double> %V to <32 x i8>
+ ret <32 x i8> %Res
+}
+
+define <1 x i16> @fun17(<1 x double> %V) {
+; CHECK-LABEL: fun17:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v24, %v24, 0, 5
+; CHECK-NEXT: br %r14
+ %Res = fptoui <1 x double> %V to <1 x i16>
+ ret <1 x i16> %Res
+}
+
+define <2 x i16> @fun18(<2 x double> %V) {
+; CHECK-LABEL: fun18:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI17_0
+; CHECK-NEXT: vl %v1, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <2 x double> %V to <2 x i16>
+ ret <2 x i16> %Res
+}
+
+define <8 x i16> @fun19(<8 x double> %V) {
+; CHECK-LABEL: fun19:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v30, 0, 5
+; CHECK-NEXT: vclgdb %v1, %v28, 0, 5
+; CHECK-NEXT: vclgdb %v2, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v3, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI18_0
+; CHECK-NEXT: larl %r2, .LCPI18_1
+; CHECK-NEXT: vl %v4, 0(%r1), 3
+; CHECK-NEXT: vl %v5, 0(%r2), 3
+; CHECK-NEXT: vperm %v0, %v1, %v0, %v4
+; CHECK-NEXT: vperm %v1, %v3, %v2, %v5
+; CHECK-NEXT: vpdi %v24, %v1, %v0, 1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <8 x double> %V to <8 x i16>
+ ret <8 x i16> %Res
+}
+
+define <16 x i16> @fun20(<16 x double> %V) {
+; CHECK-LABEL: fun20:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v6, %v30, 0, 5
+; CHECK-NEXT: vclgdb %v7, %v28, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI19_0
+; CHECK-NEXT: vl %v16, 0(%r1), 3
+; CHECK-NEXT: vclgdb %v0, %v27, 0, 5
+; CHECK-NEXT: vclgdb %v1, %v25, 0, 5
+; CHECK-NEXT: vclgdb %v2, %v31, 0, 5
+; CHECK-NEXT: vclgdb %v3, %v29, 0, 5
+; CHECK-NEXT: vclgdb %v4, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v5, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI19_1
+; CHECK-NEXT: vperm %v6, %v7, %v6, %v16
+; CHECK-NEXT: vl %v7, 0(%r1), 3
+; CHECK-NEXT: vperm %v4, %v5, %v4, %v7
+; CHECK-NEXT: vperm %v2, %v3, %v2, %v16
+; CHECK-NEXT: vperm %v0, %v1, %v0, %v7
+; CHECK-NEXT: vpdi %v24, %v4, %v6, 1
+; CHECK-NEXT: vpdi %v26, %v0, %v2, 1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <16 x double> %V to <16 x i16>
+ ret <16 x i16> %Res
+}
+
+define <1 x i32> @fun21(<1 x double> %V) {
+; CHECK-LABEL: fun21:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v24, %v24, 0, 5
+; CHECK-NEXT: br %r14
+ %Res = fptoui <1 x double> %V to <1 x i32>
+ ret <1 x i32> %Res
+}
+
+define <2 x i32> @fun22(<2 x double> %V) {
+; CHECK-LABEL: fun22:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI21_0
+; CHECK-NEXT: vl %v1, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v0, %v0, %v1
+; CHECK-NEXT: br %r14
+ %Res = fptoui <2 x double> %V to <2 x i32>
+ ret <2 x i32> %Res
+}
+
+define <4 x i32> @fun23(<4 x double> %V) {
+; CHECK-LABEL: fun23:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v1, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v2, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI22_0
+; CHECK-NEXT: vl %v0, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v2, %v1, %v0
+; CHECK-NEXT: br %r14
+ %Res = fptoui <4 x double> %V to <4 x i32>
+ ret <4 x i32> %Res
+}
+
+define <8 x i32> @fun24(<8 x double> %V) {
+; CHECK-LABEL: fun24:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vclgdb %v0, %v30, 0, 5
+; CHECK-NEXT: vclgdb %v1, %v28, 0, 5
+; CHECK-NEXT: vclgdb %v2, %v26, 0, 5
+; CHECK-NEXT: vclgdb %v3, %v24, 0, 5
+; CHECK-NEXT: larl %r1, .LCPI23_0
+; CHECK-NEXT: vl %v4, 0(%r1), 3
+; CHECK-NEXT: vperm %v24, %v3, %v2, %v4
+; CHECK-NEXT: vperm %v26, %v1, %v0, %v4
+; CHECK-NEXT: br %r14
+ %Res = fptoui <8 x double> %V to <8 x i32>
+ ret <8 x i32> %Res
+}
+
+define void @fun25(ptr %Arr, ptr %Arr2) {
+; CHECK-LABEL: fun25:
+; CHECK: # %bb.0: # %entry
+; CHECK-NEXT: larl %r4, .LCPI24_0
+; CHECK-NEXT: larl %r5, .LCPI24_1
+; CHECK-NEXT: vl %v0, 0(%r4), 3
+; CHECK-NEXT: vl %v2, 0(%r5), 3
+; CHECK-NEXT: vgbm %v1, 61440
+; CHECK-NEXT: lghi %r1, -1024
+; CHECK-NEXT: .LBB24_1: # %vector.body
+; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
+; CHECK-NEXT: vl %v3, 1024(%r1,%r3)
+; CHECK-NEXT: vfmsb %v3, %v3, %v0
+; CHECK-NEXT: vclfeb %v3, %v3, 0, 5
+; CHECK-NEXT: vperm %v3, %v3, %v1, %v2
+; CHECK-NEXT: vst %v3, 1024(%r1,%r2)
+; CHECK-NEXT: la %r1, 16(%r1)
+; CHECK-NEXT: cgijlh %r1, 0, .LBB24_1
+; CHECK-NEXT: # %bb.2: # %exit
+; CHECK-NEXT: br %r14
+entry:
+ %n.vec = add i64 128, 128
+ br label %vector.body
+
+vector.body: ; preds = %vector.body, %vector.ph
+ %lsr.iv145 = phi i64 [ %lsr.iv.next146, %vector.body ], [ 0, %entry ]
+ %lsr.iv143 = phi i64 [ %lsr.iv.next144, %vector.body ], [ %n.vec, %entry ]
+ %scevgep148 = getelementptr i8, ptr %Arr, i64 %lsr.iv145
+ %scevgep147 = getelementptr i8, ptr %Arr2, i64 %lsr.iv145
+ %wide.load = load <4 x float>, ptr %scevgep147, align 4
+ %64 = fmul <4 x float> %wide.load, splat (float 2.550000e+02)
+ %65 = fptoui <4 x float> %64 to <4 x i8>
+ %interleaved.vec = shufflevector <4 x i8> %65, <4 x i8> splat (i8 -1), <16 x i32> <i32 0, i32 0, i32 0, i32 4, i32 1, i32 1, i32 1, i32 5, i32 2, i32 2, i32 2, i32 6, i32 3, i32 3, i32 3, i32 7>
+ store <16 x i8> %interleaved.vec, ptr %scevgep148, align 1
+ %lsr.iv.next144 = add i64 %lsr.iv143, -4
+ %lsr.iv.next146 = add i64 %lsr.iv145, 16
+ %66 = icmp eq i64 %lsr.iv.next144, 0
+ br i1 %66, label %exit, label %vector.body
+
+exit:
+ ret void
+}
>From 96aa493b882f7a9237480024d7a6478d492bcd16 Mon Sep 17 00:00:00 2001
From: Jonas Paulsson <paulson1 at linux.ibm.com>
Date: Sun, 21 Jun 2026 15:05:10 +0200
Subject: [PATCH 2/2] Update the TTI cost for these operations as well.
---
.../SystemZ/SystemZTargetTransformInfo.cpp | 6 +++
.../Analysis/CostModel/SystemZ/fp-cast.ll | 48 +++++++++++--------
2 files changed, 34 insertions(+), 20 deletions(-)
diff --git a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
index 8932b90f5ff02..494635124c41b 100644
--- a/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp
@@ -1067,6 +1067,12 @@ InstructionCost SystemZTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
return getBoolVecToIntConversionCost(Opcode, Dst, I) + NumDstVectors;
}
+ // These will use vector conversions followed by vector permutes.
+ if (Opcode == Instruction::FPToUI && SrcScalarBits <= 64 &&
+ DstScalarBits < SrcScalarBits &&
+ (SrcScalarBits == 64 || ST->hasVectorEnhancements2()))
+ return NumSrcVectors * 2;
+
// Return the cost of multiple scalar invocation plus the cost of
// inserting and extracting the values. Base implementation does not
// realize float->int gets scalarized.
diff --git a/llvm/test/Analysis/CostModel/SystemZ/fp-cast.ll b/llvm/test/Analysis/CostModel/SystemZ/fp-cast.ll
index 837962cb98018..46a96b849db1c 100644
--- a/llvm/test/Analysis/CostModel/SystemZ/fp-cast.ll
+++ b/llvm/test/Analysis/CostModel/SystemZ/fp-cast.ll
@@ -236,49 +236,57 @@ define void @fptoui() {
; CHECK: Cost Model: Found an estimated cost of 4 for instruction: %v14 = fptoui <2 x fp128> undef to <2 x i16>
; CHECK: Cost Model: Found an estimated cost of 4 for instruction: %v15 = fptoui <2 x fp128> undef to <2 x i8>
; CHECK: Cost Model: Found an estimated cost of 1 for instruction: %v16 = fptoui <2 x double> undef to <2 x i64>
-; CHECK: Cost Model: Found an estimated cost of 6 for instruction: %v17 = fptoui <2 x double> undef to <2 x i32>
-; CHECK: Cost Model: Found an estimated cost of 6 for instruction: %v18 = fptoui <2 x double> undef to <2 x i16>
-; CHECK: Cost Model: Found an estimated cost of 6 for instruction: %v19 = fptoui <2 x double> undef to <2 x i8>
+; CHECK: Cost Model: Found an estimated cost of 2 for instruction: %v17 = fptoui <2 x double> undef to <2 x i32>
+; CHECK: Cost Model: Found an estimated cost of 2 for instruction: %v18 = fptoui <2 x double> undef to <2 x i16>
+; CHECK: Cost Model: Found an estimated cost of 2 for instruction: %v19 = fptoui <2 x double> undef to <2 x i8>
; CHECK: Cost Model: Found an estimated cost of 5 for instruction: %v20 = fptoui <2 x float> undef to <2 x i64>
; Z13: Cost Model: Found an estimated cost of 12 for instruction: %v21 = fptoui <2 x float> undef to <2 x i32>
; Z15: Cost Model: Found an estimated cost of 1 for instruction: %v21 = fptoui <2 x float> undef to <2 x i32>
-; CHECK: Cost Model: Found an estimated cost of 6 for instruction: %v22 = fptoui <2 x float> undef to <2 x i16>
-; CHECK: Cost Model: Found an estimated cost of 6 for instruction: %v23 = fptoui <2 x float> undef to <2 x i8>
+; Z13: Cost Model: Found an estimated cost of 6 for instruction: %v22 = fptoui <2 x float> undef to <2 x i16>
+; Z15: Cost Model: Found an estimated cost of 2 for instruction: %v22 = fptoui <2 x float> undef to <2 x i16>
+; Z13: Cost Model: Found an estimated cost of 6 for instruction: %v23 = fptoui <2 x float> undef to <2 x i8>
+; Z15: Cost Model: Found an estimated cost of 2 for instruction: %v23 = fptoui <2 x float> undef to <2 x i8>
; CHECK: Cost Model: Found an estimated cost of 6 for instruction: %v24 = fptoui <4 x fp128> undef to <4 x i64>
; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v25 = fptoui <4 x fp128> undef to <4 x i32>
; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v26 = fptoui <4 x fp128> undef to <4 x i16>
; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v27 = fptoui <4 x fp128> undef to <4 x i8>
; CHECK: Cost Model: Found an estimated cost of 2 for instruction: %v28 = fptoui <4 x double> undef to <4 x i64>
-; CHECK: Cost Model: Found an estimated cost of 12 for instruction: %v29 = fptoui <4 x double> undef to <4 x i32>
-; CHECK: Cost Model: Found an estimated cost of 12 for instruction: %v30 = fptoui <4 x double> undef to <4 x i16>
-; CHECK: Cost Model: Found an estimated cost of 12 for instruction: %v31 = fptoui <4 x double> undef to <4 x i8>
+; CHECK: Cost Model: Found an estimated cost of 4 for instruction: %v29 = fptoui <4 x double> undef to <4 x i32>
+; CHECK: Cost Model: Found an estimated cost of 4 for instruction: %v30 = fptoui <4 x double> undef to <4 x i16>
+; CHECK: Cost Model: Found an estimated cost of 4 for instruction: %v31 = fptoui <4 x double> undef to <4 x i8>
; CHECK: Cost Model: Found an estimated cost of 10 for instruction: %v32 = fptoui <4 x float> undef to <4 x i64>
; Z13: Cost Model: Found an estimated cost of 12 for instruction: %v33 = fptoui <4 x float> undef to <4 x i32>
; Z15: Cost Model: Found an estimated cost of 1 for instruction: %v33 = fptoui <4 x float> undef to <4 x i32>
-; CHECK: Cost Model: Found an estimated cost of 12 for instruction: %v34 = fptoui <4 x float> undef to <4 x i16>
-; CHECK: Cost Model: Found an estimated cost of 12 for instruction: %v35 = fptoui <4 x float> undef to <4 x i8>
+; Z13: Cost Model: Found an estimated cost of 12 for instruction: %v34 = fptoui <4 x float> undef to <4 x i16>
+; Z15: Cost Model: Found an estimated cost of 2 for instruction: %v34 = fptoui <4 x float> undef to <4 x i16>
+; Z13: Cost Model: Found an estimated cost of 12 for instruction: %v35 = fptoui <4 x float> undef to <4 x i8>
+; Z15: Cost Model: Found an estimated cost of 2 for instruction: %v35 = fptoui <4 x float> undef to <4 x i8>
; CHECK: Cost Model: Found an estimated cost of 12 for instruction: %v36 = fptoui <8 x fp128> undef to <8 x i64>
; CHECK: Cost Model: Found an estimated cost of 16 for instruction: %v37 = fptoui <8 x fp128> undef to <8 x i32>
; CHECK: Cost Model: Found an estimated cost of 16 for instruction: %v38 = fptoui <8 x fp128> undef to <8 x i16>
; CHECK: Cost Model: Found an estimated cost of 16 for instruction: %v39 = fptoui <8 x fp128> undef to <8 x i8>
; CHECK: Cost Model: Found an estimated cost of 4 for instruction: %v40 = fptoui <8 x double> undef to <8 x i64>
-; CHECK: Cost Model: Found an estimated cost of 24 for instruction: %v41 = fptoui <8 x double> undef to <8 x i32>
-; CHECK: Cost Model: Found an estimated cost of 24 for instruction: %v42 = fptoui <8 x double> undef to <8 x i16>
-; CHECK: Cost Model: Found an estimated cost of 24 for instruction: %v43 = fptoui <8 x double> undef to <8 x i8>
+; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v41 = fptoui <8 x double> undef to <8 x i32>
+; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v42 = fptoui <8 x double> undef to <8 x i16>
+; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v43 = fptoui <8 x double> undef to <8 x i8>
; CHECK: Cost Model: Found an estimated cost of 20 for instruction: %v44 = fptoui <8 x float> undef to <8 x i64>
; Z13: Cost Model: Found an estimated cost of 24 for instruction: %v45 = fptoui <8 x float> undef to <8 x i32>
; Z15: Cost Model: Found an estimated cost of 2 for instruction: %v45 = fptoui <8 x float> undef to <8 x i32>
-; CHECK: Cost Model: Found an estimated cost of 24 for instruction: %v46 = fptoui <8 x float> undef to <8 x i16>
-; CHECK: Cost Model: Found an estimated cost of 24 for instruction: %v47 = fptoui <8 x float> undef to <8 x i8>
+; Z13: Cost Model: Found an estimated cost of 24 for instruction: %v46 = fptoui <8 x float> undef to <8 x i16>
+; Z15: Cost Model: Found an estimated cost of 4 for instruction: %v46 = fptoui <8 x float> undef to <8 x i16>
+; Z13: Cost Model: Found an estimated cost of 24 for instruction: %v47 = fptoui <8 x float> undef to <8 x i8>
+; Z15: Cost Model: Found an estimated cost of 4 for instruction: %v47 = fptoui <8 x float> undef to <8 x i8>
; CHECK: Cost Model: Found an estimated cost of 8 for instruction: %v48 = fptoui <16 x double> undef to <16 x i64>
-; CHECK: Cost Model: Found an estimated cost of 48 for instruction: %v49 = fptoui <16 x double> undef to <16 x i32>
-; CHECK: Cost Model: Found an estimated cost of 48 for instruction: %v50 = fptoui <16 x double> undef to <16 x i16>
-; CHECK: Cost Model: Found an estimated cost of 48 for instruction: %v51 = fptoui <16 x double> undef to <16 x i8>
+; CHECK: Cost Model: Found an estimated cost of 16 for instruction: %v49 = fptoui <16 x double> undef to <16 x i32>
+; CHECK: Cost Model: Found an estimated cost of 16 for instruction: %v50 = fptoui <16 x double> undef to <16 x i16>
+; CHECK: Cost Model: Found an estimated cost of 16 for instruction: %v51 = fptoui <16 x double> undef to <16 x i8>
; CHECK: Cost Model: Found an estimated cost of 40 for instruction: %v52 = fptoui <16 x float> undef to <16 x i64>
; Z13: Cost Model: Found an estimated cost of 48 for instruction: %v53 = fptoui <16 x float> undef to <16 x i32>
; Z15: Cost Model: Found an estimated cost of 4 for instruction: %v53 = fptoui <16 x float> undef to <16 x i32>
-; CHECK: Cost Model: Found an estimated cost of 48 for instruction: %v54 = fptoui <16 x float> undef to <16 x i16>
-; CHECK: Cost Model: Found an estimated cost of 48 for instruction: %v55 = fptoui <16 x float> undef to <16 x i8>
+; Z13: Cost Model: Found an estimated cost of 48 for instruction: %v54 = fptoui <16 x float> undef to <16 x i16>
+; Z15: Cost Model: Found an estimated cost of 8 for instruction: %v54 = fptoui <16 x float> undef to <16 x i16>
+; Z13: Cost Model: Found an estimated cost of 48 for instruction: %v55 = fptoui <16 x float> undef to <16 x i8>
+; Z15: Cost Model: Found an estimated cost of 8 for instruction: %v55 = fptoui <16 x float> undef to <16 x i8>
ret void;
}
More information about the llvm-commits
mailing list