Comprehensive test of FE handling of __sync builtins.
[oota-llvm.git] / test / FrontendC / Atomics.c
1 // Test frontend handling of __sync builtins.
2 // Modified from a gcc testcase.
3 // RUN: %llvmgcc -S %s -o - | grep atomic | count 242
4 // RUN: %llvmgcc -S %s -o - | grep p0i8 | count 50
5 // RUN: %llvmgcc -S %s -o - | grep p0i16 | count 50
6 // RUN: %llvmgcc -S %s -o - | grep p0i32 | count 92
7 // RUN: %llvmgcc -S %s -o - | grep volatile | count 10
8
9 // Currently this is implemented only for Alpha, X86, PowerPC.
10 // Add your target here if it doesn't work.
11 // XFAIL: sparc|arm|ia64
12
13 signed char sc;
14 unsigned char uc;
15 signed short ss;
16 unsigned short us;
17 signed int si;
18 unsigned int ui;
19 signed long sl;
20 unsigned long ul;
21 signed long long sll;
22 unsigned long long ull;
23
24 void test_op_ignore (void)
25 {
26   (void) __sync_fetch_and_add (&sc, 1);
27   (void) __sync_fetch_and_add (&uc, 1);
28   (void) __sync_fetch_and_add (&ss, 1);
29   (void) __sync_fetch_and_add (&us, 1);
30   (void) __sync_fetch_and_add (&si, 1);
31   (void) __sync_fetch_and_add (&ui, 1);
32   (void) __sync_fetch_and_add (&sl, 1);
33   (void) __sync_fetch_and_add (&ul, 1);
34   (void) __sync_fetch_and_add (&sll, 1);
35   (void) __sync_fetch_and_add (&ull, 1);
36
37   (void) __sync_fetch_and_sub (&sc, 1);
38   (void) __sync_fetch_and_sub (&uc, 1);
39   (void) __sync_fetch_and_sub (&ss, 1);
40   (void) __sync_fetch_and_sub (&us, 1);
41   (void) __sync_fetch_and_sub (&si, 1);
42   (void) __sync_fetch_and_sub (&ui, 1);
43   (void) __sync_fetch_and_sub (&sl, 1);
44   (void) __sync_fetch_and_sub (&ul, 1);
45   (void) __sync_fetch_and_sub (&sll, 1);
46   (void) __sync_fetch_and_sub (&ull, 1);
47
48   (void) __sync_fetch_and_or (&sc, 1);
49   (void) __sync_fetch_and_or (&uc, 1);
50   (void) __sync_fetch_and_or (&ss, 1);
51   (void) __sync_fetch_and_or (&us, 1);
52   (void) __sync_fetch_and_or (&si, 1);
53   (void) __sync_fetch_and_or (&ui, 1);
54   (void) __sync_fetch_and_or (&sl, 1);
55   (void) __sync_fetch_and_or (&ul, 1);
56   (void) __sync_fetch_and_or (&sll, 1);
57   (void) __sync_fetch_and_or (&ull, 1);
58
59   (void) __sync_fetch_and_xor (&sc, 1);
60   (void) __sync_fetch_and_xor (&uc, 1);
61   (void) __sync_fetch_and_xor (&ss, 1);
62   (void) __sync_fetch_and_xor (&us, 1);
63   (void) __sync_fetch_and_xor (&si, 1);
64   (void) __sync_fetch_and_xor (&ui, 1);
65   (void) __sync_fetch_and_xor (&sl, 1);
66   (void) __sync_fetch_and_xor (&ul, 1);
67   (void) __sync_fetch_and_xor (&sll, 1);
68   (void) __sync_fetch_and_xor (&ull, 1);
69
70   (void) __sync_fetch_and_and (&sc, 1);
71   (void) __sync_fetch_and_and (&uc, 1);
72   (void) __sync_fetch_and_and (&ss, 1);
73   (void) __sync_fetch_and_and (&us, 1);
74   (void) __sync_fetch_and_and (&si, 1);
75   (void) __sync_fetch_and_and (&ui, 1);
76   (void) __sync_fetch_and_and (&sl, 1);
77   (void) __sync_fetch_and_and (&ul, 1);
78   (void) __sync_fetch_and_and (&sll, 1);
79   (void) __sync_fetch_and_and (&ull, 1);
80
81   (void) __sync_fetch_and_nand (&sc, 1);
82   (void) __sync_fetch_and_nand (&uc, 1);
83   (void) __sync_fetch_and_nand (&ss, 1);
84   (void) __sync_fetch_and_nand (&us, 1);
85   (void) __sync_fetch_and_nand (&si, 1);
86   (void) __sync_fetch_and_nand (&ui, 1);
87   (void) __sync_fetch_and_nand (&sl, 1);
88   (void) __sync_fetch_and_nand (&ul, 1);
89   (void) __sync_fetch_and_nand (&sll, 1);
90   (void) __sync_fetch_and_nand (&ull, 1);
91 }
92
93 void test_fetch_and_op (void)
94 {
95   sc = __sync_fetch_and_add (&sc, 11);
96   uc = __sync_fetch_and_add (&uc, 11);
97   ss = __sync_fetch_and_add (&ss, 11);
98   us = __sync_fetch_and_add (&us, 11);
99   si = __sync_fetch_and_add (&si, 11);
100   ui = __sync_fetch_and_add (&ui, 11);
101   sl = __sync_fetch_and_add (&sl, 11);
102   ul = __sync_fetch_and_add (&ul, 11);
103   sll = __sync_fetch_and_add (&sll, 11);
104   ull = __sync_fetch_and_add (&ull, 11);
105
106   sc = __sync_fetch_and_sub (&sc, 11);
107   uc = __sync_fetch_and_sub (&uc, 11);
108   ss = __sync_fetch_and_sub (&ss, 11);
109   us = __sync_fetch_and_sub (&us, 11);
110   si = __sync_fetch_and_sub (&si, 11);
111   ui = __sync_fetch_and_sub (&ui, 11);
112   sl = __sync_fetch_and_sub (&sl, 11);
113   ul = __sync_fetch_and_sub (&ul, 11);
114   sll = __sync_fetch_and_sub (&sll, 11);
115   ull = __sync_fetch_and_sub (&ull, 11);
116
117   sc = __sync_fetch_and_or (&sc, 11);
118   uc = __sync_fetch_and_or (&uc, 11);
119   ss = __sync_fetch_and_or (&ss, 11);
120   us = __sync_fetch_and_or (&us, 11);
121   si = __sync_fetch_and_or (&si, 11);
122   ui = __sync_fetch_and_or (&ui, 11);
123   sl = __sync_fetch_and_or (&sl, 11);
124   ul = __sync_fetch_and_or (&ul, 11);
125   sll = __sync_fetch_and_or (&sll, 11);
126   ull = __sync_fetch_and_or (&ull, 11);
127
128   sc = __sync_fetch_and_xor (&sc, 11);
129   uc = __sync_fetch_and_xor (&uc, 11);
130   ss = __sync_fetch_and_xor (&ss, 11);
131   us = __sync_fetch_and_xor (&us, 11);
132   si = __sync_fetch_and_xor (&si, 11);
133   ui = __sync_fetch_and_xor (&ui, 11);
134   sl = __sync_fetch_and_xor (&sl, 11);
135   ul = __sync_fetch_and_xor (&ul, 11);
136   sll = __sync_fetch_and_xor (&sll, 11);
137   ull = __sync_fetch_and_xor (&ull, 11);
138
139   sc = __sync_fetch_and_and (&sc, 11);
140   uc = __sync_fetch_and_and (&uc, 11);
141   ss = __sync_fetch_and_and (&ss, 11);
142   us = __sync_fetch_and_and (&us, 11);
143   si = __sync_fetch_and_and (&si, 11);
144   ui = __sync_fetch_and_and (&ui, 11);
145   sl = __sync_fetch_and_and (&sl, 11);
146   ul = __sync_fetch_and_and (&ul, 11);
147   sll = __sync_fetch_and_and (&sll, 11);
148   ull = __sync_fetch_and_and (&ull, 11);
149
150   sc = __sync_fetch_and_nand (&sc, 11);
151   uc = __sync_fetch_and_nand (&uc, 11);
152   ss = __sync_fetch_and_nand (&ss, 11);
153   us = __sync_fetch_and_nand (&us, 11);
154   si = __sync_fetch_and_nand (&si, 11);
155   ui = __sync_fetch_and_nand (&ui, 11);
156   sl = __sync_fetch_and_nand (&sl, 11);
157   ul = __sync_fetch_and_nand (&ul, 11);
158   sll = __sync_fetch_and_nand (&sll, 11);
159   ull = __sync_fetch_and_nand (&ull, 11);
160 }
161
162 void test_op_and_fetch (void)
163 {
164   sc = __sync_add_and_fetch (&sc, uc);
165   uc = __sync_add_and_fetch (&uc, uc);
166   ss = __sync_add_and_fetch (&ss, uc);
167   us = __sync_add_and_fetch (&us, uc);
168   si = __sync_add_and_fetch (&si, uc);
169   ui = __sync_add_and_fetch (&ui, uc);
170   sl = __sync_add_and_fetch (&sl, uc);
171   ul = __sync_add_and_fetch (&ul, uc);
172   sll = __sync_add_and_fetch (&sll, uc);
173   ull = __sync_add_and_fetch (&ull, uc);
174
175   sc = __sync_sub_and_fetch (&sc, uc);
176   uc = __sync_sub_and_fetch (&uc, uc);
177   ss = __sync_sub_and_fetch (&ss, uc);
178   us = __sync_sub_and_fetch (&us, uc);
179   si = __sync_sub_and_fetch (&si, uc);
180   ui = __sync_sub_and_fetch (&ui, uc);
181   sl = __sync_sub_and_fetch (&sl, uc);
182   ul = __sync_sub_and_fetch (&ul, uc);
183   sll = __sync_sub_and_fetch (&sll, uc);
184   ull = __sync_sub_and_fetch (&ull, uc);
185
186   sc = __sync_or_and_fetch (&sc, uc);
187   uc = __sync_or_and_fetch (&uc, uc);
188   ss = __sync_or_and_fetch (&ss, uc);
189   us = __sync_or_and_fetch (&us, uc);
190   si = __sync_or_and_fetch (&si, uc);
191   ui = __sync_or_and_fetch (&ui, uc);
192   sl = __sync_or_and_fetch (&sl, uc);
193   ul = __sync_or_and_fetch (&ul, uc);
194   sll = __sync_or_and_fetch (&sll, uc);
195   ull = __sync_or_and_fetch (&ull, uc);
196
197   sc = __sync_xor_and_fetch (&sc, uc);
198   uc = __sync_xor_and_fetch (&uc, uc);
199   ss = __sync_xor_and_fetch (&ss, uc);
200   us = __sync_xor_and_fetch (&us, uc);
201   si = __sync_xor_and_fetch (&si, uc);
202   ui = __sync_xor_and_fetch (&ui, uc);
203   sl = __sync_xor_and_fetch (&sl, uc);
204   ul = __sync_xor_and_fetch (&ul, uc);
205   sll = __sync_xor_and_fetch (&sll, uc);
206   ull = __sync_xor_and_fetch (&ull, uc);
207
208   sc = __sync_and_and_fetch (&sc, uc);
209   uc = __sync_and_and_fetch (&uc, uc);
210   ss = __sync_and_and_fetch (&ss, uc);
211   us = __sync_and_and_fetch (&us, uc);
212   si = __sync_and_and_fetch (&si, uc);
213   ui = __sync_and_and_fetch (&ui, uc);
214   sl = __sync_and_and_fetch (&sl, uc);
215   ul = __sync_and_and_fetch (&ul, uc);
216   sll = __sync_and_and_fetch (&sll, uc);
217   ull = __sync_and_and_fetch (&ull, uc);
218
219   sc = __sync_nand_and_fetch (&sc, uc);
220   uc = __sync_nand_and_fetch (&uc, uc);
221   ss = __sync_nand_and_fetch (&ss, uc);
222   us = __sync_nand_and_fetch (&us, uc);
223   si = __sync_nand_and_fetch (&si, uc);
224   ui = __sync_nand_and_fetch (&ui, uc);
225   sl = __sync_nand_and_fetch (&sl, uc);
226   ul = __sync_nand_and_fetch (&ul, uc);
227   sll = __sync_nand_and_fetch (&sll, uc);
228   ull = __sync_nand_and_fetch (&ull, uc);
229 }
230
231 void test_compare_and_swap (void)
232 {
233   sc = __sync_val_compare_and_swap (&sc, uc, sc);
234   uc = __sync_val_compare_and_swap (&uc, uc, sc);
235   ss = __sync_val_compare_and_swap (&ss, uc, sc);
236   us = __sync_val_compare_and_swap (&us, uc, sc);
237   si = __sync_val_compare_and_swap (&si, uc, sc);
238   ui = __sync_val_compare_and_swap (&ui, uc, sc);
239   sl = __sync_val_compare_and_swap (&sl, uc, sc);
240   ul = __sync_val_compare_and_swap (&ul, uc, sc);
241   sll = __sync_val_compare_and_swap (&sll, uc, sc);
242   ull = __sync_val_compare_and_swap (&ull, uc, sc);
243
244   ui = __sync_bool_compare_and_swap (&sc, uc, sc);
245   ui = __sync_bool_compare_and_swap (&uc, uc, sc);
246   ui = __sync_bool_compare_and_swap (&ss, uc, sc);
247   ui = __sync_bool_compare_and_swap (&us, uc, sc);
248   ui = __sync_bool_compare_and_swap (&si, uc, sc);
249   ui = __sync_bool_compare_and_swap (&ui, uc, sc);
250   ui = __sync_bool_compare_and_swap (&sl, uc, sc);
251   ui = __sync_bool_compare_and_swap (&ul, uc, sc);
252   ui = __sync_bool_compare_and_swap (&sll, uc, sc);
253   ui = __sync_bool_compare_and_swap (&ull, uc, sc);
254 }
255
256 void test_lock (void)
257 {
258   sc = __sync_lock_test_and_set (&sc, 1);
259   uc = __sync_lock_test_and_set (&uc, 1);
260   ss = __sync_lock_test_and_set (&ss, 1);
261   us = __sync_lock_test_and_set (&us, 1);
262   si = __sync_lock_test_and_set (&si, 1);
263   ui = __sync_lock_test_and_set (&ui, 1);
264   sl = __sync_lock_test_and_set (&sl, 1);
265   ul = __sync_lock_test_and_set (&ul, 1);
266   sll = __sync_lock_test_and_set (&sll, 1);
267   ull = __sync_lock_test_and_set (&ull, 1);
268
269   __sync_synchronize ();
270
271   __sync_lock_release (&sc);
272   __sync_lock_release (&uc);
273   __sync_lock_release (&ss);
274   __sync_lock_release (&us);
275   __sync_lock_release (&si);
276   __sync_lock_release (&ui);
277   __sync_lock_release (&sl);
278   __sync_lock_release (&ul);
279   __sync_lock_release (&sll);
280   __sync_lock_release (&ull);
281 }