1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
5 declare void @use32(i32)
7 declare i8 @llvm.umin.i8(i8, i8)
8 declare i8 @llvm.umax.i8(i8, i8)
9 declare i8 @llvm.smin.i8(i8, i8)
10 declare i8 @llvm.smax.i8(i8, i8)
12 define i32 @t1(i16 zeroext %x, i32 %y) {
14 ; CHECK-NEXT: [[CONV:%.*]] = zext i16 [[X:%.*]] to i32
15 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[Y:%.*]], 1
16 ; CHECK-NEXT: [[D1:%.*]] = lshr i32 [[CONV]], [[TMP1]]
17 ; CHECK-NEXT: ret i32 [[D1]]
19 %conv = zext i16 %x to i32
21 %d = sdiv i32 %conv, %s
25 define <2 x i32> @t1vec(<2 x i16> %x, <2 x i32> %y) {
26 ; CHECK-LABEL: @t1vec(
27 ; CHECK-NEXT: [[CONV:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i32>
28 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i32> [[Y:%.*]], <i32 1, i32 1>
29 ; CHECK-NEXT: [[D1:%.*]] = lshr <2 x i32> [[CONV]], [[TMP1]]
30 ; CHECK-NEXT: ret <2 x i32> [[D1]]
32 %conv = zext <2 x i16> %x to <2 x i32>
33 %s = shl <2 x i32> <i32 2, i32 2>, %y
34 %d = sdiv <2 x i32> %conv, %s
39 define i64 @t2(i64 %x, i32 %y) {
41 ; CHECK-NEXT: [[TMP1:%.*]] = zext nneg i32 [[Y:%.*]] to i64
42 ; CHECK-NEXT: [[TMP2:%.*]] = lshr i64 [[X:%.*]], [[TMP1]]
43 ; CHECK-NEXT: ret i64 [[TMP2]]
46 %2 = zext i32 %1 to i64
52 define i64 @t3(i64 %x, i32 %y) {
54 ; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[Y:%.*]], 2
55 ; CHECK-NEXT: [[TMP2:%.*]] = zext nneg i32 [[TMP1]] to i64
56 ; CHECK-NEXT: [[TMP3:%.*]] = lshr i64 [[X:%.*]], [[TMP2]]
57 ; CHECK-NEXT: ret i64 [[TMP3]]
60 %2 = zext i32 %1 to i64
65 define i32 @t4(i32 %x, i32 %y) {
67 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.umax.i32(i32 [[Y:%.*]], i32 5)
68 ; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[X:%.*]], [[TMP1]]
69 ; CHECK-NEXT: ret i32 [[TMP2]]
72 %2 = icmp ult i32 %1, 32
73 %3 = select i1 %2, i32 32, i32 %1
78 define i32 @t5(i1 %x, i1 %y, i32 %V) {
80 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[X:%.*]], i32 5, i32 6
81 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[Y:%.*]], i32 [[TMP1]], i32 [[V:%.*]]
82 ; CHECK-NEXT: [[TMP3:%.*]] = lshr i32 [[V]], [[TMP2]]
83 ; CHECK-NEXT: ret i32 [[TMP3]]
86 %2 = select i1 %x, i32 32, i32 64
87 %3 = select i1 %y, i32 %2, i32 %1
92 define i32 @t6(i32 %x, i32 %z) {
94 ; CHECK-NEXT: [[DIVISOR:%.*]] = call i32 @llvm.umax.i32(i32 [[X:%.*]], i32 1)
95 ; CHECK-NEXT: [[Y:%.*]] = udiv i32 [[Z:%.*]], [[DIVISOR]]
96 ; CHECK-NEXT: ret i32 [[Y]]
98 %x_is_zero = icmp eq i32 %x, 0
99 %divisor = select i1 %x_is_zero, i32 1, i32 %x
100 %y = udiv i32 %z, %divisor
104 define i8 @udiv_umin(i8 %x, i8 %y, i8 %z) {
105 ; CHECK-LABEL: @udiv_umin(
106 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[Y:%.*]], i8 [[Z:%.*]])
107 ; CHECK-NEXT: [[D1:%.*]] = lshr i8 [[X:%.*]], [[TMP1]]
108 ; CHECK-NEXT: ret i8 [[D1]]
112 %m = call i8 @llvm.umin.i8(i8 %y2, i8 %z2)
117 define i8 @udiv_umax(i8 %x, i8 %y, i8 %z) {
118 ; CHECK-LABEL: @udiv_umax(
119 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umax.i8(i8 [[Y:%.*]], i8 [[Z:%.*]])
120 ; CHECK-NEXT: [[D1:%.*]] = lshr i8 [[X:%.*]], [[TMP1]]
121 ; CHECK-NEXT: ret i8 [[D1]]
125 %m = call i8 @llvm.umax.i8(i8 %y2, i8 %z2)
130 ; Negative test, cannot take exact log2
131 define i8 @udiv_umin_(i8 %x, i8 %y, i8 %z) {
132 ; CHECK-LABEL: @udiv_umin_(
133 ; CHECK-NEXT: [[Y2:%.*]] = shl nuw i8 1, [[Y:%.*]]
134 ; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[Y2]], i8 [[Z:%.*]])
135 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[M]]
136 ; CHECK-NEXT: ret i8 [[D]]
139 %m = call i8 @llvm.umin.i8(i8 %y2, i8 %z)
144 ; Negative test, extra use
145 define i8 @udiv_umin_extra_use(i8 %x, i8 %y, i8 %z) {
146 ; CHECK-LABEL: @udiv_umin_extra_use(
147 ; CHECK-NEXT: [[Y2:%.*]] = shl nuw i8 1, [[Y:%.*]]
148 ; CHECK-NEXT: [[Z2:%.*]] = shl nuw i8 1, [[Z:%.*]]
149 ; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.umin.i8(i8 [[Y2]], i8 [[Z2]])
150 ; CHECK-NEXT: call void @use(i8 [[M]])
151 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[M]]
152 ; CHECK-NEXT: ret i8 [[D]]
156 %m = call i8 @llvm.umin.i8(i8 %y2, i8 %z2)
157 call void @use(i8 %m)
162 ; Negative test, signed min/max
163 define i8 @udiv_smin(i8 %x, i8 %y, i8 %z) {
164 ; CHECK-LABEL: @udiv_smin(
165 ; CHECK-NEXT: [[Y2:%.*]] = shl nuw i8 1, [[Y:%.*]]
166 ; CHECK-NEXT: [[Z2:%.*]] = shl nuw i8 1, [[Z:%.*]]
167 ; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smin.i8(i8 [[Y2]], i8 [[Z2]])
168 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[M]]
169 ; CHECK-NEXT: ret i8 [[D]]
173 %m = call i8 @llvm.smin.i8(i8 %y2, i8 %z2)
178 ; Negative test, signed min/max
179 define i8 @udiv_smax(i8 %x, i8 %y, i8 %z) {
180 ; CHECK-LABEL: @udiv_smax(
181 ; CHECK-NEXT: [[Y2:%.*]] = shl nuw i8 1, [[Y:%.*]]
182 ; CHECK-NEXT: [[Z2:%.*]] = shl nuw i8 1, [[Z:%.*]]
183 ; CHECK-NEXT: [[M:%.*]] = call i8 @llvm.smax.i8(i8 [[Y2]], i8 [[Z2]])
184 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[M]]
185 ; CHECK-NEXT: ret i8 [[D]]
189 %m = call i8 @llvm.smax.i8(i8 %y2, i8 %z2)
194 ; (X << C1) / X -> 1 << C1 optimizations
196 define i32 @t7(i32 %x) {
198 ; CHECK-NEXT: ret i32 4
200 %shl = shl nsw i32 %x, 2
201 %r = sdiv i32 %shl, %x
205 ; make sure the previous opt doesn't take place for wrapped shifts
207 define i32 @t8(i32 %x) {
209 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 2
210 ; CHECK-NEXT: [[R:%.*]] = sdiv i32 [[SHL]], [[X]]
211 ; CHECK-NEXT: ret i32 [[R]]
214 %r = sdiv i32 %shl, %x
218 define <2 x i32> @t9(<2 x i32> %x) {
220 ; CHECK-NEXT: ret <2 x i32> <i32 4, i32 8>
222 %shl = shl nsw <2 x i32> %x, <i32 2, i32 3>
223 %r = sdiv <2 x i32> %shl, %x
227 define i32 @t10(i32 %x, i32 %y) {
229 ; CHECK-NEXT: [[R:%.*]] = shl nuw nsw i32 1, [[Y:%.*]]
230 ; CHECK-NEXT: ret i32 [[R]]
232 %shl = shl nsw i32 %x, %y
233 %r = sdiv i32 %shl, %x
237 define <2 x i32> @t11(<2 x i32> %x, <2 x i32> %y) {
239 ; CHECK-NEXT: [[R:%.*]] = shl nuw nsw <2 x i32> <i32 1, i32 1>, [[Y:%.*]]
240 ; CHECK-NEXT: ret <2 x i32> [[R]]
242 %shl = shl nsw <2 x i32> %x, %y
243 %r = sdiv <2 x i32> %shl, %x
247 define i32 @t12(i32 %x) {
249 ; CHECK-NEXT: ret i32 4
251 %shl = shl nuw i32 %x, 2
252 %r = udiv i32 %shl, %x
256 ; make sure the previous opt doesn't take place for wrapped shifts
258 define i32 @t13(i32 %x) {
260 ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], 2
261 ; CHECK-NEXT: [[R:%.*]] = udiv i32 [[SHL]], [[X]]
262 ; CHECK-NEXT: ret i32 [[R]]
265 %r = udiv i32 %shl, %x
269 define <2 x i32> @t14(<2 x i32> %x) {
271 ; CHECK-NEXT: ret <2 x i32> <i32 4, i32 8>
273 %shl = shl nuw <2 x i32> %x, <i32 2, i32 3>
274 %r = udiv <2 x i32> %shl, %x
278 define i32 @t15(i32 %x, i32 %y) {
280 ; CHECK-NEXT: [[R:%.*]] = shl nuw i32 1, [[Y:%.*]]
281 ; CHECK-NEXT: ret i32 [[R]]
283 %shl = shl nuw i32 %x, %y
284 %r = udiv i32 %shl, %x
288 define <2 x i32> @t16(<2 x i32> %x, <2 x i32> %y) {
290 ; CHECK-NEXT: [[R:%.*]] = shl nuw <2 x i32> <i32 1, i32 1>, [[Y:%.*]]
291 ; CHECK-NEXT: ret <2 x i32> [[R]]
293 %shl = shl nuw <2 x i32> %x, %y
294 %r = udiv <2 x i32> %shl, %x
298 ; (X * Y) s/ (X << Z) --> Y s/ (1 << Z)
300 define i5 @sdiv_mul_shl_nsw(i5 %x, i5 %y, i5 %z) {
301 ; CHECK-LABEL: @sdiv_mul_shl_nsw(
302 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i5 1, [[Z:%.*]]
303 ; CHECK-NEXT: [[D:%.*]] = sdiv i5 [[Y:%.*]], [[TMP1]]
304 ; CHECK-NEXT: ret i5 [[D]]
306 %m1 = mul nsw i5 %x, %y
307 %m2 = shl nsw i5 %x, %z
308 %d = sdiv i5 %m1, %m2
312 ; (Y * Z) s/ (X << Z) --> Y s/ (1 << Z)
314 define i5 @sdiv_mul_shl_nsw_exact_commute1(i5 %x, i5 %y, i5 %z) {
315 ; CHECK-LABEL: @sdiv_mul_shl_nsw_exact_commute1(
316 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i5 1, [[Z:%.*]]
317 ; CHECK-NEXT: [[D:%.*]] = sdiv exact i5 [[Y:%.*]], [[TMP1]]
318 ; CHECK-NEXT: ret i5 [[D]]
320 %m1 = mul nsw i5 %y, %x
321 %m2 = shl nsw i5 %x, %z
322 %d = sdiv exact i5 %m1, %m2
326 ; negative test - shl is not commutative
328 define i5 @sdiv_mul_shl_nsw_commute2(i5 %x, i5 %y, i5 %z) {
329 ; CHECK-LABEL: @sdiv_mul_shl_nsw_commute2(
330 ; CHECK-NEXT: [[M1:%.*]] = mul nsw i5 [[Y:%.*]], [[X:%.*]]
331 ; CHECK-NEXT: [[M2:%.*]] = shl nsw i5 [[Z:%.*]], [[X]]
332 ; CHECK-NEXT: [[D:%.*]] = sdiv i5 [[M1]], [[M2]]
333 ; CHECK-NEXT: ret i5 [[D]]
335 %m1 = mul nsw i5 %y, %x
336 %m2 = shl nsw i5 %z, %x
337 %d = sdiv i5 %m1, %m2
343 define i8 @sdiv_mul_shl_nsw_use1(i8 %x, i8 %y, i8 %z) {
344 ; CHECK-LABEL: @sdiv_mul_shl_nsw_use1(
345 ; CHECK-NEXT: [[M1:%.*]] = mul nsw i8 [[X:%.*]], [[Y:%.*]]
346 ; CHECK-NEXT: call void @use(i8 [[M1]])
347 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i8 1, [[Z:%.*]]
348 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[Y]], [[TMP1]]
349 ; CHECK-NEXT: ret i8 [[D]]
351 %m1 = mul nsw i8 %x, %y
352 call void @use(i8 %m1)
353 %m2 = shl nsw i8 %x, %z
354 %d = sdiv i8 %m1, %m2
360 define i8 @sdiv_mul_shl_nsw_use2(i8 %x, i8 %y, i8 %z) {
361 ; CHECK-LABEL: @sdiv_mul_shl_nsw_use2(
362 ; CHECK-NEXT: [[M2:%.*]] = shl nsw i8 [[X:%.*]], [[Z:%.*]]
363 ; CHECK-NEXT: call void @use(i8 [[M2]])
364 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i8 1, [[Z]]
365 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[Y:%.*]], [[TMP1]]
366 ; CHECK-NEXT: ret i8 [[D]]
368 %m1 = mul nsw i8 %x, %y
369 %m2 = shl nsw i8 %x, %z
370 call void @use(i8 %m2)
371 %d = sdiv i8 %m1, %m2
375 ; negative test - both operands can't have extra uses
377 define i8 @sdiv_mul_shl_nsw_use3(i8 %x, i8 %y, i8 %z) {
378 ; CHECK-LABEL: @sdiv_mul_shl_nsw_use3(
379 ; CHECK-NEXT: [[M1:%.*]] = mul nsw i8 [[X:%.*]], [[Y:%.*]]
380 ; CHECK-NEXT: call void @use(i8 [[M1]])
381 ; CHECK-NEXT: [[M2:%.*]] = shl nsw i8 [[X]], [[Z:%.*]]
382 ; CHECK-NEXT: call void @use(i8 [[M2]])
383 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[M1]], [[M2]]
384 ; CHECK-NEXT: ret i8 [[D]]
386 %m1 = mul nsw i8 %x, %y
387 call void @use(i8 %m1)
388 %m2 = shl nsw i8 %x, %z
389 call void @use(i8 %m2)
390 %d = sdiv i8 %m1, %m2
394 ; negative test - shl must be divisor
396 define i5 @sdiv_shl_mul_nsw(i5 %x, i5 %y, i5 %z) {
397 ; CHECK-LABEL: @sdiv_shl_mul_nsw(
398 ; CHECK-NEXT: [[M1:%.*]] = shl nsw i5 [[Z:%.*]], [[X:%.*]]
399 ; CHECK-NEXT: [[M2:%.*]] = mul nsw i5 [[X]], [[Y:%.*]]
400 ; CHECK-NEXT: [[D:%.*]] = sdiv i5 [[M1]], [[M2]]
401 ; CHECK-NEXT: ret i5 [[D]]
403 %m1 = shl nsw i5 %z, %x
404 %m2 = mul nsw i5 %x, %y
405 %d = sdiv i5 %m1, %m2
409 ; negative test - wrong no-wrap
411 define i5 @sdiv_mul_shl_missing_nsw1(i5 %x, i5 %y, i5 %z) {
412 ; CHECK-LABEL: @sdiv_mul_shl_missing_nsw1(
413 ; CHECK-NEXT: [[M1:%.*]] = mul nsw i5 [[X:%.*]], [[Y:%.*]]
414 ; CHECK-NEXT: [[M2:%.*]] = shl nuw i5 [[Y]], [[Z:%.*]]
415 ; CHECK-NEXT: [[D:%.*]] = sdiv i5 [[M1]], [[M2]]
416 ; CHECK-NEXT: ret i5 [[D]]
418 %m1 = mul nsw i5 %x, %y
419 %m2 = shl nuw i5 %y, %z
420 %d = sdiv i5 %m1, %m2
424 ; negative test - wrong no-wrap
426 define i5 @sdiv_mul_shl_missing_nsw2(i5 %x, i5 %y, i5 %z) {
427 ; CHECK-LABEL: @sdiv_mul_shl_missing_nsw2(
428 ; CHECK-NEXT: [[M1:%.*]] = mul nuw i5 [[X:%.*]], [[Y:%.*]]
429 ; CHECK-NEXT: [[M2:%.*]] = shl nsw i5 [[Y]], [[Z:%.*]]
430 ; CHECK-NEXT: [[D:%.*]] = sdiv i5 [[M1]], [[M2]]
431 ; CHECK-NEXT: ret i5 [[D]]
433 %m1 = mul nuw i5 %x, %y
434 %m2 = shl nsw i5 %y, %z
435 %d = sdiv i5 %m1, %m2
439 ; (X * Y) u/ (X << Z) --> Y u>> Z
441 define i5 @udiv_mul_shl_nuw(i5 %x, i5 %y, i5 %z) {
442 ; CHECK-LABEL: @udiv_mul_shl_nuw(
443 ; CHECK-NEXT: [[D:%.*]] = lshr i5 [[Y:%.*]], [[Z:%.*]]
444 ; CHECK-NEXT: ret i5 [[D]]
446 %m1 = mul nuw i5 %x, %y
447 %m2 = shl nuw i5 %x, %z
448 %d = udiv i5 %m1, %m2
452 ; (Y * X) u/ (X << Z) --> Y u>> Z
454 define i5 @udiv_mul_shl_nuw_exact_commute1(i5 %x, i5 %y, i5 %z) {
455 ; CHECK-LABEL: @udiv_mul_shl_nuw_exact_commute1(
456 ; CHECK-NEXT: [[D:%.*]] = lshr exact i5 [[Y:%.*]], [[Z:%.*]]
457 ; CHECK-NEXT: ret i5 [[D]]
459 %m1 = mul nuw i5 %y, %x
460 %m2 = shl nuw i5 %x, %z
461 %d = udiv exact i5 %m1, %m2
465 ; negative test - shl is not commutative
467 define i5 @udiv_mul_shl_nuw_commute2(i5 %x, i5 %y, i5 %z) {
468 ; CHECK-LABEL: @udiv_mul_shl_nuw_commute2(
469 ; CHECK-NEXT: [[M1:%.*]] = mul nuw i5 [[X:%.*]], [[Y:%.*]]
470 ; CHECK-NEXT: [[M2:%.*]] = shl nuw i5 [[Z:%.*]], [[X]]
471 ; CHECK-NEXT: [[D:%.*]] = udiv i5 [[M1]], [[M2]]
472 ; CHECK-NEXT: ret i5 [[D]]
474 %m1 = mul nuw i5 %x, %y
475 %m2 = shl nuw i5 %z, %x
476 %d = udiv i5 %m1, %m2
482 define i8 @udiv_mul_shl_nsw_use1(i8 %x, i8 %y, i8 %z) {
483 ; CHECK-LABEL: @udiv_mul_shl_nsw_use1(
484 ; CHECK-NEXT: [[M1:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
485 ; CHECK-NEXT: call void @use(i8 [[M1]])
486 ; CHECK-NEXT: [[D:%.*]] = lshr i8 [[Y]], [[Z:%.*]]
487 ; CHECK-NEXT: ret i8 [[D]]
489 %m1 = mul nuw i8 %x, %y
490 call void @use(i8 %m1)
491 %m2 = shl nuw i8 %x, %z
492 %d = udiv i8 %m1, %m2
498 define i8 @udiv_mul_shl_nsw_use2(i8 %x, i8 %y, i8 %z) {
499 ; CHECK-LABEL: @udiv_mul_shl_nsw_use2(
500 ; CHECK-NEXT: [[M2:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
501 ; CHECK-NEXT: call void @use(i8 [[M2]])
502 ; CHECK-NEXT: [[D:%.*]] = lshr i8 [[Y:%.*]], [[Z]]
503 ; CHECK-NEXT: ret i8 [[D]]
505 %m1 = mul nuw i8 %x, %y
506 %m2 = shl nuw i8 %x, %z
507 call void @use(i8 %m2)
508 %d = udiv i8 %m1, %m2
514 define i8 @udiv_mul_shl_nsw_use3(i8 %x, i8 %y, i8 %z) {
515 ; CHECK-LABEL: @udiv_mul_shl_nsw_use3(
516 ; CHECK-NEXT: [[M1:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
517 ; CHECK-NEXT: call void @use(i8 [[M1]])
518 ; CHECK-NEXT: [[M2:%.*]] = shl nuw i8 [[X]], [[Z:%.*]]
519 ; CHECK-NEXT: call void @use(i8 [[M2]])
520 ; CHECK-NEXT: [[D:%.*]] = lshr i8 [[Y]], [[Z]]
521 ; CHECK-NEXT: ret i8 [[D]]
523 %m1 = mul nuw i8 %x, %y
524 call void @use(i8 %m1)
525 %m2 = shl nuw i8 %x, %z
526 call void @use(i8 %m2)
527 %d = udiv i8 %m1, %m2
531 ; (X << Z) / (X * Y) -> (1 << Z) / Y
533 define i5 @udiv_shl_mul_nuw(i5 %x, i5 %y, i5 %z) {
534 ; CHECK-LABEL: @udiv_shl_mul_nuw(
535 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i5 1, [[Z:%.*]]
536 ; CHECK-NEXT: [[D:%.*]] = udiv i5 [[TMP1]], [[Y:%.*]]
537 ; CHECK-NEXT: ret i5 [[D]]
539 %m1 = shl nuw i5 %x, %z
540 %m2 = mul nuw i5 %x, %y
541 %d = udiv i5 %m1, %m2
545 define i5 @udiv_shl_mul_nuw_swap(i5 %x, i5 %y, i5 %z) {
546 ; CHECK-LABEL: @udiv_shl_mul_nuw_swap(
547 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i5 1, [[Z:%.*]]
548 ; CHECK-NEXT: [[D:%.*]] = udiv i5 [[TMP1]], [[Y:%.*]]
549 ; CHECK-NEXT: ret i5 [[D]]
551 %m1 = shl nuw i5 %x, %z
552 %m2 = mul nuw i5 %y, %x
553 %d = udiv i5 %m1, %m2
557 define i5 @udiv_shl_mul_nuw_exact(i5 %x, i5 %y, i5 %z) {
558 ; CHECK-LABEL: @udiv_shl_mul_nuw_exact(
559 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i5 1, [[Z:%.*]]
560 ; CHECK-NEXT: [[D:%.*]] = udiv exact i5 [[TMP1]], [[Y:%.*]]
561 ; CHECK-NEXT: ret i5 [[D]]
563 %m1 = shl nuw i5 %x, %z
564 %m2 = mul nuw i5 %x, %y
565 %d = udiv exact i5 %m1, %m2
569 define <2 x i4> @udiv_shl_mul_nuw_vec(<2 x i4> %x, <2 x i4> %y, <2 x i4> %z) {
570 ; CHECK-LABEL: @udiv_shl_mul_nuw_vec(
571 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw <2 x i4> <i4 1, i4 1>, [[Z:%.*]]
572 ; CHECK-NEXT: [[D:%.*]] = udiv <2 x i4> [[TMP1]], [[Y:%.*]]
573 ; CHECK-NEXT: ret <2 x i4> [[D]]
575 %m1 = shl nuw <2 x i4> %x, %z
576 %m2 = mul nuw <2 x i4> %y, %x
577 %d = udiv <2 x i4> %m1, %m2
581 define i8 @udiv_shl_mul_nuw_extra_use_of_shl(i8 %x, i8 %y, i8 %z) {
582 ; CHECK-LABEL: @udiv_shl_mul_nuw_extra_use_of_shl(
583 ; CHECK-NEXT: [[M1:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
584 ; CHECK-NEXT: call void @use(i8 [[M1]])
585 ; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i8 1, [[Z]]
586 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[TMP1]], [[Y:%.*]]
587 ; CHECK-NEXT: ret i8 [[D]]
589 %m1 = shl nuw i8 %x, %z
590 call void @use(i8 %m1)
591 %m2 = mul nuw i8 %y, %x
592 %d = udiv i8 %m1, %m2
596 ; negative test - extra use
598 define i8 @udiv_shl_mul_nuw_extra_use_of_mul(i8 %x, i8 %y, i8 %z) {
599 ; CHECK-LABEL: @udiv_shl_mul_nuw_extra_use_of_mul(
600 ; CHECK-NEXT: [[M1:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
601 ; CHECK-NEXT: [[M2:%.*]] = mul nuw i8 [[Y:%.*]], [[X]]
602 ; CHECK-NEXT: call void @use(i8 [[M2]])
603 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[M1]], [[M2]]
604 ; CHECK-NEXT: ret i8 [[D]]
606 %m1 = shl nuw i8 %x, %z
607 %m2 = mul nuw i8 %y, %x
608 call void @use(i8 %m2)
609 %d = udiv i8 %m1, %m2
613 define i8 @udiv_shl_mul_nuw_extra_use(i8 %x, i8 %y, i8 %z) {
614 ; CHECK-LABEL: @udiv_shl_mul_nuw_extra_use(
615 ; CHECK-NEXT: [[M1:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
616 ; CHECK-NEXT: call void @use(i8 [[M1]])
617 ; CHECK-NEXT: [[M2:%.*]] = mul nuw i8 [[Y:%.*]], [[X]]
618 ; CHECK-NEXT: call void @use(i8 [[M2]])
619 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[M1]], [[M2]]
620 ; CHECK-NEXT: ret i8 [[D]]
622 %m1 = shl nuw i8 %x, %z
623 call void @use(i8 %m1)
624 %m2 = mul nuw i8 %y, %x
625 call void @use(i8 %m2)
626 %d = udiv i8 %m1, %m2
630 ; negative test - sdiv
632 define i5 @sdiv_shl_mul_nuw(i5 %x, i5 %y, i5 %z) {
633 ; CHECK-LABEL: @sdiv_shl_mul_nuw(
634 ; CHECK-NEXT: [[M1:%.*]] = shl nuw i5 [[X:%.*]], [[Z:%.*]]
635 ; CHECK-NEXT: [[M2:%.*]] = mul nuw i5 [[X]], [[Y:%.*]]
636 ; CHECK-NEXT: [[D:%.*]] = sdiv i5 [[M1]], [[M2]]
637 ; CHECK-NEXT: ret i5 [[D]]
639 %m1 = shl nuw i5 %x, %z
640 %m2 = mul nuw i5 %x, %y
641 %d = sdiv i5 %m1, %m2
645 ; negative test - wrong no-wrap
647 define i5 @udiv_mul_shl_missing_nsw1(i5 %x, i5 %y, i5 %z) {
648 ; CHECK-LABEL: @udiv_mul_shl_missing_nsw1(
649 ; CHECK-NEXT: [[M1:%.*]] = mul nsw i5 [[X:%.*]], [[Y:%.*]]
650 ; CHECK-NEXT: [[M2:%.*]] = shl nuw i5 [[Y]], [[Z:%.*]]
651 ; CHECK-NEXT: [[D:%.*]] = udiv i5 [[M1]], [[M2]]
652 ; CHECK-NEXT: ret i5 [[D]]
654 %m1 = mul nsw i5 %x, %y
655 %m2 = shl nuw i5 %y, %z
656 %d = udiv i5 %m1, %m2
660 ; negative test - wrong no-wrap
662 define i5 @udiv_mul_shl_missing_nsw2(i5 %x, i5 %y, i5 %z) {
663 ; CHECK-LABEL: @udiv_mul_shl_missing_nsw2(
664 ; CHECK-NEXT: [[M1:%.*]] = mul nuw i5 [[X:%.*]], [[Y:%.*]]
665 ; CHECK-NEXT: [[M2:%.*]] = shl nsw i5 [[Y]], [[Z:%.*]]
666 ; CHECK-NEXT: [[D:%.*]] = udiv i5 [[M1]], [[M2]]
667 ; CHECK-NEXT: ret i5 [[D]]
669 %m1 = mul nuw i5 %x, %y
670 %m2 = shl nsw i5 %y, %z
671 %d = udiv i5 %m1, %m2
675 define i8 @udiv_shl_nuw(i8 %x, i8 %y, i8 %z) {
676 ; CHECK-LABEL: @udiv_shl_nuw(
677 ; CHECK-NEXT: [[S:%.*]] = shl nuw i8 [[Y:%.*]], [[Z:%.*]]
678 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[S]]
679 ; CHECK-NEXT: ret i8 [[D]]
681 %s = shl nuw i8 %y, %z
686 define <2 x i4> @udiv_shl_nuw_exact(<2 x i4> %x, <2 x i4> %y, <2 x i4> %z) {
687 ; CHECK-LABEL: @udiv_shl_nuw_exact(
688 ; CHECK-NEXT: [[S:%.*]] = shl nuw <2 x i4> [[Y:%.*]], [[Z:%.*]]
689 ; CHECK-NEXT: [[D:%.*]] = udiv exact <2 x i4> [[X:%.*]], [[S]]
690 ; CHECK-NEXT: ret <2 x i4> [[D]]
692 %s = shl nuw <2 x i4> %y, %z
693 %d = udiv exact <2 x i4> %x, %s
697 define i8 @udiv_shl(i8 %x, i8 %y, i8 %z) {
698 ; CHECK-LABEL: @udiv_shl(
699 ; CHECK-NEXT: [[S:%.*]] = shl i8 [[Y:%.*]], [[Z:%.*]]
700 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[S]]
701 ; CHECK-NEXT: ret i8 [[D]]
708 define i8 @udiv_shl_nuw_use(i8 %x, i8 %y, i8 %z) {
709 ; CHECK-LABEL: @udiv_shl_nuw_use(
710 ; CHECK-NEXT: [[S:%.*]] = shl nuw i8 [[Y:%.*]], [[Z:%.*]]
711 ; CHECK-NEXT: call void @use(i8 [[S]])
712 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[S]]
713 ; CHECK-NEXT: ret i8 [[D]]
715 %s = shl nuw i8 %y, %z
716 call void @use(i8 %s)
721 ; ((X * Y) >> Z) / X --> Y >> Z
723 define i8 @udiv_lshr_mul_nuw(i8 %x, i8 %y, i8 %z) {
724 ; CHECK-LABEL: @udiv_lshr_mul_nuw(
725 ; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[Y:%.*]], [[Z:%.*]]
726 ; CHECK-NEXT: ret i8 [[DIV]]
728 %m = mul nuw i8 %x, %y
730 %div = udiv i8 %s, %x
734 ; ((Y * X) >> Z) / X --> Y >> Z
736 define <2 x i4> @udiv_lshr_mul_nuw_exact_commute1(<2 x i4> %x, <2 x i4> %y, <2 x i4> %z) {
737 ; CHECK-LABEL: @udiv_lshr_mul_nuw_exact_commute1(
738 ; CHECK-NEXT: [[DIV:%.*]] = lshr exact <2 x i4> [[Y:%.*]], [[Z:%.*]]
739 ; CHECK-NEXT: ret <2 x i4> [[DIV]]
741 %m = mul nuw <2 x i4> %y, %x
742 %s = lshr exact <2 x i4> %m, %z
743 %div = udiv exact <2 x i4> %s, %x
747 ; negative test - mul is shifted amount, not shifted value
749 define i8 @udiv_lshr_mul_nuw_commute2(i8 %x, i8 %y, i8 %z) {
750 ; CHECK-LABEL: @udiv_lshr_mul_nuw_commute2(
751 ; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[Y:%.*]], [[X:%.*]]
752 ; CHECK-NEXT: [[S:%.*]] = lshr i8 [[Z:%.*]], [[M]]
753 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[S]], [[X]]
754 ; CHECK-NEXT: ret i8 [[DIV]]
756 %m = mul nuw i8 %y, %x
758 %div = udiv i8 %s, %x
764 define i8 @udiv_lshr_mul_nuw_use1(i8 %x, i8 %y, i8 %z) {
765 ; CHECK-LABEL: @udiv_lshr_mul_nuw_use1(
766 ; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
767 ; CHECK-NEXT: call void @use(i8 [[M]])
768 ; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[Y]], [[Z:%.*]]
769 ; CHECK-NEXT: ret i8 [[DIV]]
771 %m = mul nuw i8 %x, %y
772 call void @use(i8 %m)
774 %div = udiv i8 %s, %x
780 define i8 @udiv_lshr_mul_nuw_use2(i8 %x, i8 %y, i8 %z) {
781 ; CHECK-LABEL: @udiv_lshr_mul_nuw_use2(
782 ; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
783 ; CHECK-NEXT: [[S:%.*]] = lshr i8 [[M]], [[Z:%.*]]
784 ; CHECK-NEXT: call void @use(i8 [[S]])
785 ; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[Y]], [[Z]]
786 ; CHECK-NEXT: ret i8 [[DIV]]
788 %m = mul nuw i8 %x, %y
790 call void @use(i8 %s)
791 %div = udiv i8 %s, %x
797 define i8 @udiv_lshr_mul_nuw_use3(i8 %x, i8 %y, i8 %z) {
798 ; CHECK-LABEL: @udiv_lshr_mul_nuw_use3(
799 ; CHECK-NEXT: [[M:%.*]] = mul nuw i8 [[X:%.*]], [[Y:%.*]]
800 ; CHECK-NEXT: call void @use(i8 [[M]])
801 ; CHECK-NEXT: [[S:%.*]] = lshr i8 [[M]], [[Z:%.*]]
802 ; CHECK-NEXT: call void @use(i8 [[S]])
803 ; CHECK-NEXT: [[DIV:%.*]] = lshr i8 [[Y]], [[Z]]
804 ; CHECK-NEXT: ret i8 [[DIV]]
806 %m = mul nuw i8 %x, %y
807 call void @use(i8 %m)
809 call void @use(i8 %s)
810 %div = udiv i8 %s, %x
814 ; negative test - must have nuw
816 define i8 @udiv_lshr_mul_nsw(i8 %x, i8 %y, i8 %z) {
817 ; CHECK-LABEL: @udiv_lshr_mul_nsw(
818 ; CHECK-NEXT: [[M:%.*]] = mul nsw i8 [[X:%.*]], [[Y:%.*]]
819 ; CHECK-NEXT: [[S:%.*]] = lshr i8 [[M]], [[Z:%.*]]
820 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[S]], [[X]]
821 ; CHECK-NEXT: ret i8 [[DIV]]
823 %m = mul nsw i8 %x, %y
825 %div = udiv i8 %s, %x
829 ; negative test - doesn't fold with signed div
831 define i8 @sdiv_lshr_mul_nsw(i8 %x, i8 %y, i8 %z) {
832 ; CHECK-LABEL: @sdiv_lshr_mul_nsw(
833 ; CHECK-NEXT: [[M:%.*]] = mul nsw i8 [[X:%.*]], [[Y:%.*]]
834 ; CHECK-NEXT: [[S:%.*]] = lshr i8 [[M]], [[Z:%.*]]
835 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i8 [[S]], [[X]]
836 ; CHECK-NEXT: ret i8 [[DIV]]
838 %m = mul nsw i8 %x, %y
840 %div = sdiv i8 %s, %x
844 ; (X << Z) / (Y << Z) --> X / Y
846 define i8 @sdiv_shl_shl_nsw2_nuw(i8 %x, i8 %y, i8 %z) {
847 ; CHECK-LABEL: @sdiv_shl_shl_nsw2_nuw(
848 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[X:%.*]], [[Y:%.*]]
849 ; CHECK-NEXT: ret i8 [[D]]
851 %xz = shl nsw i8 %x, %z
852 %yz = shl nsw nuw i8 %y, %z
853 %d = sdiv i8 %xz, %yz
857 ; extra uses are ok and 'exact' propagates
859 define i8 @sdiv_shl_shl_nsw2_nuw_exact_use(i8 %x, i8 %y, i8 %z) {
860 ; CHECK-LABEL: @sdiv_shl_shl_nsw2_nuw_exact_use(
861 ; CHECK-NEXT: [[XZ:%.*]] = shl nsw i8 [[X:%.*]], [[Z:%.*]]
862 ; CHECK-NEXT: call void @use(i8 [[XZ]])
863 ; CHECK-NEXT: [[D:%.*]] = sdiv exact i8 [[X]], [[Y:%.*]]
864 ; CHECK-NEXT: ret i8 [[D]]
866 %xz = shl nsw i8 %x, %z
867 call void @use(i8 %xz)
868 %yz = shl nsw nuw i8 %y, %z
869 %d = sdiv exact i8 %xz, %yz
873 ; negative test - wrong wrap
875 define i8 @sdiv_shl_shl_nsw_nuw2(i8 %x, i8 %y, i8 %z) {
876 ; CHECK-LABEL: @sdiv_shl_shl_nsw_nuw2(
877 ; CHECK-NEXT: [[XZ:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
878 ; CHECK-NEXT: [[YZ:%.*]] = shl nuw nsw i8 [[Y:%.*]], [[Z]]
879 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[XZ]], [[YZ]]
880 ; CHECK-NEXT: ret i8 [[D]]
882 %xz = shl nuw i8 %x, %z
883 %yz = shl nsw nuw i8 %y, %z
884 %d = sdiv i8 %xz, %yz
888 ; negative test - wrong wrap
890 define i8 @sdiv_shl_shl_nsw_nuw(i8 %x, i8 %y, i8 %z) {
891 ; CHECK-LABEL: @sdiv_shl_shl_nsw_nuw(
892 ; CHECK-NEXT: [[XZ:%.*]] = shl nsw i8 [[X:%.*]], [[Z:%.*]]
893 ; CHECK-NEXT: [[YZ:%.*]] = shl nuw i8 [[Y:%.*]], [[Z]]
894 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[XZ]], [[YZ]]
895 ; CHECK-NEXT: ret i8 [[D]]
897 %xz = shl nsw i8 %x, %z
898 %yz = shl nuw i8 %y, %z
899 %d = sdiv i8 %xz, %yz
903 ; negative test - wrong wrap
905 define i8 @sdiv_shl_shl_nuw_nsw2(i8 %x, i8 %y, i8 %z) {
906 ; CHECK-LABEL: @sdiv_shl_shl_nuw_nsw2(
907 ; CHECK-NEXT: [[XZ:%.*]] = shl nuw nsw i8 [[X:%.*]], [[Z:%.*]]
908 ; CHECK-NEXT: [[YZ:%.*]] = shl nsw i8 [[Y:%.*]], [[Z]]
909 ; CHECK-NEXT: [[D:%.*]] = sdiv i8 [[XZ]], [[YZ]]
910 ; CHECK-NEXT: ret i8 [[D]]
912 %xz = shl nuw nsw i8 %x, %z
913 %yz = shl nsw i8 %y, %z
914 %d = sdiv i8 %xz, %yz
918 ; (X << Z) / (Y << Z) --> X / Y
920 define <2 x i8> @udiv_shl_shl_nuw2(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
921 ; CHECK-LABEL: @udiv_shl_shl_nuw2(
922 ; CHECK-NEXT: [[D:%.*]] = udiv <2 x i8> [[X:%.*]], [[Y:%.*]]
923 ; CHECK-NEXT: ret <2 x i8> [[D]]
925 %xz = shl nuw <2 x i8> %x, %z
926 %yz = shl nuw <2 x i8> %y, %z
927 %d = udiv <2 x i8> %xz, %yz
931 ; extra uses are ok and 'exact' propagates
933 define i8 @udiv_shl_shl_nuw2_exact_use2(i8 %x, i8 %y, i8 %z) {
934 ; CHECK-LABEL: @udiv_shl_shl_nuw2_exact_use2(
935 ; CHECK-NEXT: [[XZ:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
936 ; CHECK-NEXT: call void @use(i8 [[XZ]])
937 ; CHECK-NEXT: [[YZ:%.*]] = shl nuw i8 [[Y:%.*]], [[Z]]
938 ; CHECK-NEXT: call void @use(i8 [[YZ]])
939 ; CHECK-NEXT: [[D:%.*]] = udiv exact i8 [[X]], [[Y]]
940 ; CHECK-NEXT: ret i8 [[D]]
942 %xz = shl nuw i8 %x, %z
943 call void @use(i8 %xz)
944 %yz = shl nuw i8 %y, %z
945 call void @use(i8 %yz)
946 %d = udiv exact i8 %xz, %yz
950 ; negative test - wrong wrap
952 define i8 @udiv_shl_shl_nuw_nsw(i8 %x, i8 %y, i8 %z) {
953 ; CHECK-LABEL: @udiv_shl_shl_nuw_nsw(
954 ; CHECK-NEXT: [[XZ:%.*]] = shl nuw i8 [[X:%.*]], [[Z:%.*]]
955 ; CHECK-NEXT: [[YZ:%.*]] = shl nsw i8 [[Y:%.*]], [[Z]]
956 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[XZ]], [[YZ]]
957 ; CHECK-NEXT: ret i8 [[D]]
959 %xz = shl nuw i8 %x, %z
960 %yz = shl nsw i8 %y, %z
961 %d = udiv i8 %xz, %yz
965 ; negative test - wrong wrap
967 define i8 @udiv_shl_shl_nsw_nuw(i8 %x, i8 %y, i8 %z) {
968 ; CHECK-LABEL: @udiv_shl_shl_nsw_nuw(
969 ; CHECK-NEXT: [[XZ:%.*]] = shl nsw i8 [[X:%.*]], [[Z:%.*]]
970 ; CHECK-NEXT: [[YZ:%.*]] = shl nuw i8 [[Y:%.*]], [[Z]]
971 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[XZ]], [[YZ]]
972 ; CHECK-NEXT: ret i8 [[D]]
974 %xz = shl nsw i8 %x, %z
975 %yz = shl nuw i8 %y, %z
976 %d = udiv i8 %xz, %yz
980 define i8 @udiv_shl_shl_nuw_nsw2(i8 %x, i8 %y, i8 %z) {
981 ; CHECK-LABEL: @udiv_shl_shl_nuw_nsw2(
982 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[Y:%.*]]
983 ; CHECK-NEXT: ret i8 [[D]]
985 %xz = shl nuw nsw i8 %x, %z
986 %yz = shl nsw i8 %y, %z
987 %d = udiv i8 %xz, %yz
991 ; TODO: X / (Y << Z) --> (X >> Z) / Y
992 ; https://alive2.llvm.org/ce/z/FjoN_A
994 define i8 @udiv_shl_nuw_divisor(i8 %x, i8 %y, i8 %z) {
995 ; CHECK-LABEL: @udiv_shl_nuw_divisor(
996 ; CHECK-NEXT: [[S:%.*]] = shl nuw i8 [[Y:%.*]], [[Z:%.*]]
997 ; CHECK-NEXT: [[D:%.*]] = udiv i8 [[X:%.*]], [[S]]
998 ; CHECK-NEXT: ret i8 [[D]]
1000 %s = shl nuw i8 %y, %z
1005 define i8 @udiv_fail_shl_overflow(i8 %x, i8 %y) {
1006 ; CHECK-LABEL: @udiv_fail_shl_overflow(
1007 ; CHECK-NEXT: [[SHL:%.*]] = shl i8 2, [[Y:%.*]]
1008 ; CHECK-NEXT: [[MIN:%.*]] = call i8 @llvm.umax.i8(i8 [[SHL]], i8 1)
1009 ; CHECK-NEXT: [[MUL:%.*]] = udiv i8 [[X:%.*]], [[MIN]]
1010 ; CHECK-NEXT: ret i8 [[MUL]]
1013 %min = call i8 @llvm.umax.i8(i8 %shl, i8 1)
1014 %mul = udiv i8 %x, %min
1018 define i8 @udiv_shl_no_overflow(i8 %x, i8 %y) {
1019 ; CHECK-LABEL: @udiv_shl_no_overflow(
1020 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], 1
1021 ; CHECK-NEXT: [[MUL1:%.*]] = lshr i8 [[X:%.*]], [[TMP1]]
1022 ; CHECK-NEXT: ret i8 [[MUL1]]
1024 %shl = shl nuw i8 2, %y
1025 %min = call i8 @llvm.umax.i8(i8 %shl, i8 1)
1026 %mul = udiv i8 %x, %min
1030 ; (X<<Y) / (X<<Z) -> 1 << Y >> Z
1032 define i32 @sdiv_shl_pair_const(i32 %a) {
1033 ; CHECK-LABEL: @sdiv_shl_pair_const(
1034 ; CHECK-NEXT: entry:
1035 ; CHECK-NEXT: ret i32 2
1038 %lhs = shl nsw i32 %a, 2
1039 %rhs = shl nsw i32 %a, 1
1040 %div = sdiv i32 %lhs, %rhs
1044 define i32 @udiv_shl_pair_const(i32 %a) {
1045 ; CHECK-LABEL: @udiv_shl_pair_const(
1046 ; CHECK-NEXT: entry:
1047 ; CHECK-NEXT: ret i32 2
1050 %lhs = shl nuw i32 %a, 2
1051 %rhs = shl nuw i32 %a, 1
1052 %div = udiv i32 %lhs, %rhs
1056 define i32 @sdiv_shl_pair1(i32 %a, i32 %x, i32 %y) {
1057 ; CHECK-LABEL: @sdiv_shl_pair1(
1058 ; CHECK-NEXT: entry:
1059 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw nsw i32 1, [[X:%.*]]
1060 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1061 ; CHECK-NEXT: ret i32 [[DIV]]
1064 %lhs = shl nsw i32 %a, %x
1065 %rhs = shl nuw nsw i32 %a, %y
1066 %div = sdiv i32 %lhs, %rhs
1070 define i32 @sdiv_shl_pair2(i32 %a, i32 %x, i32 %y) {
1071 ; CHECK-LABEL: @sdiv_shl_pair2(
1072 ; CHECK-NEXT: entry:
1073 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw nsw i32 1, [[X:%.*]]
1074 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1075 ; CHECK-NEXT: ret i32 [[DIV]]
1078 %lhs = shl nuw nsw i32 %a, %x
1079 %rhs = shl nsw i32 %a, %y
1080 %div = sdiv i32 %lhs, %rhs
1084 define i32 @sdiv_shl_pair3(i32 %a, i32 %x, i32 %y) {
1085 ; CHECK-LABEL: @sdiv_shl_pair3(
1086 ; CHECK-NEXT: entry:
1087 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw i32 1, [[X:%.*]]
1088 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1089 ; CHECK-NEXT: ret i32 [[DIV]]
1092 %lhs = shl nsw i32 %a, %x
1093 %rhs = shl nsw i32 %a, %y
1094 %div = sdiv i32 %lhs, %rhs
1098 define i32 @sdiv_shl_no_pair_fail(i32 %a, i32 %b, i32 %x, i32 %y) {
1099 ; CHECK-LABEL: @sdiv_shl_no_pair_fail(
1100 ; CHECK-NEXT: entry:
1101 ; CHECK-NEXT: [[LHS:%.*]] = shl nuw nsw i32 [[A:%.*]], [[X:%.*]]
1102 ; CHECK-NEXT: [[RHS:%.*]] = shl nuw i32 [[B:%.*]], [[Y:%.*]]
1103 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[LHS]], [[RHS]]
1104 ; CHECK-NEXT: ret i32 [[DIV]]
1107 %lhs = shl nuw nsw i32 %a, %x
1108 %rhs = shl nuw i32 %b, %y
1109 %div = sdiv i32 %lhs, %rhs
1113 define i32 @udiv_shl_pair1(i32 %a, i32 %x, i32 %y) {
1114 ; CHECK-LABEL: @udiv_shl_pair1(
1115 ; CHECK-NEXT: entry:
1116 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw i32 1, [[X:%.*]]
1117 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1118 ; CHECK-NEXT: ret i32 [[DIV]]
1121 %lhs = shl nuw i32 %a, %x
1122 %rhs = shl nuw i32 %a, %y
1123 %div = udiv i32 %lhs, %rhs
1127 define i32 @udiv_shl_pair2(i32 %a, i32 %x, i32 %y) {
1128 ; CHECK-LABEL: @udiv_shl_pair2(
1129 ; CHECK-NEXT: entry:
1130 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw nsw i32 1, [[X:%.*]]
1131 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1132 ; CHECK-NEXT: ret i32 [[DIV]]
1135 %lhs = shl nuw nsw i32 %a, %x
1136 %rhs = shl nuw i32 %a, %y
1137 %div = udiv i32 %lhs, %rhs
1141 define i32 @udiv_shl_pair3(i32 %a, i32 %x, i32 %y) {
1142 ; CHECK-LABEL: @udiv_shl_pair3(
1143 ; CHECK-NEXT: entry:
1144 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw i32 1, [[X:%.*]]
1145 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1146 ; CHECK-NEXT: ret i32 [[DIV]]
1149 %lhs = shl nuw i32 %a, %x
1150 %rhs = shl nuw nsw i32 %a, %y
1151 %div = udiv i32 %lhs, %rhs
1155 define i32 @sdiv_shl_pair_overflow_fail1(i32 %a, i32 %x, i32 %y) {
1156 ; CHECK-LABEL: @sdiv_shl_pair_overflow_fail1(
1157 ; CHECK-NEXT: entry:
1158 ; CHECK-NEXT: [[LHS:%.*]] = shl i32 [[A:%.*]], [[X:%.*]]
1159 ; CHECK-NEXT: [[RHS:%.*]] = shl nsw i32 [[A]], [[Y:%.*]]
1160 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[LHS]], [[RHS]]
1161 ; CHECK-NEXT: ret i32 [[DIV]]
1164 %lhs = shl i32 %a, %x
1165 %rhs = shl nsw i32 %a, %y
1166 %div = sdiv i32 %lhs, %rhs
1170 define i32 @sdiv_shl_pair_overflow_fail2(i32 %a, i32 %x, i32 %y) {
1171 ; CHECK-LABEL: @sdiv_shl_pair_overflow_fail2(
1172 ; CHECK-NEXT: entry:
1173 ; CHECK-NEXT: [[LHS:%.*]] = shl nsw i32 [[A:%.*]], [[X:%.*]]
1174 ; CHECK-NEXT: [[RHS:%.*]] = shl nuw i32 [[A]], [[Y:%.*]]
1175 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[LHS]], [[RHS]]
1176 ; CHECK-NEXT: ret i32 [[DIV]]
1179 %lhs = shl nsw i32 %a, %x
1180 %rhs = shl nuw i32 %a, %y
1181 %div = sdiv i32 %lhs, %rhs
1185 define i32 @udiv_shl_pair_overflow_fail1(i32 %a, i32 %x, i32 %y) {
1186 ; CHECK-LABEL: @udiv_shl_pair_overflow_fail1(
1187 ; CHECK-NEXT: entry:
1188 ; CHECK-NEXT: [[LHS:%.*]] = shl nsw i32 [[A:%.*]], [[X:%.*]]
1189 ; CHECK-NEXT: [[RHS:%.*]] = shl nuw i32 [[A]], [[Y:%.*]]
1190 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[LHS]], [[RHS]]
1191 ; CHECK-NEXT: ret i32 [[DIV]]
1194 %lhs = shl nsw i32 %a, %x
1195 %rhs = shl nuw i32 %a, %y
1196 %div = udiv i32 %lhs, %rhs
1200 define i32 @udiv_shl_pair_overflow_fail2(i32 %a, i32 %x, i32 %y) {
1201 ; CHECK-LABEL: @udiv_shl_pair_overflow_fail2(
1202 ; CHECK-NEXT: entry:
1203 ; CHECK-NEXT: [[LHS:%.*]] = shl nsw i32 [[A:%.*]], [[X:%.*]]
1204 ; CHECK-NEXT: [[RHS:%.*]] = shl i32 [[A]], [[Y:%.*]]
1205 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[LHS]], [[RHS]]
1206 ; CHECK-NEXT: ret i32 [[DIV]]
1209 %lhs = shl nsw i32 %a, %x
1210 %rhs = shl i32 %a, %y
1211 %div = udiv i32 %lhs, %rhs
1215 define i32 @udiv_shl_pair_overflow_fail3(i32 %a, i32 %x, i32 %y) {
1216 ; CHECK-LABEL: @udiv_shl_pair_overflow_fail3(
1217 ; CHECK-NEXT: entry:
1218 ; CHECK-NEXT: [[LHS:%.*]] = shl nuw nsw i32 [[A:%.*]], [[X:%.*]]
1219 ; CHECK-NEXT: [[RHS:%.*]] = shl i32 [[A]], [[Y:%.*]]
1220 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[LHS]], [[RHS]]
1221 ; CHECK-NEXT: ret i32 [[DIV]]
1224 %lhs = shl nuw nsw i32 %a, %x
1225 %rhs = shl i32 %a, %y
1226 %div = udiv i32 %lhs, %rhs
1230 define i32 @sdiv_shl_pair_multiuse1(i32 %a, i32 %x, i32 %y) {
1231 ; CHECK-LABEL: @sdiv_shl_pair_multiuse1(
1232 ; CHECK-NEXT: entry:
1233 ; CHECK-NEXT: [[LHS:%.*]] = shl nuw nsw i32 [[A:%.*]], [[X:%.*]]
1234 ; CHECK-NEXT: call void @use32(i32 [[LHS]])
1235 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw nsw i32 1, [[X]]
1236 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y:%.*]]
1237 ; CHECK-NEXT: ret i32 [[DIV]]
1240 %lhs = shl nuw nsw i32 %a, %x
1241 call void @use32(i32 %lhs)
1242 %rhs = shl nsw i32 %a, %y
1243 %div = sdiv i32 %lhs, %rhs
1247 define i32 @sdiv_shl_pair_multiuse2(i32 %a, i32 %x, i32 %y) {
1248 ; CHECK-LABEL: @sdiv_shl_pair_multiuse2(
1249 ; CHECK-NEXT: entry:
1250 ; CHECK-NEXT: [[RHS:%.*]] = shl nsw i32 [[A:%.*]], [[Y:%.*]]
1251 ; CHECK-NEXT: call void @use32(i32 [[RHS]])
1252 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw nsw i32 1, [[X:%.*]]
1253 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y]]
1254 ; CHECK-NEXT: ret i32 [[DIV]]
1257 %lhs = shl nuw nsw i32 %a, %x
1258 %rhs = shl nsw i32 %a, %y
1259 call void @use32(i32 %rhs)
1260 %div = sdiv i32 %lhs, %rhs
1264 define i32 @sdiv_shl_pair_multiuse3(i32 %a, i32 %x, i32 %y) {
1265 ; CHECK-LABEL: @sdiv_shl_pair_multiuse3(
1266 ; CHECK-NEXT: entry:
1267 ; CHECK-NEXT: [[LHS:%.*]] = shl nuw nsw i32 [[A:%.*]], [[X:%.*]]
1268 ; CHECK-NEXT: [[RHS:%.*]] = shl nsw i32 [[A]], [[Y:%.*]]
1269 ; CHECK-NEXT: call void @use32(i32 [[LHS]])
1270 ; CHECK-NEXT: call void @use32(i32 [[RHS]])
1271 ; CHECK-NEXT: [[SHL_DIVIDEND:%.*]] = shl nuw nsw i32 1, [[X]]
1272 ; CHECK-NEXT: [[DIV:%.*]] = lshr i32 [[SHL_DIVIDEND]], [[Y]]
1273 ; CHECK-NEXT: ret i32 [[DIV]]
1276 %lhs = shl nuw nsw i32 %a, %x
1277 %rhs = shl nsw i32 %a, %y
1278 call void @use32(i32 %lhs)
1279 call void @use32(i32 %rhs)
1280 %div = sdiv i32 %lhs, %rhs
1284 @a = external global i32
1285 define i32 @pr69291() {
1286 ; CHECK-LABEL: @pr69291(
1287 ; CHECK-NEXT: entry:
1288 ; CHECK-NEXT: ret i32 1
1291 %conv = load i32, ptr @a, align 1
1292 %add = shl nuw nsw i32 %conv, 1
1293 %add2 = shl nuw nsw i32 %conv, 1
1294 %div = sdiv i32 %add, %add2