[LLVM][IR] Use splat syntax when printing ConstantExpr based splats. (#116856)
[llvm-project.git] / clang / test / CodeGen / c11atomics-ios.c
blob8310270ed85478114b8d0851cb58c784a0a222df
1 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=armv7-apple-ios -std=c11 | FileCheck %s
3 // There isn't really anything special about iOS; it just happens to
4 // only deploy on processors with native atomics support, so it's a good
5 // way to test those code-paths.
7 // CHECK-LABEL: define{{.*}} void @testFloat(ptr
8 void testFloat(_Atomic(float) *fp) {
9 // CHECK: [[FP:%.*]] = alloca ptr
10 // CHECK-NEXT: [[X:%.*]] = alloca float
11 // CHECK-NEXT: [[F:%.*]] = alloca float
12 // CHECK-NEXT: store ptr {{%.*}}, ptr [[FP]]
14 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
15 // CHECK-NEXT: store float 1.000000e+00, ptr [[T0]], align 4
16 __c11_atomic_init(fp, 1.0f);
18 // CHECK-NEXT: store float 2.000000e+00, ptr [[X]], align 4
19 _Atomic(float) x = 2.0f;
21 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
22 // CHECK-NEXT: [[T2:%.*]] = load atomic float, ptr [[T0]] seq_cst, align 4
23 // CHECK-NEXT: store float [[T2]], ptr [[F]]
24 float f = *fp;
26 // CHECK-NEXT: [[T0:%.*]] = load float, ptr [[F]], align 4
27 // CHECK-NEXT: [[T1:%.*]] = load ptr, ptr [[FP]], align 4
28 // CHECK-NEXT: store atomic float [[T0]], ptr [[T1]] seq_cst, align 4
29 *fp = f;
31 // CHECK-NEXT: ret void
34 // CHECK: define{{.*}} void @testComplexFloat(ptr
35 void testComplexFloat(_Atomic(_Complex float) *fp) {
36 // CHECK: [[FP:%.*]] = alloca ptr, align 4
37 // CHECK-NEXT: [[X:%.*]] = alloca [[CF:{ float, float }]], align 8
38 // CHECK-NEXT: [[F:%.*]] = alloca [[CF]], align 4
39 // CHECK-NEXT: [[TMP0:%.*]] = alloca [[CF]], align 8
40 // CHECK-NEXT: [[TMP1:%.*]] = alloca [[CF]], align 8
41 // CHECK-NEXT: store ptr
43 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[FP]]
44 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[P]], i32 0, i32 0
45 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[P]], i32 0, i32 1
46 // CHECK-NEXT: store float 1.000000e+00, ptr [[T0]]
47 // CHECK-NEXT: store float 0.000000e+00, ptr [[T1]]
48 __c11_atomic_init(fp, 1.0f);
50 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[X]], i32 0, i32 0
51 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[X]], i32 0, i32 1
52 // CHECK-NEXT: store float 2.000000e+00, ptr [[T0]]
53 // CHECK-NEXT: store float 0.000000e+00, ptr [[T1]]
54 _Atomic(_Complex float) x = 2.0f;
56 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
57 // CHECK-NEXT: [[T2:%.*]] = load atomic i64, ptr [[T0]] seq_cst, align 8
58 // CHECK-NEXT: store i64 [[T2]], ptr [[TMP0]], align 8
59 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[TMP0]], i32 0, i32 0
60 // CHECK-NEXT: [[R:%.*]] = load float, ptr [[T0]]
61 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[TMP0]], i32 0, i32 1
62 // CHECK-NEXT: [[I:%.*]] = load float, ptr [[T0]]
63 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[F]], i32 0, i32 0
64 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[F]], i32 0, i32 1
65 // CHECK-NEXT: store float [[R]], ptr [[T0]]
66 // CHECK-NEXT: store float [[I]], ptr [[T1]]
67 _Complex float f = *fp;
69 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[F]], i32 0, i32 0
70 // CHECK-NEXT: [[R:%.*]] = load float, ptr [[T0]]
71 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[F]], i32 0, i32 1
72 // CHECK-NEXT: [[I:%.*]] = load float, ptr [[T0]]
73 // CHECK-NEXT: [[DEST:%.*]] = load ptr, ptr [[FP]], align 4
74 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[TMP1]], i32 0, i32 0
75 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[CF]], ptr [[TMP1]], i32 0, i32 1
76 // CHECK-NEXT: store float [[R]], ptr [[T0]]
77 // CHECK-NEXT: store float [[I]], ptr [[T1]]
78 // CHECK-NEXT: [[T1:%.*]] = load i64, ptr [[TMP1]], align 8
79 // CHECK-NEXT: store atomic i64 [[T1]], ptr [[DEST]] seq_cst, align 8
80 *fp = f;
82 // CHECK-NEXT: ret void
85 typedef struct { short x, y, z, w; } S;
86 // CHECK: define{{.*}} void @testStruct(ptr
87 void testStruct(_Atomic(S) *fp) {
88 // CHECK: [[FP:%.*]] = alloca ptr, align 4
89 // CHECK-NEXT: [[X:%.*]] = alloca [[S:.*]], align 8
90 // CHECK-NEXT: [[F:%.*]] = alloca [[S:%.*]], align 2
91 // CHECK-NEXT: [[TMP0:%.*]] = alloca [[S]], align 8
92 // CHECK-NEXT: store ptr
94 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[FP]]
95 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[P]], i32 0, i32 0
96 // CHECK-NEXT: store i16 1, ptr [[T0]], align 8
97 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[P]], i32 0, i32 1
98 // CHECK-NEXT: store i16 2, ptr [[T0]], align 2
99 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[P]], i32 0, i32 2
100 // CHECK-NEXT: store i16 3, ptr [[T0]], align 4
101 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[P]], i32 0, i32 3
102 // CHECK-NEXT: store i16 4, ptr [[T0]], align 2
103 __c11_atomic_init(fp, (S){1,2,3,4});
105 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[X]], i32 0, i32 0
106 // CHECK-NEXT: store i16 1, ptr [[T0]], align 8
107 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[X]], i32 0, i32 1
108 // CHECK-NEXT: store i16 2, ptr [[T0]], align 2
109 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[X]], i32 0, i32 2
110 // CHECK-NEXT: store i16 3, ptr [[T0]], align 4
111 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[S]], ptr [[X]], i32 0, i32 3
112 // CHECK-NEXT: store i16 4, ptr [[T0]], align 2
113 _Atomic(S) x = (S){1,2,3,4};
115 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
116 // CHECK-NEXT: [[T2:%.*]] = load atomic i64, ptr [[T0]] seq_cst, align 8
117 // CHECK-NEXT: store i64 [[T2]], ptr [[F]], align 2
118 S f = *fp;
120 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
121 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 8 [[TMP0]], ptr align 2 [[F]], i32 8, i1 false)
122 // CHECK-NEXT: [[T4:%.*]] = load i64, ptr [[TMP0]], align 8
123 // CHECK-NEXT: store atomic i64 [[T4]], ptr [[T0]] seq_cst, align 8
124 *fp = f;
126 // CHECK-NEXT: ret void
129 typedef struct { short x, y, z; } PS;
130 // CHECK: define{{.*}} void @testPromotedStruct(ptr
131 void testPromotedStruct(_Atomic(PS) *fp) {
132 // CHECK: [[FP:%.*]] = alloca ptr, align 4
133 // CHECK-NEXT: [[X:%.*]] = alloca [[APS:.*]], align 8
134 // CHECK-NEXT: [[F:%.*]] = alloca [[PS:%.*]], align 2
135 // CHECK-NEXT: [[TMP0:%.*]] = alloca [[APS]], align 8
136 // CHECK-NEXT: [[TMP1:%.*]] = alloca [[APS]], align 8
137 // CHECK-NEXT: store ptr
139 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[FP]]
140 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[P]], i8 0, i64 8, i1 false)
141 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[APS]], ptr [[P]], i32 0, i32 0
142 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[PS]], ptr [[T0]], i32 0, i32 0
143 // CHECK-NEXT: store i16 1, ptr [[T1]], align 8
144 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[PS]], ptr [[T0]], i32 0, i32 1
145 // CHECK-NEXT: store i16 2, ptr [[T1]], align 2
146 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[PS]], ptr [[T0]], i32 0, i32 2
147 // CHECK-NEXT: store i16 3, ptr [[T1]], align 4
148 __c11_atomic_init(fp, (PS){1,2,3});
150 // CHECK-NEXT: call void @llvm.memset.p0.i32(ptr align 8 [[X]], i8 0, i32 8, i1 false)
151 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[APS]], ptr [[X]], i32 0, i32 0
152 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[PS]], ptr [[T0]], i32 0, i32 0
153 // CHECK-NEXT: store i16 1, ptr [[T1]], align 8
154 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[PS]], ptr [[T0]], i32 0, i32 1
155 // CHECK-NEXT: store i16 2, ptr [[T1]], align 2
156 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[PS]], ptr [[T0]], i32 0, i32 2
157 // CHECK-NEXT: store i16 3, ptr [[T1]], align 4
158 _Atomic(PS) x = (PS){1,2,3};
160 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
161 // CHECK-NEXT: [[T2:%.*]] = load atomic i64, ptr [[T0]] seq_cst, align 8
162 // CHECK-NEXT: store i64 [[T2]], ptr [[TMP0]], align 8
163 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds nuw [[APS]], ptr [[TMP0]], i32 0, i32 0
164 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[F]], ptr align 8 [[T0]], i32 6, i1 false)
165 PS f = *fp;
167 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
168 // CHECK-NEXT: call void @llvm.memset.p0.i32(ptr align 8 [[TMP1]], i8 0, i32 8, i1 false)
169 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds nuw [[APS]], ptr [[TMP1]], i32 0, i32 0
170 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 8 [[T1]], ptr align 2 [[F]], i32 6, i1 false)
171 // CHECK-NEXT: [[T5:%.*]] = load i64, ptr [[TMP1]], align 8
172 // CHECK-NEXT: store atomic i64 [[T5]], ptr [[T0]] seq_cst, align 8
173 *fp = f;
175 // CHECK-NEXT: ret void
178 PS test_promoted_load(_Atomic(PS) *addr) {
179 // CHECK-LABEL: @test_promoted_load(ptr dead_on_unwind noalias writable sret(%struct.PS) align 2 %agg.result, ptr noundef %addr)
180 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
181 // CHECK: [[ATOMIC_RES:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
182 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
183 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
184 // CHECK: [[VAL:%.*]] = load atomic i64, ptr [[ADDR]] seq_cst, align 8
185 // CHECK: store i64 [[VAL]], ptr [[ATOMIC_RES]], align 8
186 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 %agg.result, ptr align 8 [[ATOMIC_RES]], i32 6, i1 false)
188 return __c11_atomic_load(addr, 5);
191 void test_promoted_store(_Atomic(PS) *addr, PS *val) {
192 // CHECK-LABEL: @test_promoted_store(ptr noundef %addr, ptr noundef %val)
193 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
194 // CHECK: [[VAL_ARG:%.*]] = alloca ptr, align 4
195 // CHECK: [[NONATOMIC_TMP:%.*]] = alloca %struct.PS, align 2
196 // CHECK: [[ATOMIC_VAL:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
197 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
198 // CHECK: store ptr %val, ptr [[VAL_ARG]], align 4
199 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
200 // CHECK: [[VAL:%.*]] = load ptr, ptr [[VAL_ARG]], align 4
201 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[NONATOMIC_TMP]], ptr align 2 [[VAL]], i32 6, i1 false)
202 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_VAL]], ptr align 2 [[NONATOMIC_TMP]], i64 6, i1 false)
203 // CHECK: [[VAL64:%.*]] = load i64, ptr [[ATOMIC_VAL]], align 8
204 // CHECK: store atomic i64 [[VAL64]], ptr [[ADDR]] seq_cst, align 8
206 __c11_atomic_store(addr, *val, 5);
209 PS test_promoted_exchange(_Atomic(PS) *addr, PS *val) {
210 // CHECK-LABEL: @test_promoted_exchange(ptr dead_on_unwind noalias writable sret(%struct.PS) align 2 %agg.result, ptr noundef %addr, ptr noundef %val)
211 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
212 // CHECK: [[VAL_ARG:%.*]] = alloca ptr, align 4
213 // CHECK: [[NONATOMIC_TMP:%.*]] = alloca %struct.PS, align 2
214 // CHECK: [[ATOMIC_VAL:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
215 // CHECK: [[ATOMIC_RES:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
216 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
217 // CHECK: store ptr %val, ptr [[VAL_ARG]], align 4
218 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
219 // CHECK: [[VAL:%.*]] = load ptr, ptr [[VAL_ARG]], align 4
220 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[NONATOMIC_TMP]], ptr align 2 [[VAL]], i32 6, i1 false)
221 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_VAL]], ptr align 2 [[NONATOMIC_TMP]], i64 6, i1 false)
222 // CHECK: [[VAL64:%.*]] = load i64, ptr [[ATOMIC_VAL]], align 8
223 // CHECK: [[RES:%.*]] = atomicrmw xchg ptr [[ADDR]], i64 [[VAL64]] seq_cst, align 8
224 // CHECK: store i64 [[RES]], ptr [[ATOMIC_RES]], align 8
225 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 %agg.result, ptr align 8 [[ATOMIC_RES]], i32 6, i1 false)
226 return __c11_atomic_exchange(addr, *val, 5);
229 _Bool test_promoted_cmpxchg(_Atomic(PS) *addr, PS *desired, PS *new) {
230 // CHECK: define{{.*}} zeroext i1 @test_promoted_cmpxchg(ptr noundef %addr, ptr noundef %desired, ptr noundef %new) #0 {
231 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
232 // CHECK: [[DESIRED_ARG:%.*]] = alloca ptr, align 4
233 // CHECK: [[NEW_ARG:%.*]] = alloca ptr, align 4
234 // CHECK: [[NONATOMIC_TMP:%.*]] = alloca %struct.PS, align 2
235 // CHECK: [[ATOMIC_DESIRED:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
236 // CHECK: [[ATOMIC_NEW:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
237 // CHECK: [[RES_ADDR:%.*]] = alloca i8, align 1
238 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
239 // CHECK: store ptr %desired, ptr [[DESIRED_ARG]], align 4
240 // CHECK: store ptr %new, ptr [[NEW_ARG]], align 4
241 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
242 // CHECK: [[DESIRED:%.*]] = load ptr, ptr [[DESIRED_ARG]], align 4
243 // CHECK: [[NEW:%.*]] = load ptr, ptr [[NEW_ARG]], align 4
244 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[NONATOMIC_TMP]], ptr align 2 [[NEW]], i32 6, i1 false)
245 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_DESIRED:%.*]], ptr align 2 [[DESIRED]], i64 6, i1 false)
246 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_NEW]], ptr align 2 [[NONATOMIC_TMP]], i64 6, i1 false)
247 // CHECK: [[ATOMIC_DESIRED_VAL64:%.*]] = load i64, ptr [[ATOMIC_DESIRED:%.*]], align 8
248 // CHECK: [[ATOMIC_NEW_VAL64:%.*]] = load i64, ptr [[ATOMIC_NEW]], align 8
249 // CHECK: [[RES:%.*]] = cmpxchg ptr [[ADDR]], i64 [[ATOMIC_DESIRED_VAL64]], i64 [[ATOMIC_NEW_VAL64]] seq_cst seq_cst, align 8
250 // CHECK: [[RES_VAL64:%.*]] = extractvalue { i64, i1 } [[RES]], 0
251 // CHECK: [[RES_BOOL:%.*]] = extractvalue { i64, i1 } [[RES]], 1
252 // CHECK: br i1 [[RES_BOOL]], label {{%.*}}, label {{%.*}}
254 // CHECK: store i64 [[RES_VAL64]], ptr [[ATOMIC_DESIRED]], align 8
255 // CHECK: br label {{%.*}}
257 // CHECK: [[RES_BOOL8:%.*]] = zext i1 [[RES_BOOL]] to i8
258 // CHECK: store i8 [[RES_BOOL8]], ptr [[RES_ADDR]], align 1
259 // CHECK: [[RES_BOOL8:%.*]] = load i8, ptr [[RES_ADDR]], align 1
260 // CHECK: [[RETVAL:%.*]] = trunc i8 [[RES_BOOL8]] to i1
261 // CHECK: ret i1 [[RETVAL]]
263 return __c11_atomic_compare_exchange_strong(addr, desired, *new, 5, 5);
266 struct Empty {};
268 struct Empty testEmptyStructLoad(_Atomic(struct Empty)* empty) {
269 // CHECK-LABEL: @testEmptyStructLoad(
270 // CHECK-NOT: @__atomic_load
271 // CHECK: load atomic i8, ptr %{{.*}} seq_cst, align 1
272 return *empty;
275 void testEmptyStructStore(_Atomic(struct Empty)* empty, struct Empty value) {
276 // CHECK-LABEL: @testEmptyStructStore(
277 // CHECK-NOT: @__atomic_store
278 // CHECK: store atomic i8 %{{.*}}, ptr %{{.*}} seq_cst, align 1
279 *empty = value;