[llvm] [AArch64][SVE] Match (add_like x (lsr y, c)) -> usra x, y, c (PR #197657)

Harry Ramsey via llvm-commits llvm-commits at lists.llvm.org
Mon May 18 02:11:50 PDT 2026


https://github.com/Harry-Ramsey updated https://github.com/llvm/llvm-project/pull/197657

>From 1a2ff549e599180bfd2bfe2aa6466b8beda3a7a8 Mon Sep 17 00:00:00 2001
From: Harry Ramsey <harry.ramsey at arm.com>
Date: Wed, 13 May 2026 15:46:55 +0000
Subject: [PATCH 1/2] [AArch64][SVE] Match (add_like x (lsr y, c)) -> usra x,
 y, c

Modify SVEE USRA pattern to accept add_like, so both add and or disjoint
forms can select usra.

Add known-bits support for predicated SVE logical shifts, allowing
or_disjoint matching to prove disjointness for plain ORs where possible.
---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp |  5 ----
 .../Target/AArch64/AArch64ISelLowering.cpp    | 12 ++++++++++
 .../lib/Target/AArch64/AArch64SVEInstrInfo.td |  2 +-
 llvm/test/CodeGen/AArch64/sve2-sli-sri.ll     | 24 ++++++++++++-------
 4 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index feea983983c29..6bd73199907a7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4527,11 +4527,6 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
   case ISD::INTRINSIC_WO_CHAIN:
   case ISD::INTRINSIC_W_CHAIN:
   case ISD::INTRINSIC_VOID:
-    // TODO: Probably okay to remove after audit; here to reduce change size
-    // in initial enablement patch for scalable vectors
-    if (Op.getValueType().isScalableVector())
-      break;
-
     // Allow the target to implement this method for its nodes.
     TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
     break;
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 3016e1b94c22d..7bee510f41bbf 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -2903,6 +2903,18 @@ void AArch64TargetLowering::computeKnownBitsForTargetNode(
     }
     break;
   }
