1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fblocks -emit-llvm -o - %s | FileCheck -check-prefix CHECK -check-prefix CHECK-NOARC %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fblocks -emit-llvm -fobjc-arc -o - %s | FileCheck -check-prefix CHECK -check-prefix CHECK-ARC %s
4 typedef void (^BlockTy)(void);
9 } __attribute__((transparent_union));
11 void escapingFunc0(BlockTy);
12 void noescapeFunc0(id, __attribute__((noescape)) BlockTy);
13 void noescapeFunc1(__attribute__((noescape)) int *);
14 void noescapeFunc2(__attribute__((noescape)) id);
15 void noescapeFunc3(__attribute__((noescape)) union U);
17 // Block descriptors of non-escaping blocks don't need pointers to copy/dispose
20 // When the block is non-escaping, copy/dispose helpers aren't generated, so the
21 // block layout string must include information about __strong captures.
23 // CHECK-NOARC: %[[STRUCT_BLOCK_BYREF_B0:.*]] = type { ptr, ptr, i32, i32, ptr, %[[STRUCT_S0:.*]] }
24 // CHECK-ARC: %[[STRUCT_BLOCK_BYREF_B0:.*]] = type { ptr, ptr, i32, i32, ptr, ptr, ptr, %[[STRUCT_S0:.*]] }
25 // CHECK: %[[STRUCT_S0]] = type { ptr, ptr }
26 // CHECK: @[[BLOCK_DESCIPTOR_TMP_2:.*ls32l8"]] = linkonce_odr hidden unnamed_addr constant { i64, i64, ptr, i64 } { i64 0, i64 40, ptr @{{.*}}, i64 256 }, align 8
28 // CHECK-LABEL: define{{.*}} void @test0(
29 // CHECK: call void @noescapeFunc0({{.*}}, {{.*}} nocapture {{.*}})
30 // CHECK: declare void @noescapeFunc0(ptr noundef, {{.*}} nocapture noundef)
31 void test0(BlockTy b) {
35 // CHECK-LABEL: define{{.*}} void @test1(
36 // CHECK: call void @noescapeFunc1({{.*}} nocapture {{.*}})
37 // CHECK: declare void @noescapeFunc1({{.*}} nocapture noundef)
42 // CHECK-LABEL: define{{.*}} void @test2(
43 // CHECK: call void @noescapeFunc2({{.*}} nocapture {{.*}})
44 // CHECK: declare void @noescapeFunc2({{.*}} nocapture noundef)
49 // CHECK-LABEL: define{{.*}} void @test3(
50 // CHECK: call void @noescapeFunc3({{.*}} nocapture {{.*}})
51 // CHECK: declare void @noescapeFunc3({{.*}} nocapture)
52 void test3(union U u) {
56 // CHECK: define internal void @"\01-[C0 m0:]"({{.*}}, {{.*}}, {{.*}} nocapture {{.*}})
58 // CHECK-LABEL: define{{.*}} void @test4(
59 // CHECK: call void @objc_msgSend(ptr {{.*}}, ptr {{.*}}, ptr nocapture {{.*}})
62 -(void) m0:(int*)__attribute__((noescape)) p0;
66 -(void) m0:(int*)__attribute__((noescape)) p0 {
70 void test4(C0 *c0, int *p) {
74 // CHECK-LABEL: define{{.*}} void @test5(
75 // CHECK: call void {{.*}}(ptr noundef @{{.*}}, ptr nocapture {{.*}})
76 // CHECK: call void {{.*}}(ptr {{.*}}, ptr nocapture {{.*}})
77 // CHECK: define internal void @{{.*}}(ptr {{.*}}, ptr nocapture {{.*}})
79 typedef void (^BlockTy2)(__attribute__((noescape)) int *);
81 void test5(BlockTy2 b, int *p) {
82 ^(int *__attribute__((noescape)) p0){}(p);
86 // If the block is non-escaping, set the BLOCK_IS_NOESCAPE and BLOCK_IS_GLOBAL
87 // bits of field 'flags' and set the 'isa' field to 'NSConcreteGlobalBlock'.
89 // CHECK: define{{.*}} void @test6(ptr noundef %{{.*}}, ptr noundef %[[B:.*]])
90 // CHECK: %{{.*}} = alloca ptr, align 8
91 // CHECK: %[[B_ADDR:.*]] = alloca ptr, align 8
92 // CHECK: %[[BLOCK:.*]] = alloca <{ ptr, i32, i32, ptr, ptr, ptr }>, align 8
93 // CHECK-NOARC: store ptr %[[B]], ptr %[[B_ADDR]], align 8
94 // CHECK-ARC: store ptr null, ptr %[[B_ADDR]], align 8
95 // CHECK-ARC: call void @llvm.objc.storeStrong(ptr %[[B_ADDR]], ptr %[[B]])
96 // CHECK: %[[BLOCK_ISA:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[BLOCK]], i32 0, i32 0
97 // CHECK: store ptr @_NSConcreteGlobalBlock, ptr %[[BLOCK_ISA]], align 8
98 // CHECK: %[[BLOCK_FLAGS:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[BLOCK]], i32 0, i32 1
99 // CHECK: store i32 -796917760, ptr %[[BLOCK_FLAGS]], align 8
100 // CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[BLOCK]], i32 0, i32 4
101 // CHECK: store ptr @[[BLOCK_DESCIPTOR_TMP_2]], ptr %[[BLOCK_DESCRIPTOR]], align 8
102 // CHECK: %[[BLOCK_CAPTURED:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[BLOCK]], i32 0, i32 5
103 // CHECK-NOARC: %[[V1:.*]] = load ptr, ptr %[[B_ADDR]], align 8
104 // CHECK-NOARC: store ptr %[[V1]], ptr %[[BLOCK_CAPTURED]], align 8
105 // CHECK-ARC: %[[V2:.*]] = load ptr, ptr %[[B_ADDR]], align 8
106 // CHECK-ARC: %[[V3:.*]] = call ptr @llvm.objc.retain(ptr %[[V2]])
107 // CHECK-ARC: store ptr %[[V3]], ptr %[[BLOCK_CAPTURED]], align 8
108 // CHECK: call void @noescapeFunc0(
109 // CHECK-ARC: call void @llvm.objc.storeStrong(ptr %[[BLOCK_CAPTURED]], ptr null)
110 // CHECK-ARC: call void @llvm.objc.storeStrong(ptr %[[B_ADDR]], ptr null)
112 // Non-escaping blocks don't need copy/dispose helper functions.
114 // CHECK-NOT: define internal void @__copy_helper_block_
115 // CHECK-NOT: define internal void @__destroy_helper_block_
119 void test6(id a, id b) {
120 noescapeFunc0(a, ^{ func(b); });
123 // We don't need either the byref helper functions or the byref structs for
124 // __block variables that are not captured by escaping blocks.
126 // CHECK: define{{.*}} void @test7(
127 // CHECK: alloca ptr, align 8
128 // CHECK: %[[B0:.*]] = alloca ptr, align 8
129 // CHECK: %[[BLOCK:.*]] = alloca <{ ptr, i32, i32, ptr, ptr, ptr }>, align 8
130 // CHECK: %[[BLOCK_CAPTURED:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[BLOCK]], i32 0, i32 5
131 // CHECK: store ptr %[[B0]], ptr %[[BLOCK_CAPTURED]], align 8
133 // CHECK-ARC-NOT: define internal void @__Block_byref_object_copy_
134 // CHECK-ARC-NOT: define internal void @__Block_byref_object_dispose_
139 noescapeFunc0(a, ^{ (void)b0; });
142 // __block variables captured by escaping blocks need byref helper functions.
144 // CHECK: define{{.*}} void @test8(
145 // CHECK: %[[A:.*]] = alloca ptr, align 8
146 // CHECK: %[[B0:.*]] = alloca %[[STRUCT_BLOCK_BYREF_B0]], align 8
147 // CHECK: alloca <{ ptr, i32, i32, ptr, ptr, ptr }>, align 8
148 // CHECK: %[[BLOCK1:.*]] = alloca <{ ptr, i32, i32, ptr, ptr, ptr }>, align 8
149 // CHECK: %[[BLOCK_CAPTURED7:.*]] = getelementptr inbounds nuw <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[BLOCK1]], i32 0, i32 5
150 // CHECK: store ptr %[[B0]], ptr %[[BLOCK_CAPTURED7]], align 8
152 // CHECK-ARC: define internal void @__Block_byref_object_copy_
153 // CHECK-ARC: define internal void @__Block_byref_object_dispose_
154 // CHECK: define linkonce_odr hidden void @__copy_helper_block_
155 // CHECK: define linkonce_odr hidden void @__destroy_helper_block_
163 __block struct S0 b0;
164 noescapeFunc0(a, ^{ (void)b0; });
165 escapingFunc0(^{ (void)b0; });