Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGen / blocks.c
blob469cf7cb89a2c389a24b387cca3b59fabc5ce14c
1 // RUN: %clang_cc1 -triple i386-unknown-unknown %s -emit-llvm -Wno-strict-prototypes -o - -fblocks | FileCheck %s
3 // CHECK: @{{.*}} = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr @{{.*}}, ptr null }, align 4
4 // CHECK: @[[BLOCK_DESCRIPTOR_TMP21:.*]] = internal constant { i32, i32, ptr, ptr, ptr, ptr } { i32 0, i32 24, ptr @__copy_helper_block_4_20r, ptr @__destroy_helper_block_4_20r, ptr @{{.*}}, ptr null }, align 4
6 void (^f)(void) = ^{};
8 int f0(int (^a0)()) {
9 return a0(1, 2, 3);
12 // Verify that attributes on blocks are set correctly.
13 typedef struct s0 T;
14 struct s0 {
15 int a[64];
18 // CHECK: define internal void @__f2_block_invoke(ptr noalias sret(%struct.s0) align 4 {{%.*}}, ptr noundef {{%.*}}, ptr noundef byval(%struct.s0) align 4 {{.*}})
19 struct s0 f2(struct s0 a0) {
20 return ^(struct s0 a1){ return a1; }(a0);
23 // This should not crash.
24 void *P = ^{
25 void *Q = __func__;
28 void (^test1)(void) = ^(void) {
29 __block int i;
30 ^ { i = 1; }();
33 // CHECK-LABEL: define linkonce_odr hidden void @__copy_helper_block_4_20r(ptr noundef %0, ptr noundef %1) unnamed_addr
34 // CHECK: %[[_ADDR:.*]] = alloca ptr, align 4
35 // CHECK-NEXT: %[[_ADDR1:.*]] = alloca ptr, align 4
36 // CHECK-NEXT: store ptr %0, ptr %[[_ADDR]], align 4
37 // CHECK-NEXT: store ptr %1, ptr %[[_ADDR1]], align 4
38 // CHECK-NEXT: %[[V2:.*]] = load ptr, ptr %[[_ADDR1]], align 4
39 // CHECK-NEXT: %[[V3:.*]] = load ptr, ptr %[[_ADDR]], align 4
40 // CHECK-NEXT: %[[V4:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[V2]], i32 0, i32 5
41 // CHECK-NEXT: %[[V5:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[V3]], i32 0, i32 5
42 // CHECK-NEXT: %[[BLOCKCOPY_SRC:.*]] = load ptr, ptr %[[V4]], align 4
43 // CHECK-NEXT: call void @_Block_object_assign(ptr %[[V5]], ptr %[[BLOCKCOPY_SRC]], i32 8)
44 // CHECK-NEXT: ret void
46 // CHECK-LABEL: define linkonce_odr hidden void @__destroy_helper_block_4_20r(ptr noundef %0) unnamed_addr
47 // CHECK: %[[_ADDR:.*]] = alloca ptr, align 4
48 // CHECK-NEXT: store ptr %0, ptr %[[_ADDR]], align 4
49 // CHECK-NEXT: %[[V1:.*]] = load ptr, ptr %[[_ADDR]], align 4
50 // CHECK-NEXT: %[[V2:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %[[V1]], i32 0, i32 5
51 // CHECK-NEXT: %[[V3:.*]] = load ptr, ptr %[[V2]], align 4
52 // CHECK-NEXT: call void @_Block_object_dispose(ptr %[[V3]], i32 8)
53 // CHECK-NEXT: ret void
55 typedef double ftype(double);
56 // It's not clear that we *should* support this syntax, but until that decision
57 // is made, we should support it properly and not crash.
58 ftype ^test2 = ^ftype {
59 return 0;
62 void f3_helper(void (^)(void));
63 void f3(void) {
64 _Bool b = 0;
65 f3_helper(^{ if (b) {} });
68 // The bool can fill in between the header and the long long.
69 // Add the appropriate amount of padding between them.
70 void f4_helper(long long (^)(void));
71 // CHECK-LABEL: define{{.*}} void @f4()
72 void f4(void) {
73 _Bool b = 0;
74 long long ll = 0;
75 // CHECK: alloca <{ ptr, i32, i32, ptr, ptr, i8, [3 x i8], i64 }>, align 8
76 f4_helper(^{ if (b) return ll; return 0LL; });
79 // The alignment after rounding up to the align of F5 is actually
80 // greater than the required alignment. Don't assert.
81 struct F5 {
82 char buffer[32] __attribute((aligned));
84 void f5_helper(void (^)(struct F5 *));
85 // CHECK-LABEL: define{{.*}} void @f5()
86 void f5(void) {
87 struct F5 value;
88 // CHECK: alloca <{ ptr, i32, i32, ptr, ptr, [12 x i8], [[F5:%.*]] }>, align 16
89 f5_helper(^(struct F5 *slot) { *slot = value; });
92 void (^b)() = ^{};
93 int main(void) {
94 (b?: ^{})();
96 // CHECK: [[ZERO:%.*]] = load ptr, ptr @b
97 // CHECK-NEXT: [[TB:%.*]] = icmp ne ptr [[ZERO]], null
98 // CHECK-NEXT: br i1 [[TB]], label [[CT:%.*]], label [[CF:%.*]]
99 // CHECK: br label [[CE:%.*]]
101 // Ensure that we don't emit helper code in copy/dispose routines for variables
102 // that are const-captured.
103 void testConstCaptureInCopyAndDestroyHelpers(void) {
104 const int x = 0;
105 __block int i;
106 (^ { i = x; })();
108 // CHECK-LABEL: define{{.*}} void @testConstCaptureInCopyAndDestroyHelpers(
109 // CHECK: %[[BLOCK_DESCRIPTOR:.*]] = getelementptr inbounds <{ ptr, i32, i32, ptr, ptr, ptr }>, ptr %{{.*}}, i32 0, i32 4
110 // CHECK: store ptr @[[BLOCK_DESCRIPTOR_TMP21]], ptr %[[BLOCK_DESCRIPTOR]], align 4
112 // CHECK-LABEL: define internal void @__testConstCaptureInCopyAndDestroyHelpers_block_invoke