Issue 174621
Summary [[musttaill]] generates extra unnecessary instructions at the end
Labels new issue
Assignees
Reporter brevzin
    Consider this example (courtesy of Matt Godbolt in his Advent of Compiler Optimization series, https://www.youtube.com/watch?v=J1vtP0QDLLU):

```cpp
#ifdef WITH_TAIL
#define MUSTTAIL [[clang::musttail]]
#else
#define MUSTTAIL
#endif

unsigned gcd(unsigned a, unsigned b) {
    if (b == 0) return a;
    MUSTTAIL return gcd(b, a % b);
}
```

Even without the `musttail` attribute, clang x86-64 `-O2` performs tail call optimization here. Except that with `musttail`, it adds two more instructions at the end, that do nothing?

<table>
<tr><th>without [[musttail]]</th><th>with [[musttail]]</th></tr>
<tr>
<td>

```asm
gcd(unsigned int, unsigned int):
        mov     eax, edi
        test    esi, esi
        je .LBB0_4
        mov     edx, esi
.LBB0_2:
        mov     ecx, edx
 xor     edx, edx
        div     ecx
        mov     eax, ecx
 test    edx, edx
        jne     .LBB0_2
        mov     eax, ecx



.LBB0_4:
        ret
```

</td>
<td>

```asm
gcd(unsigned int, unsigned int):
        mov     eax, edi
        test    esi, esi
 je      .LBB0_1
        mov     edx, esi
.LBB0_4:
        mov     ecx, edx
        xor     edx, edx
        div     ecx
        mov     eax, ecx
        test    edx, edx
        jne     .LBB0_4
        mov     eax, ecx
        ret
.LBB0_1:
        mov     ecx, eax
        mov     eax, ecx
        ret
```

</td>
</tr>
</table>

You can see the extra instructions generated with `[[musttaill]]`, even though there is already a tail call. [On compiler explorer](https://compiler-explorer.com/z/KMvsGbdPe). This is only true on `-O2` and `-O3`, on `-O1` they generate identical code. 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to