new testcases
[libfirm] / ir / be / test / localopts.c
1 /*$ -fno-inline $*/
2 #include <stdio.h>
3
4 #define CONST 42
5
6 int mul0(int x)
7 {
8         return -x * CONST;
9 }
10
11 int mul1(int x, int y)
12 {
13         return -x * -y;
14 }
15
16 int mul2(int x, int y, int z)
17 {
18         return -x * (y - z);
19 }
20
21 int mul3(int x, int y, int z)
22 {
23         return (x - y) * z;
24 }
25
26 int sub0(int x, int y, int z)
27 {
28         return x - (y - z);
29 }
30
31 int sub1(int x, int y)
32 {
33         return x - (y * CONST);
34 }
35
36 int sub2(int x, int y)
37 {
38         return x - -y;
39 }
40
41 int sub3(int x, int y)
42 {
43         return -x - y;
44 }
45
46 int sub4(int x) {
47         return 6 - ~x;
48 }
49
50 int cmp1(int x, int y) {
51         return -x == -y;
52 }
53
54 int cmp2(int x, int y) {
55         return -x != -y;
56 }
57
58 int cmp3(int x, int y) {
59         return ~x == ~y;
60 }
61
62 int cmp4(int x, int y) {
63         return ~x != ~y;
64 }
65
66 int cmp5(int x, int y, int z) {
67         return x + z == z + y;
68 }
69
70 int cmp6(int x, int y, int z) {
71         return x + z != y + z;
72 }
73
74 int cmp7(int x, int y, int z) {
75         return x - z == y - z;
76 }
77
78 int cmp8(int x, int y, int z) {
79         return z -x != z - y;
80 }
81
82 int cmp9(int x) {
83         return -x == 3;
84 }
85
86 int cmp10(int x) {
87         return -x != 3;
88 }
89
90 int cmp11(int x, int y) {
91         return x - y != x;
92 }
93
94 int cmp12(int x, int y) {
95         return x + y == x && y + x == y;
96 }
97
98 int and1(int a, int b) {
99         return (a|b)&a;
100 }
101
102 int and2(int a, int b) {
103         return (a|b) & ~(a&b);
104 }
105
106 int and3(int a) {
107         return (a & 2) == 2;
108 }
109
110 int and4(int a) {
111         return (a & 2) == 4;
112 }
113
114 int and5(int a) {
115         return (a & 2) != 4;
116 }
117
118 int or1(int a) {
119         return (a | 2) != 0;
120 }
121
122 int or2(int a) {
123         return (a | 7) == 0;
124 }
125
126 int add1(int x) {
127         return x + ~x;
128 }
129
130 int shr1(int x) {
131         return -(x >> 31);
132 }
133
134 int shrs1(unsigned x) {
135         return -(x >> 31);
136 }
137
138 int demorgan1(int a, int b) {
139         return (~a) & (~b);
140 }
141
142 int demorgan2(int a, int b) {
143         return (~a) | (~b);
144 }
145
146 int eor1(int a, int b) {
147         return a & (a ^ b);
148 }
149
150 int shl1(int a) {
151         return (a << 3) == (5<<3);
152 }
153
154 int shl2(int a) {
155         return (a << 3) == 41;
156 }
157
158 int shr2(unsigned int a) {
159         return (a >> 3) == 5;
160 }
161
162 int shr3(unsigned int a) {
163         return (a >> 3) == (1 << 29);
164 }
165
166 int shrs2(int a) {
167         return (a >> 3) == 5;
168 }
169
170 int shrs3(int a) {
171         return (a >> 3) == -5;
172 }
173
174 int shrs4(int a) {
175         return (a >> 3) == (1 << 29);
176 }
177
178 int conv1(signed char a) {
179         return (int)a < 0;
180 }
181
182 int conv2(unsigned char a) {
183         return (int)a > 0;
184 }
185
186 int conv3(signed char a) {
187         return (unsigned)a != 0;
188 }
189
190 int main(void)
191 {
192 #define TU(func,x,expect) \
193         printf("%s(%d) = %d (should be %d)\n", #func, x, func(x), expect);
194 #define TB(func,x,y,expect) \
195         printf("%s(%d,%d) = %d (should be %d)\n", #func, x, y, func(x,y), expect);
196 #define TT(func,x,y,z,expect) \
197         printf("%s(%d,%d,%d) = %d (should be %d)\n", #func, x, y, z, func(x,y,z), expect);
198
199         TU(mul0, 3, -126);
200         TB(mul1, 20, 3, 60);
201         TT(mul2, 9, 2, 5, 27);
202         TT(mul3, 5, 2, 9, 27);
203         TT(sub0, 42, 17, 59, 84);
204         TB(sub1, 23, 17, -691);
205         TB(sub2, 42, 17, 59);
206         TB(sub3, 42, 17, -59);
207         TU(sub4, 42, 49);
208         TB(cmp1, 42, 17, 0);
209         TB(cmp2, 42, 17, 1);
210         TB(cmp3, 42, 17, 0);
211         TB(cmp4, 42, 17, 1);
212         TT(cmp5, 42, 17, -4, 0);
213         TT(cmp6, 42, 17, -4, 1);
214         TT(cmp7, 42, 17, -4, 0);
215         TT(cmp8, 42, 17, -4, 1);
216         TU(cmp9, -3, 1);
217         TU(cmp10, -3, 0);
218         TB(cmp11, 5, 5, 1);
219         TB(cmp11, 42, 0, 0);
220         TB(cmp12, 0, 0, 1);
221         TB(cmp12, 42, 5, 0);
222         TB(cmp12, 5, 5, 0);
223         TB(and1, 42, 17, 42);
224         TB(and2, 42, 17, 42^17);
225         TU(and3, 34, 1);
226         TU(add1, -3, -1);
227         TU(shr1, -3, 1);
228         TU(shrs1, -3, -1);
229         TB(demorgan1, 42, 17, ~(42|17));
230         TB(demorgan2, 42, 17, ~(42&17));
231         TB(eor1, 42, 44, 42&~44);
232         TU(shl1, 5, 1);
233         TU(shl1, 6, 0);
234         TU(shl2, 5, 0);
235         TU(shr2, 5<<3, 1);
236         TU(shr2, 6<<3, 0);
237         TU(shr3, 5, 0);
238         TU(shrs2, 5<<3, 1);
239         TU(shrs2, 6<<3, 0);
240         TU(shrs3, -5<<3, 1);
241         TU(shrs3, -6<<3, 0);
242         TU(shrs4, 5, 0);
243         TU(conv1, 3, 0);
244         TU(conv2, 3, 1);
245         TU(conv3, 3, 1);
246         TU(and4, 7, 0);
247         TU(and5, 7, 1);
248         TU(or1, 7, 1);
249         TU(or2, 7, 0);
250 }