[llvm] [DAGCombine]Fold (fadd A, Splat(fneg(B))) -> (fsub A, Splat(B)) (PR #173967)

Liao Chunyu via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 31 03:20:41 PST 2025


https://github.com/ChunyuLiao updated https://github.com/llvm/llvm-project/pull/173967

>From 51a0325b1cb8d2b9e08d679b33037887ab656f47 Mon Sep 17 00:00:00 2001
From: Liao Chunyu <chunyu at iscas.ac.cn>
Date: Tue, 30 Dec 2025 06:20:40 +0000
Subject: [PATCH 1/4] init testcase

---
 llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
index 1047860ec8db6..f595c17dd51f8 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
@@ -33,3 +33,15 @@ define <vscale x 8 x double> @vsplat_f64_neg1() {
 ; CHECK-NEXT:    ret
   ret <vscale x 8 x double> splat (double -1.0)
 }
+
+define <vscale x 8 x float> @vfsub_vf_nxv8f32(<vscale x 8 x float> %va) {
+; CHECK-LABEL: vfsub_vf_nxv8f32:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    fli.s fa5, 2.0
+; CHECK-NEXT:    fneg.s fa5, fa5
+; CHECK-NEXT:    vsetvli a0, zero, e32, m4, ta, ma
+; CHECK-NEXT:    vfadd.vf v8, v8, fa5
+; CHECK-NEXT:    ret
+  %vd = fadd <vscale x 8 x float> %va, splat (float -2.000000e+00)
+  ret <vscale x 8 x float> %vd
+}

>From 53feacfeb2b638ce1a60bde313a52b8cadc4a283 Mon Sep 17 00:00:00 2001
From: Liao Chunyu <chunyu at iscas.ac.cn>
Date: Tue, 30 Dec 2025 06:21:48 +0000
Subject: [PATCH 2/4] [DAGCombine]Fold (fadd A, Splat(fneg(B))) -> (fsub A,
 Splat(B))

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 13 +++++++++++++
 llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll    |  3 +--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 74d00317c3649..32348ad1dfc91 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -17965,6 +17965,19 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
             N0, DAG, LegalOperations, ForCodeSize))
       return DAG.getNode(ISD::FSUB, DL, VT, N1, NegN0);
 
+  // fold (fadd A, Splat(fneg(B))) -> (fsub A, Splat(B))
+  if (VT.isVector() &&
+      (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT))) {
+    if (N1.getOpcode() == ISD::SPLAT_VECTOR) {
+      SDValue SplatN0 = N1->getOperand(0);
+      if (SplatN0.getOpcode() == ISD::FNEG && SplatN0.hasOneUse()) {
+        SDValue Splat =
+            DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, SplatN0->getOperand(0));
+        return DAG.getNode(ISD::FSUB, DL, VT, N0, Splat);
+      }
+    }
+  }
+
   auto isFMulNegTwo = [](SDValue FMul) {
     if (!FMul.hasOneUse() || FMul.getOpcode() != ISD::FMUL)
       return false;
diff --git a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
index f595c17dd51f8..8c6cb6bd59c54 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
@@ -38,9 +38,8 @@ define <vscale x 8 x float> @vfsub_vf_nxv8f32(<vscale x 8 x float> %va) {
 ; CHECK-LABEL: vfsub_vf_nxv8f32:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fli.s fa5, 2.0
-; CHECK-NEXT:    fneg.s fa5, fa5
 ; CHECK-NEXT:    vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT:    vfadd.vf v8, v8, fa5
+; CHECK-NEXT:    vfsub.vf v8, v8, fa5
 ; CHECK-NEXT:    ret
   %vd = fadd <vscale x 8 x float> %va, splat (float -2.000000e+00)
   ret <vscale x 8 x float> %vd

>From e265fc6e3091d8afb037d53e40c37e0d9b1d72c9 Mon Sep 17 00:00:00 2001
From: Liao Chunyu <chunyu at iscas.ac.cn>
Date: Wed, 31 Dec 2025 08:53:20 +0000
Subject: [PATCH 3/4] Revert "[DAGCombine]Fold (fadd A, Splat(fneg(B))) ->
 (fsub A, Splat(B))"

This reverts commit 53feacfeb2b638ce1a60bde313a52b8cadc4a283.
---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 13 -------------
 llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll    |  3 ++-
 2 files changed, 2 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 32348ad1dfc91..74d00317c3649 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -17965,19 +17965,6 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
             N0, DAG, LegalOperations, ForCodeSize))
       return DAG.getNode(ISD::FSUB, DL, VT, N1, NegN0);
 
