1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+sse4.1 | FileCheck %s --check-prefixes=CHECK,SSE
3 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx2 | FileCheck %s --check-prefixes=CHECK,AVX
6 define <4 x i32> @combine_vec_add_to_zero(<4 x i32> %a) {
7 ; CHECK-LABEL: combine_vec_add_to_zero:
10 %1 = add <4 x i32> %a, zeroinitializer
14 ; fold ((c1-A)+c2) -> (c1+c2)-A
15 define <4 x i32> @combine_vec_add_constant_sub(<4 x i32> %a) {
16 ; SSE-LABEL: combine_vec_add_constant_sub:
18 ; SSE-NEXT: movdqa {{.*#+}} xmm1 = [0,2,4,6]
19 ; SSE-NEXT: psubd %xmm0, %xmm1
20 ; SSE-NEXT: movdqa %xmm1, %xmm0
23 ; AVX-LABEL: combine_vec_add_constant_sub:
25 ; AVX-NEXT: vmovdqa {{.*#+}} xmm1 = [0,2,4,6]
26 ; AVX-NEXT: vpsubd %xmm0, %xmm1, %xmm0
28 %1 = sub <4 x i32> <i32 0, i32 1, i32 2, i32 3>, %a
29 %2 = add <4 x i32> <i32 0, i32 1, i32 2, i32 3>, %1
33 ; fold ((0-A) + B) -> B-A
34 define <4 x i32> @combine_vec_add_neg0(<4 x i32> %a, <4 x i32> %b) {
35 ; SSE-LABEL: combine_vec_add_neg0:
37 ; SSE-NEXT: psubd %xmm0, %xmm1
38 ; SSE-NEXT: movdqa %xmm1, %xmm0
41 ; AVX-LABEL: combine_vec_add_neg0:
43 ; AVX-NEXT: vpsubd %xmm0, %xmm1, %xmm0
45 %1 = sub <4 x i32> zeroinitializer, %a
46 %2 = add <4 x i32> %1, %b
50 ; fold (A + (0-B)) -> A-B
51 define <4 x i32> @combine_vec_add_neg1(<4 x i32> %a, <4 x i32> %b) {
52 ; SSE-LABEL: combine_vec_add_neg1:
54 ; SSE-NEXT: psubd %xmm1, %xmm0
57 ; AVX-LABEL: combine_vec_add_neg1:
59 ; AVX-NEXT: vpsubd %xmm1, %xmm0, %xmm0
61 %1 = sub <4 x i32> zeroinitializer, %b
62 %2 = add <4 x i32> %a, %1
67 define <4 x i32> @combine_vec_add_sub0(<4 x i32> %a, <4 x i32> %b) {
68 ; SSE-LABEL: combine_vec_add_sub0:
70 ; SSE-NEXT: movaps %xmm1, %xmm0
73 ; AVX-LABEL: combine_vec_add_sub0:
75 ; AVX-NEXT: vmovaps %xmm1, %xmm0
77 %1 = sub <4 x i32> %b, %a
78 %2 = add <4 x i32> %a, %1
83 define <4 x i32> @combine_vec_add_sub1(<4 x i32> %a, <4 x i32> %b) {
84 ; SSE-LABEL: combine_vec_add_sub1:
86 ; SSE-NEXT: movaps %xmm1, %xmm0
89 ; AVX-LABEL: combine_vec_add_sub1:
91 ; AVX-NEXT: vmovaps %xmm1, %xmm0
93 %1 = sub <4 x i32> %b, %a
94 %2 = add <4 x i32> %1, %a
98 ; fold ((A-B)+(C-A)) -> (C-B)
99 define <4 x i32> @combine_vec_add_sub_sub0(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
100 ; SSE-LABEL: combine_vec_add_sub_sub0:
102 ; SSE-NEXT: movdqa %xmm2, %xmm0
103 ; SSE-NEXT: psubd %xmm1, %xmm0
106 ; AVX-LABEL: combine_vec_add_sub_sub0:
108 ; AVX-NEXT: vpsubd %xmm1, %xmm2, %xmm0
110 %1 = sub <4 x i32> %a, %b
111 %2 = sub <4 x i32> %c, %a
112 %3 = add <4 x i32> %1, %2
116 ; fold ((A-B)+(B-C)) -> (A-C)
117 define <4 x i32> @combine_vec_add_sub_sub1(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
118 ; SSE-LABEL: combine_vec_add_sub_sub1:
120 ; SSE-NEXT: psubd %xmm2, %xmm0
123 ; AVX-LABEL: combine_vec_add_sub_sub1:
125 ; AVX-NEXT: vpsubd %xmm2, %xmm0, %xmm0
127 %1 = sub <4 x i32> %a, %b
128 %2 = sub <4 x i32> %b, %c
129 %3 = add <4 x i32> %1, %2
133 ; fold (A+(B-(A+C))) to (B-C)
134 define <4 x i32> @combine_vec_add_sub_add0(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
135 ; SSE-LABEL: combine_vec_add_sub_add0:
137 ; SSE-NEXT: movdqa %xmm1, %xmm0
138 ; SSE-NEXT: psubd %xmm2, %xmm0
141 ; AVX-LABEL: combine_vec_add_sub_add0:
143 ; AVX-NEXT: vpsubd %xmm2, %xmm1, %xmm0
145 %1 = add <4 x i32> %a, %c
146 %2 = sub <4 x i32> %b, %1
147 %3 = add <4 x i32> %a, %2
151 ; fold (A+(B-(C+A))) to (B-C)
152 define <4 x i32> @combine_vec_add_sub_add1(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
153 ; SSE-LABEL: combine_vec_add_sub_add1:
155 ; SSE-NEXT: movdqa %xmm1, %xmm0
156 ; SSE-NEXT: psubd %xmm2, %xmm0
159 ; AVX-LABEL: combine_vec_add_sub_add1:
161 ; AVX-NEXT: vpsubd %xmm2, %xmm1, %xmm0
163 %1 = add <4 x i32> %c, %a
164 %2 = sub <4 x i32> %b, %1
165 %3 = add <4 x i32> %a, %2
169 ; fold (A+((B-A)+C)) to (B+C)
170 define <4 x i32> @combine_vec_add_sub_add2(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
171 ; SSE-LABEL: combine_vec_add_sub_add2:
173 ; SSE-NEXT: movdqa %xmm1, %xmm0
174 ; SSE-NEXT: paddd %xmm2, %xmm0
177 ; AVX-LABEL: combine_vec_add_sub_add2:
179 ; AVX-NEXT: vpaddd %xmm2, %xmm1, %xmm0
181 %1 = sub <4 x i32> %b, %a
182 %2 = add <4 x i32> %1, %c
183 %3 = add <4 x i32> %a, %2
187 ; fold (A+((B-A)-C)) to (B-C)
188 define <4 x i32> @combine_vec_add_sub_add3(<4 x i32> %a, <4 x i32> %b, <4 x i32> %c) {
189 ; SSE-LABEL: combine_vec_add_sub_add3:
191 ; SSE-NEXT: movdqa %xmm1, %xmm0
192 ; SSE-NEXT: psubd %xmm2, %xmm0
195 ; AVX-LABEL: combine_vec_add_sub_add3:
197 ; AVX-NEXT: vpsubd %xmm2, %xmm1, %xmm0
199 %1 = sub <4 x i32> %b, %a
200 %2 = sub <4 x i32> %1, %c
201 %3 = add <4 x i32> %a, %2
205 ; fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
206 define <4 x i32> @combine_vec_add_sub_sub(<4 x i32> %a, <4 x i32> %b, <4 x i32> %d) {
207 ; SSE-LABEL: combine_vec_add_sub_sub:
209 ; SSE-NEXT: paddd %xmm2, %xmm1
210 ; SSE-NEXT: psubd %xmm1, %xmm0
211 ; SSE-NEXT: paddd {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
214 ; AVX-LABEL: combine_vec_add_sub_sub:
216 ; AVX-NEXT: vpaddd %xmm2, %xmm1, %xmm1
217 ; AVX-NEXT: vpsubd %xmm1, %xmm0, %xmm0
218 ; AVX-NEXT: vpaddd {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0, %xmm0
220 %1 = sub <4 x i32> %a, %b
221 %2 = sub <4 x i32> <i32 0, i32 1, i32 2, i32 3>, %d
222 %3 = add <4 x i32> %1, %2
226 ; fold (a+b) -> (a|b) iff a and b share no bits.
227 define <4 x i32> @combine_vec_add_uniquebits(<4 x i32> %a, <4 x i32> %b) {
228 ; SSE-LABEL: combine_vec_add_uniquebits:
230 ; SSE-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
231 ; SSE-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
232 ; SSE-NEXT: orps %xmm1, %xmm0
235 ; AVX-LABEL: combine_vec_add_uniquebits:
237 ; AVX-NEXT: vbroadcastss {{.*#+}} xmm2 = [61680,61680,61680,61680]
238 ; AVX-NEXT: vandps %xmm2, %xmm0, %xmm0
239 ; AVX-NEXT: vbroadcastss {{.*#+}} xmm2 = [3855,3855,3855,3855]
240 ; AVX-NEXT: vandps %xmm2, %xmm1, %xmm1
241 ; AVX-NEXT: vorps %xmm1, %xmm0, %xmm0
243 %1 = and <4 x i32> %a, <i32 61680, i32 61680, i32 61680, i32 61680>
244 %2 = and <4 x i32> %b, <i32 3855, i32 3855, i32 3855, i32 3855>
245 %3 = add <4 x i32> %1, %2
249 ; fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
250 define <4 x i32> @combine_vec_add_shl_neg0(<4 x i32> %x, <4 x i32> %y) {
251 ; SSE-LABEL: combine_vec_add_shl_neg0:
253 ; SSE-NEXT: pslld $5, %xmm1
254 ; SSE-NEXT: psubd %xmm1, %xmm0
257 ; AVX-LABEL: combine_vec_add_shl_neg0:
259 ; AVX-NEXT: vpslld $5, %xmm1, %xmm1
260 ; AVX-NEXT: vpsubd %xmm1, %xmm0, %xmm0
262 %1 = sub <4 x i32> zeroinitializer, %y
263 %2 = shl <4 x i32> %1, <i32 5, i32 5, i32 5, i32 5>
264 %3 = add <4 x i32> %x, %2
268 ; fold (add shl(0 - y, n), x) -> sub(x, shl(y, n))
269 define <4 x i32> @combine_vec_add_shl_neg1(<4 x i32> %x, <4 x i32> %y) {
270 ; SSE-LABEL: combine_vec_add_shl_neg1:
272 ; SSE-NEXT: pslld $5, %xmm1
273 ; SSE-NEXT: psubd %xmm1, %xmm0
276 ; AVX-LABEL: combine_vec_add_shl_neg1:
278 ; AVX-NEXT: vpslld $5, %xmm1, %xmm1
279 ; AVX-NEXT: vpsubd %xmm1, %xmm0, %xmm0
281 %1 = sub <4 x i32> zeroinitializer, %y
282 %2 = shl <4 x i32> %1, <i32 5, i32 5, i32 5, i32 5>
283 %3 = add <4 x i32> %2, %x
287 ; (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
288 ; and similar xforms where the inner op is either ~0 or 0.
289 define <4 x i32> @combine_vec_add_and_compare(<4 x i32> %a0, <4 x i32> %a1, <4 x i32> %a2) {
290 ; SSE-LABEL: combine_vec_add_and_compare:
292 ; SSE-NEXT: pcmpeqd %xmm2, %xmm1
293 ; SSE-NEXT: psubd %xmm1, %xmm0
296 ; AVX-LABEL: combine_vec_add_and_compare:
298 ; AVX-NEXT: vpcmpeqd %xmm2, %xmm1, %xmm1
299 ; AVX-NEXT: vpsubd %xmm1, %xmm0, %xmm0
301 %1 = icmp eq <4 x i32> %a1, %a2
302 %2 = sext <4 x i1> %1 to <4 x i32>
303 %3 = and <4 x i32> %2, <i32 1, i32 1, i32 1, i32 1>
304 %4 = add <4 x i32> %a0, %3
308 ; add (sext i1), X -> sub X, (zext i1)
309 define <4 x i32> @combine_vec_add_sext(<4 x i1> %a0, <4 x i32> %a1) {
310 ; SSE-LABEL: combine_vec_add_sext:
312 ; SSE-NEXT: pslld $31, %xmm0
313 ; SSE-NEXT: psrad $31, %xmm0
314 ; SSE-NEXT: paddd %xmm1, %xmm0
317 ; AVX-LABEL: combine_vec_add_sext:
319 ; AVX-NEXT: vpslld $31, %xmm0, %xmm0
320 ; AVX-NEXT: vpsrad $31, %xmm0, %xmm0
321 ; AVX-NEXT: vpaddd %xmm1, %xmm0, %xmm0
323 %1 = sext <4 x i1> %a0 to <4 x i32>
324 %2 = add <4 x i32> %1, %a1
328 ; add (sext i1), X -> sub X, (zext i1)
329 define <4 x i32> @combine_vec_add_sextinreg(<4 x i32> %a0, <4 x i32> %a1) {
330 ; SSE-LABEL: combine_vec_add_sextinreg:
332 ; SSE-NEXT: pslld $31, %xmm0
333 ; SSE-NEXT: psrad $31, %xmm0
334 ; SSE-NEXT: paddd %xmm1, %xmm0
337 ; AVX-LABEL: combine_vec_add_sextinreg:
339 ; AVX-NEXT: vpslld $31, %xmm0, %xmm0
340 ; AVX-NEXT: vpsrad $31, %xmm0, %xmm0
341 ; AVX-NEXT: vpaddd %xmm1, %xmm0, %xmm0
343 %1 = shl <4 x i32> %a0, <i32 31, i32 31, i32 31, i32 31>
344 %2 = ashr <4 x i32> %1, <i32 31, i32 31, i32 31, i32 31>
345 %3 = add <4 x i32> %2, %a1
349 ; (add (add (xor a, -1), b), 1) -> (sub b, a)
350 define i32 @combine_add_add_not(i32 %a, i32 %b) {
351 ; CHECK-LABEL: combine_add_add_not:
353 ; CHECK-NEXT: movl %esi, %eax
354 ; CHECK-NEXT: subl %edi, %eax
356 %nota = xor i32 %a, -1
357 %add = add i32 %nota, %b
362 define <4 x i32> @combine_vec_add_add_not(<4 x i32> %a, <4 x i32> %b) {
363 ; SSE-LABEL: combine_vec_add_add_not:
365 ; SSE-NEXT: psubd %xmm0, %xmm1
366 ; SSE-NEXT: movdqa %xmm1, %xmm0
369 ; AVX-LABEL: combine_vec_add_add_not:
371 ; AVX-NEXT: vpsubd %xmm0, %xmm1, %xmm0
373 %nota = xor <4 x i32> %a, <i32 -1, i32 -1, i32 -1, i32 -1>
374 %add = add <4 x i32> %nota, %b
375 %r = add <4 x i32> %add, <i32 1, i32 1, i32 1, i32 1>
379 declare {i32, i1} @llvm.sadd.with.overflow.i32(i32 %a, i32 %b)
381 define i1 @sadd_add(i32 %a, i32 %b, i32* %p) {
382 ; CHECK-LABEL: sadd_add:
384 ; CHECK-NEXT: notl %edi
385 ; CHECK-NEXT: addl %esi, %edi
386 ; CHECK-NEXT: seto %al
387 ; CHECK-NEXT: incl %edi
388 ; CHECK-NEXT: movl %edi, (%rdx)
390 %nota = xor i32 %a, -1
391 %a0 = call {i32, i1} @llvm.sadd.with.overflow.i32(i32 %nota, i32 %b)
392 %e0 = extractvalue {i32, i1} %a0, 0
393 %e1 = extractvalue {i32, i1} %a0, 1
394 %res = add i32 %e0, 1
395 store i32 %res, i32* %p
399 declare {i8, i1} @llvm.uadd.with.overflow.i8(i8 %a, i8 %b)
401 define i1 @uadd_add(i8 %a, i8 %b, i8* %p) {
402 ; CHECK-LABEL: uadd_add:
404 ; CHECK-NEXT: notb %dil
405 ; CHECK-NEXT: addb %sil, %dil
406 ; CHECK-NEXT: setb %al
407 ; CHECK-NEXT: incb %dil
408 ; CHECK-NEXT: movb %dil, (%rdx)
410 %nota = xor i8 %a, -1
411 %a0 = call {i8, i1} @llvm.uadd.with.overflow.i8(i8 %nota, i8 %b)
412 %e0 = extractvalue {i8, i1} %a0, 0
413 %e1 = extractvalue {i8, i1} %a0, 1
415 store i8 %res, i8* %p
419 ; This would crash because we tried to transform an add-with-overflow
420 ; based on the wrong result value.
422 define i1 @PR51238(i1 %b, i8 %x, i8 %y, i8 %z) {
423 ; CHECK-LABEL: PR51238:
425 ; CHECK-NEXT: notb %cl
426 ; CHECK-NEXT: addb %dl, %cl
427 ; CHECK-NEXT: movb $1, %al
428 ; CHECK-NEXT: adcb $0, %al
432 %minxz = select i1 %b, i8 %x, i8 %nz
433 %cmpyz = icmp ult i8 %ny, %nz
434 %r = add i1 %cmpyz, true