[llvm-bugs] [Bug 43007] New: Missed Tail Call Optimization when specifying function return value with __builtin_unreachable or __builtin_assume

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Aug 15 09:56:23 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=43007

            Bug ID: 43007
           Summary: Missed Tail Call Optimization when specifying function
                    return value with __builtin_unreachable or
                    __builtin_assume
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: mike.k at digitalcarbide.com
                CC: llvm-bugs at lists.llvm.org

When explicitly specifying that a function can only return a specific value
using either __builtin_unreachable or __builtin_assume, the optimizer fails to
perform tail call optimization.

This is observed for all architectures.

Example:

extern int function_returns_only_1_or_doesnt_return(int, int);

int foo1(int a, int b) {
    const int result = function_returns_only_1_or_doesnt_return(a, b);
    if (result == 1) {
        return result;
    }
    else {
        __builtin_unreachable();
    }
}

int foo2(int a, int b) {
    const int result = function_returns_only_1_or_doesnt_return(a, b);
    __builtin_assume(result == 1);
    return result;
}

int foo3(int a, int b) {
    return function_returns_only_1_or_doesnt_return(a, b);
}

For the flags '-O3' for an x86-64 target, this emits the following assembly:

foo1(int, int): # @foo1(int, int)
  push rax
  call function_returns_only_1_or_doesnt_return(int, int)
  mov eax, 1
  pop rcx
  ret
foo2(int, int): # @foo2(int, int)
  push rax
  call function_returns_only_1_or_doesnt_return(int, int)
  mov eax, 1
  pop rcx
  ret
foo3(int, int): # @foo3(int, int)
  jmp function_returns_only_1_or_doesnt_return(int, int) # TAILCALL

It would be expected that all three would perform tail call optimization.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190815/881dba29/attachment.html>


More information about the llvm-bugs mailing list