Fix InstCombine/2007-10-31-StringCrash.ll by removing an obvious
[oota-llvm.git] / test / Transforms / InstCombine / cast.ll
1 ; Tests to make sure elimination of casts is working correctly
2 ; RUN: llvm-upgrade < %s | llvm-as | opt -instcombine | llvm-dis | \
3 ; RUN:    grep %c | notcast
4 ; END.
5
6 %inbuf = external global [32832 x ubyte]
7
8 implementation
9
10 int %test1(int %A) {
11         %c1 = cast int %A to uint
12         %c2 = cast uint %c1 to int
13         ret int %c2
14 }
15
16 ulong %test2(ubyte %A) {
17         %c1 = cast ubyte %A to ushort
18         %c2 = cast ushort %c1 to uint
19         %Ret = cast uint %c2 to ulong
20         ret ulong %Ret
21 }
22
23 ulong %test3(ulong %A) {    ; This function should just use bitwise AND
24         %c1 = cast ulong %A to ubyte
25         %c2 = cast ubyte %c1 to ulong
26         ret ulong %c2
27 }
28
29 uint %test4(int %A, int %B) {
30         %COND = setlt int %A, %B
31         %c = cast bool %COND to ubyte     ; Booleans are unsigned integrals
32         %result = cast ubyte %c to uint   ; for the cast elim purpose
33         ret uint %result
34 }
35
36 int %test5(bool %B) {
37         %c = cast bool %B to ubyte       ; This cast should get folded into
38         %result = cast ubyte %c to int   ; this cast
39         ret int %result
40 }
41
42 int %test6(ulong %A) {
43         %c1 = cast ulong %A to uint
44         %res = cast uint %c1 to int
45         ret int %res
46 }
47
48 long %test7(bool %A) {
49         %c1 = cast bool %A to int
50         %res = cast int %c1 to long
51         ret long %res
52 }
53
54 long %test8(sbyte %A) {
55         %c1 = cast sbyte %A to ulong
56         %res = cast ulong %c1 to long
57         ret long %res
58 }
59
60 short %test9(short %A) {
61         %c1 = cast short %A to int
62         %c2 = cast int %c1 to short
63         ret short %c2
64 }
65
66 short %test10(short %A) {
67         %c1 = cast short %A to uint
68         %c2 = cast uint %c1 to short
69         ret short %c2
70 }
71
72 declare void %varargs(int, ...)
73
74 void %test11(int* %P) {
75         %c = cast int* %P to short*
76         call void(int, ...)* %varargs(int 5, short* %c)
77         ret void
78 }
79
80 int* %test12() {
81         %p = malloc [4 x sbyte]
82         %c = cast [4 x sbyte]* %p to int*
83         ret int* %c
84 }
85
86 ubyte *%test13(long %A) {
87         %c = getelementptr [0 x ubyte]* cast ([32832 x ubyte]*  %inbuf to [0 x ubyte]*), long 0, long %A
88         ret ubyte* %c
89 }
90
91 bool %test14(sbyte %A) {
92         %c = cast sbyte %A to ubyte
93         %X = setlt ubyte %c, 128   ; setge %A, 0
94         ret bool %X
95 }
96
97 ; This just won't occur when there's no difference between ubyte and sbyte
98 ;bool %test15(ubyte %A) {
99 ;        %c = cast ubyte %A to sbyte
100 ;        %X = setlt sbyte %c, 0   ; setgt %A, 127
101 ;        ret bool %X
102 ;}
103
104 bool %test16(int* %P) {
105         %c = cast int* %P to bool  ;; setne P, null
106         ret bool %c
107 }
108
109 short %test17(bool %tmp3) {
110         %c = cast bool %tmp3 to int
111         %t86 = cast int %c to short
112         ret short %t86
113 }
114
115 short %test18(sbyte %tmp3) {
116         %c = cast sbyte %tmp3 to int
117         %t86 = cast int %c to short
118         ret short %t86
119 }
120
121 bool %test19(int %X) {
122         %c = cast int %X to long
123         %Z = setlt long %c, 12345
124         ret bool %Z
125 }
126
127 bool %test20(bool %B) {
128         %c = cast bool %B to int
129         %D = setlt int %c, -1
130         ret bool %D                ;; false
131 }
132
133 uint %test21(uint %X) {
134         %c1 = cast uint %X to sbyte
135         %c2 = cast sbyte %c1 to uint ;; sext -> zext -> and -> nop
136         %RV = and uint %c2, 255
137         ret uint %RV
138 }
139
140 uint %test22(uint %X) {
141         %c1 = cast uint %X to sbyte
142         %c2 = cast sbyte %c1 to uint ;; sext -> zext -> and -> nop
143         %RV = shl uint %c2, ubyte 24
144         ret uint %RV
145 }
146
147 int %test23(int %X) {
148         %c1 = cast int %X to ushort  ;; Turn into an AND even though X
149         %c2 = cast ushort %c1 to int  ;; and Z are signed.
150         ret int %c2
151 }
152
153 bool %test24(bool %C) {
154         %X = select bool %C, uint 14, uint 1234
155         %c = cast uint %X to bool                  ;; Fold cast into select
156         ret bool %c
157 }
158
159 void %test25(int** %P) {
160         %c = cast int** %P to float**
161         store float* null, float** %c          ;; Fold cast into null
162         ret void
163 }
164
165 int %test26(float %F) {
166         %c = cast float %F to double   ;; no need to cast from float->double.
167         %D = cast double %c to int
168         ret int %D
169 }
170
171 [4 x float]* %test27([9 x [4 x float]]* %A) {
172         %c = cast [9 x [4 x float]]* %A to [4 x float]*
173         ret [4 x float]* %c
174 }
175
176 float* %test28([4 x float]* %A) {
177         %c = cast [4 x float]* %A to float*
178         ret float* %c
179 }
180
181 uint %test29(uint %c1, uint %c2) {
182         %tmp1 = cast uint %c1 to ubyte
183         %tmp4.mask = cast uint %c2 to ubyte
184         %tmp = or ubyte %tmp4.mask, %tmp1
185         %tmp10 = cast ubyte %tmp to uint
186         ret uint %tmp10
187 }
188
189 uint %test30(uint %c1) {
190         %c2 = cast uint %c1 to ubyte
191         %c3 = xor ubyte %c2, 1     
192         %c4 = cast ubyte %c3 to uint
193         ret uint %c4
194 }
195
196 bool %test31(ulong %A) {
197         %B = cast ulong %A to int
198         %C = and int %B, 42
199         %D = seteq int %C, 10
200         ret bool %D
201 }
202
203
204 void %test32(double** %tmp) {
205         %tmp8 = malloc [16 x sbyte]
206         %tmp8 = cast [16 x sbyte]* %tmp8 to double*
207         store double* %tmp8, double** %tmp
208         ret void
209 }
210
211 uint %test33(uint %c1) {
212         %x = bitcast uint %c1 to float 
213         %y = bitcast float %x to uint
214         ret uint %y
215 }
216
217 ushort %test34(ushort %a) {
218         %c1 = zext ushort %a to int
219         %tmp21 = lshr int %c1, ubyte 8
220         %c2 = trunc int %tmp21 to ushort
221         ret ushort %c2
222 }
223
224 ushort %test35(ushort %a) {
225         %c1 = bitcast ushort %a to short
226         %tmp2 = lshr short %c1, ubyte 8
227         %c2 = bitcast short %tmp2 to ushort
228         ret ushort %c2
229 }
230