• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; Tests to make sure elimination of casts is working correctly
3; RUN: opt < %s -instcombine -S | FileCheck %s
4target 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"
5
6@inbuf = external global [32832 x i8]           ; <[32832 x i8]*> [#uses=1]
7
8define i32 @test1(i32 %A) {
9; CHECK-LABEL: @test1(
10; CHECK-NEXT:    ret i32 %A
11;
12  %c1 = bitcast i32 %A to i32             ; <i32> [#uses=1]
13  %c2 = bitcast i32 %c1 to i32            ; <i32> [#uses=1]
14  ret i32 %c2
15}
16
17define i64 @test2(i8 %A) {
18; CHECK-LABEL: @test2(
19; CHECK-NEXT:    [[RET:%.*]] = zext i8 %A to i64
20; CHECK-NEXT:    ret i64 [[RET]]
21;
22  %c1 = zext i8 %A to i16         ; <i16> [#uses=1]
23  %c2 = zext i16 %c1 to i32               ; <i32> [#uses=1]
24  %Ret = zext i32 %c2 to i64              ; <i64> [#uses=1]
25  ret i64 %Ret
26}
27
28; This function should just use bitwise AND
29define i64 @test3(i64 %A) {
30; CHECK-LABEL: @test3(
31; CHECK-NEXT:    [[C2:%.*]] = and i64 %A, 255
32; CHECK-NEXT:    ret i64 [[C2]]
33;
34  %c1 = trunc i64 %A to i8                ; <i8> [#uses=1]
35  %c2 = zext i8 %c1 to i64                ; <i64> [#uses=1]
36  ret i64 %c2
37}
38
39define i32 @test4(i32 %A, i32 %B) {
40; CHECK-LABEL: @test4(
41; CHECK-NEXT:    [[COND:%.*]] = icmp slt i32 %A, %B
42; CHECK-NEXT:    [[RESULT:%.*]] = zext i1 [[COND]] to i32
43; CHECK-NEXT:    ret i32 [[RESULT]]
44;
45  %COND = icmp slt i32 %A, %B             ; <i1> [#uses=1]
46  ; Booleans are unsigned integrals
47  %c = zext i1 %COND to i8                ; <i8> [#uses=1]
48  ; for the cast elim purpose
49  %result = zext i8 %c to i32             ; <i32> [#uses=1]
50  ret i32 %result
51}
52
53define i32 @test5(i1 %B) {
54        ; This cast should get folded into
55; CHECK-LABEL: @test5(
56; CHECK-NEXT:    [[RESULT:%.*]] = zext i1 %B to i32
57; CHECK-NEXT:    ret i32 [[RESULT]]
58;
59  %c = zext i1 %B to i8           ; <i8> [#uses=1]
60  ; this cast
61  %result = zext i8 %c to i32             ; <i32> [#uses=1]
62  ret i32 %result
63}
64
65define i32 @test6(i64 %A) {
66; CHECK-LABEL: @test6(
67; CHECK-NEXT:    [[C1:%.*]] = trunc i64 %A to i32
68; CHECK-NEXT:    ret i32 [[C1]]
69;
70  %c1 = trunc i64 %A to i32               ; <i32> [#uses=1]
71  %res = bitcast i32 %c1 to i32           ; <i32> [#uses=1]
72  ret i32 %res
73}
74
75define i64 @test7(i1 %A) {
76; CHECK-LABEL: @test7(
77; CHECK-NEXT:    [[RES:%.*]] = zext i1 %A to i64
78; CHECK-NEXT:    ret i64 [[RES]]
79;
80  %c1 = zext i1 %A to i32         ; <i32> [#uses=1]
81  %res = sext i32 %c1 to i64              ; <i64> [#uses=1]
82  ret i64 %res
83}
84
85define i64 @test8(i8 %A) {
86; CHECK-LABEL: @test8(
87; CHECK-NEXT:    [[C1:%.*]] = sext i8 %A to i64
88; CHECK-NEXT:    ret i64 [[C1]]
89;
90  %c1 = sext i8 %A to i64         ; <i64> [#uses=1]
91  %res = bitcast i64 %c1 to i64           ; <i64> [#uses=1]
92  ret i64 %res
93}
94
95define i16 @test9(i16 %A) {
96; CHECK-LABEL: @test9(
97; CHECK-NEXT:    ret i16 %A
98;
99  %c1 = sext i16 %A to i32                ; <i32> [#uses=1]
100  %c2 = trunc i32 %c1 to i16              ; <i16> [#uses=1]
101  ret i16 %c2
102}
103
104define i16 @test10(i16 %A) {
105; CHECK-LABEL: @test10(
106; CHECK-NEXT:    ret i16 %A
107;
108  %c1 = sext i16 %A to i32                ; <i32> [#uses=1]
109  %c2 = trunc i32 %c1 to i16              ; <i16> [#uses=1]
110  ret i16 %c2
111}
112
113declare void @varargs(i32, ...)
114
115define void @test11(i32* %P) {
116; CHECK-LABEL: @test11(
117; CHECK-NEXT:    call void (i32, ...) @varargs(i32 5, i32* %P)
118; CHECK-NEXT:    ret void
119;
120  %c = bitcast i32* %P to i16*            ; <i16*> [#uses=1]
121  call void (i32, ...) @varargs( i32 5, i16* %c )
122  ret void
123}
124
125declare i32 @__gxx_personality_v0(...)
126define void @test_invoke_vararg_cast(i32* %a, i32* %b) personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
127; CHECK-LABEL: @test_invoke_vararg_cast(
128; CHECK-NEXT:  entry:
129; CHECK-NEXT:    invoke void (i32, ...) @varargs(i32 1, i32* %b, i32* %a)
130; CHECK-NEXT:    to label %invoke.cont unwind label %lpad
131; CHECK:       invoke.cont:
132; CHECK-NEXT:    ret void
133; CHECK:       lpad:
134; CHECK-NEXT:    [[TMP0:%.*]] = landingpad { i8*, i32
135;
136entry:
137  %0 = bitcast i32* %b to i8*
138  %1 = bitcast i32* %a to i64*
139  invoke void (i32, ...) @varargs(i32 1, i8* %0, i64* %1)
140  to label %invoke.cont unwind label %lpad
141
142invoke.cont:                                      ; preds = %entry
143  ret void
144
145lpad:                                             ; preds = %entry
146  %2 = landingpad { i8*, i32 }
147  cleanup
148  ret void
149}
150
151define i8* @test13(i64 %A) {
152; CHECK-LABEL: @test13(
153; CHECK-NEXT:    [[C:%.*]] = getelementptr [32832 x i8], [32832 x i8]* @inbuf, i64 0, i64 %A
154; CHECK-NEXT:    ret i8* [[C]]
155;
156  %c = getelementptr [0 x i8], [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A             ; <i8*> [#uses=1]
157  ret i8* %c
158}
159
160define i1 @test14(i8 %A) {
161; CHECK-LABEL: @test14(
162; CHECK-NEXT:    [[X:%.*]] = icmp sgt i8 %A, -1
163; CHECK-NEXT:    ret i1 [[X]]
164;
165  %c = bitcast i8 %A to i8                ; <i8> [#uses=1]
166  %X = icmp ult i8 %c, -128               ; <i1> [#uses=1]
167  ret i1 %X
168}
169
170
171; This just won't occur when there's no difference between ubyte and sbyte
172;bool %test15(ubyte %A) {
173;        %c = cast ubyte %A to sbyte
174;        %X = setlt sbyte %c, 0   ; setgt %A, 127
175;        ret bool %X
176;}
177
178define i1 @test16(i32* %P) {
179; CHECK-LABEL: @test16(
180; CHECK-NEXT:    [[C:%.*]] = icmp ne i32* %P, null
181; CHECK-NEXT:    ret i1 [[C]]
182;
183  %c = icmp ne i32* %P, null              ; <i1> [#uses=1]
184  ret i1 %c
185}
186
187define i16 @test17(i1 %tmp3) {
188; CHECK-LABEL: @test17(
189; CHECK-NEXT:    [[T86:%.*]] = zext i1 %tmp3 to i16
190; CHECK-NEXT:    ret i16 [[T86]]
191;
192  %c = zext i1 %tmp3 to i32               ; <i32> [#uses=1]
193  %t86 = trunc i32 %c to i16              ; <i16> [#uses=1]
194  ret i16 %t86
195}
196
197define i16 @test18(i8 %tmp3) {
198; CHECK-LABEL: @test18(
199; CHECK-NEXT:    [[T86:%.*]] = sext i8 %tmp3 to i16
200; CHECK-NEXT:    ret i16 [[T86]]
201;
202  %c = sext i8 %tmp3 to i32               ; <i32> [#uses=1]
203  %t86 = trunc i32 %c to i16              ; <i16> [#uses=1]
204  ret i16 %t86
205}
206
207define i1 @test19(i32 %X) {
208; CHECK-LABEL: @test19(
209; CHECK-NEXT:    [[Z:%.*]] = icmp slt i32 %X, 12345
210; CHECK-NEXT:    ret i1 [[Z]]
211;
212  %c = sext i32 %X to i64
213  %Z = icmp slt i64 %c, 12345
214  ret i1 %Z
215}
216
217define <2 x i1> @test19vec(<2 x i32> %X) {
218; CHECK-LABEL: @test19vec(
219; CHECK-NEXT:    [[Z:%.*]] = icmp slt <2 x i32> %X, <i32 12345, i32 2147483647>
220; CHECK-NEXT:    ret <2 x i1> [[Z]]
221;
222  %c = sext <2 x i32> %X to <2 x i64>
223  %Z = icmp slt <2 x i64> %c, <i64 12345, i64 2147483647>
224  ret <2 x i1> %Z
225}
226
227define <3 x i1> @test19vec2(<3 x i1> %X) {
228; CHECK-LABEL: @test19vec2(
229; CHECK-NEXT:    [[CMPEQ:%.*]] = xor <3 x i1> %X, <i1 true, i1 true, i1 true>
230; CHECK-NEXT:    ret <3 x i1> [[CMPEQ]]
231;
232  %sext = sext <3 x i1> %X to <3 x i32>
233  %cmpeq = icmp eq <3 x i32> %sext, zeroinitializer
234  ret <3 x i1> %cmpeq
235}
236
237define i1 @test20(i1 %B) {
238; CHECK-LABEL: @test20(
239; CHECK-NEXT:    ret i1 false
240;
241  %c = zext i1 %B to i32          ; <i32> [#uses=1]
242  %D = icmp slt i32 %c, -1                ; <i1> [#uses=1]
243  ;; false
244  ret i1 %D
245}
246
247define i32 @test21(i32 %X) {
248; CHECK-LABEL: @test21(
249; CHECK-NEXT:    [[C21:%.*]] = and i32 %X, 255
250; CHECK-NEXT:    ret i32 [[C21]]
251;
252  %c1 = trunc i32 %X to i8                ; <i8> [#uses=1]
253  ;; sext -> zext -> and -> nop
254  %c2 = sext i8 %c1 to i32                ; <i32> [#uses=1]
255  %RV = and i32 %c2, 255          ; <i32> [#uses=1]
256  ret i32 %RV
257}
258
259define i32 @test22(i32 %X) {
260; CHECK-LABEL: @test22(
261; CHECK-NEXT:    [[SEXT:%.*]] = shl i32 %X, 24
262; CHECK-NEXT:    ret i32 [[SEXT]]
263;
264  %c1 = trunc i32 %X to i8                ; <i8> [#uses=1]
265  ;; sext -> zext -> and -> nop
266  %c2 = sext i8 %c1 to i32                ; <i32> [#uses=1]
267  %RV = shl i32 %c2, 24           ; <i32> [#uses=1]
268  ret i32 %RV
269}
270
271define i32 @test23(i32 %X) {
272        ;; Turn into an AND even though X
273; CHECK-LABEL: @test23(
274; CHECK-NEXT:    [[C2:%.*]] = and i32 %X, 65535
275; CHECK-NEXT:    ret i32 [[C2]]
276;
277  %c1 = trunc i32 %X to i16               ; <i16> [#uses=1]
278  ;; and Z are signed.
279  %c2 = zext i16 %c1 to i32               ; <i32> [#uses=1]
280  ret i32 %c2
281}
282
283define i1 @test24(i1 %C) {
284; CHECK-LABEL: @test24(
285; CHECK-NEXT:    ret i1 true
286;
287  %X = select i1 %C, i32 14, i32 1234             ; <i32> [#uses=1]
288  ;; Fold cast into select
289  %c = icmp ne i32 %X, 0          ; <i1> [#uses=1]
290  ret i1 %c
291}
292
293define i32 @test26(float %F) {
294        ;; no need to cast from float->double.
295; CHECK-LABEL: @test26(
296; CHECK-NEXT:    [[D:%.*]] = fptosi float %F to i32
297; CHECK-NEXT:    ret i32 [[D]]
298;
299  %c = fpext float %F to double           ; <double> [#uses=1]
300  %D = fptosi double %c to i32            ; <i32> [#uses=1]
301  ret i32 %D
302}
303
304define [4 x float]* @test27([9 x [4 x float]]* %A) {
305; CHECK-LABEL: @test27(
306; CHECK-NEXT:    [[C:%.*]] = getelementptr inbounds [9 x [4 x float]], [9 x [4 x float]]* %A, i64 0, i64 0
307; CHECK-NEXT:    ret [4 x float]* [[C]]
308;
309  %c = bitcast [9 x [4 x float]]* %A to [4 x float]*              ; <[4 x float]*> [#uses=1]
310  ret [4 x float]* %c
311}
312
313define float* @test28([4 x float]* %A) {
314; CHECK-LABEL: @test28(
315; CHECK-NEXT:    [[C:%.*]] = getelementptr inbounds [4 x float], [4 x float]* %A, i64 0, i64 0
316; CHECK-NEXT:    ret float* [[C]]
317;
318  %c = bitcast [4 x float]* %A to float*          ; <float*> [#uses=1]
319  ret float* %c
320}
321
322define i32 @test29(i32 %c1, i32 %c2) {
323; CHECK-LABEL: @test29(
324; CHECK-NEXT:    [[TMP2:%.*]] = or i32 %c2, %c1
325; CHECK-NEXT:    [[TMP10:%.*]] = and i32 [[TMP2]], 255
326; CHECK-NEXT:    ret i32 [[TMP10]]
327;
328  %tmp1 = trunc i32 %c1 to i8             ; <i8> [#uses=1]
329  %tmp4.mask = trunc i32 %c2 to i8                ; <i8> [#uses=1]
330  %tmp = or i8 %tmp4.mask, %tmp1          ; <i8> [#uses=1]
331  %tmp10 = zext i8 %tmp to i32            ; <i32> [#uses=1]
332  ret i32 %tmp10
333}
334
335define i32 @test30(i32 %c1) {
336; CHECK-LABEL: @test30(
337; CHECK-NEXT:    [[C3:%.*]] = and i32 %c1, 255
338; CHECK-NEXT:    [[C4:%.*]] = xor i32 [[C3]], 1
339; CHECK-NEXT:    ret i32 [[C4]]
340;
341  %c2 = trunc i32 %c1 to i8               ; <i8> [#uses=1]
342  %c3 = xor i8 %c2, 1             ; <i8> [#uses=1]
343  %c4 = zext i8 %c3 to i32                ; <i32> [#uses=1]
344  ret i32 %c4
345}
346
347define i1 @test31(i64 %A) {
348; CHECK-LABEL: @test31(
349; CHECK-NEXT:    [[C:%.*]] = and i64 %A, 42
350; CHECK-NEXT:    [[D:%.*]] = icmp eq i64 [[C]], 10
351; CHECK-NEXT:    ret i1 [[D]]
352;
353  %B = trunc i64 %A to i32                ; <i32> [#uses=1]
354  %C = and i32 %B, 42             ; <i32> [#uses=1]
355  %D = icmp eq i32 %C, 10         ; <i1> [#uses=1]
356  ret i1 %D
357}
358
359define i32 @test33(i32 %c1) {
360; CHECK-LABEL: @test33(
361; CHECK-NEXT:    ret i32 %c1
362;
363  %x = bitcast i32 %c1 to float           ; <float> [#uses=1]
364  %y = bitcast float %x to i32            ; <i32> [#uses=1]
365  ret i32 %y
366}
367
368define i16 @test34(i16 %a) {
369; CHECK-LABEL: @test34(
370; CHECK-NEXT:    [[TMP21:%.*]] = lshr i16 %a, 8
371; CHECK-NEXT:    ret i16 [[TMP21]]
372;
373  %c1 = zext i16 %a to i32                ; <i32> [#uses=1]
374  %tmp21 = lshr i32 %c1, 8                ; <i32> [#uses=1]
375  %c2 = trunc i32 %tmp21 to i16           ; <i16> [#uses=1]
376  ret i16 %c2
377}
378
379define i16 @test35(i16 %a) {
380; CHECK-LABEL: @test35(
381; CHECK-NEXT:    [[TMP2:%.*]] = lshr i16 %a, 8
382; CHECK-NEXT:    ret i16 [[TMP2]]
383;
384  %c1 = bitcast i16 %a to i16             ; <i16> [#uses=1]
385  %tmp2 = lshr i16 %c1, 8         ; <i16> [#uses=1]
386  %c2 = bitcast i16 %tmp2 to i16          ; <i16> [#uses=1]
387  ret i16 %c2
388}
389
390; icmp sgt i32 %a, -1
391; rdar://6480391
392define i1 @test36(i32 %a) {
393; CHECK-LABEL: @test36(
394; CHECK-NEXT:    [[D:%.*]] = icmp sgt i32 %a, -1
395; CHECK-NEXT:    ret i1 [[D]]
396;
397  %b = lshr i32 %a, 31
398  %c = trunc i32 %b to i8
399  %d = icmp eq i8 %c, 0
400  ret i1 %d
401}
402
403; ret i1 false
404define i1 @test37(i32 %a) {
405; CHECK-LABEL: @test37(
406; CHECK-NEXT:    ret i1 false
407;
408  %b = lshr i32 %a, 31
409  %c = or i32 %b, 512
410  %d = trunc i32 %c to i8
411  %e = icmp eq i8 %d, 11
412  ret i1 %e
413}
414
415define i64 @test38(i32 %a) {
416; CHECK-LABEL: @test38(
417; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i32 %a, -2
418; CHECK-NEXT:    [[TMP2:%.*]] = zext i1 [[TMP1]] to i64
419; CHECK-NEXT:    ret i64 [[TMP2]]
420;
421  %1 = icmp eq i32 %a, -2
422  %2 = zext i1 %1 to i8
423  %3 = xor i8 %2, 1
424  %4 = zext i8 %3 to i64
425  ret i64 %4
426}
427
428define i16 @test39(i16 %a) {
429; CHECK-LABEL: @test39(
430; CHECK-NEXT:    [[TMP_UPGRD_32:%.*]] = call i16 @llvm.bswap.i16(i16 %a)
431; CHECK-NEXT:    ret i16 [[TMP_UPGRD_32]]
432;
433  %tmp = zext i16 %a to i32
434  %tmp21 = lshr i32 %tmp, 8
435  %tmp5 = shl i32 %tmp, 8
436  %tmp.upgrd.32 = or i32 %tmp21, %tmp5
437  %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
438  ret i16 %tmp.upgrd.3
439}
440
441define i16 @test40(i16 %a) {
442; CHECK-LABEL: @test40(
443; CHECK-NEXT:    [[TMP21:%.*]] = lshr i16 %a, 9
444; CHECK-NEXT:    [[TMP5:%.*]] = shl i16 %a, 8
445; CHECK-NEXT:    [[TMP_UPGRD_32:%.*]] = or i16 [[TMP21]], [[TMP5]]
446; CHECK-NEXT:    ret i16 [[TMP_UPGRD_32]]
447;
448  %tmp = zext i16 %a to i32
449  %tmp21 = lshr i32 %tmp, 9
450  %tmp5 = shl i32 %tmp, 8
451  %tmp.upgrd.32 = or i32 %tmp21, %tmp5
452  %tmp.upgrd.3 = trunc i32 %tmp.upgrd.32 to i16
453  ret i16 %tmp.upgrd.3
454}
455
456; PR1263
457define i32* @test41(i32* %tmp1) {
458; CHECK-LABEL: @test41(
459; CHECK-NEXT:    ret i32* %tmp1
460;
461  %tmp64 = bitcast i32* %tmp1 to { i32 }*
462  %tmp65 = getelementptr { i32 }, { i32 }* %tmp64, i32 0, i32 0
463  ret i32* %tmp65
464}
465
466define i32 addrspace(1)* @test41_addrspacecast_smaller(i32* %tmp1) {
467; CHECK-LABEL: @test41_addrspacecast_smaller(
468; CHECK-NEXT:    [[TMP65:%.*]] = addrspacecast i32* %tmp1 to i32 addrspace(1)*
469; CHECK-NEXT:    ret i32 addrspace(1)* [[TMP65]]
470;
471  %tmp64 = addrspacecast i32* %tmp1 to { i32 } addrspace(1)*
472  %tmp65 = getelementptr { i32 }, { i32 } addrspace(1)* %tmp64, i32 0, i32 0
473  ret i32 addrspace(1)* %tmp65
474}
475
476define i32* @test41_addrspacecast_larger(i32 addrspace(1)* %tmp1) {
477; CHECK-LABEL: @test41_addrspacecast_larger(
478; CHECK-NEXT:    [[TMP65:%.*]] = addrspacecast i32 addrspace(1)* %tmp1 to i32*
479; CHECK-NEXT:    ret i32* [[TMP65]]
480;
481  %tmp64 = addrspacecast i32 addrspace(1)* %tmp1 to { i32 }*
482  %tmp65 = getelementptr { i32 }, { i32 }* %tmp64, i32 0, i32 0
483  ret i32* %tmp65
484}
485
486define i32 @test42(i32 %X) {
487; CHECK-LABEL: @test42(
488; CHECK-NEXT:    [[Z:%.*]] = and i32 %X, 255
489; CHECK-NEXT:    ret i32 [[Z]]
490;
491  %Y = trunc i32 %X to i8         ; <i8> [#uses=1]
492  %Z = zext i8 %Y to i32          ; <i32> [#uses=1]
493  ret i32 %Z
494}
495
496; rdar://6598839
497define zeroext i64 @test43(i8 zeroext %on_off) nounwind readonly {
498; CHECK-LABEL: @test43(
499; CHECK-NEXT:    [[A:%.*]] = zext i8 %on_off to i64
500; CHECK-NEXT:    [[B:%.*]] = add nsw i64 [[A]], -1
501; CHECK-NEXT:    ret i64 [[B]]
502;
503  %A = zext i8 %on_off to i32
504  %B = add i32 %A, -1
505  %C = sext i32 %B to i64
506  ret i64 %C  ;; Should be (add (zext i8 -> i64), -1)
507}
508
509define i64 @test44(i8 %T) {
510; CHECK-LABEL: @test44(
511; CHECK-NEXT:    [[A:%.*]] = zext i8 %T to i64
512; CHECK-NEXT:    [[B:%.*]] = or i64 [[A]], 1234
513; CHECK-NEXT:    ret i64 [[B]]
514;
515  %A = zext i8 %T to i16
516  %B = or i16 %A, 1234
517  %C = zext i16 %B to i64
518  ret i64 %C
519}
520
521define i64 @test45(i8 %A, i64 %Q) {
522; CHECK-LABEL: @test45(
523; CHECK-NEXT:    [[B:%.*]] = sext i8 %A to i64
524; CHECK-NEXT:    [[C:%.*]] = or i64 [[B]], %Q
525; CHECK-NEXT:    [[E:%.*]] = and i64 [[C]], 4294967295
526; CHECK-NEXT:    ret i64 [[E]]
527;
528  %D = trunc i64 %Q to i32  ;; should be removed
529  %B = sext i8 %A to i32
530  %C = or i32 %B, %D
531  %E = zext i32 %C to i64
532  ret i64 %E
533}
534
535
536define i64 @test46(i64 %A) {
537; CHECK-LABEL: @test46(
538; CHECK-NEXT:    [[C:%.*]] = shl i64 %A, 8
539; CHECK-NEXT:    [[D:%.*]] = and i64 [[C]], 10752
540; CHECK-NEXT:    ret i64 [[D]]
541;
542  %B = trunc i64 %A to i32
543  %C = and i32 %B, 42
544  %D = shl i32 %C, 8
545  %E = zext i32 %D to i64
546  ret i64 %E
547}
548
549define i64 @test47(i8 %A) {
550; CHECK-LABEL: @test47(
551; CHECK-NEXT:    [[B:%.*]] = sext i8 %A to i64
552; CHECK-NEXT:    [[C:%.*]] = and i64 [[B]], 4294967253
553; CHECK-NEXT:    [[E:%.*]] = or i64 [[C]], 42
554; CHECK-NEXT:    ret i64 [[E]]
555;
556  %B = sext i8 %A to i32
557  %C = or i32 %B, 42
558  %E = zext i32 %C to i64
559  ret i64 %E
560}
561
562define i64 @test48(i8 %A1, i8 %a2) {
563; CHECK-LABEL: @test48(
564; CHECK-NEXT:    [[Z2:%.*]] = zext i8 %A1 to i32
565; CHECK-NEXT:    [[C:%.*]] = shl nuw nsw i32 [[Z2]], 8
566; CHECK-NEXT:    [[D:%.*]] = or i32 [[C]], [[Z2]]
567; CHECK-NEXT:    [[E:%.*]] = zext i32 [[D]] to i64
568; CHECK-NEXT:    ret i64 [[E]]
569;
570  %Z1 = zext i8 %a2 to i32
571  %Z2 = zext i8 %A1 to i32
572  %C = shl i32 %Z2, 8
573  %D = or i32 %C, %Z2
574  %E = zext i32 %D to i64
575  ret i64 %E
576}
577
578define i64 @test49(i64 %A) {
579; CHECK-LABEL: @test49(
580; CHECK-NEXT:    [[C:%.*]] = shl i64 %A, 32
581; CHECK-NEXT:    [[SEXT:%.*]] = ashr exact i64 [[C]], 32
582; CHECK-NEXT:    [[D:%.*]] = or i64 [[SEXT]], 1
583; CHECK-NEXT:    ret i64 [[D]]
584;
585  %B = trunc i64 %A to i32
586  %C = or i32 %B, 1
587  %D = sext i32 %C to i64
588  ret i64 %D
589}
590
591define i64 @test50(i64 %A) {
592; CHECK-LABEL: @test50(
593; CHECK-NEXT:    [[A:%.*]] = lshr i64 %A, 2
594; CHECK-NEXT:    [[D:%.*]] = shl i64 [[A]], 32
595; CHECK-NEXT:    [[SEXT:%.*]] = add i64 [[D]], -4294967296
596; CHECK-NEXT:    [[E:%.*]] = ashr exact i64 [[SEXT]], 32
597; CHECK-NEXT:    ret i64 [[E]]
598;
599  %a = lshr i64 %A, 2
600  %B = trunc i64 %a to i32
601  %D = add i32 %B, -1
602  %E = sext i32 %D to i64
603  ret i64 %E
604; lshr+shl will be handled by DAGCombine.
605}
606
607define i64 @test51(i64 %A, i1 %cond) {
608; CHECK-LABEL: @test51(
609; CHECK-NEXT:    [[C:%.*]] = and i64 %A, 4294967294
610; CHECK-NEXT:    [[D:%.*]] = or i64 %A, 1
611; CHECK-NEXT:    [[E:%.*]] = select i1 %cond, i64 [[C]], i64 [[D]]
612; CHECK-NEXT:    [[SEXT:%.*]] = shl i64 [[E]], 32
613; CHECK-NEXT:    [[F:%.*]] = ashr exact i64 [[SEXT]], 32
614; CHECK-NEXT:    ret i64 [[F]]
615;
616  %B = trunc i64 %A to i32
617  %C = and i32 %B, -2
618  %D = or i32 %B, 1
619  %E = select i1 %cond, i32 %C, i32 %D
620  %F = sext i32 %E to i64
621  ret i64 %F
622}
623
624define i32 @test52(i64 %A) {
625; CHECK-LABEL: @test52(
626; CHECK-NEXT:    [[B:%.*]] = trunc i64 %A to i32
627; CHECK-NEXT:    [[C:%.*]] = and i32 [[B]], 7224
628; CHECK-NEXT:    [[D:%.*]] = or i32 [[C]], 32962
629; CHECK-NEXT:    ret i32 [[D]]
630;
631  %B = trunc i64 %A to i16
632  %C = or i16 %B, -32574
633  %D = and i16 %C, -25350
634  %E = zext i16 %D to i32
635  ret i32 %E
636}
637
638define i64 @test53(i32 %A) {
639; CHECK-LABEL: @test53(
640; CHECK-NEXT:    [[B:%.*]] = zext i32 %A to i64
641; CHECK-NEXT:    [[C:%.*]] = and i64 [[B]], 7224
642; CHECK-NEXT:    [[D:%.*]] = or i64 [[C]], 32962
643; CHECK-NEXT:    ret i64 [[D]]
644;
645  %B = trunc i32 %A to i16
646  %C = or i16 %B, -32574
647  %D = and i16 %C, -25350
648  %E = zext i16 %D to i64
649  ret i64 %E
650}
651
652define i32 @test54(i64 %A) {
653; CHECK-LABEL: @test54(
654; CHECK-NEXT:    [[B:%.*]] = trunc i64 %A to i32
655; CHECK-NEXT:    [[C:%.*]] = and i32 [[B]], 7224
656; CHECK-NEXT:    [[D:%.*]] = or i32 [[C]], -32574
657; CHECK-NEXT:    ret i32 [[D]]
658;
659  %B = trunc i64 %A to i16
660  %C = or i16 %B, -32574
661  %D = and i16 %C, -25350
662  %E = sext i16 %D to i32
663  ret i32 %E
664}
665
666define i64 @test55(i32 %A) {
667; CHECK-LABEL: @test55(
668; CHECK-NEXT:    [[B:%.*]] = zext i32 %A to i64
669; CHECK-NEXT:    [[C:%.*]] = and i64 [[B]], 7224
670; CHECK-NEXT:    [[D:%.*]] = or i64 [[C]], -32574
671; CHECK-NEXT:    ret i64 [[D]]
672;
673  %B = trunc i32 %A to i16
674  %C = or i16 %B, -32574
675  %D = and i16 %C, -25350
676  %E = sext i16 %D to i64
677  ret i64 %E
678}
679
680define i64 @test56(i16 %A) nounwind {
681; CHECK-LABEL: @test56(
682; CHECK-NEXT:    [[TMP353:%.*]] = sext i16 %A to i64
683; CHECK-NEXT:    [[TMP354:%.*]] = lshr i64 [[TMP353]], 5
684; CHECK-NEXT:    [[TMP355:%.*]] = and i64 [[TMP354]], 134217727
685; CHECK-NEXT:    ret i64 [[TMP355]]
686;
687  %tmp353 = sext i16 %A to i32
688  %tmp354 = lshr i32 %tmp353, 5
689  %tmp355 = zext i32 %tmp354 to i64
690  ret i64 %tmp355
691}
692
693define i64 @test57(i64 %A) nounwind {
694; CHECK-LABEL: @test57(
695; CHECK-NEXT:    [[C:%.*]] = lshr i64 %A, 8
696; CHECK-NEXT:    [[E:%.*]] = and i64 [[C]], 16777215
697; CHECK-NEXT:    ret i64 [[E]]
698;
699  %B = trunc i64 %A to i32
700  %C = lshr i32 %B, 8
701  %E = zext i32 %C to i64
702  ret i64 %E
703}
704
705define i64 @test58(i64 %A) nounwind {
706; CHECK-LABEL: @test58(
707; CHECK-NEXT:    [[C:%.*]] = lshr i64 %A, 8
708; CHECK-NEXT:    [[D:%.*]] = and i64 [[C]], 16777087
709; CHECK-NEXT:    [[E:%.*]] = or i64 [[D]], 128
710; CHECK-NEXT:    ret i64 [[E]]
711;
712  %B = trunc i64 %A to i32
713  %C = lshr i32 %B, 8
714  %D = or i32 %C, 128
715  %E = zext i32 %D to i64
716  ret i64 %E
717
718}
719
720define i64 @test59(i8 %A, i8 %B) nounwind {
721; CHECK-LABEL: @test59(
722; CHECK-NEXT:    [[C:%.*]] = zext i8 %A to i64
723; CHECK-NEXT:    [[D:%.*]] = shl nuw nsw i64 [[C]], 4
724; CHECK-NEXT:    [[E:%.*]] = and i64 [[D]], 48
725; CHECK-NEXT:    [[F:%.*]] = zext i8 %B to i64
726; CHECK-NEXT:    [[G:%.*]] = lshr i64 [[F]], 4
727; CHECK-NEXT:    [[H:%.*]] = or i64 [[G]], [[E]]
728; CHECK-NEXT:    ret i64 [[H]]
729;
730  %C = zext i8 %A to i32
731  %D = shl i32 %C, 4
732  %E = and i32 %D, 48
733  %F = zext i8 %B to i32
734  %G = lshr i32 %F, 4
735  %H = or i32 %G, %E
736  %I = zext i32 %H to i64
737  ret i64 %I
738}
739
740define <3 x i32> @test60(<4 x i32> %call4) nounwind {
741; CHECK-LABEL: @test60(
742; CHECK-NEXT:    [[TMP10:%.*]] = shufflevector <4 x i32> %call4, <4 x i32> undef, <3 x i32> <i32 0, i32 1, i32 2>
743; CHECK-NEXT:    ret <3 x i32> [[TMP10]]
744;
745  %tmp11 = bitcast <4 x i32> %call4 to i128
746  %tmp9 = trunc i128 %tmp11 to i96
747  %tmp10 = bitcast i96 %tmp9 to <3 x i32>
748  ret <3 x i32> %tmp10
749
750}
751
752define <4 x i32> @test61(<3 x i32> %call4) nounwind {
753; CHECK-LABEL: @test61(
754; CHECK-NEXT:    [[TMP10:%.*]] = shufflevector <3 x i32> %call4, <3 x i32> <i32 0, i32 undef, i32 undef>, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
755; CHECK-NEXT:    ret <4 x i32> [[TMP10]]
756;
757  %tmp11 = bitcast <3 x i32> %call4 to i96
758  %tmp9 = zext i96 %tmp11 to i128
759  %tmp10 = bitcast i128 %tmp9 to <4 x i32>
760  ret <4 x i32> %tmp10
761}
762
763define <4 x i32> @test62(<3 x float> %call4) nounwind {
764; CHECK-LABEL: @test62(
765; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <3 x float> %call4 to <3 x i32>
766; CHECK-NEXT:    [[TMP10:%.*]] = shufflevector <3 x i32> [[TMP1]], <3 x i32> <i32 0, i32 undef, i32 undef>, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
767; CHECK-NEXT:    ret <4 x i32> [[TMP10]]
768;
769  %tmp11 = bitcast <3 x float> %call4 to i96
770  %tmp9 = zext i96 %tmp11 to i128
771  %tmp10 = bitcast i128 %tmp9 to <4 x i32>
772  ret <4 x i32> %tmp10
773}
774
775; PR7311 - Don't create invalid IR on scalar->vector cast.
776define <2 x float> @test63(i64 %tmp8) nounwind {
777; CHECK-LABEL: @test63(
778; CHECK-NEXT:  entry:
779; CHECK-NEXT:    [[A:%.*]] = bitcast i64 %tmp8 to <2 x i32>
780; CHECK-NEXT:    [[VCVT_I:%.*]] = uitofp <2 x i32> [[A]] to <2 x float>
781; CHECK-NEXT:    ret <2 x float> [[VCVT_I]]
782;
783entry:
784  %a = bitcast i64 %tmp8 to <2 x i32>
785  %vcvt.i = uitofp <2 x i32> %a to <2 x float>
786  ret <2 x float> %vcvt.i
787}
788
789define <4 x float> @test64(<4 x float> %c) nounwind {
790; CHECK-LABEL: @test64(
791; CHECK-NEXT:    ret <4 x float> %c
792;
793  %t0 = bitcast <4 x float> %c to <4 x i32>
794  %t1 = bitcast <4 x i32> %t0 to <4 x float>
795  ret <4 x float> %t1
796}
797
798define <4 x float> @test65(<4 x float> %c) nounwind {
799; CHECK-LABEL: @test65(
800; CHECK-NEXT:    ret <4 x float> %c
801;
802  %t0 = bitcast <4 x float> %c to <2 x double>
803  %t1 = bitcast <2 x double> %t0 to <4 x float>
804  ret <4 x float> %t1
805}
806
807define <2 x float> @test66(<2 x float> %c) nounwind {
808; CHECK-LABEL: @test66(
809; CHECK-NEXT:    ret <2 x float> %c
810;
811  %t0 = bitcast <2 x float> %c to double
812  %t1 = bitcast double %t0 to <2 x float>
813  ret <2 x float> %t1
814}
815
816define float @test2c() {
817; CHECK-LABEL: @test2c(
818; CHECK-NEXT:    ret float -1.000000e+00
819;
820  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)
821}
822
823define i64 @test_mmx(<2 x i32> %c) nounwind {
824; CHECK-LABEL: @test_mmx(
825; CHECK-NEXT:    [[C:%.*]] = bitcast <2 x i32> %c to i64
826; CHECK-NEXT:    ret i64 [[C]]
827;
828  %A = bitcast <2 x i32> %c to x86_mmx
829  %B = bitcast x86_mmx %A to <2 x i32>
830  %C = bitcast <2 x i32> %B to i64
831  ret i64 %C
832}
833
834define i64 @test_mmx_const(<2 x i32> %c) nounwind {
835; CHECK-LABEL: @test_mmx_const(
836; CHECK-NEXT:    ret i64 0
837;
838  %A = bitcast <2 x i32> zeroinitializer to x86_mmx
839  %B = bitcast x86_mmx %A to <2 x i32>
840  %C = bitcast <2 x i32> %B to i64
841  ret i64 %C
842}
843
844; PR12514
845define i1 @test67(i1 %a, i32 %b) {
846; CHECK-LABEL: @test67(
847; CHECK-NEXT:    ret i1 false
848;
849  %tmp2 = zext i1 %a to i32
850  %conv6 = xor i32 %tmp2, 1
851  %and = and i32 %b, %conv6
852  %sext = shl nuw nsw i32 %and, 24
853  %neg.i = xor i32 %sext, -16777216
854  %conv.i.i = ashr exact i32 %neg.i, 24
855  %trunc = trunc i32 %conv.i.i to i8
856  %tobool.i = icmp eq i8 %trunc, 0
857  ret i1 %tobool.i
858}
859
860%s = type { i32, i32, i16 }
861
862define %s @test68(%s *%p, i64 %i) {
863; CHECK-LABEL: @test68(
864; CHECK-NEXT:    [[PP1:%.*]] = getelementptr %s, %s* %p, i64 %i
865; CHECK-NEXT:    [[L:%.*]] = load %s, %s* [[PP1]], align 4
866; CHECK-NEXT:    ret %s [[L]]
867;
868  %o = mul i64 %i, 12
869  %q = bitcast %s* %p to i8*
870  %pp = getelementptr inbounds i8, i8* %q, i64 %o
871  %r = bitcast i8* %pp to %s*
872  %l = load %s, %s* %r
873  ret %s %l
874}
875
876; addrspacecasts should be eliminated.
877define %s @test68_addrspacecast(%s* %p, i64 %i) {
878; CHECK-LABEL: @test68_addrspacecast(
879; CHECK-NEXT:    [[PP1:%.*]] = getelementptr %s, %s* %p, i64 %i
880; CHECK-NEXT:    [[L:%.*]] = load %s, %s* [[PP1]], align 4
881; CHECK-NEXT:    ret %s [[L]]
882;
883  %o = mul i64 %i, 12
884  %q = addrspacecast %s* %p to i8 addrspace(2)*
885  %pp = getelementptr inbounds i8, i8 addrspace(2)* %q, i64 %o
886  %r = addrspacecast i8 addrspace(2)* %pp to %s*
887  %l = load %s, %s* %r
888  ret %s %l
889}
890
891define %s @test68_addrspacecast_2(%s* %p, i64 %i) {
892; CHECK-LABEL: @test68_addrspacecast_2(
893; CHECK-NEXT:    [[PP1:%.*]] = getelementptr %s, %s* %p, i64 %i
894; CHECK-NEXT:    [[R:%.*]] = addrspacecast %s* [[PP1]] to %s addrspace(1)*
895; CHECK-NEXT:    [[L:%.*]] = load %s, %s addrspace(1)* [[R]], align 4
896; CHECK-NEXT:    ret %s [[L]]
897;
898  %o = mul i64 %i, 12
899  %q = addrspacecast %s* %p to i8 addrspace(2)*
900  %pp = getelementptr inbounds i8, i8 addrspace(2)* %q, i64 %o
901  %r = addrspacecast i8 addrspace(2)* %pp to %s addrspace(1)*
902  %l = load %s, %s addrspace(1)* %r
903  ret %s %l
904}
905
906define %s @test68_as1(%s addrspace(1)* %p, i32 %i) {
907; CHECK-LABEL: @test68_as1(
908; CHECK-NEXT:    [[PP1:%.*]] = getelementptr %s, %s addrspace(1)* %p, i32 %i
909; CHECK-NEXT:    [[L:%.*]] = load %s, %s addrspace(1)* [[PP1]], align 4
910; CHECK-NEXT:    ret %s [[L]]
911;
912  %o = mul i32 %i, 12
913  %q = bitcast %s addrspace(1)* %p to i8 addrspace(1)*
914  %pp = getelementptr inbounds i8, i8 addrspace(1)* %q, i32 %o
915  %r = bitcast i8 addrspace(1)* %pp to %s addrspace(1)*
916  %l = load %s, %s addrspace(1)* %r
917  ret %s %l
918}
919
920define double @test69(double *%p, i64 %i) {
921; CHECK-LABEL: @test69(
922; CHECK-NEXT:    [[PP1:%.*]] = getelementptr inbounds double, double* %p, i64 %i
923; CHECK-NEXT:    [[L:%.*]] = load double, double* [[PP1]], align 8
924; CHECK-NEXT:    ret double [[L]]
925;
926  %o = shl nsw i64 %i, 3
927  %q = bitcast double* %p to i8*
928  %pp = getelementptr inbounds i8, i8* %q, i64 %o
929  %r = bitcast i8* %pp to double*
930  %l = load double, double* %r
931  ret double %l
932}
933
934define %s @test70(%s *%p, i64 %i) {
935; CHECK-LABEL: @test70(
936; CHECK-NEXT:    [[O:%.*]] = mul nsw i64 %i, 3
937; CHECK-NEXT:    [[PP1:%.*]] = getelementptr inbounds %s, %s* %p, i64 [[O]]
938; CHECK-NEXT:    [[L:%.*]] = load %s, %s* [[PP1]], align 4
939; CHECK-NEXT:    ret %s [[L]]
940;
941  %o = mul nsw i64 %i, 36
942  %q = bitcast %s* %p to i8*
943  %pp = getelementptr inbounds i8, i8* %q, i64 %o
944  %r = bitcast i8* %pp to %s*
945  %l = load %s, %s* %r
946  ret %s %l
947}
948
949define double @test71(double *%p, i64 %i) {
950; CHECK-LABEL: @test71(
951; CHECK-NEXT:    [[O:%.*]] = shl i64 %i, 2
952; CHECK-NEXT:    [[PP1:%.*]] = getelementptr double, double* %p, i64 [[O]]
953; CHECK-NEXT:    [[L:%.*]] = load double, double* [[PP1]], align 8
954; CHECK-NEXT:    ret double [[L]]
955;
956  %o = shl i64 %i, 5
957  %q = bitcast double* %p to i8*
958  %pp = getelementptr i8, i8* %q, i64 %o
959  %r = bitcast i8* %pp to double*
960  %l = load double, double* %r
961  ret double %l
962}
963
964define double @test72(double *%p, i32 %i) {
965; CHECK-LABEL: @test72(
966; CHECK-NEXT:    [[O:%.*]] = sext i32 %i to i64
967; CHECK-NEXT:    [[PP1:%.*]] = getelementptr inbounds double, double* %p, i64 [[O]]
968; CHECK-NEXT:    [[L:%.*]] = load double, double* [[PP1]], align 8
969; CHECK-NEXT:    ret double [[L]]
970;
971  %so = shl nsw i32 %i, 3
972  %o = sext i32 %so to i64
973  %q = bitcast double* %p to i8*
974  %pp = getelementptr inbounds i8, i8* %q, i64 %o
975  %r = bitcast i8* %pp to double*
976  %l = load double, double* %r
977  ret double %l
978}
979
980define double @test73(double *%p, i128 %i) {
981; CHECK-LABEL: @test73(
982; CHECK-NEXT:    [[O:%.*]] = trunc i128 %i to i64
983; CHECK-NEXT:    [[PP1:%.*]] = getelementptr double, double* %p, i64 [[O]]
984; CHECK-NEXT:    [[L:%.*]] = load double, double* [[PP1]], align 8
985; CHECK-NEXT:    ret double [[L]]
986;
987  %lo = shl nsw i128 %i, 3
988  %o = trunc i128 %lo to i64
989  %q = bitcast double* %p to i8*
990  %pp = getelementptr inbounds i8, i8* %q, i64 %o
991  %r = bitcast i8* %pp to double*
992  %l = load double, double* %r
993  ret double %l
994}
995
996define double @test74(double *%p, i64 %i) {
997; CHECK-LABEL: @test74(
998; CHECK-NEXT:    [[PP1:%.*]] = getelementptr inbounds double, double* %p, i64 %i
999; CHECK-NEXT:    [[L:%.*]] = load double, double* [[PP1]], align 8
1000; CHECK-NEXT:    ret double [[L]]
1001;
1002  %q = bitcast double* %p to i64*
1003  %pp = getelementptr inbounds i64, i64* %q, i64 %i
1004  %r = bitcast i64* %pp to double*
1005  %l = load double, double* %r
1006  ret double %l
1007}
1008
1009define i32* @test75(i32* %p, i32 %x) {
1010; CHECK-LABEL: @test75(
1011; CHECK-NEXT:    [[Y:%.*]] = shl i32 %x, 3
1012; CHECK-NEXT:    [[Z:%.*]] = sext i32 [[Y]] to i64
1013; CHECK-NEXT:    [[Q:%.*]] = bitcast i32* %p to i8*
1014; CHECK-NEXT:    [[R:%.*]] = getelementptr i8, i8* [[Q]], i64 [[Z]]
1015; CHECK-NEXT:    [[S:%.*]] = bitcast i8* [[R]] to i32*
1016; CHECK-NEXT:    ret i32* [[S]]
1017;
1018  %y = shl i32 %x, 3
1019  %z = sext i32 %y to i64
1020  %q = bitcast i32* %p to i8*
1021  %r = getelementptr i8, i8* %q, i64 %z
1022  %s = bitcast i8* %r to i32*
1023  ret i32* %s
1024}
1025
1026define %s @test76(%s *%p, i64 %i, i64 %j) {
1027; CHECK-LABEL: @test76(
1028; CHECK-NEXT:    [[O2:%.*]] = mul i64 %i, %j
1029; CHECK-NEXT:    [[PP1:%.*]] = getelementptr %s, %s* %p, i64 [[O2]]
1030; CHECK-NEXT:    [[L:%.*]] = load %s, %s* [[PP1]], align 4
1031; CHECK-NEXT:    ret %s [[L]]
1032;
1033  %o = mul i64 %i, 12
1034  %o2 = mul nsw i64 %o, %j
1035  %q = bitcast %s* %p to i8*
1036  %pp = getelementptr inbounds i8, i8* %q, i64 %o2
1037  %r = bitcast i8* %pp to %s*
1038  %l = load %s, %s* %r
1039  ret %s %l
1040}
1041
1042define %s @test77(%s *%p, i64 %i, i64 %j) {
1043; CHECK-LABEL: @test77(
1044; CHECK-NEXT:    [[O:%.*]] = mul nsw i64 %i, 3
1045; CHECK-NEXT:    [[O2:%.*]] = mul nsw i64 [[O]], %j
1046; CHECK-NEXT:    [[PP1:%.*]] = getelementptr inbounds %s, %s* %p, i64 [[O2]]
1047; CHECK-NEXT:    [[L:%.*]] = load %s, %s* [[PP1]], align 4
1048; CHECK-NEXT:    ret %s [[L]]
1049;
1050  %o = mul nsw i64 %i, 36
1051  %o2 = mul nsw i64 %o, %j
1052  %q = bitcast %s* %p to i8*
1053  %pp = getelementptr inbounds i8, i8* %q, i64 %o2
1054  %r = bitcast i8* %pp to %s*
1055  %l = load %s, %s* %r
1056  ret %s %l
1057}
1058
1059define %s @test78(%s *%p, i64 %i, i64 %j, i32 %k, i32 %l, i128 %m, i128 %n) {
1060; CHECK-LABEL: @test78(
1061; CHECK-NEXT:    [[A:%.*]] = mul nsw i32 %k, 3
1062; CHECK-NEXT:    [[B:%.*]] = mul nsw i32 [[A]], %l
1063; CHECK-NEXT:    [[C:%.*]] = sext i32 [[B]] to i128
1064; CHECK-NEXT:    [[D:%.*]] = mul nsw i128 [[C]], %m
1065; CHECK-NEXT:    [[E:%.*]] = mul i128 [[D]], %n
1066; CHECK-NEXT:    [[F:%.*]] = trunc i128 [[E]] to i64
1067; CHECK-NEXT:    [[G:%.*]] = mul i64 [[F]], %i
1068; CHECK-NEXT:    [[H:%.*]] = mul i64 [[G]], %j
1069; CHECK-NEXT:    [[PP1:%.*]] = getelementptr %s, %s* %p, i64 [[H]]
1070; CHECK-NEXT:    [[LOAD:%.*]] = load %s, %s* [[PP1]], align 4
1071; CHECK-NEXT:    ret %s [[LOAD]]
1072;
1073  %a = mul nsw i32 %k, 36
1074  %b = mul nsw i32 %a, %l
1075  %c = sext i32 %b to i128
1076  %d = mul nsw i128 %c, %m
1077  %e = mul i128 %d, %n
1078  %f = trunc i128 %e to i64
1079  %g = mul nsw i64 %f, %i
1080  %h = mul nsw i64 %g, %j
1081  %q = bitcast %s* %p to i8*
1082  %pp = getelementptr inbounds i8, i8* %q, i64 %h
1083  %r = bitcast i8* %pp to %s*
1084  %load = load %s, %s* %r
1085  ret %s %load
1086}
1087
1088define %s @test79(%s *%p, i64 %i, i32 %j) {
1089; CHECK-LABEL: @test79(
1090; CHECK-NEXT:    [[A:%.*]] = mul nsw i64 %i, 36
1091; CHECK-NEXT:    [[B:%.*]] = trunc i64 [[A]] to i32
1092; CHECK-NEXT:    [[C:%.*]] = mul i32 [[B]], %j
1093; CHECK-NEXT:    [[Q:%.*]] = bitcast %s* %p to i8*
1094; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 [[C]] to i64
1095; CHECK-NEXT:    [[PP:%.*]] = getelementptr inbounds i8, i8* [[Q]], i64 [[TMP1]]
1096; CHECK-NEXT:    [[R:%.*]] = bitcast i8* [[PP]] to %s*
1097; CHECK-NEXT:    [[L:%.*]] = load %s, %s* [[R]], align 4
1098; CHECK-NEXT:    ret %s [[L]]
1099;
1100  %a = mul nsw i64 %i, 36
1101  %b = trunc i64 %a to i32
1102  %c = mul i32 %b, %j
1103  %q = bitcast %s* %p to i8*
1104  %pp = getelementptr inbounds i8, i8* %q, i32 %c
1105  %r = bitcast i8* %pp to %s*
1106  %l = load %s, %s* %r
1107  ret %s %l
1108}
1109
1110define double @test80([100 x double]* %p, i32 %i) {
1111; CHECK-LABEL: @test80(
1112; CHECK-NEXT:    [[TMP1:%.*]] = sext i32 %i to i64
1113; CHECK-NEXT:    [[PP1:%.*]] = getelementptr [100 x double], [100 x double]* %p, i64 0, i64 [[TMP1]]
1114; CHECK-NEXT:    [[L:%.*]] = load double, double* [[PP1]], align 8
1115; CHECK-NEXT:    ret double [[L]]
1116;
1117  %tmp = shl nsw i32 %i, 3
1118  %q = bitcast [100 x double]* %p to i8*
1119  %pp = getelementptr i8, i8* %q, i32 %tmp
1120  %r = bitcast i8* %pp to double*
1121  %l = load double, double* %r
1122  ret double %l
1123}
1124
1125define double @test80_addrspacecast([100 x double] addrspace(1)* %p, i32 %i) {
1126; CHECK-LABEL: @test80_addrspacecast(
1127; CHECK-NEXT:    [[PP1:%.*]] = getelementptr [100 x double], [100 x double] addrspace(1)* %p, i32 0, i32 %i
1128; CHECK-NEXT:    [[L:%.*]] = load double, double addrspace(1)* [[PP1]], align 8
1129; CHECK-NEXT:    ret double [[L]]
1130;
1131  %tmp = shl nsw i32 %i, 3
1132  %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
1133  %pp = getelementptr i8, i8 addrspace(2)* %q, i32 %tmp
1134  %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(1)*
1135  %l = load double, double addrspace(1)* %r
1136  ret double %l
1137}
1138
1139define double @test80_addrspacecast_2([100 x double] addrspace(1)* %p, i32 %i) {
1140; CHECK-LABEL: @test80_addrspacecast_2(
1141; CHECK-NEXT:    [[PP1:%.*]] = getelementptr [100 x double], [100 x double] addrspace(1)* %p, i32 0, i32 %i
1142; CHECK-NEXT:    [[R:%.*]] = addrspacecast double addrspace(1)* [[PP1]] to double addrspace(3)*
1143; CHECK-NEXT:    [[L:%.*]] = load double, double addrspace(3)* [[R]], align 8
1144; CHECK-NEXT:    ret double [[L]]
1145;
1146  %tmp = shl nsw i32 %i, 3
1147  %q = addrspacecast [100 x double] addrspace(1)* %p to i8 addrspace(2)*
1148  %pp = getelementptr i8, i8 addrspace(2)* %q, i32 %tmp
1149  %r = addrspacecast i8 addrspace(2)* %pp to double addrspace(3)*
1150  %l = load double, double addrspace(3)* %r
1151  ret double %l
1152}
1153
1154define double @test80_as1([100 x double] addrspace(1)* %p, i16 %i) {
1155; CHECK-LABEL: @test80_as1(
1156; CHECK-NEXT:    [[TMP1:%.*]] = sext i16 %i to i32
1157; CHECK-NEXT:    [[PP1:%.*]] = getelementptr [100 x double], [100 x double] addrspace(1)* %p, i32 0, i32 [[TMP1]]
1158; CHECK-NEXT:    [[L:%.*]] = load double, double addrspace(1)* [[PP1]], align 8
1159; CHECK-NEXT:    ret double [[L]]
1160;
1161  %tmp = shl nsw i16 %i, 3
1162  %q = bitcast [100 x double] addrspace(1)* %p to i8 addrspace(1)*
1163  %pp = getelementptr i8, i8 addrspace(1)* %q, i16 %tmp
1164  %r = bitcast i8 addrspace(1)* %pp to double addrspace(1)*
1165  %l = load double, double addrspace(1)* %r
1166  ret double %l
1167}
1168
1169define double @test81(double *%p, float %f) {
1170; CHECK-LABEL: @test81(
1171; CHECK-NEXT:    [[I:%.*]] = fptosi float %f to i64
1172; CHECK-NEXT:    [[Q:%.*]] = bitcast double* %p to i8*
1173; CHECK-NEXT:    [[PP:%.*]] = getelementptr i8, i8* [[Q]], i64 [[I]]
1174; CHECK-NEXT:    [[R:%.*]] = bitcast i8* [[PP]] to double*
1175; CHECK-NEXT:    [[L:%.*]] = load double, double* [[R]], align 8
1176; CHECK-NEXT:    ret double [[L]]
1177;
1178  %i = fptosi float %f to i64
1179  %q = bitcast double* %p to i8*
1180  %pp = getelementptr i8, i8* %q, i64 %i
1181  %r = bitcast i8* %pp to double*
1182  %l = load double, double* %r
1183  ret double %l
1184}
1185
1186define i64 @test82(i64 %A) nounwind {
1187; CHECK-LABEL: @test82(
1188; CHECK-NEXT:    [[TMP1:%.*]] = shl i64 %A, 1
1189; CHECK-NEXT:    [[E:%.*]] = and i64 [[TMP1]], 4294966784
1190; CHECK-NEXT:    ret i64 [[E]]
1191;
1192  %B = trunc i64 %A to i32
1193  %C = lshr i32 %B, 8
1194  %D = shl i32 %C, 9
1195  %E = zext i32 %D to i64
1196  ret i64 %E
1197}
1198
1199; PR15959
1200define i64 @test83(i16 %a, i64 %k) {
1201; CHECK-LABEL: @test83(
1202; CHECK-NEXT:    [[CONV:%.*]] = sext i16 %a to i32
1203; CHECK-NEXT:    [[SUB:%.*]] = add i64 %k, 4294967295
1204; CHECK-NEXT:    [[SH_PROM:%.*]] = trunc i64 [[SUB]] to i32
1205; CHECK-NEXT:    [[SHL:%.*]] = shl i32 [[CONV]], [[SH_PROM]]
1206; CHECK-NEXT:    [[SH_PROM1:%.*]] = zext i32 [[SHL]] to i64
1207; CHECK-NEXT:    ret i64 [[SH_PROM1]]
1208;
1209  %conv = sext i16 %a to i32
1210  %sub = add nsw i64 %k, -1
1211  %sh_prom = trunc i64 %sub to i32
1212  %shl = shl i32 %conv, %sh_prom
1213  %sh_prom1 = zext i32 %shl to i64
1214  ret i64 %sh_prom1
1215}
1216
1217define i8 @test84(i32 %a) {
1218; CHECK-LABEL: @test84(
1219; CHECK-NEXT:    [[ADD:%.*]] = add i32 %a, 2130706432
1220; CHECK-NEXT:    [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
1221; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SHR]] to i8
1222; CHECK-NEXT:    ret i8 [[TRUNC]]
1223;
1224  %add = add nsw i32 %a, -16777216
1225  %shr = lshr exact i32 %add, 23
1226  %trunc = trunc i32 %shr to i8
1227  ret i8 %trunc
1228}
1229
1230define i8 @test85(i32 %a) {
1231; CHECK-LABEL: @test85(
1232; CHECK-NEXT:    [[ADD:%.*]] = add i32 %a, 2130706432
1233; CHECK-NEXT:    [[SHR:%.*]] = lshr exact i32 [[ADD]], 23
1234; CHECK-NEXT:    [[TRUNC:%.*]] = trunc i32 [[SHR]] to i8
1235; CHECK-NEXT:    ret i8 [[TRUNC]]
1236;
1237  %add = add nuw i32 %a, -16777216
1238  %shr = lshr exact i32 %add, 23
1239  %trunc = trunc i32 %shr to i8
1240  ret i8 %trunc
1241}
1242
1243define i16 @test86(i16 %v) {
1244; CHECK-LABEL: @test86(
1245; CHECK-NEXT:    [[S1:%.*]] = ashr i16 %v, 4
1246; CHECK-NEXT:    ret i16 [[S1]]
1247;
1248  %a = sext i16 %v to i32
1249  %s = ashr i32 %a, 4
1250  %t = trunc i32 %s to i16
1251  ret i16 %t
1252}
1253
1254define i16 @test87(i16 %v) {
1255; CHECK-LABEL: @test87(
1256; CHECK-NEXT:    [[A1:%.*]] = ashr i16 %v, 12
1257; CHECK-NEXT:    ret i16 [[A1]]
1258;
1259  %c = sext i16 %v to i32
1260  %m = mul nsw i32 %c, 16
1261  %a = ashr i32 %m, 16
1262  %t = trunc i32 %a to i16
1263  ret i16 %t
1264}
1265
1266; Do not optimize to ashr i16 (shift by 18)
1267define i16 @test88(i16 %v) {
1268; CHECK-LABEL: @test88(
1269; CHECK-NEXT:    [[A:%.*]] = sext i16 %v to i32
1270; CHECK-NEXT:    [[S:%.*]] = ashr i32 [[A]], 18
1271; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[S]] to i16
1272; CHECK-NEXT:    ret i16 [[T]]
1273;
1274  %a = sext i16 %v to i32
1275  %s = ashr i32 %a, 18
1276  %t = trunc i32 %s to i16
1277  ret i16 %t
1278}
1279
1280; Overflow on a float to int or int to float conversion is undefined (PR21130).
1281
1282define i8 @overflow_fptosi() {
1283; CHECK-LABEL: @overflow_fptosi(
1284; CHECK-NEXT:    ret i8 undef
1285;
1286  %i = fptosi double 1.56e+02 to i8
1287  ret i8 %i
1288}
1289
1290define i8 @overflow_fptoui() {
1291; CHECK-LABEL: @overflow_fptoui(
1292; CHECK-NEXT:    ret i8 undef
1293;
1294  %i = fptoui double 2.56e+02 to i8
1295  ret i8 %i
1296}
1297
1298; The maximum float is approximately 2 ** 128 which is 3.4E38.
1299; The constant below is 4E38. Use a 130 bit integer to hold that
1300; number; 129-bits for the value + 1 bit for the sign.
1301define float @overflow_uitofp() {
1302; CHECK-LABEL: @overflow_uitofp(
1303; CHECK-NEXT:    ret float undef
1304;
1305  %i = uitofp i130 400000000000000000000000000000000000000 to float
1306  ret float %i
1307}
1308
1309define float @overflow_sitofp() {
1310; CHECK-LABEL: @overflow_sitofp(
1311; CHECK-NEXT:    ret float undef
1312;
1313  %i = sitofp i130 400000000000000000000000000000000000000 to float
1314  ret float %i
1315}
1316
1317define i32 @PR21388(i32* %v) {
1318; CHECK-LABEL: @PR21388(
1319; CHECK-NEXT:    [[ICMP:%.*]] = icmp slt i32* %v, null
1320; CHECK-NEXT:    [[SEXT:%.*]] = sext i1 [[ICMP]] to i32
1321; CHECK-NEXT:    ret i32 [[SEXT]]
1322;
1323  %icmp = icmp slt i32* %v, null
1324  %sext = sext i1 %icmp to i32
1325  ret i32 %sext
1326}
1327
1328define float @sitofp_zext(i16 %a) {
1329; CHECK-LABEL: @sitofp_zext(
1330; CHECK-NEXT:    [[SITOFP:%.*]] = uitofp i16 %a to float
1331; CHECK-NEXT:    ret float [[SITOFP]]
1332;
1333  %zext = zext i16 %a to i32
1334  %sitofp = sitofp i32 %zext to float
1335  ret float %sitofp
1336}
1337
1338define i1 @PR23309(i32 %A, i32 %B) {
1339; CHECK-LABEL: @PR23309(
1340; CHECK-NEXT:    [[SUB:%.*]] = sub i32 %A, %B
1341; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[SUB]], 1
1342; CHECK-NEXT:    [[TRUNC:%.*]] = icmp ne i32 [[TMP1]], 0
1343; CHECK-NEXT:    ret i1 [[TRUNC]]
1344;
1345  %add = add i32 %A, -4
1346  %sub = sub nsw i32 %add, %B
1347  %trunc = trunc i32 %sub to i1
1348  ret i1 %trunc
1349}
1350
1351define i1 @PR23309v2(i32 %A, i32 %B) {
1352; CHECK-LABEL: @PR23309v2(
1353; CHECK-NEXT:    [[SUB:%.*]] = add i32 %A, %B
1354; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[SUB]], 1
1355; CHECK-NEXT:    [[TRUNC:%.*]] = icmp ne i32 [[TMP1]], 0
1356; CHECK-NEXT:    ret i1 [[TRUNC]]
1357;
1358  %add = add i32 %A, -4
1359  %sub = add nuw i32 %add, %B
1360  %trunc = trunc i32 %sub to i1
1361  ret i1 %trunc
1362}
1363
1364define i16 @PR24763(i8 %V) {
1365; CHECK-LABEL: @PR24763(
1366; CHECK-NEXT:    [[L:%.*]] = ashr i8 %V, 1
1367; CHECK-NEXT:    [[T:%.*]] = sext i8 [[L]] to i16
1368; CHECK-NEXT:    ret i16 [[T]]
1369;
1370  %conv = sext i8 %V to i32
1371  %l = lshr i32 %conv, 1
1372  %t = trunc i32 %l to i16
1373  ret i16 %t
1374}
1375