xref: /llvm-project/llvm/test/CodeGen/AArch64/tailcc-tail-call.ll (revision d0ea42a7c1bcd47aa2b8c009b40fc4278d63f5cf)
1; RUN: llc -verify-machineinstrs < %s -mtriple=aarch64-none-linux-gnu | FileCheck %s --check-prefixes=SDAG,COMMON
2; RUN: llc -global-isel -global-isel-abort=1 -verify-machineinstrs < %s -mtriple=aarch64-none-linux-gnu  | FileCheck %s --check-prefixes=GISEL,COMMON
3
4declare tailcc void @callee_stack0()
5declare tailcc void @callee_stack8([8 x i64], i64)
6declare tailcc void @callee_stack16([8 x i64], i64, i64)
7declare extern_weak tailcc void @callee_weak()
8
9define tailcc void @caller_to0_from0() nounwind {
10; COMMON-LABEL: caller_to0_from0:
11; COMMON-NEXT: // %bb.
12
13  tail call tailcc void @callee_stack0()
14  ret void
15
16; COMMON-NEXT: b callee_stack0
17}
18
19define tailcc void @caller_to0_from8([8 x i64], i64) #0 {
20; COMMON-LABEL: caller_to0_from8:
21
22  tail call tailcc void @callee_stack0()
23  ret void
24
25; COMMON: add sp, sp, #16
26; COMMON: .cfi_def_cfa_offset -16
27; COMMON-NEXT: b callee_stack0
28}
29
30define tailcc void @caller_to8_from0() "frame-pointer"="all" uwtable {
31; COMMON-LABEL: caller_to8_from0:
32
33; Key point is that the "42" should go #16 below incoming stack
34; pointer (we didn't have arg space to reuse).
35  tail call tailcc void @callee_stack8([8 x i64] undef, i64 42)
36  ret void
37
38; COMMON: str {{x[0-9]+}}, [x29, #16]
39; COMMON: ldp x29, x30, [sp], #16
40  ; If there is a sub here then the 42 will be briefly exposed to corruption
41  ; from an interrupt if the kernel does not honour a red-zone, and a larger
42  ; call could well overflow the red zone even if it is present.
43; COMMON-NOT: sub sp,
44; COMMON-NEXT: .cfi_def_cfa_offset 16
45; COMMON-NEXT: .cfi_restore w30
46; COMMON-NEXT: .cfi_restore w29
47; COMMON-NEXT: b callee_stack8
48}
49
50define tailcc void @caller_to8_from8([8 x i64], i64 %a) #0 {
51; COMMON-LABEL: caller_to8_from8:
52; COMMON-NOT: sub sp,
53
54; Key point is that the "%a" should go where at SP on entry.
55  tail call tailcc void @callee_stack8([8 x i64] undef, i64 42)
56  ret void
57
58; COMMON: str {{x[0-9]+}}, [sp]
59; COMMON-NEXT: b callee_stack8
60}
61
62define tailcc void @caller_to16_from8([8 x i64], i64 %a) #0 {
63; COMMON-LABEL: caller_to16_from8:
64; COMMON-NOT: sub sp,
65
66; Important point is that the call reuses the "dead" argument space
67; above %a on the stack. If it tries to go below incoming-SP then the
68; callee will not deallocate the space, even in tailcc.
69  tail call tailcc void @callee_stack16([8 x i64] undef, i64 42, i64 2)
70
71; COMMON: stp {{x[0-9]+}}, {{x[0-9]+}}, [sp]
72; COMMON-NEXT: b callee_stack16
73  ret void
74}
75
76
77define tailcc void @caller_to8_from24([8 x i64], i64 %a, i64 %b, i64 %c) #0 {
78; COMMON-LABEL: caller_to8_from24:
79; COMMON-NOT: sub sp,
80
81; Key point is that the "%a" should go where at #16 above SP on entry.
82  tail call tailcc void @callee_stack8([8 x i64] undef, i64 42)
83  ret void
84
85; COMMON: str {{x[0-9]+}}, [sp, #16]!
86; COMMON-NEXT: .cfi_def_cfa_offset -16
87; COMMON-NEXT: b callee_stack8
88}
89
90
91define tailcc void @caller_to16_from16([8 x i64], i64 %a, i64 %b) #0 {
92; COMMON-LABEL: caller_to16_from16:
93; COMMON-NOT: sub sp,
94
95; Here we want to make sure that both loads happen before the stores:
96; otherwise either %a or %b will be wrongly clobbered.
97  tail call tailcc void @callee_stack16([8 x i64] undef, i64 %b, i64 %a)
98  ret void
99
100; COMMON: ldp {{x[0-9]+}}, {{x[0-9]+}}, [sp]
101; COMMON: stp {{x[0-9]+}}, {{x[0-9]+}}, [sp]
102; COMMON-NEXT: b callee_stack16
103}
104
105define tailcc void @disable_tail_calls() nounwind "disable-tail-calls"="true" {
106; COMMON-LABEL: disable_tail_calls:
107; COMMON-NEXT: // %bb.
108
109  tail call tailcc void @callee_stack0()
110  ret void
111
112; COMMON: bl callee_stack0
113; COMMON: ret
114}
115
116; Weakly-referenced extern functions cannot be tail-called, as AAELF does
117; not define the behaviour of branch instructions to undefined weak symbols.
118define tailcc void @caller_weak() #0 {
119; COMMON-LABEL: caller_weak:
120; COMMON: bl callee_weak
121  tail call void @callee_weak()
122  ret void
123}
124
125declare { [2 x float] } @get_vec2()
126
127define { [3 x float] } @test_add_elem() #0 {
128; SDAG-LABEL: test_add_elem:
129; SDAG: bl get_vec2
130; SDAG: fmov s2, #1.0
131; SDAG: ret
132; GISEL-LABEL: test_add_elem:
133; GISEL: str	x30, [sp, #-16]!
134; GISEL: bl get_vec2
135; GISEL: fmov	s2, #1.0
136; GISEL: ldr	x30, [sp], #16
137; GISEL: ret
138
139  %call = tail call { [2 x float] } @get_vec2()
140  %arr = extractvalue { [2 x float] } %call, 0
141  %arr.0 = extractvalue [2 x float] %arr, 0
142  %arr.1 = extractvalue [2 x float] %arr, 1
143
144  %res.0 = insertvalue { [3 x float] } undef, float %arr.0, 0, 0
145  %res.01 = insertvalue { [3 x float] } %res.0, float %arr.1, 0, 1
146  %res.012 = insertvalue { [3 x float] } %res.01, float 1.000000e+00, 0, 2
147  ret { [3 x float] } %res.012
148}
149
150declare double @get_double()
151define { double, [2 x double] } @test_mismatched_insert() #0 {
152; COMMON-LABEL: test_mismatched_insert:
153; COMMON: bl get_double
154; COMMON: bl get_double
155; COMMON: bl get_double
156; COMMON: ret
157
158  %val0 = call double @get_double()
159  %val1 = call double @get_double()
160  %val2 = tail call double @get_double()
161
162  %res.0 = insertvalue { double, [2 x double] } undef, double %val0, 0
163  %res.01 = insertvalue { double, [2 x double] } %res.0, double %val1, 1, 0
164  %res.012 = insertvalue { double, [2 x double] } %res.01, double %val2, 1, 1
165
166  ret { double, [2 x double] } %res.012
167}
168
169define void @fromC_totail() #0 {
170; COMMON-LABEL: fromC_totail:
171; COMMON: sub sp, sp, #32
172
173; COMMON-NOT: sub sp,
174; COMMON: mov w[[TMP:[0-9]+]], #42
175; COMMON: str x[[TMP]], [sp]
176; COMMON: bl callee_stack8
177  ; We must reset the stack to where it was before the call by undoing its extra stack pop.
178; COMMON: str x[[TMP]], [sp, #-16]!
179; COMMON: bl callee_stack8
180; COMMON: sub sp, sp, #16
181
182  call tailcc void @callee_stack8([8 x i64] undef, i64 42)
183  call tailcc void @callee_stack8([8 x i64] undef, i64 42)
184  ret void
185}
186
187define void @fromC_totail_noreservedframe(i32 %len) #0 {
188; COMMON-LABEL: fromC_totail_noreservedframe:
189; COMMON: stp x29, x30, [sp, #-32]!
190
191; COMMON: mov w[[TMP:[0-9]+]], #42
192  ; Note stack is subtracted here to allocate space for arg
193; COMMON: str x[[TMP]], [sp, #-16]!
194; COMMON: bl callee_stack8
195  ; And here.
196; COMMON: str x[[TMP]], [sp, #-16]!
197; COMMON: bl callee_stack8
198  ; But not restored here because callee_stack8 did that for us.
199; COMMON-NOT: sub sp,
200
201  ; Variable sized allocation prevents reserving frame at start of function so each call must allocate any stack space it needs.
202  %var = alloca i32, i32 %len
203
204  call tailcc void @callee_stack8([8 x i64] undef, i64 42)
205  call tailcc void @callee_stack8([8 x i64] undef, i64 42)
206  ret void
207}
208
209declare void @Ccallee_stack8([8 x i64], i64)
210
211define tailcc void @fromtail_toC() #0 {
212; COMMON-LABEL: fromtail_toC:
213; COMMON: sub sp, sp, #32
214
215; COMMON-NOT: sub sp,
216; COMMON: mov w[[TMP:[0-9]+]], #42
217; COMMON: str x[[TMP]], [sp]
218; COMMON: bl Ccallee_stack8
219  ; C callees will return with the stack exactly where we left it, so we mustn't try to fix anything.
220; COMMON-NOT: add sp,
221; COMMON-NOT: sub sp,
222; COMMON: str x[[TMP]], [sp]{{$}}
223; COMMON: bl Ccallee_stack8
224; COMMON-NOT: sub sp,
225
226
227  call void @Ccallee_stack8([8 x i64] undef, i64 42)
228  call void @Ccallee_stack8([8 x i64] undef, i64 42)
229  ret void
230}
231
232attributes #0 = { uwtable }