[InstCombine] Change LLVM To canonicalize toward the value type being
[oota-llvm.git] / test / Transforms / InstCombine / cast.ll
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-p1:32:32:32-p2:64:64:64-p3: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"
4
5 @inbuf = external global [32832 x i8]           ; <[32832 x i8]*> [#uses=1]
6
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]
10         ret i32 %c2
11 ; CHECK: ret i32 %A
12 }
13
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]
18         ret i64 %Ret
19 ; CHECK: %Ret = zext i8 %A to i64
20 ; CHECK: ret i64 %Ret
21 }
22
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]
27         ret i64 %c2
28 ; CHECK: %c2 = and i64 %A, 255
29 ; CHECK: ret i64 %c2
30 }
31
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]
38         ret i32 %result
39 ; CHECK: %COND = icmp slt i32 %A, %B
40 ; CHECK: %result = zext i1 %COND to i32
41 ; CHECK: ret i32 %result
42 }
43
44 define i32 @test5(i1 %B) {
45         ; This cast should get folded into
46         %c = zext i1 %B to i8           ; <i8> [#uses=1]
47         ; this cast        
48         %result = zext i8 %c to i32             ; <i32> [#uses=1]
49         ret i32 %result
50 ; CHECK: %result = zext i1 %B to i32
51 ; CHECK: ret i32 %result
52 }
53
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]
57         ret i32 %res
58 ; CHECK:  trunc i64 %A to i32
59 ; CHECK-NEXT: ret i32
60 }
61
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]
65         ret i64 %res
66 ; CHECK: %res = zext i1 %A to i64
67 ; CHECK: ret i64 %res
68 }
69
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]
73         ret i64 %res
74 ; CHECK: = sext i8 %A to i64
75 ; CHECK-NEXT: ret i64
76 }
77
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]
81         ret i16 %c2
82 ; CHECK: ret i16 %A
83 }
84
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]
88         ret i16 %c2
89 ; CHECK: ret i16 %A
90 }
91
92 declare void @varargs(i32, ...)
93
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 )
97         ret void
98 ; CHECK: call void (i32, ...)* @varargs(i32 5, i32* %P)
99 ; CHECK: ret void
100 }
101
102 define i8* @test13(i64 %A) {
103         %c = getelementptr [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A             ; <i8*> [#uses=1]
104         ret i8* %c
105 ; CHECK: %c = getelementptr [32832 x i8]* @inbuf, i64 0, i64 %A
106 ; CHECK: ret i8* %c
107 }
108
109 define i1 @test14(i8 %A) {
110         %c = bitcast i8 %A to i8                ; <i8> [#uses=1]
111         %X = icmp ult i8 %c, -128               ; <i1> [#uses=1]
112         ret i1 %X
113 ; CHECK: %X = icmp sgt i8 %A, -1
114 ; CHECK: ret i1 %X
115 }
116
117
118 ; This just won't occur when there's no difference between ubyte and sbyte
119 ;bool %test15(ubyte %A) {
120 ;        %c = cast ubyte %A to sbyte
121 ;        %X = setlt sbyte %c, 0   ; setgt %A, 127
122 ;        ret bool %X
123 ;}
124
125 define i1 @test16(i32* %P) {
126         %c = icmp ne i32* %P, null              ; <i1> [#uses=1]
127         ret i1 %c
128 ; CHECK: %c = icmp ne i32* %P, null
129 ; CHECK: ret i1 %c
130 }
131
132 define i16 @test17(i1 %tmp3) {
133         %c = zext i1 %tmp3 to i32               ; <i32> [#uses=1]
134         %t86 = trunc i32 %c to i16              ; <i16> [#uses=1]
135         ret i16 %t86
136 ; CHECK: %t86 = zext i1 %tmp3 to i16
137 ; CHECK: ret i16 %t86
138 }
139
140 define i16 @test18(i8 %tmp3) {
141         %c = sext i8 %tmp3 to i32               ; <i32> [#uses=1]
142         %t86 = trunc i32 %c to i16              ; <i16> [#uses=1]
143         ret i16 %t86
144 ; CHECK: %t86 = sext i8 %tmp3 to i16
145 ; CHECK: ret i16 %t86
146 }
147
148 define i1 @test19(i32 %X) {
149         %c = sext i32 %X to i64         ; <i64> [#uses=1]
150         %Z = icmp slt i64 %c, 12345             ; <i1> [#uses=1]
151         ret i1 %Z
152 ; CHECK: %Z = icmp slt i32 %X, 12345
153 ; CHECK: ret i1 %Z
154 }
155
156 define i1 @test20(i1 %B) {
157         %c = zext i1 %B to i32          ; <i32> [#uses=1]
158         %D = icmp slt i32 %c, -1                ; <i1> [#uses=1]
159         ;; false
160         ret i1 %D
161 ; CHECK: ret i1 false
162 }
163
164 define i32 @test21(i32 %X) {
165         %c1 = trunc i32 %X to i8                ; <i8> [#uses=1]
166         ;; sext -> zext -> and -> nop
167         %c2 = sext i8 %c1 to i32                ; <i32> [#uses=1]
168         %RV = and i32 %c2, 255          ; <i32> [#uses=1]
169         ret i32 %RV
170 ; CHECK: %c21 = and i32 %X, 255
171 ; CHECK: ret i32 %c21
172 }
173
174 define i32 @test22(i32 %X) {
175         %c1 = trunc i32 %X to i8                ; <i8> [#uses=1]
176         ;; sext -> zext -> and -> nop
177         %c2 = sext i8 %c1 to i32                ; <i32> [#uses=1]
178         %RV = shl i32 %c2, 24           ; <i32> [#uses=1]
179         ret i32 %RV
180 ; CHECK: shl i32 %X, 24
181 ; CHECK-NEXT: ret i32
182 }
183
184 define i32 @test23(i32 %X) {
185         ;; Turn into an AND even though X
186         %c1 = trunc i32 %X to i16               ; <i16> [#uses=1]
187         ;; and Z are signed.
188         %c2 = zext i16 %c1 to i32               ; <i32> [#uses=1]
189         ret i32 %c2
190 ; CHECK: %c2 = and i32 %X, 65535
191 ; CHECK: ret i32 %c2
192 }
193
194 define i1 @test24(i1 %C) {
195         %X = select i1 %C, i32 14, i32 1234             ; <i32> [#uses=1]
196         ;; Fold cast into select
197         %c = icmp ne i32 %X, 0          ; <i1> [#uses=1]
198         ret i1 %c
199 ; CHECK: ret i1 true
200 }
201
202 define i32 @test26(float %F) {
203         ;; no need to cast from float->double.
204         %c = fpext float %F to double           ; <double> [#uses=1]
205         %D = fptosi double %c to i32            ; <i32> [#uses=1]
206         ret i32 %D
207 ; CHECK: %D = fptosi float %F to i32
208 ; CHECK: ret i32 %D
209 }
210
211 define [4 x float]* @test27([9 x [4 x float]]* %A) {
212         %c = bitcast [9 x [4 x float]]* %A to [4 x float]*              ; <[4 x float]*> [#uses=1]
213         ret [4 x float]* %c
214 ; CHECK: %c = getelementptr inbounds [9 x [4 x float]]* %A, i64 0, i64 0
215 ; CHECK: ret [4 x float]* %c
216 }
217
218 define float* @test28([4 x float]* %A) {
219         %c = bitcast [4 x float]* %A to float*          ; <float*> [#uses=1]
220         ret float* %c
221 ; CHECK: %c = getelementptr inbounds [4 x float]* %A, i64 0, i64 0
222 ; CHECK: ret float* %c
223 }
224
225 define i32 @test29(i32 %c1, i32 %c2) {
226         %tmp1 = trunc i32 %c1 to i8             ; <i8> [#uses=1]
227         %tmp4.mask = trunc i32 %c2 to i8                ; <i8> [#uses=1]
228         %tmp = or i8 %tmp4.mask, %tmp1          ; <i8> [#uses=1]
229         %tmp10 = zext i8 %tmp to i32            ; <i32> [#uses=1]
230         ret i32 %tmp10
231 ; CHECK: %tmp2 = or i32 %c2, %c1
232 ; CHECK: %tmp10 = and i32 %tmp2, 255
233 ; CHECK: ret i32 %tmp10
234 }
235
236 define i32 @test30(i32 %c1) {
237         %c2 = trunc i32 %c1 to i8               ; <i8> [#uses=1]
238         %c3 = xor i8 %c2, 1             ; <i8> [#uses=1]
239         %c4 = zext i8 %c3 to i32                ; <i32> [#uses=1]
240         ret i32 %c4
241 ; CHECK: %c3 = and i32 %c1, 255
242 ; CHECK: %c4 = xor i32 %c3, 1
243 ; CHECK: ret i32 %c4
244 }
245
246 define i1 @test31(i64 %A) {
247         %B = trunc i64 %A to i32                ; <i32> [#uses=1]
248         %C = and i32 %B, 42             ; <i32> [#uses=1]
249         %D = icmp eq i32 %C, 10         ; <i1> [#uses=1]
250         ret i1 %D
251 ; CHECK: %C = and i64 %A, 42
252 ; CHECK: %D = icmp eq i64 %C, 10
253 ; CHECK: ret i1 %D
254 }
255
256 define i32 @test33(i32 %c1) {
257         %x = bitcast i32 %c1 to float           ; <float> [#uses=1]
258         %y = bitcast float %x to i32            ; <i32> [#uses=1]
259         ret i32 %y
260 ; CHECK: ret i32 %c1
261 }
262
263 define i16 @test34(i16 %a) {
264         %c1 = zext i16 %a to i32                ; <i32> [#uses=1]
265         %tmp21 = lshr i32 %c1, 8                ; <i32> [#uses=1]
266         %c2 = trunc i32 %tmp21 to i16           ; <i16> [#uses=1]
267         ret i16 %c2
268 ; CHECK: %tmp21 = lshr i16 %a, 8
269 ; CHECK: ret i16 %tmp21
270 }
271
272 define i16 @test35(i16 %a) {
273         %c1 = bitcast i16 %a to i16             ; <i16> [#uses=1]
274         %tmp2 = lshr i16 %c1, 8         ; <i16> [#uses=1]
275         %c2 = bitcast i16 %tmp2 to i16          ; <i16> [#uses=1]
276         ret i16 %c2
277 ; CHECK: %tmp2 = lshr i16 %a, 8
278 ; CHECK: ret i16 %tmp2
279 }
280
281 ; icmp sgt i32 %a, -1
282 ; rdar://6480391
283 define i1 @test36(i32 %a) {
284         %b = lshr i32 %a, 31
285         %c = trunc i32 %b to i8
286         %d = icmp eq i8 %c, 0
287         ret i1 %d
288 ; CHECK: %d = icmp sgt i32 %a, -1
289 ; CHECK: ret i1 %d
290 }
291
292 ; ret i1 false
293 define i1 @test37(i32 %a) {
294         %b = lshr i32 %a, 31
295         %c = or i32 %b, 512
296         %d = trunc i32 %c to i8
297         %e = icmp eq i8 %d, 11
298         ret i1 %e
299 ; CHECK: ret i1 false
300 }
301
302 define i64 @test38(i32 %a) {
303         %1 = icmp eq i32 %a, -2
304         %2 = zext i1 %1 to i8
305         %3 = xor i8 %2, 1
306         %4 = zext i8 %3 to i64
307         ret i64 %4
308 ; CHECK: %1 = icmp ne i32 %a, -2
309 ; CHECK: %2 = zext i1 %1 to i64
310 ; CHECK: ret i64 %2
311 }
312
313 define i16 @test39(i16 %a) {
314         %tmp = zext i16 %a to i32
315         %tmp21 = lshr i32 %tmp, 8
316         %tmp5 = shl i32 %tmp, 8
317         %tmp.upgrd.32 = or i32 %tmp21, %tmp5
318         %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
319         ret i16 %tmp.upgrd.3
320 ; CHECK-LABEL: @test39(
321 ; CHECK: %tmp.upgrd.32 = call i16 @llvm.bswap.i16(i16 %a)
322 ; CHECK: ret i16 %tmp.upgrd.32
323 }
324
325 define i16 @test40(i16 %a) {
326         %tmp = zext i16 %a to i32
327         %tmp21 = lshr i32 %tmp, 9
328         %tmp5 = shl i32 %tmp, 8
329         %tmp.upgrd.32 = or i32 %tmp21, %tmp5
330         %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
331         ret i16 %tmp.upgrd.3
332 ; CHECK-LABEL: @test40(
333 ; CHECK: %tmp21 = lshr i16 %a, 9
334 ; CHECK: %tmp5 = shl i16 %a, 8
335 ; CHECK: %tmp.upgrd.32 = or i16 %tmp21, %tmp5
336 ; CHECK: ret i16 %tmp.upgrd.32
337 }
338
339 ; PR1263
340 define i32* @test41(i32* %tmp1) {
341         %tmp64 = bitcast i32* %tmp1 to { i32 }*
342         %tmp65 = getelementptr { i32 }* %tmp64, i32 0, i32 0
343         ret i32* %tmp65
344 ; CHECK-LABEL: @test41(
345 ; CHECK: ret i32* %tmp1
346 }
347
348 define i32 addrspace(1)* @test41_addrspacecast_smaller(i32* %tmp1) {
349   %tmp64 = addrspacecast i32* %tmp1 to { i32 } addrspace(1)*
350   %tmp65 = getelementptr { i32 } addrspace(1)* %tmp64, i32 0, i32 0
351   ret i32 addrspace(1)* %tmp65
352 ; CHECK-LABEL: @test41_addrspacecast_smaller(
353 ; CHECK: addrspacecast i32* %tmp1 to i32 addrspace(1)*
354 ; CHECK-NEXT: ret i32 addrspace(1)*
355 }
356
357 define i32* @test41_addrspacecast_larger(i32 addrspace(1)* %tmp1) {
358   %tmp64 = addrspacecast i32 addrspace(1)* %tmp1 to { i32 }*
359   %tmp65 = getelementptr { i32 }* %tmp64, i32 0, i32 0
360   ret i32* %tmp65
361 ; CHECK-LABEL: @test41_addrspacecast_larger(
362 ; CHECK: addrspacecast i32 addrspace(1)* %tmp1 to i32*
363 ; CHECK-NEXT: ret i32*
364 }
365
366 define i32 @test42(i32 %X) {
367         %Y = trunc i32 %X to i8         ; <i8> [#uses=1]
368         %Z = zext i8 %Y to i32          ; <i32> [#uses=1]
369         ret i32 %Z
370 ; CHECK-LABEL: @test42(
371 ; CHECK: %Z = and i32 %X, 255
372 }
373
374 ; rdar://6598839
375 define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly {
376         %A = zext i8 %on_off to i32
377         %B = add i32 %A, -1
378         %C = sext i32 %B to i64
379         ret i64 %C  ;; Should be (add (zext i8 -> i64), -1)
380 ; CHECK-LABEL: @test43(
381 ; CHECK-NEXT: %A = zext i8 %on_off to i64
382 ; CHECK-NEXT: %B = add nsw i64 %A, -1
383 ; CHECK-NEXT: ret i64 %B
384 }
385
386 define i64 @test44(i8 %T) {
387  %A = zext i8 %T to i16
388  %B = or i16 %A, 1234
389  %C = zext i16 %B to i64
390  ret i64 %C
391 ; CHECK-LABEL: @test44(
392 ; CHECK-NEXT: %A = zext i8 %T to i64
393 ; CHECK-NEXT: %B = or i64 %A, 1234
394 ; CHECK-NEXT: ret i64 %B
395 }
396
397 define i64 @test45(i8 %A, i64 %Q) {
398  %D = trunc i64 %Q to i32  ;; should be removed
399  %B = sext i8 %A to i32
400  %C = or i32 %B, %D
401  %E = zext i32 %C to i64 
402  ret i64 %E
403 ; CHECK-LABEL: @test45(
404 ; CHECK-NEXT: %B = sext i8 %A to i64
405 ; CHECK-NEXT: %C = or i64 %B, %Q
406 ; CHECK-NEXT: %E = and i64 %C, 4294967295
407 ; CHECK-NEXT: ret i64 %E
408 }
409
410
411 define i64 @test46(i64 %A) {
412  %B = trunc i64 %A to i32
413  %C = and i32 %B, 42
414  %D = shl i32 %C, 8
415  %E = zext i32 %D to i64 
416  ret i64 %E
417 ; CHECK-LABEL: @test46(
418 ; CHECK-NEXT: %C = shl i64 %A, 8
419 ; CHECK-NEXT: %D = and i64 %C, 10752
420 ; CHECK-NEXT: ret i64 %D
421 }
422
423 define i64 @test47(i8 %A) {
424  %B = sext i8 %A to i32
425  %C = or i32 %B, 42
426  %E = zext i32 %C to i64 
427  ret i64 %E
428 ; CHECK-LABEL: @test47(
429 ; CHECK-NEXT:   %B = sext i8 %A to i64
430 ; CHECK-NEXT: %C = and i64 %B, 4294967253
431 ; CHECK-NEXT:  %E = or i64 %C, 42
432 ; CHECK-NEXT:  ret i64 %E
433 }
434
435 define i64 @test48(i8 %A, i8 %a) {
436   %b = zext i8 %a to i32
437   %B = zext i8 %A to i32
438   %C = shl i32 %B, 8
439   %D = or i32 %C, %b
440   %E = zext i32 %D to i64
441   ret i64 %E
442 ; CHECK-LABEL: @test48(
443 ; CHECK-NEXT: %b = zext i8 %a to i64
444 ; CHECK-NEXT: %B = zext i8 %A to i64
445 ; CHECK-NEXT: %C = shl nuw nsw i64 %B, 8
446 ; CHECK-NEXT: %D = or i64 %C, %b
447 ; CHECK-NEXT: ret i64 %D
448 }
449
450 define i64 @test49(i64 %A) {
451  %B = trunc i64 %A to i32
452  %C = or i32 %B, 1
453  %D = sext i32 %C to i64 
454  ret i64 %D
455 ; CHECK-LABEL: @test49(
456 ; CHECK-NEXT: %C = shl i64 %A, 32
457 ; CHECK-NEXT: ashr exact i64 %C, 32
458 ; CHECK-NEXT: %D = or i64 {{.*}}, 1
459 ; CHECK-NEXT: ret i64 %D
460 }
461
462 define i64 @test50(i64 %A) {
463   %a = lshr i64 %A, 2
464   %B = trunc i64 %a to i32
465   %D = add i32 %B, -1
466   %E = sext i32 %D to i64
467   ret i64 %E
468 ; CHECK-LABEL: @test50(
469 ; lshr+shl will be handled by DAGCombine.
470 ; CHECK-NEXT: lshr i64 %A, 2
471 ; CHECK-NEXT: shl i64 %a, 32
472 ; CHECK-NEXT: add i64 {{.*}}, -4294967296
473 ; CHECK-NEXT: %E = ashr exact i64 {{.*}}, 32
474 ; CHECK-NEXT: ret i64 %E
475 }
476
477 define i64 @test51(i64 %A, i1 %cond) {
478   %B = trunc i64 %A to i32
479   %C = and i32 %B, -2
480   %D = or i32 %B, 1
481   %E = select i1 %cond, i32 %C, i32 %D
482   %F = sext i32 %E to i64
483   ret i64 %F
484 ; CHECK-LABEL: @test51(
485 ; CHECK-NEXT: %C = and i64 %A, 4294967294
486 ; CHECK-NEXT: %D = or i64 %A, 1
487 ; CHECK-NEXT: %E = select i1 %cond, i64 %C, i64 %D
488 ; CHECK-NEXT: %sext = shl i64 %E, 32
489 ; CHECK-NEXT: %F = ashr exact i64 %sext, 32
490 ; CHECK-NEXT: ret i64 %F
491 }
492
493 define i32 @test52(i64 %A) {
494   %B = trunc i64 %A to i16
495   %C = or i16 %B, -32574
496   %D = and i16 %C, -25350
497   %E = zext i16 %D to i32
498   ret i32 %E
499 ; CHECK-LABEL: @test52(
500 ; CHECK-NEXT: %B = trunc i64 %A to i32
501 ; CHECK-NEXT: %C = and i32 %B, 7224
502 ; CHECK-NEXT: %D = or i32 %C, 32962
503 ; CHECK-NEXT: ret i32 %D
504 }
505
506 define i64 @test53(i32 %A) {
507   %B = trunc i32 %A to i16
508   %C = or i16 %B, -32574
509   %D = and i16 %C, -25350
510   %E = zext i16 %D to i64
511   ret i64 %E
512 ; CHECK-LABEL: @test53(
513 ; CHECK-NEXT: %B = zext i32 %A to i64
514 ; CHECK-NEXT: %C = and i64 %B, 7224
515 ; CHECK-NEXT: %D = or i64 %C, 32962
516 ; CHECK-NEXT: ret i64 %D
517 }
518
519 define i32 @test54(i64 %A) {
520   %B = trunc i64 %A to i16
521   %C = or i16 %B, -32574
522   %D = and i16 %C, -25350
523   %E = sext i16 %D to i32
524   ret i32 %E
525 ; CHECK-LABEL: @test54(
526 ; CHECK-NEXT: %B = trunc i64 %A to i32
527 ; CHECK-NEXT: %C = and i32 %B, 7224
528 ; CHECK-NEXT: %D = or i32 %C, -32574
529 ; CHECK-NEXT: ret i32 %D
530 }
531
532 define i64 @test55(i32 %A) {
533   %B = trunc i32 %A to i16
534   %C = or i16 %B, -32574
535   %D = and i16 %C, -25350
536   %E = sext i16 %D to i64
537   ret i64 %E
538 ; CHECK-LABEL: @test55(
539 ; CHECK-NEXT: %B = zext i32 %A to i64
540 ; CHECK-NEXT: %C = and i64 %B, 7224
541 ; CHECK-NEXT: %D = or i64 %C, -32574
542 ; CHECK-NEXT: ret i64 %D
543 }
544
545 define i64 @test56(i16 %A) nounwind {
546   %tmp353 = sext i16 %A to i32
547   %tmp354 = lshr i32 %tmp353, 5
548   %tmp355 = zext i32 %tmp354 to i64
549   ret i64 %tmp355
550 ; CHECK-LABEL: @test56(
551 ; CHECK-NEXT: %tmp353 = sext i16 %A to i64
552 ; CHECK-NEXT: %tmp354 = lshr i64 %tmp353, 5
553 ; CHECK-NEXT: %tmp355 = and i64 %tmp354, 134217727
554 ; CHECK-NEXT: ret i64 %tmp355
555 }
556
557 define i64 @test57(i64 %A) nounwind {
558  %B = trunc i64 %A to i32
559  %C = lshr i32 %B, 8
560  %E = zext i32 %C to i64
561  ret i64 %E
562 ; CHECK-LABEL: @test57(
563 ; CHECK-NEXT: %C = lshr i64 %A, 8 
564 ; CHECK-NEXT: %E = and i64 %C, 16777215
565 ; CHECK-NEXT: ret i64 %E
566 }
567
568 define i64 @test58(i64 %A) nounwind {
569  %B = trunc i64 %A to i32
570  %C = lshr i32 %B, 8
571  %D = or i32 %C, 128
572  %E = zext i32 %D to i64
573  ret i64 %E
574  
575 ; CHECK-LABEL: @test58(
576 ; CHECK-NEXT:   %C = lshr i64 %A, 8
577 ; CHECK-NEXT:   %D = and i64 %C, 16777087
578 ; CHECK-NEXT:   %E = or i64 %D, 128
579 ; CHECK-NEXT:   ret i64 %E
580 }
581
582 define i64 @test59(i8 %A, i8 %B) nounwind {
583   %C = zext i8 %A to i32
584   %D = shl i32 %C, 4
585   %E = and i32 %D, 48
586   %F = zext i8 %B to i32
587   %G = lshr i32 %F, 4
588   %H = or i32 %G, %E
589   %I = zext i32 %H to i64
590   ret i64 %I
591 ; CHECK-LABEL: @test59(
592 ; CHECK-NEXT:   %C = zext i8 %A to i64
593 ; CHECK-NOT: i32
594 ; CHECK:   %F = zext i8 %B to i64
595 ; CHECK-NOT: i32
596 ; CHECK:   ret i64 %H
597 }
598
599 define <3 x i32> @test60(<4 x i32> %call4) nounwind {
600   %tmp11 = bitcast <4 x i32> %call4 to i128
601   %tmp9 = trunc i128 %tmp11 to i96
602   %tmp10 = bitcast i96 %tmp9 to <3 x i32>
603   ret <3 x i32> %tmp10
604   
605 ; CHECK-LABEL: @test60(
606 ; CHECK-NEXT: shufflevector
607 ; CHECK-NEXT: ret
608 }
609
610 define <4 x i32> @test61(<3 x i32> %call4) nounwind {
611   %tmp11 = bitcast <3 x i32> %call4 to i96
612   %tmp9 = zext i96 %tmp11 to i128
613   %tmp10 = bitcast i128 %tmp9 to <4 x i32>
614   ret <4 x i32> %tmp10
615 ; CHECK-LABEL: @test61(
616 ; CHECK-NEXT: shufflevector
617 ; CHECK-NEXT: ret
618 }
619
620 define <4 x i32> @test62(<3 x float> %call4) nounwind {
621   %tmp11 = bitcast <3 x float> %call4 to i96
622   %tmp9 = zext i96 %tmp11 to i128
623   %tmp10 = bitcast i128 %tmp9 to <4 x i32>
624   ret <4 x i32> %tmp10
625 ; CHECK-LABEL: @test62(
626 ; CHECK-NEXT: bitcast
627 ; CHECK-NEXT: shufflevector
628 ; CHECK-NEXT: ret
629 }
630
631 ; PR7311 - Don't create invalid IR on scalar->vector cast.
632 define <2 x float> @test63(i64 %tmp8) nounwind {
633 entry:
634   %a = bitcast i64 %tmp8 to <2 x i32>           
635   %vcvt.i = uitofp <2 x i32> %a to <2 x float>  
636   ret <2 x float> %vcvt.i
637 ; CHECK-LABEL: @test63(
638 ; CHECK: bitcast
639 ; CHECK: uitofp
640 }
641
642 define <4 x float> @test64(<4 x float> %c) nounwind {
643   %t0 = bitcast <4 x float> %c to <4 x i32>
644   %t1 = bitcast <4 x i32> %t0 to <4 x float>
645   ret <4 x float> %t1
646 ; CHECK-LABEL: @test64(
647 ; CHECK-NEXT: ret <4 x float> %c
648 }
649
650 define <4 x float> @test65(<4 x float> %c) nounwind {
651   %t0 = bitcast <4 x float> %c to <2 x double>
652   %t1 = bitcast <2 x double> %t0 to <4 x float>
653   ret <4 x float> %t1
654 ; CHECK-LABEL: @test65(
655 ; CHECK-NEXT: ret <4 x float> %c
656 }
657
658 define <2 x float> @test66(<2 x float> %c) nounwind {
659   %t0 = bitcast <2 x float> %c to double
660   %t1 = bitcast double %t0 to <2 x float>
661   ret <2 x float> %t1
662 ; CHECK-LABEL: @test66(
663 ; CHECK-NEXT: ret <2 x float> %c
664 }
665
666 define float @test2c() {
667   ret float extractelement (<2 x float> bitcast (double bitcast (<2 x float> <float -1.000000e+00, float -1.000000e+00> to double) to <2 x float>), i32 0)
668 ; CHECK-LABEL: @test2c(
669 ; CHECK-NOT: extractelement
670 }
671
672 define i64 @test_mmx(<2 x i32> %c) nounwind {
673   %A = bitcast <2 x i32> %c to x86_mmx
674   %B = bitcast x86_mmx %A to <2 x i32>
675   %C = bitcast <2 x i32> %B to i64
676   ret i64 %C
677 ; CHECK-LABEL: @test_mmx(
678 ; CHECK-NOT: x86_mmx
679 }
680
681 define i64 @test_mmx_const(<2 x i32> %c) nounwind {
682   %A = bitcast <2 x i32> zeroinitializer to x86_mmx
683   %B = bitcast x86_mmx %A to <2 x i32>
684   %C = bitcast <2 x i32> %B to i64
685   ret i64 %C
686 ; CHECK-LABEL: @test_mmx_const(
687 ; CHECK-NOT: x86_mmx
688 }
689
690 ; PR12514
691 define i1 @test67(i1 %a, i32 %b) {
692   %tmp2 = zext i1 %a to i32
693   %conv6 = xor i32 %tmp2, 1
694   %and = and i32 %b, %conv6
695   %sext = shl nuw nsw i32 %and, 24
696   %neg.i = xor i32 %sext, -16777216
697   %conv.i.i = ashr exact i32 %neg.i, 24
698   %trunc = trunc i32 %conv.i.i to i8
699   %tobool.i = icmp eq i8 %trunc, 0
700   ret i1 %tobool.i
701 ; CHECK-LABEL: @test67(
702 ; CHECK: ret i1 false
703 }
704
705 %s = type { i32, i32, i32 }
706
707 define %s @test68(%s *%p, i64 %i) {
708 ; CHECK-LABEL: @test68(
709   %o = mul i64 %i, 12
710   %q = bitcast %s* %p to i8*
711   %pp = getelementptr inbounds i8* %q, i64 %o
712 ; CHECK-NEXT: getelementptr %s*
713   %r = bitcast i8* %pp to %s*
714   %l = load %s* %r
715 ; CHECK-NEXT: load %s*
716   ret %s %l
717 ; CHECK-NEXT: ret %s
718 }
719
720 ; addrspacecasts should be eliminated.
721 define %s @test68_addrspacecast(%s* %p, i64 %i) {
722 ; CHECK-LABEL: @test68_addrspacecast(
723 ; CHECK-NEXT: getelementptr %s*
724 ; CHECK-NEXT: load %s*
725 ; CHECK-NEXT: ret %s
726   %o = mul i64 %i, 12
727   %q = addrspacecast %s* %p to i8 addrspace(2)*
728   %pp = getelementptr inbounds i8 addrspace(2)* %q, i64 %o
729   %r = addrspacecast i8 addrspace(2)* %pp to %s*
730   %l = load %s* %r
731   ret %s %l
732 }
733
734 define %s @test68_addrspacecast_2(%s* %p, i64 %i) {
735 ; CHECK-LABEL: @test68_addrspacecast_2(
736 ; CHECK-NEXT: getelementptr %s* %p
737 ; CHECK-NEXT: addrspacecast
738 ; CHECK-NEXT: load %s addrspace(1)*
739 ; CHECK-NEXT: ret %s
740   %o = mul i64 %i, 12
741   %q = addrspacecast %s* %p to i8 addrspace(2)*
742   %pp = getelementptr inbounds i8 addrspace(2)* %q, i64 %o
743   %r = addrspacecast i8 addrspace(2)* %pp to %s addrspace(1)*
744   %l = load %s addrspace(1)* %r
745   ret %s %l
746 }
747
748 define %s @test68_as1(%s addrspace(1)* %p, i32 %i) {
749 ; CHECK-LABEL: @test68_as1(
750   %o = mul i32 %i, 12
751   %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)*
752   %pp = getelementptr inbounds i8 addrspace(1)* %q, i32 %o
753 ; CHECK-NEXT: getelementptr %s addrspace(1)*
754   %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)*
755   %l = load %s addrspace(1)* %r
756 ; CHECK-NEXT: load %s addrspace(1)*
757   ret %s %l
758 ; CHECK-NEXT: ret %s
759 }
760
761 define double @test69(double *%p, i64 %i) {
762 ; CHECK-LABEL: @test69(
763   %o = shl nsw i64 %i, 3
764   %q = bitcast double* %p to i8*
765   %pp = getelementptr inbounds i8* %q, i64 %o
766 ; CHECK-NEXT: getelementptr inbounds double*
767   %r = bitcast i8* %pp to double*
768   %l = load double* %r
769 ; CHECK-NEXT: load double*
770   ret double %l
771 ; CHECK-NEXT: ret double
772 }
773
774 define %s @test70(%s *%p, i64 %i) {
775 ; CHECK-LABEL: @test70(
776   %o = mul nsw i64 %i, 36
777 ; CHECK-NEXT: mul nsw i64 %i, 3
778   %q = bitcast %s* %p to i8*
779   %pp = getelementptr inbounds i8* %q, i64 %o
780 ; CHECK-NEXT: getelementptr inbounds %s*
781   %r = bitcast i8* %pp to %s*
782   %l = load %s* %r
783 ; CHECK-NEXT: load %s*
784   ret %s %l
785 ; CHECK-NEXT: ret %s
786 }
787
788 define double @test71(double *%p, i64 %i) {
789 ; CHECK-LABEL: @test71(
790   %o = shl i64 %i, 5
791 ; CHECK-NEXT: shl i64 %i, 2
792   %q = bitcast double* %p to i8*
793   %pp = getelementptr i8* %q, i64 %o
794 ; CHECK-NEXT: getelementptr double*
795   %r = bitcast i8* %pp to double*
796   %l = load double* %r
797 ; CHECK-NEXT: load double*
798   ret double %l
799 ; CHECK-NEXT: ret double
800 }
801
802 define double @test72(double *%p, i32 %i) {
803 ; CHECK-LABEL: @test72(
804   %so = shl nsw i32 %i, 3
805   %o = sext i32 %so to i64
806 ; CHECK-NEXT: sext i32 %i to i64
807   %q = bitcast double* %p to i8*
808   %pp = getelementptr inbounds i8* %q, i64 %o
809 ; CHECK-NEXT: getelementptr inbounds double*
810   %r = bitcast i8* %pp to double*
811   %l = load double* %r
812 ; CHECK-NEXT: load double*
813   ret double %l
814 ; CHECK-NEXT: ret double
815 }
816
817 define double @test73(double *%p, i128 %i) {
818 ; CHECK-LABEL: @test73(
819   %lo = shl nsw i128 %i, 3
820   %o = trunc i128 %lo to i64
821 ; CHECK-NEXT: trunc i128 %i to i64
822   %q = bitcast double* %p to i8*
823   %pp = getelementptr inbounds i8* %q, i64 %o
824 ; CHECK-NEXT: getelementptr double*
825   %r = bitcast i8* %pp to double*
826   %l = load double* %r
827 ; CHECK-NEXT: load double*
828   ret double %l
829 ; CHECK-NEXT: ret double
830 }
831
832 define double @test74(double *%p, i64 %i) {
833 ; CHECK-LABEL: @test74(
834   %q = bitcast double* %p to i64*
835   %pp = getelementptr inbounds i64* %q, i64 %i
836 ; CHECK-NEXT: getelementptr inbounds double*
837   %r = bitcast i64* %pp to double*
838   %l = load double* %r
839 ; CHECK-NEXT: load double*
840   ret double %l
841 ; CHECK-NEXT: ret double
842 }
843
844 define i32* @test75(i32* %p, i32 %x) {
845 ; CHECK-LABEL: @test75(
846   %y = shl i32 %x, 3
847 ; CHECK-NEXT: shl i32 %x, 3
848   %z = sext i32 %y to i64
849 ; CHECK-NEXT: sext i32 %y to i64
850   %q = bitcast i32* %p to i8*
851   %r = getelementptr i8* %q, i64 %z
852   %s = bitcast i8* %r to i32*
853   ret i32* %s
854 }
855
856 define %s @test76(%s *%p, i64 %i, i64 %j) {
857 ; CHECK-LABEL: @test76(
858   %o = mul i64 %i, 12
859   %o2 = mul nsw i64 %o, %j
860 ; CHECK-NEXT: %o2 = mul i64 %i, %j
861   %q = bitcast %s* %p to i8*
862   %pp = getelementptr inbounds i8* %q, i64 %o2
863 ; CHECK-NEXT: getelementptr %s* %p, i64 %o2
864   %r = bitcast i8* %pp to %s*
865   %l = load %s* %r
866 ; CHECK-NEXT: load %s*
867   ret %s %l
868 ; CHECK-NEXT: ret %s
869 }
870
871 define %s @test77(%s *%p, i64 %i, i64 %j) {
872 ; CHECK-LABEL: @test77(
873   %o = mul nsw i64 %i, 36
874   %o2 = mul nsw i64 %o, %j
875 ; CHECK-NEXT: %o = mul nsw i64 %i, 3
876 ; CHECK-NEXT: %o2 = mul nsw i64 %o, %j
877   %q = bitcast %s* %p to i8*
878   %pp = getelementptr inbounds i8* %q, i64 %o2
879 ; CHECK-NEXT: getelementptr inbounds %s* %p, i64 %o2
880   %r = bitcast i8* %pp to %s*
881   %l = load %s* %r
882 ; CHECK-NEXT: load %s*
883   ret %s %l
884 ; CHECK-NEXT: ret %s
885 }
886
887 define %s @test78(%s *%p, i64 %i, i64 %j, i32 %k, i32 %l, i128 %m, i128 %n) {
888 ; CHECK-LABEL: @test78(
889   %a = mul nsw i32 %k, 36
890 ; CHECK-NEXT: mul nsw i32 %k, 3
891   %b = mul nsw i32 %a, %l
892 ; CHECK-NEXT: mul nsw i32 %a, %l
893   %c = sext i32 %b to i128
894 ; CHECK-NEXT: sext i32 %b to i128
895   %d = mul nsw i128 %c, %m
896 ; CHECK-NEXT: mul nsw i128 %c, %m
897   %e = mul i128 %d, %n
898 ; CHECK-NEXT: mul i128 %d, %n
899   %f = trunc i128 %e to i64
900 ; CHECK-NEXT: trunc i128 %e to i64
901   %g = mul nsw i64 %f, %i
902 ; CHECK-NEXT: mul i64 %f, %i
903   %h = mul nsw i64 %g, %j
904 ; CHECK-NEXT: mul i64 %g, %j
905   %q = bitcast %s* %p to i8*
906   %pp = getelementptr inbounds i8* %q, i64 %h
907 ; CHECK-NEXT: getelementptr %s* %p, i64 %h
908   %r = bitcast i8* %pp to %s*
909   %load = load %s* %r
910 ; CHECK-NEXT: load %s*
911   ret %s %load
912 ; CHECK-NEXT: ret %s
913 }
914
915 define %s @test79(%s *%p, i64 %i, i32 %j) {
916 ; CHECK-LABEL: @test79(
917   %a = mul nsw i64 %i, 36
918 ; CHECK: mul nsw i64 %i, 36
919   %b = trunc i64 %a to i32
920   %c = mul i32 %b, %j
921   %q = bitcast %s* %p to i8*
922 ; CHECK: bitcast
923   %pp = getelementptr inbounds i8* %q, i32 %c
924   %r = bitcast i8* %pp to %s*
925   %l = load %s* %r
926   ret %s %l
927 }
928
929 define double @test80([100 x double]* %p, i32 %i) {
930 ; CHECK-LABEL: @test80(
931   %tmp = shl nsw i32 %i, 3
932 ; CHECK-NEXT: sext i32 %i to i64
933   %q = bitcast [100 x double]* %p to i8*
934   %pp = getelementptr i8* %q, i32 %tmp
935 ; CHECK-NEXT: getelementptr [100 x double]*
936   %r = bitcast i8* %pp to double*
937   %l = load double* %r
938 ; CHECK-NEXT: load double*
939   ret double %l
940 ; CHECK-NEXT: ret double
941 }
942
943 define double @test80_addrspacecast([100 x double] addrspace(1)* %p, i32 %i) {
944 ; CHECK-LABEL: @test80_addrspacecast(
945 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)* %p
946 ; CHECK-NEXT: load double addrspace(1)*
947 ; CHECK-NEXT: ret double
948   %tmp = shl nsw i32 %i, 3
949   %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
950   %pp = getelementptr i8 addrspace(2)* %q, i32 %tmp
951   %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(1)*
952   %l = load double addrspace(1)* %r
953   ret double %l
954 }
955
956 define double @test80_addrspacecast_2([100 x double] addrspace(1)* %p, i32 %i) {
957 ; CHECK-LABEL: @test80_addrspacecast_2(
958 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*
959 ; CHECK-NEXT: addrspacecast double addrspace(1)*
960 ; CHECK-NEXT: load double addrspace(3)*
961 ; CHECK-NEXT: ret double
962   %tmp = shl nsw i32 %i, 3
963   %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
964   %pp = getelementptr i8 addrspace(2)* %q, i32 %tmp
965   %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(3)*
966   %l = load double addrspace(3)* %r
967   ret double %l
968 }
969
970 define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) {
971 ; CHECK-LABEL: @test80_as1(
972   %tmp = shl nsw i16 %i, 3
973 ; CHECK-NEXT: sext i16 %i to i32
974   %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)*
975   %pp = getelementptr i8 addrspace(1)* %q, i16 %tmp
976 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*
977   %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)*
978   %l = load double addrspace(1)* %r
979 ; CHECK-NEXT: load double addrspace(1)*
980   ret double %l
981 ; CHECK-NEXT: ret double
982 }
983
984 define double @test81(double *%p, float %f) {
985   %i = fptosi float %f to i64
986   %q = bitcast double* %p to i8*
987   %pp = getelementptr i8* %q, i64 %i
988   %r = bitcast i8* %pp to double*
989   %l = load double* %r
990   ret double %l
991 }
992
993 define i64 @test82(i64 %A) nounwind {
994   %B = trunc i64 %A to i32
995   %C = lshr i32 %B, 8
996   %D = shl i32 %C, 9
997   %E = zext i32 %D to i64
998   ret i64 %E
999
1000 ; CHECK-LABEL: @test82(
1001 ; CHECK-NEXT:   [[REG:%[0-9]*]] = shl i64 %A, 1
1002 ; CHECK-NEXT:   %E = and i64 [[REG]], 4294966784
1003 ; CHECK-NEXT:   ret i64 %E
1004 }
1005
1006 ; PR15959
1007 define i64 @test83(i16 %a, i64 %k) {
1008   %conv = sext i16 %a to i32
1009   %sub = add nsw i64 %k, -1
1010   %sh_prom = trunc i64 %sub to i32
1011   %shl = shl i32 %conv, %sh_prom
1012   %sh_prom1 = zext i32 %shl to i64
1013   ret i64 %sh_prom1
1014
1015 ; CHECK-LABEL: @test83(
1016 ; CHECK: %sub = add i64 %k, 4294967295
1017 ; CHECK: %sh_prom = trunc i64 %sub to i32
1018 ; CHECK: %shl = shl i32 %conv, %sh_prom
1019 }
1020
1021 define i8 @test84(i32 %a) {
1022   %add = add nsw i32 %a, -16777216
1023   %shr = lshr exact i32 %add, 23
1024   %trunc = trunc i32 %shr to i8
1025   ret i8 %trunc
1026
1027 ; CHECK-LABEL: @test84(
1028 ; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432
1029 ; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
1030 ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
1031 }
1032
1033 define i8 @test85(i32 %a) {
1034   %add = add nuw i32 %a, -16777216
1035   %shr = lshr exact i32 %add, 23
1036   %trunc = trunc i32 %shr to i8
1037   ret i8 %trunc
1038
1039 ; CHECK-LABEL: @test85(
1040 ; CHECK: [[ADD:%.*]] = add i32 %a, 2130706432
1041 ; CHECK: [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
1042 ; CHECK: [[CST:%.*]] = trunc i32 [[SHR]] to i8
1043 }
1044
1045 ; Overflow on a float to int or int to float conversion is undefined (PR21130).
1046
1047 define i8 @overflow_fptosi() {
1048   %i = fptosi double 1.56e+02 to i8
1049   ret i8 %i
1050 ; CHECK-LABEL: @overflow_fptosi(
1051 ; CHECK-NEXT: ret i8 undef 
1052 }
1053
1054 define i8 @overflow_fptoui() {
1055   %i = fptoui double 2.56e+02 to i8
1056   ret i8 %i
1057 ; CHECK-LABEL: @overflow_fptoui(
1058 ; CHECK-NEXT: ret i8 undef 
1059 }
1060
1061 ; The maximum float is approximately 2 ** 128 which is 3.4E38. 
1062 ; The constant below is 4E38. Use a 130 bit integer to hold that
1063 ; number; 129-bits for the value + 1 bit for the sign.
1064 define float @overflow_uitofp() {
1065   %i = uitofp i130 400000000000000000000000000000000000000 to float
1066   ret float %i
1067 ; CHECK-LABEL: @overflow_uitofp(
1068 ; CHECK-NEXT: ret float undef 
1069 }
1070
1071 define float @overflow_sitofp() {
1072   %i = sitofp i130 400000000000000000000000000000000000000 to float
1073   ret float %i
1074 ; CHECK-LABEL: @overflow_sitofp(
1075 ; CHECK-NEXT: ret float undef 
1076 }
1077
1078 define i32 @PR21388(i32* %v) {
1079   %icmp = icmp slt i32* %v, null
1080   %sext = sext i1 %icmp to i32
1081   ret i32 %sext
1082 ; CHECK-LABEL: @PR21388(
1083 ; CHECK-NEXT: %[[icmp:.*]] = icmp slt i32* %v, null
1084 ; CHECK-NEXT: %[[sext:.*]] = sext i1 %[[icmp]] to i32
1085 ; CHECK-NEXT: ret i32 %[[sext]]
1086 }