xref: /llvm-project/clang/test/CodeGen/AArch64/byval-temp.c (revision 207e5ccceec8d3cc3f32723e78f2a142bc61b07d)
1 // RUN: %clang_cc1 -emit-llvm -triple arm64-- -fexperimental-max-bitint-width=1024  -o - %s -O0 | FileCheck %s --check-prefix=CHECK-O0
2 // RUN: %clang_cc1 -emit-llvm -disable-llvm-optzns -fexperimental-max-bitint-width=1024  -triple arm64-- -o - %s -O3 | FileCheck %s --check-prefix=CHECK-O3
3 
4 struct large {
5     void* pointers[8];
6 };
7 
8 void pass_large(struct large);
9 void pass_large_BitInt(_BitInt(129));
10 
11 // For arm64, we don't use byval to pass structs and _BitInt(>128) type, but instead we create
12 // temporary allocas.
13 //
14 // Make sure we generate the appropriate lifetime markers for the temporary
15 // allocas so that the optimizer can re-use stack slots if possible.
16 void example(void) {
17     struct large l = {0};
18     pass_large(l);
19     pass_large(l);
20 }
21 // CHECK-O0-LABEL: define{{.*}} void @example(
22 // The alloca for the struct on the stack.
23 // CHECK-O0: %[[l:[0-9A-Za-z-]+]] = alloca %struct.large, align 8
24 // The alloca for the temporary stack space that we use to pass the argument.
25 // CHECK-O0-NEXT: %[[byvaltemp:[0-9A-Za-z-]+]] = alloca %struct.large, align 8
26 // Another one to pass the argument to the second function call.
27 // CHECK-O0-NEXT: %[[byvaltemp1:[0-9A-Za-z-]+]] = alloca %struct.large, align 8
28 // First, memset `l` to 0.
29 // CHECK-O0-NEXT: call void @llvm.memset.p0.i64(ptr align 8 %[[l]], i8 0, i64 64, i1 false)
30 // Then, memcpy `l` to the temporary stack space.
31 // CHECK-O0-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[byvaltemp]], ptr align 8 %[[l]], i64 64, i1 false)
32 // Finally, call using a pointer to the temporary stack space.
33 // CHECK-O0-NEXT: call void @pass_large(ptr noundef %[[byvaltemp]])
34 // Now, do the same for the second call, using the second temporary alloca.
35 // CHECK-O0-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[byvaltemp1]], ptr align 8 %[[l]], i64 64, i1 false)
36 // CHECK-O0-NEXT: call void @pass_large(ptr noundef %[[byvaltemp1]])
37 // CHECK-O0-NEXT: ret void
38 //
39 // At O3, we should have lifetime markers to help the optimizer re-use the temporary allocas.
40 //
41 // CHECK-O3-LABEL: define{{.*}} void @example(
42 // The alloca for the struct on the stack.
43 // CHECK-O3: %[[l:[0-9A-Za-z-]+]] = alloca %struct.large, align 8
44 // The alloca for the temporary stack space that we use to pass the argument.
45 // CHECK-O3-NEXT: %[[byvaltemp:[0-9A-Za-z-]+]] = alloca %struct.large, align 8
46 // Another one to pass the argument to the second function call.
47 // CHECK-O3-NEXT: %[[byvaltemp1:[0-9A-Za-z-]+]] = alloca %struct.large, align 8
48 //
49 // Mark the start of the lifetime for `l`
50 // CHECK-O3-NEXT: call void @llvm.lifetime.start.p0(i64 64, ptr %[[l]])
51 //
52 // First, memset `l` to 0.
53 // CHECK-O3-NEXT: call void @llvm.memset.p0.i64(ptr align 8 %[[l]], i8 0, i64 64, i1 false)
54 //
55 // Lifetime of the first temporary starts here and ends right after the call.
56 // CHECK-O3-NEXT: call void @llvm.lifetime.start.p0(i64 64, ptr %[[byvaltemp]])
57 //
58 // Then, memcpy `l` to the temporary stack space.
59 // CHECK-O3-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[byvaltemp]], ptr align 8 %[[l]], i64 64, i1 false)
60 // Finally, call using a pointer to the temporary stack space.
61 // CHECK-O3-NEXT: call void @pass_large(ptr noundef %[[byvaltemp]])
62 //
63 // The lifetime of the temporary used to pass a pointer to the struct ends here.
64 // CHECK-O3-NEXT: call void @llvm.lifetime.end.p0(i64 64, ptr %[[byvaltemp]])
65 //
66 // Now, do the same for the second call, using the second temporary alloca.
67 // CHECK-O3-NEXT: call void @llvm.lifetime.start.p0(i64 64, ptr %[[byvaltemp1]])
68 // CHECK-O3-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[byvaltemp1]], ptr align 8 %[[l]], i64 64, i1 false)
69 // CHECK-O3-NEXT: call void @pass_large(ptr noundef %[[byvaltemp1]])
70 // CHECK-O3-NEXT: call void @llvm.lifetime.end.p0(i64 64, ptr %[[byvaltemp1]])
71 //
72 // Mark the end of the lifetime of `l`.
73 // CHECK-O3-NEXT: call void @llvm.lifetime.end.p0(i64 64, ptr %l)
74 // CHECK-O3-NEXT: ret void
75 
76 void example_BitInt(void) {
77     _BitInt(129) l = {0};
78     pass_large_BitInt(l);
79     pass_large_BitInt(l);
80 }
81 // CHECK-O0-LABEL: define dso_local void @example_BitInt(
82 // CHECK-O0-NEXT:  entry:
83 // CHECK-O0-NEXT:    [[L:%.*]] = alloca i256, align 16
84 // CHECK-O0-NEXT:    [[INDIRECT_ARG_TEMP:%.*]] = alloca i256, align 16
85 // CHECK-O0-NEXT:    [[INDIRECT_ARG_TEMP1:%.*]] = alloca i256, align 16
86 // CHECK-O0-NEXT:    store i256 0, ptr [[L]], align 16
87 // CHECK-O0-NEXT:    [[TMP0:%.*]] = load i256, ptr [[L]], align 16
88 // CHECK-O0-NEXT:    [[LOADEDV:%.*]] = trunc i256 [[TMP0]] to i129
89 // CHECK-O0-NEXT:    [[STOREDV:%.*]] = sext i129 [[LOADEDV]] to i256
90 // CHECK-O0-NEXT:    store i256 [[STOREDV]], ptr [[INDIRECT_ARG_TEMP]], align 16
91 // CHECK-O0-NEXT:    call void @pass_large_BitInt(ptr noundef [[INDIRECT_ARG_TEMP]])
92 // CHECK-O0-NEXT:    [[TMP1:%.*]] = load i256, ptr [[L]], align 16
93 // CHECK-O0-NEXT:    [[LOADEDV1:%.*]] = trunc i256 [[TMP1]] to i129
94 // CHECK-O0-NEXT:    [[STOREDV1:%.*]] = sext i129 [[LOADEDV1]] to i256
95 // CHECK-O0-NEXT:    store i256 [[STOREDV1]], ptr [[INDIRECT_ARG_TEMP1]], align 16
96 // CHECK-O0-NEXT:    call void @pass_large_BitInt(ptr noundef [[INDIRECT_ARG_TEMP1]])
97 // CHECK-O0-NEXT:    ret void
98 //
99 // CHECK-O3-LABEL: define dso_local void @example_BitInt(
100 // CHECK-O3-NEXT:  entry:
101 // CHECK-O3-NEXT:    [[L:%.*]] = alloca i256, align 16
102 // CHECK-O3-NEXT:    [[INDIRECT_ARG_TEMP:%.*]] = alloca i256, align 16
103 // CHECK-O3-NEXT:    [[INDIRECT_ARG_TEMP1:%.*]] = alloca i256, align 16
104 // CHECK-O3-NEXT:    call void @llvm.lifetime.start.p0(i64 32, ptr [[L]])
105 // CHECK-O3-NEXT:    store i256 0, ptr [[L]], align 16, !tbaa [[TBAA6:![0-9]+]]
106 // CHECK-O3-NEXT:    [[TMP0:%.*]] = load i256, ptr [[L]], align 16, !tbaa [[TBAA6]]
107 // CHECK-O3-NEXT:    [[LOADEDV:%.*]] = trunc i256 [[TMP0]] to i129
108 // CHECK-O3-NEXT:    call void @llvm.lifetime.start.p0(i64 32, ptr [[INDIRECT_ARG_TEMP]])
109 // CHECK-O3-NEXT:    [[STOREDV:%.*]] = sext i129 [[LOADEDV]] to i256
110 // CHECK-O3-NEXT:    store i256 [[STOREDV]], ptr [[INDIRECT_ARG_TEMP]], align 16, !tbaa [[TBAA6]]
111 // CHECK-O3-NEXT:    call void @pass_large_BitInt(ptr noundef [[INDIRECT_ARG_TEMP]])
112 // CHECK-O3-NEXT:    call void @llvm.lifetime.end.p0(i64 32, ptr [[INDIRECT_ARG_TEMP]])
113 // CHECK-O3-NEXT:    [[TMP1:%.*]] = load i256, ptr [[L]], align 16, !tbaa [[TBAA6]]
114 // CHECK-O3-NEXT:    [[LOADEDV1:%.*]] = trunc i256 [[TMP1]] to i129
115 // CHECK-O3-NEXT:    call void @llvm.lifetime.start.p0(i64 32, ptr [[INDIRECT_ARG_TEMP1]])
116 // CHECK-O3-NEXT:    [[STOREDV1:%.*]] = sext i129 [[LOADEDV1]] to i256
117 // CHECK-O3-NEXT:    store i256 [[STOREDV1]], ptr [[INDIRECT_ARG_TEMP1]], align 16, !tbaa [[TBAA6]]
118 // CHECK-O3-NEXT:    call void @pass_large_BitInt(ptr noundef [[INDIRECT_ARG_TEMP1]])
119 // CHECK-O3-NEXT:    call void @llvm.lifetime.end.p0(i64 32, ptr [[INDIRECT_ARG_TEMP1]])
120 // CHECK-O3-NEXT:    call void @llvm.lifetime.end.p0(i64 32, ptr [[L]])
121