<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/75455>75455</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: not tail calling memset
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:optimizations,
            missed-optimization
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dvyukov
      </td>
    </tr>
</table>

<pre>
    The code is (extracted from calloc implementation):
```
void* foo(void* x, long s) {
    if (__builtin_expect(x != nullptr, true))
      return __builtin_memset(x, 0, s);
    return x;
}
```

Clang generages:
```
foo(void*, long):                              # @foo(void*, long)
        pushq   %rbx
        movq    %rdi, %rbx
 testq   %rdi, %rdi
        je      .LBB0_2
        movq    %rsi, %rdx
        movq    %rbx, %rdi
        xorl    %esi, %esi
 callq   memset@PLT
.LBB0_2:
        movq    %rbx, %rax
        popq %rbx
        retq
```
https://godbolt.org/z/YojTG44bs

This should be:
```
foo(void*, long):                              # @foo(void*, long)
        testq   %rdi, %rdi
        je .LBB0_2
        movq    %rsi, %rdx
        xorl    %esi, %esi
 jmp     memset@PLT
.LBB0_2:
        retq
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVE2PokAQ_TXFpTIGClA5cNAx7mU22YOXPRk-Smi3obG7Mcz8-g2go87q7CR7WGNo26p6r16_ohNjRFEzxxAuIVw5SWtLpeP8-Nr-UkcnVflrvCkZM5UzCoNAc-6sTjLLOe60qjBLpFQZiqqRXHFtEytUDRSBvwB3Be4Cpu7pO2yPSuRAC9wpBTQ_7zqgZ5SqLtAARQiz5ZiNiCh2Pe12m7ZCWlFvuWs4s0DzDoE88FdYt1I2VvcYVrfcs1N0AUDUbFtd4wWj4srwgNEXuf3DDE1f8Z6Kuvc_Yba6K2l8PsukLrDgmnVSsHmk_0b3WfV4XvjpB8hHCNyH9VdyEZvWlIehKNRpdxur1PGAp1gueoibLMvGHv4I5-IWZM_jOnlZLt0tPWYwF4hP-ki7R0Sd0vKUxe9Y_a8xqx-_HuVkaOD-eNmMoXNnZx8-p00-NNeo5nD39DTbw11jS2ubwXVaA60LladK2onSBdD6DWj9U-0334IgNddDsymFQVOqVuaY8v-dmS8b_w-e_8XNfdWMYF92844dTh77eeRHicOxN3MpDObeNHDKOAloF3Ey9zibzucc7vwoS3PahWE6y1Lfd0RMLvkeeYHnuhQEk8DzUy_0ppE3zcJd6kLgcpUIOZHyWPXeOsKYluNZGIShI5OUpRmuUqI-A_yFaqyoxNtwKxogAnoGokoYw_nTdbCPhStHx33hU9oWBgJXCmPNhcwKKzn-PhTjTbG_wFpZtImQwysh6uJ0iE6rZfxhNoUt23SSqQpoPfQ5Lk-NVvvhal0PsgzQelD2OwAA___uNKys">