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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization due to aliasing of subobjects on transparent replacement
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    ## Sample Code to Reproduce
```cpp
#include <new>

struct A { int x; };

struct B { int x; };

int get_noalias(A &a, B &b) {
 a.x = 1;
    b.x = 5;
    return a.x;
}

int get_alias(A &a, B &b) {
    a.x = 1;
    new (&b.x) int(5);
    return a.x;
}
```
## Output
```asm
get_noalias(A&, B&):
 mov     dword ptr [rdi], 1
        mov     dword ptr [rsi], 5
 mov     eax, 1
        ret
get_alias(A&, B&):
        mov     dword ptr [rdi], 1
        mov     dword ptr [rsi], 5
        mov     eax, dword ptr [rdi]
        ret
```
## Explanation
The codegen for `get_noalias` and `get_alias` should be identical. According to [[basic.life] p8.5](https://eel.is/c++draft/basic.life#8.5), transparent replacement can only take place when the two objects **o1** and **o2** are members of transparently replaceable objects **p1** and **p2**. The subobjects `a.x` and `b.x` are subobjects of `a` and `b`, and these cannot transparently replace each other because they don't have the same type.

After `new (&b.x) int(5);`, `a.x` should be unchanged, and the compiler should be able to emit `mov eax, 1` as in the first function.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVUuv6jYQ_jVmM7pRYucBiyzgcNhVldruKz8mxK1jR7ZzgP76yuF5KLfnShchTL58M_PNK-Eh6L1FbEm1IdV2wafYO9--64D2wD9wIZw6tYQyQhn8zofRILw5hRAd_Iajd2qSSPItydekzs9fOY4XhDJtpZkUAmFvFg-EvV_uzL8h-klGWANpNqBthCNhGyDNlrDNC97mK166tcf4p3XcaB4IXa6B0JoT-paMaS0IXSUnZzrw7AiE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