1 // RUN: %clang_cc1 %s -emit-llvm -o - -fblocks -triple x86_64-apple-darwin10 | FileCheck %s
2
3 // CHECK: [[AGG:%.*]] = type { i32 }
4 typedef struct { int v; } Agg;
5 Agg makeAgg(void);
6
7 // When assigning into a __block variable, ensure that we compute that
8 // address *after* evaluating the RHS when the RHS has the capacity to
9 // cause a block copy. rdar://9309454
test0()10 void test0() {
11 __block Agg a = {100};
12 ^{ (void)a; };
13
14 a = makeAgg();
15 }
16 // CHECK-LABEL: define void @test0()
17 // CHECK: [[A:%.*]] = alloca [[BYREF:%.*]], align 8
18 // CHECK-NEXT: alloca <{ i8*, i32, i32, i8*, %{{.*}}*, i8* }>, align 8
19 // CHECK-NEXT: [[TEMP:%.*]] = alloca [[AGG]], align 4
20 // CHECK: [[RESULT:%.*]] = call i32 @makeAgg()
21 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[AGG]], [[AGG]]* [[TEMP]], i32 0, i32 0
22 // CHECK-NEXT: store i32 [[RESULT]], i32* [[T0]]
23 // Check that we properly assign into the forwarding pointer.
24 // CHECK-NEXT: [[A_FORWARDING:%.*]] = getelementptr inbounds [[BYREF]], [[BYREF]]* [[A]], i32 0, i32 1
25 // CHECK-NEXT: [[T0:%.*]] = load [[BYREF]]*, [[BYREF]]** [[A_FORWARDING]]
26 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[BYREF]], [[BYREF]]* [[T0]], i32 0, i32 4
27 // CHECK-NEXT: [[T2:%.*]] = bitcast [[AGG]]* [[T1]] to i8*
28 // CHECK-NEXT: [[T3:%.*]] = bitcast [[AGG]]* [[TEMP]] to i8*
29 // CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[T2]], i8* align 4 [[T3]], i64 4, i1 false)
30 // Verify that there's nothing else significant in the function.
31 // CHECK-NEXT: [[T0:%.*]] = bitcast [[BYREF]]* [[A]] to i8*
32 // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
33 // CHECK-NEXT: ret void
34
35 // When chaining assignments into __block variables, make sure we
36 // propagate the actual value into the outer variable.
37 // rdar://11757470
test1()38 void test1() {
39 __block Agg a, b;
40 ^{ (void)a; (void)b; };
41 a = b = makeAgg();
42 }
43 // CHECK-LABEL: define void @test1()
44 // CHECK: [[A:%.*]] = alloca [[A_BYREF:%.*]], align 8
45 // CHECK-NEXT: [[B:%.*]] = alloca [[B_BYREF:%.*]], align 8
46 // CHECK-NEXT: alloca <{ i8*, i32, i32, i8*, %{{.*}}*, i8*, i8* }>, align 8
47 // CHECK-NEXT: [[TEMP:%.*]] = alloca [[AGG]], align 4
48 // CHECK: [[RESULT:%.*]] = call i32 @makeAgg()
49 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [[AGG]], [[AGG]]* [[TEMP]], i32 0, i32 0
50 // CHECK-NEXT: store i32 [[RESULT]], i32* [[T0]]
51 // Check that we properly assign into the forwarding pointer, first for b:
52 // CHECK-NEXT: [[B_FORWARDING:%.*]] = getelementptr inbounds [[B_BYREF]], [[B_BYREF]]* [[B]], i32 0, i32 1
53 // CHECK-NEXT: [[T0:%.*]] = load [[B_BYREF]]*, [[B_BYREF]]** [[B_FORWARDING]]
54 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[B_BYREF]], [[B_BYREF]]* [[T0]], i32 0, i32 4
55 // CHECK-NEXT: [[T2:%.*]] = bitcast [[AGG]]* [[T1]] to i8*
56 // CHECK-NEXT: [[T3:%.*]] = bitcast [[AGG]]* [[TEMP]] to i8*
57 // CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[T2]], i8* align 4 [[T3]], i64 4, i1 false)
58 // Then for 'a':
59 // CHECK-NEXT: [[A_FORWARDING:%.*]] = getelementptr inbounds [[A_BYREF]], [[A_BYREF]]* [[A]], i32 0, i32 1
60 // CHECK-NEXT: [[T0:%.*]] = load [[A_BYREF]]*, [[A_BYREF]]** [[A_FORWARDING]]
61 // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[A_BYREF]], [[A_BYREF]]* [[T0]], i32 0, i32 4
62 // CHECK-NEXT: [[T2:%.*]] = bitcast [[AGG]]* [[T1]] to i8*
63 // CHECK-NEXT: [[T3:%.*]] = bitcast [[AGG]]* [[TEMP]] to i8*
64 // CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[T2]], i8* align 4 [[T3]], i64 4, i1 false)
65 // Verify that there's nothing else significant in the function.
66 // CHECK-NEXT: [[T0:%.*]] = bitcast [[B_BYREF]]* [[B]] to i8*
67 // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
68 // CHECK-NEXT: [[T0:%.*]] = bitcast [[A_BYREF]]* [[A]] to i8*
69 // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
70 // CHECK-NEXT: ret void
71