Skip to content

Commit

Permalink
[DAG] visitADDLike - update "(x - y) + -1 -> add (xor y, -1), x" fold…
Browse files Browse the repository at this point in the history
… to accept UNDEF in a splat vector of -1

Make sure we use getNOT instead of reusing the allones (with undefs) vector
  • Loading branch information
RKSimon committed Apr 19, 2024
1 parent 7fbdadb commit 2e68ba9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2831,9 +2831,9 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {

// (x - y) + -1 -> add (xor y, -1), x
if (N0.getOpcode() == ISD::SUB && N0.hasOneUse() &&
isAllOnesOrAllOnesSplat(N1)) {
SDValue Xor = DAG.getNode(ISD::XOR, DL, VT, N0.getOperand(1), N1);
return DAG.getNode(ISD::ADD, DL, VT, Xor, N0.getOperand(0));
isAllOnesOrAllOnesSplat(N1, /*AllowUndefs=*/true)) {
SDValue Not = DAG.getNOT(DL, N0.getOperand(1), VT);
return DAG.getNode(ISD::ADD, DL, VT, Not, N0.getOperand(0));
}

if (SDValue Combined = visitADDLikeCommutative(N0, N1, N))
Expand Down
5 changes: 2 additions & 3 deletions llvm/test/CodeGen/AArch64/xor.ll
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ define <4 x i32> @vec_add_of_not_decrement(<4 x i32> %x, <4 x i32> %y) {
define <4 x i32> @vec_add_of_not_with_undef(<4 x i32> %x, <4 x i32> %y) {
; CHECK-LABEL: vec_add_of_not_with_undef:
; CHECK: // %bb.0:
; CHECK-NEXT: movi v2.2d, #0xffffffffffffffff
; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s
; CHECK-NEXT: add v0.4s, v0.4s, v2.4s
; CHECK-NEXT: mvn v1.16b, v1.16b
; CHECK-NEXT: add v0.4s, v1.4s, v0.4s
; CHECK-NEXT: ret
%t0 = sub <4 x i32> %x, %y
%r = add <4 x i32> %t0, <i32 -1, i32 undef, i32 -1, i32 -1>
Expand Down
17 changes: 8 additions & 9 deletions llvm/test/CodeGen/X86/xor.ll
Original file line number Diff line number Diff line change
Expand Up @@ -627,24 +627,23 @@ define <4 x i32> @vec_add_of_not_decrement(<4 x i32> %x, <4 x i32> %y) {
define <4 x i32> @vec_add_of_not_with_undef(<4 x i32> %x, <4 x i32> %y) {
; X86-LABEL: vec_add_of_not_with_undef:
; X86: # %bb.0:
; X86-NEXT: psubd %xmm1, %xmm0
; X86-NEXT: pcmpeqd %xmm1, %xmm1
; X86-NEXT: paddd %xmm1, %xmm0
; X86-NEXT: pcmpeqd %xmm2, %xmm2
; X86-NEXT: pxor %xmm1, %xmm2
; X86-NEXT: paddd %xmm2, %xmm0
; X86-NEXT: retl
;
; X64-LIN-LABEL: vec_add_of_not_with_undef:
; X64-LIN: # %bb.0:
; X64-LIN-NEXT: psubd %xmm1, %xmm0
; X64-LIN-NEXT: pcmpeqd %xmm1, %xmm1
; X64-LIN-NEXT: paddd %xmm1, %xmm0
; X64-LIN-NEXT: pcmpeqd %xmm2, %xmm2
; X64-LIN-NEXT: pxor %xmm1, %xmm2
; X64-LIN-NEXT: paddd %xmm2, %xmm0
; X64-LIN-NEXT: retq
;
; X64-WIN-LABEL: vec_add_of_not_with_undef:
; X64-WIN: # %bb.0:
; X64-WIN-NEXT: movdqa (%rcx), %xmm1
; X64-WIN-NEXT: psubd (%rdx), %xmm1
; X64-WIN-NEXT: pcmpeqd %xmm0, %xmm0
; X64-WIN-NEXT: paddd %xmm1, %xmm0
; X64-WIN-NEXT: pxor (%rdx), %xmm0
; X64-WIN-NEXT: paddd (%rcx), %xmm0
; X64-WIN-NEXT: retq
%t0 = sub <4 x i32> %x, %y
%r = add <4 x i32> %t0, <i32 -1, i32 undef, i32 -1, i32 -1>
Expand Down

0 comments on commit 2e68ba9

Please sign in to comment.