[PATCH] D126646: [DAGCombine] fold (A+B)-(A-C) -> B+C

Pretty-box via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 30 06:03:52 PDT 2022


Pretty-box created this revision.
Pretty-box added reviewers: benshi001, craig.topper, frasercrmck.
Pretty-box added projects: All, LLVM.
Herald added subscribers: luke957, StephenFan, ecnelises, luismarques, apazos, sameer.abuasal, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, niosHD, sabuasal, simoncook, johnrusso, rbar, asb, hiraditya.
Pretty-box requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, MaskRay.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126646

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/RISCV/sub-combine.ll


Index: llvm/test/CodeGen/RISCV/sub-combine.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/RISCV/sub-combine.ll
@@ -0,0 +1,21 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
+; RUN:   | FileCheck -check-prefix=RV32I %s
+; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
+; RUN:   | FileCheck -check-prefix=RV64I %s
+
+define signext i32 @sub_combine(i32 %a, i32 %b, i32 %c) nounwind {
+; RV32I-LABEL: sub_combine:
+; RV32I:       # %bb.0:
+; RV32I-NEXT:    add a0, a2, a1
+; RV32I-NEXT:    ret
+;
+; RV64I-LABEL: sub_combine:
+; RV64I:       # %bb.0:
+; RV64I-NEXT:    addw a0, a2, a1
+; RV64I-NEXT:    ret
+  %1 = add i32 %a, %b
+  %2 = sub i32 %a, %c
+  %3 = sub i32 %1, %2
+  ret i32 %3
+}
Index: llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -3516,6 +3516,11 @@
     return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0),
                        N0.getOperand(1).getOperand(0));
 
+  // fold (A+B)-(A-C) -> B+C
+  if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SUB &&
+      N0.getOperand(0) == N1.getOperand(0))
+    return DAG.getNode(ISD::ADD, DL, VT, N1.getOperand(1), N0.getOperand(1));
+
   // fold (A-(B-C)) -> A+(C-B)
   if (N1.getOpcode() == ISD::SUB && N1.hasOneUse())
     return DAG.getNode(ISD::ADD, DL, VT, N0,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126646.432901.patch
Type: text/x-patch
Size: 1568 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220530/ea4f752b/attachment.bin>


More information about the llvm-commits mailing list