+  case AArch64ISD::SHL_PRED:
+  case AArch64ISD::SRL_PRED: {
+    KnownBits KnownVal =
+        DAG.computeKnownBits(Op->getOperand(1), DemandedElts, Depth + 1);
+    KnownBits KnownAmt =
+        DAG.computeKnownBits(Op->getOperand(2), DemandedElts, Depth + 1);
+
+    Known = Op.getOpcode() == AArch64ISD::SHL_PRED
+                ? KnownBits::shl(KnownVal, KnownAmt)
+                : KnownBits::lshr(KnownVal, KnownAmt);
+    break;
+  }
   case ISD::INTRINSIC_WO_CHAIN:
   case ISD::INTRINSIC_VOID: {
     unsigned IntNo = Op.getConstantOperandVal(0);
diff --git a/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td b/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
index 9df77f8e93c64..e4c281a84d72b 100644
--- a/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
+++ b/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
@@ -362,7 +362,7 @@ def AArch64uaba : PatFrags<(ops node:$op1, node:$op2, node:$op3),
 
 def AArch64usra : PatFrags<(ops node:$op1, node:$op2, node:$op3),
                            [(int_aarch64_sve_usra node:$op1, node:$op2, node:$op3),
-                            (add node:$op1, (AArch64lsr_p (SVEAnyPredicate), node:$op2, (SVEShiftSplatImmR (i32 node:$op3))))]>;
+                            (add_like node:$op1, (AArch64lsr_p (SVEAnyPredicate), node:$op2, (SVEShiftSplatImmR (i32 node:$op3))))]>;
 
 def AArch64ssra : PatFrags<(ops node:$op1, node:$op2, node:$op3),
                            [(int_aarch64_sve_ssra node:$op1, node:$op2, node:$op3),
diff --git a/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll b/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll
index 80999fb1f4864..a7048731850d3 100644
--- a/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll
+++ b/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll
@@ -118,14 +118,22 @@ define <vscale x 8 x i16> @testRightGood8x16(<vscale x 8 x i16> %src1, <vscale x
 }
 
 define <vscale x 8 x i16> @testRightBad8x16(<vscale x 8 x i16> %src1, <vscale x 8 x i16> %src2) {
-; CHECK-LABEL: testRightBad8x16:
-; CHECK:       // %bb.0:
-; CHECK-NEXT:    mov w8, #16500 // =0x4074
-; CHECK-NEXT:    lsr z1.h, z1.h, #14
-; CHECK-NEXT:    mov z2.h, w8
-; CHECK-NEXT:    and z0.d, z0.d, z2.d
-; CHECK-NEXT:    orr z0.d, z0.d, z1.d
-; CHECK-NEXT:    ret
+; SVE-LABEL: testRightBad8x16:
+; SVE:       // %bb.0:
+; SVE-NEXT:    mov w8, #16500 // =0x4074
+; SVE-NEXT:    lsr z1.h, z1.h, #14
+; SVE-NEXT:    mov z2.h, w8
+; SVE-NEXT:    and z0.d, z0.d, z2.d
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: testRightBad8x16:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    mov w8, #16500 // =0x4074
+; SVE2-NEXT:    mov z2.h, w8
+; SVE2-NEXT:    and z0.d, z0.d, z2.d
+; SVE2-NEXT:    usra z0.h, z1.h, #14
+; SVE2-NEXT:    ret
   %and.i = and <vscale x 8 x i16> %src1, splat(i16 16500)
   %vshl_n = lshr <vscale x 8 x i16> %src2, splat(i16 14)
   %result = or <vscale x 8 x i16> %and.i, %vshl_n

>From 21e077d8bc6248292bde4f91dfe7f994ba6a3412 Mon Sep 17 00:00:00 2001
From: Harry Ramsey <harry.ramsey at arm.com>
Date: Fri, 15 May 2026 16:05:22 +0000
Subject: [PATCH 2/2] fixup! [AArch64][SVE] Match (add_like x (lsr y, c)) ->
 usra x, y, c

---
 .../Target/AArch64/AArch64ISelLowering.cpp    |   7 +-
 llvm/lib/Target/AArch64/AArch64ISelLowering.h |   2 +-
 .../lib/Target/AArch64/AArch64SVEInstrInfo.td |   2 +-
 llvm/test/CodeGen/AArch64/sve2-sli-sri.ll     | 148 ++++++++++++++++--
 4 files changed, 141 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 7bee510f41bbf..e10345debfd83 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -2905,6 +2905,9 @@ void AArch64TargetLowering::computeKnownBitsForTargetNode(
   }
   case AArch64ISD::SHL_PRED:
   case AArch64ISD::SRL_PRED: {
+    if (isAllActivePredicate(DAG, Op->getOperand(0)))
+      break;
+
     KnownBits KnownVal =
         DAG.computeKnownBits(Op->getOperand(1), DemandedElts, Depth + 1);
     KnownBits KnownAmt =
@@ -15766,7 +15769,7 @@ static bool isAllInactivePredicate(SDValue N) {
   return ISD::isConstantSplatVectorAllZeros(N.getNode());
 }
 
-static bool isAllActivePredicate(SelectionDAG &DAG, SDValue N) {
+static bool isAllActivePredicate(const SelectionDAG &DAG, SDValue N) {
   unsigned NumElts = N.getValueType().getVectorMinNumElements();
 
   // Look through cast.
@@ -33494,7 +33497,7 @@ SDValue AArch64TargetLowering::getSVESafeBitCast(EVT VT, SDValue Op,
   return Op;
 }
 
-bool AArch64TargetLowering::isAllActivePredicate(SelectionDAG &DAG,
+bool AArch64TargetLowering::isAllActivePredicate(const SelectionDAG &DAG,
                                                  SDValue N) const {
   return ::isAllActivePredicate(DAG, N);
 }
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.h b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
index 25cab4d3eed7e..77f8f888776bc 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.h
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
@@ -539,7 +539,7 @@ class AArch64TargetLowering : public TargetLowering {
     return 128;
   }
 
-  bool isAllActivePredicate(SelectionDAG &DAG, SDValue N) const;
+  bool isAllActivePredicate(const SelectionDAG &DAG, SDValue N) const;
   EVT getPromotedVTForPredicate(EVT VT) const;
 
   EVT getAsmOperandValueType(const DataLayout &DL, Type *Ty,
diff --git a/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td b/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
index e4c281a84d72b..f1ff8e402c345 100644
--- a/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
+++ b/llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
@@ -366,7 +366,7 @@ def AArch64usra : PatFrags<(ops node:$op1, node:$op2, node:$op3),
 
 def AArch64ssra : PatFrags<(ops node:$op1, node:$op2, node:$op3),
                            [(int_aarch64_sve_ssra node:$op1, node:$op2, node:$op3),
-                            (add node:$op1, (AArch64asr_p (SVEAnyPredicate), node:$op2, (SVEShiftSplatImmR (i32 node:$op3))))]>;
+                            (add_like node:$op1, (AArch64asr_p (SVEAnyPredicate), node:$op2, (SVEShiftSplatImmR (i32 node:$op3))))]>;
 
 def AArch64ursra : PatFrags<(ops node:$op1, node:$op2, node:$op3),
                             [(int_aarch64_sve_ursra node:$op1, node:$op2, node:$op3),
diff --git a/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll b/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll
index a7048731850d3..047f2ccfeee63 100644
--- a/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll
+++ b/llvm/test/CodeGen/AArch64/sve2-sli-sri.ll
@@ -52,6 +52,38 @@ define <vscale x 16 x i8> @testRightGood16x8(<vscale x 16 x i8> %src1, <vscale x
   ret <vscale x 16 x i8> %result
 }
 
+define <vscale x 16 x i8> @usra_disjoint_or16xi8(<vscale x 16 x i8> %a, <vscale x 16 x i8> %b) {
+; SVE-LABEL: usra_disjoint_or16xi8:
+; SVE:       // %bb.0:
+; SVE-NEXT:    lsr z1.b, z1.b, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: usra_disjoint_or16xi8:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    usra z0.b, z1.b, #4
+; SVE2-NEXT:    ret
+  %shift = lshr <vscale x 16 x i8> %b, splat(i8 4)
+  %add = or disjoint <vscale x 16 x i8> %a, %shift
+  ret <vscale x 16 x i8> %add
+}
+
+define <vscale x 16 x i8> @ssra_disjoint_or16xi8(<vscale x 16 x i8> %a, <vscale x 16 x i8> %b) {
+; SVE-LABEL: ssra_disjoint_or16xi8:
+; SVE:       // %bb.0:
+; SVE-NEXT:    asr z1.b, z1.b, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: ssra_disjoint_or16xi8:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    ssra z0.b, z1.b, #4
+; SVE2-NEXT:    ret
+  %shift = ashr <vscale x 16 x i8> %b, splat(i8 4)
+  %add = or disjoint <vscale x 16 x i8> %a, %shift
+  ret <vscale x 16 x i8> %add
+}
+
 define <vscale x 16 x i8> @testRightBad16x8(<vscale x 16 x i8> %src1, <vscale x 16 x i8> %src2) {
 ; CHECK-LABEL: testRightBad16x8:
 ; CHECK:       // %bb.0:
@@ -118,26 +150,50 @@ define <vscale x 8 x i16> @testRightGood8x16(<vscale x 8 x i16> %src1, <vscale x
 }
 
 define <vscale x 8 x i16> @testRightBad8x16(<vscale x 8 x i16> %src1, <vscale x 8 x i16> %src2) {
-; SVE-LABEL: testRightBad8x16:
+; CHECK-LABEL: testRightBad8x16:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    mov w8, #16500 // =0x4074
+; CHECK-NEXT:    lsr z1.h, z1.h, #14
+; CHECK-NEXT:    mov z2.h, w8
+; CHECK-NEXT:    and z0.d, z0.d, z2.d
+; CHECK-NEXT:    orr z0.d, z0.d, z1.d
+; CHECK-NEXT:    ret
+  %and.i = and <vscale x 8 x i16> %src1, splat(i16 16500)
+  %vshl_n = lshr <vscale x 8 x i16> %src2, splat(i16 14)
+  %result = or <vscale x 8 x i16> %and.i, %vshl_n
+  ret <vscale x 8 x i16> %result
+}
+
+define <vscale x 8 x i16> @usra_disjoint_or8xi16(<vscale x 8 x i16> %a, <vscale x 8 x i16> %b) {
+; SVE-LABEL: usra_disjoint_or8xi16:
 ; SVE:       // %bb.0:
-; SVE-NEXT:    mov w8, #16500 // =0x4074
-; SVE-NEXT:    lsr z1.h, z1.h, #14
-; SVE-NEXT:    mov z2.h, w8
-; SVE-NEXT:    and z0.d, z0.d, z2.d
+; SVE-NEXT:    lsr z1.h, z1.h, #4
 ; SVE-NEXT:    orr z0.d, z0.d, z1.d
 ; SVE-NEXT:    ret
 ;
-; SVE2-LABEL: testRightBad8x16:
+; SVE2-LABEL: usra_disjoint_or8xi16:
 ; SVE2:       // %bb.0:
-; SVE2-NEXT:    mov w8, #16500 // =0x4074
-; SVE2-NEXT:    mov z2.h, w8
-; SVE2-NEXT:    and z0.d, z0.d, z2.d
-; SVE2-NEXT:    usra z0.h, z1.h, #14
+; SVE2-NEXT:    usra z0.h, z1.h, #4
 ; SVE2-NEXT:    ret
-  %and.i = and <vscale x 8 x i16> %src1, splat(i16 16500)
-  %vshl_n = lshr <vscale x 8 x i16> %src2, splat(i16 14)
-  %result = or <vscale x 8 x i16> %and.i, %vshl_n
-  ret <vscale x 8 x i16> %result
+  %shift = lshr <vscale x 8 x i16> %b, splat(i16 4)
+  %add = or disjoint <vscale x 8 x i16> %a, %shift
+  ret <vscale x 8 x i16> %add
+}
+
+define <vscale x 8 x i16> @ssra_disjoint_or8xi16(<vscale x 8 x i16> %a, <vscale x 8 x i16> %b) {
+; SVE-LABEL: ssra_disjoint_or8xi16:
+; SVE:       // %bb.0:
+; SVE-NEXT:    asr z1.h, z1.h, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: ssra_disjoint_or8xi16:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    ssra z0.h, z1.h, #4
+; SVE2-NEXT:    ret
+  %shift = ashr <vscale x 8 x i16> %b, splat(i16 4)
+  %add = or disjoint <vscale x 8 x i16> %a, %shift
+  ret <vscale x 8 x i16> %add
 }
 
 define <vscale x 4 x i32> @testLeftGood4x32(<vscale x 4 x i32> %src1, <vscale x 4 x i32> %src2) {
@@ -202,6 +258,38 @@ define <vscale x 4 x i32> @testRightBad4x32(<vscale x 4 x i32> %src1, <vscale x
   ret <vscale x 4 x i32> %result
 }
 
+define <vscale x 4 x i32> @usra_disjoint_or4xi32(<vscale x 4 x i32> %a, <vscale x 4 x i32> %b) {
+; SVE-LABEL: usra_disjoint_or4xi32:
+; SVE:       // %bb.0:
+; SVE-NEXT:    lsr z1.s, z1.s, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: usra_disjoint_or4xi32:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    usra z0.s, z1.s, #4
+; SVE2-NEXT:    ret
+  %shift = lshr <vscale x 4 x i32> %b, splat(i32 4)
+  %add = or disjoint <vscale x 4 x i32> %a, %shift
+  ret <vscale x 4 x i32> %add
+}
+
+define <vscale x 4 x i32> @ssra_disjoint_or4xi32(<vscale x 4 x i32> %a, <vscale x 4 x i32> %b) {
+; SVE-LABEL: ssra_disjoint_or4xi32:
+; SVE:       // %bb.0:
+; SVE-NEXT:    asr z1.s, z1.s, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: ssra_disjoint_or4xi32:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    ssra z0.s, z1.s, #4
+; SVE2-NEXT:    ret
+  %shift = ashr <vscale x 4 x i32> %b, splat(i32 4)
+  %add = or disjoint <vscale x 4 x i32> %a, %shift
+  ret <vscale x 4 x i32> %add
+}
+
 define <vscale x 2 x i64> @testLeftGood2x64(<vscale x 2 x i64> %src1, <vscale x 2 x i64> %src2) {
 ; SVE-LABEL: testLeftGood2x64:
 ; SVE:       // %bb.0:
@@ -269,3 +357,35 @@ define <vscale x 2 x i64> @testRightBad2x64(<vscale x 2 x i64> %src1, <vscale x
   %result = or <vscale x 2 x i64> %and.i, %vshl_n
   ret <vscale x 2 x i64> %result
 }
+
+define <vscale x 2 x i64> @usra_disjoint_or2xi64(<vscale x 2 x i64> %a, <vscale x 2 x i64> %b) {
+; SVE-LABEL: usra_disjoint_or2xi64:
+; SVE:       // %bb.0:
+; SVE-NEXT:    lsr z1.d, z1.d, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: usra_disjoint_or2xi64:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    usra z0.d, z1.d, #4
+; SVE2-NEXT:    ret
+  %shift = lshr <vscale x 2 x i64> %b, splat(i64 4)
+  %add = or disjoint <vscale x 2 x i64> %a, %shift
+  ret <vscale x 2 x i64> %add
+}
+
+define <vscale x 2 x i64> @ssra_disjoint_or2xi64(<vscale x 2 x i64> %a, <vscale x 2 x i64> %b) {
+; SVE-LABEL: ssra_disjoint_or2xi64:
+; SVE:       // %bb.0:
+; SVE-NEXT:    asr z1.d, z1.d, #4
+; SVE-NEXT:    orr z0.d, z0.d, z1.d
+; SVE-NEXT:    ret
+;
+; SVE2-LABEL: ssra_disjoint_or2xi64:
+; SVE2:       // %bb.0:
+; SVE2-NEXT:    ssra z0.d, z1.d, #4
+; SVE2-NEXT:    ret
+  %shift = ashr <vscale x 2 x i64> %b, splat(i64 4)
+  %add = or disjoint <vscale x 2 x i64> %a, %shift
+  ret <vscale x 2 x i64> %add
+}



More information about the llvm-commits mailing list