From aa83bdd1982fbf1174d0769a7842a86779041f37 Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Thu, 8 Sep 2022 22:56:46 -0700 Subject: [PATCH] [DAGCombiner][X86] Fold (sub (subcarry X, 0, Carry), Y) -> (subcarry X, Y, Carry) Fixes PR57576. Differential Revision: https://reviews.llvm.org/D133471 --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 6 ++++++ llvm/test/CodeGen/X86/pr57576.ll | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index d2640b6..c215772 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -3753,6 +3753,12 @@ SDValue DAGCombiner::visitSUB(SDNode *N) { return DAG.getNode(ISD::ADD, DL, VT, N1, N0); } + // (sub (subcarry X, 0, Carry), Y) -> (subcarry X, Y, Carry) + if (N0.getOpcode() == ISD::SUBCARRY && isNullConstant(N0.getOperand(1)) && + N0.getResNo() == 0 && N0.hasOneUse()) + return DAG.getNode(ISD::SUBCARRY, DL, N0->getVTList(), + N0.getOperand(0), N1, N0.getOperand(2)); + if (TLI.isOperationLegalOrCustom(ISD::ADDCARRY, VT)) { // (sub Carry, X) -> (addcarry (sub 0, X), 0, Carry) if (SDValue Carry = getAsCarry(TLI, N0)) { diff --git a/llvm/test/CodeGen/X86/pr57576.ll b/llvm/test/CodeGen/X86/pr57576.ll index ecc30f6..b44eaf3 100644 --- a/llvm/test/CodeGen/X86/pr57576.ll +++ b/llvm/test/CodeGen/X86/pr57576.ll @@ -6,8 +6,7 @@ define { i64, i64 } @sub(i64 noundef %0, i64 noundef %1, i64 noundef %2, i64 nou ; CHECK: # %bb.0: ; CHECK-NEXT: movq %rdi, %rax ; CHECK-NEXT: subq %rdx, %rax -; CHECK-NEXT: sbbq $0, %rsi -; CHECK-NEXT: subq %rcx, %rsi +; CHECK-NEXT: sbbq %rcx, %rsi ; CHECK-NEXT: movq %rsi, %rdx ; CHECK-NEXT: retq %5 = zext i64 %1 to i128 -- 2.7.4