[llvm] s390x: optimize 128-bit fshl and fshr by high values (PR #154919)
Folkert de Vries via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 22 03:46:31 PDT 2025
https://github.com/folkertdev updated https://github.com/llvm/llvm-project/pull/154919
>From b3dc11808be73554c8c830e3c0454db8b00c8c2f Mon Sep 17 00:00:00 2001
From: Folkert de Vries <folkert at folkertdev.nl>
Date: Fri, 22 Aug 2025 11:20:02 +0200
Subject: [PATCH] s390x: optimize 128-bit fshl and fshr by high values
Turn a funnel shift by N in the range 121..128 into a funnel shift in
the opposite direction by `128 - N`. Because there are dedicated
instructions for funnel shifts by values smaller than 8, this emits
fewer instructions.
This additional rule is useful because LLVM appears to canonicalize
`fshr` into `fshl`, meaning that the rules for `fshr` on values less
than 8 would not match on organic input.
---
.../Target/SystemZ/SystemZISelLowering.cpp | 18 ++++++-
llvm/test/CodeGen/SystemZ/shift-17.ll | 51 +++++++++++++++++++
2 files changed, 68 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
index c73dc3021eb42..382741ca40301 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp
@@ -6719,6 +6719,14 @@ SDValue SystemZTargetLowering::lowerFSHL(SDValue Op, SelectionDAG &DAG) const {
if ((ShiftAmt & 7) == 0 || Subtarget.hasVectorEnhancements2()) {
SDValue Op0 = DAG.getBitcast(MVT::v16i8, Op.getOperand(0));
SDValue Op1 = DAG.getBitcast(MVT::v16i8, Op.getOperand(1));
+ if (ShiftAmt > 120) {
+ // For N in 121..128, fshl N == fshr (128 - N), and for 1 <= N < 8
+ // SHR_DOUBLE_BIT emits fewer instructions.
+ SDValue Val =
+ DAG.getNode(SystemZISD::SHR_DOUBLE_BIT, DL, MVT::v16i8, Op0, Op1,
+ DAG.getTargetConstant(128 - ShiftAmt, DL, MVT::i32));
+ return DAG.getBitcast(MVT::i128, Val);
+ }
SmallVector<int, 16> Mask(16);
for (unsigned Elt = 0; Elt < 16; Elt++)
Mask[Elt] = (ShiftAmt >> 3) + Elt;
@@ -6742,13 +6750,21 @@ SDValue SystemZTargetLowering::lowerFSHR(SDValue Op, SelectionDAG &DAG) const {
// i128 FSHR with a constant amount that is a multiple of 8 can be
// implemented via VECTOR_SHUFFLE. If we have the vector-enhancements-2
// facility, FSHR with a constant amount less than 8 can be implemented
- // via SHL_DOUBLE_BIT, and FSHR with other constant amounts by a
+ // via SHR_DOUBLE_BIT, and FSHR with other constant amounts by a
// combination of the two.
if (auto *ShiftAmtNode = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
uint64_t ShiftAmt = ShiftAmtNode->getZExtValue() & 127;
if ((ShiftAmt & 7) == 0 || Subtarget.hasVectorEnhancements2()) {
SDValue Op0 = DAG.getBitcast(MVT::v16i8, Op.getOperand(0));
SDValue Op1 = DAG.getBitcast(MVT::v16i8, Op.getOperand(1));
+ if (ShiftAmt > 120) {
+ // For N in 121..128, fshr N == fshl (128 - N), and for 1 <= N < 8
+ // SHL_DOUBLE_BIT emits fewer instructions.
+ SDValue Val =
+ DAG.getNode(SystemZISD::SHL_DOUBLE_BIT, DL, MVT::v16i8, Op0, Op1,
+ DAG.getTargetConstant(128 - ShiftAmt, DL, MVT::i32));
+ return DAG.getBitcast(MVT::i128, Val);
+ }
SmallVector<int, 16> Mask(16);
for (unsigned Elt = 0; Elt < 16; Elt++)
Mask[Elt] = 16 - (ShiftAmt >> 3) + Elt;
diff --git a/llvm/test/CodeGen/SystemZ/shift-17.ll b/llvm/test/CodeGen/SystemZ/shift-17.ll
index 45f4ed4d70d20..8f5f9abd0540b 100644
--- a/llvm/test/CodeGen/SystemZ/shift-17.ll
+++ b/llvm/test/CodeGen/SystemZ/shift-17.ll
@@ -249,3 +249,54 @@ define i128 @f8(i128 %a, i128 %b, i128 %sh) {
ret i128 %res
}
+; Funnel shift left by constant N in 121..128, in such cases fshl N == fshr (128 - N)
+define i128 @f9(i128 %a, i128 %b) {
+; CHECK-LABEL: f9:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vl %v1, 0(%r4), 3
+; CHECK-NEXT: vl %v0, 0(%r3), 3
+; CHECK-NEXT: vrepib %v2, 5
+; CHECK-NEXT: vsrl %v1, %v1, %v2
+; CHECK-NEXT: vrepib %v2, 123
+; CHECK-NEXT: vslb %v0, %v0, %v2
+; CHECK-NEXT: vsl %v0, %v0, %v2
+; CHECK-NEXT: vo %v0, %v0, %v1
+; CHECK-NEXT: vst %v0, 0(%r2), 3
+; CHECK-NEXT: br %r14
+;
+; Z15-LABEL: f9:
+; Z15: # %bb.0:
+; Z15-NEXT: vl %v0, 0(%r4), 3
+; Z15-NEXT: vl %v1, 0(%r3), 3
+; Z15-NEXT: vsrd %v0, %v1, %v0, 5
+; Z15-NEXT: vst %v0, 0(%r2), 3
+; Z15-NEXT: br %r14
+ %res = tail call i128 @llvm.fshl.i128(i128 %a, i128 %b, i128 123)
+ ret i128 %res
+}
+
+; Funnel shift right by constant N in 121..128, in such cases fshr N == fshl (128 - N)
+define i128 @f10(i128 %a, i128 %b) {
+; CHECK-LABEL: f10:
+; CHECK: # %bb.0:
+; CHECK-NEXT: vl %v1, 0(%r3), 3
+; CHECK-NEXT: vl %v0, 0(%r4), 3
+; CHECK-NEXT: vrepib %v2, 5
+; CHECK-NEXT: vsl %v1, %v1, %v2
+; CHECK-NEXT: vrepib %v2, 123
+; CHECK-NEXT: vsrlb %v0, %v0, %v2
+; CHECK-NEXT: vsrl %v0, %v0, %v2
+; CHECK-NEXT: vo %v0, %v1, %v0
+; CHECK-NEXT: vst %v0, 0(%r2), 3
+; CHECK-NEXT: br %r14
+;
+; Z15-LABEL: f10:
+; Z15: # %bb.0:
+; Z15-NEXT: vl %v0, 0(%r4), 3
+; Z15-NEXT: vl %v1, 0(%r3), 3
+; Z15-NEXT: vsld %v0, %v1, %v0, 5
+; Z15-NEXT: vst %v0, 0(%r2), 3
+; Z15-NEXT: br %r14
+ %res = tail call i128 @llvm.fshr.i128(i128 %a, i128 %b, i128 123)
+ ret i128 %res
+}
More information about the llvm-commits
mailing list