1 ; Tests to make sure elimination of casts is working correctly
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128-n8:16:32:64"
5 @inbuf = external global [32832 x i8] ; <[32832 x i8]*> [#uses=1]
7 define i32 @test1(i32 %A) {
8 %c1 = bitcast i32 %A to i32 ; <i32> [#uses=1]
9 %c2 = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
14 define i64 @test2(i8 %A) {
15 %c1 = zext i8 %A to i16 ; <i16> [#uses=1]
16 %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
17 %Ret = zext i32 %c2 to i64 ; <i64> [#uses=1]
19 ; CHECK: %Ret = zext i8 %A to i64
23 ; This function should just use bitwise AND
24 define i64 @test3(i64 %A) {
25 %c1 = trunc i64 %A to i8 ; <i8> [#uses=1]
26 %c2 = zext i8 %c1 to i64 ; <i64> [#uses=1]
28 ; CHECK: %c2 = and i64 %A, 255
32 define i32 @test4(i32 %A, i32 %B) {
33 %COND = icmp slt i32 %A, %B ; <i1> [#uses=1]
34 ; Booleans are unsigned integrals
35 %c = zext i1 %COND to i8 ; <i8> [#uses=1]
36 ; for the cast elim purpose
37 %result = zext i8 %c to i32 ; <i32> [#uses=1]
39 ; CHECK: %COND = icmp slt i32 %A, %B
40 ; CHECK: %result = zext i1 %COND to i32
41 ; CHECK: ret i32 %result
44 define i32 @test5(i1 %B) {
45 ; This cast should get folded into
46 %c = zext i1 %B to i8 ; <i8> [#uses=1]
48 %result = zext i8 %c to i32 ; <i32> [#uses=1]
50 ; CHECK: %result = zext i1 %B to i32
51 ; CHECK: ret i32 %result
54 define i32 @test6(i64 %A) {
55 %c1 = trunc i64 %A to i32 ; <i32> [#uses=1]
56 %res = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
58 ; CHECK: trunc i64 %A to i32
62 define i64 @test7(i1 %A) {
63 %c1 = zext i1 %A to i32 ; <i32> [#uses=1]
64 %res = sext i32 %c1 to i64 ; <i64> [#uses=1]
66 ; CHECK: %res = zext i1 %A to i64
70 define i64 @test8(i8 %A) {
71 %c1 = sext i8 %A to i64 ; <i64> [#uses=1]
72 %res = bitcast i64 %c1 to i64 ; <i64> [#uses=1]
74 ; CHECK: = sext i8 %A to i64
78 define i16 @test9(i16 %A) {
79 %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
80 %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
85 define i16 @test10(i16 %A) {
86 %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
87 %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
92 declare void @varargs(i32, ...)
94 define void @test11(i32* %P) {
95 %c = bitcast i32* %P to i16* ; <i16*> [#uses=1]
96 call void (i32, ...)* @varargs( i32 5, i16* %c )
98 ; CHECK: call void (i32, ...)* @varargs(i32 5, i32* %P)
102 define i32* @test12() {
103 %p = malloc [4 x i8] ; <[4 x i8]*> [#uses=1]
104 %c = bitcast [4 x i8]* %p to i32* ; <i32*> [#uses=1]
106 ; CHECK: %malloccall = tail call i8* @malloc(i32 4)
110 define i8* @test13(i64 %A) {
111 %c = getelementptr [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A ; <i8*> [#uses=1]
113 ; CHECK: %c = getelementptr [32832 x i8]* @inbuf, i64 0, i64 %A
117 define i1 @test14(i8 %A) {
118 %c = bitcast i8 %A to i8 ; <i8> [#uses=1]
119 %X = icmp ult i8 %c, -128 ; <i1> [#uses=1]
121 ; CHECK: %X = icmp sgt i8 %A, -1
126 ; This just won't occur when there's no difference between ubyte and sbyte
127 ;bool %test15(ubyte %A) {
128 ; %c = cast ubyte %A to sbyte
129 ; %X = setlt sbyte %c, 0 ; setgt %A, 127
133 define i1 @test16(i32* %P) {
134 %c = icmp ne i32* %P, null ; <i1> [#uses=1]
136 ; CHECK: %c = icmp ne i32* %P, null
140 define i16 @test17(i1 %tmp3) {
141 %c = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
142 %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
144 ; CHECK: %t86 = zext i1 %tmp3 to i16
145 ; CHECK: ret i16 %t86
148 define i16 @test18(i8 %tmp3) {
149 %c = sext i8 %tmp3 to i32 ; <i32> [#uses=1]
150 %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
152 ; CHECK: %t86 = sext i8 %tmp3 to i16
153 ; CHECK: ret i16 %t86
156 define i1 @test19(i32 %X) {
157 %c = sext i32 %X to i64 ; <i64> [#uses=1]
158 %Z = icmp slt i64 %c, 12345 ; <i1> [#uses=1]
160 ; CHECK: %Z = icmp slt i32 %X, 12345
164 define i1 @test20(i1 %B) {
165 %c = zext i1 %B to i32 ; <i32> [#uses=1]
166 %D = icmp slt i32 %c, -1 ; <i1> [#uses=1]
169 ; CHECK: ret i1 false
172 define i32 @test21(i32 %X) {
173 %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
174 ;; sext -> zext -> and -> nop
175 %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
176 %RV = and i32 %c2, 255 ; <i32> [#uses=1]
178 ; CHECK: %c21 = and i32 %X, 255
179 ; CHECK: ret i32 %c21
182 define i32 @test22(i32 %X) {
183 %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
184 ;; sext -> zext -> and -> nop
185 %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
186 %RV = shl i32 %c2, 24 ; <i32> [#uses=1]
188 ; CHECK: shl i32 %X, 24
189 ; CHECK-NEXT: ret i32
192 define i32 @test23(i32 %X) {
193 ;; Turn into an AND even though X
194 %c1 = trunc i32 %X to i16 ; <i16> [#uses=1]
196 %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
198 ; CHECK: %c2 = and i32 %X, 65535
202 define i1 @test24(i1 %C) {
203 %X = select i1 %C, i32 14, i32 1234 ; <i32> [#uses=1]
204 ;; Fold cast into select
205 %c = icmp ne i32 %X, 0 ; <i1> [#uses=1]
210 define void @test25(i32** %P) {
211 %c = bitcast i32** %P to float** ; <float**> [#uses=1]
212 ;; Fold cast into null
213 store float* null, float** %c
215 ; CHECK: store i32* null, i32** %P
219 define i32 @test26(float %F) {
220 ;; no need to cast from float->double.
221 %c = fpext float %F to double ; <double> [#uses=1]
222 %D = fptosi double %c to i32 ; <i32> [#uses=1]
224 ; CHECK: %D = fptosi float %F to i32
228 define [4 x float]* @test27([9 x [4 x float]]* %A) {
229 %c = bitcast [9 x [4 x float]]* %A to [4 x float]* ; <[4 x float]*> [#uses=1]
231 ; CHECK: %c = getelementptr inbounds [9 x [4 x float]]* %A, i64 0, i64 0
232 ; CHECK: ret [4 x float]* %c
235 define float* @test28([4 x float]* %A) {
236 %c = bitcast [4 x float]* %A to float* ; <float*> [#uses=1]
238 ; CHECK: %c = getelementptr inbounds [4 x float]* %A, i64 0, i64 0
239 ; CHECK: ret float* %c
242 define i32 @test29(i32 %c1, i32 %c2) {
243 %tmp1 = trunc i32 %c1 to i8 ; <i8> [#uses=1]
244 %tmp4.mask = trunc i32 %c2 to i8 ; <i8> [#uses=1]
245 %tmp = or i8 %tmp4.mask, %tmp1 ; <i8> [#uses=1]
246 %tmp10 = zext i8 %tmp to i32 ; <i32> [#uses=1]
248 ; CHECK: %tmp2 = or i32 %c2, %c1
249 ; CHECK: %tmp10 = and i32 %tmp2, 255
250 ; CHECK: ret i32 %tmp10
253 define i32 @test30(i32 %c1) {
254 %c2 = trunc i32 %c1 to i8 ; <i8> [#uses=1]
255 %c3 = xor i8 %c2, 1 ; <i8> [#uses=1]
256 %c4 = zext i8 %c3 to i32 ; <i32> [#uses=1]
258 ; CHECK: %c3 = and i32 %c1, 255
259 ; CHECK: %c4 = xor i32 %c3, 1
263 define i1 @test31(i64 %A) {
264 %B = trunc i64 %A to i32 ; <i32> [#uses=1]
265 %C = and i32 %B, 42 ; <i32> [#uses=1]
266 %D = icmp eq i32 %C, 10 ; <i1> [#uses=1]
268 ; CHECK: %C1 = and i64 %A, 42
269 ; CHECK: %D = icmp eq i64 %C1, 10
273 define void @test32(double** %tmp) {
274 %tmp8 = malloc [16 x i8] ; <[16 x i8]*> [#uses=1]
275 %tmp8.upgrd.1 = bitcast [16 x i8]* %tmp8 to double* ; <double*> [#uses=1]
276 store double* %tmp8.upgrd.1, double** %tmp
278 ; CHECK: %malloccall = tail call i8* @malloc(i32 16)
279 ; CHECK: %tmp8.upgrd.1 = bitcast i8* %malloccall to double*
280 ; CHECK: store double* %tmp8.upgrd.1, double** %tmp
284 define i32 @test33(i32 %c1) {
285 %x = bitcast i32 %c1 to float ; <float> [#uses=1]
286 %y = bitcast float %x to i32 ; <i32> [#uses=1]
291 define i16 @test34(i16 %a) {
292 %c1 = zext i16 %a to i32 ; <i32> [#uses=1]
293 %tmp21 = lshr i32 %c1, 8 ; <i32> [#uses=1]
294 %c2 = trunc i32 %tmp21 to i16 ; <i16> [#uses=1]
296 ; CHECK: %tmp21 = lshr i16 %a, 8
297 ; CHECK: ret i16 %tmp21
300 define i16 @test35(i16 %a) {
301 %c1 = bitcast i16 %a to i16 ; <i16> [#uses=1]
302 %tmp2 = lshr i16 %c1, 8 ; <i16> [#uses=1]
303 %c2 = bitcast i16 %tmp2 to i16 ; <i16> [#uses=1]
305 ; CHECK: %tmp2 = lshr i16 %a, 8
306 ; CHECK: ret i16 %tmp2
309 ; icmp sgt i32 %a, -1
311 define i1 @test36(i32 %a) {
313 %c = trunc i32 %b to i8
314 %d = icmp eq i8 %c, 0
316 ; CHECK: %d = icmp sgt i32 %a, -1
321 define i1 @test37(i32 %a) {
324 %d = trunc i32 %c to i8
325 %e = icmp eq i8 %d, 11
327 ; CHECK: ret i1 false
330 define i64 @test38(i32 %a) {
331 %1 = icmp eq i32 %a, -2
332 %2 = zext i1 %1 to i8
334 %4 = zext i8 %3 to i64
336 ; CHECK: %1 = icmp ne i32 %a, -2
337 ; CHECK: %2 = zext i1 %1 to i64
341 define i16 @test39(i16 %a) {
342 %tmp = zext i16 %a to i32
343 %tmp21 = lshr i32 %tmp, 8
344 %tmp5 = shl i32 %tmp, 8
345 %tmp.upgrd.32 = or i32 %tmp21, %tmp5
346 %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
349 ; CHECK: %tmp.upgrd.32 = call i16 @llvm.bswap.i16(i16 %a)
350 ; CHECK: ret i16 %tmp.upgrd.32
353 define i16 @test40(i16 %a) {
354 %tmp = zext i16 %a to i32
355 %tmp21 = lshr i32 %tmp, 9
356 %tmp5 = shl i32 %tmp, 8
357 %tmp.upgrd.32 = or i32 %tmp21, %tmp5
358 %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
361 ; CHECK: %tmp21 = lshr i16 %a, 9
362 ; CHECK: %tmp5 = shl i16 %a, 8
363 ; CHECK: %tmp.upgrd.32 = or i16 %tmp21, %tmp5
364 ; CHECK: ret i16 %tmp.upgrd.32
368 define i32* @test41(i32* %tmp1) {
369 %tmp64 = bitcast i32* %tmp1 to { i32 }*
370 %tmp65 = getelementptr { i32 }* %tmp64, i32 0, i32 0
373 ; CHECK: ret i32* %tmp1
376 define i32 @test42(i32 %X) {
377 %Y = trunc i32 %X to i8 ; <i8> [#uses=1]
378 %Z = zext i8 %Y to i32 ; <i32> [#uses=1]
381 ; CHECK: %Z = and i32 %X, 255
385 define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly {
386 %A = zext i8 %on_off to i32
388 %C = sext i32 %B to i64
389 ret i64 %C ;; Should be (add (zext i8 -> i64), -1)
391 ; CHECK-NEXT: %A = zext i8 %on_off to i64
392 ; CHECK-NEXT: %B = add i64 %A, -1
393 ; CHECK-NEXT: ret i64 %B
396 define i64 @test44(i8 %T) {
397 %A = zext i8 %T to i16
399 %C = zext i16 %B to i64
402 ; CHECK-NEXT: %A = zext i8 %T to i64
403 ; CHECK-NEXT: %B = or i64 %A, 1234
404 ; CHECK-NEXT: ret i64 %B
407 define i64 @test45(i8 %A, i64 %Q) {
408 %D = trunc i64 %Q to i32 ;; should be removed
409 %B = sext i8 %A to i32
411 %E = zext i32 %C to i64
414 ; CHECK-NEXT: %B = sext i8 %A to i64
415 ; CHECK-NEXT: %C = or i64 %B, %Q
416 ; CHECK-NEXT: %E = and i64 %C, 4294967295
417 ; CHECK-NEXT: ret i64 %E
421 define i64 @test46(i64 %A) {
422 %B = trunc i64 %A to i32
425 %E = zext i32 %D to i64
428 ; CHECK-NEXT: %C = shl i64 %A, 8
429 ; CHECK-NEXT: %D = and i64 %C, 10752
430 ; CHECK-NEXT: ret i64 %D
433 define i64 @test47(i8 %A) {
434 %B = sext i8 %A to i32
436 %E = zext i32 %C to i64
439 ; CHECK-NEXT: %B = sext i8 %A to i64
440 ; CHECK-NEXT: %C = and i64 %B, 4294967253
441 ; CHECK-NEXT: %E = or i64 %C, 42
442 ; CHECK-NEXT: ret i64 %E
445 define i64 @test48(i8 %A, i8 %a) {
446 %b = zext i8 %a to i32
447 %B = zext i8 %A to i32
450 %E = zext i32 %D to i64
453 ; CHECK-NEXT: %b = zext i8 %a to i64
454 ; CHECK-NEXT: %B = zext i8 %A to i64
455 ; CHECK-NEXT: %C = shl i64 %B, 8
456 ; CHECK-NEXT: %D = or i64 %C, %b
457 ; CHECK-NEXT: ret i64 %D
460 define i64 @test49(i64 %A) {
461 %B = trunc i64 %A to i32
463 %D = sext i32 %C to i64
466 ; CHECK-NEXT: %C = shl i64 %A, 32
467 ; CHECK-NEXT: ashr i64 %C, 32
468 ; CHECK-NEXT: %D = or i64 {{.*}}, 1
469 ; CHECK-NEXT: ret i64 %D
472 define i64 @test50(i64 %A) {
474 %B = trunc i64 %a to i32
476 %E = sext i32 %D to i64
479 ; CHECK-NEXT: shl i64 %A, 30
480 ; CHECK-NEXT: add i64 {{.*}}, -4294967296
481 ; CHECK-NEXT: %E = ashr i64 {{.*}}, 32
482 ; CHECK-NEXT: ret i64 %E
485 define i64 @test51(i64 %A, i1 %cond) {
486 %B = trunc i64 %A to i32
489 %E = select i1 %cond, i32 %C, i32 %D
490 %F = sext i32 %E to i64
494 ; FIXME: disabled, see PR5997
495 ; HECK-NEXT: %C = and i64 %A, 4294967294
496 ; HECK-NEXT: %D = or i64 %A, 1
497 ; HECK-NEXT: %E = select i1 %cond, i64 %C, i64 %D
498 ; HECK-NEXT: %sext = shl i64 %E, 32
499 ; HECK-NEXT: %F = ashr i64 %sext, 32
500 ; HECK-NEXT: ret i64 %F
503 define i32 @test52(i64 %A) {
504 %B = trunc i64 %A to i16
505 %C = or i16 %B, -32574
506 %D = and i16 %C, -25350
507 %E = zext i16 %D to i32
510 ; CHECK-NEXT: %B = trunc i64 %A to i32
511 ; CHECK-NEXT: %C = and i32 %B, 7224
512 ; CHECK-NEXT: %D = or i32 %C, 32962
513 ; CHECK-NEXT: ret i32 %D
516 define i64 @test53(i32 %A) {
517 %B = trunc i32 %A to i16
518 %C = or i16 %B, -32574
519 %D = and i16 %C, -25350
520 %E = zext i16 %D to i64
523 ; CHECK-NEXT: %B = zext i32 %A to i64
524 ; CHECK-NEXT: %C = and i64 %B, 7224
525 ; CHECK-NEXT: %D = or i64 %C, 32962
526 ; CHECK-NEXT: ret i64 %D
529 define i32 @test54(i64 %A) {
530 %B = trunc i64 %A to i16
531 %C = or i16 %B, -32574
532 %D = and i16 %C, -25350
533 %E = sext i16 %D to i32
536 ; CHECK-NEXT: %B = trunc i64 %A to i32
537 ; CHECK-NEXT: %C = and i32 %B, 7224
538 ; CHECK-NEXT: %D = or i32 %C, -32574
539 ; CHECK-NEXT: ret i32 %D
542 define i64 @test55(i32 %A) {
543 %B = trunc i32 %A to i16
544 %C = or i16 %B, -32574
545 %D = and i16 %C, -25350
546 %E = sext i16 %D to i64
549 ; CHECK-NEXT: %B = zext i32 %A to i64
550 ; CHECK-NEXT: %C = and i64 %B, 7224
551 ; CHECK-NEXT: %D = or i64 %C, -32574
552 ; CHECK-NEXT: ret i64 %D
555 define i64 @test56(i16 %A) nounwind {
556 %tmp353 = sext i16 %A to i32
557 %tmp354 = lshr i32 %tmp353, 5
558 %tmp355 = zext i32 %tmp354 to i64
561 ; CHECK-NEXT: %tmp353 = sext i16 %A to i64
562 ; CHECK-NEXT: %tmp354 = lshr i64 %tmp353, 5
563 ; CHECK-NEXT: %tmp355 = and i64 %tmp354, 134217727
564 ; CHECK-NEXT: ret i64 %tmp355
567 define i64 @test57(i64 %A) nounwind {
568 %B = trunc i64 %A to i32
570 %E = zext i32 %C to i64
573 ; CHECK-NEXT: %C = lshr i64 %A, 8
574 ; CHECK-NEXT: %E = and i64 %C, 16777215
575 ; CHECK-NEXT: ret i64 %E
578 define i64 @test58(i64 %A) nounwind {
579 %B = trunc i64 %A to i32
582 %E = zext i32 %D to i64
586 ; CHECK-NEXT: %C = lshr i64 %A, 8
587 ; CHECK-NEXT: %D = and i64 %C, 16777087
588 ; CHECK-NEXT: %E = or i64 %D, 128
589 ; CHECK-NEXT: ret i64 %E
592 define i64 @test59(i8 %A, i8 %B) nounwind {
593 %C = zext i8 %A to i32
596 %F = zext i8 %B to i32
599 %I = zext i32 %H to i64
602 ; CHECK-NEXT: %C = zext i8 %A to i64
604 ; CHECK: %F = zext i8 %B to i64
609 define <3 x i32> @test60(<4 x i32> %call4) nounwind {
610 %tmp11 = bitcast <4 x i32> %call4 to i128
611 %tmp9 = trunc i128 %tmp11 to i96
612 %tmp10 = bitcast i96 %tmp9 to <3 x i32>
616 ; CHECK-NEXT: shufflevector
620 define <4 x i32> @test61(<3 x i32> %call4) nounwind {
621 %tmp11 = bitcast <3 x i32> %call4 to i96
622 %tmp9 = zext i96 %tmp11 to i128
623 %tmp10 = bitcast i128 %tmp9 to <4 x i32>
626 ; CHECK-NEXT: shufflevector
630 define <4 x i32> @test62(<3 x float> %call4) nounwind {
631 %tmp11 = bitcast <3 x float> %call4 to i96
632 %tmp9 = zext i96 %tmp11 to i128
633 %tmp10 = bitcast i128 %tmp9 to <4 x i32>
636 ; CHECK-NEXT: bitcast
637 ; CHECK-NEXT: shufflevector
641 ; PR7311 - Don't create invalid IR on scalar->vector cast.
642 define <2 x float> @test63(i64 %tmp8) nounwind {
644 %a = bitcast i64 %tmp8 to <2 x i32>
645 %vcvt.i = uitofp <2 x i32> %a to <2 x float>
646 ret <2 x float> %vcvt.i