Fix shift by constants for vector.
[oota-llvm.git] / test / Transforms / InstCombine / shift.ll
1 ; This test makes sure that these instructions are properly eliminated.
2 ;
3 ; RUN: opt < %s -instcombine -S | FileCheck %s
4
5 define i32 @test1(i32 %A) {
6 ; CHECK-LABEL: @test1(
7 ; CHECK: ret i32 %A
8         %B = shl i32 %A, 0              ; <i32> [#uses=1]
9         ret i32 %B
10 }
11
12 define i32 @test2(i8 %A) {
13 ; CHECK-LABEL: @test2(
14 ; CHECK: ret i32 0
15         %shift.upgrd.1 = zext i8 %A to i32              ; <i32> [#uses=1]
16         %B = shl i32 0, %shift.upgrd.1          ; <i32> [#uses=1]
17         ret i32 %B
18 }
19
20 define i32 @test3(i32 %A) {
21 ; CHECK-LABEL: @test3(
22 ; CHECK: ret i32 %A
23         %B = ashr i32 %A, 0             ; <i32> [#uses=1]
24         ret i32 %B
25 }
26
27 define i32 @test4(i8 %A) {
28 ; CHECK-LABEL: @test4(
29 ; CHECK: ret i32 0
30         %shift.upgrd.2 = zext i8 %A to i32              ; <i32> [#uses=1]
31         %B = ashr i32 0, %shift.upgrd.2         ; <i32> [#uses=1]
32         ret i32 %B
33 }
34
35
36 define i32 @test5(i32 %A) {
37 ; CHECK-LABEL: @test5(
38 ; CHECK: ret i32 undef
39         %B = lshr i32 %A, 32  ;; shift all bits out
40         ret i32 %B
41 }
42
43 define <4 x i32> @test5_splat_vector(<4 x i32> %A) {
44 ; CHECK-LABEL: @test5_splat_vector(
45 ; CHECK: ret <4 x i32> undef
46   %B = lshr <4 x i32> %A, <i32 32, i32 32, i32 32, i32 32>     ;; shift all bits out
47   ret <4 x i32> %B
48 }
49
50 define <4 x i32> @test5_zero_vector(<4 x i32> %A) {
51 ; CHECK-LABEL: @test5_zero_vector(
52 ; CHECK-NEXT: ret <4 x i32> %A
53   %B = lshr <4 x i32> %A, zeroinitializer
54   ret <4 x i32> %B
55 }
56
57 define <4 x i32> @test5_non_splat_vector(<4 x i32> %A) {
58 ; CHECK-LABEL: @test5_non_splat_vector(
59 ; CHECK-NOT: ret <4 x i32> undef
60   %B = shl <4 x i32> %A, <i32 32, i32 1, i32 2, i32 3>
61   ret <4 x i32> %B
62 }
63
64 define i32 @test5a(i32 %A) {
65 ; CHECK-LABEL: @test5a(
66 ; CHECK: ret i32 undef
67         %B = shl i32 %A, 32     ;; shift all bits out
68         ret i32 %B
69 }
70
71 define <4 x i32> @test5a_splat_vector(<4 x i32> %A) {
72 ; CHECK-LABEL: @test5a_splat_vector(
73 ; CHECK: ret <4 x i32> undef
74   %B = shl <4 x i32> %A, <i32 32, i32 32, i32 32, i32 32>     ;; shift all bits out
75   ret <4 x i32> %B
76 }
77
78 define <4 x i32> @test5a_non_splat_vector(<4 x i32> %A) {
79 ; CHECK-LABEL: @test5a_non_splat_vector(
80 ; CHECK-NOT: ret <4 x i32> undef
81   %B = shl <4 x i32> %A, <i32 32, i32 1, i32 2, i32 3>
82   ret <4 x i32> %B
83 }
84
85 define i32 @test5b() {
86 ; CHECK-LABEL: @test5b(
87 ; CHECK: ret i32 -1
88         %B = ashr i32 undef, 2  ;; top two bits must be equal, so not undef
89         ret i32 %B
90 }
91
92 define i32 @test5b2(i32 %A) {
93 ; CHECK-LABEL: @test5b2(
94 ; CHECK: ret i32 -1
95         %B = ashr i32 undef, %A  ;; top %A bits must be equal, so not undef
96         ret i32 %B
97 }
98
99 define i32 @test6(i32 %A) {
100 ; CHECK-LABEL: @test6(
101 ; CHECK-NEXT: mul i32 %A, 6
102 ; CHECK-NEXT: ret i32
103         %B = shl i32 %A, 1      ;; convert to an mul instruction
104         %C = mul i32 %B, 3
105         ret i32 %C
106 }
107
108 define i32 @test6a(i32 %A) {
109 ; CHECK-LABEL: @test6a(
110 ; CHECK-NEXT: mul i32 %A, 6
111 ; CHECK-NEXT: ret i32
112         %B = mul i32 %A, 3
113         %C = shl i32 %B, 1      ;; convert to an mul instruction
114         ret i32 %C
115 }
116
117 define i32 @test7(i8 %A) {
118 ; CHECK-LABEL: @test7(
119 ; CHECK-NEXT: ret i32 -1
120         %shift.upgrd.3 = zext i8 %A to i32
121         %B = ashr i32 -1, %shift.upgrd.3  ;; Always equal to -1
122         ret i32 %B
123 }
124
125 ;; (A << 5) << 3 === A << 8 == 0
126 define i8 @test8(i8 %A) {
127 ; CHECK-LABEL: @test8(
128 ; CHECK: ret i8 0
129         %B = shl i8 %A, 5               ; <i8> [#uses=1]
130         %C = shl i8 %B, 3               ; <i8> [#uses=1]
131         ret i8 %C
132 }
133
134 ;; (A << 7) >> 7 === A & 1
135 define i8 @test9(i8 %A) {
136 ; CHECK-LABEL: @test9(
137 ; CHECK-NEXT: and i8 %A, 1
138 ; CHECK-NEXT: ret i8
139         %B = shl i8 %A, 7               ; <i8> [#uses=1]
140         %C = lshr i8 %B, 7              ; <i8> [#uses=1]
141         ret i8 %C
142 }
143
144 ;; This transformation is deferred to DAGCombine:
145 ;; (A >> 7) << 7 === A & 128
146 ;; The shl may be valuable to scalar evolution.
147 define i8 @test10(i8 %A) {
148 ; CHECK-LABEL: @test10(
149 ; CHECK-NEXT: and i8 %A, -128
150 ; CHECK-NEXT: ret i8
151         %B = lshr i8 %A, 7              ; <i8> [#uses=1]
152         %C = shl i8 %B, 7               ; <i8> [#uses=1]
153         ret i8 %C
154 }
155
156 ;; Allow the simplification when the lshr shift is exact.
157 define i8 @test10a(i8 %A) {
158 ; CHECK-LABEL: @test10a(
159 ; CHECK-NEXT: ret i8 %A
160         %B = lshr exact i8 %A, 7
161         %C = shl i8 %B, 7
162         ret i8 %C
163 }
164
165 ;; This transformation is deferred to DAGCombine:
166 ;; (A >> 3) << 4 === (A & 0x1F) << 1
167 ;; The shl may be valuable to scalar evolution.
168 define i8 @test11(i8 %A) {
169 ; CHECK-LABEL: @test11(
170 ; CHECK: shl i8
171 ; CHECK-NEXT: ret i8
172         %a = mul i8 %A, 3               ; <i8> [#uses=1]
173         %B = lshr i8 %a, 3              ; <i8> [#uses=1]
174         %C = shl i8 %B, 4               ; <i8> [#uses=1]
175         ret i8 %C
176 }
177
178 ;; Allow the simplification in InstCombine when the lshr shift is exact.
179 define i8 @test11a(i8 %A) {
180 ; CHECK-LABEL: @test11a(
181 ; CHECK-NEXT: mul i8 %A, 6
182 ; CHECK-NEXT: ret i8
183         %a = mul i8 %A, 3
184         %B = lshr exact i8 %a, 3
185         %C = shl i8 %B, 4
186         ret i8 %C
187 }
188
189 ;; This is deferred to DAGCombine unless %B is single-use.
190 ;; (A >> 8) << 8 === A & -256
191 define i32 @test12(i32 %A) {
192 ; CHECK-LABEL: @test12(
193 ; CHECK-NEXT: and i32 %A, -256
194 ; CHECK-NEXT: ret i32
195         %B = ashr i32 %A, 8             ; <i32> [#uses=1]
196         %C = shl i32 %B, 8              ; <i32> [#uses=1]
197         ret i32 %C
198 }
199
200 ;; This transformation is deferred to DAGCombine:
201 ;; (A >> 3) << 4 === (A & -8) * 2
202 ;; The shl may be valuable to scalar evolution.
203 define i8 @test13(i8 %A) {
204 ; CHECK-LABEL: @test13(
205 ; CHECK: shl i8
206 ; CHECK-NEXT: ret i8
207         %a = mul i8 %A, 3               ; <i8> [#uses=1]
208         %B = ashr i8 %a, 3              ; <i8> [#uses=1]
209         %C = shl i8 %B, 4               ; <i8> [#uses=1]
210         ret i8 %C
211 }
212
213 define i8 @test13a(i8 %A) {
214 ; CHECK-LABEL: @test13a(
215 ; CHECK-NEXT: mul i8 %A, 6
216 ; CHECK-NEXT: ret i8
217         %a = mul i8 %A, 3
218         %B = ashr exact i8 %a, 3
219         %C = shl i8 %B, 4
220         ret i8 %C
221 }
222
223 ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
224 define i32 @test14(i32 %A) {
225 ; CHECK-LABEL: @test14(
226 ; CHECK-NEXT: %B = and i32 %A, -19760
227 ; CHECK-NEXT: or i32 %B, 19744
228 ; CHECK-NEXT: ret i32
229         %B = lshr i32 %A, 4             ; <i32> [#uses=1]
230         %C = or i32 %B, 1234            ; <i32> [#uses=1]
231         %D = shl i32 %C, 4              ; <i32> [#uses=1]
232         ret i32 %D
233 }
234
235 ;; D = ((B | 1234) << 4) === ((B << 4)|(1234 << 4)
236 define i32 @test14a(i32 %A) {
237 ; CHECK-LABEL: @test14a(
238 ; CHECK-NEXT: and i32 %A, 77
239 ; CHECK-NEXT: ret i32
240         %B = shl i32 %A, 4              ; <i32> [#uses=1]
241         %C = and i32 %B, 1234           ; <i32> [#uses=1]
242         %D = lshr i32 %C, 4             ; <i32> [#uses=1]
243         ret i32 %D
244 }
245
246 define i32 @test15(i1 %C) {
247 ; CHECK-LABEL: @test15(
248 ; CHECK-NEXT: select i1 %C, i32 12, i32 4
249 ; CHECK-NEXT: ret i32
250         %A = select i1 %C, i32 3, i32 1         ; <i32> [#uses=1]
251         %V = shl i32 %A, 2              ; <i32> [#uses=1]
252         ret i32 %V
253 }
254
255 define i32 @test15a(i1 %C) {
256 ; CHECK-LABEL: @test15a(
257 ; CHECK-NEXT: select i1 %C, i32 512, i32 128
258 ; CHECK-NEXT: ret i32
259         %A = select i1 %C, i8 3, i8 1           ; <i8> [#uses=1]
260         %shift.upgrd.4 = zext i8 %A to i32              ; <i32> [#uses=1]
261         %V = shl i32 64, %shift.upgrd.4         ; <i32> [#uses=1]
262         ret i32 %V
263 }
264
265 define i1 @test16(i32 %X) {
266 ; CHECK-LABEL: @test16(
267 ; CHECK-NEXT: and i32 %X, 16
268 ; CHECK-NEXT: icmp ne i32
269 ; CHECK-NEXT: ret i1
270         %tmp.3 = ashr i32 %X, 4
271         %tmp.6 = and i32 %tmp.3, 1
272         %tmp.7 = icmp ne i32 %tmp.6, 0
273         ret i1 %tmp.7
274 }
275
276 define i1 @test17(i32 %A) {
277 ; CHECK-LABEL: @test17(
278 ; CHECK-NEXT: and i32 %A, -8
279 ; CHECK-NEXT: icmp eq i32
280 ; CHECK-NEXT: ret i1
281         %B = lshr i32 %A, 3             ; <i32> [#uses=1]
282         %C = icmp eq i32 %B, 1234               ; <i1> [#uses=1]
283         ret i1 %C
284 }
285
286
287 define i1 @test18(i8 %A) {
288 ; CHECK-LABEL: @test18(
289 ; CHECK: ret i1 false
290
291         %B = lshr i8 %A, 7              ; <i8> [#uses=1]
292         ;; false
293         %C = icmp eq i8 %B, 123         ; <i1> [#uses=1]
294         ret i1 %C
295 }
296
297 define i1 @test19(i32 %A) {
298 ; CHECK-LABEL: @test19(
299 ; CHECK-NEXT: icmp ult i32 %A, 4
300 ; CHECK-NEXT: ret i1
301         %B = ashr i32 %A, 2             ; <i32> [#uses=1]
302         ;; (X & -4) == 0
303         %C = icmp eq i32 %B, 0          ; <i1> [#uses=1]
304         ret i1 %C
305 }
306
307
308 define i1 @test19a(i32 %A) {
309 ; CHECK-LABEL: @test19a(
310 ; CHECK-NEXT: icmp ugt i32 %A, -5
311 ; CHECK-NEXT: ret i1
312         %B = ashr i32 %A, 2             ; <i32> [#uses=1]
313         ;; X >u ~4
314         %C = icmp eq i32 %B, -1         ; <i1> [#uses=1]
315         ret i1 %C
316 }
317
318 define i1 @test20(i8 %A) {
319 ; CHECK-LABEL: @test20(
320 ; CHECK: ret i1 false
321         %B = ashr i8 %A, 7              ; <i8> [#uses=1]
322         ;; false
323         %C = icmp eq i8 %B, 123         ; <i1> [#uses=1]
324         ret i1 %C
325 }
326
327 define i1 @test21(i8 %A) {
328 ; CHECK-LABEL: @test21(
329 ; CHECK-NEXT: and i8 %A, 15
330 ; CHECK-NEXT: icmp eq i8
331 ; CHECK-NEXT: ret i1
332         %B = shl i8 %A, 4               ; <i8> [#uses=1]
333         %C = icmp eq i8 %B, -128                ; <i1> [#uses=1]
334         ret i1 %C
335 }
336
337 define i1 @test22(i8 %A) {
338 ; CHECK-LABEL: @test22(
339 ; CHECK-NEXT: and i8 %A, 15
340 ; CHECK-NEXT: icmp eq i8
341 ; CHECK-NEXT: ret i1
342         %B = shl i8 %A, 4               ; <i8> [#uses=1]
343         %C = icmp eq i8 %B, 0           ; <i1> [#uses=1]
344         ret i1 %C
345 }
346
347 define i8 @test23(i32 %A) {
348 ; CHECK-LABEL: @test23(
349 ; CHECK-NEXT: trunc i32 %A to i8
350 ; CHECK-NEXT: ret i8
351
352         ;; casts not needed
353         %B = shl i32 %A, 24             ; <i32> [#uses=1]
354         %C = ashr i32 %B, 24            ; <i32> [#uses=1]
355         %D = trunc i32 %C to i8         ; <i8> [#uses=1]
356         ret i8 %D
357 }
358
359 define i8 @test24(i8 %X) {
360 ; CHECK-LABEL: @test24(
361 ; CHECK-NEXT: and i8 %X, 3
362 ; CHECK-NEXT: ret i8
363         %Y = and i8 %X, -5              ; <i8> [#uses=1]
364         %Z = shl i8 %Y, 5               ; <i8> [#uses=1]
365         %Q = ashr i8 %Z, 5              ; <i8> [#uses=1]
366         ret i8 %Q
367 }
368
369 define i32 @test25(i32 %tmp.2, i32 %AA) {
370 ; CHECK-LABEL: @test25(
371 ; CHECK-NEXT: and i32 %tmp.2, -131072
372 ; CHECK-NEXT: add i32 %{{[^,]*}}, %AA
373 ; CHECK-NEXT: and i32 %{{[^,]*}}, -131072
374 ; CHECK-NEXT: ret i32
375         %x = lshr i32 %AA, 17           ; <i32> [#uses=1]
376         %tmp.3 = lshr i32 %tmp.2, 17            ; <i32> [#uses=1]
377         %tmp.5 = add i32 %tmp.3, %x             ; <i32> [#uses=1]
378         %tmp.6 = shl i32 %tmp.5, 17             ; <i32> [#uses=1]
379         ret i32 %tmp.6
380 }
381
382 ;; handle casts between shifts.
383 define i32 @test26(i32 %A) {
384 ; CHECK-LABEL: @test26(
385 ; CHECK-NEXT: and i32 %A, -2
386 ; CHECK-NEXT: ret i32
387         %B = lshr i32 %A, 1             ; <i32> [#uses=1]
388         %C = bitcast i32 %B to i32              ; <i32> [#uses=1]
389         %D = shl i32 %C, 1              ; <i32> [#uses=1]
390         ret i32 %D
391 }
392
393
394 define i1 @test27(i32 %x) nounwind {
395 ; CHECK-LABEL: @test27(
396 ; CHECK-NEXT: and i32 %x, 8
397 ; CHECK-NEXT: icmp ne i32
398 ; CHECK-NEXT: ret i1
399   %y = lshr i32 %x, 3
400   %z = trunc i32 %y to i1
401   ret i1 %z
402 }
403
404 define i8 @test28(i8 %x) {
405 entry:
406 ; CHECK-LABEL: @test28(
407 ; CHECK:     icmp slt i8 %x, 0
408 ; CHECK-NEXT:     br i1
409         %tmp1 = lshr i8 %x, 7
410         %cond1 = icmp ne i8 %tmp1, 0
411         br i1 %cond1, label %bb1, label %bb2
412
413 bb1:
414         ret i8 0
415
416 bb2:
417         ret i8 1
418 }
419
420 define i8 @test28a(i8 %x, i8 %y) {
421 entry:
422 ; This shouldn't be transformed.
423 ; CHECK-LABEL: @test28a(
424 ; CHECK:     %tmp1 = lshr i8 %x, 7
425 ; CHECK:     %cond1 = icmp eq i8 %tmp1, 0
426 ; CHECK:     br i1 %cond1, label %bb2, label %bb1
427         %tmp1 = lshr i8 %x, 7
428         %cond1 = icmp ne i8 %tmp1, 0
429         br i1 %cond1, label %bb1, label %bb2
430 bb1:
431         ret i8 %tmp1
432 bb2:
433         %tmp2 = add i8 %tmp1, %y
434         ret i8 %tmp2
435 }
436
437
438 define i32 @test29(i64 %d18) {
439 entry:
440         %tmp916 = lshr i64 %d18, 32
441         %tmp917 = trunc i64 %tmp916 to i32
442         %tmp10 = lshr i32 %tmp917, 31
443         ret i32 %tmp10
444 ; CHECK-LABEL: @test29(
445 ; CHECK:  %tmp916 = lshr i64 %d18, 63
446 ; CHECK:  %tmp10 = trunc i64 %tmp916 to i32
447 }
448
449
450 define i32 @test30(i32 %A, i32 %B, i32 %C) {
451         %X = shl i32 %A, %C
452         %Y = shl i32 %B, %C
453         %Z = and i32 %X, %Y
454         ret i32 %Z
455 ; CHECK-LABEL: @test30(
456 ; CHECK: %X1 = and i32 %A, %B
457 ; CHECK: %Z = shl i32 %X1, %C
458 }
459
460 define i32 @test31(i32 %A, i32 %B, i32 %C) {
461         %X = lshr i32 %A, %C
462         %Y = lshr i32 %B, %C
463         %Z = or i32 %X, %Y
464         ret i32 %Z
465 ; CHECK-LABEL: @test31(
466 ; CHECK: %X1 = or i32 %A, %B
467 ; CHECK: %Z = lshr i32 %X1, %C
468 }
469
470 define i32 @test32(i32 %A, i32 %B, i32 %C) {
471         %X = ashr i32 %A, %C
472         %Y = ashr i32 %B, %C
473         %Z = xor i32 %X, %Y
474         ret i32 %Z
475 ; CHECK-LABEL: @test32(
476 ; CHECK: %X1 = xor i32 %A, %B
477 ; CHECK: %Z = ashr i32 %X1, %C
478 ; CHECK: ret i32 %Z
479 }
480
481 define i1 @test33(i32 %X) {
482         %tmp1 = shl i32 %X, 7
483         %tmp2 = icmp slt i32 %tmp1, 0
484         ret i1 %tmp2
485 ; CHECK-LABEL: @test33(
486 ; CHECK: %tmp1.mask = and i32 %X, 16777216
487 ; CHECK: %tmp2 = icmp ne i32 %tmp1.mask, 0
488 }
489
490 define i1 @test34(i32 %X) {
491         %tmp1 = lshr i32 %X, 7
492         %tmp2 = icmp slt i32 %tmp1, 0
493         ret i1 %tmp2
494 ; CHECK-LABEL: @test34(
495 ; CHECK: ret i1 false
496 }
497
498 define i1 @test35(i32 %X) {
499         %tmp1 = ashr i32 %X, 7
500         %tmp2 = icmp slt i32 %tmp1, 0
501         ret i1 %tmp2
502 ; CHECK-LABEL: @test35(
503 ; CHECK: %tmp2 = icmp slt i32 %X, 0
504 ; CHECK: ret i1 %tmp2
505 }
506
507 define i128 @test36(i128 %A, i128 %B) {
508 entry:
509   %tmp27 = shl i128 %A, 64
510   %tmp23 = shl i128 %B, 64
511   %ins = or i128 %tmp23, %tmp27
512   %tmp45 = lshr i128 %ins, 64
513   ret i128 %tmp45
514
515 ; CHECK-LABEL: @test36(
516 ; CHECK:  %tmp231 = or i128 %B, %A
517 ; CHECK:  %ins = and i128 %tmp231, 18446744073709551615
518 ; CHECK:  ret i128 %ins
519 }
520
521 define i64 @test37(i128 %A, i32 %B) {
522 entry:
523   %tmp27 = shl i128 %A, 64
524   %tmp22 = zext i32 %B to i128
525   %tmp23 = shl i128 %tmp22, 96
526   %ins = or i128 %tmp23, %tmp27
527   %tmp45 = lshr i128 %ins, 64
528   %tmp46 = trunc i128 %tmp45 to i64
529   ret i64 %tmp46
530
531 ; CHECK-LABEL: @test37(
532 ; CHECK:  %tmp23 = shl nuw nsw i128 %tmp22, 32
533 ; CHECK:  %ins = or i128 %tmp23, %A
534 ; CHECK:  %tmp46 = trunc i128 %ins to i64
535 }
536
537 define i32 @test38(i32 %x) nounwind readnone {
538   %rem = srem i32 %x, 32
539   %shl = shl i32 1, %rem
540   ret i32 %shl
541 ; CHECK-LABEL: @test38(
542 ; CHECK-NEXT: and i32 %x, 31
543 ; CHECK-NEXT: shl i32 1
544 ; CHECK-NEXT: ret i32
545 }
546
547 ; <rdar://problem/8756731>
548 ; CHECK-LABEL: @test39(
549 define i8 @test39(i32 %a0) {
550 entry:
551   %tmp4 = trunc i32 %a0 to i8
552 ; CHECK: and i8 %tmp49, 64
553   %tmp5 = shl i8 %tmp4, 5
554   %tmp48 = and i8 %tmp5, 32
555   %tmp49 = lshr i8 %tmp48, 5
556   %tmp50 = mul i8 %tmp49, 64
557   %tmp51 = xor i8 %tmp50, %tmp5
558   %tmp52 = and i8 %tmp51, -128
559   %tmp53 = lshr i8 %tmp52, 7
560   %tmp54 = mul i8 %tmp53, 16
561 ; CHECK: %0 = shl i8 %tmp4, 2
562 ; CHECK: %tmp54 = and i8 %0, 16
563   %tmp55 = xor i8 %tmp54, %tmp51
564 ; CHECK: ret i8 %tmp551
565   ret i8 %tmp55
566 }
567
568 ; PR9809
569 define i32 @test40(i32 %a, i32 %b) nounwind {
570   %shl1 = shl i32 1, %b
571   %shl2 = shl i32 %shl1, 2
572   %div = udiv i32 %a, %shl2
573   ret i32 %div
574 ; CHECK-LABEL: @test40(
575 ; CHECK-NEXT: add i32 %b, 2
576 ; CHECK-NEXT: lshr i32 %a
577 ; CHECK-NEXT: ret i32
578 }
579
580 define i32 @test41(i32 %a, i32 %b) nounwind {
581   %1 = shl i32 1, %b
582   %2 = shl i32 %1, 3
583   ret i32 %2
584 ; CHECK-LABEL: @test41(
585 ; CHECK-NEXT: shl i32 8, %b
586 ; CHECK-NEXT: ret i32
587 }
588
589 define i32 @test42(i32 %a, i32 %b) nounwind {
590   %div = lshr i32 4096, %b    ; must be exact otherwise we'd divide by zero
591   %div2 = udiv i32 %a, %div
592   ret i32 %div2
593 ; CHECK-LABEL: @test42(
594 ; CHECK-NEXT: lshr exact i32 4096, %b
595 }
596
597 define i32 @test43(i32 %a, i32 %b) nounwind {
598   %div = shl i32 4096, %b    ; must be exact otherwise we'd divide by zero
599   %div2 = udiv i32 %a, %div
600   ret i32 %div2
601 ; CHECK-LABEL: @test43(
602 ; CHECK-NEXT: add i32 %b, 12
603 ; CHECK-NEXT: lshr
604 ; CHECK-NEXT: ret
605 }
606
607 define i32 @test44(i32 %a) nounwind {
608   %y = shl nuw i32 %a, 1
609   %z = shl i32 %y, 4
610   ret i32 %z
611 ; CHECK-LABEL: @test44(
612 ; CHECK-NEXT: %y = shl i32 %a, 5
613 ; CHECK-NEXT: ret i32 %y
614 }
615
616 define i32 @test45(i32 %a) nounwind {
617   %y = lshr exact i32 %a, 1
618   %z = lshr i32 %y, 4
619   ret i32 %z
620 ; CHECK-LABEL: @test45(
621 ; CHECK-NEXT: %y = lshr i32 %a, 5
622 ; CHECK-NEXT: ret i32 %y
623 }
624
625 define i32 @test46(i32 %a) {
626   %y = ashr exact i32 %a, 3
627   %z = shl i32 %y, 1
628   ret i32 %z
629 ; CHECK-LABEL: @test46(
630 ; CHECK-NEXT: %z = ashr exact i32 %a, 2
631 ; CHECK-NEXT: ret i32 %z
632 }
633
634 define i32 @test47(i32 %a) {
635   %y = lshr exact i32 %a, 3
636   %z = shl i32 %y, 1
637   ret i32 %z
638 ; CHECK-LABEL: @test47(
639 ; CHECK-NEXT: %z = lshr exact i32 %a, 2
640 ; CHECK-NEXT: ret i32 %z
641 }
642
643 define i32 @test48(i32 %x) {
644   %A = lshr exact i32 %x, 1
645   %B = shl i32 %A, 3
646   ret i32 %B
647 ; CHECK-LABEL: @test48(
648 ; CHECK-NEXT: %B = shl i32 %x, 2
649 ; CHECK-NEXT: ret i32 %B
650 }
651
652 define i32 @test49(i32 %x) {
653   %A = ashr exact i32 %x, 1
654   %B = shl i32 %A, 3
655   ret i32 %B
656 ; CHECK-LABEL: @test49(
657 ; CHECK-NEXT: %B = shl i32 %x, 2
658 ; CHECK-NEXT: ret i32 %B
659 }
660
661 define i32 @test50(i32 %x) {
662   %A = shl nsw i32 %x, 1
663   %B = ashr i32 %A, 3
664   ret i32 %B
665 ; CHECK-LABEL: @test50(
666 ; CHECK-NEXT: %B = ashr i32 %x, 2
667 ; CHECK-NEXT: ret i32 %B
668 }
669
670 define i32 @test51(i32 %x) {
671   %A = shl nuw i32 %x, 1
672   %B = lshr i32 %A, 3
673   ret i32 %B
674 ; CHECK-LABEL: @test51(
675 ; CHECK-NEXT: %B = lshr i32 %x, 2
676 ; CHECK-NEXT: ret i32 %B
677 }
678
679 define i32 @test52(i32 %x) {
680   %A = shl nsw i32 %x, 3
681   %B = ashr i32 %A, 1
682   ret i32 %B
683 ; CHECK-LABEL: @test52(
684 ; CHECK-NEXT: %B = shl nsw i32 %x, 2
685 ; CHECK-NEXT: ret i32 %B
686 }
687
688 define i32 @test53(i32 %x) {
689   %A = shl nuw i32 %x, 3
690   %B = lshr i32 %A, 1
691   ret i32 %B
692 ; CHECK-LABEL: @test53(
693 ; CHECK-NEXT: %B = shl nuw i32 %x, 2
694 ; CHECK-NEXT: ret i32 %B
695 }
696
697 define i32 @test54(i32 %x) {
698   %shr2 = lshr i32 %x, 1
699   %shl = shl i32 %shr2, 4
700   %and = and i32 %shl, 16
701   ret i32 %and
702 ; CHECK-LABEL: @test54(
703 ; CHECK: shl i32 %x, 3
704 }
705
706
707 define i32 @test55(i32 %x) {
708   %shr2 = lshr i32 %x, 1
709   %shl = shl i32 %shr2, 4
710   %or = or i32 %shl, 8
711   ret i32 %or
712 ; CHECK-LABEL: @test55(
713 ; CHECK: shl i32 %x, 3
714 }
715
716 define i32 @test56(i32 %x) {
717   %shr2 = lshr i32 %x, 1
718   %shl = shl i32 %shr2, 4
719   %or = or i32 %shl, 7
720   ret i32 %or
721 ; CHECK-LABEL: @test56(
722 ; CHECK: shl i32 %shr2, 4
723 }
724
725
726 define i32 @test57(i32 %x) {
727   %shr = lshr i32 %x, 1
728   %shl = shl i32 %shr, 4
729   %and = and i32 %shl, 16
730   ret i32 %and
731 ; CHECK-LABEL: @test57(
732 ; CHECK: shl i32 %x, 3
733 }
734
735 define i32 @test58(i32 %x) {
736   %shr = lshr i32 %x, 1
737   %shl = shl i32 %shr, 4
738   %or = or i32 %shl, 8
739   ret i32 %or
740 ; CHECK-LABEL: @test58(
741 ; CHECK: shl i32 %x, 3
742 }
743
744 define i32 @test59(i32 %x) {
745   %shr = ashr i32 %x, 1
746   %shl = shl i32 %shr, 4
747   %or = or i32 %shl, 7
748   ret i32 %or
749 ; CHECK-LABEL: @test59(
750 ; CHECK: %shl = shl i32 %shr1, 4
751 }
752
753
754 define i32 @test60(i32 %x) {
755   %shr = ashr i32 %x, 4
756   %shl = shl i32 %shr, 1
757   %or = or i32 %shl, 1
758   ret i32 %or
759 ; CHECK-LABEL: @test60(
760 ; CHECK: ashr i32 %x, 3
761 }
762
763
764 define i32 @test61(i32 %x) {
765   %shr = ashr i32 %x, 4
766   %shl = shl i32 %shr, 1
767   %or = or i32 %shl, 2
768   ret i32 %or
769 ; CHECK-LABEL: @test61(
770 ; CHECK: ashr i32 %x, 4
771 }
772
773 ; propagate "exact" trait
774 define i32 @test62(i32 %x) {
775   %shr = ashr exact i32 %x, 4
776   %shl = shl i32 %shr, 1
777   %or = or i32 %shl, 1
778   ret i32 %or
779 ; CHECK-LABEL: @test62(
780 ; CHECK: ashr exact i32 %x, 3
781 }
782
783 ; PR17026
784 ; CHECK-LABEL: @test63(
785 ; CHECK-NOT: sh
786 ; CHECK: ret
787 define void @test63(i128 %arg) {
788 bb:
789   br i1 undef, label %bb1, label %bb12
790
791 bb1:                                              ; preds = %bb11, %bb
792   br label %bb2
793
794 bb2:                                              ; preds = %bb7, %bb1
795   br i1 undef, label %bb3, label %bb7
796
797 bb3:                                              ; preds = %bb2
798   %tmp = lshr i128 %arg, 36893488147419103232
799   %tmp4 = shl i128 %tmp, 0
800   %tmp5 = or i128 %tmp4, undef
801   %tmp6 = trunc i128 %tmp5 to i16
802   br label %bb8
803
804 bb7:                                              ; preds = %bb2
805   br i1 undef, label %bb8, label %bb2
806
807 bb8:                                              ; preds = %bb7, %bb3
808   %tmp9 = phi i16 [ %tmp6, %bb3 ], [ undef, %bb7 ]
809   %tmp10 = icmp eq i16 %tmp9, 0
810   br i1 %tmp10, label %bb11, label %bb12
811
812 bb11:                                             ; preds = %bb8
813   br i1 undef, label %bb1, label %bb12
814
815 bb12:                                             ; preds = %bb11, %bb8, %bb
816   ret void
817 }
818
819 define i32 @test64(i32 %a) {
820 ; CHECK-LABEL: @test64(
821 ; CHECK-NEXT: ret i32 undef
822   %b = ashr i32 %a, 32  ; shift all bits out
823   ret i32 %b
824 }
825
826 define <4 x i32> @test64_splat_vector(<4 x i32> %a) {
827 ; CHECK-LABEL: @test64_splat_vector
828 ; CHECK-NEXT: ret <4 x i32> undef
829   %b = ashr <4 x i32> %a, <i32 32, i32 32, i32 32, i32 32>  ; shift all bits out
830   ret <4 x i32> %b
831 }
832
833 define <4 x i32> @test64_non_splat_vector(<4 x i32> %a) {
834 ; CHECK-LABEL: @test64_non_splat_vector
835 ; CHECK-NOT: ret <4 x i32> undef
836   %b = ashr <4 x i32> %a, <i32 32, i32 0, i32 1, i32 2>  ; shift all bits out
837   ret <4 x i32> %b
838 }
839
840 define <2 x i65> @test_65(<2 x i64> %t) {
841 ; CHECK-LABEL: @test_65
842   %a = zext <2 x i64> %t to <2 x i65>
843   %sext = shl <2 x i65> %a, <i65 33, i65 33>
844   %b = ashr <2 x i65> %sext, <i65 33, i65 33>
845   ret <2 x i65> %b
846 }