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 @test7(i8 %A) {
75 ; CHECK-NEXT: ret i32 -1
76 %shift.upgrd.3 = zext i8 %A to i32
77 %B = ashr i32 -1, %shift.upgrd.3 ;; Always equal to -1
81 ;; (A << 5) << 3 === A << 8 == 0
82 define i8 @test8(i8 %A) {
85 %B = shl i8 %A, 5 ; <i8> [#uses=1]
86 %C = shl i8 %B, 3 ; <i8> [#uses=1]
90 ;; (A << 7) >> 7 === A & 1
91 define i8 @test9(i8 %A) {
93 ; CHECK-NEXT: and i8 %A, 1
95 %B = shl i8 %A, 7 ; <i8> [#uses=1]
96 %C = lshr i8 %B, 7 ; <i8> [#uses=1]
100 ;; (A >> 7) << 7 === A & 128
101 define i8 @test10(i8 %A) {
103 ; CHECK-NEXT: and i8 %A, -128
105 %B = lshr i8 %A, 7 ; <i8> [#uses=1]
106 %C = shl i8 %B, 7 ; <i8> [#uses=1]
110 ;; (A >> 3) << 4 === (A & 0x1F) << 1
111 define i8 @test11(i8 %A) {
113 ; CHECK-NEXT: mul i8 %A, 6
116 %a = mul i8 %A, 3 ; <i8> [#uses=1]
117 %B = lshr i8 %a, 3 ; <i8> [#uses=1]
118 %C = shl i8 %B, 4 ; <i8> [#uses=1]
122 ;; (A >> 8) << 8 === A & -256
123 define i32 @test12(i32 %A) {
125 ; CHECK-NEXT: and i32 %A, -256
126 ; CHECK-NEXT: ret i32
127 %B = ashr i32 %A, 8 ; <i32> [#uses=1]
128 %C = shl i32 %B, 8 ; <i32> [#uses=1]
132 ;; (A >> 3) << 4 === (A & -8) * 2
133 define i8 @test13(i8 %A) {
135 ; CHECK-NEXT: mul i8 %A, 6
138 %a = mul i8 %A, 3 ; <i8> [#uses=1]
139 %B = ashr i8 %a, 3 ; <i8> [#uses=1]
140 %C = shl i8 %B, 4 ; <i8> [#uses=1]
144 ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
145 define i32 @test14(i32 %A) {
147 ; CHECK-NEXT: %B = and i32 %A, -19760
148 ; CHECK-NEXT: or i32 %B, 19744
149 ; CHECK-NEXT: ret i32
150 %B = lshr i32 %A, 4 ; <i32> [#uses=1]
151 %C = or i32 %B, 1234 ; <i32> [#uses=1]
152 %D = shl i32 %C, 4 ; <i32> [#uses=1]
156 ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
157 define i32 @test14a(i32 %A) {
159 ; CHECK-NEXT: and i32 %A, 77
160 ; CHECK-NEXT: ret i32
161 %B = shl i32 %A, 4 ; <i32> [#uses=1]
162 %C = and i32 %B, 1234 ; <i32> [#uses=1]
163 %D = lshr i32 %C, 4 ; <i32> [#uses=1]
167 define i32 @test15(i1 %C) {
169 ; CHECK-NEXT: select i1 %C, i32 12, i32 4
170 ; CHECK-NEXT: ret i32
171 %A = select i1 %C, i32 3, i32 1 ; <i32> [#uses=1]
172 %V = shl i32 %A, 2 ; <i32> [#uses=1]
176 define i32 @test15a(i1 %C) {
178 ; CHECK-NEXT: select i1 %C, i32 512, i32 128
179 ; CHECK-NEXT: ret i32
180 %A = select i1 %C, i8 3, i8 1 ; <i8> [#uses=1]
181 %shift.upgrd.4 = zext i8 %A to i32 ; <i32> [#uses=1]
182 %V = shl i32 64, %shift.upgrd.4 ; <i32> [#uses=1]
186 define i1 @test16(i32 %X) {
188 ; CHECK-NEXT: and i32 %X, 16
189 ; CHECK-NEXT: icmp ne i32
191 %tmp.3 = ashr i32 %X, 4
192 %tmp.6 = and i32 %tmp.3, 1
193 %tmp.7 = icmp ne i32 %tmp.6, 0
197 define i1 @test17(i32 %A) {
199 ; CHECK-NEXT: and i32 %A, -8
200 ; CHECK-NEXT: icmp eq i32
202 %B = lshr i32 %A, 3 ; <i32> [#uses=1]
203 %C = icmp eq i32 %B, 1234 ; <i1> [#uses=1]
208 define i1 @test18(i8 %A) {
210 ; CHECK: ret i1 false
212 %B = lshr i8 %A, 7 ; <i8> [#uses=1]
214 %C = icmp eq i8 %B, 123 ; <i1> [#uses=1]
218 define i1 @test19(i32 %A) {
220 ; CHECK-NEXT: icmp ult i32 %A, 4
222 %B = ashr i32 %A, 2 ; <i32> [#uses=1]
224 %C = icmp eq i32 %B, 0 ; <i1> [#uses=1]
229 define i1 @test19a(i32 %A) {
231 ; CHECK-NEXT: and i32 %A, -4
232 ; CHECK-NEXT: icmp eq i32
234 %B = ashr i32 %A, 2 ; <i32> [#uses=1]
236 %C = icmp eq i32 %B, -1 ; <i1> [#uses=1]
240 define i1 @test20(i8 %A) {
242 ; CHECK: ret i1 false
243 %B = ashr i8 %A, 7 ; <i8> [#uses=1]
245 %C = icmp eq i8 %B, 123 ; <i1> [#uses=1]
249 define i1 @test21(i8 %A) {
251 ; CHECK-NEXT: and i8 %A, 15
252 ; CHECK-NEXT: icmp eq i8
254 %B = shl i8 %A, 4 ; <i8> [#uses=1]
255 %C = icmp eq i8 %B, -128 ; <i1> [#uses=1]
259 define i1 @test22(i8 %A) {
261 ; CHECK-NEXT: and i8 %A, 15
262 ; CHECK-NEXT: icmp eq i8
264 %B = shl i8 %A, 4 ; <i8> [#uses=1]
265 %C = icmp eq i8 %B, 0 ; <i1> [#uses=1]
269 define i8 @test23(i32 %A) {
271 ; CHECK-NEXT: trunc i32 %A to i8
275 %B = shl i32 %A, 24 ; <i32> [#uses=1]
276 %C = ashr i32 %B, 24 ; <i32> [#uses=1]
277 %D = trunc i32 %C to i8 ; <i8> [#uses=1]
281 define i8 @test24(i8 %X) {
283 ; CHECK-NEXT: and i8 %X, 3
285 %Y = and i8 %X, -5 ; <i8> [#uses=1]
286 %Z = shl i8 %Y, 5 ; <i8> [#uses=1]
287 %Q = ashr i8 %Z, 5 ; <i8> [#uses=1]
291 define i32 @test25(i32 %tmp.2, i32 %AA) {
293 ; CHECK-NEXT: and i32 %tmp.2, -131072
294 ; CHECK-NEXT: add i32 %{{[^,]*}}, %AA
295 ; CHECK-NEXT: and i32 %{{[^,]*}}, -131072
296 ; CHECK-NEXT: ret i32
297 %x = lshr i32 %AA, 17 ; <i32> [#uses=1]
298 %tmp.3 = lshr i32 %tmp.2, 17 ; <i32> [#uses=1]
299 %tmp.5 = add i32 %tmp.3, %x ; <i32> [#uses=1]
300 %tmp.6 = shl i32 %tmp.5, 17 ; <i32> [#uses=1]
304 ;; handle casts between shifts.
305 define i32 @test26(i32 %A) {
307 ; CHECK-NEXT: and i32 %A, -2
308 ; CHECK-NEXT: ret i32
309 %B = lshr i32 %A, 1 ; <i32> [#uses=1]
310 %C = bitcast i32 %B to i32 ; <i32> [#uses=1]
311 %D = shl i32 %C, 1 ; <i32> [#uses=1]
316 define i1 @test27(i32 %x) nounwind {
318 ; CHECK-NEXT: and i32 %x, 8
319 ; CHECK-NEXT: icmp ne i32
322 %z = trunc i32 %y to i1
326 define i8 @test28(i8 %x) {
329 ; CHECK: icmp slt i8 %x, 0
331 %tmp1 = lshr i8 %x, 7
332 %cond1 = icmp ne i8 %tmp1, 0
333 br i1 %cond1, label %bb1, label %bb2
342 define i8 @test28a(i8 %x, i8 %y) {
344 ; This shouldn't be transformed.
346 ; CHECK: %tmp1 = lshr i8 %x, 7
347 ; CHECK: %cond1 = icmp eq i8 %tmp1, 0
348 ; CHECK: br i1 %cond1, label %bb2, label %bb1
349 %tmp1 = lshr i8 %x, 7
350 %cond1 = icmp ne i8 %tmp1, 0
351 br i1 %cond1, label %bb1, label %bb2
355 %tmp2 = add i8 %tmp1, %y
360 define i32 @test29(i64 %d18) {
362 %tmp916 = lshr i64 %d18, 32
363 %tmp917 = trunc i64 %tmp916 to i32
364 %tmp10 = lshr i32 %tmp917, 31
367 ; CHECK: %tmp916 = lshr i64 %d18, 63
368 ; CHECK: %tmp10 = trunc i64 %tmp916 to i32
372 define i32 @test30(i32 %A, i32 %B, i32 %C) {
378 ; CHECK: %X1 = and i32 %A, %B
379 ; CHECK: %Z = shl i32 %X1, %C
382 define i32 @test31(i32 %A, i32 %B, i32 %C) {
388 ; CHECK: %X1 = or i32 %A, %B
389 ; CHECK: %Z = lshr i32 %X1, %C
392 define i32 @test32(i32 %A, i32 %B, i32 %C) {
398 ; CHECK: %X1 = xor i32 %A, %B
399 ; CHECK: %Z = ashr i32 %X1, %C
403 define i1 @test33(i32 %X) {
404 %tmp1 = shl i32 %X, 7
405 %tmp2 = icmp slt i32 %tmp1, 0
408 ; CHECK: %tmp1.mask = and i32 %X, 16777216
409 ; CHECK: %tmp2 = icmp ne i32 %tmp1.mask, 0
412 define i1 @test34(i32 %X) {
413 %tmp1 = lshr i32 %X, 7
414 %tmp2 = icmp slt i32 %tmp1, 0
417 ; CHECK: ret i1 false
420 define i1 @test35(i32 %X) {
421 %tmp1 = ashr i32 %X, 7
422 %tmp2 = icmp slt i32 %tmp1, 0
425 ; CHECK: %tmp2 = icmp slt i32 %X, 0
426 ; CHECK: ret i1 %tmp2
429 define i128 @test36(i128 %A, i128 %B) {
431 %tmp27 = shl i128 %A, 64
432 %tmp23 = shl i128 %B, 64
433 %ins = or i128 %tmp23, %tmp27
434 %tmp45 = lshr i128 %ins, 64
438 ; CHECK: %tmp231 = or i128 %B, %A
439 ; CHECK: %ins = and i128 %tmp231, 18446744073709551615
440 ; CHECK: ret i128 %ins
443 define i64 @test37(i128 %A, i32 %B) {
445 %tmp27 = shl i128 %A, 64
446 %tmp22 = zext i32 %B to i128
447 %tmp23 = shl i128 %tmp22, 96
448 %ins = or i128 %tmp23, %tmp27
449 %tmp45 = lshr i128 %ins, 64
450 %tmp46 = trunc i128 %tmp45 to i64
454 ; CHECK: %tmp23 = shl i128 %tmp22, 32
455 ; CHECK: %ins = or i128 %tmp23, %A
456 ; CHECK: %tmp46 = trunc i128 %ins to i64
459 define i32 @test38(i32 %x) nounwind readnone {
460 %rem = srem i32 %x, 32
461 %shl = shl i32 1, %rem
464 ; CHECK-NEXT: and i32 %x, 31
465 ; CHECK-NEXT: shl i32 1
466 ; CHECK-NEXT: ret i32
469 ; <rdar://problem/8756731>
471 define i8 @test39(i32 %a0) {
473 %tmp4 = trunc i32 %a0 to i8
474 ; CHECK: and i8 %tmp49, 64
475 %tmp5 = shl i8 %tmp4, 5
476 %tmp48 = and i8 %tmp5, 32
477 %tmp49 = lshr i8 %tmp48, 5
478 %tmp50 = mul i8 %tmp49, 64
479 %tmp51 = xor i8 %tmp50, %tmp5
480 ; CHECK: and i8 %0, 16
481 %tmp52 = and i8 %tmp51, -128
482 %tmp53 = lshr i8 %tmp52, 7
483 %tmp54 = mul i8 %tmp53, 16
484 %tmp55 = xor i8 %tmp54, %tmp51
485 ; CHECK: ret i8 %tmp551