1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; This test makes sure that div instructions are properly eliminated.
4 ; RUN: opt < %s -instcombine -S | FileCheck %s
6 define i32 @test1(i32 %A) {
8 ; CHECK-NEXT: ret i32 [[A:%.*]]
14 define i32 @test2(i32 %A) {
15 ; CHECK-LABEL: @test2(
16 ; CHECK-NEXT: [[B:%.*]] = lshr i32 [[A:%.*]], 3
17 ; CHECK-NEXT: ret i32 [[B]]
23 define i32 @sdiv_by_minus1(i32 %A) {
24 ; CHECK-LABEL: @sdiv_by_minus1(
25 ; CHECK-NEXT: [[B:%.*]] = sub i32 0, [[A:%.*]]
26 ; CHECK-NEXT: ret i32 [[B]]
32 define <2 x i64> @sdiv_by_minus1_vec(<2 x i64> %x) {
33 ; CHECK-LABEL: @sdiv_by_minus1_vec(
34 ; CHECK-NEXT: [[DIV:%.*]] = sub <2 x i64> zeroinitializer, [[X:%.*]]
35 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
37 %div = sdiv <2 x i64> %x, <i64 -1, i64 -1>
41 define <2 x i64> @sdiv_by_minus1_vec_undef_elt(<2 x i64> %x) {
42 ; CHECK-LABEL: @sdiv_by_minus1_vec_undef_elt(
43 ; CHECK-NEXT: ret <2 x i64> undef
45 %div = sdiv <2 x i64> %x, <i64 -1, i64 undef>
49 define i32 @sdiv_by_sext_minus1(i1 %x, i32 %y) {
50 ; CHECK-LABEL: @sdiv_by_sext_minus1(
51 ; CHECK-NEXT: [[DIV:%.*]] = sub i32 0, [[Y:%.*]]
52 ; CHECK-NEXT: ret i32 [[DIV]]
54 %sext = sext i1 %x to i32
55 %div = sdiv i32 %y, %sext
59 define <2 x i32> @sdiv_by_sext_minus1_vec(<2 x i1> %x, <2 x i32> %y) {
60 ; CHECK-LABEL: @sdiv_by_sext_minus1_vec(
61 ; CHECK-NEXT: [[DIV:%.*]] = sub <2 x i32> zeroinitializer, [[Y:%.*]]
62 ; CHECK-NEXT: ret <2 x i32> [[DIV]]
64 %sext = sext <2 x i1> %x to <2 x i32>
65 %div = sdiv <2 x i32> %y, %sext
69 define i8 @udiv_by_negative(i8 %x) {
70 ; CHECK-LABEL: @udiv_by_negative(
71 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i8 [[X:%.*]], -7
72 ; CHECK-NEXT: [[A:%.*]] = zext i1 [[TMP1]] to i8
73 ; CHECK-NEXT: ret i8 [[A]]
79 define i32 @udiv_by_minus1(i32 %A) {
80 ; CHECK-LABEL: @udiv_by_minus1(
81 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[A:%.*]], -1
82 ; CHECK-NEXT: [[B:%.*]] = zext i1 [[TMP1]] to i32
83 ; CHECK-NEXT: ret i32 [[B]]
89 define <2 x i64> @udiv_by_minus1_vec(<2 x i64> %x) {
90 ; CHECK-LABEL: @udiv_by_minus1_vec(
91 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i64> [[X:%.*]], <i64 -1, i64 -1>
92 ; CHECK-NEXT: [[DIV:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i64>
93 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
95 %div = udiv <2 x i64> %x, <i64 -1, i64 -1>
99 define i32 @udiv_by_sext_all_ones(i1 %x, i32 %y) {
100 ; CHECK-LABEL: @udiv_by_sext_all_ones(
101 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[Y:%.*]], -1
102 ; CHECK-NEXT: [[DIV:%.*]] = zext i1 [[TMP1]] to i32
103 ; CHECK-NEXT: ret i32 [[DIV]]
105 %sext = sext i1 %x to i32
106 %div = udiv i32 %y, %sext
110 define <2 x i32> @udiv_by_sext_all_ones_vec(<2 x i1> %x, <2 x i32> %y) {
111 ; CHECK-LABEL: @udiv_by_sext_all_ones_vec(
112 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[Y:%.*]], <i32 -1, i32 -1>
113 ; CHECK-NEXT: [[DIV:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i32>
114 ; CHECK-NEXT: ret <2 x i32> [[DIV]]
116 %sext = sext <2 x i1> %x to <2 x i32>
117 %div = udiv <2 x i32> %y, %sext
121 define i32 @test5(i32 %A) {
122 ; CHECK-LABEL: @test5(
123 ; CHECK-NEXT: ret i32 0
125 %B = udiv i32 %A, -16
130 define i1 @test6(i32 %A) {
131 ; CHECK-LABEL: @test6(
132 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[A:%.*]], 123
133 ; CHECK-NEXT: ret i1 [[TMP1]]
135 %B = udiv i32 %A, 123
137 %C = icmp eq i32 %B, 0
141 define i1 @test7(i32 %A) {
142 ; CHECK-LABEL: @test7(
143 ; CHECK-NEXT: [[A_OFF:%.*]] = add i32 [[A:%.*]], -20
144 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[A_OFF]], 10
145 ; CHECK-NEXT: ret i1 [[TMP1]]
149 %C = icmp eq i32 %B, 2
153 define <2 x i1> @test7vec(<2 x i32> %A) {
154 ; CHECK-LABEL: @test7vec(
155 ; CHECK-NEXT: [[A_OFF:%.*]] = add <2 x i32> [[A:%.*]], <i32 -20, i32 -20>
156 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult <2 x i32> [[A_OFF]], <i32 10, i32 10>
157 ; CHECK-NEXT: ret <2 x i1> [[TMP1]]
159 %B = udiv <2 x i32> %A, <i32 10, i32 10>
160 %C = icmp eq <2 x i32> %B, <i32 2, i32 2>
164 define i1 @test8(i8 %A) {
165 ; CHECK-LABEL: @test8(
166 ; CHECK-NEXT: [[C:%.*]] = icmp ugt i8 [[A:%.*]], -11
167 ; CHECK-NEXT: ret i1 [[C]]
171 %C = icmp eq i8 %B, 2
175 define <2 x i1> @test8vec(<2 x i8> %A) {
176 ; CHECK-LABEL: @test8vec(
177 ; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i8> [[A:%.*]], <i8 -11, i8 -11>
178 ; CHECK-NEXT: ret <2 x i1> [[C]]
180 %B = udiv <2 x i8> %A, <i8 123, i8 123>
181 %C = icmp eq <2 x i8> %B, <i8 2, i8 2>
185 define i1 @test9(i8 %A) {
186 ; CHECK-LABEL: @test9(
187 ; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[A:%.*]], -10
188 ; CHECK-NEXT: ret i1 [[C]]
192 %C = icmp ne i8 %B, 2
196 define <2 x i1> @test9vec(<2 x i8> %A) {
197 ; CHECK-LABEL: @test9vec(
198 ; CHECK-NEXT: [[C:%.*]] = icmp ult <2 x i8> [[A:%.*]], <i8 -10, i8 -10>
199 ; CHECK-NEXT: ret <2 x i1> [[C]]
201 %B = udiv <2 x i8> %A, <i8 123, i8 123>
202 %C = icmp ne <2 x i8> %B, <i8 2, i8 2>
206 define i32 @test10(i32 %X, i1 %C) {
207 ; CHECK-LABEL: @test10(
208 ; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i32 6, i32 3
209 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[X:%.*]], [[R_V]]
210 ; CHECK-NEXT: ret i32 [[R]]
212 %V = select i1 %C, i32 64, i32 8
217 define i32 @test11(i32 %X, i1 %C) {
218 ; CHECK-LABEL: @test11(
219 ; CHECK-NEXT: [[B_V:%.*]] = select i1 [[C:%.*]], i32 10, i32 5
220 ; CHECK-NEXT: [[B:%.*]] = lshr i32 [[X:%.*]], [[B_V]]
221 ; CHECK-NEXT: ret i32 [[B]]
223 %A = select i1 %C, i32 1024, i32 32
229 define i32 @test12(i32 %x) {
230 ; CHECK-LABEL: @test12(
231 ; CHECK-NEXT: ret i32 1
233 %tmp3 = udiv i32 %x, %x ; 1
237 define i32 @test13(i32 %x) {
238 ; CHECK-LABEL: @test13(
239 ; CHECK-NEXT: ret i32 1
241 %tmp3 = sdiv i32 %x, %x ; 1
245 define i32 @test14(i8 %x) {
246 ; CHECK-LABEL: @test14(
247 ; CHECK-NEXT: ret i32 0
249 %zext = zext i8 %x to i32
250 %div = udiv i32 %zext, 257 ; 0
255 define i32 @test15(i32 %a, i32 %b) {
256 ; CHECK-LABEL: @test15(
257 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[B:%.*]], -2
258 ; CHECK-NEXT: [[DIV2:%.*]] = lshr i32 [[A:%.*]], [[TMP1]]
259 ; CHECK-NEXT: ret i32 [[DIV2]]
262 %div = lshr i32 %shl, 2
263 %div2 = udiv i32 %a, %div
267 define <2 x i64> @test16(<2 x i64> %x) {
268 ; CHECK-LABEL: @test16(
269 ; CHECK-NEXT: [[DIV:%.*]] = udiv <2 x i64> [[X:%.*]], <i64 192, i64 192>
270 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
272 %shr = lshr <2 x i64> %x, <i64 5, i64 5>
273 %div = udiv <2 x i64> %shr, <i64 6, i64 6>
277 define i32 @test19(i32 %x) {
278 ; CHECK-LABEL: @test19(
279 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 1
280 ; CHECK-NEXT: [[A:%.*]] = zext i1 [[TMP1]] to i32
281 ; CHECK-NEXT: ret i32 [[A]]
287 define <2 x i32> @test19vec(<2 x i32> %x) {
288 ; CHECK-LABEL: @test19vec(
289 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 1, i32 1>
290 ; CHECK-NEXT: [[A:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i32>
291 ; CHECK-NEXT: ret <2 x i32> [[A]]
293 %A = udiv <2 x i32> <i32 1, i32 1>, %x
297 define i32 @test20(i32 %x) {
298 ; CHECK-LABEL: @test20(
299 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], 1
300 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
301 ; CHECK-NEXT: [[A:%.*]] = select i1 [[TMP2]], i32 [[X]], i32 0
302 ; CHECK-NEXT: ret i32 [[A]]
308 define <2 x i32> @test20vec(<2 x i32> %x) {
309 ; CHECK-LABEL: @test20vec(
310 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> [[X:%.*]], <i32 1, i32 1>
311 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult <2 x i32> [[TMP1]], <i32 3, i32 3>
312 ; CHECK-NEXT: [[A:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> [[X]], <2 x i32> zeroinitializer
313 ; CHECK-NEXT: ret <2 x i32> [[A]]
315 %A = sdiv <2 x i32> <i32 1, i32 1>, %x
319 define i32 @test21(i32 %a) {
320 ; CHECK-LABEL: @test21(
321 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A:%.*]], 3
322 ; CHECK-NEXT: ret i32 [[DIV]]
324 %shl = shl nsw i32 %a, 2
325 %div = sdiv i32 %shl, 12
329 define i32 @test22(i32 %a) {
330 ; CHECK-LABEL: @test22(
331 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A:%.*]], 4
332 ; CHECK-NEXT: ret i32 [[DIV]]
334 %mul = mul nsw i32 %a, 3
335 %div = sdiv i32 %mul, 12
339 define i32 @test23(i32 %a) {
340 ; CHECK-LABEL: @test23(
341 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[A:%.*]], 3
342 ; CHECK-NEXT: ret i32 [[DIV]]
344 %shl = shl nuw i32 %a, 2
345 %div = udiv i32 %shl, 12
349 define i32 @test24(i32 %a) {
350 ; CHECK-LABEL: @test24(
351 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[A:%.*]], 2
352 ; CHECK-NEXT: ret i32 [[DIV]]
354 %mul = mul nuw i32 %a, 3
355 %div = udiv i32 %mul, 12
359 define i32 @test25(i32 %a) {
360 ; CHECK-LABEL: @test25(
361 ; CHECK-NEXT: [[DIV:%.*]] = shl nsw i32 [[A:%.*]], 1
362 ; CHECK-NEXT: ret i32 [[DIV]]
364 %shl = shl nsw i32 %a, 2
365 %div = sdiv i32 %shl, 2
369 define i32 @test26(i32 %a) {
370 ; CHECK-LABEL: @test26(
371 ; CHECK-NEXT: [[DIV:%.*]] = shl nsw i32 [[A:%.*]], 2
372 ; CHECK-NEXT: ret i32 [[DIV]]
374 %mul = mul nsw i32 %a, 12
375 %div = sdiv i32 %mul, 3
379 define i32 @test27(i32 %a) {
380 ; CHECK-LABEL: @test27(
381 ; CHECK-NEXT: [[DIV:%.*]] = shl nuw i32 [[A:%.*]], 1
382 ; CHECK-NEXT: ret i32 [[DIV]]
384 %shl = shl nuw i32 %a, 2
385 %div = udiv i32 %shl, 2
389 define i32 @test28(i32 %a) {
390 ; CHECK-LABEL: @test28(
391 ; CHECK-NEXT: [[DIV:%.*]] = mul nuw i32 [[A:%.*]], 12
392 ; CHECK-NEXT: ret i32 [[DIV]]
394 %mul = mul nuw i32 %a, 36
395 %div = udiv i32 %mul, 3
399 define i32 @test29(i32 %a) {
400 ; CHECK-LABEL: @test29(
401 ; CHECK-NEXT: [[MUL_LOBIT:%.*]] = and i32 [[A:%.*]], 1
402 ; CHECK-NEXT: ret i32 [[MUL_LOBIT]]
404 %mul = shl nsw i32 %a, 31
405 %div = sdiv i32 %mul, -2147483648
409 define i32 @test30(i32 %a) {
410 ; CHECK-LABEL: @test30(
411 ; CHECK-NEXT: ret i32 [[A:%.*]]
413 %mul = shl nuw i32 %a, 31
414 %div = udiv i32 %mul, -2147483648
418 define <2 x i32> @test31(<2 x i32> %x) {
419 ; CHECK-LABEL: @test31(
420 ; CHECK-NEXT: ret <2 x i32> zeroinitializer
422 %shr = lshr <2 x i32> %x, <i32 31, i32 31>
423 %div = udiv <2 x i32> %shr, <i32 2147483647, i32 2147483647>
427 define i32 @test32(i32 %a, i32 %b) {
428 ; CHECK-LABEL: @test32(
429 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[B:%.*]]
430 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL]], 2
431 ; CHECK-NEXT: [[DIV2:%.*]] = udiv i32 [[A:%.*]], [[DIV]]
432 ; CHECK-NEXT: ret i32 [[DIV2]]
435 %div = lshr i32 %shl, 2
436 %div2 = udiv i32 %a, %div
440 define <2 x i64> @test33(<2 x i64> %x) {
441 ; CHECK-LABEL: @test33(
442 ; CHECK-NEXT: [[DIV:%.*]] = udiv exact <2 x i64> [[X:%.*]], <i64 192, i64 192>
443 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
445 %shr = lshr exact <2 x i64> %x, <i64 5, i64 5>
446 %div = udiv exact <2 x i64> %shr, <i64 6, i64 6>
450 ; -X / C --> X / -C (if negation does not overflow)
452 define i8 @sdiv_negated_dividend_constant_divisor(i8 %x) {
453 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor(
454 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[X:%.*]], 42
455 ; CHECK-NEXT: ret i8 [[D]]
457 %neg = sub nsw i8 0, %x
458 %d = sdiv i8 %neg, -42
462 define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_splat(<2 x i8> %x) {
463 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_splat(
464 ; CHECK-NEXT: [[D:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 42, i8 42>
465 ; CHECK-NEXT: ret <2 x i8> [[D]]
467 %neg = sub nsw <2 x i8> zeroinitializer, %x
468 %d = sdiv <2 x i8> %neg, <i8 -42, i8 -42>
472 define i8 @sdiv_exact_negated_dividend_constant_divisor(i8 %x) {
473 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor(
474 ; CHECK-NEXT: [[D:%.*]] = sdiv exact i8 [[X:%.*]], 42
475 ; CHECK-NEXT: ret i8 [[D]]
477 %neg = sub nsw i8 0, %x
478 %d = sdiv exact i8 %neg, -42
482 define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_splat(<2 x i8> %x) {
483 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec_splat(
484 ; CHECK-NEXT: [[D:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 42, i8 42>
485 ; CHECK-NEXT: ret <2 x i8> [[D]]
487 %neg = sub nsw <2 x i8> zeroinitializer, %x
488 %d = sdiv exact <2 x i8> %neg, <i8 -42, i8 -42>
492 define i8 @sdiv_negated_dividend_constant_divisor_smin(i8 %x) {
493 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_smin(
494 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
495 ; CHECK-NEXT: [[D:%.*]] = zext i1 [[TMP1]] to i8
496 ; CHECK-NEXT: ret i8 [[D]]
498 %neg = sub nsw i8 0, %x
499 %d = sdiv i8 %neg, -128
503 define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_splat_smin(<2 x i8> %x) {
504 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_splat_smin(
505 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
506 ; CHECK-NEXT: [[D:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
507 ; CHECK-NEXT: ret <2 x i8> [[D]]
509 %neg = sub nsw <2 x i8> zeroinitializer, %x
510 %d = sdiv <2 x i8> %neg, <i8 -128, i8 -128>
514 define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_undef(<2 x i8> %x) {
515 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_undef(
516 ; CHECK-NEXT: ret <2 x i8> undef
518 %neg = sub nsw <2 x i8> zeroinitializer, %x
519 %d = sdiv <2 x i8> %neg, <i8 -128, i8 undef>
523 define <2 x i64> @sdiv_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
524 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec(
525 ; CHECK-NEXT: [[DIV1:%.*]] = sdiv <2 x i64> [[X:%.*]], <i64 3, i64 4>
526 ; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV1]]
527 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
529 %neg = sub nsw <2 x i64> zeroinitializer, %x
530 %div = sdiv <2 x i64> %neg, <i64 3, i64 4>
534 define <2 x i64> @sdiv_exact_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
535 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec(
536 ; CHECK-NEXT: [[DIV1:%.*]] = sdiv exact <2 x i64> [[X:%.*]], <i64 3, i64 4>
537 ; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV1]]
538 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
540 %neg = sub nsw <2 x i64> zeroinitializer, %x
541 %div = sdiv exact <2 x i64> %neg, <i64 3, i64 4>
545 ; Can't negate signed min vector element.
547 define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(<2 x i8> %x) {
548 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(
549 ; CHECK-NEXT: [[DIV1:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 -128, i8 42>
550 ; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i8> zeroinitializer, [[DIV1]]
551 ; CHECK-NEXT: ret <2 x i8> [[DIV]]
553 %neg = sub nsw <2 x i8> zeroinitializer, %x
554 %div = sdiv exact <2 x i8> %neg, <i8 -128, i8 42>
558 define i32 @test35(i32 %A) {
559 ; CHECK-LABEL: @test35(
560 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], 2147483647
561 ; CHECK-NEXT: [[MUL:%.*]] = udiv exact i32 [[AND]], 2147483647
562 ; CHECK-NEXT: ret i32 [[MUL]]
564 %and = and i32 %A, 2147483647
565 %mul = sdiv exact i32 %and, 2147483647
569 define <2 x i32> @test35vec(<2 x i32> %A) {
570 ; CHECK-LABEL: @test35vec(
571 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[A:%.*]], <i32 2147483647, i32 2147483647>
572 ; CHECK-NEXT: [[MUL:%.*]] = udiv exact <2 x i32> [[AND]], <i32 2147483647, i32 2147483647>
573 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
575 %and = and <2 x i32> %A, <i32 2147483647, i32 2147483647>
576 %mul = sdiv exact <2 x i32> %and, <i32 2147483647, i32 2147483647>
580 define i32 @test36(i32 %A) {
581 ; CHECK-LABEL: @test36(
582 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], 2147483647
583 ; CHECK-NEXT: [[MUL:%.*]] = lshr exact i32 [[AND]], [[A]]
584 ; CHECK-NEXT: ret i32 [[MUL]]
586 %and = and i32 %A, 2147483647
587 %shl = shl nsw i32 1, %A
588 %mul = sdiv exact i32 %and, %shl
592 define <2 x i32> @test36vec(<2 x i32> %A) {
593 ; CHECK-LABEL: @test36vec(
594 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[A:%.*]], <i32 2147483647, i32 2147483647>
595 ; CHECK-NEXT: [[MUL:%.*]] = lshr exact <2 x i32> [[AND]], [[A]]
596 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
598 %and = and <2 x i32> %A, <i32 2147483647, i32 2147483647>
599 %shl = shl nsw <2 x i32> <i32 1, i32 1>, %A
600 %mul = sdiv exact <2 x i32> %and, %shl
604 define i32 @test37(i32* %b) {
605 ; CHECK-LABEL: @test37(
607 ; CHECK-NEXT: store i32 0, i32* [[B:%.*]], align 4
608 ; CHECK-NEXT: br i1 undef, label [[LOR_RHS:%.*]], label [[LOR_END:%.*]]
610 ; CHECK-NEXT: br label [[LOR_END]]
612 ; CHECK-NEXT: ret i32 0
615 store i32 0, i32* %b, align 4
616 %0 = load i32, i32* %b, align 4
617 br i1 undef, label %lor.rhs, label %lor.end
619 lor.rhs: ; preds = %entry
620 %mul = mul nsw i32 undef, %0
623 lor.end: ; preds = %lor.rhs, %entry
624 %t.0 = phi i32 [ %0, %entry ], [ %mul, %lor.rhs ]
625 %div = sdiv i32 %t.0, 2
629 ; We can perform the division in the smaller type.
631 define i32 @shrink(i8 %x) {
632 ; CHECK-LABEL: @shrink(
633 ; CHECK-NEXT: [[TMP1:%.*]] = sdiv i8 [[X:%.*]], 127
634 ; CHECK-NEXT: [[DIV:%.*]] = sext i8 [[TMP1]] to i32
635 ; CHECK-NEXT: ret i32 [[DIV]]
637 %conv = sext i8 %x to i32
638 %div = sdiv i32 %conv, 127
642 ; Division in the smaller type can lead to more optimizations.
644 define i32 @zap(i8 %x) {
646 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
647 ; CHECK-NEXT: [[DIV:%.*]] = zext i1 [[TMP1]] to i32
648 ; CHECK-NEXT: ret i32 [[DIV]]
650 %conv = sext i8 %x to i32
651 %div = sdiv i32 %conv, -128
655 ; Splat constant divisors should get the same folds.
657 define <3 x i32> @shrink_vec(<3 x i8> %x) {
658 ; CHECK-LABEL: @shrink_vec(
659 ; CHECK-NEXT: [[TMP1:%.*]] = sdiv <3 x i8> [[X:%.*]], <i8 127, i8 127, i8 127>
660 ; CHECK-NEXT: [[DIV:%.*]] = sext <3 x i8> [[TMP1]] to <3 x i32>
661 ; CHECK-NEXT: ret <3 x i32> [[DIV]]
663 %conv = sext <3 x i8> %x to <3 x i32>
664 %div = sdiv <3 x i32> %conv, <i32 127, i32 127, i32 127>
668 define <2 x i32> @zap_vec(<2 x i8> %x) {
669 ; CHECK-LABEL: @zap_vec(
670 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
671 ; CHECK-NEXT: [[DIV:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i32>
672 ; CHECK-NEXT: ret <2 x i32> [[DIV]]
674 %conv = sext <2 x i8> %x to <2 x i32>
675 %div = sdiv <2 x i32> %conv, <i32 -128, i32 -128>
679 ; But we can't do this if the signed constant won't fit in the original type.
681 define i32 @shrink_no(i8 %x) {
682 ; CHECK-LABEL: @shrink_no(
683 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X:%.*]] to i32
684 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[CONV]], 128
685 ; CHECK-NEXT: ret i32 [[DIV]]
687 %conv = sext i8 %x to i32
688 %div = sdiv i32 %conv, 128
692 ; When the divisor is known larger than the quotient,
693 ; InstSimplify should kill it before InstCombine sees it.
695 define i32 @shrink_no2(i8 %x) {
696 ; CHECK-LABEL: @shrink_no2(
697 ; CHECK-NEXT: ret i32 0
699 %conv = sext i8 %x to i32
700 %div = sdiv i32 %conv, -129
704 define i32 @shrink_no3(i16 %x) {
705 ; CHECK-LABEL: @shrink_no3(
706 ; CHECK-NEXT: ret i32 0
708 %conv = sext i16 %x to i32
709 %div = sdiv i32 %conv, 65535
713 ; This previously crashed when trying to simplify the zext/icmp this becomes.
714 define <2 x i8> @PR34841(<2 x i8> %x) {
715 ; CHECK-LABEL: @PR34841(
716 ; CHECK-NEXT: ret <2 x i8> zeroinitializer
718 %neg = and <2 x i8> %x, <i8 2, i8 2>
719 %div = udiv <2 x i8> <i8 1, i8 1>, %neg
723 ; X / (X * Y) -> 1 / Y if the multiplication does not overflow
725 define i8 @div_factor_signed(i8 %x, i8 %y) {
726 ; CHECK-LABEL: @div_factor_signed(
727 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1
728 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i8 [[TMP1]], 3
729 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i8 [[Y]], i8 0
730 ; CHECK-NEXT: ret i8 [[R]]
732 %a = mul nsw i8 %x, %y
737 ; X / (Y * X) -> 1 / Y if the multiplication does not overflow
739 define <2 x i8> @div_factor_signed_vec(<2 x i8> %x, <2 x i8> %y) {
740 ; CHECK-LABEL: @div_factor_signed_vec(
741 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[Y:%.*]], <i8 1, i8 1>
742 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult <2 x i8> [[TMP1]], <i8 3, i8 3>
743 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[TMP2]], <2 x i8> [[Y]], <2 x i8> zeroinitializer
744 ; CHECK-NEXT: ret <2 x i8> [[R]]
746 %a = mul nsw <2 x i8> %y, %x
747 %r = sdiv <2 x i8> %x, %a
751 ; X / (Y * X) -> 1 / Y if the multiplication does not overflow
753 define i8 @div_factor_unsigned(i8 %x, i8 %y) {
754 ; CHECK-LABEL: @div_factor_unsigned(
755 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[Y:%.*]], 1
756 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i8
757 ; CHECK-NEXT: ret i8 [[R]]
759 %a = mul nuw i8 %y, %x
764 ; X / (X * Y) -> 1 / Y if the multiplication does not overflow
766 define <2 x i8> @div_factor_unsigned_vec(<2 x i8> %x, <2 x i8> %y) {
767 ; CHECK-LABEL: @div_factor_unsigned_vec(
768 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[Y:%.*]], <i8 1, i8 1>
769 ; CHECK-NEXT: [[R:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
770 ; CHECK-NEXT: ret <2 x i8> [[R]]
772 %a = mul nuw <2 x i8> %x, %y
773 %r = udiv <2 x i8> %x, %a
777 define i8 @udiv_common_factor(i8 %x, i8 %y, i8 %z) {
778 ; CHECK-LABEL: @udiv_common_factor(
779 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
780 ; CHECK-NEXT: ret i8 [[C]]
782 %a = mul nuw i8 %z, %x
783 %b = mul nuw i8 %z, %y
788 define <2 x i8> @udiv_common_factor_commute1_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
789 ; CHECK-LABEL: @udiv_common_factor_commute1_vec(
790 ; CHECK-NEXT: [[C:%.*]] = udiv <2 x i8> [[X:%.*]], [[Y:%.*]]
791 ; CHECK-NEXT: ret <2 x i8> [[C]]
793 %a = mul nuw <2 x i8> %x, %z
794 %b = mul nuw <2 x i8> %z, %y
795 %c = udiv <2 x i8> %a, %b
799 define i8 @udiv_common_factor_commute2(i8 %x, i8 %y, i8 %z) {
800 ; CHECK-LABEL: @udiv_common_factor_commute2(
801 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
802 ; CHECK-NEXT: ret i8 [[C]]
804 %a = mul nuw i8 %x, %z
805 %b = mul nuw i8 %y, %z
810 define i8 @udiv_common_factor_commute3(i8 %x, i8 %y, i8 %z) {
811 ; CHECK-LABEL: @udiv_common_factor_commute3(
812 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
813 ; CHECK-NEXT: ret i8 [[C]]
815 %a = mul nuw i8 %z, %x
816 %b = mul nuw i8 %y, %z
821 ; Negative test: both mul must be 'nuw'.
823 define i8 @udiv_common_factor_not_nuw(i8 %x, i8 %y, i8 %z) {
824 ; CHECK-LABEL: @udiv_common_factor_not_nuw(
825 ; CHECK-NEXT: [[A:%.*]] = mul i8 [[Z:%.*]], [[X:%.*]]
826 ; CHECK-NEXT: [[B:%.*]] = mul nuw i8 [[Z]], [[Y:%.*]]
827 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[A]], [[B]]
828 ; CHECK-NEXT: ret i8 [[C]]
831 %b = mul nuw i8 %z, %y
836 ; Negative test: both mul must be 'nuw'.
838 define <2 x i8> @udiv_common_factor_not_nuw_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
839 ; CHECK-LABEL: @udiv_common_factor_not_nuw_vec(
840 ; CHECK-NEXT: [[A:%.*]] = mul nuw <2 x i8> [[Z:%.*]], [[X:%.*]]
841 ; CHECK-NEXT: [[B:%.*]] = mul <2 x i8> [[Z]], [[Y:%.*]]
842 ; CHECK-NEXT: [[C:%.*]] = udiv <2 x i8> [[A]], [[B]]
843 ; CHECK-NEXT: ret <2 x i8> [[C]]
845 %a = mul nuw <2 x i8> %z, %x
846 %b = mul <2 x i8> %z, %y
847 %c = udiv <2 x i8> %a, %b
851 define i32 @test_exact_nsw_exact(i32 %x) {
852 ; CHECK-LABEL: @test_exact_nsw_exact(
853 ; CHECK-NEXT: [[NEG:%.*]] = sdiv exact i32 [[X:%.*]], -3
854 ; CHECK-NEXT: ret i32 [[NEG]]
856 %div = sdiv exact i32 %x, 3
857 %neg = sub nsw i32 0, %div
861 define <2 x i64> @test_exact_vec(<2 x i64> %x) {
862 ; CHECK-LABEL: @test_exact_vec(
863 ; CHECK-NEXT: [[DIV:%.*]] = sdiv exact <2 x i64> [[X:%.*]], <i64 3, i64 4>
864 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw <2 x i64> zeroinitializer, [[DIV]]
865 ; CHECK-NEXT: ret <2 x i64> [[NEG]]
867 %div = sdiv exact <2 x i64> %x, <i64 3, i64 4>
868 %neg = sub nsw <2 x i64> zeroinitializer, %div
872 ; Constant is safe to negate.
874 define <2 x i8> @negate_sdiv_vec_splat(<2 x i8> %x) {
875 ; CHECK-LABEL: @negate_sdiv_vec_splat(
876 ; CHECK-NEXT: [[NEG:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -42, i8 -42>
877 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
879 %div = sdiv <2 x i8> %x, <i8 42, i8 42>
880 %neg = sub <2 x i8> zeroinitializer, %div
884 ; Dividing by undef is UB.
886 define <2 x i8> @negate_sdiv_vec_undef_elt(<2 x i8> %x) {
887 ; CHECK-LABEL: @negate_sdiv_vec_undef_elt(
888 ; CHECK-NEXT: ret <2 x i8> undef
890 %div = sdiv <2 x i8> %x, <i8 undef, i8 42>
891 %neg = sub <2 x i8> zeroinitializer, %div
895 ; Division by -1 may be UB (if numerator is the signed min val), but div-by-1 can be simplified.
897 define <2 x i8> @negate_sdiv_vec_splat_one(<2 x i8> %x) {
898 ; CHECK-LABEL: @negate_sdiv_vec_splat_one(
899 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]]
900 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
902 %div = sdiv <2 x i8> %x, <i8 1, i8 1>
903 %neg = sub <2 x i8> zeroinitializer, %div
907 ; Can't negate signed-min constant, but can convert to a compare..
909 define <2 x i8> @negate_sdiv_vec_splat_signed_min(<2 x i8> %x) {
910 ; CHECK-LABEL: @negate_sdiv_vec_splat_signed_min(
911 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
912 ; CHECK-NEXT: [[NEG:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
913 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
915 %div = sdiv <2 x i8> %x, <i8 -128, i8 -128>
916 %neg = sub <2 x i8> zeroinitializer, %div
920 ; Division by -1 may be UB for any element of a vector.
922 define <2 x i8> @negate_sdiv_vec_one_element(<2 x i8> %x) {
923 ; CHECK-LABEL: @negate_sdiv_vec_one_element(
924 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -1, i8 1>
925 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[DIV]]
926 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
928 %div = sdiv <2 x i8> %x, <i8 -1, i8 1>
929 %neg = sub <2 x i8> zeroinitializer, %div
933 ; Can't negate signed-min constant for any element of a vector.
935 define <2 x i8> @negate_sdiv_vec_signed_min_elt(<2 x i8> %x) {
936 ; CHECK-LABEL: @negate_sdiv_vec_signed_min_elt(
937 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -1, i8 -128>
938 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[DIV]]
939 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
941 %div = sdiv <2 x i8> %x, <i8 -1, i8 -128>
942 %neg = sub <2 x i8> zeroinitializer, %div
946 ; Division by -1 may be UB and can't negate signed-min.
948 define <2 x i8> @negate_sdiv_vec_signed_min_and_one_elt(<2 x i8> %x) {
949 ; CHECK-LABEL: @negate_sdiv_vec_signed_min_and_one_elt(
950 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 1, i8 -128>
951 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[DIV]]
952 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
954 %div = sdiv <2 x i8> %x, <i8 1, i8 -128>
955 %neg = sub <2 x i8> zeroinitializer, %div
959 define i32 @test_exact_nonsw_exact(i32 %x) {
960 ; CHECK-LABEL: @test_exact_nonsw_exact(
961 ; CHECK-NEXT: [[NEG:%.*]] = sdiv exact i32 [[X:%.*]], -3
962 ; CHECK-NEXT: ret i32 [[NEG]]
964 %div = sdiv exact i32 %x, 3
965 %neg = sub i32 0, %div
969 define i32 @test_exact_nsw_noexact(i32 %x) {
970 ; CHECK-LABEL: @test_exact_nsw_noexact(
971 ; CHECK-NEXT: [[NEG:%.*]] = sdiv i32 [[X:%.*]], -3
972 ; CHECK-NEXT: ret i32 [[NEG]]
974 %div = sdiv i32 %x, 3
975 %neg = sub nsw i32 0, %div
979 define i32 @test_exact_nonsw_noexact(i32 %x) {
980 ; CHECK-LABEL: @test_exact_nonsw_noexact(
981 ; CHECK-NEXT: [[NEG:%.*]] = sdiv i32 [[X:%.*]], -3
982 ; CHECK-NEXT: ret i32 [[NEG]]
984 %div = sdiv i32 %x, 3
985 %neg = sub i32 0, %div
989 define i32 @test_exact_div_nonconst(i32 %x, i32 %y) {
990 ; CHECK-LABEL: @test_exact_div_nonconst(
991 ; CHECK-NEXT: [[DIV:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]]
992 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[DIV]]
993 ; CHECK-NEXT: ret i32 [[NEG]]
995 %div = sdiv exact i32 %x, %y
996 %neg = sub nsw i32 0, %div
1000 define i32 @test_exact_div_one(i32 %x) {
1001 ; CHECK-LABEL: @test_exact_div_one(
1002 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
1003 ; CHECK-NEXT: ret i32 [[NEG]]
1005 %div = sdiv exact i32 %x, 1
1006 %neg = sub nsw i32 0, %div
1010 define i8 @test_exact_div_minSigned(i8 %x) {
1011 ; CHECK-LABEL: @test_exact_div_minSigned(
1012 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
1013 ; CHECK-NEXT: [[NEG:%.*]] = sext i1 [[TMP1]] to i8
1014 ; CHECK-NEXT: ret i8 [[NEG]]
1016 %div = sdiv exact i8 %x, -128
1017 %neg = sub nsw i8 0, %div
1021 ; X / INT_MIN --> X == INT_MIN
1023 define i8 @sdiv_by_int_min(i8 %x) {
1024 ; CHECK-LABEL: @sdiv_by_int_min(
1025 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
1026 ; CHECK-NEXT: [[D:%.*]] = zext i1 [[TMP1]] to i8
1027 ; CHECK-NEXT: ret i8 [[D]]
1029 %d = sdiv i8 %x, -128
1033 define <2 x i8> @sdiv_by_int_min_vec_splat(<2 x i8> %x) {
1034 ; CHECK-LABEL: @sdiv_by_int_min_vec_splat(
1035 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
1036 ; CHECK-NEXT: [[D:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
1037 ; CHECK-NEXT: ret <2 x i8> [[D]]
1039 %d = sdiv <2 x i8> %x, <i8 -128, i8 -128>
1043 define <2 x i8> @sdiv_by_int_min_vec_splat_undef(<2 x i8> %x) {
1044 ; CHECK-LABEL: @sdiv_by_int_min_vec_splat_undef(
1045 ; CHECK-NEXT: ret <2 x i8> undef
1047 %d = sdiv <2 x i8> %x, <i8 -128, i8 undef>