1 ; Tests to make sure elimination of casts is working correctly
2 ; RUN: opt < %s -instcombine -S | grep %c | notcast
4 @inbuf = external global [32832 x i8] ; <[32832 x i8]*> [#uses=1]
6 define i32 @test1(i32 %A) {
7 %c1 = bitcast i32 %A to i32 ; <i32> [#uses=1]
8 %c2 = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
12 define i64 @test2(i8 %A) {
13 %c1 = zext i8 %A to i16 ; <i16> [#uses=1]
14 %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
15 %Ret = zext i32 %c2 to i64 ; <i64> [#uses=1]
19 ; This function should just use bitwise AND
20 define i64 @test3(i64 %A) {
21 %c1 = trunc i64 %A to i8 ; <i8> [#uses=1]
22 %c2 = zext i8 %c1 to i64 ; <i64> [#uses=1]
26 define i32 @test4(i32 %A, i32 %B) {
27 %COND = icmp slt i32 %A, %B ; <i1> [#uses=1]
28 ; Booleans are unsigned integrals
29 %c = zext i1 %COND to i8 ; <i8> [#uses=1]
30 ; for the cast elim purpose
31 %result = zext i8 %c to i32 ; <i32> [#uses=1]
35 define i32 @test5(i1 %B) {
36 ; This cast should get folded into
37 %c = zext i1 %B to i8 ; <i8> [#uses=1]
39 %result = zext i8 %c to i32 ; <i32> [#uses=1]
43 define i32 @test6(i64 %A) {
44 %c1 = trunc i64 %A to i32 ; <i32> [#uses=1]
45 %res = bitcast i32 %c1 to i32 ; <i32> [#uses=1]
49 define i64 @test7(i1 %A) {
50 %c1 = zext i1 %A to i32 ; <i32> [#uses=1]
51 %res = sext i32 %c1 to i64 ; <i64> [#uses=1]
55 define i64 @test8(i8 %A) {
56 %c1 = sext i8 %A to i64 ; <i64> [#uses=1]
57 %res = bitcast i64 %c1 to i64 ; <i64> [#uses=1]
61 define i16 @test9(i16 %A) {
62 %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
63 %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
67 define i16 @test10(i16 %A) {
68 %c1 = sext i16 %A to i32 ; <i32> [#uses=1]
69 %c2 = trunc i32 %c1 to i16 ; <i16> [#uses=1]
73 declare void @varargs(i32, ...)
75 define void @test11(i32* %P) {
76 %c = bitcast i32* %P to i16* ; <i16*> [#uses=1]
77 call void (i32, ...)* @varargs( i32 5, i16* %c )
81 define i32* @test12() {
82 %p = malloc [4 x i8] ; <[4 x i8]*> [#uses=1]
83 %c = bitcast [4 x i8]* %p to i32* ; <i32*> [#uses=1]
86 define i8* @test13(i64 %A) {
87 %c = getelementptr [0 x i8]* bitcast ([32832 x i8]* @inbuf to [0 x i8]*), i64 0, i64 %A ; <i8*> [#uses=1]
91 define i1 @test14(i8 %A) {
92 %c = bitcast i8 %A to i8 ; <i8> [#uses=1]
93 %X = icmp ult i8 %c, -128 ; <i1> [#uses=1]
98 ; This just won't occur when there's no difference between ubyte and sbyte
99 ;bool %test15(ubyte %A) {
100 ; %c = cast ubyte %A to sbyte
101 ; %X = setlt sbyte %c, 0 ; setgt %A, 127
105 define i1 @test16(i32* %P) {
106 %c = icmp ne i32* %P, null ; <i1> [#uses=1]
110 define i16 @test17(i1 %tmp3) {
111 %c = zext i1 %tmp3 to i32 ; <i32> [#uses=1]
112 %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
116 define i16 @test18(i8 %tmp3) {
117 %c = sext i8 %tmp3 to i32 ; <i32> [#uses=1]
118 %t86 = trunc i32 %c to i16 ; <i16> [#uses=1]
122 define i1 @test19(i32 %X) {
123 %c = sext i32 %X to i64 ; <i64> [#uses=1]
124 %Z = icmp slt i64 %c, 12345 ; <i1> [#uses=1]
128 define i1 @test20(i1 %B) {
129 %c = zext i1 %B to i32 ; <i32> [#uses=1]
130 %D = icmp slt i32 %c, -1 ; <i1> [#uses=1]
135 define i32 @test21(i32 %X) {
136 %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
137 ;; sext -> zext -> and -> nop
138 %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
139 %RV = and i32 %c2, 255 ; <i32> [#uses=1]
143 define i32 @test22(i32 %X) {
144 %c1 = trunc i32 %X to i8 ; <i8> [#uses=1]
145 ;; sext -> zext -> and -> nop
146 %c2 = sext i8 %c1 to i32 ; <i32> [#uses=1]
147 %RV = shl i32 %c2, 24 ; <i32> [#uses=1]
151 define i32 @test23(i32 %X) {
152 ;; Turn into an AND even though X
153 %c1 = trunc i32 %X to i16 ; <i16> [#uses=1]
155 %c2 = zext i16 %c1 to i32 ; <i32> [#uses=1]
159 define i1 @test24(i1 %C) {
160 %X = select i1 %C, i32 14, i32 1234 ; <i32> [#uses=1]
161 ;; Fold cast into select
162 %c = icmp ne i32 %X, 0 ; <i1> [#uses=1]
166 define void @test25(i32** %P) {
167 %c = bitcast i32** %P to float** ; <float**> [#uses=1]
168 ;; Fold cast into null
169 store float* null, float** %c
173 define i32 @test26(float %F) {
174 ;; no need to cast from float->double.
175 %c = fpext float %F to double ; <double> [#uses=1]
176 %D = fptosi double %c to i32 ; <i32> [#uses=1]
180 define [4 x float]* @test27([9 x [4 x float]]* %A) {
181 %c = bitcast [9 x [4 x float]]* %A to [4 x float]* ; <[4 x float]*> [#uses=1]
185 define float* @test28([4 x float]* %A) {
186 %c = bitcast [4 x float]* %A to float* ; <float*> [#uses=1]
190 define i32 @test29(i32 %c1, i32 %c2) {
191 %tmp1 = trunc i32 %c1 to i8 ; <i8> [#uses=1]
192 %tmp4.mask = trunc i32 %c2 to i8 ; <i8> [#uses=1]
193 %tmp = or i8 %tmp4.mask, %tmp1 ; <i8> [#uses=1]
194 %tmp10 = zext i8 %tmp to i32 ; <i32> [#uses=1]
198 define i32 @test30(i32 %c1) {
199 %c2 = trunc i32 %c1 to i8 ; <i8> [#uses=1]
200 %c3 = xor i8 %c2, 1 ; <i8> [#uses=1]
201 %c4 = zext i8 %c3 to i32 ; <i32> [#uses=1]
205 define i1 @test31(i64 %A) {
206 %B = trunc i64 %A to i32 ; <i32> [#uses=1]
207 %C = and i32 %B, 42 ; <i32> [#uses=1]
208 %D = icmp eq i32 %C, 10 ; <i1> [#uses=1]
212 define void @test32(double** %tmp) {
213 %tmp8 = malloc [16 x i8] ; <[16 x i8]*> [#uses=1]
214 %tmp8.upgrd.1 = bitcast [16 x i8]* %tmp8 to double* ; <double*> [#uses=1]
215 store double* %tmp8.upgrd.1, double** %tmp
219 define i32 @test33(i32 %c1) {
220 %x = bitcast i32 %c1 to float ; <float> [#uses=1]
221 %y = bitcast float %x to i32 ; <i32> [#uses=1]
225 define i16 @test34(i16 %a) {
226 %c1 = zext i16 %a to i32 ; <i32> [#uses=1]
227 %tmp21 = lshr i32 %c1, 8 ; <i32> [#uses=1]
228 %c2 = trunc i32 %tmp21 to i16 ; <i16> [#uses=1]
232 define i16 @test35(i16 %a) {
233 %c1 = bitcast i16 %a to i16 ; <i16> [#uses=1]
234 %tmp2 = lshr i16 %c1, 8 ; <i16> [#uses=1]
235 %c2 = bitcast i16 %tmp2 to i16 ; <i16> [#uses=1]
239 ; icmp sgt i32 %a, -1
241 define i1 @test36(i32 %a) {
243 %c = trunc i32 %b to i8
244 %d = icmp eq i8 %c, 0
249 define i1 @test37(i32 %a) {
252 %d = trunc i32 %c to i8
253 %e = icmp eq i8 %d, 11
257 define i64 @test38(i32 %a) {
258 %1 = icmp eq i32 %a, -2
259 %2 = zext i1 %1 to i8
261 %4 = zext i8 %3 to i64