[llvm] [AArch64] optimize manual addp (PR #181549)

Folkert de Vries via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 15 08:25:50 PST 2026


https://github.com/folkertdev updated https://github.com/llvm/llvm-project/pull/181549

>From 6d2ccafe57daf9658cea6f95040a0e61fb08f966 Mon Sep 17 00:00:00 2001
From: Folkert de Vries <folkert at folkertdev.nl>
Date: Sun, 15 Feb 2026 15:01:22 +0100
Subject: [PATCH] [AArch64] optimize manual addp

---
 .../Target/AArch64/AArch64ISelLowering.cpp    | 42 ++++++++++
 llvm/test/CodeGen/AArch64/addp-shuffle.ll     | 84 +++++++++++++++++++
 llvm/test/CodeGen/AArch64/arm64-trn.ll        |  8 +-
 llvm/test/CodeGen/AArch64/arm64-uzp.ll        | 55 +++++++-----
 4 files changed, 163 insertions(+), 26 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 2b2e057c80373..2c6dbaf383f63 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -23022,6 +23022,46 @@ static SDValue performAddTruncShiftCombine(SDNode *N, SelectionDAG &DAG) {
   return DAG.getNode(ISD::ADD, DL, VT, Trunc, Shift);
 }
 
+static SDValue performAddIntoAddP(SDNode *N, SelectionDAG &DAG) {
+  if (N->getOpcode() != ISD::ADD)
+    return SDValue();
+
+  const auto &Subtarget = DAG.getSubtarget<AArch64Subtarget>();
+  if (Subtarget.isStreaming() || Subtarget.isStreamingCompatible())
+    return SDValue();
+
+  EVT VT = N->getValueType(0);
+  if (!VT.isVector() || VT.isScalableVector() ||
+      !VT.getScalarType().isInteger())
+    return SDValue();
+
+  unsigned VBits = VT.getSizeInBits();
+  if (VBits != 64 && VBits != 128)
+    return SDValue();
+
+  // Check whether the two shuffles deinterleave, so one mask is all even
+  // indices, the other all odd indices of a vector.
+  auto *S0 = dyn_cast<ShuffleVectorSDNode>(N->getOperand(0));
+  auto *S1 = dyn_cast<ShuffleVectorSDNode>(N->getOperand(1));
+  if (!S0 || !S1 || S0 == S1)
+    return SDValue();
+
+  if (S0->getOperand(0) != S1->getOperand(0) ||
+      S0->getOperand(1) != S1->getOperand(1))
+    return SDValue();
+
+  ArrayRef<int> M0 = S0->getMask(), M1 = S1->getMask();
+  auto Is = [](ArrayRef<int> M, unsigned I) {
+    return ShuffleVectorInst::isDeInterleaveMaskOfFactor(M, 2, I);
+  };
+  if (!((Is(M0, 0) && Is(M1, 1)) || (Is(M0, 1) && Is(M1, 0))))
+    return SDValue();
+
+  SDLoc DL(N);
+  return DAG.getNode(AArch64ISD::ADDP, DL, VT, S0->getOperand(0),
+                     S0->getOperand(1));
+}
+
 static SDValue performAddSubCombine(SDNode *N,
                                     TargetLowering::DAGCombinerInfo &DCI) {
   // Try to change sum of two reductions.
@@ -23047,6 +23087,8 @@ static SDValue performAddSubCombine(SDNode *N,
     return Val;
   if (SDValue Val = performAddTruncShiftCombine(N, DCI.DAG))
     return Val;
+  if (SDValue Val = performAddIntoAddP(N, DCI.DAG))
+    return Val;
 
   if (SDValue Val = performExtBinopLoadFold(N, DCI.DAG))
     return Val;
diff --git a/llvm/test/CodeGen/AArch64/addp-shuffle.ll b/llvm/test/CodeGen/AArch64/addp-shuffle.ll
index 54c96820285d3..9b62addaef385 100644
--- a/llvm/test/CodeGen/AArch64/addp-shuffle.ll
+++ b/llvm/test/CodeGen/AArch64/addp-shuffle.ll
@@ -208,3 +208,87 @@ define <4 x i32> @sdot(<4 x i32> %z, <16 x i8> %a, <16 x i8> %b) {
   %n = add <4 x i32> %z, %o
   ret <4 x i32> %n
 }
+
+define <8 x i8> @manual_addp_v8i8(<8 x i8> %a, <8 x i8> %b) {
+; CHECK-LABEL: manual_addp_v8i8:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.8b, v0.8b, v1.8b
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <8 x i8> %a, <8 x i8> %b, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
+  %1 = shufflevector <8 x i8> %a, <8 x i8> %b, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
+  %2 = add <8 x i8> %0, %1
+  ret <8 x i8> %2
+}
+
+define <4 x i16> @manual_addp_v4i16(<4 x i16> %a, <4 x i16> %b) {
+; CHECK-LABEL: manual_addp_v4i16:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.4h, v0.4h, v1.4h
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <4 x i16> %a, <4 x i16> %b, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
+  %1 = shufflevector <4 x i16> %a, <4 x i16> %b, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
+  %2 = add <4 x i16> %0, %1
+  ret <4 x i16> %2
+}
+
+define <2 x i32> @manual_addp_v2i32(<2 x i32> %a, <2 x i32> %b) {
+; CHECK-LABEL: manual_addp_v2i32:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.2s, v0.2s, v1.2s
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <2 x i32> %a, <2 x i32> %b, <2 x i32> <i32 0, i32 2>
+  %1 = shufflevector <2 x i32> %a, <2 x i32> %b, <2 x i32> <i32 1, i32 3>
+  %2 = add <2 x i32> %0, %1
+  ret <2 x i32> %2
+}
+
+define <16 x i8> @manual_addp_v16i8(<16 x i8> %a, <16 x i8> %b) {
+; CHECK-LABEL: manual_addp_v16i8:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.16b, v0.16b, v1.16b
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <16 x i8> %a, <16 x i8> %b, <16 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14, i32 16, i32 18, i32 20, i32 22, i32 24, i32 26, i32 28, i32 30>
+  %1 = shufflevector <16 x i8> %a, <16 x i8> %b, <16 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15, i32 17, i32 19, i32 21, i32 23, i32 25, i32 27, i32 29, i32 31>
+  %2 = add <16 x i8> %0, %1
+  ret <16 x i8> %2
+}
+
+define <8 x i16> @manual_addp_v8i16(<8 x i16> %a, <8 x i16> %b) {
+; CHECK-LABEL: manual_addp_v8i16:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.8h, v0.8h, v1.8h
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <8 x i16> %a, <8 x i16> %b, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
+  %1 = shufflevector <8 x i16> %a, <8 x i16> %b, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
+  %2 = add <8 x i16> %0, %1
+  ret <8 x i16> %2
+}
+
+define <4 x i32> @manual_addp_v4i32(<4 x i32> %a, <4 x i32> %b) {
+; CHECK-LABEL: manual_addp_v4i32:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.4s, v0.4s, v1.4s
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <4 x i32> %a, <4 x i32> %b, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
+  %1 = shufflevector <4 x i32> %a, <4 x i32> %b, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
+  %2 = add <4 x i32> %0, %1
+  ret <4 x i32> %2
+}
+
+define <2 x i64> @manual_addp_v2i64(<2 x i64> %a, <2 x i64> %b) {
+; CHECK-LABEL: manual_addp_v2i64:
+; CHECK:       // %bb.0: // %start
+; CHECK-NEXT:    addp v0.2d, v0.2d, v1.2d
+; CHECK-NEXT:    ret
+start:
+  %0 = shufflevector <2 x i64> %a, <2 x i64> %b, <2 x i32> <i32 0, i32 2>
+  %1 = shufflevector <2 x i64> %a, <2 x i64> %b, <2 x i32> <i32 1, i32 3>
+  %2 = add <2 x i64> %0, %1
+  ret <2 x i64> %2
+}
diff --git a/llvm/test/CodeGen/AArch64/arm64-trn.ll b/llvm/test/CodeGen/AArch64/arm64-trn.ll
index 120c2d13a7ab7..606f668efed7b 100644
--- a/llvm/test/CodeGen/AArch64/arm64-trn.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-trn.ll
@@ -85,18 +85,14 @@ define <2 x i32> @vtrni32(ptr %A, ptr %B) nounwind {
 ; CHECKLE:       // %bb.0:
 ; CHECKLE-NEXT:    ldr d0, [x0]
 ; CHECKLE-NEXT:    ldr d1, [x1]
-; CHECKLE-NEXT:    zip1 v2.2s, v0.2s, v1.2s
-; CHECKLE-NEXT:    zip2 v0.2s, v0.2s, v1.2s
-; CHECKLE-NEXT:    add v0.2s, v2.2s, v0.2s
+; CHECKLE-NEXT:    addp v0.2s, v0.2s, v1.2s
 ; CHECKLE-NEXT:    ret
 ;
 ; CHECKBE-LABEL: vtrni32:
 ; CHECKBE:       // %bb.0:
 ; CHECKBE-NEXT:    ld1 { v0.2s }, [x0]
 ; CHECKBE-NEXT:    ld1 { v1.2s }, [x1]
-; CHECKBE-NEXT:    zip1 v2.2s, v0.2s, v1.2s
-; CHECKBE-NEXT:    zip2 v0.2s, v0.2s, v1.2s
-; CHECKBE-NEXT:    add v0.2s, v2.2s, v0.2s
+; CHECKBE-NEXT:    addp v0.2s, v0.2s, v1.2s
 ; CHECKBE-NEXT:    rev64 v0.2s, v0.2s
 ; CHECKBE-NEXT:    ret
 	%tmp1 = load <2 x i32>, ptr %A
diff --git a/llvm/test/CodeGen/AArch64/arm64-uzp.ll b/llvm/test/CodeGen/AArch64/arm64-uzp.ll
index 02068428c3a4f..271543d5a22c6 100644
--- a/llvm/test/CodeGen/AArch64/arm64-uzp.ll
+++ b/llvm/test/CodeGen/AArch64/arm64-uzp.ll
@@ -1,14 +1,19 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
-; RUN: llc < %s -mtriple=arm64-eabi -aarch64-neon-syntax=apple | FileCheck %s
-; RUN: llc < %s -mtriple=arm64-eabi -aarch64-neon-syntax=apple -global-isel | FileCheck %s
+; RUN: llc < %s -mtriple=arm64-eabi -aarch64-neon-syntax=apple | FileCheck %s --check-prefix=CHECK,SDAG
+; RUN: llc < %s -mtriple=arm64-eabi -aarch64-neon-syntax=apple -global-isel | FileCheck %s --check-prefix=CHECK,GI
 
 define <8 x i8> @vuzpi8(<8 x i8> %A, <8 x i8> %B) nounwind {
-; CHECK-LABEL: vuzpi8:
-; CHECK:       // %bb.0:
-; CHECK-NEXT:    uzp1.8b v2, v0, v1
-; CHECK-NEXT:    uzp2.8b v0, v0, v1
-; CHECK-NEXT:    add.8b v0, v2, v0
-; CHECK-NEXT:    ret
+; SDAG-LABEL: vuzpi8:
+; SDAG:       // %bb.0:
+; SDAG-NEXT:    addp.8b v0, v0, v1
+; SDAG-NEXT:    ret
+;
+; GI-LABEL: vuzpi8:
+; GI:       // %bb.0:
+; GI-NEXT:    uzp1.8b v2, v0, v1
+; GI-NEXT:    uzp2.8b v0, v0, v1
+; GI-NEXT:    add.8b v0, v2, v0
+; GI-NEXT:    ret
   %tmp3 = shufflevector <8 x i8> %A, <8 x i8> %B, <8 x i32> <i32 0, i32 2, i32 4, i32 6, i32 8, i32 10, i32 12, i32 14>
   %tmp4 = shufflevector <8 x i8> %A, <8 x i8> %B, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 9, i32 11, i32 13, i32 15>
   %tmp5 = add <8 x i8> %tmp3, %tmp4
@@ -16,12 +21,17 @@ define <8 x i8> @vuzpi8(<8 x i8> %A, <8 x i8> %B) nounwind {
 }
 
 define <4 x i16> @vuzpi16(<4 x i16> %A, <4 x i16> %B) nounwind {
-; CHECK-LABEL: vuzpi16:
-; CHECK:       // %bb.0:
-; CHECK-NEXT:    uzp1.4h v2, v0, v1
-; CHECK-NEXT:    uzp2.4h v0, v0, v1
-; CHECK-NEXT:    add.4h v0, v2, v0
-; CHECK-NEXT:    ret
+; SDAG-LABEL: vuzpi16:
+; SDAG:       // %bb.0:
+; SDAG-NEXT:    addp.4h v0, v0, v1
+; SDAG-NEXT:    ret
+;
+; GI-LABEL: vuzpi16:
+; GI:       // %bb.0:
+; GI-NEXT:    uzp1.4h v2, v0, v1
+; GI-NEXT:    uzp2.4h v0, v0, v1
+; GI-NEXT:    add.4h v0, v2, v0
+; GI-NEXT:    ret
   %tmp3 = shufflevector <4 x i16> %A, <4 x i16> %B, <4 x i32> <i32 0, i32 2, i32 4, i32 6>
   %tmp4 = shufflevector <4 x i16> %A, <4 x i16> %B, <4 x i32> <i32 1, i32 3, i32 5, i32 7>
   %tmp5 = add <4 x i16> %tmp3, %tmp4
@@ -83,12 +93,17 @@ define <4 x float> @vuzpQf(<4 x float> %A, <4 x float> %B) nounwind {
 ; Undef shuffle indices should not prevent matching to VUZP:
 
 define <8 x i8> @vuzpi8_undef(<8 x i8> %A, <8 x i8> %B) nounwind {
-; CHECK-LABEL: vuzpi8_undef:
-; CHECK:       // %bb.0:
-; CHECK-NEXT:    uzp1.8b v2, v0, v1
-; CHECK-NEXT:    uzp2.8b v0, v0, v1
-; CHECK-NEXT:    add.8b v0, v2, v0
-; CHECK-NEXT:    ret
+; SDAG-LABEL: vuzpi8_undef:
+; SDAG:       // %bb.0:
+; SDAG-NEXT:    addp.8b v0, v0, v1
+; SDAG-NEXT:    ret
+;
+; GI-LABEL: vuzpi8_undef:
+; GI:       // %bb.0:
+; GI-NEXT:    uzp1.8b v2, v0, v1
+; GI-NEXT:    uzp2.8b v0, v0, v1
+; GI-NEXT:    add.8b v0, v2, v0
+; GI-NEXT:    ret
   %tmp3 = shufflevector <8 x i8> %A, <8 x i8> %B, <8 x i32> <i32 0, i32 2, i32 undef, i32 undef, i32 8, i32 10, i32 12, i32 14>
   %tmp4 = shufflevector <8 x i8> %A, <8 x i8> %B, <8 x i32> <i32 1, i32 3, i32 5, i32 7, i32 undef, i32 undef, i32 13, i32 15>
   %tmp5 = add <8 x i8> %tmp3, %tmp4



More information about the llvm-commits mailing list