xref: /llvm-project/clang/test/CodeGenObjC/weak-in-c-struct.m (revision 158d72d728261c1e54dc77931372b2322c52849f)
1// RUN: %clang_cc1 -triple arm64-apple-ios11 -fobjc-arc -fblocks -fobjc-runtime=ios-11.0 -emit-llvm -o - %s | FileCheck -check-prefix=ARM64 -check-prefix=COMMON %s
2// RUN: %clang_cc1 -triple thumbv7-apple-ios10 -fobjc-arc -fblocks -fobjc-runtime=ios-10.0 -emit-llvm -o - %s | FileCheck -check-prefix=COMMON %s
3// RUN: %clang_cc1 -triple x86_64-apple-macosx10.13 -fobjc-arc -fblocks -fobjc-runtime=macosx-10.13.0 -emit-llvm -o - %s | FileCheck -check-prefix=COMMON %s
4// RUN: %clang_cc1 -triple i386-apple-macosx10.13.0 -fobjc-arc -fblocks -fobjc-runtime=macosx-fragile-10.13.0 -emit-llvm -o - %s | FileCheck -check-prefix=COMMON %s
5
6typedef void (^BlockTy)(void);
7
8// COMMON: %[[STRUCT_WEAK:.*]] = type { i32, ptr }
9
10typedef struct {
11  int f0;
12  __weak id f1;
13} Weak;
14
15Weak getWeak(void);
16void calleeWeak(Weak);
17
18// ARM64: define{{.*}} void @test_constructor_destructor_Weak()
19// ARM64: %[[T:.*]] = alloca %[[STRUCT_WEAK]], align 8
20// ARM64: call void @__default_constructor_8_w8(ptr %[[T]])
21// ARM64: call void @__destructor_8_w8(ptr %[[T]])
22// ARM64: ret void
23
24// ARM64: define linkonce_odr hidden void @__default_constructor_8_w8(ptr noundef %[[DST:.*]])
25// ARM64: %[[DST_ADDR:.*]] = alloca ptr, align 8
26// ARM64: store ptr %[[DST]], ptr %[[DST_ADDR]], align 8
27// ARM64: %[[V0:.*]] = load ptr, ptr %[[DST_ADDR]], align 8
28// ARM64: %[[V2:.*]] = getelementptr inbounds i8, ptr %[[V0]], i64 8
29// ARM64: call void @llvm.memset.p0.i64(ptr align 8 %[[V2]], i8 0, i64 8, i1 false)
30
31// ARM64: define linkonce_odr hidden void @__destructor_8_w8(ptr noundef %[[DST:.*]])
32// ARM64: %[[DST_ADDR:.*]] = alloca ptr, align 8
33// ARM64: store ptr %[[DST]], ptr %[[DST_ADDR]], align 8
34// ARM64: %[[V0:.*]] = load ptr, ptr %[[DST_ADDR]], align 8
35// ARM64: %[[V2:.*]] = getelementptr inbounds i8, ptr %[[V0]], i64 8
36// ARM64: call void @llvm.objc.destroyWeak(ptr %[[V2]])
37
38@interface C
39- (void)m:(Weak)a;
40@end
41
42void test_constructor_destructor_Weak(void) {
43  Weak t;
44}
45
46// ARM64: define{{.*}} void @test_copy_constructor_Weak(ptr noundef %{{.*}})
47// ARM64: call void @__copy_constructor_8_8_t0w4_w8(ptr %{{.*}}, ptr %{{.*}})
48// ARM64: call void @__destructor_8_w8(ptr %{{.*}})
49
50// ARM64: define linkonce_odr hidden void @__copy_constructor_8_8_t0w4_w8(ptr noundef %[[DST:.*]], ptr noundef %[[SRC:.*]])
51// ARM64: %[[DST_ADDR:.*]] = alloca ptr, align 8
52// ARM64: %[[SRC_ADDR:.*]] = alloca ptr, align 8
53// ARM64: store ptr %[[DST]], ptr %[[DST_ADDR]], align 8
54// ARM64: store ptr %[[SRC]], ptr %[[SRC_ADDR]], align 8
55// ARM64: %[[V0:.*]] = load ptr, ptr %[[DST_ADDR]], align 8
56// ARM64: %[[V1:.*]] = load ptr, ptr %[[SRC_ADDR]], align 8
57// ARM64: %[[V4:.*]] = load i32, ptr %[[V1]], align 8
58// ARM64: store i32 %[[V4]], ptr %[[V0]], align 8
59// ARM64: %[[V6:.*]] = getelementptr inbounds i8, ptr %[[V0]], i64 8
60// ARM64: %[[V9:.*]] = getelementptr inbounds i8, ptr %[[V1]], i64 8
61// ARM64: call void @llvm.objc.copyWeak(ptr %[[V6]], ptr %[[V9]])
62
63void test_copy_constructor_Weak(Weak *s) {
64  Weak t = *s;
65}
66
67// ARM64: define{{.*}} void @test_copy_assignment_Weak(ptr noundef %{{.*}}, ptr noundef %{{.*}})
68// ARM64: call void @__copy_assignment_8_8_t0w4_w8(ptr %{{.*}}, ptr %{{.*}})
69
70// ARM64: define linkonce_odr hidden void @__copy_assignment_8_8_t0w4_w8(ptr noundef %[[DST:.*]], ptr noundef %[[SRC:.*]])
71// ARM64: %[[DST_ADDR:.*]] = alloca ptr, align 8
72// ARM64: %[[SRC_ADDR:.*]] = alloca ptr, align 8
73// ARM64: store ptr %[[DST]], ptr %[[DST_ADDR]], align 8
74// ARM64: store ptr %[[SRC]], ptr %[[SRC_ADDR]], align 8
75// ARM64: %[[V0:.*]] = load ptr, ptr %[[DST_ADDR]], align 8
76// ARM64: %[[V1:.*]] = load ptr, ptr %[[SRC_ADDR]], align 8
77// ARM64: %[[V4:.*]] = load i32, ptr %[[V1]], align 8
78// ARM64: store i32 %[[V4]], ptr %[[V0]], align 8
79// ARM64: %[[V6:.*]] = getelementptr inbounds i8, ptr %[[V0]], i64 8
80// ARM64: %[[V9:.*]] = getelementptr inbounds i8, ptr %[[V1]], i64 8
81// ARM64: %[[V11:.*]] = call ptr @llvm.objc.loadWeakRetained(ptr %[[V9]])
82// ARM64: %[[V12:.*]] = call ptr @llvm.objc.storeWeak(ptr %[[V6]], ptr %[[V11]])
83// ARM64: call void @llvm.objc.release(ptr %[[V11]])
84
85void test_copy_assignment_Weak(Weak *d, Weak *s) {
86  *d = *s;
87}
88
89// ARM64: define internal void @__Block_byref_object_copy_(ptr noundef %0, ptr noundef %1)
90// ARM64: call void @__move_constructor_8_8_t0w4_w8(ptr %{{.*}}, ptr %{{.*}})
91
92// ARM64: define linkonce_odr hidden void @__move_constructor_8_8_t0w4_w8(ptr noundef %[[DST:.*]], ptr noundef %[[SRC:.*]])
93// ARM64: %[[DST_ADDR:.*]] = alloca ptr, align 8
94// ARM64: %[[SRC_ADDR:.*]] = alloca ptr, align 8
95// ARM64: store ptr %[[DST]], ptr %[[DST_ADDR]], align 8
96// ARM64: store ptr %[[SRC]], ptr %[[SRC_ADDR]], align 8
97// ARM64: %[[V0:.*]] = load ptr, ptr %[[DST_ADDR]], align 8
98// ARM64: %[[V1:.*]] = load ptr, ptr %[[SRC_ADDR]], align 8
99// ARM64: %[[V4:.*]] = load i32, ptr %[[V1]], align 8
100// ARM64: store i32 %[[V4]], ptr %[[V0]], align 8
101// ARM64: %[[V6:.*]] = getelementptr inbounds i8, ptr %[[V0]], i64 8
102// ARM64: %[[V9:.*]] = getelementptr inbounds i8, ptr %[[V1]], i64 8
103// ARM64: call void @llvm.objc.moveWeak(ptr %[[V6]], ptr %[[V9]])
104
105void test_move_constructor_Weak(void) {
106  __block Weak t;
107  BlockTy b = ^{ (void)t; };
108}
109
110// ARM64: define{{.*}} void @test_move_assignment_Weak(ptr noundef %{{.*}})
111// ARM64: call void @__move_assignment_8_8_t0w4_w8(ptr %{{.*}}, ptr %{{.*}})
112
113// ARM64: define linkonce_odr hidden void @__move_assignment_8_8_t0w4_w8(ptr noundef %[[DST:.*]], ptr noundef %[[SRC:.*]])
114// ARM64: %[[DST_ADDR:.*]] = alloca ptr, align 8
115// ARM64: %[[SRC_ADDR:.*]] = alloca ptr, align 8
116// ARM64: store ptr %[[DST]], ptr %[[DST_ADDR]], align 8
117// ARM64: store ptr %[[SRC]], ptr %[[SRC_ADDR]], align 8
118// ARM64: %[[V0:.*]] = load ptr, ptr %[[DST_ADDR]], align 8
119// ARM64: %[[V1:.*]] = load ptr, ptr %[[SRC_ADDR]], align 8
120// ARM64: %[[V4:.*]] = load i32, ptr %[[V1]], align 8
121// ARM64: store i32 %[[V4]], ptr %[[V0]], align 8
122// ARM64: %[[V6:.*]] = getelementptr inbounds i8, ptr %[[V0]], i64 8
123// ARM64: %[[V9:.*]] = getelementptr inbounds i8, ptr %[[V1]], i64 8
124// ARM64: %[[V11:.*]] = call ptr @llvm.objc.loadWeakRetained(ptr %[[V9]])
125// ARM64: %[[V12:.*]] = call ptr @llvm.objc.storeWeak(ptr %[[V6]], ptr %[[V11]])
126// ARM64: call void @llvm.objc.destroyWeak(ptr %[[V9]])
127// ARM64: call void @llvm.objc.release(ptr %[[V11]])
128
129void test_move_assignment_Weak(Weak *p) {
130  *p = getWeak();
131}
132
133// COMMON: define{{.*}} void @test_parameter_Weak(ptr noundef %[[A:.*]])
134// COMMON: call void @__destructor_{{.*}}(ptr %[[A]])
135
136void test_parameter_Weak(Weak a) {
137}
138
139// COMMON: define{{.*}} void @test_argument_Weak(ptr noundef %[[A:.*]])
140// COMMON: %[[A_ADDR:.*]] = alloca ptr
141// COMMON: %[[AGG_TMP:.*]] = alloca %[[STRUCT_WEAK]]
142// COMMON: store ptr %[[A]], ptr %[[A_ADDR]]
143// COMMON: %[[V0:.*]] = load ptr, ptr %[[A_ADDR]]
144// COMMON: call void @__copy_constructor_{{.*}}(ptr %[[AGG_TMP]], ptr %[[V0]])
145// COMMON: call void @calleeWeak(ptr noundef %[[AGG_TMP]])
146// COMMON-NEXT: ret
147
148void test_argument_Weak(Weak *a) {
149  calleeWeak(*a);
150}
151
152// COMMON: define{{.*}} void @test_return_Weak(ptr dead_on_unwind noalias writable sret(%[[STRUCT_WEAK]]) align {{.*}} %[[AGG_RESULT:.*]], ptr noundef %[[A:.*]])
153// COMMON: %[[A_ADDR:.*]] = alloca ptr
154// COMMON: store ptr %[[A]], ptr %[[A_ADDR]]
155// COMMON: %[[V0:.*]] = load ptr, ptr %[[A_ADDR]]
156// COMMON: call void @__copy_constructor_{{.*}}(ptr %[[AGG_RESULT]], ptr %[[V0]])
157// COMMON: ret void
158
159Weak test_return_Weak(Weak *a) {
160  return *a;
161}
162
163// COMMON-LABEL: define{{.*}} void @test_null_receiver(
164// COMMON: %[[AGG_TMP:.*]] = alloca %[[STRUCT_WEAK]]
165// COMMON: br i1
166
167// COMMON: call void @objc_msgSend({{.*}}, ptr noundef %[[AGG_TMP]])
168// COMMON: br
169
170// COMMON: call void @__destructor_{{.*}}(ptr %[[AGG_TMP]])
171// COMMON: br
172
173void test_null_receiver(C *c) {
174  [c m:getWeak()];
175}
176