xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGen/atomic-ops.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -emit-llvm -o - -ffreestanding -triple=i686-apple-darwin9 | FileCheck %s
2f4a2713aSLionel Sambuc 
3f4a2713aSLionel Sambuc // Also test serialization of atomic operations here, to avoid duplicating the
4f4a2713aSLionel Sambuc // test.
5*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -emit-pch -o %t -ffreestanding -triple=i686-apple-darwin9
6*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -include-pch %t -ffreestanding -triple=i686-apple-darwin9 -emit-llvm -o - | FileCheck %s
7f4a2713aSLionel Sambuc #ifndef ALREADY_INCLUDED
8f4a2713aSLionel Sambuc #define ALREADY_INCLUDED
9f4a2713aSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #include <stdatomic.h>
11*0a6a1f1dSLionel Sambuc 
12f4a2713aSLionel Sambuc // Basic IRGen tests for __c11_atomic_* and GNU __atomic_*
13f4a2713aSLionel Sambuc 
fi1(_Atomic (int)* i)14f4a2713aSLionel Sambuc int fi1(_Atomic(int) *i) {
15*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi1
16f4a2713aSLionel Sambuc   // CHECK: load atomic i32* {{.*}} seq_cst
17f4a2713aSLionel Sambuc   return __c11_atomic_load(i, memory_order_seq_cst);
18f4a2713aSLionel Sambuc }
19f4a2713aSLionel Sambuc 
fi1a(int * i)20f4a2713aSLionel Sambuc int fi1a(int *i) {
21*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi1a
22f4a2713aSLionel Sambuc   // CHECK: load atomic i32* {{.*}} seq_cst
23f4a2713aSLionel Sambuc   int v;
24f4a2713aSLionel Sambuc   __atomic_load(i, &v, memory_order_seq_cst);
25f4a2713aSLionel Sambuc   return v;
26f4a2713aSLionel Sambuc }
27f4a2713aSLionel Sambuc 
fi1b(int * i)28f4a2713aSLionel Sambuc int fi1b(int *i) {
29*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi1b
30f4a2713aSLionel Sambuc   // CHECK: load atomic i32* {{.*}} seq_cst
31f4a2713aSLionel Sambuc   return __atomic_load_n(i, memory_order_seq_cst);
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc 
fi1c(atomic_int * i)34*0a6a1f1dSLionel Sambuc int fi1c(atomic_int *i) {
35*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi1c
36*0a6a1f1dSLionel Sambuc   // CHECK: load atomic i32* {{.*}} seq_cst
37*0a6a1f1dSLionel Sambuc   return atomic_load(i);
38*0a6a1f1dSLionel Sambuc }
39*0a6a1f1dSLionel Sambuc 
fi2(_Atomic (int)* i)40f4a2713aSLionel Sambuc void fi2(_Atomic(int) *i) {
41*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi2
42f4a2713aSLionel Sambuc   // CHECK: store atomic i32 {{.*}} seq_cst
43f4a2713aSLionel Sambuc   __c11_atomic_store(i, 1, memory_order_seq_cst);
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
fi2a(int * i)46f4a2713aSLionel Sambuc void fi2a(int *i) {
47*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi2a
48f4a2713aSLionel Sambuc   // CHECK: store atomic i32 {{.*}} seq_cst
49f4a2713aSLionel Sambuc   int v = 1;
50f4a2713aSLionel Sambuc   __atomic_store(i, &v, memory_order_seq_cst);
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc 
fi2b(int * i)53f4a2713aSLionel Sambuc void fi2b(int *i) {
54*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi2b
55f4a2713aSLionel Sambuc   // CHECK: store atomic i32 {{.*}} seq_cst
56f4a2713aSLionel Sambuc   __atomic_store_n(i, 1, memory_order_seq_cst);
57f4a2713aSLionel Sambuc }
58f4a2713aSLionel Sambuc 
fi2c(atomic_int * i)59*0a6a1f1dSLionel Sambuc void fi2c(atomic_int *i) {
60*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi2c
61*0a6a1f1dSLionel Sambuc   // CHECK: store atomic i32 {{.*}} seq_cst
62*0a6a1f1dSLionel Sambuc   atomic_store(i, 1);
63*0a6a1f1dSLionel Sambuc }
64*0a6a1f1dSLionel Sambuc 
fi3(_Atomic (int)* i)65f4a2713aSLionel Sambuc int fi3(_Atomic(int) *i) {
66*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi3
67f4a2713aSLionel Sambuc   // CHECK: atomicrmw and
68f4a2713aSLionel Sambuc   // CHECK-NOT: and
69f4a2713aSLionel Sambuc   return __c11_atomic_fetch_and(i, 1, memory_order_seq_cst);
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc 
fi3a(int * i)72f4a2713aSLionel Sambuc int fi3a(int *i) {
73*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi3a
74f4a2713aSLionel Sambuc   // CHECK: atomicrmw xor
75f4a2713aSLionel Sambuc   // CHECK-NOT: xor
76f4a2713aSLionel Sambuc   return __atomic_fetch_xor(i, 1, memory_order_seq_cst);
77f4a2713aSLionel Sambuc }
78f4a2713aSLionel Sambuc 
fi3b(int * i)79f4a2713aSLionel Sambuc int fi3b(int *i) {
80*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi3b
81f4a2713aSLionel Sambuc   // CHECK: atomicrmw add
82f4a2713aSLionel Sambuc   // CHECK: add
83f4a2713aSLionel Sambuc   return __atomic_add_fetch(i, 1, memory_order_seq_cst);
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc 
fi3c(int * i)86f4a2713aSLionel Sambuc int fi3c(int *i) {
87*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi3c
88f4a2713aSLionel Sambuc   // CHECK: atomicrmw nand
89f4a2713aSLionel Sambuc   // CHECK-NOT: and
90f4a2713aSLionel Sambuc   return __atomic_fetch_nand(i, 1, memory_order_seq_cst);
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
fi3d(int * i)93f4a2713aSLionel Sambuc int fi3d(int *i) {
94*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi3d
95f4a2713aSLionel Sambuc   // CHECK: atomicrmw nand
96f4a2713aSLionel Sambuc   // CHECK: and
97f4a2713aSLionel Sambuc   // CHECK: xor
98f4a2713aSLionel Sambuc   return __atomic_nand_fetch(i, 1, memory_order_seq_cst);
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc 
fi3e(atomic_int * i)101*0a6a1f1dSLionel Sambuc int fi3e(atomic_int *i) {
102*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi3e
103*0a6a1f1dSLionel Sambuc   // CHECK: atomicrmw or
104*0a6a1f1dSLionel Sambuc   // CHECK-NOT: {{ or }}
105*0a6a1f1dSLionel Sambuc   return atomic_fetch_or(i, 1);
106*0a6a1f1dSLionel Sambuc }
107*0a6a1f1dSLionel Sambuc 
fi4(_Atomic (int)* i)108f4a2713aSLionel Sambuc _Bool fi4(_Atomic(int) *i) {
109*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi4(
110*0a6a1f1dSLionel Sambuc   // CHECK: [[PAIR:%[.0-9A-Z_a-z]+]] = cmpxchg i32* [[PTR:%[.0-9A-Z_a-z]+]], i32 [[EXPECTED:%[.0-9A-Z_a-z]+]], i32 [[DESIRED:%[.0-9A-Z_a-z]+]]
111*0a6a1f1dSLionel Sambuc   // CHECK: [[OLD:%[.0-9A-Z_a-z]+]] = extractvalue { i32, i1 } [[PAIR]], 0
112*0a6a1f1dSLionel Sambuc   // CHECK: [[CMP:%[.0-9A-Z_a-z]+]] = extractvalue { i32, i1 } [[PAIR]], 1
113*0a6a1f1dSLionel Sambuc   // CHECK: br i1 [[CMP]], label %[[STORE_EXPECTED:[.0-9A-Z_a-z]+]], label %[[CONTINUE:[.0-9A-Z_a-z]+]]
114*0a6a1f1dSLionel Sambuc   // CHECK: store i32 [[OLD]]
115f4a2713aSLionel Sambuc   int cmp = 0;
116f4a2713aSLionel Sambuc   return __c11_atomic_compare_exchange_strong(i, &cmp, 1, memory_order_acquire, memory_order_acquire);
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc 
fi4a(int * i)119f4a2713aSLionel Sambuc _Bool fi4a(int *i) {
120*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi4a
121*0a6a1f1dSLionel Sambuc   // CHECK: [[PAIR:%[.0-9A-Z_a-z]+]] = cmpxchg i32* [[PTR:%[.0-9A-Z_a-z]+]], i32 [[EXPECTED:%[.0-9A-Z_a-z]+]], i32 [[DESIRED:%[.0-9A-Z_a-z]+]]
122*0a6a1f1dSLionel Sambuc   // CHECK: [[OLD:%[.0-9A-Z_a-z]+]] = extractvalue { i32, i1 } [[PAIR]], 0
123*0a6a1f1dSLionel Sambuc   // CHECK: [[CMP:%[.0-9A-Z_a-z]+]] = extractvalue { i32, i1 } [[PAIR]], 1
124*0a6a1f1dSLionel Sambuc   // CHECK: br i1 [[CMP]], label %[[STORE_EXPECTED:[.0-9A-Z_a-z]+]], label %[[CONTINUE:[.0-9A-Z_a-z]+]]
125*0a6a1f1dSLionel Sambuc   // CHECK: store i32 [[OLD]]
126f4a2713aSLionel Sambuc   int cmp = 0;
127f4a2713aSLionel Sambuc   int desired = 1;
128f4a2713aSLionel Sambuc   return __atomic_compare_exchange(i, &cmp, &desired, 0, memory_order_acquire, memory_order_acquire);
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc 
fi4b(int * i)131f4a2713aSLionel Sambuc _Bool fi4b(int *i) {
132*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi4b(
133*0a6a1f1dSLionel Sambuc   // CHECK: [[PAIR:%[.0-9A-Z_a-z]+]] = cmpxchg weak i32* [[PTR:%[.0-9A-Z_a-z]+]], i32 [[EXPECTED:%[.0-9A-Z_a-z]+]], i32 [[DESIRED:%[.0-9A-Z_a-z]+]]
134*0a6a1f1dSLionel Sambuc   // CHECK: [[OLD:%[.0-9A-Z_a-z]+]] = extractvalue { i32, i1 } [[PAIR]], 0
135*0a6a1f1dSLionel Sambuc   // CHECK: [[CMP:%[.0-9A-Z_a-z]+]] = extractvalue { i32, i1 } [[PAIR]], 1
136*0a6a1f1dSLionel Sambuc   // CHECK: br i1 [[CMP]], label %[[STORE_EXPECTED:[.0-9A-Z_a-z]+]], label %[[CONTINUE:[.0-9A-Z_a-z]+]]
137*0a6a1f1dSLionel Sambuc   // CHECK: store i32 [[OLD]]
138f4a2713aSLionel Sambuc   int cmp = 0;
139f4a2713aSLionel Sambuc   return __atomic_compare_exchange_n(i, &cmp, 1, 1, memory_order_acquire, memory_order_acquire);
140f4a2713aSLionel Sambuc }
141f4a2713aSLionel Sambuc 
fi4c(atomic_int * i)142*0a6a1f1dSLionel Sambuc _Bool fi4c(atomic_int *i) {
143*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fi4c
144*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg i32*
145*0a6a1f1dSLionel Sambuc   int cmp = 0;
146*0a6a1f1dSLionel Sambuc   return atomic_compare_exchange_strong(i, &cmp, 1);
147*0a6a1f1dSLionel Sambuc }
148*0a6a1f1dSLionel Sambuc 
ff1(_Atomic (float)* d)149f4a2713aSLionel Sambuc float ff1(_Atomic(float) *d) {
150*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @ff1
151f4a2713aSLionel Sambuc   // CHECK: load atomic i32* {{.*}} monotonic
152f4a2713aSLionel Sambuc   return __c11_atomic_load(d, memory_order_relaxed);
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc 
ff2(_Atomic (float)* d)155f4a2713aSLionel Sambuc void ff2(_Atomic(float) *d) {
156*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @ff2
157f4a2713aSLionel Sambuc   // CHECK: store atomic i32 {{.*}} release
158f4a2713aSLionel Sambuc   __c11_atomic_store(d, 1, memory_order_release);
159f4a2713aSLionel Sambuc }
160f4a2713aSLionel Sambuc 
ff3(_Atomic (float)* d)161f4a2713aSLionel Sambuc float ff3(_Atomic(float) *d) {
162f4a2713aSLionel Sambuc   return __c11_atomic_exchange(d, 2, memory_order_seq_cst);
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc struct S {
166*0a6a1f1dSLionel Sambuc   double x;
167*0a6a1f1dSLionel Sambuc };
168*0a6a1f1dSLionel Sambuc 
fd1(struct S * a)169*0a6a1f1dSLionel Sambuc struct S fd1(struct S *a) {
170*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fd1
171*0a6a1f1dSLionel Sambuc   // CHECK: [[RETVAL:%.*]] = alloca %struct.S, align 4
172*0a6a1f1dSLionel Sambuc   // CHECK: [[RET:%.*]]    = alloca %struct.S, align 4
173*0a6a1f1dSLionel Sambuc   // CHECK: [[CALL:%.*]]   = call i64 @__atomic_load_8(
174*0a6a1f1dSLionel Sambuc   // CHECK: [[CAST:%.*]]   = bitcast %struct.S* [[RET]] to i64*
175*0a6a1f1dSLionel Sambuc   // CHECK: store i64 [[CALL]], i64* [[CAST]], align 4
176*0a6a1f1dSLionel Sambuc   struct S ret;
177*0a6a1f1dSLionel Sambuc   __atomic_load(a, &ret, memory_order_seq_cst);
178*0a6a1f1dSLionel Sambuc   return ret;
179*0a6a1f1dSLionel Sambuc }
180*0a6a1f1dSLionel Sambuc 
fd2(struct S * a,struct S * b)181*0a6a1f1dSLionel Sambuc void fd2(struct S *a, struct S *b) {
182*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fd2
183*0a6a1f1dSLionel Sambuc   // CHECK:      [[A_ADDR:%.*]] = alloca %struct.S*, align 4
184*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[B_ADDR:%.*]] = alloca %struct.S*, align 4
185*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %a, %struct.S** [[A_ADDR]], align 4
186*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %b, %struct.S** [[B_ADDR]], align 4
187*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_A_PTR:%.*]] = load %struct.S** [[A_ADDR]], align 4
188*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_B_PTR:%.*]] = load %struct.S** [[B_ADDR]], align 4
189*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_A:%.*]] = bitcast %struct.S* [[LOAD_A_PTR]] to i8*
190*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_B:%.*]] = bitcast %struct.S* [[LOAD_B_PTR]] to i64*
191*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_B:%.*]] = load i64* [[COERCED_B]], align 4
192*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: call void @__atomic_store_8(i8* [[COERCED_A]], i64 [[LOAD_B]],
193*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ret void
194*0a6a1f1dSLionel Sambuc   __atomic_store(a, b, memory_order_seq_cst);
195*0a6a1f1dSLionel Sambuc }
196*0a6a1f1dSLionel Sambuc 
fd3(struct S * a,struct S * b,struct S * c)197*0a6a1f1dSLionel Sambuc void fd3(struct S *a, struct S *b, struct S *c) {
198*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fd3
199*0a6a1f1dSLionel Sambuc   // CHECK:      [[A_ADDR:%.*]] = alloca %struct.S*, align 4
200*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[B_ADDR:%.*]] = alloca %struct.S*, align 4
201*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[C_ADDR:%.*]] = alloca %struct.S*, align 4
202*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %a, %struct.S** [[A_ADDR]], align 4
203*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %b, %struct.S** [[B_ADDR]], align 4
204*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %c, %struct.S** [[C_ADDR]], align 4
205*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_A_PTR:%.*]] = load %struct.S** [[A_ADDR]], align 4
206*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_B_PTR:%.*]] = load %struct.S** [[B_ADDR]], align 4
207*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_C_PTR:%.*]] = load %struct.S** [[C_ADDR]], align 4
208*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_A:%.*]] = bitcast %struct.S* [[LOAD_A_PTR]] to i8*
209*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_B:%.*]] = bitcast %struct.S* [[LOAD_B_PTR]] to i64*
210*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_B:%.*]] = load i64* [[COERCED_B]], align 4
211*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[CALL:%.*]] = call i64 @__atomic_exchange_8(i8* [[COERCED_A]], i64 [[LOAD_B]],
212*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_C:%.*]] = bitcast %struct.S* [[LOAD_C_PTR]] to i64*
213*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i64 [[CALL]], i64* [[COERCED_C]], align 4
214*0a6a1f1dSLionel Sambuc 
215*0a6a1f1dSLionel Sambuc   __atomic_exchange(a, b, c, memory_order_seq_cst);
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc 
fd4(struct S * a,struct S * b,struct S * c)218*0a6a1f1dSLionel Sambuc _Bool fd4(struct S *a, struct S *b, struct S *c) {
219*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fd4
220*0a6a1f1dSLionel Sambuc   // CHECK:      [[A_ADDR:%.*]] = alloca %struct.S*, align 4
221*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[B_ADDR:%.*]] = alloca %struct.S*, align 4
222*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[C_ADDR:%.*]] = alloca %struct.S*, align 4
223*0a6a1f1dSLionel Sambuc   // CHECK:      store %struct.S* %a, %struct.S** [[A_ADDR]], align 4
224*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %b, %struct.S** [[B_ADDR]], align 4
225*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store %struct.S* %c, %struct.S** [[C_ADDR]], align 4
226*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_A_PTR:%.*]] = load %struct.S** [[A_ADDR]], align 4
227*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_B_PTR:%.*]] = load %struct.S** [[B_ADDR]], align 4
228*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_C_PTR:%.*]] = load %struct.S** [[C_ADDR]], align 4
229*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_A:%.*]] = bitcast %struct.S* [[LOAD_A_PTR]] to i8*
230*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_B:%.*]] = bitcast %struct.S* [[LOAD_B_PTR]] to i8*
231*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[COERCED_C:%.*]] = bitcast %struct.S* [[LOAD_C_PTR]] to i64*
232*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[LOAD_C:%.*]] = load i64* [[COERCED_C]], align 4
233*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: [[CALL:%.*]] = call zeroext i1 @__atomic_compare_exchange_8(i8* [[COERCED_A]], i8* [[COERCED_B]], i64 [[LOAD_C]]
234*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ret i1 [[CALL]]
235*0a6a1f1dSLionel Sambuc   return __atomic_compare_exchange(a, b, c, 1, 5, 5);
236*0a6a1f1dSLionel Sambuc }
237*0a6a1f1dSLionel Sambuc 
fp1(_Atomic (int *)* p)238f4a2713aSLionel Sambuc int* fp1(_Atomic(int*) *p) {
239*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fp1
240f4a2713aSLionel Sambuc   // CHECK: load atomic i32* {{.*}} seq_cst
241f4a2713aSLionel Sambuc   return __c11_atomic_load(p, memory_order_seq_cst);
242f4a2713aSLionel Sambuc }
243f4a2713aSLionel Sambuc 
fp2(_Atomic (int *)* p)244f4a2713aSLionel Sambuc int* fp2(_Atomic(int*) *p) {
245*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fp2
246f4a2713aSLionel Sambuc   // CHECK: store i32 4
247f4a2713aSLionel Sambuc   // CHECK: atomicrmw add {{.*}} monotonic
248f4a2713aSLionel Sambuc   return __c11_atomic_fetch_add(p, 1, memory_order_relaxed);
249f4a2713aSLionel Sambuc }
250f4a2713aSLionel Sambuc 
fp2a(int ** p)251f4a2713aSLionel Sambuc int *fp2a(int **p) {
252*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fp2a
253f4a2713aSLionel Sambuc   // CHECK: store i32 4
254f4a2713aSLionel Sambuc   // CHECK: atomicrmw sub {{.*}} monotonic
255f4a2713aSLionel Sambuc   // Note, the GNU builtins do not multiply by sizeof(T)!
256f4a2713aSLionel Sambuc   return __atomic_fetch_sub(p, 4, memory_order_relaxed);
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc 
fc(_Atomic (_Complex float)* c)259f4a2713aSLionel Sambuc _Complex float fc(_Atomic(_Complex float) *c) {
260*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fc
261f4a2713aSLionel Sambuc   // CHECK: atomicrmw xchg i64*
262f4a2713aSLionel Sambuc   return __c11_atomic_exchange(c, 2, memory_order_seq_cst);
263f4a2713aSLionel Sambuc }
264f4a2713aSLionel Sambuc 
265f4a2713aSLionel Sambuc typedef struct X { int x; } X;
fs(_Atomic (X)* c)266f4a2713aSLionel Sambuc X fs(_Atomic(X) *c) {
267*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fs
268f4a2713aSLionel Sambuc   // CHECK: atomicrmw xchg i32*
269f4a2713aSLionel Sambuc   return __c11_atomic_exchange(c, (X){2}, memory_order_seq_cst);
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc 
fsa(X * c,X * d)272f4a2713aSLionel Sambuc X fsa(X *c, X *d) {
273*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fsa
274f4a2713aSLionel Sambuc   // CHECK: atomicrmw xchg i32*
275f4a2713aSLionel Sambuc   X ret;
276f4a2713aSLionel Sambuc   __atomic_exchange(c, d, &ret, memory_order_seq_cst);
277f4a2713aSLionel Sambuc   return ret;
278f4a2713aSLionel Sambuc }
279f4a2713aSLionel Sambuc 
fsb(_Bool * c)280f4a2713aSLionel Sambuc _Bool fsb(_Bool *c) {
281*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @fsb
282f4a2713aSLionel Sambuc   // CHECK: atomicrmw xchg i8*
283f4a2713aSLionel Sambuc   return __atomic_exchange_n(c, 1, memory_order_seq_cst);
284f4a2713aSLionel Sambuc }
285f4a2713aSLionel Sambuc 
286f4a2713aSLionel Sambuc char flag1;
287f4a2713aSLionel Sambuc volatile char flag2;
test_and_set()288f4a2713aSLionel Sambuc void test_and_set() {
289f4a2713aSLionel Sambuc   // CHECK: atomicrmw xchg i8* @flag1, i8 1 seq_cst
290f4a2713aSLionel Sambuc   __atomic_test_and_set(&flag1, memory_order_seq_cst);
291f4a2713aSLionel Sambuc   // CHECK: atomicrmw volatile xchg i8* @flag2, i8 1 acquire
292f4a2713aSLionel Sambuc   __atomic_test_and_set(&flag2, memory_order_acquire);
293f4a2713aSLionel Sambuc   // CHECK: store atomic volatile i8 0, i8* @flag2 release
294f4a2713aSLionel Sambuc   __atomic_clear(&flag2, memory_order_release);
295f4a2713aSLionel Sambuc   // CHECK: store atomic i8 0, i8* @flag1 seq_cst
296f4a2713aSLionel Sambuc   __atomic_clear(&flag1, memory_order_seq_cst);
297f4a2713aSLionel Sambuc }
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc struct Sixteen {
300f4a2713aSLionel Sambuc   char c[16];
301f4a2713aSLionel Sambuc } sixteen;
302f4a2713aSLionel Sambuc struct Seventeen {
303f4a2713aSLionel Sambuc   char c[17];
304f4a2713aSLionel Sambuc } seventeen;
305f4a2713aSLionel Sambuc 
lock_free(struct Incomplete * incomplete)306f4a2713aSLionel Sambuc int lock_free(struct Incomplete *incomplete) {
307*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @lock_free
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc   // CHECK: call i32 @__atomic_is_lock_free(i32 3, i8* null)
310f4a2713aSLionel Sambuc   __c11_atomic_is_lock_free(3);
311f4a2713aSLionel Sambuc 
312f4a2713aSLionel Sambuc   // CHECK: call i32 @__atomic_is_lock_free(i32 16, i8* {{.*}}@sixteen{{.*}})
313f4a2713aSLionel Sambuc   __atomic_is_lock_free(16, &sixteen);
314f4a2713aSLionel Sambuc 
315f4a2713aSLionel Sambuc   // CHECK: call i32 @__atomic_is_lock_free(i32 17, i8* {{.*}}@seventeen{{.*}})
316f4a2713aSLionel Sambuc   __atomic_is_lock_free(17, &seventeen);
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc   // CHECK: call i32 @__atomic_is_lock_free(i32 4, {{.*}})
319f4a2713aSLionel Sambuc   __atomic_is_lock_free(4, incomplete);
320f4a2713aSLionel Sambuc 
321f4a2713aSLionel Sambuc   char cs[20];
322f4a2713aSLionel Sambuc   // CHECK: call i32 @__atomic_is_lock_free(i32 4, {{.*}})
323f4a2713aSLionel Sambuc   __atomic_is_lock_free(4, cs+1);
324f4a2713aSLionel Sambuc 
325f4a2713aSLionel Sambuc   // CHECK-NOT: call
326f4a2713aSLionel Sambuc   __atomic_always_lock_free(3, 0);
327f4a2713aSLionel Sambuc   __atomic_always_lock_free(16, 0);
328f4a2713aSLionel Sambuc   __atomic_always_lock_free(17, 0);
329f4a2713aSLionel Sambuc   __atomic_always_lock_free(16, &sixteen);
330f4a2713aSLionel Sambuc   __atomic_always_lock_free(17, &seventeen);
331f4a2713aSLionel Sambuc 
332f4a2713aSLionel Sambuc   int n;
333f4a2713aSLionel Sambuc   __atomic_is_lock_free(4, &n);
334f4a2713aSLionel Sambuc 
335f4a2713aSLionel Sambuc   // CHECK: ret i32 1
336f4a2713aSLionel Sambuc   return __c11_atomic_is_lock_free(sizeof(_Atomic(int)));
337f4a2713aSLionel Sambuc }
338f4a2713aSLionel Sambuc 
339f4a2713aSLionel Sambuc // Tests for atomic operations on big values.  These should call the functions
340f4a2713aSLionel Sambuc // defined here:
341f4a2713aSLionel Sambuc // http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary#The_Library_interface
342f4a2713aSLionel Sambuc 
343f4a2713aSLionel Sambuc struct foo {
344f4a2713aSLionel Sambuc   int big[128];
345f4a2713aSLionel Sambuc };
346f4a2713aSLionel Sambuc struct bar {
347f4a2713aSLionel Sambuc   char c[3];
348f4a2713aSLionel Sambuc };
349f4a2713aSLionel Sambuc 
350f4a2713aSLionel Sambuc struct bar smallThing, thing1, thing2;
351f4a2713aSLionel Sambuc struct foo bigThing;
352f4a2713aSLionel Sambuc _Atomic(struct foo) bigAtomic;
353f4a2713aSLionel Sambuc 
structAtomicStore()354f4a2713aSLionel Sambuc void structAtomicStore() {
355*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @structAtomicStore
356f4a2713aSLionel Sambuc   struct foo f = {0};
357f4a2713aSLionel Sambuc   struct bar b = {0};
358f4a2713aSLionel Sambuc   __atomic_store(&smallThing, &b, 5);
359f4a2713aSLionel Sambuc   // CHECK: call void @__atomic_store(i32 3, i8* {{.*}} @smallThing
360f4a2713aSLionel Sambuc 
361f4a2713aSLionel Sambuc   __atomic_store(&bigThing, &f, 5);
362f4a2713aSLionel Sambuc   // CHECK: call void @__atomic_store(i32 512, i8* {{.*}} @bigThing
363f4a2713aSLionel Sambuc }
structAtomicLoad()364f4a2713aSLionel Sambuc void structAtomicLoad() {
365*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @structAtomicLoad
366f4a2713aSLionel Sambuc   struct bar b;
367f4a2713aSLionel Sambuc   __atomic_load(&smallThing, &b, 5);
368f4a2713aSLionel Sambuc   // CHECK: call void @__atomic_load(i32 3, i8* {{.*}} @smallThing
369f4a2713aSLionel Sambuc 
370f4a2713aSLionel Sambuc   struct foo f = {0};
371f4a2713aSLionel Sambuc   __atomic_load(&bigThing, &f, 5);
372f4a2713aSLionel Sambuc   // CHECK: call void @__atomic_load(i32 512, i8* {{.*}} @bigThing
373f4a2713aSLionel Sambuc }
structAtomicExchange()374f4a2713aSLionel Sambuc struct foo structAtomicExchange() {
375*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @structAtomicExchange
376f4a2713aSLionel Sambuc   struct foo f = {0};
377f4a2713aSLionel Sambuc   struct foo old;
378f4a2713aSLionel Sambuc   __atomic_exchange(&f, &bigThing, &old, 5);
379f4a2713aSLionel Sambuc   // CHECK: call void @__atomic_exchange(i32 512, {{.*}}, i8* bitcast ({{.*}} @bigThing to i8*),
380f4a2713aSLionel Sambuc 
381f4a2713aSLionel Sambuc   return __c11_atomic_exchange(&bigAtomic, f, 5);
382f4a2713aSLionel Sambuc   // CHECK: call void @__atomic_exchange(i32 512, i8* bitcast ({{.*}} @bigAtomic to i8*),
383f4a2713aSLionel Sambuc }
structAtomicCmpExchange()384f4a2713aSLionel Sambuc int structAtomicCmpExchange() {
385*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @structAtomicCmpExchange
386*0a6a1f1dSLionel Sambuc   // CHECK: %[[x_mem:.*]] = alloca i8
387f4a2713aSLionel Sambuc   _Bool x = __atomic_compare_exchange(&smallThing, &thing1, &thing2, 1, 5, 5);
388*0a6a1f1dSLionel Sambuc   // CHECK: %[[call1:.*]] = call zeroext i1 @__atomic_compare_exchange(i32 3, {{.*}} @smallThing{{.*}} @thing1{{.*}} @thing2
389*0a6a1f1dSLionel Sambuc   // CHECK: %[[zext1:.*]] = zext i1 %[[call1]] to i8
390*0a6a1f1dSLionel Sambuc   // CHECK: store i8 %[[zext1]], i8* %[[x_mem]], align 1
391*0a6a1f1dSLionel Sambuc   // CHECK: %[[x:.*]] = load i8* %[[x_mem]]
392*0a6a1f1dSLionel Sambuc   // CHECK: %[[x_bool:.*]] = trunc i8 %[[x]] to i1
393*0a6a1f1dSLionel Sambuc   // CHECK: %[[conv1:.*]] = zext i1 %[[x_bool]] to i32
394f4a2713aSLionel Sambuc 
395f4a2713aSLionel Sambuc   struct foo f = {0};
396f4a2713aSLionel Sambuc   struct foo g = {0};
397f4a2713aSLionel Sambuc   g.big[12] = 12;
398f4a2713aSLionel Sambuc   return x & __c11_atomic_compare_exchange_strong(&bigAtomic, &f, g, 5, 5);
399*0a6a1f1dSLionel Sambuc   // CHECK: %[[call2:.*]] = call zeroext i1 @__atomic_compare_exchange(i32 512, i8* bitcast ({{.*}} @bigAtomic to i8*),
400*0a6a1f1dSLionel Sambuc   // CHECK: %[[conv2:.*]] = zext i1 %[[call2]] to i32
401*0a6a1f1dSLionel Sambuc   // CHECK: %[[and:.*]] = and i32 %[[conv1]], %[[conv2]]
402*0a6a1f1dSLionel Sambuc   // CHECK: ret i32 %[[and]]
403f4a2713aSLionel Sambuc }
404f4a2713aSLionel Sambuc 
405f4a2713aSLionel Sambuc // Check that no atomic operations are used in any initialisation of _Atomic
406f4a2713aSLionel Sambuc // types.
407f4a2713aSLionel Sambuc _Atomic(int) atomic_init_i = 42;
408f4a2713aSLionel Sambuc 
409*0a6a1f1dSLionel Sambuc // CHECK-LABEL: @atomic_init_foo
atomic_init_foo()410f4a2713aSLionel Sambuc void atomic_init_foo()
411f4a2713aSLionel Sambuc {
412f4a2713aSLionel Sambuc   // CHECK-NOT: }
413f4a2713aSLionel Sambuc   // CHECK-NOT: atomic
414f4a2713aSLionel Sambuc   // CHECK: store
415f4a2713aSLionel Sambuc   _Atomic(int) j = 12;
416f4a2713aSLionel Sambuc 
417f4a2713aSLionel Sambuc   // CHECK-NOT: }
418f4a2713aSLionel Sambuc   // CHECK-NOT: atomic
419f4a2713aSLionel Sambuc   // CHECK: store
420f4a2713aSLionel Sambuc   __c11_atomic_init(&j, 42);
421f4a2713aSLionel Sambuc 
422f4a2713aSLionel Sambuc   // CHECK-NOT: atomic
423f4a2713aSLionel Sambuc   // CHECK: }
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc 
426*0a6a1f1dSLionel Sambuc // CHECK-LABEL: @failureOrder
failureOrder(_Atomic (int)* ptr,int * ptr2)427*0a6a1f1dSLionel Sambuc void failureOrder(_Atomic(int) *ptr, int *ptr2) {
428*0a6a1f1dSLionel Sambuc   __c11_atomic_compare_exchange_strong(ptr, ptr2, 43, memory_order_acquire, memory_order_relaxed);
429*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg i32* {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z_.]+}} acquire monotonic
430*0a6a1f1dSLionel Sambuc 
431*0a6a1f1dSLionel Sambuc   __c11_atomic_compare_exchange_weak(ptr, ptr2, 43, memory_order_seq_cst, memory_order_acquire);
432*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg weak i32* {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z_.]+}} seq_cst acquire
433*0a6a1f1dSLionel Sambuc 
434*0a6a1f1dSLionel Sambuc   // Unknown ordering: conservatively pick strongest valid option (for now!).
435*0a6a1f1dSLionel Sambuc   __atomic_compare_exchange(ptr2, ptr2, ptr2, 0, memory_order_acq_rel, *ptr2);
436*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg i32* {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z_.]+}} acq_rel acquire
437*0a6a1f1dSLionel Sambuc 
438*0a6a1f1dSLionel Sambuc   // Undefined behaviour: don't really care what that last ordering is so leave
439*0a6a1f1dSLionel Sambuc   // it out:
440*0a6a1f1dSLionel Sambuc   __atomic_compare_exchange_n(ptr2, ptr2, 43, 1, memory_order_seq_cst, 42);
441*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg weak i32* {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z._]+}}, i32 {{%[0-9A-Za-z_.]+}} seq_cst
442*0a6a1f1dSLionel Sambuc }
443*0a6a1f1dSLionel Sambuc 
444*0a6a1f1dSLionel Sambuc // CHECK-LABEL: @generalFailureOrder
generalFailureOrder(_Atomic (int)* ptr,int * ptr2,int success,int fail)445*0a6a1f1dSLionel Sambuc void generalFailureOrder(_Atomic(int) *ptr, int *ptr2, int success, int fail) {
446*0a6a1f1dSLionel Sambuc   __c11_atomic_compare_exchange_strong(ptr, ptr2, 42, success, fail);
447*0a6a1f1dSLionel Sambuc   // CHECK: switch i32 {{.*}}, label %[[MONOTONIC:[0-9a-zA-Z._]+]] [
448*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 1, label %[[ACQUIRE:[0-9a-zA-Z._]+]]
449*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 2, label %[[ACQUIRE]]
450*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 3, label %[[RELEASE:[0-9a-zA-Z._]+]]
451*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 4, label %[[ACQREL:[0-9a-zA-Z._]+]]
452*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 5, label %[[SEQCST:[0-9a-zA-Z._]+]]
453*0a6a1f1dSLionel Sambuc 
454*0a6a1f1dSLionel Sambuc   // CHECK: [[MONOTONIC]]
455*0a6a1f1dSLionel Sambuc   // CHECK: switch {{.*}}, label %[[MONOTONIC_MONOTONIC:[0-9a-zA-Z._]+]] [
456*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ]
457*0a6a1f1dSLionel Sambuc 
458*0a6a1f1dSLionel Sambuc   // CHECK: [[ACQUIRE]]
459*0a6a1f1dSLionel Sambuc   // CHECK: switch {{.*}}, label %[[ACQUIRE_MONOTONIC:[0-9a-zA-Z._]+]] [
460*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 1, label %[[ACQUIRE_ACQUIRE:[0-9a-zA-Z._]+]]
461*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 2, label %[[ACQUIRE_ACQUIRE:[0-9a-zA-Z._]+]]
462*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ]
463*0a6a1f1dSLionel Sambuc 
464*0a6a1f1dSLionel Sambuc   // CHECK: [[RELEASE]]
465*0a6a1f1dSLionel Sambuc   // CHECK: switch {{.*}}, label %[[RELEASE_MONOTONIC:[0-9a-zA-Z._]+]] [
466*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ]
467*0a6a1f1dSLionel Sambuc 
468*0a6a1f1dSLionel Sambuc   // CHECK: [[ACQREL]]
469*0a6a1f1dSLionel Sambuc   // CHECK: switch {{.*}}, label %[[ACQREL_MONOTONIC:[0-9a-zA-Z._]+]] [
470*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 1, label %[[ACQREL_ACQUIRE:[0-9a-zA-Z._]+]]
471*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 2, label %[[ACQREL_ACQUIRE:[0-9a-zA-Z._]+]]
472*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ]
473*0a6a1f1dSLionel Sambuc 
474*0a6a1f1dSLionel Sambuc   // CHECK: [[SEQCST]]
475*0a6a1f1dSLionel Sambuc   // CHECK: switch {{.*}}, label %[[SEQCST_MONOTONIC:[0-9a-zA-Z._]+]] [
476*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 1, label %[[SEQCST_ACQUIRE:[0-9a-zA-Z._]+]]
477*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 2, label %[[SEQCST_ACQUIRE:[0-9a-zA-Z._]+]]
478*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i32 5, label %[[SEQCST_SEQCST:[0-9a-zA-Z._]+]]
479*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ]
480*0a6a1f1dSLionel Sambuc 
481*0a6a1f1dSLionel Sambuc   // CHECK: [[MONOTONIC_MONOTONIC]]
482*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} monotonic monotonic
483*0a6a1f1dSLionel Sambuc   // CHECK: br
484*0a6a1f1dSLionel Sambuc 
485*0a6a1f1dSLionel Sambuc   // CHECK: [[ACQUIRE_MONOTONIC]]
486*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} acquire monotonic
487*0a6a1f1dSLionel Sambuc   // CHECK: br
488*0a6a1f1dSLionel Sambuc 
489*0a6a1f1dSLionel Sambuc   // CHECK: [[ACQUIRE_ACQUIRE]]
490*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} acquire acquire
491*0a6a1f1dSLionel Sambuc   // CHECK: br
492*0a6a1f1dSLionel Sambuc 
493*0a6a1f1dSLionel Sambuc   // CHECK: [[ACQREL_MONOTONIC]]
494*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} acq_rel monotonic
495*0a6a1f1dSLionel Sambuc   // CHECK: br
496*0a6a1f1dSLionel Sambuc 
497*0a6a1f1dSLionel Sambuc   // CHECK: [[ACQREL_ACQUIRE]]
498*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} acq_rel acquire
499*0a6a1f1dSLionel Sambuc   // CHECK: br
500*0a6a1f1dSLionel Sambuc 
501*0a6a1f1dSLionel Sambuc   // CHECK: [[SEQCST_MONOTONIC]]
502*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} seq_cst monotonic
503*0a6a1f1dSLionel Sambuc   // CHECK: br
504*0a6a1f1dSLionel Sambuc 
505*0a6a1f1dSLionel Sambuc   // CHECK: [[SEQCST_ACQUIRE]]
506*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} seq_cst acquire
507*0a6a1f1dSLionel Sambuc   // CHECK: br
508*0a6a1f1dSLionel Sambuc 
509*0a6a1f1dSLionel Sambuc   // CHECK: [[SEQCST_SEQCST]]
510*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} seq_cst seq_cst
511*0a6a1f1dSLionel Sambuc   // CHECK: br
512*0a6a1f1dSLionel Sambuc }
513*0a6a1f1dSLionel Sambuc 
generalWeakness(int * ptr,int * ptr2,_Bool weak)514*0a6a1f1dSLionel Sambuc void generalWeakness(int *ptr, int *ptr2, _Bool weak) {
515*0a6a1f1dSLionel Sambuc   __atomic_compare_exchange_n(ptr, ptr2, 42, weak, memory_order_seq_cst, memory_order_seq_cst);
516*0a6a1f1dSLionel Sambuc   // CHECK: switch i1 {{.*}}, label %[[WEAK:[0-9a-zA-Z._]+]] [
517*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: i1 false, label %[[STRONG:[0-9a-zA-Z._]+]]
518*0a6a1f1dSLionel Sambuc 
519*0a6a1f1dSLionel Sambuc   // CHECK: [[STRONG]]
520*0a6a1f1dSLionel Sambuc   // CHECK-NOT: br
521*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg {{.*}} seq_cst seq_cst
522*0a6a1f1dSLionel Sambuc   // CHECK: br
523*0a6a1f1dSLionel Sambuc 
524*0a6a1f1dSLionel Sambuc   // CHECK: [[WEAK]]
525*0a6a1f1dSLionel Sambuc   // CHECK-NOT: br
526*0a6a1f1dSLionel Sambuc   // CHECK: cmpxchg weak {{.*}} seq_cst seq_cst
527*0a6a1f1dSLionel Sambuc   // CHECK: br
528*0a6a1f1dSLionel Sambuc }
529*0a6a1f1dSLionel Sambuc 
530*0a6a1f1dSLionel Sambuc // Having checked the flow in the previous two cases, we'll trust clang to
531*0a6a1f1dSLionel Sambuc // combine them sanely.
EMIT_ALL_THE_THINGS(int * ptr,int * ptr2,int new,_Bool weak,int success,int fail)532*0a6a1f1dSLionel Sambuc void EMIT_ALL_THE_THINGS(int *ptr, int *ptr2, int new, _Bool weak, int success, int fail) {
533*0a6a1f1dSLionel Sambuc   __atomic_compare_exchange(ptr, ptr2, &new, weak, success, fail);
534*0a6a1f1dSLionel Sambuc 
535*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} monotonic monotonic
536*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} monotonic monotonic
537*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} acquire monotonic
538*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} acquire acquire
539*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} acquire monotonic
540*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} acquire acquire
541*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} release monotonic
542*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} release monotonic
543*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} acq_rel monotonic
544*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} acq_rel acquire
545*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} acq_rel monotonic
546*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} acq_rel acquire
547*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} seq_cst monotonic
548*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} seq_cst acquire
549*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg {{.*}} seq_cst seq_cst
550*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} seq_cst monotonic
551*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} seq_cst acquire
552*0a6a1f1dSLionel Sambuc   // CHECK: = cmpxchg weak {{.*}} seq_cst seq_cst
553*0a6a1f1dSLionel Sambuc }
554*0a6a1f1dSLionel Sambuc 
PR21643()555*0a6a1f1dSLionel Sambuc int PR21643() {
556*0a6a1f1dSLionel Sambuc   return __atomic_or_fetch((int __attribute__((address_space(257))) *)0x308, 1,
557*0a6a1f1dSLionel Sambuc                            __ATOMIC_RELAXED);
558*0a6a1f1dSLionel Sambuc   // CHECK: %[[atomictmp:.*]] = alloca i32, align 4
559*0a6a1f1dSLionel Sambuc   // CHECK: %[[atomicdst:.*]] = alloca i32, align 4
560*0a6a1f1dSLionel Sambuc   // CHECK: store i32 1, i32* %[[atomictmp]]
561*0a6a1f1dSLionel Sambuc   // CHECK: %[[one:.*]] = load i32* %[[atomictmp]], align 4
562*0a6a1f1dSLionel Sambuc   // CHECK: %[[old:.*]] = atomicrmw or i32 addrspace(257)* inttoptr (i32 776 to i32 addrspace(257)*), i32 %[[one]] monotonic
563*0a6a1f1dSLionel Sambuc   // CHECK: %[[new:.*]] = or i32 %[[old]], %[[one]]
564*0a6a1f1dSLionel Sambuc   // CHECK: store i32 %[[new]], i32* %[[atomicdst]], align 4
565*0a6a1f1dSLionel Sambuc   // CHECK: %[[ret:.*]] = load i32* %[[atomicdst]], align 4
566*0a6a1f1dSLionel Sambuc   // CHECK: ret i32 %[[ret]]
567*0a6a1f1dSLionel Sambuc }
568*0a6a1f1dSLionel Sambuc 
PR17306_1(volatile _Atomic (int)* i)569*0a6a1f1dSLionel Sambuc int PR17306_1(volatile _Atomic(int) *i) {
570*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @PR17306_1
571*0a6a1f1dSLionel Sambuc   // CHECK:      %[[i_addr:.*]] = alloca i32
572*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[atomicdst:.*]] = alloca i32
573*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i32* %i, i32** %[[i_addr]]
574*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[addr:.*]] = load i32** %[[i_addr]]
575*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[res:.*]] = load atomic volatile i32* %[[addr]] seq_cst
576*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i32 %[[res]], i32* %[[atomicdst]]
577*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[retval:.*]] = load i32* %[[atomicdst]]
578*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ret i32 %[[retval]]
579*0a6a1f1dSLionel Sambuc   return __c11_atomic_load(i, memory_order_seq_cst);
580*0a6a1f1dSLionel Sambuc }
581*0a6a1f1dSLionel Sambuc 
PR17306_2(volatile int * i,int value)582*0a6a1f1dSLionel Sambuc int PR17306_2(volatile int *i, int value) {
583*0a6a1f1dSLionel Sambuc   // CHECK-LABEL: @PR17306_2
584*0a6a1f1dSLionel Sambuc   // CHECK:      %[[i_addr:.*]] = alloca i32*
585*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[value_addr:.*]] = alloca i32
586*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[atomictmp:.*]] = alloca i32
587*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[atomicdst:.*]] = alloca i32
588*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i32* %i, i32** %[[i_addr]]
589*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i32 %value, i32* %[[value_addr]]
590*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[i_lval:.*]] = load i32** %[[i_addr]]
591*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[value:.*]] = load i32* %[[value_addr]]
592*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i32 %[[value]], i32* %[[atomictmp]]
593*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[value_lval:.*]] = load i32* %[[atomictmp]]
594*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[old_val:.*]] = atomicrmw volatile add i32* %[[i_lval]], i32 %[[value_lval]] seq_cst
595*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[new_val:.*]] = add i32 %[[old_val]], %[[value_lval]]
596*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: store i32 %[[new_val]], i32* %[[atomicdst]]
597*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: %[[retval:.*]] = load i32* %[[atomicdst]]
598*0a6a1f1dSLionel Sambuc   // CHECK-NEXT: ret i32 %[[retval]]
599*0a6a1f1dSLionel Sambuc   return __atomic_add_fetch(i, value, memory_order_seq_cst);
600f4a2713aSLionel Sambuc }
601f4a2713aSLionel Sambuc 
602f4a2713aSLionel Sambuc #endif
603