Skip to content

Commit 2e68ba9

Browse files
committed
[DAG] visitADDLike - update "(x - y) + -1 -> add (xor y, -1), x" fold to accept UNDEF in a splat vector of -1
Make sure we use getNOT instead of reusing the allones (with undefs) vector
1 parent 7fbdadb commit 2e68ba9

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,9 +2831,9 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
28312831

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

28392839
if (SDValue Combined = visitADDLikeCommutative(N0, N1, N))

llvm/test/CodeGen/AArch64/xor.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ define <4 x i32> @vec_add_of_not_decrement(<4 x i32> %x, <4 x i32> %y) {
6161
define <4 x i32> @vec_add_of_not_with_undef(<4 x i32> %x, <4 x i32> %y) {
6262
; CHECK-LABEL: vec_add_of_not_with_undef:
6363
; CHECK: // %bb.0:
64-
; CHECK-NEXT: movi v2.2d, #0xffffffffffffffff
65-
; CHECK-NEXT: sub v0.4s, v0.4s, v1.4s
66-
; CHECK-NEXT: add v0.4s, v0.4s, v2.4s
64+
; CHECK-NEXT: mvn v1.16b, v1.16b
65+
; CHECK-NEXT: add v0.4s, v1.4s, v0.4s
6766
; CHECK-NEXT: ret
6867
%t0 = sub <4 x i32> %x, %y
6968
%r = add <4 x i32> %t0, <i32 -1, i32 undef, i32 -1, i32 -1>

llvm/test/CodeGen/X86/xor.ll

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -627,24 +627,23 @@ define <4 x i32> @vec_add_of_not_decrement(<4 x i32> %x, <4 x i32> %y) {
627627
define <4 x i32> @vec_add_of_not_with_undef(<4 x i32> %x, <4 x i32> %y) {
628628
; X86-LABEL: vec_add_of_not_with_undef:
629629
; X86: # %bb.0:
630-
; X86-NEXT: psubd %xmm1, %xmm0
631-
; X86-NEXT: pcmpeqd %xmm1, %xmm1
632-
; X86-NEXT: paddd %xmm1, %xmm0
630+
; X86-NEXT: pcmpeqd %xmm2, %xmm2
631+
; X86-NEXT: pxor %xmm1, %xmm2
632+
; X86-NEXT: paddd %xmm2, %xmm0
633633
; X86-NEXT: retl
634634
;
635635
; X64-LIN-LABEL: vec_add_of_not_with_undef:
636636
; X64-LIN: # %bb.0:
637-
; X64-LIN-NEXT: psubd %xmm1, %xmm0
638-
; X64-LIN-NEXT: pcmpeqd %xmm1, %xmm1
639-
; X64-LIN-NEXT: paddd %xmm1, %xmm0
637+
; X64-LIN-NEXT: pcmpeqd %xmm2, %xmm2
638+
; X64-LIN-NEXT: pxor %xmm1, %xmm2
639+
; X64-LIN-NEXT: paddd %xmm2, %xmm0
640640
; X64-LIN-NEXT: retq
641641
;
642642
; X64-WIN-LABEL: vec_add_of_not_with_undef:
643643
; X64-WIN: # %bb.0:
644-
; X64-WIN-NEXT: movdqa (%rcx), %xmm1
645-
; X64-WIN-NEXT: psubd (%rdx), %xmm1
646644
; X64-WIN-NEXT: pcmpeqd %xmm0, %xmm0
647-
; X64-WIN-NEXT: paddd %xmm1, %xmm0
645+
; X64-WIN-NEXT: pxor (%rdx), %xmm0
646+
; X64-WIN-NEXT: paddd (%rcx), %xmm0
648647
; X64-WIN-NEXT: retq
649648
%t0 = sub <4 x i32> %x, %y
650649
%r = add <4 x i32> %t0, <i32 -1, i32 undef, i32 -1, i32 -1>

0 commit comments

Comments
 (0)