1; RUN: llc -mtriple=aarch64-linux-gnu -disable-post-ra -verify-machineinstrs -o - %s | FileCheck %s
2; RUN: llc -mtriple=arm64-apple-ios -disable-post-ra -verify-machineinstrs -o - %s | FileCheck %s --check-prefix=CHECK-MACHO
3; RUN: llc -mtriple=aarch64-none-linux-gnu -disable-post-ra -mattr=-fp-armv8 -verify-machineinstrs < %s | FileCheck --check-prefix=CHECK-NOFP-ARM64 %s
4
5declare void @use_addr(i8*)
6
7define void @test_simple_alloca(i64 %n) {
8; CHECK-LABEL: test_simple_alloca:
9
10  %buf = alloca i8, i64 %n
11  ; Make sure we align the stack change to 16 bytes:
12; CHECK: {{mov|add}} x29
13; CHECK: mov [[TMP:x[0-9]+]], sp
14; CHECK: add [[SPDELTA_TMP:x[0-9]+]], x0, #15
15; CHECK: and [[SPDELTA:x[0-9]+]], [[SPDELTA_TMP]], #0xfffffffffffffff0
16
17  ; Make sure we change SP. It would be surprising if anything but x0 were used
18  ; for the final sp, but it could be if it was then moved into x0.
19; CHECK: sub [[NEWSP:x[0-9]+]], [[TMP]], [[SPDELTA]]
20; CHECK: mov sp, [[NEWSP]]
21
22  call void @use_addr(i8* %buf)
23; CHECK: bl use_addr
24
25  ret void
26  ; Make sure epilogue restores sp from fp
27; CHECK: {{sub|mov}} sp, x29
28; CHECK: ret
29}
30
31declare void @use_addr_loc(i8*, i64*)
32
33define i64 @test_alloca_with_local(i64 %n) {
34; CHECK-LABEL: test_alloca_with_local:
35; CHECK-DAG: sub sp, sp, [[LOCAL_STACK:#[0-9]+]]
36; CHECK-DAG: {{mov|add}} x29, sp
37
38  %loc = alloca i64
39  %buf = alloca i8, i64 %n
40  ; Make sure we align the stack change to 16 bytes:
41; CHECK: mov [[TMP:x[0-9]+]], sp
42; CHECK: add [[SPDELTA_TMP:x[0-9]+]], x0, #15
43; CHECK: and [[SPDELTA:x[0-9]+]], [[SPDELTA_TMP]], #0xfffffffffffffff0
44
45  ; Make sure we change SP. It would be surprising if anything but x0 were used
46  ; for the final sp, but it could be if it was then moved into x0.
47; CHECK: sub [[NEWSP:x[0-9]+]], [[TMP]], [[SPDELTA]]
48; CHECK: mov sp, [[NEWSP]]
49
50; CHECK: sub {{x[0-9]+}}, x29, #[[LOC_FROM_FP:[0-9]+]]
51
52  call void @use_addr_loc(i8* %buf, i64* %loc)
53; CHECK: bl use_addr
54
55  %val = load i64, i64* %loc
56
57; CHECK: ldur x0, [x29, #-[[LOC_FROM_FP]]]
58
59  ret i64 %val
60  ; Make sure epilogue restores sp from fp
61; CHECK: {{sub|mov}} sp, x29
62; CHECK: ret
63}
64
65define void @test_variadic_alloca(i64 %n, ...) {
66; CHECK-LABEL: test_variadic_alloca:
67
68; [...]
69
70
71; CHECK-NOFP-AARCH64: sub     sp, sp, #80
72; CHECK-NOFP-AARCH64: stp     x29, x30, [sp, #64]
73; CHECK-NOFP-AARCH64: add     x29, sp, #64
74; CHECK-NOFP-AARCH64: sub     [[TMP:x[0-9]+]], x29, #64
75; CHECK-NOFP-AARCH64: add     x8, [[TMP]], #0
76
77
78; CHECK: stp     x29, x30, [sp, #-16]!
79; CHECK: mov     x29, sp
80; CHECK: sub     sp, sp, #192
81; CHECK: stp     q6, q7, [x29, #-96]
82; [...]
83; CHECK: stp     q0, q1, [x29, #-192]
84
85; CHECK: stp     x6, x7, [x29, #-16]
86; [...]
87; CHECK: stp     x2, x3, [x29, #-48]
88
89; CHECK-NOFP-ARM64: stp     x29, x30, [sp, #-16]!
90; CHECK-NOFP-ARM64: mov     x29, sp
91; CHECK-NOFP-ARM64: sub     sp, sp, #64
92; CHECK-NOFP-ARM64: stp     x6, x7, [x29, #-16]
93; [...]
94; CHECK-NOFP-ARM64: stp     x4, x5, [x29, #-32]
95; [...]
96; CHECK-NOFP-ARM64: stp     x2, x3, [x29, #-48]
97; [...]
98; CHECK-NOFP-ARM64: mov     x8, sp
99
100  %addr = alloca i8, i64 %n
101
102  call void @use_addr(i8* %addr)
103; CHECK: bl use_addr
104
105  ret void
106
107; CHECK-NOFP-AARCH64: sub sp, x29, #64
108; CHECK-NOFP-AARCH64: ldp x29, x30, [sp, #64]
109; CHECK-NOFP-AARCH64: add sp, sp, #80
110
111; CHECK-NOFP-ARM64: mov sp, x29
112; CHECK-NOFP-ARM64: ldp x29, x30, [sp], #16
113}
114
115define void @test_alloca_large_frame(i64 %n) {
116; CHECK-LABEL: test_alloca_large_frame:
117; CHECK-MACHO-LABEL: test_alloca_large_frame:
118
119
120; CHECK: stp     x28, x19, [sp, #-32]!
121; CHECK: stp     x29, x30, [sp, #16]
122; CHECK: add     x29, sp, #16
123; CHECK: sub     sp, sp, #1953, lsl #12
124; CHECK: sub     sp, sp, #512
125
126; CHECK-MACHO: stp     x20, x19, [sp, #-32]!
127; CHECK-MACHO: stp     x29, x30, [sp, #16]
128; CHECK-MACHO: add     x29, sp, #16
129; CHECK-MACHO: sub     sp, sp, #1953, lsl #12
130; CHECK-MACHO: sub     sp, sp, #512
131
132  %addr1 = alloca i8, i64 %n
133  %addr2 = alloca i64, i64 1000000
134
135  call void @use_addr_loc(i8* %addr1, i64* %addr2)
136
137  ret void
138
139; CHECK: sub     sp, x29, #16
140; CHECK: ldp     x29, x30, [sp, #16]
141; CHECK: ldp     x28, x19, [sp], #32
142
143; CHECK-MACHO: sub     sp, x29, #16
144; CHECK-MACHO: ldp     x29, x30, [sp, #16]
145; CHECK-MACHO: ldp     x20, x19, [sp], #32
146}
147
148declare i8* @llvm.stacksave()
149declare void @llvm.stackrestore(i8*)
150
151define void @test_scoped_alloca(i64 %n) {
152; CHECK-LABEL: test_scoped_alloca:
153
154  %sp = call i8* @llvm.stacksave()
155; CHECK: mov [[SAVED_SP:x[0-9]+]], sp
156; CHECK: mov [[OLDSP:x[0-9]+]], sp
157
158  %addr = alloca i8, i64 %n
159; CHECK: and [[SPDELTA:x[0-9]+]], {{x[0-9]+}}, #0xfffffffffffffff0
160; CHECK-DAG: sub [[NEWSP:x[0-9]+]], [[OLDSP]], [[SPDELTA]]
161; CHECK: mov sp, [[NEWSP]]
162
163  call void @use_addr(i8* %addr)
164; CHECK: bl use_addr
165
166  call void @llvm.stackrestore(i8* %sp)
167; CHECK: mov sp, [[SAVED_SP]]
168
169  ret void
170}
171