1 // RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple -fobjc-arc -O2 -disable-llvm-passes | FileCheck %s --check-prefixes=CHECK,CHECK-O2
2 // RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple -fobjc-arc -O0 | FileCheck %s --check-prefixes=CHECK,CHECK-O0
3 // RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple -O2 -disable-llvm-passes | FileCheck %s --check-prefix=CHECK-MRR
5 // Make sure we emit clang.arc.use before calling objc_release as part of the
6 // cleanup. This way we make sure the object will not be released until the
7 // end of the full expression.
9 // rdar://problem/24528966
19 extern __attribute__((visibility("default"))) NSString *GenString(void);
20 void os_log_pack_send(void *);
22 // CHECK-LABEL: define{{.*}} void @test_builtin_os_log1(
23 // CHECK: alloca i8*, align 8
24 // CHECK: %[[A_ADDR:.*]] = alloca i8*, align 8
25 // CHECK: %[[OS_LOG_ARG:.*]] = alloca %{{.*}}*, align 8
26 // CHECK-O2: %[[V0:.*]] = call i8* @llvm.objc.retain(
27 // CHECK-O2: store i8* %[[V0]], i8** %[[A_ADDR]], align 8,
28 // CHECK-O0: call void @llvm.objc.storeStrong(i8** %[[A_ADDR]], i8* %{{.*}})
29 // CHECK-O2: %[[V4:.*]] = call %{{.*}}* @GenString() [ "clang.arc.attachedcall"(i8* (i8*)* @llvm.objc.retainAutoreleasedReturnValue) ]
30 // CHECK-O0: %[[CALL:.*]] = call %{{.*}}* @GenString()
31 // CHECK-O0: %[[V2:.*]] = bitcast %{{.*}}* %[[CALL]] to i8*
32 // CHECK-O0: %[[V3:.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[V2]])
33 // CHECK-O0: %[[V4:.*]] = bitcast i8* %[[V3]] to %{{.*}}*
34 // CHECK: %[[V5:.*]] = bitcast %{{.*}}* %[[V4]] to i8*
35 // CHECK: %[[V6:.*]] = call i8* @llvm.objc.retain(i8* %[[V5]])
36 // CHECK: %[[V7:.*]] = bitcast i8* %[[V6]] to %{{.*}}*
37 // CHECK: store %{{.*}}* %[[V7]], %{{.*}}** %[[OS_LOG_ARG]],
38 // CHECK: %[[V8:.*]] = ptrtoint %{{.*}}* %[[V7]] to i64
39 // CHECK: %[[V9:.*]] = load i8*, i8** %[[A_ADDR]], align 8
40 // CHECK: %[[V10:.*]] = ptrtoint i8* %[[V9]] to i64
41 // CHECK: call void @__os_log_helper_1_2_2_8_64_8_64(i8* noundef %{{.*}}, i64 noundef %[[V8]], i64 noundef %[[V10]])
42 // CHECK: %[[V11:.*]] = bitcast %{{.*}}* %[[V4]] to i8*
43 // CHECK: call void @llvm.objc.release(i8* %[[V11]])
44 // CHECK: call void @os_log_pack_send(i8* noundef %{{.*}})
45 // CHECK-O2: call void (...) @llvm.objc.clang.arc.use(%{{.*}}* %[[V7]])
46 // CHECK-O2: %[[V13:.*]] = load %{{.*}}*, %{{.*}}** %[[OS_LOG_ARG]], align 8
47 // CHECK-O2: %[[V14:.*]] = bitcast %{{.*}}* %[[V13]] to i8*
48 // CHECK-O2: call void @llvm.objc.release(i8* %[[V14]])
49 // CHECK-O2: %[[V15:.*]] = load i8*, i8** %[[A_ADDR]], align 8
50 // CHECK-O2: call void @llvm.objc.release(i8* %[[V15]])
51 // CHECK-O0: %[[V12:.*]] = bitcast %{{.*}}** %[[OS_LOG_ARG]] to i8**
52 // CHECK-O0: call void @llvm.objc.storeStrong(i8** %[[V12]], i8* null)
53 // CHECK-O0: call void @llvm.objc.storeStrong(i8** %[[A_ADDR]], i8* null)
55 // CHECK-MRR-LABEL: define{{.*}} void @test_builtin_os_log1(
56 // CHECK-MRR-NOT: call {{.*}} @llvm.objc
57 // CHECK-MRR: ret void
59 void test_builtin_os_log1(void *buf, id a) {
60 __builtin_os_log_format(buf, "capabilities: %@ %@", GenString(), a);
61 os_log_pack_send(buf);
64 // CHECK: define{{.*}} void @test_builtin_os_log2(
65 // CHECK-NOT: @llvm.objc.retain(
67 void test_builtin_os_log2(void *buf, id __unsafe_unretained a) {
68 __builtin_os_log_format(buf, "capabilities: %@", a);
69 os_log_pack_send(buf);
72 // CHECK-LABEL: define{{.*}} void @test_builtin_os_log3(
73 // CHECK: alloca i8*, align 8
74 // CHECK: %[[OS_LOG_ARG:.*]] = alloca i8*, align 8
75 // CHECK-O2: %[[V3:.*]] = call %{{.*}}* @GenString() [ "clang.arc.attachedcall"(i8* (i8*)* @llvm.objc.retainAutoreleasedReturnValue) ]
76 // CHECK-O0: %[[CALL:.*]] = call %{{.*}}* @GenString()
77 // CHECK-O0: %[[V1:.*]] = bitcast %{{.*}}* %[[CALL]] to i8*
78 // CHECK-O0: %[[V2:.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[V1]])
79 // CHECK-O0: %[[V3:.*]] = bitcast i8* %[[V2]] to %{{.*}}*
80 // CHECK: %[[V4:.*]] = bitcast %{{.*}}* %[[V3]] to i8*
81 // CHECK: %[[V5:.*]] = call i8* @llvm.objc.retain(i8* %[[V4]])
82 // CHECK: store i8* %[[V5]], i8** %[[OS_LOG_ARG]], align 8
83 // CHECK: %[[V6:.*]] = ptrtoint i8* %[[V5]] to i64
84 // CHECK: call void @__os_log_helper_1_2_1_8_64(i8* noundef %{{.*}}, i64 noundef %[[V6]])
85 // CHECK: %[[V7:.*]] = bitcast %{{.*}}* %[[V3]] to i8*
86 // CHECK: call void @llvm.objc.release(i8* %[[V7]])
87 // CHECK: call void @os_log_pack_send(i8* noundef %{{.*}})
88 // CHECK-O2: call void (...) @llvm.objc.clang.arc.use(i8* %[[V5]])
89 // CHECK-O2: %[[V9:.*]] = load i8*, i8** %[[OS_LOG_ARG]], align 8
90 // CHECK-O2: call void @llvm.objc.release(i8* %[[V9]])
91 // CHECK-O0: call void @llvm.objc.storeStrong(i8** %[[OS_LOG_ARG]], i8* null)
93 void test_builtin_os_log3(void *buf) {
94 __builtin_os_log_format(buf, "capabilities: %@", (id)GenString());
95 os_log_pack_send(buf);
98 // CHECK-LABEL: define{{.*}} void @test_builtin_os_log4(
99 // CHECK: alloca i8*, align 8
100 // CHECK: %[[OS_LOG_ARG:.*]] = alloca i8*, align 8
101 // CHECK: %[[OS_LOG_ARG2:.*]] = alloca i8*, align 8
102 // CHECK-O2: %[[V4:.*]] = call {{.*}} @objc_msgSend{{.*}} [ "clang.arc.attachedcall"(i8* (i8*)* @llvm.objc.retainAutoreleasedReturnValue) ]
103 // CHECK-O0: %[[CALL:.*]] = call {{.*}} @objc_msgSend
104 // CHECK-O0: %[[V4:.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[CALL]])
105 // CHECK: %[[V5:.*]] = call i8* @llvm.objc.retain(i8* %[[V4]])
106 // CHECK: store i8* %[[V5]], i8** %[[OS_LOG_ARG]], align 8
107 // CHECK: %[[V6:.*]] = ptrtoint i8* %[[V5]] to i64
108 // CHECK-O2: %[[V10:.*]] = call {{.*}} @objc_msgSend{{.*}} [ "clang.arc.attachedcall"(i8* (i8*)* @llvm.objc.retainAutoreleasedReturnValue) ]
109 // CHECK-O0: %[[CALL1:.*]] = call {{.*}} @objc_msgSend
110 // CHECK-O0: %[[V10:.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[CALL1]])
111 // CHECK: %[[V11:.*]] = call i8* @llvm.objc.retain(i8* %[[V10]])
112 // CHECK: store i8* %[[V11]], i8** %[[OS_LOG_ARG2]], align 8
113 // CHECK: %[[V12:.*]] = ptrtoint i8* %[[V11]] to i64
114 // CHECK: call void @__os_log_helper_1_2_2_8_64_8_64(i8* noundef %{{.*}}, i64 noundef %[[V6]], i64 noundef %[[V12]])
115 // CHECK: call void @llvm.objc.release(i8* %[[V10]])
116 // CHECK: call void @llvm.objc.release(i8* %[[V4]])
117 // CHECK: call void @os_log_pack_send(i8* noundef %{{.*}})
118 // CHECK-O2: call void (...) @llvm.objc.clang.arc.use(i8* %[[V11]])
119 // CHECK-O2: %[[V14:.*]] = load i8*, i8** %[[OS_LOG_ARG2]], align 8
120 // CHECK-O2: call void @llvm.objc.release(i8* %[[V14]])
121 // CHECK-O2: call void (...) @llvm.objc.clang.arc.use(i8* %[[V5]])
122 // CHECK-O2: %[[V15:.*]] = load i8*, i8** %[[OS_LOG_ARG]], align 8
123 // CHECK-O2: call void @llvm.objc.release(i8* %[[V15]])
125 void test_builtin_os_log4(void *buf) {
126 __builtin_os_log_format(buf, "capabilities: %@ %@", [c m0], [C m1]);
127 os_log_pack_send(buf);
130 // FIXME: Lifetime of GenString's return should be extended in this case too.
131 // CHECK-LABEL: define{{.*}} void @test_builtin_os_log5(
132 // CHECK: call void @os_log_pack_send(
133 // CHECK-NOT: call void @llvm.objc.release(
135 void test_builtin_os_log5(void *buf) {
136 __builtin_os_log_format(buf, "capabilities: %@", (0, GenString()));
137 os_log_pack_send(buf);