1f4a2713aSLionel Sambuc // RUN: %clang_cc1 %s -emit-llvm -o - -triple=i686-apple-darwin9 | FileCheck %s
2f4a2713aSLionel Sambuc
atomic(void)3f4a2713aSLionel Sambuc int atomic(void) {
4f4a2713aSLionel Sambuc // non-sensical test for sync functions
5f4a2713aSLionel Sambuc int old;
6f4a2713aSLionel Sambuc int val = 1;
7f4a2713aSLionel Sambuc char valc = 1;
8f4a2713aSLionel Sambuc _Bool valb = 0;
9f4a2713aSLionel Sambuc unsigned int uval = 1;
10f4a2713aSLionel Sambuc int cmp = 0;
11f4a2713aSLionel Sambuc int* ptrval;
12f4a2713aSLionel Sambuc
13f4a2713aSLionel Sambuc old = __sync_fetch_and_add(&val, 1);
14f4a2713aSLionel Sambuc // CHECK: atomicrmw add i32* %val, i32 1 seq_cst
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc old = __sync_fetch_and_sub(&valc, 2);
17f4a2713aSLionel Sambuc // CHECK: atomicrmw sub i8* %valc, i8 2 seq_cst
18f4a2713aSLionel Sambuc
19f4a2713aSLionel Sambuc old = __sync_fetch_and_min(&val, 3);
20f4a2713aSLionel Sambuc // CHECK: atomicrmw min i32* %val, i32 3 seq_cst
21f4a2713aSLionel Sambuc
22f4a2713aSLionel Sambuc old = __sync_fetch_and_max(&val, 4);
23f4a2713aSLionel Sambuc // CHECK: atomicrmw max i32* %val, i32 4 seq_cst
24f4a2713aSLionel Sambuc
25f4a2713aSLionel Sambuc old = __sync_fetch_and_umin(&uval, 5u);
26f4a2713aSLionel Sambuc // CHECK: atomicrmw umin i32* %uval, i32 5 seq_cst
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc old = __sync_fetch_and_umax(&uval, 6u);
29f4a2713aSLionel Sambuc // CHECK: atomicrmw umax i32* %uval, i32 6 seq_cst
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc old = __sync_lock_test_and_set(&val, 7);
32f4a2713aSLionel Sambuc // CHECK: atomicrmw xchg i32* %val, i32 7 seq_cst
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc old = __sync_swap(&val, 8);
35f4a2713aSLionel Sambuc // CHECK: atomicrmw xchg i32* %val, i32 8 seq_cst
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc old = __sync_val_compare_and_swap(&val, 4, 1976);
38*0a6a1f1dSLionel Sambuc // CHECK: [[PAIR:%[a-z0-9_.]+]] = cmpxchg i32* %val, i32 4, i32 1976 seq_cst
39*0a6a1f1dSLionel Sambuc // CHECK: extractvalue { i32, i1 } [[PAIR]], 0
40f4a2713aSLionel Sambuc
41f4a2713aSLionel Sambuc old = __sync_bool_compare_and_swap(&val, 4, 1976);
42*0a6a1f1dSLionel Sambuc // CHECK: [[PAIR:%[a-z0-9_.]+]] = cmpxchg i32* %val, i32 4, i32 1976 seq_cst
43*0a6a1f1dSLionel Sambuc // CHECK: extractvalue { i32, i1 } [[PAIR]], 1
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc old = __sync_fetch_and_and(&val, 0x9);
46f4a2713aSLionel Sambuc // CHECK: atomicrmw and i32* %val, i32 9 seq_cst
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc old = __sync_fetch_and_or(&val, 0xa);
49f4a2713aSLionel Sambuc // CHECK: atomicrmw or i32* %val, i32 10 seq_cst
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc old = __sync_fetch_and_xor(&val, 0xb);
52f4a2713aSLionel Sambuc // CHECK: atomicrmw xor i32* %val, i32 11 seq_cst
53f4a2713aSLionel Sambuc
54*0a6a1f1dSLionel Sambuc old = __sync_fetch_and_nand(&val, 0xc);
55*0a6a1f1dSLionel Sambuc // CHECK: atomicrmw nand i32* %val, i32 12 seq_cst
56*0a6a1f1dSLionel Sambuc
57f4a2713aSLionel Sambuc old = __sync_add_and_fetch(&val, 1);
58f4a2713aSLionel Sambuc // CHECK: atomicrmw add i32* %val, i32 1 seq_cst
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc old = __sync_sub_and_fetch(&val, 2);
61f4a2713aSLionel Sambuc // CHECK: atomicrmw sub i32* %val, i32 2 seq_cst
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc old = __sync_and_and_fetch(&valc, 3);
64f4a2713aSLionel Sambuc // CHECK: atomicrmw and i8* %valc, i8 3 seq_cst
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc old = __sync_or_and_fetch(&valc, 4);
67f4a2713aSLionel Sambuc // CHECK: atomicrmw or i8* %valc, i8 4 seq_cst
68f4a2713aSLionel Sambuc
69f4a2713aSLionel Sambuc old = __sync_xor_and_fetch(&valc, 5);
70f4a2713aSLionel Sambuc // CHECK: atomicrmw xor i8* %valc, i8 5 seq_cst
71f4a2713aSLionel Sambuc
72*0a6a1f1dSLionel Sambuc old = __sync_nand_and_fetch(&valc, 6);
73*0a6a1f1dSLionel Sambuc // CHECK: atomicrmw nand i8* %valc, i8 6 seq_cst
74*0a6a1f1dSLionel Sambuc
75f4a2713aSLionel Sambuc __sync_val_compare_and_swap((void **)0, (void *)0, (void *)0);
76*0a6a1f1dSLionel Sambuc // CHECK: [[PAIR:%[a-z0-9_.]+]] = cmpxchg i32* null, i32 0, i32 0 seq_cst
77*0a6a1f1dSLionel Sambuc // CHECK: extractvalue { i32, i1 } [[PAIR]], 0
78f4a2713aSLionel Sambuc
79f4a2713aSLionel Sambuc if ( __sync_val_compare_and_swap(&valb, 0, 1)) {
80*0a6a1f1dSLionel Sambuc // CHECK: [[PAIR:%[a-z0-9_.]+]] = cmpxchg i8* %valb, i8 0, i8 1 seq_cst
81*0a6a1f1dSLionel Sambuc // CHECK: [[VAL:%[a-z0-9_.]+]] = extractvalue { i8, i1 } [[PAIR]], 0
82*0a6a1f1dSLionel Sambuc // CHECK: trunc i8 [[VAL]] to i1
83f4a2713aSLionel Sambuc old = 42;
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc __sync_bool_compare_and_swap((void **)0, (void *)0, (void *)0);
87f4a2713aSLionel Sambuc // CHECK: cmpxchg i32* null, i32 0, i32 0 seq_cst
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc __sync_lock_release(&val);
90f4a2713aSLionel Sambuc // CHECK: store atomic i32 0, {{.*}} release, align 4
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc __sync_lock_release(&ptrval);
93f4a2713aSLionel Sambuc // CHECK: store atomic i32 0, {{.*}} release, align 4
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc __sync_synchronize ();
96f4a2713aSLionel Sambuc // CHECK: fence seq_cst
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc return old;
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc // CHECK: @release_return
release_return(int * lock)102f4a2713aSLionel Sambuc void release_return(int *lock) {
103f4a2713aSLionel Sambuc // Ensure this is actually returning void all the way through.
104f4a2713aSLionel Sambuc return __sync_lock_release(lock);
105f4a2713aSLionel Sambuc // CHECK: store atomic {{.*}} release, align 4
106f4a2713aSLionel Sambuc }
107f4a2713aSLionel Sambuc
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc // rdar://8461279 - Atomics with address spaces.
110f4a2713aSLionel Sambuc // CHECK: @addrspace
addrspace(int * P)111f4a2713aSLionel Sambuc void addrspace(int __attribute__((address_space(256))) * P) {
112f4a2713aSLionel Sambuc __sync_bool_compare_and_swap(P, 0, 1);
113f4a2713aSLionel Sambuc // CHECK: cmpxchg i32 addrspace(256)*{{.*}}, i32 0, i32 1 seq_cst
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc __sync_val_compare_and_swap(P, 0, 1);
116f4a2713aSLionel Sambuc // CHECK: cmpxchg i32 addrspace(256)*{{.*}}, i32 0, i32 1 seq_cst
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc __sync_xor_and_fetch(P, 123);
119f4a2713aSLionel Sambuc // CHECK: atomicrmw xor i32 addrspace(256)*{{.*}}, i32 123 seq_cst
120f4a2713aSLionel Sambuc }
121