1 ; RUN: llc -mtriple armv7 -target-abi apcs -O0 -o - < %s \
2 ; RUN: | FileCheck %s -check-prefix CHECK-TAIL -check-prefix CHECK
3 ; RUN: llc -mtriple armv7 -target-abi apcs -O0 -disable-tail-calls -o - < %s \
4 ; RUN: | FileCheck %s -check-prefix CHECK-NO-TAIL -check-prefix CHECK
5 ; RUN: llc -mtriple armv7 -target-abi aapcs -O0 -o - < %s \
6 ; RUN: | FileCheck %s -check-prefix CHECK-TAIL-AAPCS -check-prefix CHECK
8 declare i32 @callee(i32 %i)
9 declare extern_weak fastcc void @callee_weak()
11 define i32 @caller(i32 %i) {
13 %r = tail call i32 @callee(i32 %i)
17 ; CHECK-TAIL-LABEL: caller
18 ; CHECK-TAIL: b callee
20 ; CHECK-NO-TAIL-LABEL: caller
21 ; CHECK-NO-TAIL: push {lr}
22 ; CHECK-NO-TAIL: bl callee
23 ; CHECK-NO-TAIL: pop {lr}
24 ; CHECK-NO-TAIL: bx lr
27 ; Weakly-referenced extern functions cannot be tail-called, as AAELF does
28 ; not define the behaviour of branch instructions to undefined weak symbols.
29 define fastcc void @caller_weak() {
30 ; CHECK-LABEL: caller_weak:
31 ; CHECK: bl callee_weak
32 tail call void @callee_weak()
36 ; A tail call can be optimized if all the arguments can be passed in registers
37 ; R0-R3, or the remaining arguments are already in the caller's parameter area
38 ; in the stack. Variadic functions are no different.
39 declare i32 @variadic(i32, ...)
42 define void @v_caller_ints1(i32 %a, i32 %b) {
43 ; CHECK-LABEL: v_caller_ints1:
44 ; CHECK-TAIL: b variadic
45 ; CHECK-TAIL-AAPCS: b variadic
46 ; CHECK-NO-TAIL: bl variadic
48 %call = tail call i32 (i32, ...) @variadic(i32 %a, i32 %b, i32 %b, i32 %a)
52 ; e.g. two 32-bit integers, one 64-bit integer (needs to span two regs)
53 define void @v_caller_ints2(i32 %y, i64 %z) {
54 ; CHECK-LABEL: v_caller_ints2:
55 ; CHECK-TAIL: b variadic
56 ; CHECK-TAIL-AAPCS: b variadic
57 ; CHECK-NO-TAIL: bl variadic
59 %call = tail call i32 (i32, ...) @variadic(i32 %y, i32 %y, i64 %z)
63 ; e.g. two 32-bit integers, one 64-bit integer (needs to span two regs). Notice
64 ; that %z is passed in r1-r2 if APCS is used, contrary to AAPCS where r2-r3
65 ; would be used (since double-word types must start at an even register). In the
66 ; latter case, the third argument needs to be passed through the stack.
67 define void @v_caller_ints3(i32 %y, i64 %z) {
68 ; CHECK-LABEL: v_caller_ints3:
69 ; CHECK-TAIL: b variadic
70 ; CHECK-TAIL-AAPCS: bl variadic
71 ; CHECK-NO-TAIL: bl variadic
73 %call = tail call i32 (i32, ...) @variadic(i32 %y, i64 %z, i32 %y)
77 ; e.g. two 32-bit integers, one 64-bit integer and another 64-bit integer that
78 ; doesn't fit in r0-r3 but comes from the caller argument list and is in the
80 define void @v_caller_ints4(i64 %a, i32 %b, i32 %c, i64 %d) {
81 ; CHECK-LABEL: v_caller_ints4:
82 ; CHECK-TAIL: b variadic
83 ; CHECK-TAIL-AAPCS: b variadic
84 ; CHECK-NO-TAIL: bl variadic
86 %call = tail call i32 (i32, ...) @variadic(i32 %b, i32 %c, i64 %a, i64 %d)
90 ; If the arguments do not fit in r0-r3 and the existing parameters cannot be
91 ; taken from the caller's parameter region, the optimization is not supported.
93 ; e.g. one 32-bit integer, two 64-bit integers
94 define void @v_caller_ints_fail(i32 %y, i64 %z) {
95 ; CHECK-LABEL: v_caller_ints_fail:
98 %call = tail call i32 (i32, ...) @variadic(i32 %y, i64 %z, i64 %z)
102 ; Check that nonnull attributes don't inhibit tailcalls.
104 declare nonnull ptr @nonnull_callee(ptr %p, i32 %val)
105 define ptr @nonnull_caller(ptr %p, i32 %val) {
106 ; CHECK-LABEL: nonnull_caller:
107 ; CHECK-TAIL: b nonnull_callee
108 ; CHECK-NO-TAIL: bl nonnull_callee
110 %call = tail call ptr @nonnull_callee(ptr %p, i32 %val)
114 ; Check that noalias attributes don't inhibit tailcalls.
116 declare noalias ptr @noalias_callee(ptr %p, i32 %val)
117 define ptr @noalias_caller(ptr %p, i32 %val) {
118 ; CHECK-LABEL: noalias_caller:
119 ; CHECK-TAIL: b noalias_callee
120 ; CHECK-NO-TAIL: bl noalias_callee
122 %call = tail call ptr @noalias_callee(ptr %p, i32 %val)
127 ; Check that alignment attributes don't inhibit tailcalls.
129 declare align 8 ptr @align8_callee(ptr %p, i32 %val)
130 define ptr @align8_caller(ptr %p, i32 %val) {
131 ; CHECK-LABEL: align8_caller:
132 ; CHECK-TAIL: b align8_callee
133 ; CHECK-NO-TAIL: bl align8_callee
135 %call = tail call ptr @align8_callee(ptr %p, i32 %val)