Skip to content

Commit 31bfae3

Browse files
authored
[DAGCombiner] Add hasOneUse checks for folding (not (add X, -1)) to (neg X) (#126667)
To get more better codegen for AArch with bic, x86 with andn and riscv with andn.
1 parent caa9fae commit 31bfae3

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9691,7 +9691,7 @@ SDValue DAGCombiner::visitXOR(SDNode *N) {
96919691
}
96929692

96939693
// fold (not (add X, -1)) -> (neg X)
9694-
if (isAllOnesConstant(N1) && N0.getOpcode() == ISD::ADD &&
9694+
if (N0.getOpcode() == ISD::ADD && N0.hasOneUse() && isAllOnesConstant(N1) &&
96959695
isAllOnesOrAllOnesSplat(N0.getOperand(1))) {
96969696
return DAG.getNegative(N0.getOperand(0), DL, VT);
96979697
}

llvm/test/CodeGen/AArch64/align-down.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ define i32 @t2_commutative(i32 %ptr, i32 %alignment) nounwind {
5454
define i32 @t3_extrause0(i32 %ptr, i32 %alignment, ptr %mask_storage) nounwind {
5555
; CHECK-LABEL: t3_extrause0:
5656
; CHECK: // %bb.0:
57-
; CHECK-NEXT: neg w8, w1
58-
; CHECK-NEXT: sub w9, w1, #1
59-
; CHECK-NEXT: and w0, w0, w8
60-
; CHECK-NEXT: str w9, [x2]
57+
; CHECK-NEXT: sub w8, w1, #1
58+
; CHECK-NEXT: bic w0, w0, w8
59+
; CHECK-NEXT: str w8, [x2]
6160
; CHECK-NEXT: ret
6261
%mask = add i32 %alignment, -1
6362
store i32 %mask, ptr %mask_storage

llvm/test/CodeGen/X86/align-down.ll

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,25 +82,40 @@ define i32 @t2_commutative(i32 %ptr, i32 %alignment) nounwind {
8282
; Extra use tests
8383

8484
define i32 @t3_extrause0(i32 %ptr, i32 %alignment, ptr %mask_storage) nounwind {
85-
; X86-LABEL: t3_extrause0:
86-
; X86: # %bb.0:
87-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
88-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
89-
; X86-NEXT: leal -1(%eax), %edx
90-
; X86-NEXT: movl %edx, (%ecx)
91-
; X86-NEXT: negl %eax
92-
; X86-NEXT: andl {{[0-9]+}}(%esp), %eax
93-
; X86-NEXT: retl
85+
; NOBMI-X86-LABEL: t3_extrause0:
86+
; NOBMI-X86: # %bb.0:
87+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
88+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
89+
; NOBMI-X86-NEXT: decl %eax
90+
; NOBMI-X86-NEXT: movl %eax, (%ecx)
91+
; NOBMI-X86-NEXT: notl %eax
92+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
93+
; NOBMI-X86-NEXT: retl
9494
;
95-
; X64-LABEL: t3_extrause0:
96-
; X64: # %bb.0:
97-
; X64-NEXT: movl %esi, %eax
98-
; X64-NEXT: leal -1(%rax), %ecx
99-
; X64-NEXT: movl %ecx, (%rdx)
100-
; X64-NEXT: negl %eax
101-
; X64-NEXT: andl %edi, %eax
102-
; X64-NEXT: # kill: def $eax killed $eax killed $rax
103-
; X64-NEXT: retq
95+
; BMI-X86-LABEL: t3_extrause0:
96+
; BMI-X86: # %bb.0:
97+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
98+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
99+
; BMI-X86-NEXT: decl %ecx
100+
; BMI-X86-NEXT: movl %ecx, (%eax)
101+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %ecx, %eax
102+
; BMI-X86-NEXT: retl
103+
;
104+
; NOBMI-X64-LABEL: t3_extrause0:
105+
; NOBMI-X64: # %bb.0:
106+
; NOBMI-X64-NEXT: # kill: def $esi killed $esi def $rsi
107+
; NOBMI-X64-NEXT: leal -1(%rsi), %eax
108+
; NOBMI-X64-NEXT: movl %eax, (%rdx)
109+
; NOBMI-X64-NEXT: notl %eax
110+
; NOBMI-X64-NEXT: andl %edi, %eax
111+
; NOBMI-X64-NEXT: retq
112+
;
113+
; BMI-X64-LABEL: t3_extrause0:
114+
; BMI-X64: # %bb.0:
115+
; BMI-X64-NEXT: decl %esi
116+
; BMI-X64-NEXT: movl %esi, (%rdx)
117+
; BMI-X64-NEXT: andnl %edi, %esi, %eax
118+
; BMI-X64-NEXT: retq
104119
%mask = add i32 %alignment, -1
105120
store i32 %mask, ptr %mask_storage
106121
%bias = and i32 %ptr, %mask

llvm/test/CodeGen/X86/not-of-dec.ll

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ define i32 @t2_extrause(i32 %alignment, ptr %mask_storage) nounwind {
5757
; X86: # %bb.0:
5858
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
5959
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
60-
; X86-NEXT: leal -1(%eax), %edx
61-
; X86-NEXT: movl %edx, (%ecx)
62-
; X86-NEXT: negl %eax
60+
; X86-NEXT: decl %eax
61+
; X86-NEXT: movl %eax, (%ecx)
62+
; X86-NEXT: notl %eax
6363
; X86-NEXT: retl
6464
;
6565
; X64-LABEL: t2_extrause:
6666
; X64: # %bb.0:
67-
; X64-NEXT: movl %edi, %eax
68-
; X64-NEXT: leal -1(%rax), %ecx
69-
; X64-NEXT: movl %ecx, (%rsi)
70-
; X64-NEXT: negl %eax
71-
; X64-NEXT: # kill: def $eax killed $eax killed $rax
67+
; X64-NEXT: # kill: def $edi killed $edi def $rdi
68+
; X64-NEXT: leal -1(%rdi), %eax
69+
; X64-NEXT: movl %eax, (%rsi)
70+
; X64-NEXT: notl %eax
7271
; X64-NEXT: retq
7372
%mask = add i32 %alignment, -1
7473
store i32 %mask, ptr %mask_storage

0 commit comments

Comments
 (0)