• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// RUN: %clang_cc1 -fblocks -fobjc-arc -fobjc-runtime-has-weak -triple x86_64-apple-darwin -emit-llvm %s -o %t-64.s
2// RUN: FileCheck -check-prefix CHECK-LP64 --input-file=%t-64.s %s
3// rdar://9503326
4// rdar://9606600
5
6extern void use(id);
7extern void use_block(void (^)(void));
8
9struct NSFastEnumerationState;
10@interface NSArray
11- (unsigned long) countByEnumeratingWithState: (struct NSFastEnumerationState*) state
12                  objects: (id*) buffer
13                  count: (unsigned long) bufferSize;
14@end;
15
16void test0(NSArray *array) {
17  // 'x' should be initialized without a retain.
18  // We should actually do a non-constant capture, and that
19  // capture should require a retain.
20  for (id x in array) {
21    use_block(^{ use(x); });
22  }
23}
24
25// CHECK-LP64-LABEL:    define void @test0(
26// CHECK-LP64:      [[ARRAY:%.*]] = alloca [[ARRAY_T:%.*]]*,
27// CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*,
28// CHECK-LP64-NEXT: [[STATE:%.*]] = alloca [[STATE_T:%.*]],
29// CHECK-LP64-NEXT: [[BUFFER:%.*]] = alloca [16 x i8*], align 8
30// CHECK-LP64-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
31
32// Initialize 'array'.
33// CHECK-LP64-NEXT: store [[ARRAY_T]]* null, [[ARRAY_T]]** [[ARRAY]]
34// CHECK-LP64-NEXT: [[ZERO:%.*]] = bitcast [[ARRAY_T]]** [[ARRAY]] to i8**
35// CHECK-LP64-NEXT: [[ONE:%.*]] = bitcast [[ARRAY_T]]* {{%.*}} to i8*
36// CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[ZERO]], i8* [[ONE]]) [[NUW:#[0-9]+]]
37
38// Initialize the fast enumaration state.
39// CHECK-LP64-NEXT: [[T0:%.*]] = bitcast [[STATE_T]]* [[STATE]] to i8*
40// CHECK-LP64-NEXT: call void @llvm.memset.p0i8.i64(i8* [[T0]], i8 0, i64 64, i32 8, i1 false)
41
42// Evaluate the collection expression and retain.
43// CHECK-LP64-NEXT: [[T0:%.*]] = load [[ARRAY_T]]*, [[ARRAY_T]]** [[ARRAY]], align 8
44// CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[T0]] to i8*
45// CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]])
46// CHECK-LP64-NEXT: [[SAVED_ARRAY:%.*]] = bitcast i8* [[T2]] to [[ARRAY_T]]*
47
48// Call the enumeration method.
49// CHECK-LP64-NEXT: [[T0:%.*]] = load i8*, i8** @OBJC_SELECTOR_REFERENCES_
50// CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8*
51// CHECK-LP64-NEXT: [[SIZE:%.*]] = call i64 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i64 (i8*, i8*, [[STATE_T]]*, [16 x i8*]*, i64)*)(i8* [[T1]], i8* [[T0]], [[STATE_T]]* [[STATE]], [16 x i8*]* [[BUFFER]], i64 16)
52
53// Check for a nonzero result.
54// CHECK-LP64-NEXT: [[T0:%.*]] = icmp eq i64 [[SIZE]], 0
55// CHECK-LP64-NEXT: br i1 [[T0]]
56
57// CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[STATE_T]], [[STATE_T]]* [[STATE]], i32 0, i32 1
58// CHECK-LP64-NEXT: [[T1:%.*]] = load i8**, i8*** [[T0]]
59// CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr i8*, i8** [[T1]], i64
60// CHECK-LP64-NEXT: [[T3:%.*]] = load i8*, i8** [[T2]]
61// CHECK-LP64-NEXT: store i8* [[T3]], i8** [[X]]
62
63// CHECK-LP64:      [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
64// CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
65// CHECK-LP64-NEXT: [[T1:%.*]] = load i8*, i8** [[X]]
66// CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retain(i8* [[T1]])
67// CHECK-LP64-NEXT: store i8* [[T2]], i8** [[T0]]
68// CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]]
69// CHECK-LP64: call void @use_block(
70// CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[D0]], i8* null)
71
72// CHECK-LP64:      [[T0:%.*]] = load i8*, i8** @OBJC_SELECTOR_REFERENCES_
73// CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8*
74// CHECK-LP64-NEXT: [[SIZE:%.*]] = call i64 bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i64 (i8*, i8*, [[STATE_T]]*, [16 x i8*]*, i64)*)(i8* [[T1]], i8* [[T0]], [[STATE_T]]* [[STATE]], [16 x i8*]* [[BUFFER]], i64 16)
75
76// Release the array.
77// CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[SAVED_ARRAY]] to i8*
78// CHECK-LP64-NEXT: call void @objc_release(i8* [[T0]])
79
80// Destroy 'array'.
81// CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]** [[ARRAY]] to i8**
82// CHECK-LP64-NEXT: call void @objc_storeStrong(i8** [[T0]], i8* null)
83// CHECK-LP64-NEXT: ret void
84
85// CHECK-LP64-LABEL:    define internal void @__test0_block_invoke
86// CHECK-LP64:      [[BLOCK:%.*]] = bitcast i8* {{%.*}} to [[BLOCK_T]]*
87// CHECK-LP64-NOT:  ret
88// CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
89// CHECK-LP64-NEXT: [[T2:%.*]] = load i8*, i8** [[T0]], align 8
90// CHECK-LP64-NEXT: call void @use(i8* [[T2]])
91
92void test1(NSArray *array) {
93  for (__weak id x in array) {
94    use_block(^{ use(x); });
95  }
96}
97
98// CHECK-LP64-LABEL:    define void @test1(
99// CHECK-LP64:      alloca [[ARRAY_T:%.*]]*,
100// CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*,
101// CHECK-LP64-NEXT: [[STATE:%.*]] = alloca [[STATE_T:%.*]],
102// CHECK-LP64-NEXT: alloca [16 x i8*], align 8
103// CHECK-LP64-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
104
105// CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[STATE_T]], [[STATE_T]]* [[STATE]], i32 0, i32 1
106// CHECK-LP64-NEXT: [[T1:%.*]] = load i8**, i8*** [[T0]]
107// CHECK-LP64-NEXT: [[T2:%.*]] = getelementptr i8*, i8** [[T1]], i64
108// CHECK-LP64-NEXT: [[T3:%.*]] = load i8*, i8** [[T2]]
109// CHECK-LP64-NEXT: call i8* @objc_initWeak(i8** [[X]], i8* [[T3]])
110
111// CHECK-LP64:      [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
112// CHECK-LP64:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]], [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
113// CHECK-LP64-NEXT: call void @objc_copyWeak(i8** [[T0]], i8** [[X]])
114// CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to
115// CHECK-LP64: call void @use_block
116// CHECK-LP64-NEXT: call void @objc_destroyWeak(i8** [[D0]])
117// CHECK-LP64-NEXT: call void @objc_destroyWeak(i8** [[X]])
118
119// rdar://problem/9817306
120@interface Test2
121- (NSArray *) array;
122@end
123void test2(Test2 *a) {
124  for (id x in a.array) {
125    use(x);
126  }
127}
128
129// CHECK-LP64-LABEL:    define void @test2(
130// CHECK-LP64:      [[T0:%.*]] = call [[ARRAY_T]]* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to [[ARRAY_T]]* (i8*, i8*)*)(
131// CHECK-LP64-NEXT: [[T1:%.*]] = bitcast [[ARRAY_T]]* [[T0]] to i8*
132// CHECK-LP64-NEXT: [[T2:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T1]])
133// CHECK-LP64-NEXT: [[COLL:%.*]] = bitcast i8* [[T2]] to [[ARRAY_T]]*
134
135// Make sure it's not immediately released before starting the iteration.
136// CHECK-LP64-NEXT: load i8*, i8** @OBJC_SELECTOR_REFERENCES_
137// CHECK-LP64-NEXT: [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
138// CHECK-LP64-NEXT: @objc_msgSend
139
140// This bitcast is for the mutation check.
141// CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
142// CHECK-LP64-NEXT: @objc_enumerationMutation
143
144// This bitcast is for the 'next' message send.
145// CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
146// CHECK-LP64-NEXT: @objc_msgSend
147
148// This bitcast is for the final release.
149// CHECK-LP64:      [[T0:%.*]] = bitcast [[ARRAY_T]]* [[COLL]] to i8*
150// CHECK-LP64-NEXT: call void @objc_release(i8* [[T0]])
151
152
153// Check that the 'continue' label is positioned appropriately
154// relative to the collection clenaup.
155void test3(NSArray *array) {
156  for (id x in array) {
157    if (!x) continue;
158    use(x);
159  }
160
161  // CHECK-LP64-LABEL:    define void @test3(
162  // CHECK-LP64:      [[ARRAY:%.*]] = alloca [[ARRAY_T]]*, align 8
163  // CHECK-LP64-NEXT: [[X:%.*]] = alloca i8*, align 8
164  // CHECK-LP64:      [[T0:%.*]] = load i8*, i8** [[X]], align 8
165  // CHECK-LP64-NEXT: [[T1:%.*]] = icmp ne i8* [[T0]], null
166  // CHECK-LP64-NEXT: br i1 [[T1]],
167  // CHECK-LP64:      br label [[L:%[^ ]+]]
168  // CHECK-LP64:      [[T0:%.*]] = load i8*, i8** [[X]], align 8
169  // CHECK-LP64-NEXT: call void @use(i8* [[T0]])
170  // CHECK-LP64-NEXT: br label [[L]]
171}
172
173// CHECK-LP64: attributes [[NUW]] = { nounwind }
174