138 lines · plain
1; RUN: llc -mtriple armv7 -target-abi apcs -O0 -o - < %s \2; RUN: | FileCheck %s -check-prefix CHECK-TAIL -check-prefix CHECK3; RUN: llc -mtriple armv7 -target-abi apcs -O0 -disable-tail-calls -o - < %s \4; RUN: | FileCheck %s -check-prefix CHECK-NO-TAIL -check-prefix CHECK5; RUN: llc -mtriple armv7 -target-abi aapcs -O0 -o - < %s \6; RUN: | FileCheck %s -check-prefix CHECK-TAIL-AAPCS -check-prefix CHECK7 8declare i32 @callee(i32 %i)9declare extern_weak fastcc void @callee_weak()10 11define i32 @caller(i32 %i) {12entry:13 %r = tail call i32 @callee(i32 %i)14 ret i32 %r15}16 17; CHECK-TAIL-LABEL: caller18; CHECK-TAIL: b callee19 20; CHECK-NO-TAIL-LABEL: caller21; CHECK-NO-TAIL: push {lr}22; CHECK-NO-TAIL: bl callee23; CHECK-NO-TAIL: pop {lr}24; CHECK-NO-TAIL: bx lr25 26 27; Weakly-referenced extern functions cannot be tail-called, as AAELF does28; not define the behaviour of branch instructions to undefined weak symbols.29define fastcc void @caller_weak() {30; CHECK-LABEL: caller_weak:31; CHECK: bl callee_weak32 tail call void @callee_weak()33 ret void34}35 36; A tail call can be optimized if all the arguments can be passed in registers37; R0-R3, or the remaining arguments are already in the caller's parameter area38; in the stack. Variadic functions are no different.39declare i32 @variadic(i32, ...)40 41; e.g. four integers42define void @v_caller_ints1(i32 %a, i32 %b) {43; CHECK-LABEL: v_caller_ints1:44; CHECK-TAIL: b variadic45; CHECK-TAIL-AAPCS: b variadic46; CHECK-NO-TAIL: bl variadic47entry:48 %call = tail call i32 (i32, ...) @variadic(i32 %a, i32 %b, i32 %b, i32 %a)49 ret void50}51 52; e.g. two 32-bit integers, one 64-bit integer (needs to span two regs)53define void @v_caller_ints2(i32 %y, i64 %z) {54; CHECK-LABEL: v_caller_ints2:55; CHECK-TAIL: b variadic56; CHECK-TAIL-AAPCS: b variadic57; CHECK-NO-TAIL: bl variadic58entry:59 %call = tail call i32 (i32, ...) @variadic(i32 %y, i32 %y, i64 %z)60 ret void61}62 63; e.g. two 32-bit integers, one 64-bit integer (needs to span two regs). Notice64; that %z is passed in r1-r2 if APCS is used, contrary to AAPCS where r2-r365; would be used (since double-word types must start at an even register). In the66; latter case, the third argument needs to be passed through the stack.67define void @v_caller_ints3(i32 %y, i64 %z) {68; CHECK-LABEL: v_caller_ints3:69; CHECK-TAIL: b variadic70; CHECK-TAIL-AAPCS: bl variadic71; CHECK-NO-TAIL: bl variadic72entry:73 %call = tail call i32 (i32, ...) @variadic(i32 %y, i64 %z, i32 %y)74 ret void75}76 77; e.g. two 32-bit integers, one 64-bit integer and another 64-bit integer that78; doesn't fit in r0-r3 but comes from the caller argument list and is in the79; same position.80define void @v_caller_ints4(i64 %a, i32 %b, i32 %c, i64 %d) {81; CHECK-LABEL: v_caller_ints4:82; CHECK-TAIL: b variadic83; CHECK-TAIL-AAPCS: b variadic84; CHECK-NO-TAIL: bl variadic85entry:86 %call = tail call i32 (i32, ...) @variadic(i32 %b, i32 %c, i64 %a, i64 %d)87 ret void88}89 90; If the arguments do not fit in r0-r3 and the existing parameters cannot be91; taken from the caller's parameter region, the optimization is not supported.92 93; e.g. one 32-bit integer, two 64-bit integers94define void @v_caller_ints_fail(i32 %y, i64 %z) {95; CHECK-LABEL: v_caller_ints_fail:96; CHECK: bl variadic97entry:98 %call = tail call i32 (i32, ...) @variadic(i32 %y, i64 %z, i64 %z)99 ret void100}101 102; Check that nonnull attributes don't inhibit tailcalls.103 104declare nonnull ptr @nonnull_callee(ptr %p, i32 %val)105define ptr @nonnull_caller(ptr %p, i32 %val) {106; CHECK-LABEL: nonnull_caller:107; CHECK-TAIL: b nonnull_callee108; CHECK-NO-TAIL: bl nonnull_callee109entry:110 %call = tail call ptr @nonnull_callee(ptr %p, i32 %val)111 ret ptr %call112}113 114; Check that noalias attributes don't inhibit tailcalls.115 116declare noalias ptr @noalias_callee(ptr %p, i32 %val)117define ptr @noalias_caller(ptr %p, i32 %val) {118; CHECK-LABEL: noalias_caller:119; CHECK-TAIL: b noalias_callee120; CHECK-NO-TAIL: bl noalias_callee121entry:122 %call = tail call ptr @noalias_callee(ptr %p, i32 %val)123 ret ptr %call124}125 126 127; Check that alignment attributes don't inhibit tailcalls.128 129declare align 8 ptr @align8_callee(ptr %p, i32 %val)130define ptr @align8_caller(ptr %p, i32 %val) {131; CHECK-LABEL: align8_caller:132; CHECK-TAIL: b align8_callee133; CHECK-NO-TAIL: bl align8_callee134entry:135 %call = tail call ptr @align8_callee(ptr %p, i32 %val)136 ret ptr %call137}138