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