Skip to content

Commit f0c3c38

Browse files
author
git apple-llvm automerger
committed
Merge commit '0727e2b90c7b' from llvm.org/master into apple/master
2 parents 36bfe77 + 0727e2b commit f0c3c38

File tree

5 files changed

+138
-103
lines changed

5 files changed

+138
-103
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,31 +3104,20 @@ SDValue DAGCombiner::visitSUB(SDNode *N) {
31043104
DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(1),
31053105
N1.getOperand(0)));
31063106

3107-
// A - (A & (B - 1)) -> A & (~(B - 1)) -> A & (0 - B)
3108-
if (N1.getOpcode() == ISD::AND && N1.hasOneUse()) {
3107+
// A - (A & B) -> A & (~B)
3108+
if (N1.getOpcode() == ISD::AND) {
31093109
SDValue A = N1.getOperand(0);
3110-
SDValue BDec = N1.getOperand(1);
3110+
SDValue B = N1.getOperand(1);
31113111
if (A != N0)
3112-
std::swap(A, BDec);
3113-
if (A == N0 && BDec.getOpcode() == ISD::ADD &&
3114-
isAllOnesOrAllOnesSplat(BDec->getOperand(1))) {
3115-
SDValue B = BDec.getOperand(0);
3116-
SDValue NegB =
3117-
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), B);
3118-
return DAG.getNode(ISD::AND, DL, VT, A, NegB);
3112+
std::swap(A, B);
3113+
if (A == N0 &&
3114+
(N1.hasOneUse() || isConstantOrConstantVector(B, /*NoOpaques=*/true))) {
3115+
SDValue InvB =
3116+
DAG.getNode(ISD::XOR, DL, VT, B, DAG.getAllOnesConstant(DL, VT));
3117+
return DAG.getNode(ISD::AND, DL, VT, A, InvB);
31193118
}
31203119
}
31213120

