Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGen / c11atomics-ios.c
blobaf489811edc58f8819b531e95f6683612a88a33b
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 i32, ptr [[T0]] seq_cst, align 4
23 // CHECK-NEXT: [[T3:%.*]] = bitcast i32 [[T2]] to float
24 // CHECK-NEXT: store float [[T3]], ptr [[F]]
25 float f = *fp;
27 // CHECK-NEXT: [[T0:%.*]] = load float, ptr [[F]], align 4
28 // CHECK-NEXT: [[T1:%.*]] = load ptr, ptr [[FP]], align 4
29 // CHECK-NEXT: [[T2:%.*]] = bitcast float [[T0]] to i32
30 // CHECK-NEXT: store atomic i32 [[T2]], ptr [[T1]] seq_cst, align 4
31 *fp = f;
33 // CHECK-NEXT: ret void
36 // CHECK: define{{.*}} void @testComplexFloat(ptr
37 void testComplexFloat(_Atomic(_Complex float) *fp) {
38 // CHECK: [[FP:%.*]] = alloca ptr, align 4
39 // CHECK-NEXT: [[X:%.*]] = alloca [[CF:{ float, float }]], align 8
40 // CHECK-NEXT: [[F:%.*]] = alloca [[CF]], align 4
41 // CHECK-NEXT: [[TMP0:%.*]] = alloca [[CF]], align 8
42 // CHECK-NEXT: [[TMP1:%.*]] = alloca [[CF]], align 8
43 // CHECK-NEXT: store ptr
45 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[FP]]
46 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[P]], i32 0, i32 0
47 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[CF]], ptr [[P]], i32 0, i32 1
48 // CHECK-NEXT: store float 1.000000e+00, ptr [[T0]]
49 // CHECK-NEXT: store float 0.000000e+00, ptr [[T1]]
50 __c11_atomic_init(fp, 1.0f);
52 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[X]], i32 0, i32 0
53 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[CF]], ptr [[X]], i32 0, i32 1
54 // CHECK-NEXT: store float 2.000000e+00, ptr [[T0]]
55 // CHECK-NEXT: store float 0.000000e+00, ptr [[T1]]
56 _Atomic(_Complex float) x = 2.0f;
58 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
59 // CHECK-NEXT: [[T2:%.*]] = load atomic i64, ptr [[T0]] seq_cst, align 8
60 // CHECK-NEXT: store i64 [[T2]], ptr [[TMP0]], align 8
61 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[TMP0]], i32 0, i32 0
62 // CHECK-NEXT: [[R:%.*]] = load float, ptr [[T0]]
63 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[TMP0]], i32 0, i32 1
64 // CHECK-NEXT: [[I:%.*]] = load float, ptr [[T0]]
65 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[F]], i32 0, i32 0
66 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[CF]], ptr [[F]], i32 0, i32 1
67 // CHECK-NEXT: store float [[R]], ptr [[T0]]
68 // CHECK-NEXT: store float [[I]], ptr [[T1]]
69 _Complex float f = *fp;
71 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[F]], i32 0, i32 0
72 // CHECK-NEXT: [[R:%.*]] = load float, ptr [[T0]]
73 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[F]], i32 0, i32 1
74 // CHECK-NEXT: [[I:%.*]] = load float, ptr [[T0]]
75 // CHECK-NEXT: [[DEST:%.*]] = load ptr, ptr [[FP]], align 4
76 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[CF]], ptr [[TMP1]], i32 0, i32 0
77 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[CF]], ptr [[TMP1]], i32 0, i32 1
78 // CHECK-NEXT: store float [[R]], ptr [[T0]]
79 // CHECK-NEXT: store float [[I]], ptr [[T1]]
80 // CHECK-NEXT: [[T1:%.*]] = load i64, ptr [[TMP1]], align 8
81 // CHECK-NEXT: store atomic i64 [[T1]], ptr [[DEST]] seq_cst, align 8
82 *fp = f;
84 // CHECK-NEXT: ret void
87 typedef struct { short x, y, z, w; } S;
88 // CHECK: define{{.*}} void @testStruct(ptr
89 void testStruct(_Atomic(S) *fp) {
90 // CHECK: [[FP:%.*]] = alloca ptr, align 4
91 // CHECK-NEXT: [[X:%.*]] = alloca [[S:.*]], align 8
92 // CHECK-NEXT: [[F:%.*]] = alloca [[S:%.*]], align 2
93 // CHECK-NEXT: [[TMP0:%.*]] = alloca [[S]], align 8
94 // CHECK-NEXT: store ptr
96 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[FP]]
97 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[P]], i32 0, i32 0
98 // CHECK-NEXT: store i16 1, ptr [[T0]], align 8
99 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[P]], i32 0, i32 1
100 // CHECK-NEXT: store i16 2, ptr [[T0]], align 2
101 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[P]], i32 0, i32 2
102 // CHECK-NEXT: store i16 3, ptr [[T0]], align 4
103 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[P]], i32 0, i32 3
104 // CHECK-NEXT: store i16 4, ptr [[T0]], align 2
105 __c11_atomic_init(fp, (S){1,2,3,4});
107 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[X]], i32 0, i32 0
108 // CHECK-NEXT: store i16 1, ptr [[T0]], align 8
109 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[X]], i32 0, i32 1
110 // CHECK-NEXT: store i16 2, ptr [[T0]], align 2
111 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[X]], i32 0, i32 2
112 // CHECK-NEXT: store i16 3, ptr [[T0]], align 4
113 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[S]], ptr [[X]], i32 0, i32 3
114 // CHECK-NEXT: store i16 4, ptr [[T0]], align 2
115 _Atomic(S) x = (S){1,2,3,4};
117 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
118 // CHECK-NEXT: [[T2:%.*]] = load atomic i64, ptr [[T0]] seq_cst, align 8
119 // CHECK-NEXT: store i64 [[T2]], ptr [[F]], align 2
120 S f = *fp;
122 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
123 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 8 [[TMP0]], ptr align 2 [[F]], i32 8, i1 false)
124 // CHECK-NEXT: [[T4:%.*]] = load i64, ptr [[TMP0]], align 8
125 // CHECK-NEXT: store atomic i64 [[T4]], ptr [[T0]] seq_cst, align 8
126 *fp = f;
128 // CHECK-NEXT: ret void
131 typedef struct { short x, y, z; } PS;
132 // CHECK: define{{.*}} void @testPromotedStruct(ptr
133 void testPromotedStruct(_Atomic(PS) *fp) {
134 // CHECK: [[FP:%.*]] = alloca ptr, align 4
135 // CHECK-NEXT: [[X:%.*]] = alloca [[APS:.*]], align 8
136 // CHECK-NEXT: [[F:%.*]] = alloca [[PS:%.*]], align 2
137 // CHECK-NEXT: [[TMP0:%.*]] = alloca [[APS]], align 8
138 // CHECK-NEXT: [[TMP1:%.*]] = alloca [[APS]], align 8
139 // CHECK-NEXT: store ptr
141 // CHECK-NEXT: [[P:%.*]] = load ptr, ptr [[FP]]
142 // CHECK-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[P]], i8 0, i64 8, i1 false)
143 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[APS]], ptr [[P]], i32 0, i32 0
144 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[PS]], ptr [[T0]], i32 0, i32 0
145 // CHECK-NEXT: store i16 1, ptr [[T1]], align 8
146 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[PS]], ptr [[T0]], i32 0, i32 1
147 // CHECK-NEXT: store i16 2, ptr [[T1]], align 2
148 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[PS]], ptr [[T0]], i32 0, i32 2
149 // CHECK-NEXT: store i16 3, ptr [[T1]], align 4
150 __c11_atomic_init(fp, (PS){1,2,3});
152 // CHECK-NEXT: call void @llvm.memset.p0.i32(ptr align 8 [[X]], i8 0, i32 8, i1 false)
153 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[APS]], ptr [[X]], i32 0, i32 0
154 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[PS]], ptr [[T0]], i32 0, i32 0
155 // CHECK-NEXT: store i16 1, ptr [[T1]], align 8
156 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[PS]], ptr [[T0]], i32 0, i32 1
157 // CHECK-NEXT: store i16 2, ptr [[T1]], align 2
158 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[PS]], ptr [[T0]], i32 0, i32 2
159 // CHECK-NEXT: store i16 3, ptr [[T1]], align 4
160 _Atomic(PS) x = (PS){1,2,3};
162 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
163 // CHECK-NEXT: [[T2:%.*]] = load atomic i64, ptr [[T0]] seq_cst, align 8
164 // CHECK-NEXT: store i64 [[T2]], ptr [[TMP0]], align 8
165 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[APS]], ptr [[TMP0]], i32 0, i32 0
166 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[F]], ptr align 8 [[T0]], i32 6, i1 false)
167 PS f = *fp;
169 // CHECK-NEXT: [[T0:%.*]] = load ptr, ptr [[FP]]
170 // CHECK-NEXT: call void @llvm.memset.p0.i32(ptr align 8 [[TMP1]], i8 0, i32 8, i1 false)
171 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[APS]], ptr [[TMP1]], i32 0, i32 0
172 // CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 8 [[T1]], ptr align 2 [[F]], i32 6, i1 false)
173 // CHECK-NEXT: [[T5:%.*]] = load i64, ptr [[TMP1]], align 8
174 // CHECK-NEXT: store atomic i64 [[T5]], ptr [[T0]] seq_cst, align 8
175 *fp = f;
177 // CHECK-NEXT: ret void
180 PS test_promoted_load(_Atomic(PS) *addr) {
181 // CHECK-LABEL: @test_promoted_load(ptr noalias sret(%struct.PS) align 2 %agg.result, ptr noundef %addr)
182 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
183 // CHECK: [[ATOMIC_RES:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
184 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
185 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
186 // CHECK: [[VAL:%.*]] = load atomic i64, ptr [[ADDR]] seq_cst, align 8
187 // CHECK: store i64 [[VAL]], ptr [[ATOMIC_RES]], align 8
188 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 %agg.result, ptr align 8 [[ATOMIC_RES]], i32 6, i1 false)
190 return __c11_atomic_load(addr, 5);
193 void test_promoted_store(_Atomic(PS) *addr, PS *val) {
194 // CHECK-LABEL: @test_promoted_store(ptr noundef %addr, ptr noundef %val)
195 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
196 // CHECK: [[VAL_ARG:%.*]] = alloca ptr, align 4
197 // CHECK: [[NONATOMIC_TMP:%.*]] = alloca %struct.PS, align 2
198 // CHECK: [[ATOMIC_VAL:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
199 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
200 // CHECK: store ptr %val, ptr [[VAL_ARG]], align 4
201 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
202 // CHECK: [[VAL:%.*]] = load ptr, ptr [[VAL_ARG]], align 4
203 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[NONATOMIC_TMP]], ptr align 2 [[VAL]], i32 6, i1 false)
204 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_VAL]], ptr align 2 [[NONATOMIC_TMP]], i64 6, i1 false)
205 // CHECK: [[VAL64:%.*]] = load i64, ptr [[ATOMIC_VAL]], align 8
206 // CHECK: store atomic i64 [[VAL64]], ptr [[ADDR]] seq_cst, align 8
208 __c11_atomic_store(addr, *val, 5);
211 PS test_promoted_exchange(_Atomic(PS) *addr, PS *val) {
212 // CHECK-LABEL: @test_promoted_exchange(ptr noalias sret(%struct.PS) align 2 %agg.result, ptr noundef %addr, ptr noundef %val)
213 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
214 // CHECK: [[VAL_ARG:%.*]] = alloca ptr, align 4
215 // CHECK: [[NONATOMIC_TMP:%.*]] = alloca %struct.PS, align 2
216 // CHECK: [[ATOMIC_VAL:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
217 // CHECK: [[ATOMIC_RES:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
218 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
219 // CHECK: store ptr %val, ptr [[VAL_ARG]], align 4
220 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
221 // CHECK: [[VAL:%.*]] = load ptr, ptr [[VAL_ARG]], align 4
222 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[NONATOMIC_TMP]], ptr align 2 [[VAL]], i32 6, i1 false)
223 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_VAL]], ptr align 2 [[NONATOMIC_TMP]], i64 6, i1 false)
224 // CHECK: [[VAL64:%.*]] = load i64, ptr [[ATOMIC_VAL]], align 8
225 // CHECK: [[RES:%.*]] = atomicrmw xchg ptr [[ADDR]], i64 [[VAL64]] seq_cst, align 8
226 // CHECK: store i64 [[RES]], ptr [[ATOMIC_RES]], align 8
227 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 %agg.result, ptr align 8 [[ATOMIC_RES]], i32 6, i1 false)
228 return __c11_atomic_exchange(addr, *val, 5);
231 _Bool test_promoted_cmpxchg(_Atomic(PS) *addr, PS *desired, PS *new) {
232 // CHECK: define{{.*}} zeroext i1 @test_promoted_cmpxchg(ptr noundef %addr, ptr noundef %desired, ptr noundef %new) #0 {
233 // CHECK: [[ADDR_ARG:%.*]] = alloca ptr, align 4
234 // CHECK: [[DESIRED_ARG:%.*]] = alloca ptr, align 4
235 // CHECK: [[NEW_ARG:%.*]] = alloca ptr, align 4
236 // CHECK: [[NONATOMIC_TMP:%.*]] = alloca %struct.PS, align 2
237 // CHECK: [[ATOMIC_DESIRED:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
238 // CHECK: [[ATOMIC_NEW:%.*]] = alloca { %struct.PS, [2 x i8] }, align 8
239 // CHECK: [[RES_ADDR:%.*]] = alloca i8, align 1
240 // CHECK: store ptr %addr, ptr [[ADDR_ARG]], align 4
241 // CHECK: store ptr %desired, ptr [[DESIRED_ARG]], align 4
242 // CHECK: store ptr %new, ptr [[NEW_ARG]], align 4
243 // CHECK: [[ADDR:%.*]] = load ptr, ptr [[ADDR_ARG]], align 4
244 // CHECK: [[DESIRED:%.*]] = load ptr, ptr [[DESIRED_ARG]], align 4
245 // CHECK: [[NEW:%.*]] = load ptr, ptr [[NEW_ARG]], align 4
246 // CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 2 [[NONATOMIC_TMP]], ptr align 2 [[NEW]], i32 6, i1 false)
247 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_DESIRED:%.*]], ptr align 2 [[DESIRED]], i64 6, i1 false)
248 // CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[ATOMIC_NEW]], ptr align 2 [[NONATOMIC_TMP]], i64 6, i1 false)
249 // CHECK: [[ATOMIC_DESIRED_VAL64:%.*]] = load i64, ptr [[ATOMIC_DESIRED:%.*]], align 8
250 // CHECK: [[ATOMIC_NEW_VAL64:%.*]] = load i64, ptr [[ATOMIC_NEW]], align 8
251 // CHECK: [[RES:%.*]] = cmpxchg ptr [[ADDR]], i64 [[ATOMIC_DESIRED_VAL64]], i64 [[ATOMIC_NEW_VAL64]] seq_cst seq_cst, align 8
252 // CHECK: [[RES_VAL64:%.*]] = extractvalue { i64, i1 } [[RES]], 0
253 // CHECK: [[RES_BOOL:%.*]] = extractvalue { i64, i1 } [[RES]], 1
254 // CHECK: br i1 [[RES_BOOL]], label {{%.*}}, label {{%.*}}
256 // CHECK: store i64 [[RES_VAL64]], ptr [[ATOMIC_DESIRED]], align 8
257 // CHECK: br label {{%.*}}
259 // CHECK: [[RES_BOOL8:%.*]] = zext i1 [[RES_BOOL]] to i8
260 // CHECK: store i8 [[RES_BOOL8]], ptr [[RES_ADDR]], align 1
261 // CHECK: [[RES_BOOL8:%.*]] = load i8, ptr [[RES_ADDR]], align 1
262 // CHECK: [[RETVAL:%.*]] = trunc i8 [[RES_BOOL8]] to i1
263 // CHECK: ret i1 [[RETVAL]]
265 return __c11_atomic_compare_exchange_strong(addr, desired, *new, 5, 5);
268 struct Empty {};
270 struct Empty testEmptyStructLoad(_Atomic(struct Empty)* empty) {
271 // CHECK-LABEL: @testEmptyStructLoad(
272 // CHECK-NOT: @__atomic_load
273 // CHECK: load atomic i8, ptr %{{.*}} seq_cst, align 1
274 return *empty;
277 void testEmptyStructStore(_Atomic(struct Empty)* empty, struct Empty value) {
278 // CHECK-LABEL: @testEmptyStructStore(
279 // CHECK-NOT: @__atomic_store
280 // CHECK: store atomic i8 %{{.*}}, ptr %{{.*}} seq_cst, align 1
281 *empty = value;