-  // fold (fadd A, Splat(fneg(B))) -> (fsub A, Splat(B))
-  if (VT.isVector() &&
-      (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT))) {
-    if (N1.getOpcode() == ISD::SPLAT_VECTOR) {
-      SDValue SplatN0 = N1->getOperand(0);
-      if (SplatN0.getOpcode() == ISD::FNEG && SplatN0.hasOneUse()) {
-        SDValue Splat =
-            DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, SplatN0->getOperand(0));
-        return DAG.getNode(ISD::FSUB, DL, VT, N0, Splat);
-      }
-    }
-  }
-
   auto isFMulNegTwo = [](SDValue FMul) {
     if (!FMul.hasOneUse() || FMul.getOpcode() != ISD::FMUL)
       return false;
diff --git a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
index 8c6cb6bd59c54..f595c17dd51f8 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
@@ -38,8 +38,9 @@ define <vscale x 8 x float> @vfsub_vf_nxv8f32(<vscale x 8 x float> %va) {
 ; CHECK-LABEL: vfsub_vf_nxv8f32:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fli.s fa5, 2.0
+; CHECK-NEXT:    fneg.s fa5, fa5
 ; CHECK-NEXT:    vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT:    vfsub.vf v8, v8, fa5
+; CHECK-NEXT:    vfadd.vf v8, v8, fa5
 ; CHECK-NEXT:    ret
   %vd = fadd <vscale x 8 x float> %va, splat (float -2.000000e+00)
   ret <vscale x 8 x float> %vd

>From 30f4c8f230eee7b3d76b43af781bfdf1b3d2454c Mon Sep 17 00:00:00 2001
From: Liao Chunyu <chunyu at iscas.ac.cn>
Date: Wed, 31 Dec 2025 09:49:57 +0000
Subject: [PATCH 4/4] [DAG]Add splat_vector to
 TargetLowering::getNegatedExpression splat_vector(fneg(x)) to
 splat_vector(x), then DAGCombine performs some optimizations using these
 results.

---
 llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 13 +++++++++++++
 llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll       | 14 ++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index e3300000fa6f4..da0f2b412af14 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -7554,6 +7554,19 @@ SDValue TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG,
     Cost = NegatibleCost::Neutral;
     return CFP;
   }
+  case ISD::SPLAT_VECTOR: {
+    SDValue X = Op.getOperand(0);
+    if (!isOperationLegal(ISD::SPLAT_VECTOR, VT))
+      break;
+
+    NegatibleCost CostX = NegatibleCost::Expensive;
+    SDValue NegX =
+        getNegatedExpression(X, DAG, LegalOps, OptForSize, CostX, Depth);
+    if (!NegX || CostX >= NegatibleCost::Neutral)
+      break;
+    Cost = CostX;
+    return DAG.getNode(ISD::SPLAT_VECTOR, DL, VT, NegX);
+  }
   case ISD::BUILD_VECTOR: {
     // Only permit BUILD_VECTOR of constants.
     if (llvm::any_of(Op->op_values(), [&](SDValue N) {
diff --git a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
index f595c17dd51f8..24396181b865e 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vsplats-zfa.ll
@@ -38,10 +38,20 @@ define <vscale x 8 x float> @vfsub_vf_nxv8f32(<vscale x 8 x float> %va) {
 ; CHECK-LABEL: vfsub_vf_nxv8f32:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    fli.s fa5, 2.0
-; CHECK-NEXT:    fneg.s fa5, fa5
 ; CHECK-NEXT:    vsetvli a0, zero, e32, m4, ta, ma
-; CHECK-NEXT:    vfadd.vf v8, v8, fa5
+; CHECK-NEXT:    vfsub.vf v8, v8, fa5
 ; CHECK-NEXT:    ret
   %vd = fadd <vscale x 8 x float> %va, splat (float -2.000000e+00)
   ret <vscale x 8 x float> %vd
 }
+
+define <vscale x 8 x float> @vfsub_vf(<vscale x 8 x float> %va) {
+; CHECK-LABEL: vfsub_vf:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    fli.s fa5, 2.0
+; CHECK-NEXT:    vsetvli a0, zero, e32, m4, ta, ma
+; CHECK-NEXT:    vfadd.vf v8, v8, fa5
+; CHECK-NEXT:    ret
+  %vd = fsub <vscale x 8 x float> %va, splat (float -2.000000e+00)
+  ret <vscale x 8 x float> %vd
+}



More information about the llvm-commits mailing list