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