• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fblocks -fobjc-arc -fobjc-runtime-has-weak -O2 -disable-llvm-optzns -o - %s | FileCheck %s
2
3// This shouldn't crash.
4void test0(id (^maker)(void)) {
5  maker();
6}
7
8int (^test1(int x))(void) {
9  // CHECK:    define i32 ()* @test1(
10  // CHECK:      [[X:%.*]] = alloca i32,
11  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
12  // CHECK-NEXT: store i32 {{%.*}}, i32* [[X]]
13  // CHECK:      [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to i32 ()*
14  // CHECK-NEXT: [[T1:%.*]] = bitcast i32 ()* [[T0]] to i8*
15  // CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainBlock(i8* [[T1]]) nounwind
16  // CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to i32 ()*
17  // CHECK-NEXT: [[T4:%.*]] = bitcast i32 ()* [[T3]] to i8*
18  // CHECK-NEXT: [[T5:%.*]] = call i8* @objc_autoreleaseReturnValue(i8* [[T4]]) nounwind
19  // CHECK-NEXT: [[T6:%.*]] = bitcast i8* [[T5]] to i32 ()*
20  // CHECK-NEXT: ret i32 ()* [[T6]]
21  return ^{ return x; };
22}
23
24void test2(id x) {
25// CHECK:    define void @test2(
26// CHECK:      [[X:%.*]] = alloca i8*,
27// CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
28// CHECK-NEXT: [[PARM:%.*]] = call i8* @objc_retain(i8* {{%.*}})
29// CHECK-NEXT: store i8* [[PARM]], i8** [[X]]
30// CHECK-NEXT: [[SLOTREL:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
31// CHECK:      [[SLOT:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
32// CHECK-NEXT: [[T0:%.*]] = load i8** [[X]],
33// CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]])
34// CHECK-NEXT: store i8* [[T1]], i8** [[SLOT]],
35// CHECK-NEXT: bitcast
36// CHECK-NEXT: call void @test2_helper(
37// CHECK-NEXT: [[T0:%.*]] = load i8** [[SLOTREL]]
38// CHECK-NEXT: call void @objc_release(i8* [[T0]]) nounwind, !clang.imprecise_release
39// CHECK-NEXT: [[T0:%.*]] = load i8** [[X]]
40// CHECK-NEXT: call void @objc_release(i8* [[T0]]) nounwind, !clang.imprecise_release
41// CHECK-NEXT: ret void
42  extern void test2_helper(id (^)(void));
43  test2_helper(^{ return x; });
44}
45
46void test3(void (^sink)(id*)) {
47  __strong id strong;
48  sink(&strong);
49
50  // CHECK:    define void @test3(
51  // CHECK:      [[SINK:%.*]] = alloca void (i8**)*
52  // CHECK-NEXT: [[STRONG:%.*]] = alloca i8*
53  // CHECK-NEXT: [[TEMP:%.*]] = alloca i8*
54  // CHECK-NEXT: bitcast void (i8**)* {{%.*}} to i8*
55  // CHECK-NEXT: call i8* @objc_retain(
56  // CHECK-NEXT: bitcast i8*
57  // CHECK-NEXT: store void (i8**)* {{%.*}}, void (i8**)** [[SINK]]
58  // CHECK-NEXT: store i8* null, i8** [[STRONG]]
59
60  // CHECK-NEXT: load void (i8**)** [[SINK]]
61  // CHECK-NEXT: bitcast
62  // CHECK-NEXT: getelementptr
63  // CHECK-NEXT: [[BLOCK:%.*]] = bitcast
64  // CHECK-NEXT: [[T0:%.*]] = load i8** [[STRONG]]
65  // CHECK-NEXT: store i8* [[T0]], i8** [[TEMP]]
66  // CHECK-NEXT: [[F0:%.*]] = load i8**
67  // CHECK-NEXT: [[F1:%.*]] = bitcast i8* [[F0]] to void (i8*, i8**)*
68  // CHECK-NEXT: call void [[F1]](i8* [[BLOCK]], i8** [[TEMP]])
69  // CHECK-NEXT: [[T0:%.*]] = load i8** [[TEMP]]
70  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]])
71  // CHECK-NEXT: [[T2:%.*]] = load i8** [[STRONG]]
72  // CHECK-NEXT: store i8* [[T1]], i8** [[STRONG]]
73  // CHECK-NEXT: call void @objc_release(i8* [[T2]])
74
75  // CHECK-NEXT: [[T0:%.*]] = load i8** [[STRONG]]
76  // CHECK-NEXT: call void @objc_release(i8* [[T0]])
77
78  // CHECK-NEXT: load void (i8**)** [[SINK]]
79  // CHECK-NEXT: bitcast
80  // CHECK-NEXT: call void @objc_release
81  // CHECK-NEXT: ret void
82
83}
84
85void test4(void) {
86  id test4_source(void);
87  void test4_helper(void (^)(void));
88  __block id var = test4_source();
89  test4_helper(^{ var = 0; });
90
91  // CHECK:    define void @test4()
92  // CHECK:      [[VAR:%.*]] = alloca [[BYREF_T:%.*]],
93  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
94  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* [[VAR]], i32 0, i32 2
95  // 0x02000000 - has copy/dispose helpers
96  // CHECK-NEXT: store i32 33554432, i32* [[T0]]
97  // CHECK:      [[SLOT:%.*]] = getelementptr inbounds [[BYREF_T]]* [[VAR]], i32 0, i32 6
98  // CHECK-NEXT: [[T0:%.*]] = call i8* @test4_source()
99  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
100  // CHECK-NEXT: store i8* [[T1]], i8** [[SLOT]]
101  // CHECK-NEXT: [[SLOT:%.*]] = getelementptr inbounds [[BYREF_T]]* [[VAR]], i32 0, i32 6
102  // 0x42000000 - has signature, copy/dispose helpers
103  // CHECK:      store i32 1107296256,
104  // CHECK:      [[T0:%.*]] = bitcast [[BYREF_T]]* [[VAR]] to i8*
105  // CHECK-NEXT: store i8* [[T0]], i8**
106  // CHECK:      call void @test4_helper(
107  // CHECK:      [[T0:%.*]] = bitcast [[BYREF_T]]* [[VAR]] to i8*
108  // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
109  // CHECK-NEXT: [[T0:%.*]] = load i8** [[SLOT]]
110  // CHECK-NEXT: call void @objc_release(i8* [[T0]])
111  // CHECK-NEXT: ret void
112
113  // CHECK:    define internal void @__Block_byref_object_copy_
114  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* {{%.*}}, i32 0, i32 6
115  // CHECK-NEXT: load i8**
116  // CHECK-NEXT: bitcast i8* {{%.*}} to [[BYREF_T]]*
117  // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[BYREF_T]]* {{%.*}}, i32 0, i32 6
118  // CHECK-NEXT: [[T2:%.*]] = load i8** [[T1]]
119  // CHECK-NEXT: store i8* [[T2]], i8** [[T0]]
120  // CHECK-NEXT: store i8* null, i8** [[T1]]
121
122  // CHECK:    define internal void @__Block_byref_object_dispose_
123  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* {{%.*}}, i32 0, i32 6
124  // CHECK-NEXT: [[T1:%.*]] = load i8** [[T0]]
125  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
126
127  // CHECK:    define internal void @__test4_block_invoke_
128  // CHECK:      [[SLOT:%.*]] = getelementptr inbounds {{.*}}, i32 0, i32 6
129  // CHECK-NEXT: [[T0:%.*]] = load i8** [[SLOT]], align 8
130  // CHECK-NEXT: store i8* null, i8** [[SLOT]],
131  // CHECK-NEXT: call void @objc_release(i8* [[T0]])
132  // CHECK-NEXT: ret void
133
134  // CHECK:    define internal void @__copy_helper_block_
135  // CHECK:      call void @_Block_object_assign(i8* {{%.*}}, i8* {{%.*}}, i32 8)
136
137  // CHECK:    define internal void @__destroy_helper_block_
138  // CHECK:      call void @_Block_object_dispose(i8* {{%.*}}, i32 8)
139}
140
141void test5(void) {
142  extern id test5_source(void);
143  void test5_helper(void (^)(void));
144  __unsafe_unretained id var = test5_source();
145  test5_helper(^{ (void) var; });
146
147  // CHECK:    define void @test5()
148  // CHECK:      [[VAR:%.*]] = alloca i8*
149  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
150  // CHECK: [[T0:%.*]] = call i8* @test5_source()
151  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
152  // CHECK-NEXT: store i8* [[T1]], i8** [[VAR]],
153  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
154  // 0x40000000 - has signature but no copy/dispose
155  // CHECK:      store i32 1073741824, i32*
156  // CHECK:      [[CAPTURE:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
157  // CHECK-NEXT: [[T0:%.*]] = load i8** [[VAR]]
158  // CHECK-NEXT: store i8* [[T0]], i8** [[CAPTURE]]
159  // CHECK-NEXT: [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to
160  // CHECK: call void @test5_helper
161  // CHECK-NEXT: ret void
162}
163
164void test6(void) {
165  id test6_source(void);
166  void test6_helper(void (^)(void));
167  __block __weak id var = test6_source();
168  test6_helper(^{ var = 0; });
169
170  // CHECK:    define void @test6()
171  // CHECK:      [[VAR:%.*]] = alloca [[BYREF_T:%.*]],
172  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
173  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* [[VAR]], i32 0, i32 2
174  // 0x02000000 - has copy/dispose helpers
175  // CHECK-NEXT: store i32 33554432, i32* [[T0]]
176  // CHECK:      [[SLOT:%.*]] = getelementptr inbounds [[BYREF_T]]* [[VAR]], i32 0, i32 6
177  // CHECK-NEXT: [[T0:%.*]] = call i8* @test6_source()
178  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
179  // CHECK-NEXT: call i8* @objc_initWeak(i8** [[SLOT]], i8* [[T1]])
180  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
181  // CHECK-NEXT: [[SLOT:%.*]] = getelementptr inbounds [[BYREF_T]]* [[VAR]], i32 0, i32 6
182  // 0x42000000 - has signature, copy/dispose helpers
183  // CHECK:      store i32 1107296256,
184  // CHECK:      [[T0:%.*]] = bitcast [[BYREF_T]]* [[VAR]] to i8*
185  // CHECK-NEXT: store i8* [[T0]], i8**
186  // CHECK:      call void @test6_helper(
187  // CHECK:      [[T0:%.*]] = bitcast [[BYREF_T]]* [[VAR]] to i8*
188  // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
189  // CHECK-NEXT: call void @objc_destroyWeak(i8** [[SLOT]])
190  // CHECK-NEXT: ret void
191
192  // CHECK:    define internal void @__Block_byref_object_copy_
193  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* {{%.*}}, i32 0, i32 6
194  // CHECK-NEXT: load i8**
195  // CHECK-NEXT: bitcast i8* {{%.*}} to [[BYREF_T]]*
196  // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds [[BYREF_T]]* {{%.*}}, i32 0, i32 6
197  // CHECK-NEXT: call void @objc_moveWeak(i8** [[T0]], i8** [[T1]])
198
199  // CHECK:    define internal void @__Block_byref_object_dispose_
200  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* {{%.*}}, i32 0, i32 6
201  // CHECK-NEXT: call void @objc_destroyWeak(i8** [[T0]])
202
203  // CHECK:    define internal void @__test6_block_invoke_
204  // CHECK:      [[SLOT:%.*]] = getelementptr inbounds {{.*}}, i32 0, i32 6
205  // CHECK-NEXT: call i8* @objc_storeWeak(i8** [[SLOT]], i8* null)
206  // CHECK-NEXT: ret void
207
208  // CHECK:    define internal void @__copy_helper_block_
209  // 0x8 - FIELD_IS_BYREF (no FIELD_IS_WEAK because clang in control)
210  // CHECK:      call void @_Block_object_assign(i8* {{%.*}}, i8* {{%.*}}, i32 8)
211
212  // CHECK:    define internal void @__destroy_helper_block_
213  // 0x8 - FIELD_IS_BYREF (no FIELD_IS_WEAK because clang in control)
214  // CHECK:      call void @_Block_object_dispose(i8* {{%.*}}, i32 8)
215}
216
217void test7(void) {
218  id test7_source(void);
219  void test7_helper(void (^)(void));
220  void test7_consume(id);
221  __weak id var = test7_source();
222  test7_helper(^{ test7_consume(var); });
223
224  // CHECK:    define void @test7()
225  // CHECK:      [[VAR:%.*]] = alloca i8*,
226  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
227  // CHECK:      [[T0:%.*]] = call i8* @test7_source()
228  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainAutoreleasedReturnValue(i8* [[T0]])
229  // CHECK-NEXT: call i8* @objc_initWeak(i8** [[VAR]], i8* [[T1]])
230  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
231  // 0x42000000 - has signature, copy/dispose helpers
232  // CHECK:      store i32 1107296256,
233  // CHECK:      [[SLOT:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
234  // CHECK-NEXT: [[T0:%.*]] = call i8* @objc_loadWeak(i8** [[VAR]])
235  // CHECK-NEXT: call i8* @objc_initWeak(i8** [[SLOT]], i8* [[T0]])
236  // CHECK:      call void @test7_helper(
237  // CHECK-NEXT: call void @objc_destroyWeak(i8** {{%.*}})
238  // CHECK-NEXT: call void @objc_destroyWeak(i8** [[VAR]])
239  // CHECK-NEXT: ret void
240
241  // CHECK:    define internal void @__test7_block_invoke_
242  // CHECK:      [[SLOT:%.*]] = getelementptr inbounds [[BLOCK_T]]* {{%.*}}, i32 0, i32 5
243  // CHECK-NEXT: [[T0:%.*]] = call i8* @objc_loadWeak(i8** [[SLOT]])
244  // CHECK-NEXT: call void @test7_consume(i8* [[T0]])
245  // CHECK-NEXT: ret void
246
247  // CHECK:    define internal void @__copy_helper_block_
248  // CHECK:      getelementptr
249  // CHECK-NEXT: getelementptr
250  // CHECK-NEXT: call void @objc_copyWeak(
251
252  // CHECK:    define internal void @__destroy_helper_block_
253  // CHECK:      getelementptr
254  // CHECK-NEXT: call void @objc_destroyWeak(
255}
256
257@interface Test8 @end
258@implementation Test8
259- (void) test {
260// CHECK:    define internal void @"\01-[Test8 test]"
261// CHECK:      [[SELF:%.*]] = alloca [[TEST8:%.*]]*,
262// CHECK-NEXT: alloca i8*
263// CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]],
264// CHECK: store
265// CHECK-NEXT: store
266// CHECK:      [[D0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
267// CHECK:      [[T0:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
268// CHECK-NEXT: [[T1:%.*]] = load [[TEST8]]** [[SELF]],
269// CHECK-NEXT: [[T2:%.*]] = bitcast [[TEST8]]* [[T1]] to i8*
270// CHECK-NEXT: [[T3:%.*]] = call i8* @objc_retain(i8* [[T2]])
271// CHECK-NEXT: [[T4:%.*]] = bitcast i8* [[T3]] to [[TEST8]]*
272// CHECK-NEXT: store [[TEST8]]* [[T4]], [[TEST8]]** [[T0]]
273// CHECK-NEXT: bitcast [[BLOCK_T]]* [[BLOCK]] to
274// CHECK: call void @test8_helper(
275// CHECK-NEXT: [[T1:%.*]] = load [[TEST8]]** [[D0]]
276// CHECK-NEXT: [[T2:%.*]] = bitcast [[TEST8]]* [[T1]] to i8*
277// CHECK-NEXT: call void @objc_release(i8* [[T2]])
278// CHECK-NEXT: ret void
279
280  extern void test8_helper(void (^)(void));
281  test8_helper(^{ (void) self; });
282}
283@end
284
285id test9(void) {
286  typedef id __attribute__((ns_returns_retained)) blocktype(void);
287  extern void test9_consume_block(blocktype^);
288  return ^blocktype {
289      extern id test9_produce(void);
290      return test9_produce();
291  }();
292
293// CHECK:    define i8* @test9(
294// CHECK:      load i8** getelementptr
295// CHECK-NEXT: bitcast i8*
296// CHECK-NEXT: call i8*
297// CHECK-NEXT: call i8* @objc_autoreleaseReturnValue
298// CHECK-NEXT: ret i8*
299
300// CHECK:      call i8* @test9_produce()
301// CHECK-NEXT: call i8* @objc_retain
302// CHECK-NEXT: ret i8*
303}
304
305// rdar://problem/9814099
306// Test that we correctly initialize __block variables
307// when the initialization captures the variable.
308void test10a(void) {
309  __block void (^block)(void) = ^{ block(); };
310  // CHECK:    define void @test10a()
311  // CHECK:      [[BYREF:%.*]] = alloca [[BYREF_T:%.*]],
312
313  // Zero-initialization before running the initializer.
314  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* [[BYREF]], i32 0, i32 6
315  // CHECK-NEXT: store void ()* null, void ()** [[T0]], align 8
316
317  // Run the initializer as an assignment.
318  // CHECK:      [[T0:%.*]] = bitcast void ()* {{%.*}} to i8*
319  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainBlock(i8* [[T0]])
320  // CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to void ()*
321  // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[BYREF_T]]* [[BYREF]], i32 0, i32 1
322  // CHECK-NEXT: [[T4:%.*]] = load [[BYREF_T]]** [[T3]]
323  // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [[BYREF_T]]* [[T4]], i32 0, i32 6
324  // CHECK-NEXT: [[T6:%.*]] = load void ()** [[T5]], align 8
325  // CHECK-NEXT: store void ()* {{%.*}}, void ()** [[T5]], align 8
326  // CHECK-NEXT: [[T7:%.*]] = bitcast void ()* [[T6]] to i8*
327  // CHECK-NEXT: call void @objc_release(i8* [[T7]])
328
329  // Destroy at end of function.
330  // CHECK-NEXT: [[SLOT:%.*]] = getelementptr inbounds [[BYREF_T]]* [[BYREF]], i32 0, i32 6
331  // CHECK-NEXT: [[T0:%.*]] = bitcast [[BYREF_T]]* [[BYREF]] to i8*
332  // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
333  // CHECK-NEXT: [[T1:%.*]] = load void ()** [[SLOT]]
334  // CHECK-NEXT: [[T2:%.*]] = bitcast void ()* [[T1]] to i8*
335  // CHECK-NEXT: call void @objc_release(i8* [[T2]])
336  // CHECK-NEXT: ret void
337}
338
339// <rdar://problem/10402698>: do this copy and dispose with
340// objc_retainBlock/release instead of _Block_object_assign/destroy.
341// We can also use _Block_object_assign/destroy with
342// BLOCK_FIELD_IS_BLOCK as long as we don't pass BLOCK_BYREF_CALLER.
343
344// CHECK: define internal void @__Block_byref_object_copy
345// CHECK:      [[D0:%.*]] = load i8** {{%.*}}
346// CHECK-NEXT: [[D1:%.*]] = bitcast i8* [[D0]] to [[BYREF_T]]*
347// CHECK-NEXT: [[D2:%.*]] = getelementptr inbounds [[BYREF_T]]* [[D1]], i32 0, i32 6
348// CHECK-NEXT: [[S0:%.*]] = load i8** {{%.*}}
349// CHECK-NEXT: [[S1:%.*]] = bitcast i8* [[S0]] to [[BYREF_T]]*
350// CHECK-NEXT: [[S2:%.*]] = getelementptr inbounds [[BYREF_T]]* [[S1]], i32 0, i32 6
351// CHECK-NEXT: [[T0:%.*]] = load void ()** [[S2]], align 8
352// CHECK-NEXT: [[T1:%.*]] = bitcast void ()* [[T0]] to i8*
353// CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainBlock(i8* [[T1]])
354// CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to void ()*
355// CHECK-NEXT: store void ()* [[T3]], void ()** [[D2]], align 8
356// CHECK-NEXT: ret void
357
358// CHECK: define internal void @__Block_byref_object_dispose
359// CHECK:      [[T0:%.*]] = load i8** {{%.*}}
360// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to [[BYREF_T]]*
361// CHECK-NEXT: [[T2:%.*]] = getelementptr inbounds [[BYREF_T]]* [[T1]], i32 0, i32 6
362// CHECK-NEXT: [[T3:%.*]] = load void ()** [[T2]], align 8
363// CHECK-NEXT: [[T4:%.*]] = bitcast void ()* [[T3]] to i8*
364// CHECK-NEXT: call void @objc_release(i8* [[T4]])
365// CHECK-NEXT: ret void
366
367// Test that we correctly assign to __block variables when the
368// assignment captures the variable.
369void test10b(void) {
370  __block void (^block)(void);
371  block = ^{ block(); };
372
373  // CHECK:    define void @test10b()
374  // CHECK:      [[BYREF:%.*]] = alloca [[BYREF_T:%.*]],
375
376  // Zero-initialize.
377  // CHECK:      [[T0:%.*]] = getelementptr inbounds [[BYREF_T]]* [[BYREF]], i32 0, i32 6
378  // CHECK-NEXT: store void ()* null, void ()** [[T0]], align 8
379
380  // CHECK-NEXT: [[SLOT:%.*]] = getelementptr inbounds [[BYREF_T]]* [[BYREF]], i32 0, i32 6
381
382  // The assignment.
383  // CHECK:      [[T0:%.*]] = bitcast void ()* {{%.*}} to i8*
384  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retainBlock(i8* [[T0]])
385  // CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to void ()*
386  // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds [[BYREF_T]]* [[BYREF]], i32 0, i32 1
387  // CHECK-NEXT: [[T4:%.*]] = load [[BYREF_T]]** [[T3]]
388  // CHECK-NEXT: [[T5:%.*]] = getelementptr inbounds [[BYREF_T]]* [[T4]], i32 0, i32 6
389  // CHECK-NEXT: [[T6:%.*]] = load void ()** [[T5]], align 8
390  // CHECK-NEXT: store void ()* {{%.*}}, void ()** [[T5]], align 8
391  // CHECK-NEXT: [[T7:%.*]] = bitcast void ()* [[T6]] to i8*
392  // CHECK-NEXT: call void @objc_release(i8* [[T7]])
393
394  // Destroy at end of function.
395  // CHECK-NEXT: [[T0:%.*]] = bitcast [[BYREF_T]]* [[BYREF]] to i8*
396  // CHECK-NEXT: call void @_Block_object_dispose(i8* [[T0]], i32 8)
397  // CHECK-NEXT: [[T1:%.*]] = load void ()** [[SLOT]]
398  // CHECK-NEXT: [[T2:%.*]] = bitcast void ()* [[T1]] to i8*
399  // CHECK-NEXT: call void @objc_release(i8* [[T2]])
400  // CHECK-NEXT: ret void
401}
402
403// rdar://problem/10088932
404void test11_helper(id);
405void test11a(void) {
406  int x;
407  test11_helper(^{ (void) x; });
408
409  // CHECK:    define void @test11a()
410  // CHECK:      [[X:%.*]] = alloca i32, align 4
411  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], align 8
412  // CHECK:      [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
413  // CHECK-NEXT: [[T1:%.*]] = bitcast void ()* [[T0]] to i8*
414  // CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainBlock(i8* [[T1]])
415  // CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to void ()*
416  // CHECK-NEXT: [[T4:%.*]] = bitcast void ()* [[T3]] to i8*
417  // CHECK-NEXT: call void @test11_helper(i8* [[T4]])
418  // CHECK-NEXT: [[T5:%.*]] = bitcast void ()* [[T3]] to i8*
419  // CHECK-NEXT: call void @objc_release(i8* [[T5]])
420  // CHECK-NEXT: ret void
421}
422void test11b(void) {
423  int x;
424  id b = ^{ (void) x; };
425
426  // CHECK:    define void @test11b()
427  // CHECK:      [[X:%.*]] = alloca i32, align 4
428  // CHECK-NEXT: [[B:%.*]] = alloca i8*, align 8
429  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:<{.*}>]], align 8
430  // CHECK:      [[T0:%.*]] = bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
431  // CHECK-NEXT: [[T1:%.*]] = bitcast void ()* [[T0]] to i8*
432  // CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainBlock(i8* [[T1]])
433  // CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to void ()*
434  // CHECK-NEXT: [[T4:%.*]] = bitcast void ()* [[T3]] to i8*
435  // CHECK-NEXT: store i8* [[T4]], i8** [[B]], align 8
436  // CHECK-NEXT: [[T5:%.*]] = load i8** [[B]]
437  // CHECK-NEXT: call void @objc_release(i8* [[T5]])
438  // CHECK-NEXT: ret void
439}
440
441// rdar://problem/9979150
442@interface Test12
443@property (strong) void(^ablock)(void);
444@property (nonatomic, strong) void(^nblock)(void);
445@end
446@implementation Test12
447@synthesize ablock, nblock;
448// CHECK:    define internal void ()* @"\01-[Test12 ablock]"(
449// CHECK:    call i8* @objc_getProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i1 zeroext true)
450
451// CHECK:    define internal void @"\01-[Test12 setAblock:]"(
452// CHECK:    call void @objc_setProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i8* {{%.*}}, i1 zeroext true, i1 zeroext true)
453
454// CHECK:    define internal void ()* @"\01-[Test12 nblock]"(
455// CHECK:    call i8* @objc_getProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i1 zeroext false)
456
457// CHECK:    define internal void @"\01-[Test12 setNblock:]"(
458// CHECK:    call void @objc_setProperty(i8* {{%.*}}, i8* {{%.*}}, i64 {{%.*}}, i8* {{%.*}}, i1 zeroext false, i1 zeroext true)
459@end
460
461// rdar://problem/10131784
462void test13(id x) {
463  extern void test13_helper(id);
464  extern void test13_use(void(^)(void));
465
466  void (^b)(void) = (x ? ^{test13_helper(x);} : 0);
467  test13_use(b);
468
469  // CHECK:    define void @test13(
470  // CHECK:      [[X:%.*]] = alloca i8*, align 8
471  // CHECK-NEXT: [[B:%.*]] = alloca void ()*, align 8
472  // CHECK-NEXT: [[BLOCK:%.*]] = alloca [[BLOCK_T:.*]], align 8
473  // CHECK-NEXT: [[CLEANUP_ACTIVE:%.*]] = alloca i1
474  // CHECK-NEXT: [[T0:%.*]] = call i8* @objc_retain(i8* {{%.*}})
475  // CHECK-NEXT: store i8* [[T0]], i8** [[X]], align 8
476  // CHECK-NEXT: [[CLEANUP_ADDR:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
477  // CHECK-NEXT: [[T0:%.*]] = load i8** [[X]], align 8
478  // CHECK-NEXT: [[T1:%.*]] = icmp ne i8* [[T0]], null
479  // CHECK-NEXT: store i1 false, i1* [[CLEANUP_ACTIVE]]
480  // CHECK-NEXT: br i1 [[T1]],
481
482  // CHECK-NOT:  br
483  // CHECK:      [[CAPTURE:%.*]] = getelementptr inbounds [[BLOCK_T]]* [[BLOCK]], i32 0, i32 5
484  // CHECK-NEXT: [[T0:%.*]] = load i8** [[X]], align 8
485  // CHECK-NEXT: [[T1:%.*]] = call i8* @objc_retain(i8* [[T0]])
486  // CHECK-NEXT: store i8* [[T1]], i8** [[CAPTURE]], align 8
487  // CHECK-NEXT: store i1 true, i1* [[CLEANUP_ACTIVE]]
488  // CHECK-NEXT: bitcast [[BLOCK_T]]* [[BLOCK]] to void ()*
489  // CHECK-NEXT: br label
490  // CHECK:      br label
491  // CHECK:      [[T0:%.*]] = phi void ()*
492  // CHECK-NEXT: [[T1:%.*]] = bitcast void ()* [[T0]] to i8*
493  // CHECK-NEXT: [[T2:%.*]] = call i8* @objc_retainBlock(i8* [[T1]])
494  // CHECK-NEXT: [[T3:%.*]] = bitcast i8* [[T2]] to void ()*
495  // CHECK-NEXT: store void ()* [[T3]], void ()** [[B]], align 8
496  // CHECK-NEXT: [[T0:%.*]] = load void ()** [[B]], align 8
497  // CHECK-NEXT: call void @test13_use(void ()* [[T0]])
498  // CHECK-NEXT: [[T0:%.*]] = load void ()** [[B]]
499  // CHECK-NEXT: [[T1:%.*]] = bitcast void ()* [[T0]] to i8*
500  // CHECK-NEXT: call void @objc_release(i8* [[T1]])
501
502  // CHECK-NEXT: [[T0:%.*]] = load i1* [[CLEANUP_ACTIVE]]
503  // CHECK-NEXT: br i1 [[T0]]
504  // CHECK:      [[T0:%.*]] = load i8** [[CLEANUP_ADDR]]
505  // CHECK-NEXT: call void @objc_release(i8* [[T0]])
506  // CHECK-NEXT: br label
507
508  // CHECK:      [[T0:%.*]] = load i8** [[X]]
509  // CHECK-NEXT: call void @objc_release(i8* [[T0]])
510  // CHECK-NEXT: ret void
511}
512
513// <rdar://problem/10907510>
514void test14() {
515  void (^const x[1])(void) = { ^{} };
516}
517
518// rdar://11149025
519// Don't make invalid ASTs and crash.
520void test15_helper(void (^block)(void), int x);
521void test15(int a) {
522  test15_helper(^{ (void) a; }, ({ a; }));
523}
524