1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=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: [[B1:%.*]] = lshr i32 [[A:%.*]], 3
17 ; CHECK-NEXT: ret i32 [[B1]]
23 define i32 @sdiv_by_minus1(i32 %A) {
24 ; CHECK-LABEL: @sdiv_by_minus1(
25 ; CHECK-NEXT: [[B:%.*]] = sub nsw 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 nsw <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_poison_elt(<2 x i64> %x) {
42 ; CHECK-LABEL: @sdiv_by_minus1_vec_poison_elt(
43 ; CHECK-NEXT: ret <2 x i64> poison
45 %div = sdiv <2 x i64> %x, <i64 -1, i64 poison>
49 define i32 @sdiv_by_sext_minus1(i1 %x, i32 %y) {
50 ; CHECK-LABEL: @sdiv_by_sext_minus1(
51 ; CHECK-NEXT: [[DIV:%.*]] = sub nsw 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 nsw <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: [[C:%.*]] = icmp ult i32 [[A:%.*]], 123
133 ; CHECK-NEXT: ret i1 [[C]]
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: [[C:%.*]] = icmp ult i32 [[A_OFF]], 10
145 ; CHECK-NEXT: ret i1 [[C]]
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: [[C:%.*]] = icmp ult <2 x i32> [[A_OFF]], <i32 10, i32 10>
157 ; CHECK-NEXT: ret <2 x i1> [[C]]
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: [[TMP1:%.*]] = select i1 [[C:%.*]], i32 6, i32 3
209 ; CHECK-NEXT: [[R1:%.*]] = lshr i32 [[X:%.*]], [[TMP1]]
210 ; CHECK-NEXT: ret i32 [[R1]]
212 %V = select i1 %C, i32 64, i32 8
217 define i32 @test11(i32 %X, i1 %C) {
218 ; CHECK-LABEL: @test11(
219 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C:%.*]], i32 10, i32 5
220 ; CHECK-NEXT: [[B1:%.*]] = lshr i32 [[X:%.*]], [[TMP1]]
221 ; CHECK-NEXT: ret i32 [[B1]]
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: [[DIV21:%.*]] = lshr i32 [[A:%.*]], [[TMP1]]
259 ; CHECK-NEXT: ret i32 [[DIV21]]
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: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
300 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X_FR]], 1
301 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i32 [[TMP1]], 3
302 ; CHECK-NEXT: [[A:%.*]] = select i1 [[TMP2]], i32 [[X_FR]], i32 0
303 ; CHECK-NEXT: ret i32 [[A]]
309 define <2 x i32> @test20vec(<2 x i32> %x) {
310 ; CHECK-LABEL: @test20vec(
311 ; CHECK-NEXT: [[X_FR:%.*]] = freeze <2 x i32> [[X:%.*]]
312 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> [[X_FR]], <i32 1, i32 1>
313 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult <2 x i32> [[TMP1]], <i32 3, i32 3>
314 ; CHECK-NEXT: [[A:%.*]] = select <2 x i1> [[TMP2]], <2 x i32> [[X_FR]], <2 x i32> zeroinitializer
315 ; CHECK-NEXT: ret <2 x i32> [[A]]
317 %A = sdiv <2 x i32> <i32 1, i32 1>, %x
321 define i32 @test21(i32 %a) {
322 ; CHECK-LABEL: @test21(
323 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A:%.*]], 3
324 ; CHECK-NEXT: ret i32 [[DIV]]
326 %shl = shl nsw i32 %a, 2
327 %div = sdiv i32 %shl, 12
331 define i32 @test22(i32 %a) {
332 ; CHECK-LABEL: @test22(
333 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A:%.*]], 4
334 ; CHECK-NEXT: ret i32 [[DIV]]
336 %mul = mul nsw i32 %a, 3
337 %div = sdiv i32 %mul, 12
341 define i32 @test23(i32 %a) {
342 ; CHECK-LABEL: @test23(
343 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[A:%.*]], 3
344 ; CHECK-NEXT: ret i32 [[DIV]]
346 %shl = shl nuw i32 %a, 2
347 %div = udiv i32 %shl, 12
351 define i32 @test24(i32 %a) {
352 ; CHECK-LABEL: @test24(
353 ; CHECK-NEXT: [[DIV1:%.*]] = lshr i32 [[A:%.*]], 2
354 ; CHECK-NEXT: ret i32 [[DIV1]]
356 %mul = mul nuw i32 %a, 3
357 %div = udiv i32 %mul, 12
361 define i32 @test25(i32 %a) {
362 ; CHECK-LABEL: @test25(
363 ; CHECK-NEXT: [[DIV:%.*]] = shl nsw i32 [[A:%.*]], 1
364 ; CHECK-NEXT: ret i32 [[DIV]]
366 %shl = shl nsw i32 %a, 2
367 %div = sdiv i32 %shl, 2
371 define i32 @test26(i32 %a) {
372 ; CHECK-LABEL: @test26(
373 ; CHECK-NEXT: [[DIV:%.*]] = shl nsw i32 [[A:%.*]], 2
374 ; CHECK-NEXT: ret i32 [[DIV]]
376 %mul = mul nsw i32 %a, 12
377 %div = sdiv i32 %mul, 3
381 define i32 @test27(i32 %a) {
382 ; CHECK-LABEL: @test27(
383 ; CHECK-NEXT: [[DIV:%.*]] = shl nuw i32 [[A:%.*]], 1
384 ; CHECK-NEXT: ret i32 [[DIV]]
386 %shl = shl nuw i32 %a, 2
387 %div = udiv i32 %shl, 2
391 define i32 @test28(i32 %a) {
392 ; CHECK-LABEL: @test28(
393 ; CHECK-NEXT: [[DIV:%.*]] = mul nuw i32 [[A:%.*]], 12
394 ; CHECK-NEXT: ret i32 [[DIV]]
396 %mul = mul nuw i32 %a, 36
397 %div = udiv i32 %mul, 3
401 define i32 @test29(i32 %a) {
402 ; CHECK-LABEL: @test29(
403 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[A:%.*]], 0
404 ; CHECK-NEXT: [[DIV:%.*]] = zext i1 [[TMP1]] to i32
405 ; CHECK-NEXT: ret i32 [[DIV]]
407 %mul = shl nsw i32 %a, 31
408 %div = sdiv i32 %mul, -2147483648
412 define i32 @test30(i32 %a) {
413 ; CHECK-LABEL: @test30(
414 ; CHECK-NEXT: ret i32 [[A:%.*]]
416 %mul = shl nuw i32 %a, 31
417 %div = udiv i32 %mul, -2147483648
421 define <2 x i32> @test31(<2 x i32> %x) {
422 ; CHECK-LABEL: @test31(
423 ; CHECK-NEXT: ret <2 x i32> zeroinitializer
425 %shr = lshr <2 x i32> %x, <i32 31, i32 31>
426 %div = udiv <2 x i32> %shr, <i32 2147483647, i32 2147483647>
430 define i32 @test32(i32 %a, i32 %b) {
431 ; CHECK-LABEL: @test32(
432 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[B:%.*]]
433 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL]], 2
434 ; CHECK-NEXT: [[DIV2:%.*]] = udiv i32 [[A:%.*]], [[DIV]]
435 ; CHECK-NEXT: ret i32 [[DIV2]]
438 %div = lshr i32 %shl, 2
439 %div2 = udiv i32 %a, %div
443 define <2 x i64> @test33(<2 x i64> %x) {
444 ; CHECK-LABEL: @test33(
445 ; CHECK-NEXT: [[DIV:%.*]] = udiv exact <2 x i64> [[X:%.*]], <i64 192, i64 192>
446 ; CHECK-NEXT: ret <2 x i64> [[DIV]]
448 %shr = lshr exact <2 x i64> %x, <i64 5, i64 5>
449 %div = udiv exact <2 x i64> %shr, <i64 6, i64 6>
453 ; -X / C --> X / -C (if negation does not overflow)
455 define i8 @sdiv_negated_dividend_constant_divisor(i8 %x) {
456 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor(
457 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[X:%.*]], 42
458 ; CHECK-NEXT: ret i8 [[D]]
460 %neg = sub nsw i8 0, %x
461 %d = sdiv i8 %neg, -42
465 define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_splat(<2 x i8> %x) {
466 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_splat(
467 ; CHECK-NEXT: [[D:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 42, i8 42>
468 ; CHECK-NEXT: ret <2 x i8> [[D]]
470 %neg = sub nsw <2 x i8> zeroinitializer, %x
471 %d = sdiv <2 x i8> %neg, <i8 -42, i8 -42>
475 define i8 @sdiv_exact_negated_dividend_constant_divisor(i8 %x) {
476 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor(
477 ; CHECK-NEXT: [[D:%.*]] = sdiv exact i8 [[X:%.*]], 42
478 ; CHECK-NEXT: ret i8 [[D]]
480 %neg = sub nsw i8 0, %x
481 %d = sdiv exact i8 %neg, -42
485 define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_splat(<2 x i8> %x) {
486 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec_splat(
487 ; CHECK-NEXT: [[D:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 42, i8 42>
488 ; CHECK-NEXT: ret <2 x i8> [[D]]
490 %neg = sub nsw <2 x i8> zeroinitializer, %x
491 %d = sdiv exact <2 x i8> %neg, <i8 -42, i8 -42>
495 define i8 @sdiv_negated_dividend_constant_divisor_smin(i8 %x) {
496 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_smin(
497 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
498 ; CHECK-NEXT: [[D:%.*]] = zext i1 [[TMP1]] to i8
499 ; CHECK-NEXT: ret i8 [[D]]
501 %neg = sub nsw i8 0, %x
502 %d = sdiv i8 %neg, -128
506 define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_splat_smin(<2 x i8> %x) {
507 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_splat_smin(
508 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
509 ; CHECK-NEXT: [[D:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
510 ; CHECK-NEXT: ret <2 x i8> [[D]]
512 %neg = sub nsw <2 x i8> zeroinitializer, %x
513 %d = sdiv <2 x i8> %neg, <i8 -128, i8 -128>
517 define <2 x i8> @sdiv_negated_dividend_constant_divisor_vec_poison(<2 x i8> %x) {
518 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec_poison(
519 ; CHECK-NEXT: ret <2 x i8> poison
521 %neg = sub nsw <2 x i8> zeroinitializer, %x
522 %d = sdiv <2 x i8> %neg, <i8 -128, i8 poison>
526 define <2 x i64> @sdiv_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
527 ; CHECK-LABEL: @sdiv_negated_dividend_constant_divisor_vec(
528 ; CHECK-NEXT: [[DIV1_NEG:%.*]] = sdiv <2 x i64> [[X:%.*]], <i64 -3, i64 -4>
529 ; CHECK-NEXT: ret <2 x i64> [[DIV1_NEG]]
531 %neg = sub nsw <2 x i64> zeroinitializer, %x
532 %div = sdiv <2 x i64> %neg, <i64 3, i64 4>
536 define <2 x i64> @sdiv_exact_negated_dividend_constant_divisor_vec(<2 x i64> %x) {
537 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec(
538 ; CHECK-NEXT: [[DIV1_NEG:%.*]] = sdiv exact <2 x i64> [[X:%.*]], <i64 -3, i64 -4>
539 ; CHECK-NEXT: ret <2 x i64> [[DIV1_NEG]]
541 %neg = sub nsw <2 x i64> zeroinitializer, %x
542 %div = sdiv exact <2 x i64> %neg, <i64 3, i64 4>
546 ; Can't negate signed min vector element.
548 define <2 x i8> @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(<2 x i8> %x) {
549 ; CHECK-LABEL: @sdiv_exact_negated_dividend_constant_divisor_vec_overflow(
550 ; CHECK-NEXT: [[DIV1:%.*]] = sdiv exact <2 x i8> [[X:%.*]], <i8 -128, i8 42>
551 ; CHECK-NEXT: [[DIV:%.*]] = sub nsw <2 x i8> zeroinitializer, [[DIV1]]
552 ; CHECK-NEXT: ret <2 x i8> [[DIV]]
554 %neg = sub nsw <2 x i8> zeroinitializer, %x
555 %div = sdiv exact <2 x i8> %neg, <i8 -128, i8 42>
559 define i32 @test35(i32 %A) {
560 ; CHECK-LABEL: @test35(
561 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], 2147483647
562 ; CHECK-NEXT: [[MUL:%.*]] = udiv exact i32 [[AND]], 2147483647
563 ; CHECK-NEXT: ret i32 [[MUL]]
565 %and = and i32 %A, 2147483647
566 %mul = sdiv exact i32 %and, 2147483647
570 define <2 x i32> @test35vec(<2 x i32> %A) {
571 ; CHECK-LABEL: @test35vec(
572 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[A:%.*]], <i32 2147483647, i32 2147483647>
573 ; CHECK-NEXT: [[MUL:%.*]] = udiv exact <2 x i32> [[AND]], <i32 2147483647, i32 2147483647>
574 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
576 %and = and <2 x i32> %A, <i32 2147483647, i32 2147483647>
577 %mul = sdiv exact <2 x i32> %and, <i32 2147483647, i32 2147483647>
581 define i32 @test36(i32 %A) {
582 ; CHECK-LABEL: @test36(
583 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], 2147483647
584 ; CHECK-NEXT: [[MUL:%.*]] = lshr exact i32 [[AND]], [[A]]
585 ; CHECK-NEXT: ret i32 [[MUL]]
587 %and = and i32 %A, 2147483647
588 %shl = shl nsw i32 1, %A
589 %mul = sdiv exact i32 %and, %shl
593 define <2 x i32> @test36vec(<2 x i32> %A) {
594 ; CHECK-LABEL: @test36vec(
595 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[A:%.*]], <i32 2147483647, i32 2147483647>
596 ; CHECK-NEXT: [[MUL:%.*]] = lshr exact <2 x i32> [[AND]], [[A]]
597 ; CHECK-NEXT: ret <2 x i32> [[MUL]]
599 %and = and <2 x i32> %A, <i32 2147483647, i32 2147483647>
600 %shl = shl nsw <2 x i32> <i32 1, i32 1>, %A
601 %mul = sdiv exact <2 x i32> %and, %shl
605 define i32 @test37(ptr %b, i1 %c1) {
606 ; CHECK-LABEL: @test37(
608 ; CHECK-NEXT: store i32 0, ptr [[B:%.*]], align 4
609 ; CHECK-NEXT: br i1 [[C1:%.*]], label [[LOR_RHS:%.*]], label [[LOR_END:%.*]]
611 ; CHECK-NEXT: br label [[LOR_END]]
613 ; CHECK-NEXT: ret i32 0
616 store i32 0, ptr %b, align 4
617 %0 = load i32, ptr %b, align 4
618 br i1 %c1, label %lor.rhs, label %lor.end
620 lor.rhs: ; preds = %entry
621 %mul = mul nsw i32 1, %0
624 lor.end: ; preds = %lor.rhs, %entry
625 %t.0 = phi i32 [ %0, %entry ], [ %mul, %lor.rhs ]
626 %div = sdiv i32 %t.0, 2
630 ; We can perform the division in the smaller type.
632 define i32 @shrink(i8 %x) {
633 ; CHECK-LABEL: @shrink(
634 ; CHECK-NEXT: [[TMP1:%.*]] = sdiv i8 [[X:%.*]], 127
635 ; CHECK-NEXT: [[DIV:%.*]] = sext i8 [[TMP1]] to i32
636 ; CHECK-NEXT: ret i32 [[DIV]]
638 %conv = sext i8 %x to i32
639 %div = sdiv i32 %conv, 127
643 ; Division in the smaller type can lead to more optimizations.
645 define i32 @zap(i8 %x) {
647 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
648 ; CHECK-NEXT: [[DIV:%.*]] = zext i1 [[TMP1]] to i32
649 ; CHECK-NEXT: ret i32 [[DIV]]
651 %conv = sext i8 %x to i32
652 %div = sdiv i32 %conv, -128
656 ; Splat constant divisors should get the same folds.
658 define <3 x i32> @shrink_vec(<3 x i8> %x) {
659 ; CHECK-LABEL: @shrink_vec(
660 ; CHECK-NEXT: [[TMP1:%.*]] = sdiv <3 x i8> [[X:%.*]], <i8 127, i8 127, i8 127>
661 ; CHECK-NEXT: [[DIV:%.*]] = sext <3 x i8> [[TMP1]] to <3 x i32>
662 ; CHECK-NEXT: ret <3 x i32> [[DIV]]
664 %conv = sext <3 x i8> %x to <3 x i32>
665 %div = sdiv <3 x i32> %conv, <i32 127, i32 127, i32 127>
669 define <2 x i32> @zap_vec(<2 x i8> %x) {
670 ; CHECK-LABEL: @zap_vec(
671 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
672 ; CHECK-NEXT: [[DIV:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i32>
673 ; CHECK-NEXT: ret <2 x i32> [[DIV]]
675 %conv = sext <2 x i8> %x to <2 x i32>
676 %div = sdiv <2 x i32> %conv, <i32 -128, i32 -128>
680 ; But we can't do this if the signed constant won't fit in the original type.
682 define i32 @shrink_no(i8 %x) {
683 ; CHECK-LABEL: @shrink_no(
684 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X:%.*]] to i32
685 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[CONV]], 128
686 ; CHECK-NEXT: ret i32 [[DIV]]
688 %conv = sext i8 %x to i32
689 %div = sdiv i32 %conv, 128
693 ; When the divisor is known larger than the quotient,
694 ; InstSimplify should kill it before InstCombine sees it.
696 define i32 @shrink_no2(i8 %x) {
697 ; CHECK-LABEL: @shrink_no2(
698 ; CHECK-NEXT: ret i32 0
700 %conv = sext i8 %x to i32
701 %div = sdiv i32 %conv, -129
705 define i32 @shrink_no3(i16 %x) {
706 ; CHECK-LABEL: @shrink_no3(
707 ; CHECK-NEXT: ret i32 0
709 %conv = sext i16 %x to i32
710 %div = sdiv i32 %conv, 65535
714 ; This previously crashed when trying to simplify the zext/icmp this becomes.
715 define <2 x i8> @PR34841(<2 x i8> %x) {
716 ; CHECK-LABEL: @PR34841(
717 ; CHECK-NEXT: ret <2 x i8> zeroinitializer
719 %neg = and <2 x i8> %x, <i8 2, i8 2>
720 %div = udiv <2 x i8> <i8 1, i8 1>, %neg
724 ; X / (X * Y) -> 1 / Y if the multiplication does not overflow
726 define i8 @div_factor_signed(i8 %x, i8 %y) {
727 ; CHECK-LABEL: @div_factor_signed(
728 ; CHECK-NEXT: [[Y_FR:%.*]] = freeze i8 [[Y:%.*]]
729 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y_FR]], 1
730 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult i8 [[TMP1]], 3
731 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i8 [[Y_FR]], i8 0
732 ; CHECK-NEXT: ret i8 [[R]]
734 %a = mul nsw i8 %x, %y
739 ; X / (Y * X) -> 1 / Y if the multiplication does not overflow
741 define <2 x i8> @div_factor_signed_vec(<2 x i8> %x, <2 x i8> %y) {
742 ; CHECK-LABEL: @div_factor_signed_vec(
743 ; CHECK-NEXT: [[Y_FR:%.*]] = freeze <2 x i8> [[Y:%.*]]
744 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[Y_FR]], <i8 1, i8 1>
745 ; CHECK-NEXT: [[TMP2:%.*]] = icmp ult <2 x i8> [[TMP1]], <i8 3, i8 3>
746 ; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[TMP2]], <2 x i8> [[Y_FR]], <2 x i8> zeroinitializer
747 ; CHECK-NEXT: ret <2 x i8> [[R]]
749 %a = mul nsw <2 x i8> %y, %x
750 %r = sdiv <2 x i8> %x, %a
754 ; X / (Y * X) -> 1 / Y if the multiplication does not overflow
756 define i8 @div_factor_unsigned(i8 %x, i8 %y) {
757 ; CHECK-LABEL: @div_factor_unsigned(
758 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[Y:%.*]], 1
759 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i8
760 ; CHECK-NEXT: ret i8 [[R]]
762 %a = mul nuw i8 %y, %x
767 ; X / (X * Y) -> 1 / Y if the multiplication does not overflow
769 define <2 x i8> @div_factor_unsigned_vec(<2 x i8> %x, <2 x i8> %y) {
770 ; CHECK-LABEL: @div_factor_unsigned_vec(
771 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[Y:%.*]], <i8 1, i8 1>
772 ; CHECK-NEXT: [[R:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
773 ; CHECK-NEXT: ret <2 x i8> [[R]]
775 %a = mul nuw <2 x i8> %x, %y
776 %r = udiv <2 x i8> %x, %a
780 define i8 @udiv_common_factor(i8 %x, i8 %y, i8 %z) {
781 ; CHECK-LABEL: @udiv_common_factor(
782 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
783 ; CHECK-NEXT: ret i8 [[C]]
785 %a = mul nuw i8 %z, %x
786 %b = mul nuw i8 %z, %y
791 define <2 x i8> @udiv_common_factor_commute1_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
792 ; CHECK-LABEL: @udiv_common_factor_commute1_vec(
793 ; CHECK-NEXT: [[C:%.*]] = udiv <2 x i8> [[X:%.*]], [[Y:%.*]]
794 ; CHECK-NEXT: ret <2 x i8> [[C]]
796 %a = mul nuw <2 x i8> %x, %z
797 %b = mul nuw <2 x i8> %z, %y
798 %c = udiv <2 x i8> %a, %b
802 define i8 @udiv_common_factor_commute2(i8 %x, i8 %y, i8 %z) {
803 ; CHECK-LABEL: @udiv_common_factor_commute2(
804 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
805 ; CHECK-NEXT: ret i8 [[C]]
807 %a = mul nuw i8 %x, %z
808 %b = mul nuw i8 %y, %z
813 define i8 @udiv_common_factor_commute3(i8 %x, i8 %y, i8 %z) {
814 ; CHECK-LABEL: @udiv_common_factor_commute3(
815 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
816 ; CHECK-NEXT: ret i8 [[C]]
818 %a = mul nuw i8 %z, %x
819 %b = mul nuw i8 %y, %z
824 ; Negative test: both mul must be 'nuw'.
826 define i8 @udiv_common_factor_not_nuw(i8 %x, i8 %y, i8 %z) {
827 ; CHECK-LABEL: @udiv_common_factor_not_nuw(
828 ; CHECK-NEXT: [[A:%.*]] = mul i8 [[Z:%.*]], [[X:%.*]]
829 ; CHECK-NEXT: [[B:%.*]] = mul nuw i8 [[Z]], [[Y:%.*]]
830 ; CHECK-NEXT: [[C:%.*]] = udiv i8 [[A]], [[B]]
831 ; CHECK-NEXT: ret i8 [[C]]
834 %b = mul nuw i8 %z, %y
839 ; Negative test: both mul must be 'nuw'.
841 define <2 x i8> @udiv_common_factor_not_nuw_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
842 ; CHECK-LABEL: @udiv_common_factor_not_nuw_vec(
843 ; CHECK-NEXT: [[A:%.*]] = mul nuw <2 x i8> [[Z:%.*]], [[X:%.*]]
844 ; CHECK-NEXT: [[B:%.*]] = mul <2 x i8> [[Z]], [[Y:%.*]]
845 ; CHECK-NEXT: [[C:%.*]] = udiv <2 x i8> [[A]], [[B]]
846 ; CHECK-NEXT: ret <2 x i8> [[C]]
848 %a = mul nuw <2 x i8> %z, %x
849 %b = mul <2 x i8> %z, %y
850 %c = udiv <2 x i8> %a, %b
854 define i32 @test_exact_nsw_exact(i32 %x) {
855 ; CHECK-LABEL: @test_exact_nsw_exact(
856 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv exact i32 [[X:%.*]], -3
857 ; CHECK-NEXT: ret i32 [[DIV_NEG]]
859 %div = sdiv exact i32 %x, 3
860 %neg = sub nsw i32 0, %div
864 define <2 x i64> @test_exact_vec(<2 x i64> %x) {
865 ; CHECK-LABEL: @test_exact_vec(
866 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv exact <2 x i64> [[X:%.*]], <i64 -3, i64 -4>
867 ; CHECK-NEXT: ret <2 x i64> [[DIV_NEG]]
869 %div = sdiv exact <2 x i64> %x, <i64 3, i64 4>
870 %neg = sub nsw <2 x i64> zeroinitializer, %div
874 ; Constant is safe to negate.
876 define <2 x i8> @negate_sdiv_vec_splat(<2 x i8> %x) {
877 ; CHECK-LABEL: @negate_sdiv_vec_splat(
878 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -42, i8 -42>
879 ; CHECK-NEXT: ret <2 x i8> [[DIV_NEG]]
881 %div = sdiv <2 x i8> %x, <i8 42, i8 42>
882 %neg = sub <2 x i8> zeroinitializer, %div
886 ; Dividing by poison is UB.
888 define <2 x i8> @negate_sdiv_vec_poison_elt(<2 x i8> %x) {
889 ; CHECK-LABEL: @negate_sdiv_vec_poison_elt(
890 ; CHECK-NEXT: ret <2 x i8> poison
892 %div = sdiv <2 x i8> %x, <i8 poison, i8 42>
893 %neg = sub <2 x i8> zeroinitializer, %div
897 ; Division by -1 may be UB (if numerator is the signed min val), but div-by-1 can be simplified.
899 define <2 x i8> @negate_sdiv_vec_splat_one(<2 x i8> %x) {
900 ; CHECK-LABEL: @negate_sdiv_vec_splat_one(
901 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]]
902 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
904 %div = sdiv <2 x i8> %x, <i8 1, i8 1>
905 %neg = sub <2 x i8> zeroinitializer, %div
909 ; Can't negate signed-min constant, but can convert to a compare..
911 define <2 x i8> @negate_sdiv_vec_splat_signed_min(<2 x i8> %x) {
912 ; CHECK-LABEL: @negate_sdiv_vec_splat_signed_min(
913 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
914 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
915 ; CHECK-NEXT: ret <2 x i8> [[DIV_NEG]]
917 %div = sdiv <2 x i8> %x, <i8 -128, i8 -128>
918 %neg = sub <2 x i8> zeroinitializer, %div
922 ; Division by -1 may be UB for any element of a vector.
924 define <2 x i8> @negate_sdiv_vec_one_element(<2 x i8> %x) {
925 ; CHECK-LABEL: @negate_sdiv_vec_one_element(
926 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -1, i8 1>
927 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[DIV]]
928 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
930 %div = sdiv <2 x i8> %x, <i8 -1, i8 1>
931 %neg = sub <2 x i8> zeroinitializer, %div
935 ; Can't negate signed-min constant for any element of a vector.
937 define <2 x i8> @negate_sdiv_vec_signed_min_elt(<2 x i8> %x) {
938 ; CHECK-LABEL: @negate_sdiv_vec_signed_min_elt(
939 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 -1, i8 -128>
940 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[DIV]]
941 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
943 %div = sdiv <2 x i8> %x, <i8 -1, i8 -128>
944 %neg = sub <2 x i8> zeroinitializer, %div
948 ; Division by -1 may be UB and can't negate signed-min.
950 define <2 x i8> @negate_sdiv_vec_signed_min_and_one_elt(<2 x i8> %x) {
951 ; CHECK-LABEL: @negate_sdiv_vec_signed_min_and_one_elt(
952 ; CHECK-NEXT: [[DIV:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 1, i8 -128>
953 ; CHECK-NEXT: [[NEG:%.*]] = sub <2 x i8> zeroinitializer, [[DIV]]
954 ; CHECK-NEXT: ret <2 x i8> [[NEG]]
956 %div = sdiv <2 x i8> %x, <i8 1, i8 -128>
957 %neg = sub <2 x i8> zeroinitializer, %div
961 define i32 @test_exact_nonsw_exact(i32 %x) {
962 ; CHECK-LABEL: @test_exact_nonsw_exact(
963 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv exact i32 [[X:%.*]], -3
964 ; CHECK-NEXT: ret i32 [[DIV_NEG]]
966 %div = sdiv exact i32 %x, 3
967 %neg = sub i32 0, %div
971 define i32 @test_exact_nsw_noexact(i32 %x) {
972 ; CHECK-LABEL: @test_exact_nsw_noexact(
973 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv i32 [[X:%.*]], -3
974 ; CHECK-NEXT: ret i32 [[DIV_NEG]]
976 %div = sdiv i32 %x, 3
977 %neg = sub nsw i32 0, %div
981 define i32 @test_exact_nonsw_noexact(i32 %x) {
982 ; CHECK-LABEL: @test_exact_nonsw_noexact(
983 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv i32 [[X:%.*]], -3
984 ; CHECK-NEXT: ret i32 [[DIV_NEG]]
986 %div = sdiv i32 %x, 3
987 %neg = sub i32 0, %div
991 define i32 @test_exact_div_nonconst(i32 %x, i32 %y) {
992 ; CHECK-LABEL: @test_exact_div_nonconst(
993 ; CHECK-NEXT: [[DIV:%.*]] = sdiv exact i32 [[X:%.*]], [[Y:%.*]]
994 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[DIV]]
995 ; CHECK-NEXT: ret i32 [[NEG]]
997 %div = sdiv exact i32 %x, %y
998 %neg = sub nsw i32 0, %div
1002 define i32 @test_exact_div_one(i32 %x) {
1003 ; CHECK-LABEL: @test_exact_div_one(
1004 ; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[X:%.*]]
1005 ; CHECK-NEXT: ret i32 [[NEG]]
1007 %div = sdiv exact i32 %x, 1
1008 %neg = sub nsw i32 0, %div
1012 define i8 @test_exact_div_minSigned(i8 %x) {
1013 ; CHECK-LABEL: @test_exact_div_minSigned(
1014 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
1015 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sext i1 [[TMP1]] to i8
1016 ; CHECK-NEXT: ret i8 [[DIV_NEG]]
1018 %div = sdiv exact i8 %x, -128
1019 %neg = sub nsw i8 0, %div
1023 ; X / INT_MIN --> X == INT_MIN
1025 define i8 @sdiv_by_int_min(i8 %x) {
1026 ; CHECK-LABEL: @sdiv_by_int_min(
1027 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i8 [[X:%.*]], -128
1028 ; CHECK-NEXT: [[D:%.*]] = zext i1 [[TMP1]] to i8
1029 ; CHECK-NEXT: ret i8 [[D]]
1031 %d = sdiv i8 %x, -128
1035 define <2 x i8> @sdiv_by_int_min_vec_splat(<2 x i8> %x) {
1036 ; CHECK-LABEL: @sdiv_by_int_min_vec_splat(
1037 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
1038 ; CHECK-NEXT: [[D:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8>
1039 ; CHECK-NEXT: ret <2 x i8> [[D]]
1041 %d = sdiv <2 x i8> %x, <i8 -128, i8 -128>
1045 define <2 x i8> @sdiv_by_int_min_vec_splat_poison(<2 x i8> %x) {
1046 ; CHECK-LABEL: @sdiv_by_int_min_vec_splat_poison(
1047 ; CHECK-NEXT: ret <2 x i8> poison
1049 %d = sdiv <2 x i8> %x, <i8 -128, i8 poison>
1053 define <2 x i8> @sdiv_by_negconst_v2i8(<2 x i8> %x) {
1054 ; CHECK-LABEL: @sdiv_by_negconst_v2i8(
1055 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv <2 x i8> [[X:%.*]], <i8 108, i8 108>
1056 ; CHECK-NEXT: ret <2 x i8> [[DIV_NEG]]
1058 %div = sdiv <2 x i8> %x, <i8 -108, i8 -108>
1059 %sub = sub <2 x i8> zeroinitializer, %div
1063 define <vscale x 2 x i8> @sdiv_by_negconst_nxv2i8(<vscale x 2 x i8> %x) {
1064 ; CHECK-LABEL: @sdiv_by_negconst_nxv2i8(
1065 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sdiv <vscale x 2 x i8> [[X:%.*]], shufflevector (<vscale x 2 x i8> insertelement (<vscale x 2 x i8> poison, i8 108, i64 0), <vscale x 2 x i8> poison, <vscale x 2 x i32> zeroinitializer)
1066 ; CHECK-NEXT: ret <vscale x 2 x i8> [[DIV_NEG]]
1068 %div = sdiv <vscale x 2 x i8> %x, splat (i8 -108)
1069 %sub = sub <vscale x 2 x i8> zeroinitializer, %div
1070 ret <vscale x 2 x i8> %sub
1073 define <2 x i8> @sdiv_by_minSigned_v2i8(<2 x i8> %x) {
1074 ; CHECK-LABEL: @sdiv_by_minSigned_v2i8(
1075 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -128, i8 -128>
1076 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
1077 ; CHECK-NEXT: ret <2 x i8> [[DIV_NEG]]
1079 %div = sdiv <2 x i8> %x, <i8 -128, i8 -128>
1080 %sub = sub <2 x i8> zeroinitializer, %div
1084 define <vscale x 2 x i8> @sdiv_by_minSigned_nxv2i8(<vscale x 2 x i8> %x) {
1085 ; CHECK-LABEL: @sdiv_by_minSigned_nxv2i8(
1086 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <vscale x 2 x i8> [[X:%.*]], shufflevector (<vscale x 2 x i8> insertelement (<vscale x 2 x i8> poison, i8 -128, i64 0), <vscale x 2 x i8> poison, <vscale x 2 x i32> zeroinitializer)
1087 ; CHECK-NEXT: [[DIV_NEG:%.*]] = sext <vscale x 2 x i1> [[TMP1]] to <vscale x 2 x i8>
1088 ; CHECK-NEXT: ret <vscale x 2 x i8> [[DIV_NEG]]
1090 %div = sdiv <vscale x 2 x i8> %x, splat (i8 -128)
1091 %sub = sub <vscale x 2 x i8> zeroinitializer, %div
1092 ret <vscale x 2 x i8> %sub
1095 define i32 @sdiv_constant_dividend_select_of_constants_divisor(i1 %b) {
1096 ; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor(
1097 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i32 3, i32 -14
1098 ; CHECK-NEXT: ret i32 [[R]]
1100 %s = select i1 %b, i32 12, i32 -3
1101 %r = sdiv i32 42, %s
1105 define i32 @sdiv_constant_dividend_select_of_constants_divisor_use(i1 %b) {
1106 ; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor_use(
1107 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 -3
1108 ; CHECK-NEXT: call void @use(i32 [[S]])
1109 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B]], i32 3, i32 -14
1110 ; CHECK-NEXT: ret i32 [[R]]
1112 %s = select i1 %b, i32 12, i32 -3
1113 call void @use(i32 %s)
1114 %r = sdiv i32 42, %s
1118 define i32 @sdiv_constant_dividend_select_of_constants_divisor_0_arm(i1 %b) {
1119 ; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor_0_arm(
1120 ; CHECK-NEXT: ret i32 3
1122 %s = select i1 %b, i32 12, i32 0
1123 %r = sdiv i32 42, %s
1127 ; negative test - not safe to speculate div with variable divisor
1129 define i32 @sdiv_constant_dividend_select_divisor1(i1 %b, i32 %x) {
1130 ; CHECK-LABEL: @sdiv_constant_dividend_select_divisor1(
1131 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 [[X:%.*]], i32 -3
1132 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 42, [[S]]
1133 ; CHECK-NEXT: ret i32 [[R]]
1135 %s = select i1 %b, i32 %x, i32 -3
1136 %r = sdiv i32 42, %s
1140 ; negative test - not safe to speculate div with variable divisor
1142 define i32 @sdiv_constant_dividend_select_divisor2(i1 %b, i32 %x) {
1143 ; CHECK-LABEL: @sdiv_constant_dividend_select_divisor2(
1144 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 [[X:%.*]]
1145 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 42, [[S]]
1146 ; CHECK-NEXT: ret i32 [[R]]
1148 %s = select i1 %b, i32 12, i32 %x
1149 %r = sdiv i32 42, %s
1153 define <2 x i8> @sdiv_constant_dividend_select_of_constants_divisor_vec(i1 %b) {
1154 ; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor_vec(
1155 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 3, i8 8>, <2 x i8> <i8 -10, i8 -10>
1156 ; CHECK-NEXT: ret <2 x i8> [[R]]
1158 %s = select i1 %b, <2 x i8> <i8 12, i8 -5>, <2 x i8> <i8 -4, i8 4>
1159 %r = sdiv <2 x i8> <i8 42, i8 -42>, %s
1163 ; Div-by-0 element is immediate UB, so select is simplified.
1165 define <2 x i8> @sdiv_constant_dividend_select_of_constants_divisor_vec_ub1(i1 %b) {
1166 ; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor_vec_ub1(
1167 ; CHECK-NEXT: ret <2 x i8> <i8 -10, i8 -10>
1169 %s = select i1 %b, <2 x i8> <i8 0, i8 -5>, <2 x i8> <i8 -4, i8 4>
1170 %r = sdiv <2 x i8> <i8 42, i8 -42>, %s
1174 ; SMIN / -1 element is poison.
1176 define <2 x i8> @sdiv_constant_dividend_select_of_constants_divisor_vec_ub2(i1 %b) {
1177 ; CHECK-LABEL: @sdiv_constant_dividend_select_of_constants_divisor_vec_ub2(
1178 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 3, i8 25>, <2 x i8> <i8 -10, i8 poison>
1179 ; CHECK-NEXT: ret <2 x i8> [[R]]
1181 %s = select i1 %b, <2 x i8> <i8 12, i8 -5>, <2 x i8> <i8 -4, i8 -1>
1182 %r = sdiv <2 x i8> <i8 42, i8 -128>, %s
1186 ; negative test - must have constant dividend
1188 define i32 @sdiv_select_of_constants_divisor(i1 %b, i32 %x) {
1189 ; CHECK-LABEL: @sdiv_select_of_constants_divisor(
1190 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 -3
1191 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[X:%.*]], [[S]]
1192 ; CHECK-NEXT: ret i32 [[R]]
1194 %s = select i1 %b, i32 12, i32 -3
1195 %r = sdiv i32 %x, %s
1199 define i32 @udiv_constant_dividend_select_of_constants_divisor(i1 %b) {
1200 ; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor(
1201 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i32 3, i32 0
1202 ; CHECK-NEXT: ret i32 [[R]]
1204 %s = select i1 %b, i32 12, i32 -3
1205 %r = udiv i32 42, %s
1209 define i32 @udiv_constant_dividend_select_of_constants_divisor_use(i1 %b) {
1210 ; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor_use(
1211 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 -3
1212 ; CHECK-NEXT: call void @use(i32 [[S]])
1213 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B]], i32 3, i32 0
1214 ; CHECK-NEXT: ret i32 [[R]]
1216 %s = select i1 %b, i32 12, i32 -3
1217 call void @use(i32 %s)
1218 %r = udiv i32 42, %s
1222 ; Div-by-0 is immediate UB, so select is simplified.
1224 define i32 @udiv_constant_dividend_select_of_constants_divisor_0_arm(i1 %b) {
1225 ; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor_0_arm(
1226 ; CHECK-NEXT: ret i32 3
1228 %s = select i1 %b, i32 12, i32 0
1229 %r = udiv i32 42, %s
1233 ; negative test - not safe to speculate div with variable divisor
1235 define i32 @udiv_constant_dividend_select_divisor1(i1 %b, i32 %x) {
1236 ; CHECK-LABEL: @udiv_constant_dividend_select_divisor1(
1237 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 [[X:%.*]], i32 -3
1238 ; CHECK-NEXT: [[R:%.*]] = udiv i32 42, [[S]]
1239 ; CHECK-NEXT: ret i32 [[R]]
1241 %s = select i1 %b, i32 %x, i32 -3
1242 %r = udiv i32 42, %s
1246 ; negative test - not safe to speculate div with variable divisor
1248 define i32 @udiv_constant_dividend_select_divisor2(i1 %b, i32 %x) {
1249 ; CHECK-LABEL: @udiv_constant_dividend_select_divisor2(
1250 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 [[X:%.*]]
1251 ; CHECK-NEXT: [[R:%.*]] = udiv i32 42, [[S]]
1252 ; CHECK-NEXT: ret i32 [[R]]
1254 %s = select i1 %b, i32 12, i32 %x
1255 %r = udiv i32 42, %s
1259 define <2 x i8> @udiv_constant_dividend_select_of_constants_divisor_vec(i1 %b) {
1260 ; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor_vec(
1261 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 3, i8 0>, <2 x i8> <i8 0, i8 53>
1262 ; CHECK-NEXT: ret <2 x i8> [[R]]
1264 %s = select i1 %b, <2 x i8> <i8 12, i8 -5>, <2 x i8> <i8 -4, i8 4>
1265 %r = udiv <2 x i8> <i8 42, i8 -42>, %s
1269 ; Div-by-0 element is immediate UB, so select is simplified.
1271 define <2 x i8> @udiv_constant_dividend_select_of_constants_divisor_vec_ub1(i1 %b) {
1272 ; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor_vec_ub1(
1273 ; CHECK-NEXT: ret <2 x i8> <i8 0, i8 53>
1275 %s = select i1 %b, <2 x i8> <i8 0, i8 -5>, <2 x i8> <i8 -4, i8 4>
1276 %r = udiv <2 x i8> <i8 42, i8 -42>, %s
1280 ; There's no unsigned equivalent to "SMIN / -1", so this is just the usual constant folding.
1282 define <2 x i8> @udiv_constant_dividend_select_of_constants_divisor_vec_ub2(i1 %b) {
1283 ; CHECK-LABEL: @udiv_constant_dividend_select_of_constants_divisor_vec_ub2(
1284 ; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], <2 x i8> <i8 3, i8 0>, <2 x i8> zeroinitializer
1285 ; CHECK-NEXT: ret <2 x i8> [[R]]
1287 %s = select i1 %b, <2 x i8> <i8 12, i8 -5>, <2 x i8> <i8 -4, i8 -1>
1288 %r = udiv <2 x i8> <i8 42, i8 -128>, %s
1292 ; negative test - must have constant dividend
1294 define i32 @udiv_select_of_constants_divisor(i1 %b, i32 %x) {
1295 ; CHECK-LABEL: @udiv_select_of_constants_divisor(
1296 ; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 12, i32 -3
1297 ; CHECK-NEXT: [[R:%.*]] = udiv i32 [[X:%.*]], [[S]]
1298 ; CHECK-NEXT: ret i32 [[R]]
1300 %s = select i1 %b, i32 12, i32 -3
1301 %r = udiv i32 %x, %s
1308 define i1 @sdiv_one_icmpeq_one(i32 %x) {
1309 ; CHECK-LABEL: @sdiv_one_icmpeq_one(
1310 ; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
1311 ; CHECK-NEXT: [[B1:%.*]] = icmp eq i32 [[X_FR]], 1
1312 ; CHECK-NEXT: ret i1 [[B1]]
1315 %B = icmp eq i32 %A, 1
1319 define i1 @sdiv_one_icmpeq_negone(i32 %x) {
1320 ; CHECK-LABEL: @sdiv_one_icmpeq_negone(
1321 ; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
1322 ; CHECK-NEXT: [[B1:%.*]] = icmp eq i32 [[X_FR]], -1
1323 ; CHECK-NEXT: ret i1 [[B1]]
1326 %B = icmp eq i32 %A, -1
1330 define i1 @udiv_one_icmpeq_one(i32 %x) {
1331 ; CHECK-LABEL: @udiv_one_icmpeq_one(
1332 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 1
1333 ; CHECK-NEXT: ret i1 [[TMP1]]
1336 %B = icmp eq i32 %A, 1
1340 define i1 @udiv_one_icmpne_one(i32 %x) {
1341 ; CHECK-LABEL: @udiv_one_icmpne_one(
1342 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], 1
1343 ; CHECK-NEXT: ret i1 [[TMP1]]
1346 %B = icmp ne i32 %A, 1
1350 ; ((X * Y) / Z) / X --> Y / Z
1352 define i8 @udiv_udiv_mul_nuw(i8 %x, i8 %y, i8 %z) {
1353 ; CHECK-LABEL: @udiv_udiv_mul_nuw(
1354 ; CHECK-NEXT: [[R:%.*]] = udiv i8 [[Y:%.*]], [[Z:%.*]]
1355 ; CHECK-NEXT: ret i8 [[R]]
1357 %m = mul nuw i8 %x, %y
1363 ; exact propagates and commute is ok
1365 define i8 @udiv_udiv_mul_nuw_exact_exact(i8 %x, i8 %y, i8 %z) {
1366 ; CHECK-LABEL: @udiv_udiv_mul_nuw_exact_exact(
1367 ; CHECK-NEXT: [[R:%.*]] = udiv exact i8 [[Y:%.*]], [[Z:%.*]]
1368 ; CHECK-NEXT: ret i8 [[R]]
1370 %m = mul nuw i8 %y, %x
1371 %d = udiv exact i8 %m, %z
1372 %r = udiv exact i8 %d, %x
1378 define i32 @udiv_udiv_mul_nuw_exact_use(i32 %x, i32 %y, i32 %z) {
1379 ; CHECK-LABEL: @udiv_udiv_mul_nuw_exact_use(
1380 ; CHECK-NEXT: [[M:%.*]] = mul nuw i32 [[X:%.*]], [[Y:%.*]]
1381 ; CHECK-NEXT: call void @use(i32 [[M]])
1382 ; CHECK-NEXT: [[R:%.*]] = udiv i32 [[Y]], [[Z:%.*]]
1383 ; CHECK-NEXT: ret i32 [[R]]
1385 %m = mul nuw i32 %x, %y
1386 call void @use(i32 %m)
1387 %d = udiv exact i32 %m, %z
1388 %r = udiv i32 %d, %x
1392 ; negative test - must have nuw
1394 define i8 @udiv_udiv_mul_nsw(i8 %x, i8 %y, i8 %z) {
1395 ; CHECK-LABEL: @udiv_udiv_mul_nsw(
1396 ; CHECK-NEXT: [[M:%.*]] = mul nsw i8 [[X:%.*]], [[Y:%.*]]
1397 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[M]], [[Z:%.*]]
1398 ; CHECK-NEXT: [[R:%.*]] = udiv i8 [[D]], [[X]]
1399 ; CHECK-NEXT: ret i8 [[R]]
1401 %m = mul nsw i8 %x, %y
1407 ; negative test - opcode mismatch
1409 define i8 @udiv_sdiv_mul_nuw(i8 %x, i8 %y, i8 %z) {
1410 ; CHECK-LABEL: @udiv_sdiv_mul_nuw(
1411 ; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
1412 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[M]], [[Z:%.*]]
1413 ; CHECK-NEXT: [[R:%.*]] = udiv i8 [[D]], [[X]]
1414 ; CHECK-NEXT: ret i8 [[R]]
1416 %m = mul nuw i8 %x, %y
1422 ; ((Y * X) / Z) / X --> Y / Z
1424 define <2 x i8> @sdiv_sdiv_mul_nsw(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
1425 ; CHECK-LABEL: @sdiv_sdiv_mul_nsw(
1426 ; CHECK-NEXT: [[R:%.*]] = sdiv <2 x i8> [[Y:%.*]], [[Z:%.*]]
1427 ; CHECK-NEXT: ret <2 x i8> [[R]]
1429 %m = mul nsw <2 x i8> %y, %x
1430 %d = sdiv <2 x i8> %m, %z
1431 %r = sdiv <2 x i8> %d, %x
1435 ; (X * C0) / (X * C1) --> C0 / C1
1436 define i8 @sdiv_mul_nsw_mul_nsw(i8 %x,i8 %y,i8 %z) {
1437 ; CHECK-LABEL: @sdiv_mul_nsw_mul_nsw(
1438 ; CHECK-NEXT: [[ADD4:%.*]] = mul nsw i8 [[X:%.*]], [[Z:%.*]]
1439 ; CHECK-NEXT: [[ADD5:%.*]] = mul nsw i8 [[X]], [[Y:%.*]]
1440 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[ADD5]], [[ADD4]]
1441 ; CHECK-NEXT: ret i8 [[DIV]]
1443 %add4 = mul nsw i8 %x, %z
1444 %add5 = mul nsw i8 %x, %y
1445 %div = sdiv i8 %add5, %add4
1449 define i8 @udiv_mul_nuw_mul_nuw(i8 %x,i8 %y,i8 %z) {
1450 ; CHECK-LABEL: @udiv_mul_nuw_mul_nuw(
1451 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[Y:%.*]], [[Z:%.*]]
1452 ; CHECK-NEXT: ret i8 [[DIV]]
1454 %add4 = mul nuw i8 %x, %z
1455 %add5 = mul nuw i8 %x, %y
1456 %div = udiv i8 %add5, %add4
1460 define i8 @sdiv_mul_nsw_constant_mul_nsw_constant(i8 %x) {
1461 ; CHECK-LABEL: @sdiv_mul_nsw_constant_mul_nsw_constant(
1462 ; CHECK-NEXT: ret i8 2
1464 %add4 = mul nsw i8 %x, 5
1465 %add5 = mul nsw i8 %x, 10
1466 %div = sdiv i8 %add5, %add4
1470 define i4 @sdiv_mul_nsw_constant_mul_constant(i4 %a) {
1471 ; CHECK-LABEL: @sdiv_mul_nsw_constant_mul_constant(
1472 ; CHECK-NEXT: [[ADD4:%.*]] = mul i4 [[A:%.*]], 3
1473 ; CHECK-NEXT: [[ADD5:%.*]] = mul nsw i4 [[A]], 6
1474 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[ADD5]], [[ADD4]]
1475 ; CHECK-NEXT: ret i4 [[DIV]]
1477 %add4 = mul i4 %a, 3
1478 %add5 = mul nsw i4 %a, 6
1479 %div = sdiv i4 %add5, %add4
1482 define i4 @sdiv_mul_nsw_constant_mul_constant2(i4 %a) {
1483 ; CHECK-LABEL: @sdiv_mul_nsw_constant_mul_constant2(
1484 ; CHECK-NEXT: [[ADD4:%.*]] = sub i4 0, [[A:%.*]]
1485 ; CHECK-NEXT: [[ADD5:%.*]] = shl i4 [[A]], 3
1486 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[ADD5]], [[ADD4]]
1487 ; CHECK-NEXT: ret i4 [[DIV]]
1489 %add4 = mul i4 %a, 15
1490 %add5 = mul nsw i4 %a, 8
1491 %div = sdiv i4 %add5, %add4
1495 define i4 @sdiv_mul_nsw_constant_mul_constant3(i4 %a) {
1496 ; CHECK-LABEL: @sdiv_mul_nsw_constant_mul_constant3(
1497 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i4 [[A:%.*]], -8
1498 ; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i4 1, i4 -1
1499 ; CHECK-NEXT: ret i4 [[DIV]]
1501 %add4 = mul i4 %a, 15
1502 %add5 = mul nsw i4 %a, 1
1503 %div = sdiv i4 %add5, %add4
1507 define i4 @sdiv_mul_nsw_mul(i4 %a) {
1508 ; CHECK-LABEL: @sdiv_mul_nsw_mul(
1509 ; CHECK-NEXT: [[ADD4:%.*]] = sub i4 0, [[A:%.*]]
1510 ; CHECK-NEXT: [[ADD5:%.*]] = shl i4 [[A]], 3
1511 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[ADD5]], [[ADD4]]
1512 ; CHECK-NEXT: ret i4 [[DIV]]
1514 %add4 = mul i4 %a, -1
1515 %add5 = mul nsw i4 %a, -8
1516 %div = sdiv i4 %add5, %add4
1520 define i4 @udiv_mul_nuw_constant_mul_constant(i4 %a) {
1521 ; CHECK-LABEL: @udiv_mul_nuw_constant_mul_constant(
1522 ; CHECK-NEXT: ret i4 2
1524 %add4 = mul i4 %a, 3
1525 %add5 = mul nuw i4 %a, 6
1526 %div = udiv i4 %add5, %add4
1530 define i4 @udiv_mul_nuw_mul_negative(i4 %a) {
1531 ; CHECK-LABEL: @udiv_mul_nuw_mul_negative(
1532 ; CHECK-NEXT: [[ADD4:%.*]] = mul i4 [[A:%.*]], -3
1533 ; CHECK-NEXT: [[ADD5:%.*]] = shl nuw i4 [[A]], 2
1534 ; CHECK-NEXT: [[DIV:%.*]] = udiv i4 [[ADD5]], [[ADD4]]
1535 ; CHECK-NEXT: ret i4 [[DIV]]
1537 %add4 = mul i4 %a, 13
1538 %add5 = mul nuw i4 %a, 4
1539 %div = udiv i4 %add5, %add4
1543 define i4 @sdiv_mul_nsw_mul_nsw_allones(i4 %a) {
1544 ; CHECK-LABEL: @sdiv_mul_nsw_mul_nsw_allones(
1545 ; CHECK-NEXT: [[ADD4:%.*]] = sub nsw i4 0, [[A:%.*]]
1546 ; CHECK-NEXT: [[ADD5:%.*]] = shl i4 [[A]], 3
1547 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[ADD5]], [[ADD4]]
1548 ; CHECK-NEXT: ret i4 [[DIV]]
1550 %add4 = mul nsw i4 %a, -1
1551 %add5 = mul nsw i4 %a, -8
1552 %div = sdiv i4 %add5, %add4
1556 define i4 @sdiv_mul_nsw_mul_signmask(i4 %a, i4 %c2) {
1557 ; CHECK-LABEL: @sdiv_mul_nsw_mul_signmask(
1558 ; CHECK-NEXT: [[ADD4:%.*]] = shl i4 [[A:%.*]], 3
1559 ; CHECK-NEXT: [[ADD5:%.*]] = mul nsw i4 [[A]], [[C2:%.*]]
1560 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i4 [[ADD5]], [[ADD4]]
1561 ; CHECK-NEXT: ret i4 [[DIV]]
1563 %add4 = mul nsw i4 %a, -8
1564 %add5 = mul nsw i4 %a, %c2
1565 %div = sdiv i4 %add5, %add4
1569 define i32 @sdiv_sub1(i32 %arg) {
1570 ; CHECK-LABEL: @sdiv_sub1(
1571 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
1572 ; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1573 ; CHECK-NEXT: ret i32 [[DIV]]
1575 %neg = sub i32 0, %arg
1576 %div = sdiv i32 %neg, %arg
1580 define i32 @sdiv_sub2(i32 %arg) {
1581 ; CHECK-LABEL: @sdiv_sub2(
1582 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG:%.*]], -2147483648
1583 ; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1584 ; CHECK-NEXT: ret i32 [[DIV]]
1586 %neg = sub i32 0, %arg
1587 %div = sdiv i32 %arg, %neg
1591 define i32 @sub_sdiv_multiuse(i32 %arg) {
1592 ; CHECK-LABEL: @sub_sdiv_multiuse(
1593 ; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[ARG:%.*]]
1594 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[ARG]], -2147483648
1595 ; CHECK-NEXT: [[DIV:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1596 ; CHECK-NEXT: call void @use(i32 [[NEG]])
1597 ; CHECK-NEXT: ret i32 [[DIV]]
1599 %neg = sub i32 0, %arg
1600 %div = sdiv i32 %arg, %neg
1601 call void @use(i32 %neg)
1605 define i32 @sdiv_sub_sub(i32 %x ,i32 %y) {
1606 ; CHECK-LABEL: @sdiv_sub_sub(
1607 ; CHECK-NEXT: [[S:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
1608 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[S]], -2147483648
1609 ; CHECK-NEXT: [[D:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1610 ; CHECK-NEXT: ret i32 [[D]]
1614 %d = sdiv i32 %s, %u
1618 define i32 @sdiv_mul_sub(i32 %x, i32 %y) {
1619 ; CHECK-LABEL: @sdiv_mul_sub(
1620 ; CHECK-NEXT: [[M:%.*]] = mul i32 [[Y:%.*]], [[X:%.*]]
1621 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[M]], -2147483648
1622 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i32 1, i32 -1
1623 ; CHECK-NEXT: ret i32 [[R]]
1627 %r = sdiv i32 %d, %m
1631 define i32 @sdiv_mul_sub_nsw(i32 %x, i32 %y) {
1632 ; CHECK-LABEL: @sdiv_mul_sub_nsw(
1633 ; CHECK-NEXT: ret i32 -1
1636 %n = sub nsw i32 0, %m
1637 %d = sdiv i32 %m, %n
1641 define i32 @sdiv_mul_nsw_sub_nsw(i32 %x, i32 %y) {
1642 ; CHECK-LABEL: @sdiv_mul_nsw_sub_nsw(
1643 ; CHECK-NEXT: ret i32 -1
1645 %m = mul nsw i32 %y, %x
1646 %n = sub nsw i32 0, %m
1647 %d = sdiv i32 %m, %n
1653 define i8 @sdiv_sdiv_mul_nsw_exact_exact(i8 %x, i8 %y, i8 %z) {
1654 ; CHECK-LABEL: @sdiv_sdiv_mul_nsw_exact_exact(
1655 ; CHECK-NEXT: [[R:%.*]] = sdiv exact i8 [[Y:%.*]], [[Z:%.*]]
1656 ; CHECK-NEXT: ret i8 [[R]]
1658 %m = mul nsw i8 %x, %y
1659 %d = sdiv exact i8 %m, %z
1660 %r = sdiv exact i8 %d, %x
1666 define i32 @sdiv_sdiv_mul_nsw_exact_use(i32 %x, i32 %y, i32 %z) {
1667 ; CHECK-LABEL: @sdiv_sdiv_mul_nsw_exact_use(
1668 ; CHECK-NEXT: [[M:%.*]] = mul nsw i32 [[X:%.*]], [[Y:%.*]]
1669 ; CHECK-NEXT: [[D:%.*]] = sdiv i32 [[M]], [[Z:%.*]]
1670 ; CHECK-NEXT: call void @use(i32 [[D]])
1671 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[Y]], [[Z]]
1672 ; CHECK-NEXT: ret i32 [[R]]
1674 %m = mul nsw i32 %x, %y
1675 %d = sdiv i32 %m, %z
1676 call void @use(i32 %d)
1677 %r = sdiv exact i32 %d, %x
1681 ; negative test - must have nsw
1683 define i8 @sdiv_sdiv_mul_nuw(i8 %x, i8 %y, i8 %z) {
1684 ; CHECK-LABEL: @sdiv_sdiv_mul_nuw(
1685 ; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
1686 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[M]], [[Z:%.*]]
1687 ; CHECK-NEXT: [[R:%.*]] = sdiv i8 [[D]], [[X]]
1688 ; CHECK-NEXT: ret i8 [[R]]
1690 %m = mul nuw i8 %x, %y
1696 ; negative test - opcode mismatch
1698 define i8 @sdiv_udiv_mul_nsw(i8 %x, i8 %y, i8 %z) {
1699 ; CHECK-LABEL: @sdiv_udiv_mul_nsw(
1700 ; CHECK-NEXT: [[M:%.*]] = mul nsw i8 [[X:%.*]], [[Y:%.*]]
1701 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[M]], [[Z:%.*]]
1702 ; CHECK-NEXT: [[R:%.*]] = sdiv i8 [[D]], [[X]]
1703 ; CHECK-NEXT: ret i8 [[R]]
1705 %m = mul nsw i8 %x, %y
1711 ; ((X * C2) + C1) / C2 --> X + C1/C2
1713 define i6 @sdiv_distribute_mul_nsw_add_nsw(i6 %x) {
1714 ; CHECK-LABEL: @sdiv_distribute_mul_nsw_add_nsw(
1715 ; CHECK-NEXT: [[DIV:%.*]] = add nsw i6 [[X:%.*]], -5
1716 ; CHECK-NEXT: ret i6 [[DIV]]
1718 %mul = mul nsw i6 %x, 3
1719 %add = add nsw i6 %mul, -15
1720 %div = sdiv i6 %add, 3
1726 define i32 @sdiv_distribute_mul_nsw_add_nsw_uses(i32 %x) {
1727 ; CHECK-LABEL: @sdiv_distribute_mul_nsw_add_nsw_uses(
1728 ; CHECK-NEXT: [[MUL:%.*]] = mul nsw i32 [[X:%.*]], 42
1729 ; CHECK-NEXT: call void @use(i32 [[MUL]])
1730 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[MUL]], 126
1731 ; CHECK-NEXT: call void @use(i32 [[ADD]])
1732 ; CHECK-NEXT: [[DIV:%.*]] = add nsw i32 [[X]], 3
1733 ; CHECK-NEXT: ret i32 [[DIV]]
1735 %mul = mul nsw i32 %x, 42
1736 call void @use(i32 %mul)
1737 %add = add nsw i32 %mul, 126
1738 call void @use(i32 %add)
1739 %div = sdiv i32 %add, 42
1743 ; vector splats work
1745 define <2 x i6> @udiv_distribute_mul_nuw_add_nuw(<2 x i6> %x) {
1746 ; CHECK-LABEL: @udiv_distribute_mul_nuw_add_nuw(
1747 ; CHECK-NEXT: [[DIV:%.*]] = add nuw <2 x i6> [[X:%.*]], <i6 5, i6 5>
1748 ; CHECK-NEXT: ret <2 x i6> [[DIV]]
1750 %mul = mul nuw <2 x i6> %x, <i6 3, i6 3>
1751 %add = add nuw <2 x i6> %mul, <i6 15, i6 15>
1752 %div = udiv <2 x i6> %add, <i6 3, i6 3>
1756 ; negative test - constants must be evenly divisible
1758 define i6 @sdiv_distribute_mul_nsw_add_nsw_not_multiple_offset(i6 %x) {
1759 ; CHECK-LABEL: @sdiv_distribute_mul_nsw_add_nsw_not_multiple_offset(
1760 ; CHECK-NEXT: [[MUL:%.*]] = mul nsw i6 [[X:%.*]], 3
1761 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i6 [[MUL]], -16
1762 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i6 [[ADD]], 3
1763 ; CHECK-NEXT: ret i6 [[DIV]]
1765 %mul = mul nsw i6 %x, 3
1766 %add = add nsw i6 %mul, -16
1767 %div = sdiv i6 %add, 3
1771 ; constants do not have to be evenly divisible with unsigned division
1773 define i6 @udiv_distribute_mul_nuw_add_nuw_not_multiple_offset(i6 %x) {
1774 ; CHECK-LABEL: @udiv_distribute_mul_nuw_add_nuw_not_multiple_offset(
1775 ; CHECK-NEXT: [[DIV:%.*]] = add nuw i6 [[X:%.*]], 2
1776 ; CHECK-NEXT: ret i6 [[DIV]]
1778 %mul = mul nuw i6 %x, 3
1779 %add = add nuw i6 %mul, 7
1780 %div = udiv i6 %add, 3
1784 ; negative test - wrong no-wrap
1786 define i6 @sdiv_distribute_mul_nuw_add_nsw(i6 %x) {
1787 ; CHECK-LABEL: @sdiv_distribute_mul_nuw_add_nsw(
1788 ; CHECK-NEXT: [[MUL:%.*]] = mul nuw i6 [[X:%.*]], 3
1789 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i6 [[MUL]], -15
1790 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i6 [[ADD]], 3
1791 ; CHECK-NEXT: ret i6 [[DIV]]
1793 %mul = mul nuw i6 %x, 3
1794 %add = add nsw i6 %mul, -15
1795 %div = sdiv i6 %add, 3
1799 ; negative test - wrong no-wrap
1801 define i6 @udiv_distribute_mul_nsw_add_nuw(i6 %x) {
1802 ; CHECK-LABEL: @udiv_distribute_mul_nsw_add_nuw(
1803 ; CHECK-NEXT: [[MUL:%.*]] = mul nsw i6 [[X:%.*]], 3
1804 ; CHECK-NEXT: [[ADD:%.*]] = add nuw i6 [[MUL]], 9
1805 ; CHECK-NEXT: [[DIV:%.*]] = udiv i6 [[ADD]], 3
1806 ; CHECK-NEXT: ret i6 [[DIV]]
1808 %mul = mul nsw i6 %x, 3
1809 %add = add nuw i6 %mul, 9
1810 %div = udiv i6 %add, 3
1814 define i32 @fold_disjoint_or_over_sdiv(i32 %x) {
1815 ; CHECK-LABEL: @fold_disjoint_or_over_sdiv(
1816 ; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[X:%.*]], 9
1817 ; CHECK-NEXT: ret i32 [[R]]
1819 %mul = mul nsw i32 %x, 9
1820 %or = or disjoint i32 %mul, 81
1821 %r = sdiv i32 %or, 9
1825 define i32 @fold_disjoint_or_over_udiv(i32 %x) {
1826 ; CHECK-LABEL: @fold_disjoint_or_over_udiv(
1827 ; CHECK-NEXT: [[R:%.*]] = add nuw i32 [[X:%.*]], 9
1828 ; CHECK-NEXT: ret i32 [[R]]
1830 %mul = mul nuw i32 %x, 9
1831 %or = or disjoint i32 %mul, 81
1832 %r = udiv i32 %or, 9