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

    <tr>
        <th>Summary</th>
        <td>
            clang not generating aliasing check to switch to alias-free implementation
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    This seems like a missed optimization to me so I thought I report it.

Full source/assembly at: https://gcc.godbolt.org/z/nMWozxze4

Looking at something like the classic aliasing example:

```
void foo(int* ptr, const int& mult)
{
    for (int i = 0; i < 10; ++i)
    {
        ptr[i] += mult;
    }
}
```

then clang will straight away generate code to reload `mult` every time as it might alias.

gcc does somehting interesting, it generates the following prefix:

```
        lea     rdx, [rsi+4]
        mov     rax, rdi
        cmp     rdi, rdx
        jnb     .L5
        lea     rdx, [rdi+40]
        cmp     rsi, rdx
        jb      .L2
```

which basically checks whether `mult` does actually alias `ptr`. If not it jumps to the aliasing-free implementation. Otherwise it jumps to what clang does and reloads every time.

I guess if the loop count is smaller this might not be worth it but the behaviour doesn't change even for something like a loop count of `40`.

Is my analsis right and might it be worth adding the same optimization as gcc?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyFVU2P2zgM_TXOhajhr3wdfJg0GGCAFnvYAnuWLdrWjGwZkhxn5teXlJNu3O22gRPLJsVHvUcylZHv5bdOOXCIvQOt3hAE9Mo5lGBGr3r1IbwyA3gDPYIz8AK-M1PbeVpZHI31oHwcJecoeVp-nyetyXOyNUbZs6BYfaXfQfgof4LO-9HRgix0tXUdt0ZWRvvY2JbefNB3-PqP-bh-YPEY9Ysxb2poKQzF7tF3_BAS9h1CrQlH1SC0Eo4teBX9qJGRHoJEu-R2hceLURIaY6LsoAYfZU8wehtln6E2g6Nz8bsd9JOm-_EWYX9aFkCfxlhY9oKC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