3122-
// A - (A & C) -> A & (~C)
3123-
if (N1.getOpcode() == ISD::AND && N1.getOperand(0) == N0 &&
3124-
isConstantOrConstantVector(N1.getOperand(1), /*NoOpaques=*/true)) {
3125-
SDValue InvC =
3126-
DAG.FoldConstantArithmetic(ISD::XOR, DL, VT, N1.getOperand(1).getNode(),
3127-
DAG.getAllOnesConstant(DL, VT).getNode());
3128-
assert(InvC && "Constant folding failed");
3129-
return DAG.getNode(ISD::AND, DL, VT, N0, InvC);
3130-
}
3131-
31323121
// fold (X - (-Y * Z)) -> (X + (Y * Z))
31333122
if (N1.getOpcode() == ISD::MUL && N1.hasOneUse()) {
31343123
if (N1.getOperand(0).getOpcode() == ISD::SUB &&

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ define i32 @n8_not_lowbit_mask(i32 %ptr, i32 %alignment) nounwind {
127127
; CHECK-LABEL: n8_not_lowbit_mask:
128128
; CHECK: // %bb.0:
129129
; CHECK-NEXT: add w8, w1, #1 // =1
130-
; CHECK-NEXT: and w8, w0, w8
131-
; CHECK-NEXT: sub w0, w0, w8
130+
; CHECK-NEXT: bic w0, w0, w8
132131
; CHECK-NEXT: ret
133132
%mask = add i32 %alignment, 1 ; not -1
134133
%bias = and i32 %ptr, %mask

llvm/test/CodeGen/AArch64/sub-of-bias.ll

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
define i32 @t0_32(i32 %ptr, i32 %mask) nounwind {
1818
; CHECK-LABEL: t0_32:
1919
; CHECK: // %bb.0:
20-
; CHECK-NEXT: and w8, w0, w1
21-
; CHECK-NEXT: sub w0, w0, w8
20+
; CHECK-NEXT: bic w0, w0, w1
2221
; CHECK-NEXT: ret
2322
%bias = and i32 %ptr, %mask
2423
%r = sub i32 %ptr, %bias
@@ -27,8 +26,7 @@ define i32 @t0_32(i32 %ptr, i32 %mask) nounwind {
2726
define i64 @t1_64(i64 %ptr, i64 %mask) nounwind {
2827
; CHECK-LABEL: t1_64:
2928
; CHECK: // %bb.0:
30-
; CHECK-NEXT: and x8, x0, x1
31-
; CHECK-NEXT: sub x0, x0, x8
29+
; CHECK-NEXT: bic x0, x0, x1
3230
; CHECK-NEXT: ret
3331
%bias = and i64 %ptr, %mask
3432
%r = sub i64 %ptr, %bias
@@ -38,8 +36,7 @@ define i64 @t1_64(i64 %ptr, i64 %mask) nounwind {
3836
define i32 @t2_commutative(i32 %ptr, i32 %mask) nounwind {
3937
; CHECK-LABEL: t2_commutative:
4038
; CHECK: // %bb.0:
41-
; CHECK-NEXT: and w8, w1, w0
42-
; CHECK-NEXT: sub w0, w0, w8
39+
; CHECK-NEXT: bic w0, w0, w1
4340
; CHECK-NEXT: ret
4441
%bias = and i32 %mask, %ptr ; swapped
4542
%r = sub i32 %ptr, %bias
@@ -87,8 +84,7 @@ define i32 @n5_different_ptrs_commutative(i32 %ptr0, i32 %ptr1, i32 %mask) nounw
8784
define i32 @n6_not_lowbit_mask(i32 %ptr, i32 %mask) nounwind {
8885
; CHECK-LABEL: n6_not_lowbit_mask:
8986
; CHECK: // %bb.0:
90-
; CHECK-NEXT: and w8, w0, w1
91-
; CHECK-NEXT: sub w0, w0, w8
87+
; CHECK-NEXT: bic w0, w0, w1
9288
; CHECK-NEXT: ret
9389
%bias = and i32 %ptr, %mask
9490
%r = sub i32 %ptr, %bias

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,22 +214,34 @@ define i32 @n7_different_ptrs_commutative(i32 %ptr0, i32 %ptr1, i32 %alignment)
214214
}
215215

216216
define i32 @n8_not_lowbit_mask(i32 %ptr, i32 %alignment) nounwind {
217-
; X86-LABEL: n8_not_lowbit_mask:
218-
; X86: # %bb.0:
219-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
220-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
221-
; X86-NEXT: incl %ecx
222-
; X86-NEXT: andl %eax, %ecx
223-
; X86-NEXT: subl %ecx, %eax
224-
; X86-NEXT: retl
217+
; NOBMI-X86-LABEL: n8_not_lowbit_mask:
218+
; NOBMI-X86: # %bb.0:
219+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
220+
; NOBMI-X86-NEXT: incl %eax
221+
; NOBMI-X86-NEXT: notl %eax
222+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
223+
; NOBMI-X86-NEXT: retl
225224
;
226-
; X64-LABEL: n8_not_lowbit_mask:
227-
; X64: # %bb.0:
228-
; X64-NEXT: movl %edi, %eax
229-
; X64-NEXT: incl %esi
230-
; X64-NEXT: andl %edi, %esi
231-
; X64-NEXT: subl %esi, %eax
232-
; X64-NEXT: retq
225+
; BMI-X86-LABEL: n8_not_lowbit_mask:
226+
; BMI-X86: # %bb.0:
227+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
228+
; BMI-X86-NEXT: incl %eax
229+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %eax, %eax
230+
; BMI-X86-NEXT: retl
231+
;
232+
; NOBMI-X64-LABEL: n8_not_lowbit_mask:
233+
; NOBMI-X64: # %bb.0:
234+
; NOBMI-X64-NEXT: movl %esi, %eax
235+
; NOBMI-X64-NEXT: incl %eax
236+
; NOBMI-X64-NEXT: notl %eax
237+
; NOBMI-X64-NEXT: andl %edi, %eax
238+
; NOBMI-X64-NEXT: retq
239+
;
240+
; BMI-X64-LABEL: n8_not_lowbit_mask:
241+
; BMI-X64: # %bb.0:
242+
; BMI-X64-NEXT: incl %esi
243+
; BMI-X64-NEXT: andnl %edi, %esi, %eax
244+
; BMI-X64-NEXT: retq
233245
%mask = add i32 %alignment, 1 ; not -1
234246
%bias = and i32 %ptr, %mask
235247
%r = sub i32 %ptr, %bias

llvm/test/CodeGen/X86/sub-of-bias.ll

Lines changed: 97 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,65 +18,94 @@
1818
; The basic positive tests
1919

2020
define i32 @t0_32(i32 %ptr, i32 %mask) nounwind {
21-
; X86-LABEL: t0_32:
22-
; X86: # %bb.0:
23-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
24-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
25-
; X86-NEXT: andl %eax, %ecx
26-
; X86-NEXT: subl %ecx, %eax
27-
; X86-NEXT: retl
21+
; NOBMI-X86-LABEL: t0_32:
22+
; NOBMI-X86: # %bb.0:
23+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
24+
; NOBMI-X86-NEXT: notl %eax
25+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
26+
; NOBMI-X86-NEXT: retl
2827
;
29-
; X64-LABEL: t0_32:
30-
; X64: # %bb.0:
31-
; X64-NEXT: movl %edi, %eax
32-
; X64-NEXT: andl %edi, %esi
33-
; X64-NEXT: subl %esi, %eax
34-
; X64-NEXT: retq
28+
; BMI-X86-LABEL: t0_32:
29+
; BMI-X86: # %bb.0:
30+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
31+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %eax, %eax
32+
; BMI-X86-NEXT: retl
33+
;
34+
; NOBMI-X64-LABEL: t0_32:
35+
; NOBMI-X64: # %bb.0:
36+
; NOBMI-X64-NEXT: movl %esi, %eax
37+
; NOBMI-X64-NEXT: notl %eax
38+
; NOBMI-X64-NEXT: andl %edi, %eax
39+
; NOBMI-X64-NEXT: retq
40+
;
41+
; BMI-X64-LABEL: t0_32:
42+
; BMI-X64: # %bb.0:
43+
; BMI-X64-NEXT: andnl %edi, %esi, %eax
44+
; BMI-X64-NEXT: retq
3545
%bias = and i32 %ptr, %mask
3646
%r = sub i32 %ptr, %bias
3747
ret i32 %r
3848
}
3949
define i64 @t1_64(i64 %ptr, i64 %mask) nounwind {
40-
; X86-LABEL: t1_64:
41-
; X86: # %bb.0:
42-
; X86-NEXT: pushl %esi
43-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
44-
; X86-NEXT: movl {{[0-9]+}}(%esp), %edx
45-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
46-
; X86-NEXT: andl %edx, %ecx
47-
; X86-NEXT: movl {{[0-9]+}}(%esp), %esi
48-
; X86-NEXT: andl %eax, %esi
49-
; X86-NEXT: subl %esi, %eax
50-
; X86-NEXT: sbbl %ecx, %edx
51-
; X86-NEXT: popl %esi
52-
; X86-NEXT: retl
50+
; NOBMI-X86-LABEL: t1_64:
51+
; NOBMI-X86: # %bb.0:
52+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
53+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %edx
54+
; NOBMI-X86-NEXT: notl %eax
55+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
56+
; NOBMI-X86-NEXT: notl %edx
57+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %edx
58+
; NOBMI-X86-NEXT: retl
5359
;
54-
; X64-LABEL: t1_64:
55-
; X64: # %bb.0:
56-
; X64-NEXT: movq %rdi, %rax
57-
; X64-NEXT: andq %rdi, %rsi
58-
; X64-NEXT: subq %rsi, %rax
59-
; X64-NEXT: retq
60+
; BMI-X86-LABEL: t1_64:
61+
; BMI-X86: # %bb.0:
62+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
63+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
64+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %eax, %eax
65+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %ecx, %edx
66+
; BMI-X86-NEXT: retl
67+
;
68+
; NOBMI-X64-LABEL: t1_64:
69+
; NOBMI-X64: # %bb.0:
70+
; NOBMI-X64-NEXT: movq %rsi, %rax
71+
; NOBMI-X64-NEXT: notq %rax
72+
; NOBMI-X64-NEXT: andq %rdi, %rax
73+
; NOBMI-X64-NEXT: retq
74+
;
75+
; BMI-X64-LABEL: t1_64:
76+
; BMI-X64: # %bb.0:
77+
; BMI-X64-NEXT: andnq %rdi, %rsi, %rax
78+
; BMI-X64-NEXT: retq
6079
%bias = and i64 %ptr, %mask
6180
%r = sub i64 %ptr, %bias
6281
ret i64 %r
6382
}
6483

6584
define i32 @t2_commutative(i32 %ptr, i32 %mask) nounwind {
66-
; X86-LABEL: t2_commutative:
67-
; X86: # %bb.0:
68-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
69-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
70-
; X86-NEXT: andl %eax, %ecx
71-
; X86-NEXT: subl %ecx, %eax
72-
; X86-NEXT: retl
85+
; NOBMI-X86-LABEL: t2_commutative:
86+
; NOBMI-X86: # %bb.0:
87+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
88+
; NOBMI-X86-NEXT: notl %eax
89+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
90+
; NOBMI-X86-NEXT: retl
7391
;
74-
; X64-LABEL: t2_commutative:
75-
; X64: # %bb.0:
76-
; X64-NEXT: movl %edi, %eax
77-
; X64-NEXT: andl %edi, %esi
78-
; X64-NEXT: subl %esi, %eax
79-
; X64-NEXT: retq
92+
; BMI-X86-LABEL: t2_commutative:
93+
; BMI-X86: # %bb.0:
94+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
95+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %eax, %eax
96+
; BMI-X86-NEXT: retl
97+
;
98+
; NOBMI-X64-LABEL: t2_commutative:
99+
; NOBMI-X64: # %bb.0:
100+
; NOBMI-X64-NEXT: movl %esi, %eax
101+
; NOBMI-X64-NEXT: notl %eax
102+
; NOBMI-X64-NEXT: andl %edi, %eax
103+
; NOBMI-X64-NEXT: retq
104+
;
105+
; BMI-X64-LABEL: t2_commutative:
106+
; BMI-X64: # %bb.0:
107+
; BMI-X64-NEXT: andnl %edi, %esi, %eax
108+
; BMI-X64-NEXT: retq
80109
%bias = and i32 %mask, %ptr ; swapped
81110
%r = sub i32 %ptr, %bias
82111
ret i32 %r
@@ -150,20 +179,30 @@ define i32 @n5_different_ptrs_commutative(i32 %ptr0, i32 %ptr1, i32 %mask) nounw
150179
}
151180

152181
define i32 @n6_not_lowbit_mask(i32 %ptr, i32 %mask) nounwind {
153-
; X86-LABEL: n6_not_lowbit_mask:
154-
; X86: # %bb.0:
155-
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
156-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
157-
; X86-NEXT: andl %eax, %ecx
158-
; X86-NEXT: subl %ecx, %eax
159-
; X86-NEXT: retl
182+
; NOBMI-X86-LABEL: n6_not_lowbit_mask:
183+
; NOBMI-X86: # %bb.0:
184+
; NOBMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
185+
; NOBMI-X86-NEXT: notl %eax
186+
; NOBMI-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
187+
; NOBMI-X86-NEXT: retl
160188
;
161-
; X64-LABEL: n6_not_lowbit_mask:
162-
; X64: # %bb.0:
163-
; X64-NEXT: movl %edi, %eax
164-
; X64-NEXT: andl %edi, %esi
165-
; X64-NEXT: subl %esi, %eax
166-
; X64-NEXT: retq
189+
; BMI-X86-LABEL: n6_not_lowbit_mask:
190+
; BMI-X86: # %bb.0:
191+
; BMI-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
192+
; BMI-X86-NEXT: andnl {{[0-9]+}}(%esp), %eax, %eax
193+
; BMI-X86-NEXT: retl
194+
;
195+
; NOBMI-X64-LABEL: n6_not_lowbit_mask:
196+
; NOBMI-X64: # %bb.0:
197+
; NOBMI-X64-NEXT: movl %esi, %eax
198+
; NOBMI-X64-NEXT: notl %eax
199+
; NOBMI-X64-NEXT: andl %edi, %eax
200+
; NOBMI-X64-NEXT: retq
201+
;
202+
; BMI-X64-LABEL: n6_not_lowbit_mask:
203+
; BMI-X64: # %bb.0:
204+
; BMI-X64-NEXT: andnl %edi, %esi, %eax
205+
; BMI-X64-NEXT: retq
167206
%bias = and i32 %ptr, %mask
168207
%r = sub i32 %ptr, %bias
169208
ret i32 %r

0 commit comments

Comments
 (0)