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-prefix=CHECK --check-prefix=SSE
3 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx | FileCheck %s --check-prefix=CHECK --check-prefix=AVX --check-prefix=AVX1
4 ; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+avx2 | FileCheck %s --check-prefix=CHECK --check-prefix=AVX --check-prefix=AVX2
6 ; fold (srem x, 1) -> 0
7 define i32 @combine_srem_by_one(i32 %x) {
8 ; CHECK-LABEL: combine_srem_by_one:
10 ; CHECK-NEXT: xorl %eax, %eax
16 define <4 x i32> @combine_vec_srem_by_one(<4 x i32> %x) {
17 ; SSE-LABEL: combine_vec_srem_by_one:
19 ; SSE-NEXT: xorps %xmm0, %xmm0
22 ; AVX-LABEL: combine_vec_srem_by_one:
24 ; AVX-NEXT: vxorps %xmm0, %xmm0, %xmm0
26 %1 = srem <4 x i32> %x, <i32 1, i32 1, i32 1, i32 1>
30 ; fold (srem x, -1) -> 0
31 define i32 @combine_srem_by_negone(i32 %x) {
32 ; CHECK-LABEL: combine_srem_by_negone:
34 ; CHECK-NEXT: xorl %eax, %eax
40 define <4 x i32> @combine_vec_srem_by_negone(<4 x i32> %x) {
41 ; SSE-LABEL: combine_vec_srem_by_negone:
43 ; SSE-NEXT: xorps %xmm0, %xmm0
46 ; AVX-LABEL: combine_vec_srem_by_negone:
48 ; AVX-NEXT: vxorps %xmm0, %xmm0, %xmm0
50 %1 = srem <4 x i32> %x, <i32 -1, i32 -1, i32 -1, i32 -1>
54 ; TODO fold (srem x, INT_MIN)
55 define i32 @combine_srem_by_minsigned(i32 %x) {
56 ; CHECK-LABEL: combine_srem_by_minsigned:
58 ; CHECK-NEXT: # kill: def $edi killed $edi def $rdi
59 ; CHECK-NEXT: movl %edi, %eax
60 ; CHECK-NEXT: sarl $31, %eax
61 ; CHECK-NEXT: shrl %eax
62 ; CHECK-NEXT: addl %edi, %eax
63 ; CHECK-NEXT: andl $-2147483648, %eax # imm = 0x80000000
64 ; CHECK-NEXT: leal (%rax,%rdi), %eax
66 %1 = srem i32 %x, -2147483648
70 define <4 x i32> @combine_vec_srem_by_minsigned(<4 x i32> %x) {
71 ; SSE-LABEL: combine_vec_srem_by_minsigned:
73 ; SSE-NEXT: movdqa %xmm0, %xmm1
74 ; SSE-NEXT: psrad $31, %xmm1
75 ; SSE-NEXT: psrld $1, %xmm1
76 ; SSE-NEXT: paddd %xmm0, %xmm1
77 ; SSE-NEXT: pand {{.*}}(%rip), %xmm1
78 ; SSE-NEXT: psubd %xmm1, %xmm0
81 ; AVX1-LABEL: combine_vec_srem_by_minsigned:
83 ; AVX1-NEXT: vpsrad $31, %xmm0, %xmm1
84 ; AVX1-NEXT: vpsrld $1, %xmm1, %xmm1
85 ; AVX1-NEXT: vpaddd %xmm1, %xmm0, %xmm1
86 ; AVX1-NEXT: vpand {{.*}}(%rip), %xmm1, %xmm1
87 ; AVX1-NEXT: vpsubd %xmm1, %xmm0, %xmm0
90 ; AVX2-LABEL: combine_vec_srem_by_minsigned:
92 ; AVX2-NEXT: vpsrad $31, %xmm0, %xmm1
93 ; AVX2-NEXT: vpsrld $1, %xmm1, %xmm1
94 ; AVX2-NEXT: vpaddd %xmm1, %xmm0, %xmm1
95 ; AVX2-NEXT: vpbroadcastd {{.*#+}} xmm2 = [2147483648,2147483648,2147483648,2147483648]
96 ; AVX2-NEXT: vpand %xmm2, %xmm1, %xmm1
97 ; AVX2-NEXT: vpsubd %xmm1, %xmm0, %xmm0
99 %1 = srem <4 x i32> %x, <i32 -2147483648, i32 -2147483648, i32 -2147483648, i32 -2147483648>
103 ; fold (srem 0, x) -> 0
104 define i32 @combine_srem_zero(i32 %x) {
105 ; CHECK-LABEL: combine_srem_zero:
107 ; CHECK-NEXT: xorl %eax, %eax
113 define <4 x i32> @combine_vec_srem_zero(<4 x i32> %x) {
114 ; SSE-LABEL: combine_vec_srem_zero:
116 ; SSE-NEXT: xorps %xmm0, %xmm0
119 ; AVX-LABEL: combine_vec_srem_zero:
121 ; AVX-NEXT: vxorps %xmm0, %xmm0, %xmm0
123 %1 = srem <4 x i32> zeroinitializer, %x
127 ; fold (srem x, x) -> 0
128 define i32 @combine_srem_dupe(i32 %x) {
129 ; CHECK-LABEL: combine_srem_dupe:
131 ; CHECK-NEXT: xorl %eax, %eax
137 define <4 x i32> @combine_vec_srem_dupe(<4 x i32> %x) {
138 ; SSE-LABEL: combine_vec_srem_dupe:
140 ; SSE-NEXT: xorps %xmm0, %xmm0
143 ; AVX-LABEL: combine_vec_srem_dupe:
145 ; AVX-NEXT: vxorps %xmm0, %xmm0, %xmm0
147 %1 = srem <4 x i32> %x, %x
151 ; fold (srem x, y) -> (urem x, y) iff x and y are positive
152 define <4 x i32> @combine_vec_srem_by_pos0(<4 x i32> %x) {
153 ; SSE-LABEL: combine_vec_srem_by_pos0:
155 ; SSE-NEXT: andps {{.*}}(%rip), %xmm0
158 ; AVX1-LABEL: combine_vec_srem_by_pos0:
160 ; AVX1-NEXT: vandps {{.*}}(%rip), %xmm0, %xmm0
163 ; AVX2-LABEL: combine_vec_srem_by_pos0:
165 ; AVX2-NEXT: vbroadcastss {{.*#+}} xmm1 = [3,3,3,3]
166 ; AVX2-NEXT: vandps %xmm1, %xmm0, %xmm0
168 %1 = and <4 x i32> %x, <i32 255, i32 255, i32 255, i32 255>
169 %2 = srem <4 x i32> %1, <i32 4, i32 4, i32 4, i32 4>
173 define <4 x i32> @combine_vec_srem_by_pos1(<4 x i32> %x) {
174 ; SSE-LABEL: combine_vec_srem_by_pos1:
176 ; SSE-NEXT: andps {{.*}}(%rip), %xmm0
179 ; AVX-LABEL: combine_vec_srem_by_pos1:
181 ; AVX-NEXT: vandps {{.*}}(%rip), %xmm0, %xmm0
183 %1 = and <4 x i32> %x, <i32 255, i32 255, i32 255, i32 255>
184 %2 = srem <4 x i32> %1, <i32 1, i32 4, i32 8, i32 16>
188 ; fold (srem x, (1 << c)) -> x - (x / (1 << c)) * (1 << c).
189 define <4 x i32> @combine_vec_srem_by_pow2a(<4 x i32> %x) {
190 ; SSE-LABEL: combine_vec_srem_by_pow2a:
192 ; SSE-NEXT: movdqa %xmm0, %xmm1
193 ; SSE-NEXT: psrad $31, %xmm1
194 ; SSE-NEXT: psrld $30, %xmm1
195 ; SSE-NEXT: paddd %xmm0, %xmm1
196 ; SSE-NEXT: pand {{.*}}(%rip), %xmm1
197 ; SSE-NEXT: psubd %xmm1, %xmm0
200 ; AVX1-LABEL: combine_vec_srem_by_pow2a:
202 ; AVX1-NEXT: vpsrad $31, %xmm0, %xmm1
203 ; AVX1-NEXT: vpsrld $30, %xmm1, %xmm1
204 ; AVX1-NEXT: vpaddd %xmm1, %xmm0, %xmm1
205 ; AVX1-NEXT: vpand {{.*}}(%rip), %xmm1, %xmm1
206 ; AVX1-NEXT: vpsubd %xmm1, %xmm0, %xmm0
209 ; AVX2-LABEL: combine_vec_srem_by_pow2a:
211 ; AVX2-NEXT: vpsrad $31, %xmm0, %xmm1
212 ; AVX2-NEXT: vpsrld $30, %xmm1, %xmm1
213 ; AVX2-NEXT: vpaddd %xmm1, %xmm0, %xmm1
214 ; AVX2-NEXT: vpbroadcastd {{.*#+}} xmm2 = [4294967292,4294967292,4294967292,4294967292]
215 ; AVX2-NEXT: vpand %xmm2, %xmm1, %xmm1
216 ; AVX2-NEXT: vpsubd %xmm1, %xmm0, %xmm0
218 %1 = srem <4 x i32> %x, <i32 4, i32 4, i32 4, i32 4>
222 define <4 x i32> @combine_vec_srem_by_pow2a_neg(<4 x i32> %x) {
223 ; SSE-LABEL: combine_vec_srem_by_pow2a_neg:
225 ; SSE-NEXT: movdqa %xmm0, %xmm1
226 ; SSE-NEXT: psrad $31, %xmm1
227 ; SSE-NEXT: psrld $30, %xmm1
228 ; SSE-NEXT: paddd %xmm0, %xmm1
229 ; SSE-NEXT: psrld $2, %xmm1
230 ; SSE-NEXT: pxor %xmm2, %xmm2
231 ; SSE-NEXT: psubd %xmm1, %xmm2
232 ; SSE-NEXT: pslld $2, %xmm2
233 ; SSE-NEXT: paddd %xmm2, %xmm0
236 ; AVX-LABEL: combine_vec_srem_by_pow2a_neg:
238 ; AVX-NEXT: vpsrad $31, %xmm0, %xmm1
239 ; AVX-NEXT: vpsrld $30, %xmm1, %xmm1
240 ; AVX-NEXT: vpaddd %xmm1, %xmm0, %xmm1
241 ; AVX-NEXT: vpsrld $2, %xmm1, %xmm1
242 ; AVX-NEXT: vpxor %xmm2, %xmm2, %xmm2
243 ; AVX-NEXT: vpsubd %xmm1, %xmm2, %xmm1
244 ; AVX-NEXT: vpslld $2, %xmm1, %xmm1
245 ; AVX-NEXT: vpaddd %xmm1, %xmm0, %xmm0
247 %1 = srem <4 x i32> %x, <i32 -4, i32 -4, i32 -4, i32 -4>
251 define <4 x i32> @combine_vec_srem_by_pow2b(<4 x i32> %x) {
252 ; SSE-LABEL: combine_vec_srem_by_pow2b:
254 ; SSE-NEXT: movdqa %xmm0, %xmm1
255 ; SSE-NEXT: psrad $31, %xmm1
256 ; SSE-NEXT: movdqa %xmm1, %xmm2
257 ; SSE-NEXT: psrld $29, %xmm2
258 ; SSE-NEXT: movdqa %xmm1, %xmm3
259 ; SSE-NEXT: psrld $31, %xmm3
260 ; SSE-NEXT: pblendw {{.*#+}} xmm3 = xmm3[0,1,2,3],xmm2[4,5,6,7]
261 ; SSE-NEXT: psrld $30, %xmm1
262 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm1[0,1],xmm3[2,3],xmm1[4,5],xmm3[6,7]
263 ; SSE-NEXT: paddd %xmm0, %xmm1
264 ; SSE-NEXT: movdqa %xmm1, %xmm2
265 ; SSE-NEXT: psrad $3, %xmm2
266 ; SSE-NEXT: movdqa %xmm1, %xmm3
267 ; SSE-NEXT: psrad $1, %xmm3
268 ; SSE-NEXT: pblendw {{.*#+}} xmm3 = xmm3[0,1,2,3],xmm2[4,5,6,7]
269 ; SSE-NEXT: psrad $2, %xmm1
270 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm1[0,1],xmm3[2,3],xmm1[4,5],xmm3[6,7]
271 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm0[0,1],xmm1[2,3,4,5,6,7]
272 ; SSE-NEXT: pmulld {{.*}}(%rip), %xmm1
273 ; SSE-NEXT: psubd %xmm1, %xmm0
276 ; AVX1-LABEL: combine_vec_srem_by_pow2b:
278 ; AVX1-NEXT: vpsrad $31, %xmm0, %xmm1
279 ; AVX1-NEXT: vpsrld $29, %xmm1, %xmm2
280 ; AVX1-NEXT: vpsrld $31, %xmm1, %xmm3
281 ; AVX1-NEXT: vpblendw {{.*#+}} xmm2 = xmm3[0,1,2,3],xmm2[4,5,6,7]
282 ; AVX1-NEXT: vpsrld $30, %xmm1, %xmm1
283 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm1[0,1],xmm2[2,3],xmm1[4,5],xmm2[6,7]
284 ; AVX1-NEXT: vpaddd %xmm1, %xmm0, %xmm1
285 ; AVX1-NEXT: vpsrad $3, %xmm1, %xmm2
286 ; AVX1-NEXT: vpsrad $1, %xmm1, %xmm3
287 ; AVX1-NEXT: vpblendw {{.*#+}} xmm2 = xmm3[0,1,2,3],xmm2[4,5,6,7]
288 ; AVX1-NEXT: vpsrad $2, %xmm1, %xmm1
289 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm1[0,1],xmm2[2,3],xmm1[4,5],xmm2[6,7]
290 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm0[0,1],xmm1[2,3,4,5,6,7]
291 ; AVX1-NEXT: vpmulld {{.*}}(%rip), %xmm1, %xmm1
292 ; AVX1-NEXT: vpsubd %xmm1, %xmm0, %xmm0
295 ; AVX2-LABEL: combine_vec_srem_by_pow2b:
297 ; AVX2-NEXT: vpsrad $31, %xmm0, %xmm1
298 ; AVX2-NEXT: vpsrlvd {{.*}}(%rip), %xmm1, %xmm1
299 ; AVX2-NEXT: vpaddd %xmm1, %xmm0, %xmm1
300 ; AVX2-NEXT: vmovdqa {{.*#+}} xmm2 = [0,1,2,3]
301 ; AVX2-NEXT: vpsravd %xmm2, %xmm1, %xmm1
302 ; AVX2-NEXT: vpblendd {{.*#+}} xmm1 = xmm0[0],xmm1[1,2,3]
303 ; AVX2-NEXT: vpsllvd %xmm2, %xmm1, %xmm1
304 ; AVX2-NEXT: vpsubd %xmm1, %xmm0, %xmm0
306 %1 = srem <4 x i32> %x, <i32 1, i32 2, i32 4, i32 8>
310 define <4 x i32> @combine_vec_srem_by_pow2b_neg(<4 x i32> %x) {
311 ; SSE-LABEL: combine_vec_srem_by_pow2b_neg:
313 ; SSE-NEXT: movdqa %xmm0, %xmm1
314 ; SSE-NEXT: psrad $31, %xmm1
315 ; SSE-NEXT: movdqa %xmm1, %xmm2
316 ; SSE-NEXT: psrld $28, %xmm2
317 ; SSE-NEXT: movdqa %xmm1, %xmm3
318 ; SSE-NEXT: psrld $30, %xmm3
319 ; SSE-NEXT: pblendw {{.*#+}} xmm3 = xmm3[0,1,2,3],xmm2[4,5,6,7]
320 ; SSE-NEXT: movdqa %xmm1, %xmm2
321 ; SSE-NEXT: psrld $29, %xmm2
322 ; SSE-NEXT: psrld $31, %xmm1
323 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm1[0,1,2,3],xmm2[4,5,6,7]
324 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm1[0,1],xmm3[2,3],xmm1[4,5],xmm3[6,7]
325 ; SSE-NEXT: paddd %xmm0, %xmm1
326 ; SSE-NEXT: movdqa %xmm1, %xmm2
327 ; SSE-NEXT: psrad $4, %xmm2
328 ; SSE-NEXT: movdqa %xmm1, %xmm3
329 ; SSE-NEXT: psrad $2, %xmm3
330 ; SSE-NEXT: pblendw {{.*#+}} xmm3 = xmm3[0,1,2,3],xmm2[4,5,6,7]
331 ; SSE-NEXT: movdqa %xmm1, %xmm2
332 ; SSE-NEXT: psrad $3, %xmm2
333 ; SSE-NEXT: psrad $1, %xmm1
334 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm1[0,1,2,3],xmm2[4,5,6,7]
335 ; SSE-NEXT: pblendw {{.*#+}} xmm1 = xmm1[0,1],xmm3[2,3],xmm1[4,5],xmm3[6,7]
336 ; SSE-NEXT: pmulld {{.*}}(%rip), %xmm1
337 ; SSE-NEXT: paddd %xmm0, %xmm1
338 ; SSE-NEXT: movdqa %xmm1, %xmm0
341 ; AVX1-LABEL: combine_vec_srem_by_pow2b_neg:
343 ; AVX1-NEXT: vpsrad $31, %xmm0, %xmm1
344 ; AVX1-NEXT: vpsrld $28, %xmm1, %xmm2
345 ; AVX1-NEXT: vpsrld $30, %xmm1, %xmm3
346 ; AVX1-NEXT: vpblendw {{.*#+}} xmm2 = xmm3[0,1,2,3],xmm2[4,5,6,7]
347 ; AVX1-NEXT: vpsrld $29, %xmm1, %xmm3
348 ; AVX1-NEXT: vpsrld $31, %xmm1, %xmm1
349 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm1[0,1,2,3],xmm3[4,5,6,7]
350 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm1[0,1],xmm2[2,3],xmm1[4,5],xmm2[6,7]
351 ; AVX1-NEXT: vpaddd %xmm1, %xmm0, %xmm1
352 ; AVX1-NEXT: vpsrad $4, %xmm1, %xmm2
353 ; AVX1-NEXT: vpsrad $2, %xmm1, %xmm3
354 ; AVX1-NEXT: vpblendw {{.*#+}} xmm2 = xmm3[0,1,2,3],xmm2[4,5,6,7]
355 ; AVX1-NEXT: vpsrad $3, %xmm1, %xmm3
356 ; AVX1-NEXT: vpsrad $1, %xmm1, %xmm1
357 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm1[0,1,2,3],xmm3[4,5,6,7]
358 ; AVX1-NEXT: vpblendw {{.*#+}} xmm1 = xmm1[0,1],xmm2[2,3],xmm1[4,5],xmm2[6,7]
359 ; AVX1-NEXT: vpmulld {{.*}}(%rip), %xmm1, %xmm1
360 ; AVX1-NEXT: vpaddd %xmm1, %xmm0, %xmm0
363 ; AVX2-LABEL: combine_vec_srem_by_pow2b_neg:
365 ; AVX2-NEXT: vpsrad $31, %xmm0, %xmm1
366 ; AVX2-NEXT: vpsrlvd {{.*}}(%rip), %xmm1, %xmm1
367 ; AVX2-NEXT: vpaddd %xmm1, %xmm0, %xmm1
368 ; AVX2-NEXT: vpsravd {{.*}}(%rip), %xmm1, %xmm1
369 ; AVX2-NEXT: vpmulld {{.*}}(%rip), %xmm1, %xmm1
370 ; AVX2-NEXT: vpaddd %xmm1, %xmm0, %xmm0
372 %1 = srem <4 x i32> %x, <i32 -2, i32 -4, i32 -8, i32 -16>
377 ; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=6883
378 define i32 @ossfuzz6883() {
379 ; CHECK-LABEL: ossfuzz6883:
381 ; CHECK-NEXT: movl (%rax), %ecx
382 ; CHECK-NEXT: movl $2147483647, %eax # imm = 0x7FFFFFFF
383 ; CHECK-NEXT: xorl %edx, %edx
384 ; CHECK-NEXT: idivl %ecx
385 ; CHECK-NEXT: movl %eax, %esi
386 ; CHECK-NEXT: movl $2147483647, %eax # imm = 0x7FFFFFFF
387 ; CHECK-NEXT: xorl %edx, %edx
388 ; CHECK-NEXT: divl %ecx
389 ; CHECK-NEXT: movl %eax, %edi
390 ; CHECK-NEXT: movl %esi, %eax
392 ; CHECK-NEXT: idivl %edi
393 ; CHECK-NEXT: movl %edx, %esi
394 ; CHECK-NEXT: movl %ecx, %eax
396 ; CHECK-NEXT: idivl %esi
397 ; CHECK-NEXT: movl %edx, %edi
398 ; CHECK-NEXT: movl %ecx, %eax
399 ; CHECK-NEXT: xorl %edx, %edx
400 ; CHECK-NEXT: divl %esi
401 ; CHECK-NEXT: andl %edi, %eax
403 %B17 = or i32 0, 2147483647
404 %L6 = load i32, i32* undef
405 %B11 = sdiv i32 %B17, %L6
406 %B13 = udiv i32 %B17, %L6
407 %B14 = srem i32 %B11, %B13
408 %B16 = srem i32 %L6, %B14
409 %B10 = udiv i32 %L6, %B14
410 %B6 = and i32 %B16, %B10
414 define i1 @bool_srem(i1 %x, i1 %y) {
415 ; CHECK-LABEL: bool_srem:
417 ; CHECK-NEXT: xorl %eax, %eax
422 define <4 x i1> @boolvec_srem(<4 x i1> %x, <4 x i1> %y) {
423 ; SSE-LABEL: boolvec_srem:
425 ; SSE-NEXT: xorps %xmm0, %xmm0
428 ; AVX-LABEL: boolvec_srem:
430 ; AVX-NEXT: vxorps %xmm0, %xmm0, %xmm0
432 %r = srem <4 x i1> %x, %y