• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// RUN: %clang_cc1 -triple=x86_64-apple-darwin10 -emit-llvm -std=c++14 -fblocks -fobjc-arc -o - %s | FileCheck %s
2
3// CHECK: %[[STRUCT_BLOCK_DESCRIPTOR:.*]] = type { i64, i64 }
4// CHECK: %[[S:.*]] = type { i32 }
5// CHECK: %[[CLASS_ANON_2:.*]] = type { %[[S]]* }
6// CHECK: %[[CLASS_ANON_3:.*]] = type { %[[S]] }
7
8// CHECK: %[[BLOCK_CAPTURED0:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32*, i32* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32*, i32* }>* %[[BLOCK:.*]], i32 0, i32 5
9// CHECK: %[[V0:.*]] = getelementptr inbounds %[[LAMBDA_CLASS:.*]], %[[LAMBDA_CLASS]]* %[[THIS:.*]], i32 0, i32 0
10// CHECK: %[[V1:.*]] = load i32*, i32** %[[V0]], align 8
11// CHECK: store i32* %[[V1]], i32** %[[BLOCK_CAPTURED0]], align 8
12// CHECK: %[[BLOCK_CAPTURED1:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32*, i32* }>, <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, i32*, i32* }>* %[[BLOCK]], i32 0, i32 6
13// CHECK: %[[V2:.*]] = getelementptr inbounds %[[LAMBDA_CLASS]], %[[LAMBDA_CLASS]]* %[[THIS]], i32 0, i32 1
14// CHECK: %[[V3:.*]] = load i32*, i32** %[[V2]], align 8
15// CHECK: store i32* %[[V3]], i32** %[[BLOCK_CAPTURED1]], align 8
16
17void foo1(int &, int &);
18
19void block_in_lambda(int &s1, int &s2) {
20  auto lambda = [&s1, &s2]() {
21    auto block = ^{
22      foo1(s1, s2);
23    };
24    block();
25  };
26
27  lambda();
28}
29
30namespace CaptureByReference {
31
32id getObj();
33void use(id);
34
35// Block copy/dispose helpers aren't needed because 'a' is captured by
36// reference.
37
38// CHECK-LABEL: define void @_ZN18CaptureByReference5test0Ev(
39// CHECK-LABEL: define internal void @"_ZZN18CaptureByReference5test0EvENK3$_3clEv"(
40// CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8** }>* %{{.*}}, i32 0, i32 4
41// CHECK: store %[[STRUCT_BLOCK_DESCRIPTOR]]* bitcast ({ i64, i64, i8*, i64 }* @"__block_descriptor_40_e5_v8\01?0ls32l8" to %[[STRUCT_BLOCK_DESCRIPTOR]]*), %[[STRUCT_BLOCK_DESCRIPTOR]]** %[[BLOCK_DESCRIPTOR]], align 8
42
43void test0() {
44  id a = getObj();
45  [&]{ ^{ a = 0; }(); }();
46}
47
48// Block copy/dispose helpers shouldn't have to retain/release 'a' because it
49// is captured by reference.
50
51// CHECK-LABEL: define void @_ZN18CaptureByReference5test1Ev(
52// CHECK-LABEL: define internal void @"_ZZN18CaptureByReference5test1EvENK3$_4clEv"(
53// CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 4
54// CHECK: store %[[STRUCT_BLOCK_DESCRIPTOR]]* bitcast ({ i64, i64, i8*, i8*, i8*, i64 }* @"__block_descriptor_56_8_32s40s_e5_v8\01?0l" to %[[STRUCT_BLOCK_DESCRIPTOR]]*), %[[STRUCT_BLOCK_DESCRIPTOR]]** %[[BLOCK_DESCRIPTOR]], align 8
55
56void test1() {
57  id a = getObj(), b = getObj(), c = getObj();
58  [&a, b, c]{ ^{ a = 0; use(b); use(c); }(); }();
59}
60
61struct S {
62  int val() const;
63  int a;
64  S();
65  S(const S&);
66  S &operator=(const S&);
67  S(S&&);
68  S &operator=(S&&);
69};
70
71S getS();
72
73// CHECK: define internal i32 @"_ZZN18CaptureByReference5test2EvENK3$_1clIiEEDaT_"(%[[CLASS_ANON_2]]* {{[^,]*}} %{{.*}}, i32 %{{.*}})
74// CHECK: %[[BLOCK:.*]] = alloca <{ i8*, i32, i32, i8*, %{{.*}}, %[[S]]* }>, align 8
75// CHECK: %[[BLOCK_CAPTURED:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %{{.*}}, %[[S]]* }>, <{ i8*, i32, i32, i8*, %{{.*}}, %[[S]]* }>* %[[BLOCK]], i32 0, i32 5
76// CHECK: %[[V0:.*]] = getelementptr inbounds %[[CLASS_ANON_2]], %[[CLASS_ANON_2]]* %{{.*}}, i32 0, i32 0
77// CHECK: %[[V1:.*]] = load %[[S]]*, %[[S]]** %[[V0]], align 8
78// CHECK: store %[[S]]* %[[V1]], %[[S]]** %[[BLOCK_CAPTURED]], align 8
79
80int test2() {
81  S s;
82  auto fn = [&](const auto a){
83    return ^{
84      return s.val();
85    }();
86  };
87  return fn(123);
88}
89
90// CHECK: define internal i32 @"_ZZN18CaptureByReference5test3EvENK3$_2clIiEEDaT_"(%[[CLASS_ANON_3]]* {{[^,]*}} %{{.*}}, i32 %{{.*}})
91// CHECK: %[[BLOCK:.*]] = alloca <{ i8*, i32, i32, i8*, %{{.*}}*, %[[S]] }>, align 8
92// CHECK: %[[BLOCK_CAPTURED:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %{{.*}}*, %[[S]] }>, <{ i8*, i32, i32, i8*, %{{.*}}*, %[[S]] }>* %[[BLOCK]], i32 0, i32 5
93// CHECK: %[[V0:.*]] = getelementptr inbounds %[[CLASS_ANON_3]], %[[CLASS_ANON_3]]* %{{.*}}, i32 0, i32 0
94// CHECK: call void @_ZN18CaptureByReference1SC1ERKS0_(%[[S]]* {{[^,]*}} %[[BLOCK_CAPTURED]], %[[S]]* {{.*}} %[[V0]])
95
96int test3() {
97  const S &s = getS();
98  auto fn = [=](const auto a){
99    return ^{
100      return s.val();
101    }();
102  };
103  return fn(123);
104}
105
106// CHECK-LABEL: define linkonce_odr hidden void @__copy_helper_block_8_32s40s(
107// CHECK-NOT: call void @llvm.objc.storeStrong(
108// CHECK: %[[V4:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 5
109// CHECK: %[[V5:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 5
110// CHECK: %[[BLOCKCOPY_SRC:.*]] = load i8*, i8** %[[V4]], align 8
111// CHECK: store i8* null, i8** %[[V5]], align 8
112// CHECK: call void @llvm.objc.storeStrong(i8** %[[V5]], i8* %[[BLOCKCOPY_SRC]])
113// CHECK: %[[V6:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 6
114// CHECK: %[[V7:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 6
115// CHECK: %[[BLOCKCOPY_SRC2:.*]] = load i8*, i8** %[[V6]], align 8
116// CHECK: store i8* null, i8** %[[V7]], align 8
117// CHECK: call void @llvm.objc.storeStrong(i8** %[[V7]], i8* %[[BLOCKCOPY_SRC2]])
118// CHECK-NOT: call void @llvm.objc.storeStrong(
119// CHECK: ret void
120
121// CHECK-LABEL: define linkonce_odr hidden void @__destroy_helper_block_8_32s40s(
122// CHECK: %[[V2:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 5
123// CHECK: %[[V3:.*]] = getelementptr inbounds <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>, <{ i8*, i32, i32, i8*, %[[STRUCT_BLOCK_DESCRIPTOR]]*, i8*, i8*, i8** }>* %{{.*}}, i32 0, i32 6
124// CHECK-NOT: call void @llvm.objc.storeStrong(
125// CHECK: call void @llvm.objc.storeStrong(i8** %[[V3]], i8* null)
126// CHECK: call void @llvm.objc.storeStrong(i8** %[[V2]], i8* null)
127// CHECK-NOT: call void @llvm.objc.storeStrong(
128// CHECK: ret void
129
130}
131