<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Performance bug: Clang or LLVM failure to eliminate tail recursion in Linux"
   href="https://bugs.llvm.org/show_bug.cgi?id=34662">34662</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Performance bug: Clang or LLVM failure to eliminate tail recursion in Linux
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>5.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>thecppzoo@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Hello.  For this code:

__int128 factorial(unsigned arg, __int128 soFar) {
    if(arg <= 1) { return soFar; }
    return factorial(arg - 1, arg * soFar);
}

Clang 5.0.0 and others fails to eliminate the tail recursion, as can be seen at
the compiler explorer:
<a href="https://godbolt.org/g/iqLwgJ">https://godbolt.org/g/iqLwgJ</a>
(optimization level -O3)

factorial(unsigned int, __int128):                         #
@factorial(unsigned int, __int128)
        mov     rcx, rdx
        mov     rax, rsi
        cmp     edi, 2
        jb      .LBB0_2
        push    rax
        mov     edx, edi
        dec     edi
        imul    rcx, rdx
        mul     rdx
        add     rdx, rcx
        mov     rsi, rax
        call    factorial(unsigned int, __int128)
        mov     rcx, rdx
        add     rsp, 8
.LBB0_2:
        mov     rdx, rcx
        ret

Curiously, -target aarch64-linux-gnu also with optimization -O3 produces
non-tail-recursive code.

GCC eliminates the tail recursion</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>