1 ; This test makes sure that these instructions are properly eliminated.
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
5 define i32 @test1(i32 %A) {
8 %B = shl i32 %A, 0 ; <i32> [#uses=1]
12 define i32 @test2(i8 %A) {
15 %shift.upgrd.1 = zext i8 %A to i32 ; <i32> [#uses=1]
16 %B = shl i32 0, %shift.upgrd.1 ; <i32> [#uses=1]
20 define i32 @test3(i32 %A) {
23 %B = ashr i32 %A, 0 ; <i32> [#uses=1]
27 define i32 @test4(i8 %A) {
30 %shift.upgrd.2 = zext i8 %A to i32 ; <i32> [#uses=1]
31 %B = ashr i32 0, %shift.upgrd.2 ; <i32> [#uses=1]
36 define i32 @test5(i32 %A) {
38 ; CHECK: ret i32 undef
39 %B = lshr i32 %A, 32 ;; shift all bits out
43 define i32 @test5a(i32 %A) {
45 ; CHECK: ret i32 undef
46 %B = shl i32 %A, 32 ;; shift all bits out
50 define i32 @test5b() {
53 %B = ashr i32 undef, 2 ;; top two bits must be equal, so not undef
57 define i32 @test5b2(i32 %A) {
60 %B = ashr i32 undef, %A ;; top %A bits must be equal, so not undef
64 define i32 @test6(i32 %A) {
66 ; CHECK-NEXT: mul i32 %A, 6
68 %B = shl i32 %A, 1 ;; convert to an mul instruction
73 define i32 @test6a(i32 %A) {
75 ; CHECK-NEXT: mul i32 %A, 6
78 %C = shl i32 %B, 1 ;; convert to an mul instruction
82 define i32 @test7(i8 %A) {
84 ; CHECK-NEXT: ret i32 -1
85 %shift.upgrd.3 = zext i8 %A to i32
86 %B = ashr i32 -1, %shift.upgrd.3 ;; Always equal to -1
90 ;; (A << 5) << 3 === A << 8 == 0
91 define i8 @test8(i8 %A) {
94 %B = shl i8 %A, 5 ; <i8> [#uses=1]
95 %C = shl i8 %B, 3 ; <i8> [#uses=1]
99 ;; (A << 7) >> 7 === A & 1
100 define i8 @test9(i8 %A) {
102 ; CHECK-NEXT: and i8 %A, 1
104 %B = shl i8 %A, 7 ; <i8> [#uses=1]
105 %C = lshr i8 %B, 7 ; <i8> [#uses=1]
109 ;; This transformation is deferred to DAGCombine:
110 ;; (A >> 7) << 7 === A & 128
111 ;; The shl may be valuable to scalar evolution.
112 define i8 @test10(i8 %A) {
114 ; CHECK-NEXT: and i8 %A, -128
116 %B = lshr i8 %A, 7 ; <i8> [#uses=1]
117 %C = shl i8 %B, 7 ; <i8> [#uses=1]
121 ;; Allow the simplification when the lshr shift is exact.
122 define i8 @test10a(i8 %A) {
124 ; CHECK-NEXT: ret i8 %A
125 %B = lshr exact i8 %A, 7
130 ;; This transformation is deferred to DAGCombine:
131 ;; (A >> 3) << 4 === (A & 0x1F) << 1
132 ;; The shl may be valuable to scalar evolution.
133 define i8 @test11(i8 %A) {
137 %a = mul i8 %A, 3 ; <i8> [#uses=1]
138 %B = lshr i8 %a, 3 ; <i8> [#uses=1]
139 %C = shl i8 %B, 4 ; <i8> [#uses=1]
143 ;; Allow the simplification in InstCombine when the lshr shift is exact.
144 define i8 @test11a(i8 %A) {
146 ; CHECK-NEXT: mul i8 %A, 6
149 %B = lshr exact i8 %a, 3
154 ;; This is deferred to DAGCombine unless %B is single-use.
155 ;; (A >> 8) << 8 === A & -256
156 define i32 @test12(i32 %A) {
158 ; CHECK-NEXT: and i32 %A, -256
159 ; CHECK-NEXT: ret i32
160 %B = ashr i32 %A, 8 ; <i32> [#uses=1]
161 %C = shl i32 %B, 8 ; <i32> [#uses=1]
165 ;; This transformation is deferred to DAGCombine:
166 ;; (A >> 3) << 4 === (A & -8) * 2
167 ;; The shl may be valuable to scalar evolution.
168 define i8 @test13(i8 %A) {
172 %a = mul i8 %A, 3 ; <i8> [#uses=1]
173 %B = ashr i8 %a, 3 ; <i8> [#uses=1]
174 %C = shl i8 %B, 4 ; <i8> [#uses=1]
178 define i8 @test13a(i8 %A) {
180 ; CHECK-NEXT: mul i8 %A, 6
183 %B = ashr exact i8 %a, 3
188 ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
189 define i32 @test14(i32 %A) {
191 ; CHECK-NEXT: %B = and i32 %A, -19760
192 ; CHECK-NEXT: or i32 %B, 19744
193 ; CHECK-NEXT: ret i32
194 %B = lshr i32 %A, 4 ; <i32> [#uses=1]
195 %C = or i32 %B, 1234 ; <i32> [#uses=1]
196 %D = shl i32 %C, 4 ; <i32> [#uses=1]
200 ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
201 define i32 @test14a(i32 %A) {
203 ; CHECK-NEXT: and i32 %A, 77
204 ; CHECK-NEXT: ret i32
205 %B = shl i32 %A, 4 ; <i32> [#uses=1]
206 %C = and i32 %B, 1234 ; <i32> [#uses=1]
207 %D = lshr i32 %C, 4 ; <i32> [#uses=1]
211 define i32 @test15(i1 %C) {
213 ; CHECK-NEXT: select i1 %C, i32 12, i32 4
214 ; CHECK-NEXT: ret i32
215 %A = select i1 %C, i32 3, i32 1 ; <i32> [#uses=1]
216 %V = shl i32 %A, 2 ; <i32> [#uses=1]
220 define i32 @test15a(i1 %C) {
222 ; CHECK-NEXT: select i1 %C, i32 512, i32 128
223 ; CHECK-NEXT: ret i32
224 %A = select i1 %C, i8 3, i8 1 ; <i8> [#uses=1]
225 %shift.upgrd.4 = zext i8 %A to i32 ; <i32> [#uses=1]
226 %V = shl i32 64, %shift.upgrd.4 ; <i32> [#uses=1]
230 define i1 @test16(i32 %X) {
232 ; CHECK-NEXT: and i32 %X, 16
233 ; CHECK-NEXT: icmp ne i32
235 %tmp.3 = ashr i32 %X, 4
236 %tmp.6 = and i32 %tmp.3, 1
237 %tmp.7 = icmp ne i32 %tmp.6, 0
241 define i1 @test17(i32 %A) {
243 ; CHECK-NEXT: and i32 %A, -8
244 ; CHECK-NEXT: icmp eq i32
246 %B = lshr i32 %A, 3 ; <i32> [#uses=1]
247 %C = icmp eq i32 %B, 1234 ; <i1> [#uses=1]
252 define i1 @test18(i8 %A) {
254 ; CHECK: ret i1 false
256 %B = lshr i8 %A, 7 ; <i8> [#uses=1]
258 %C = icmp eq i8 %B, 123 ; <i1> [#uses=1]
262 define i1 @test19(i32 %A) {
264 ; CHECK-NEXT: icmp ult i32 %A, 4
266 %B = ashr i32 %A, 2 ; <i32> [#uses=1]
268 %C = icmp eq i32 %B, 0 ; <i1> [#uses=1]
273 define i1 @test19a(i32 %A) {
275 ; CHECK-NEXT: and i32 %A, -4
276 ; CHECK-NEXT: icmp eq i32
278 %B = ashr i32 %A, 2 ; <i32> [#uses=1]
280 %C = icmp eq i32 %B, -1 ; <i1> [#uses=1]
284 define i1 @test20(i8 %A) {
286 ; CHECK: ret i1 false
287 %B = ashr i8 %A, 7 ; <i8> [#uses=1]
289 %C = icmp eq i8 %B, 123 ; <i1> [#uses=1]
293 define i1 @test21(i8 %A) {
295 ; CHECK-NEXT: and i8 %A, 15
296 ; CHECK-NEXT: icmp eq i8
298 %B = shl i8 %A, 4 ; <i8> [#uses=1]
299 %C = icmp eq i8 %B, -128 ; <i1> [#uses=1]
303 define i1 @test22(i8 %A) {
305 ; CHECK-NEXT: and i8 %A, 15
306 ; CHECK-NEXT: icmp eq i8
308 %B = shl i8 %A, 4 ; <i8> [#uses=1]
309 %C = icmp eq i8 %B, 0 ; <i1> [#uses=1]
313 define i8 @test23(i32 %A) {
315 ; CHECK-NEXT: trunc i32 %A to i8
319 %B = shl i32 %A, 24 ; <i32> [#uses=1]
320 %C = ashr i32 %B, 24 ; <i32> [#uses=1]
321 %D = trunc i32 %C to i8 ; <i8> [#uses=1]
325 define i8 @test24(i8 %X) {
327 ; CHECK-NEXT: and i8 %X, 3
329 %Y = and i8 %X, -5 ; <i8> [#uses=1]
330 %Z = shl i8 %Y, 5 ; <i8> [#uses=1]
331 %Q = ashr i8 %Z, 5 ; <i8> [#uses=1]
335 define i32 @test25(i32 %tmp.2, i32 %AA) {
337 ; CHECK-NEXT: and i32 %tmp.2, -131072
338 ; CHECK-NEXT: add i32 %{{[^,]*}}, %AA
339 ; CHECK-NEXT: and i32 %{{[^,]*}}, -131072
340 ; CHECK-NEXT: ret i32
341 %x = lshr i32 %AA, 17 ; <i32> [#uses=1]
342 %tmp.3 = lshr i32 %tmp.2, 17 ; <i32> [#uses=1]
343 %tmp.5 = add i32 %tmp.3, %x ; <i32> [#uses=1]
344 %tmp.6 = shl i32 %tmp.5, 17 ; <i32> [#uses=1]
348 ;; handle casts between shifts.
349 define i32 @test26(i32 %A) {
351 ; CHECK-NEXT: and i32 %A, -2
352 ; CHECK-NEXT: ret i32
353 %B = lshr i32 %A, 1 ; <i32> [#uses=1]
354 %C = bitcast i32 %B to i32 ; <i32> [#uses=1]
355 %D = shl i32 %C, 1 ; <i32> [#uses=1]
360 define i1 @test27(i32 %x) nounwind {
362 ; CHECK-NEXT: and i32 %x, 8
363 ; CHECK-NEXT: icmp ne i32
366 %z = trunc i32 %y to i1
370 define i8 @test28(i8 %x) {
373 ; CHECK: icmp slt i8 %x, 0
375 %tmp1 = lshr i8 %x, 7
376 %cond1 = icmp ne i8 %tmp1, 0
377 br i1 %cond1, label %bb1, label %bb2
386 define i8 @test28a(i8 %x, i8 %y) {
388 ; This shouldn't be transformed.
390 ; CHECK: %tmp1 = lshr i8 %x, 7
391 ; CHECK: %cond1 = icmp eq i8 %tmp1, 0
392 ; CHECK: br i1 %cond1, label %bb2, label %bb1
393 %tmp1 = lshr i8 %x, 7
394 %cond1 = icmp ne i8 %tmp1, 0
395 br i1 %cond1, label %bb1, label %bb2
399 %tmp2 = add i8 %tmp1, %y
404 define i32 @test29(i64 %d18) {
406 %tmp916 = lshr i64 %d18, 32
407 %tmp917 = trunc i64 %tmp916 to i32
408 %tmp10 = lshr i32 %tmp917, 31
411 ; CHECK: %tmp916 = lshr i64 %d18, 63
412 ; CHECK: %tmp10 = trunc i64 %tmp916 to i32
416 define i32 @test30(i32 %A, i32 %B, i32 %C) {
422 ; CHECK: %X1 = and i32 %A, %B
423 ; CHECK: %Z = shl i32 %X1, %C
426 define i32 @test31(i32 %A, i32 %B, i32 %C) {
432 ; CHECK: %X1 = or i32 %A, %B
433 ; CHECK: %Z = lshr i32 %X1, %C
436 define i32 @test32(i32 %A, i32 %B, i32 %C) {
442 ; CHECK: %X1 = xor i32 %A, %B
443 ; CHECK: %Z = ashr i32 %X1, %C
447 define i1 @test33(i32 %X) {
448 %tmp1 = shl i32 %X, 7
449 %tmp2 = icmp slt i32 %tmp1, 0
452 ; CHECK: %tmp1.mask = and i32 %X, 16777216
453 ; CHECK: %tmp2 = icmp ne i32 %tmp1.mask, 0
456 define i1 @test34(i32 %X) {
457 %tmp1 = lshr i32 %X, 7
458 %tmp2 = icmp slt i32 %tmp1, 0
461 ; CHECK: ret i1 false
464 define i1 @test35(i32 %X) {
465 %tmp1 = ashr i32 %X, 7
466 %tmp2 = icmp slt i32 %tmp1, 0
469 ; CHECK: %tmp2 = icmp slt i32 %X, 0
470 ; CHECK: ret i1 %tmp2
473 define i128 @test36(i128 %A, i128 %B) {
475 %tmp27 = shl i128 %A, 64
476 %tmp23 = shl i128 %B, 64
477 %ins = or i128 %tmp23, %tmp27
478 %tmp45 = lshr i128 %ins, 64
482 ; CHECK: %tmp231 = or i128 %B, %A
483 ; CHECK: %ins = and i128 %tmp231, 18446744073709551615
484 ; CHECK: ret i128 %ins
487 define i64 @test37(i128 %A, i32 %B) {
489 %tmp27 = shl i128 %A, 64
490 %tmp22 = zext i32 %B to i128
491 %tmp23 = shl i128 %tmp22, 96
492 %ins = or i128 %tmp23, %tmp27
493 %tmp45 = lshr i128 %ins, 64
494 %tmp46 = trunc i128 %tmp45 to i64
498 ; CHECK: %tmp23 = shl nuw nsw i128 %tmp22, 32
499 ; CHECK: %ins = or i128 %tmp23, %A
500 ; CHECK: %tmp46 = trunc i128 %ins to i64
503 define i32 @test38(i32 %x) nounwind readnone {
504 %rem = srem i32 %x, 32
505 %shl = shl i32 1, %rem
508 ; CHECK-NEXT: and i32 %x, 31
509 ; CHECK-NEXT: shl i32 1
510 ; CHECK-NEXT: ret i32
513 ; <rdar://problem/8756731>
515 define i8 @test39(i32 %a0) {
517 %tmp4 = trunc i32 %a0 to i8
518 ; CHECK: and i8 %tmp49, 64
519 %tmp5 = shl i8 %tmp4, 5
520 %tmp48 = and i8 %tmp5, 32
521 %tmp49 = lshr i8 %tmp48, 5
522 %tmp50 = mul i8 %tmp49, 64
523 %tmp51 = xor i8 %tmp50, %tmp5
524 %tmp52 = and i8 %tmp51, -128
525 %tmp53 = lshr i8 %tmp52, 7
526 %tmp54 = mul i8 %tmp53, 16
527 ; CHECK: %0 = shl i8 %tmp4, 2
528 ; CHECK: %tmp54 = and i8 %0, 16
529 %tmp55 = xor i8 %tmp54, %tmp51
530 ; CHECK: ret i8 %tmp551
535 define i32 @test40(i32 %a, i32 %b) nounwind {
536 %shl1 = shl i32 1, %b
537 %shl2 = shl i32 %shl1, 2
538 %div = udiv i32 %a, %shl2
541 ; CHECK-NEXT: add i32 %b, 2
542 ; CHECK-NEXT: lshr i32 %a
543 ; CHECK-NEXT: ret i32
546 define i32 @test41(i32 %a, i32 %b) nounwind {
551 ; CHECK-NEXT: shl i32 8, %b
552 ; CHECK-NEXT: ret i32
555 define i32 @test42(i32 %a, i32 %b) nounwind {
556 %div = lshr i32 4096, %b ; must be exact otherwise we'd divide by zero
557 %div2 = udiv i32 %a, %div
560 ; CHECK-NEXT: lshr exact i32 4096, %b
563 define i32 @test43(i32 %a, i32 %b) nounwind {
564 %div = shl i32 4096, %b ; must be exact otherwise we'd divide by zero
565 %div2 = udiv i32 %a, %div
568 ; CHECK-NEXT: add i32 %b, 12
573 define i32 @test44(i32 %a) nounwind {
574 %y = shl nuw i32 %a, 1
578 ; CHECK-NEXT: %y = shl i32 %a, 5
579 ; CHECK-NEXT: ret i32 %y
582 define i32 @test45(i32 %a) nounwind {
583 %y = lshr exact i32 %a, 1
587 ; CHECK-NEXT: %y = lshr i32 %a, 5
588 ; CHECK-NEXT: ret i32 %y
591 define i32 @test46(i32 %a) {
592 %y = ashr exact i32 %a, 3
596 ; CHECK-NEXT: %z = ashr exact i32 %a, 2
597 ; CHECK-NEXT: ret i32 %z
600 define i32 @test47(i32 %a) {
601 %y = lshr exact i32 %a, 3
605 ; CHECK-NEXT: %z = lshr exact i32 %a, 2
606 ; CHECK-NEXT: ret i32 %z
609 define i32 @test48(i32 %x) {
610 %A = lshr exact i32 %x, 1
614 ; CHECK-NEXT: %B = shl i32 %x, 2
615 ; CHECK-NEXT: ret i32 %B
618 define i32 @test49(i32 %x) {
619 %A = ashr exact i32 %x, 1
623 ; CHECK-NEXT: %B = shl i32 %x, 2
624 ; CHECK-NEXT: ret i32 %B
627 define i32 @test50(i32 %x) {
628 %A = shl nsw i32 %x, 1
632 ; CHECK-NEXT: %B = ashr i32 %x, 2
633 ; CHECK-NEXT: ret i32 %B
636 define i32 @test51(i32 %x) {
637 %A = shl nuw i32 %x, 1
641 ; CHECK-NEXT: %B = lshr i32 %x, 2
642 ; CHECK-NEXT: ret i32 %B
645 define i32 @test52(i32 %x) {
646 %A = shl nsw i32 %x, 3
650 ; CHECK-NEXT: %B = shl nsw i32 %x, 2
651 ; CHECK-NEXT: ret i32 %B
654 define i32 @test53(i32 %x) {
655 %A = shl nuw i32 %x, 3
659 ; CHECK-NEXT: %B = shl nuw i32 %x, 2
660 ; CHECK-NEXT: ret i32 %B
663 define i32 @test54(i32 %x) {
664 %shr2 = lshr i32 %x, 1
665 %shl = shl i32 %shr2, 4
666 %and = and i32 %shl, 16
669 ; CHECK: shl i32 %x, 3
673 define i32 @test55(i32 %x) {
674 %shr2 = lshr i32 %x, 1
675 %shl = shl i32 %shr2, 4
679 ; CHECK: shl i32 %x, 3
682 define i32 @test56(i32 %x) {
683 %shr2 = lshr i32 %x, 1
684 %shl = shl i32 %shr2, 4
688 ; CHECK: shl i32 %shr2, 4
692 define i32 @test57(i32 %x) {
693 %shr = lshr i32 %x, 1
694 %shl = shl i32 %shr, 4
695 %and = and i32 %shl, 16
698 ; CHECK: shl i32 %x, 3
701 define i32 @test58(i32 %x) {
702 %shr = lshr i32 %x, 1
703 %shl = shl i32 %shr, 4
707 ; CHECK: shl i32 %x, 3
710 define i32 @test59(i32 %x) {
711 %shr = ashr i32 %x, 1
712 %shl = shl i32 %shr, 4
716 ; CHECK: %shl = shl i32 %shr1, 4
720 define i32 @test60(i32 %x) {
721 %shr = ashr i32 %x, 4
722 %shl = shl i32 %shr, 1
726 ; CHECK: ashr i32 %x, 3
730 define i32 @test61(i32 %x) {
731 %shr = ashr i32 %x, 4
732 %shl = shl i32 %shr, 1
736 ; CHECK: ashr i32 %x, 4
739 ; propagate "exact" trait
740 define i32 @test62(i32 %x) {
741 %shr = ashr exact i32 %x, 4
742 %shl = shl i32 %shr, 1
746 ; CHECK: ashr exact i32 %x, 3