1// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple -fobjc-arc -O2 -fno-experimental-new-pass-manager | FileCheck %s --check-prefixes=CHECK,CHECK-LEGACY
2// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple -fobjc-arc -O2 -fexperimental-new-pass-manager | FileCheck %s --check-prefixes=CHECK,CHECK-NEWPM
3// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-darwin-apple -fobjc-arc -O0 | FileCheck %s -check-prefix=CHECK-O0
4
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.
8
9// rdar://problem/24528966
10
11@class NSString;
12extern __attribute__((visibility("default"))) NSString *GenString();
13
14// Behavior of __builtin_os_log differs between platforms, so only test on X86
15#ifdef __x86_64__
16// CHECK-LABEL: define i8* @test_builtin_os_log
17// CHECK-O0-LABEL: define i8* @test_builtin_os_log
18// CHECK: (i8* returned %[[BUF:.*]])
19// CHECK-O0: (i8* %[[BUF:.*]])
20void *test_builtin_os_log(void *buf) {
21  return __builtin_os_log_format(buf, "capabilities: %@", GenString());
22
23  // CHECK: %[[CALL:.*]] = tail call %[[TY0:.*]]* (...) @GenString()
24  // CHECK: %[[V0:.*]] = bitcast %[[TY0]]* %[[CALL]] to i8*
25  // CHECK: %[[V1:.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[V0]])
26  // CHECK-LEGACY: %[[V2:.*]] = ptrtoint %[[TY0]]* %[[CALL]] to i64
27  // CHECK-NEWPM: %[[V2:.*]] = ptrtoint i8* %[[V1]] to i64
28  // CHECK: store i8 2, i8* %[[BUF]], align 1
29  // CHECK: %[[NUMARGS_I:.*]] = getelementptr i8, i8* %[[BUF]], i64 1
30  // CHECK: store i8 1, i8* %[[NUMARGS_I]], align 1
31  // CHECK: %[[ARGDESCRIPTOR_I:.*]] = getelementptr i8, i8* %[[BUF]], i64 2
32  // CHECK: store i8 64, i8* %[[ARGDESCRIPTOR_I]], align 1
33  // CHECK: %[[ARGSIZE_I:.*]] = getelementptr i8, i8* %[[BUF]], i64 3
34  // CHECK: store i8 8, i8* %[[ARGSIZE_I]], align 1
35  // CHECK: %[[ARGDATA_I:.*]] = getelementptr i8, i8* %[[BUF]], i64 4
36  // CHECK: %[[ARGDATACAST_I:.*]] = bitcast i8* %[[ARGDATA_I]] to i64*
37  // CHECK: store i64 %[[V2]], i64* %[[ARGDATACAST_I]], align 1
38  // CHECK-LEGACY: tail call void (...) @llvm.objc.clang.arc.use(%[[TY0]]* %[[CALL]])
39  // CHECK-LEGACY: tail call void @llvm.objc.release(i8* %[[V0]])
40  // CHECK-NEWPM: tail call void (...) @llvm.objc.clang.arc.use(i8* %[[V1]])
41  // CHECK-NEWPM: tail call void @llvm.objc.release(i8* %[[V1]])
42  // CHECK: ret i8* %[[BUF]]
43
44  // clang.arc.use is used and removed in IR optimizations. At O0, we should not
45  // emit clang.arc.use, since it will not be removed and we will have a link
46  // error.
47  // CHECK-O0: %[[BUF_ADDR:.*]] = alloca i8*, align 8
48  // CHECK-O0: store i8* %[[BUF]], i8** %[[BUF_ADDR]], align 8
49  // CHECK-O0: %[[V0:.*]] = load i8*, i8** %[[BUF_ADDR]], align 8
50  // CHECK-O0: %[[CALL:.*]] = call %[[TY0:.*]]* (...) @GenString()
51  // CHECK-O0: %[[V1:.*]] = bitcast %[[TY0]]* %[[CALL]] to i8*
52  // CHECK-O0: %[[V2:.*]] = notail call i8* @llvm.objc.retainAutoreleasedReturnValue(i8* %[[V1]])
53  // CHECK-O0: %[[V3:.*]] = bitcast i8* %[[V2]] to %[[TY0]]*
54  // CHECK-O0: %[[V4:.*]] = ptrtoint %[[TY0]]* %[[V3]] to i64
55  // CHECK-O0: call void @__os_log_helper_1_2_1_8_64(i8* %[[V0]], i64 %[[V4]])
56  // CHECK-O0: %[[V5:.*]] = bitcast %[[TY0]]* %[[V3]] to i8*
57  // CHECK-O0-NOT: call void (...) @llvm.objc.clang.arc.use({{.*}}
58  // CHECK-O0: call void @llvm.objc.release(i8* %[[V5]])
59  // CHECK-O0: ret i8* %[[V0]]
60}
61
62// CHECK-O0-LABEL: define linkonce_odr hidden void @__os_log_helper_1_2_1_8_64
63// CHECK-O0: (i8* %[[BUFFER:.*]], i64 %[[ARG0:.*]])
64
65// CHECK-O0: %[[BUFFER_ADDR:.*]] = alloca i8*, align 8
66// CHECK-O0: %[[ARG0_ADDR:.*]] = alloca i64, align 8
67// CHECK-O0: store i8* %[[BUFFER]], i8** %[[BUFFER_ADDR]], align 8
68// CHECK-O0: store i64 %[[ARG0]], i64* %[[ARG0_ADDR]], align 8
69// CHECK-O0: %[[BUF:.*]] = load i8*, i8** %[[BUFFER_ADDR]], align 8
70// CHECK-O0: %[[SUMMARY:.*]] = getelementptr i8, i8* %[[BUF]], i64 0
71// CHECK-O0: store i8 2, i8* %[[SUMMARY]], align 1
72// CHECK-O0: %[[NUMARGS:.*]] = getelementptr i8, i8* %[[BUF]], i64 1
73// CHECK-O0: store i8 1, i8* %[[NUMARGS]], align 1
74// CHECK-O0: %[[ARGDESCRIPTOR:.*]] = getelementptr i8, i8* %[[BUF]], i64 2
75// CHECK-O0: store i8 64, i8* %[[ARGDESCRIPTOR]], align 1
76// CHECK-O0: %[[ARGSIZE:.*]] = getelementptr i8, i8* %[[BUF]], i64 3
77// CHECK-O0: store i8 8, i8* %[[ARGSIZE]], align 1
78// CHECK-O0: %[[ARGDATA:.*]] = getelementptr i8, i8* %[[BUF]], i64 4
79// CHECK-O0: %[[ARGDATACAST:.*]] = bitcast i8* %[[ARGDATA]] to i64*
80// CHECK-O0: %[[V0:.*]] = load i64, i64* %[[ARG0_ADDR]], align 8
81// CHECK-O0: store i64 %[[V0]], i64* %[[ARGDATACAST]], align 1
82
83#endif
84