Allwo bitcast + struct GEP transform to work with addrspacecast
[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 void @test25(i32** %P) {
203         %c = bitcast i32** %P to float**                ; <float**> [#uses=1]
204         ;; Fold cast into null
205         store float* null, float** %c
206         ret void
207 ; CHECK: store i32* null, i32** %P
208 ; CHECK: ret void
209 }
210
211 define i32 @test26(float %F) {
212         ;; no need to cast from float->double.
213         %c = fpext float %F to double           ; <double> [#uses=1]
214         %D = fptosi double %c to i32            ; <i32> [#uses=1]
215         ret i32 %D
216 ; CHECK: %D = fptosi float %F to i32
217 ; CHECK: ret i32 %D
218 }
219
220 define [4 x float]* @test27([9 x [4 x float]]* %A) {
221         %c = bitcast [9 x [4 x float]]* %A to [4 x float]*              ; <[4 x float]*> [#uses=1]
222         ret [4 x float]* %c
223 ; CHECK: %c = getelementptr inbounds [9 x [4 x float]]* %A, i64 0, i64 0
224 ; CHECK: ret [4 x float]* %c
225 }
226
227 define float* @test28([4 x float]* %A) {
228         %c = bitcast [4 x float]* %A to float*          ; <float*> [#uses=1]
229         ret float* %c
230 ; CHECK: %c = getelementptr inbounds [4 x float]* %A, i64 0, i64 0
231 ; CHECK: ret float* %c
232 }
233
234 define i32 @test29(i32 %c1, i32 %c2) {
235         %tmp1 = trunc i32 %c1 to i8             ; <i8> [#uses=1]
236         %tmp4.mask = trunc i32 %c2 to i8                ; <i8> [#uses=1]
237         %tmp = or i8 %tmp4.mask, %tmp1          ; <i8> [#uses=1]
238         %tmp10 = zext i8 %tmp to i32            ; <i32> [#uses=1]
239         ret i32 %tmp10
240 ; CHECK: %tmp2 = or i32 %c2, %c1
241 ; CHECK: %tmp10 = and i32 %tmp2, 255
242 ; CHECK: ret i32 %tmp10
243 }
244
245 define i32 @test30(i32 %c1) {
246         %c2 = trunc i32 %c1 to i8               ; <i8> [#uses=1]
247         %c3 = xor i8 %c2, 1             ; <i8> [#uses=1]
248         %c4 = zext i8 %c3 to i32                ; <i32> [#uses=1]
249         ret i32 %c4
250 ; CHECK: %c3 = and i32 %c1, 255
251 ; CHECK: %c4 = xor i32 %c3, 1
252 ; CHECK: ret i32 %c4
253 }
254
255 define i1 @test31(i64 %A) {
256         %B = trunc i64 %A to i32                ; <i32> [#uses=1]
257         %C = and i32 %B, 42             ; <i32> [#uses=1]
258         %D = icmp eq i32 %C, 10         ; <i1> [#uses=1]
259         ret i1 %D
260 ; CHECK: %C = and i64 %A, 42
261 ; CHECK: %D = icmp eq i64 %C, 10
262 ; CHECK: ret i1 %D
263 }
264
265 define i32 @test33(i32 %c1) {
266         %x = bitcast i32 %c1 to float           ; <float> [#uses=1]
267         %y = bitcast float %x to i32            ; <i32> [#uses=1]
268         ret i32 %y
269 ; CHECK: ret i32 %c1
270 }
271
272 define i16 @test34(i16 %a) {
273         %c1 = zext i16 %a to i32                ; <i32> [#uses=1]
274         %tmp21 = lshr i32 %c1, 8                ; <i32> [#uses=1]
275         %c2 = trunc i32 %tmp21 to i16           ; <i16> [#uses=1]
276         ret i16 %c2
277 ; CHECK: %tmp21 = lshr i16 %a, 8
278 ; CHECK: ret i16 %tmp21
279 }
280
281 define i16 @test35(i16 %a) {
282         %c1 = bitcast i16 %a to i16             ; <i16> [#uses=1]
283         %tmp2 = lshr i16 %c1, 8         ; <i16> [#uses=1]
284         %c2 = bitcast i16 %tmp2 to i16          ; <i16> [#uses=1]
285         ret i16 %c2
286 ; CHECK: %tmp2 = lshr i16 %a, 8
287 ; CHECK: ret i16 %tmp2
288 }
289
290 ; icmp sgt i32 %a, -1
291 ; rdar://6480391
292 define i1 @test36(i32 %a) {
293         %b = lshr i32 %a, 31
294         %c = trunc i32 %b to i8
295         %d = icmp eq i8 %c, 0
296         ret i1 %d
297 ; CHECK: %d = icmp sgt i32 %a, -1
298 ; CHECK: ret i1 %d
299 }
300
301 ; ret i1 false
302 define i1 @test37(i32 %a) {
303         %b = lshr i32 %a, 31
304         %c = or i32 %b, 512
305         %d = trunc i32 %c to i8
306         %e = icmp eq i8 %d, 11
307         ret i1 %e
308 ; CHECK: ret i1 false
309 }
310
311 define i64 @test38(i32 %a) {
312         %1 = icmp eq i32 %a, -2
313         %2 = zext i1 %1 to i8
314         %3 = xor i8 %2, 1
315         %4 = zext i8 %3 to i64
316         ret i64 %4
317 ; CHECK: %1 = icmp ne i32 %a, -2
318 ; CHECK: %2 = zext i1 %1 to i64
319 ; CHECK: ret i64 %2
320 }
321
322 define i16 @test39(i16 %a) {
323         %tmp = zext i16 %a to i32
324         %tmp21 = lshr i32 %tmp, 8
325         %tmp5 = shl i32 %tmp, 8
326         %tmp.upgrd.32 = or i32 %tmp21, %tmp5
327         %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
328         ret i16 %tmp.upgrd.3
329 ; CHECK-LABEL: @test39(
330 ; CHECK: %tmp.upgrd.32 = call i16 @llvm.bswap.i16(i16 %a)
331 ; CHECK: ret i16 %tmp.upgrd.32
332 }
333
334 define i16 @test40(i16 %a) {
335         %tmp = zext i16 %a to i32
336         %tmp21 = lshr i32 %tmp, 9
337         %tmp5 = shl i32 %tmp, 8
338         %tmp.upgrd.32 = or i32 %tmp21, %tmp5
339         %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
340         ret i16 %tmp.upgrd.3
341 ; CHECK-LABEL: @test40(
342 ; CHECK: %tmp21 = lshr i16 %a, 9
343 ; CHECK: %tmp5 = shl i16 %a, 8
344 ; CHECK: %tmp.upgrd.32 = or i16 %tmp21, %tmp5
345 ; CHECK: ret i16 %tmp.upgrd.32
346 }
347
348 ; PR1263
349 define i32* @test41(i32* %tmp1) {
350         %tmp64 = bitcast i32* %tmp1 to { i32 }*
351         %tmp65 = getelementptr { i32 }* %tmp64, i32 0, i32 0
352         ret i32* %tmp65
353 ; CHECK-LABEL: @test41(
354 ; CHECK: ret i32* %tmp1
355 }
356
357 define i32 addrspace(1)* @test41_addrspacecast_smaller(i32* %tmp1) {
358   %tmp64 = addrspacecast i32* %tmp1 to { i32 } addrspace(1)*
359   %tmp65 = getelementptr { i32 } addrspace(1)* %tmp64, i32 0, i32 0
360   ret i32 addrspace(1)* %tmp65
361 ; CHECK-LABEL: @test41_addrspacecast_smaller(
362 ; CHECK: addrspacecast i32* %tmp1 to i32 addrspace(1)*
363 ; CHECK-NEXT: ret i32 addrspace(1)*
364 }
365
366 define i32* @test41_addrspacecast_larger(i32 addrspace(1)* %tmp1) {
367   %tmp64 = addrspacecast i32 addrspace(1)* %tmp1 to { i32 }*
368   %tmp65 = getelementptr { i32 }* %tmp64, i32 0, i32 0
369   ret i32* %tmp65
370 ; CHECK-LABEL: @test41_addrspacecast_larger(
371 ; CHECK: addrspacecast i32 addrspace(1)* %tmp1 to i32*
372 ; CHECK-NEXT: ret i32*
373 }
374
375 define i32 @test42(i32 %X) {
376         %Y = trunc i32 %X to i8         ; <i8> [#uses=1]
377         %Z = zext i8 %Y to i32          ; <i32> [#uses=1]
378         ret i32 %Z
379 ; CHECK-LABEL: @test42(
380 ; CHECK: %Z = and i32 %X, 255
381 }
382
383 ; rdar://6598839
384 define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly {
385         %A = zext i8 %on_off to i32
386         %B = add i32 %A, -1
387         %C = sext i32 %B to i64
388         ret i64 %C  ;; Should be (add (zext i8 -> i64), -1)
389 ; CHECK-LABEL: @test43(
390 ; CHECK-NEXT: %A = zext i8 %on_off to i64
391 ; CHECK-NEXT: %B = add nsw i64 %A, -1
392 ; CHECK-NEXT: ret i64 %B
393 }
394
395 define i64 @test44(i8 %T) {
396  %A = zext i8 %T to i16
397  %B = or i16 %A, 1234
398  %C = zext i16 %B to i64
399  ret i64 %C
400 ; CHECK-LABEL: @test44(
401 ; CHECK-NEXT: %A = zext i8 %T to i64
402 ; CHECK-NEXT: %B = or i64 %A, 1234
403 ; CHECK-NEXT: ret i64 %B
404 }
405
406 define i64 @test45(i8 %A, i64 %Q) {
407  %D = trunc i64 %Q to i32  ;; should be removed
408  %B = sext i8 %A to i32
409  %C = or i32 %B, %D
410  %E = zext i32 %C to i64 
411  ret i64 %E
412 ; CHECK-LABEL: @test45(
413 ; CHECK-NEXT: %B = sext i8 %A to i64
414 ; CHECK-NEXT: %C = or i64 %B, %Q
415 ; CHECK-NEXT: %E = and i64 %C, 4294967295
416 ; CHECK-NEXT: ret i64 %E
417 }
418
419
420 define i64 @test46(i64 %A) {
421  %B = trunc i64 %A to i32
422  %C = and i32 %B, 42
423  %D = shl i32 %C, 8
424  %E = zext i32 %D to i64 
425  ret i64 %E
426 ; CHECK-LABEL: @test46(
427 ; CHECK-NEXT: %C = shl i64 %A, 8
428 ; CHECK-NEXT: %D = and i64 %C, 10752
429 ; CHECK-NEXT: ret i64 %D
430 }
431
432 define i64 @test47(i8 %A) {
433  %B = sext i8 %A to i32
434  %C = or i32 %B, 42
435  %E = zext i32 %C to i64 
436  ret i64 %E
437 ; CHECK-LABEL: @test47(
438 ; CHECK-NEXT:   %B = sext i8 %A to i64
439 ; CHECK-NEXT: %C = and i64 %B, 4294967253
440 ; CHECK-NEXT:  %E = or i64 %C, 42
441 ; CHECK-NEXT:  ret i64 %E
442 }
443
444 define i64 @test48(i8 %A, i8 %a) {
445   %b = zext i8 %a to i32
446   %B = zext i8 %A to i32
447   %C = shl i32 %B, 8
448   %D = or i32 %C, %b
449   %E = zext i32 %D to i64
450   ret i64 %E
451 ; CHECK-LABEL: @test48(
452 ; CHECK-NEXT: %b = zext i8 %a to i64
453 ; CHECK-NEXT: %B = zext i8 %A to i64
454 ; CHECK-NEXT: %C = shl nuw nsw i64 %B, 8
455 ; CHECK-NEXT: %D = or i64 %C, %b
456 ; CHECK-NEXT: ret i64 %D
457 }
458
459 define i64 @test49(i64 %A) {
460  %B = trunc i64 %A to i32
461  %C = or i32 %B, 1
462  %D = sext i32 %C to i64 
463  ret i64 %D
464 ; CHECK-LABEL: @test49(
465 ; CHECK-NEXT: %C = shl i64 %A, 32
466 ; CHECK-NEXT: ashr exact i64 %C, 32
467 ; CHECK-NEXT: %D = or i64 {{.*}}, 1
468 ; CHECK-NEXT: ret i64 %D
469 }
470
471 define i64 @test50(i64 %A) {
472   %a = lshr i64 %A, 2
473   %B = trunc i64 %a to i32
474   %D = add i32 %B, -1
475   %E = sext i32 %D to i64
476   ret i64 %E
477 ; CHECK-LABEL: @test50(
478 ; lshr+shl will be handled by DAGCombine.
479 ; CHECK-NEXT: lshr i64 %A, 2
480 ; CHECK-NEXT: shl i64 %a, 32
481 ; CHECK-NEXT: add i64 {{.*}}, -4294967296
482 ; CHECK-NEXT: %E = ashr exact i64 {{.*}}, 32
483 ; CHECK-NEXT: ret i64 %E
484 }
485
486 define i64 @test51(i64 %A, i1 %cond) {
487   %B = trunc i64 %A to i32
488   %C = and i32 %B, -2
489   %D = or i32 %B, 1
490   %E = select i1 %cond, i32 %C, i32 %D
491   %F = sext i32 %E to i64
492   ret i64 %F
493 ; CHECK-LABEL: @test51(
494 ; CHECK-NEXT: %C = and i64 %A, 4294967294
495 ; CHECK-NEXT: %D = or i64 %A, 1
496 ; CHECK-NEXT: %E = select i1 %cond, i64 %C, i64 %D
497 ; CHECK-NEXT: %sext = shl i64 %E, 32
498 ; CHECK-NEXT: %F = ashr exact i64 %sext, 32
499 ; CHECK-NEXT: ret i64 %F
500 }
501
502 define i32 @test52(i64 %A) {
503   %B = trunc i64 %A to i16
504   %C = or i16 %B, -32574
505   %D = and i16 %C, -25350
506   %E = zext i16 %D to i32
507   ret i32 %E
508 ; CHECK-LABEL: @test52(
509 ; CHECK-NEXT: %B = trunc i64 %A to i32
510 ; CHECK-NEXT: %C = and i32 %B, 7224
511 ; CHECK-NEXT: %D = or i32 %C, 32962
512 ; CHECK-NEXT: ret i32 %D
513 }
514
515 define i64 @test53(i32 %A) {
516   %B = trunc i32 %A to i16
517   %C = or i16 %B, -32574
518   %D = and i16 %C, -25350
519   %E = zext i16 %D to i64
520   ret i64 %E
521 ; CHECK-LABEL: @test53(
522 ; CHECK-NEXT: %B = zext i32 %A to i64
523 ; CHECK-NEXT: %C = and i64 %B, 7224
524 ; CHECK-NEXT: %D = or i64 %C, 32962
525 ; CHECK-NEXT: ret i64 %D
526 }
527
528 define i32 @test54(i64 %A) {
529   %B = trunc i64 %A to i16
530   %C = or i16 %B, -32574
531   %D = and i16 %C, -25350
532   %E = sext i16 %D to i32
533   ret i32 %E
534 ; CHECK-LABEL: @test54(
535 ; CHECK-NEXT: %B = trunc i64 %A to i32
536 ; CHECK-NEXT: %C = and i32 %B, 7224
537 ; CHECK-NEXT: %D = or i32 %C, -32574
538 ; CHECK-NEXT: ret i32 %D
539 }
540
541 define i64 @test55(i32 %A) {
542   %B = trunc i32 %A to i16
543   %C = or i16 %B, -32574
544   %D = and i16 %C, -25350
545   %E = sext i16 %D to i64
546   ret i64 %E
547 ; CHECK-LABEL: @test55(
548 ; CHECK-NEXT: %B = zext i32 %A to i64
549 ; CHECK-NEXT: %C = and i64 %B, 7224
550 ; CHECK-NEXT: %D = or i64 %C, -32574
551 ; CHECK-NEXT: ret i64 %D
552 }
553
554 define i64 @test56(i16 %A) nounwind {
555   %tmp353 = sext i16 %A to i32
556   %tmp354 = lshr i32 %tmp353, 5
557   %tmp355 = zext i32 %tmp354 to i64
558   ret i64 %tmp355
559 ; CHECK-LABEL: @test56(
560 ; CHECK-NEXT: %tmp353 = sext i16 %A to i64
561 ; CHECK-NEXT: %tmp354 = lshr i64 %tmp353, 5
562 ; CHECK-NEXT: %tmp355 = and i64 %tmp354, 134217727
563 ; CHECK-NEXT: ret i64 %tmp355
564 }
565
566 define i64 @test57(i64 %A) nounwind {
567  %B = trunc i64 %A to i32
568  %C = lshr i32 %B, 8
569  %E = zext i32 %C to i64
570  ret i64 %E
571 ; CHECK-LABEL: @test57(
572 ; CHECK-NEXT: %C = lshr i64 %A, 8 
573 ; CHECK-NEXT: %E = and i64 %C, 16777215
574 ; CHECK-NEXT: ret i64 %E
575 }
576
577 define i64 @test58(i64 %A) nounwind {
578  %B = trunc i64 %A to i32
579  %C = lshr i32 %B, 8
580  %D = or i32 %C, 128
581  %E = zext i32 %D to i64
582  ret i64 %E
583  
584 ; CHECK-LABEL: @test58(
585 ; CHECK-NEXT:   %C = lshr i64 %A, 8
586 ; CHECK-NEXT:   %D = and i64 %C, 16777087
587 ; CHECK-NEXT:   %E = or i64 %D, 128
588 ; CHECK-NEXT:   ret i64 %E
589 }
590
591 define i64 @test59(i8 %A, i8 %B) nounwind {
592   %C = zext i8 %A to i32
593   %D = shl i32 %C, 4
594   %E = and i32 %D, 48
595   %F = zext i8 %B to i32
596   %G = lshr i32 %F, 4
597   %H = or i32 %G, %E
598   %I = zext i32 %H to i64
599   ret i64 %I
600 ; CHECK-LABEL: @test59(
601 ; CHECK-NEXT:   %C = zext i8 %A to i64
602 ; CHECK-NOT: i32
603 ; CHECK:   %F = zext i8 %B to i64
604 ; CHECK-NOT: i32
605 ; CHECK:   ret i64 %H
606 }
607
608 define <3 x i32> @test60(<4 x i32> %call4) nounwind {
609   %tmp11 = bitcast <4 x i32> %call4 to i128
610   %tmp9 = trunc i128 %tmp11 to i96
611   %tmp10 = bitcast i96 %tmp9 to <3 x i32>
612   ret <3 x i32> %tmp10
613   
614 ; CHECK-LABEL: @test60(
615 ; CHECK-NEXT: shufflevector
616 ; CHECK-NEXT: ret
617 }
618
619 define <4 x i32> @test61(<3 x i32> %call4) nounwind {
620   %tmp11 = bitcast <3 x i32> %call4 to i96
621   %tmp9 = zext i96 %tmp11 to i128
622   %tmp10 = bitcast i128 %tmp9 to <4 x i32>
623   ret <4 x i32> %tmp10
624 ; CHECK-LABEL: @test61(
625 ; CHECK-NEXT: shufflevector
626 ; CHECK-NEXT: ret
627 }
628
629 define <4 x i32> @test62(<3 x float> %call4) nounwind {
630   %tmp11 = bitcast <3 x float> %call4 to i96
631   %tmp9 = zext i96 %tmp11 to i128
632   %tmp10 = bitcast i128 %tmp9 to <4 x i32>
633   ret <4 x i32> %tmp10
634 ; CHECK-LABEL: @test62(
635 ; CHECK-NEXT: bitcast
636 ; CHECK-NEXT: shufflevector
637 ; CHECK-NEXT: ret
638 }
639
640 ; PR7311 - Don't create invalid IR on scalar->vector cast.
641 define <2 x float> @test63(i64 %tmp8) nounwind {
642 entry:
643   %a = bitcast i64 %tmp8 to <2 x i32>           
644   %vcvt.i = uitofp <2 x i32> %a to <2 x float>  
645   ret <2 x float> %vcvt.i
646 ; CHECK-LABEL: @test63(
647 ; CHECK: bitcast
648 ; CHECK: uitofp
649 }
650
651 define <4 x float> @test64(<4 x float> %c) nounwind {
652   %t0 = bitcast <4 x float> %c to <4 x i32>
653   %t1 = bitcast <4 x i32> %t0 to <4 x float>
654   ret <4 x float> %t1
655 ; CHECK-LABEL: @test64(
656 ; CHECK-NEXT: ret <4 x float> %c
657 }
658
659 define <4 x float> @test65(<4 x float> %c) nounwind {
660   %t0 = bitcast <4 x float> %c to <2 x double>
661   %t1 = bitcast <2 x double> %t0 to <4 x float>
662   ret <4 x float> %t1
663 ; CHECK-LABEL: @test65(
664 ; CHECK-NEXT: ret <4 x float> %c
665 }
666
667 define <2 x float> @test66(<2 x float> %c) nounwind {
668   %t0 = bitcast <2 x float> %c to double
669   %t1 = bitcast double %t0 to <2 x float>
670   ret <2 x float> %t1
671 ; CHECK-LABEL: @test66(
672 ; CHECK-NEXT: ret <2 x float> %c
673 }
674
675 define float @test2c() {
676   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)
677 ; CHECK-LABEL: @test2c(
678 ; CHECK-NOT: extractelement
679 }
680
681 define i64 @test_mmx(<2 x i32> %c) nounwind {
682   %A = bitcast <2 x i32> %c 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(
687 ; CHECK-NOT: x86_mmx
688 }
689
690 define i64 @test_mmx_const(<2 x i32> %c) nounwind {
691   %A = bitcast <2 x i32> zeroinitializer to x86_mmx
692   %B = bitcast x86_mmx %A to <2 x i32>
693   %C = bitcast <2 x i32> %B to i64
694   ret i64 %C
695 ; CHECK-LABEL: @test_mmx_const(
696 ; CHECK-NOT: x86_mmx
697 }
698
699 ; PR12514
700 define i1 @test67(i1 %a, i32 %b) {
701   %tmp2 = zext i1 %a to i32
702   %conv6 = xor i32 %tmp2, 1
703   %and = and i32 %b, %conv6
704   %sext = shl nuw nsw i32 %and, 24
705   %neg.i = xor i32 %sext, -16777216
706   %conv.i.i = ashr exact i32 %neg.i, 24
707   %trunc = trunc i32 %conv.i.i to i8
708   %tobool.i = icmp eq i8 %trunc, 0
709   ret i1 %tobool.i
710 ; CHECK-LABEL: @test67(
711 ; CHECK: ret i1 false
712 }
713
714 %s = type { i32, i32, i32 }
715
716 define %s @test68(%s *%p, i64 %i) {
717 ; CHECK-LABEL: @test68(
718   %o = mul i64 %i, 12
719   %q = bitcast %s* %p to i8*
720   %pp = getelementptr inbounds i8* %q, i64 %o
721 ; CHECK-NEXT: getelementptr %s*
722   %r = bitcast i8* %pp to %s*
723   %l = load %s* %r
724 ; CHECK-NEXT: load %s*
725   ret %s %l
726 ; CHECK-NEXT: ret %s
727 }
728
729 ; addrspacecasts should be eliminated.
730 define %s @test68_addrspacecast(%s* %p, i64 %i) {
731 ; CHECK-LABEL: @test68_addrspacecast(
732 ; CHECK-NEXT: getelementptr %s*
733 ; CHECK-NEXT: load %s*
734 ; CHECK-NEXT: ret %s
735   %o = mul i64 %i, 12
736   %q = addrspacecast %s* %p to i8 addrspace(2)*
737   %pp = getelementptr inbounds i8 addrspace(2)* %q, i64 %o
738   %r = addrspacecast i8 addrspace(2)* %pp to %s*
739   %l = load %s* %r
740   ret %s %l
741 }
742
743 define %s @test68_addrspacecast_2(%s* %p, i64 %i) {
744 ; CHECK-LABEL: @test68_addrspacecast_2(
745 ; CHECK-NEXT: getelementptr %s* %p
746 ; CHECK-NEXT: addrspacecast
747 ; CHECK-NEXT: load %s addrspace(1)*
748 ; CHECK-NEXT: ret %s
749   %o = mul i64 %i, 12
750   %q = addrspacecast %s* %p to i8 addrspace(2)*
751   %pp = getelementptr inbounds i8 addrspace(2)* %q, i64 %o
752   %r = addrspacecast i8 addrspace(2)* %pp to %s addrspace(1)*
753   %l = load %s addrspace(1)* %r
754   ret %s %l
755 }
756
757 define %s @test68_as1(%s addrspace(1)* %p, i32 %i) {
758 ; CHECK-LABEL: @test68_as1(
759   %o = mul i32 %i, 12
760   %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)*
761   %pp = getelementptr inbounds i8 addrspace(1)* %q, i32 %o
762 ; CHECK-NEXT: getelementptr %s addrspace(1)*
763   %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)*
764   %l = load %s addrspace(1)* %r
765 ; CHECK-NEXT: load %s addrspace(1)*
766   ret %s %l
767 ; CHECK-NEXT: ret %s
768 }
769
770 define double @test69(double *%p, i64 %i) {
771 ; CHECK-LABEL: @test69(
772   %o = shl nsw i64 %i, 3
773   %q = bitcast double* %p to i8*
774   %pp = getelementptr inbounds i8* %q, i64 %o
775 ; CHECK-NEXT: getelementptr inbounds double*
776   %r = bitcast i8* %pp to double*
777   %l = load double* %r
778 ; CHECK-NEXT: load double*
779   ret double %l
780 ; CHECK-NEXT: ret double
781 }
782
783 define %s @test70(%s *%p, i64 %i) {
784 ; CHECK-LABEL: @test70(
785   %o = mul nsw i64 %i, 36
786 ; CHECK-NEXT: mul nsw i64 %i, 3
787   %q = bitcast %s* %p to i8*
788   %pp = getelementptr inbounds i8* %q, i64 %o
789 ; CHECK-NEXT: getelementptr inbounds %s*
790   %r = bitcast i8* %pp to %s*
791   %l = load %s* %r
792 ; CHECK-NEXT: load %s*
793   ret %s %l
794 ; CHECK-NEXT: ret %s
795 }
796
797 define double @test71(double *%p, i64 %i) {
798 ; CHECK-LABEL: @test71(
799   %o = shl i64 %i, 5
800 ; CHECK-NEXT: shl i64 %i, 2
801   %q = bitcast double* %p to i8*
802   %pp = getelementptr i8* %q, i64 %o
803 ; CHECK-NEXT: getelementptr double*
804   %r = bitcast i8* %pp to double*
805   %l = load double* %r
806 ; CHECK-NEXT: load double*
807   ret double %l
808 ; CHECK-NEXT: ret double
809 }
810
811 define double @test72(double *%p, i32 %i) {
812 ; CHECK-LABEL: @test72(
813   %so = mul nsw i32 %i, 8
814   %o = sext i32 %so to i64
815 ; CHECK-NEXT: sext i32 %i to i64
816   %q = bitcast double* %p to i8*
817   %pp = getelementptr inbounds i8* %q, i64 %o
818 ; CHECK-NEXT: getelementptr inbounds double*
819   %r = bitcast i8* %pp to double*
820   %l = load double* %r
821 ; CHECK-NEXT: load double*
822   ret double %l
823 ; CHECK-NEXT: ret double
824 }
825
826 define double @test73(double *%p, i128 %i) {
827 ; CHECK-LABEL: @test73(
828   %lo = mul nsw i128 %i, 8
829   %o = trunc i128 %lo to i64
830 ; CHECK-NEXT: trunc i128 %i to i64
831   %q = bitcast double* %p to i8*
832   %pp = getelementptr inbounds i8* %q, i64 %o
833 ; CHECK-NEXT: getelementptr double*
834   %r = bitcast i8* %pp to double*
835   %l = load double* %r
836 ; CHECK-NEXT: load double*
837   ret double %l
838 ; CHECK-NEXT: ret double
839 }
840
841 define double @test74(double *%p, i64 %i) {
842 ; CHECK-LABEL: @test74(
843   %q = bitcast double* %p to i64*
844   %pp = getelementptr inbounds i64* %q, i64 %i
845 ; CHECK-NEXT: getelementptr inbounds double*
846   %r = bitcast i64* %pp to double*
847   %l = load double* %r
848 ; CHECK-NEXT: load double*
849   ret double %l
850 ; CHECK-NEXT: ret double
851 }
852
853 define i32* @test75(i32* %p, i32 %x) {
854 ; CHECK-LABEL: @test75(
855   %y = shl i32 %x, 3
856 ; CHECK-NEXT: shl i32 %x, 3
857   %z = sext i32 %y to i64
858 ; CHECK-NEXT: sext i32 %y to i64
859   %q = bitcast i32* %p to i8*
860   %r = getelementptr i8* %q, i64 %z
861   %s = bitcast i8* %r to i32*
862   ret i32* %s
863 }
864
865 define %s @test76(%s *%p, i64 %i, i64 %j) {
866 ; CHECK-LABEL: @test76(
867   %o = mul i64 %i, 12
868   %o2 = mul nsw i64 %o, %j
869 ; CHECK-NEXT: %o2 = mul i64 %i, %j
870   %q = bitcast %s* %p to i8*
871   %pp = getelementptr inbounds i8* %q, i64 %o2
872 ; CHECK-NEXT: getelementptr %s* %p, i64 %o2
873   %r = bitcast i8* %pp to %s*
874   %l = load %s* %r
875 ; CHECK-NEXT: load %s*
876   ret %s %l
877 ; CHECK-NEXT: ret %s
878 }
879
880 define %s @test77(%s *%p, i64 %i, i64 %j) {
881 ; CHECK-LABEL: @test77(
882   %o = mul nsw i64 %i, 36
883   %o2 = mul nsw i64 %o, %j
884 ; CHECK-NEXT: %o = mul nsw i64 %i, 3
885 ; CHECK-NEXT: %o2 = mul nsw i64 %o, %j
886   %q = bitcast %s* %p to i8*
887   %pp = getelementptr inbounds i8* %q, i64 %o2
888 ; CHECK-NEXT: getelementptr inbounds %s* %p, i64 %o2
889   %r = bitcast i8* %pp to %s*
890   %l = load %s* %r
891 ; CHECK-NEXT: load %s*
892   ret %s %l
893 ; CHECK-NEXT: ret %s
894 }
895
896 define %s @test78(%s *%p, i64 %i, i64 %j, i32 %k, i32 %l, i128 %m, i128 %n) {
897 ; CHECK-LABEL: @test78(
898   %a = mul nsw i32 %k, 36
899 ; CHECK-NEXT: mul nsw i32 %k, 3
900   %b = mul nsw i32 %a, %l
901 ; CHECK-NEXT: mul nsw i32 %a, %l
902   %c = sext i32 %b to i128
903 ; CHECK-NEXT: sext i32 %b to i128
904   %d = mul nsw i128 %c, %m
905 ; CHECK-NEXT: mul nsw i128 %c, %m
906   %e = mul i128 %d, %n
907 ; CHECK-NEXT: mul i128 %d, %n
908   %f = trunc i128 %e to i64
909 ; CHECK-NEXT: trunc i128 %e to i64
910   %g = mul nsw i64 %f, %i
911 ; CHECK-NEXT: mul i64 %f, %i
912   %h = mul nsw i64 %g, %j
913 ; CHECK-NEXT: mul i64 %g, %j
914   %q = bitcast %s* %p to i8*
915   %pp = getelementptr inbounds i8* %q, i64 %h
916 ; CHECK-NEXT: getelementptr %s* %p, i64 %h
917   %r = bitcast i8* %pp to %s*
918   %load = load %s* %r
919 ; CHECK-NEXT: load %s*
920   ret %s %load
921 ; CHECK-NEXT: ret %s
922 }
923
924 define %s @test79(%s *%p, i64 %i, i32 %j) {
925 ; CHECK-LABEL: @test79(
926   %a = mul nsw i64 %i, 36
927 ; CHECK: mul nsw i64 %i, 36
928   %b = trunc i64 %a to i32
929   %c = mul i32 %b, %j
930   %q = bitcast %s* %p to i8*
931 ; CHECK: bitcast
932   %pp = getelementptr inbounds i8* %q, i32 %c
933   %r = bitcast i8* %pp to %s*
934   %l = load %s* %r
935   ret %s %l
936 }
937
938 define double @test80([100 x double]* %p, i32 %i) {
939 ; CHECK-LABEL: @test80(
940   %tmp = mul nsw i32 %i, 8
941 ; CHECK-NEXT: sext i32 %i to i64
942   %q = bitcast [100 x double]* %p to i8*
943   %pp = getelementptr i8* %q, i32 %tmp
944 ; CHECK-NEXT: getelementptr [100 x double]*
945   %r = bitcast i8* %pp to double*
946   %l = load double* %r
947 ; CHECK-NEXT: load double*
948   ret double %l
949 ; CHECK-NEXT: ret double
950 }
951
952 define double @test80_addrspacecast([100 x double] addrspace(1)* %p, i32 %i) {
953 ; CHECK-LABEL: @test80_addrspacecast(
954 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)* %p
955 ; CHECK-NEXT: load double addrspace(1)*
956 ; CHECK-NEXT: ret double
957   %tmp = mul nsw i32 %i, 8
958   %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
959   %pp = getelementptr i8 addrspace(2)* %q, i32 %tmp
960   %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(1)*
961   %l = load double addrspace(1)* %r
962   ret double %l
963 }
964
965 define double @test80_addrspacecast_2([100 x double] addrspace(1)* %p, i32 %i) {
966 ; CHECK-LABEL: @test80_addrspacecast_2(
967 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*
968 ; CHECK-NEXT: addrspacecast double addrspace(1)*
969 ; CHECK-NEXT: load double addrspace(3)*
970 ; CHECK-NEXT: ret double
971   %tmp = mul nsw i32 %i, 8
972   %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
973   %pp = getelementptr i8 addrspace(2)* %q, i32 %tmp
974   %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(3)*
975   %l = load double addrspace(3)* %r
976   ret double %l
977 }
978
979 define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) {
980 ; CHECK-LABEL: @test80_as1(
981   %tmp = mul nsw i16 %i, 8
982 ; CHECK-NEXT: sext i16 %i to i32
983   %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)*
984   %pp = getelementptr i8 addrspace(1)* %q, i16 %tmp
985 ; CHECK-NEXT: getelementptr [100 x double] addrspace(1)*
986   %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)*
987   %l = load double addrspace(1)* %r
988 ; CHECK-NEXT: load double addrspace(1)*
989   ret double %l
990 ; CHECK-NEXT: ret double
991 }
992
993 define double @test81(double *%p, float %f) {
994   %i = fptosi float %f to i64
995   %q = bitcast double* %p to i8*
996   %pp = getelementptr i8* %q, i64 %i
997   %r = bitcast i8* %pp to double*
998   %l = load double* %r
999   ret double %l
1000 }
1001
1002 define i64 @test82(i64 %A) nounwind {
1003   %B = trunc i64 %A to i32
1004   %C = lshr i32 %B, 8
1005   %D = shl i32 %C, 9
1006   %E = zext i32 %D to i64
1007   ret i64 %E
1008
1009 ; CHECK-LABEL: @test82(
1010 ; CHECK-NEXT:   [[REG:%[0-9]*]] = shl i64 %A, 1
1011 ; CHECK-NEXT:   %E = and i64 [[REG]], 4294966784
1012 ; CHECK-NEXT:   ret i64 %E
1013 }
1014
1015 ; PR15959
1016 define i64 @test83(i16 %a, i64 %k) {
1017   %conv = sext i16 %a to i32
1018   %sub = add nsw i64 %k, -1
1019   %sh_prom = trunc i64 %sub to i32
1020   %shl = shl i32 %conv, %sh_prom
1021   %sh_prom1 = zext i32 %shl to i64
1022   ret i64 %sh_prom1
1023
1024 ; CHECK-LABEL: @test83(
1025 ; CHECK: %sub = add nsw i64 %k, 4294967295
1026 ; CHECK: %sh_prom = trunc i64 %sub to i32
1027 ; CHECK: %shl = shl i32 %conv, %sh_prom
1028 }