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

    <tr>
        <th>Summary</th>
        <td>
            [clang] reference gets unnecessarily loaded twice
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          M-Kusumgar
      </td>
    </tr>
</table>

<pre>
    test assembly: https://godbolt.org/z/TWdvPPGYs

```c++
#include <stdint.h>

struct foo {
    const uint64_t& x;
    void f();

 uint64_t g() {
        uint64_t y = x;
        f();
        return y * x;
    }
};

uint64_t h(foo* x) {
    return x->g();
}
```

the reference to x gets loaded twice vs

expected assembly: https://godbolt.org/z/rW39W6h1e

```c++
#include <stdint.h>

struct foo {
    const uint64_t& x;
    void f();

    uint64_t g() {
        auto& tmp = x;
        uint64_t y = tmp;
        f();
        return y * tmp;
 }
};

uint64_t h(foo* x) {
    return x->g();
}
```
the reference to x gets loaded once. shouldn't the compiler be able to optimise the first code block into the assembly produced by the second code block?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVEGL2zwQ_TXyRSQ4Izu2Dz5kv6y_QynsobD0VGxpYquVLSON0qS_vthOdpNloeyh1AwY9Oa9GeZJU3uv2wGxZOkDS_dRHaizrvy8-hR86NvaRY1V55LQE6-9x74xZyZ2vCMaPRM7BhWDqrWqsYbW1rUMql8Mqi_P6vj09P9Xz-LdFNt4CcngYYp4x0DoQZqgkDPxnyelB1p3TDwuDE8uSOIHaznLpnzOOZd28MSDHmibfCMGW35i4goerVb8wCBnUCynE3BN5u2C3KhN3wt85kzsb-Wm707teuiQghsmAuxuCSzbTzWz_UvxF_GOQX6wdiHc9XARO62YeGzvel_UrnNbBKlD7vCADgeJnCw_8RbJc2NrhYrTTy2RHy8zx9OIklB9yDf3LIrnbbfBf-3brTfvW1cHspMU9eN73r1xlvrxo96-Uv62tX_w1Q4S19x3Nhg1MMiITwRp-1EbdLxBXjdm5tmRdK89zgkH7TxxaRXyxlj5g-uB7IxcbwQfnVVBouLNeQY8SjuoGw4TVaRKoQpR1BGWmyyJs0QUaRx1pZDZFhKp6qIW0GAqGtgC5LlKCshkGke6hBjSGCDfbNJiE6_z7FDnElWSZJtCZTlLYuxrbdbGHPvpFkba-4DlBgoQIjJ1g8bPqwlAmnpoGcC0pVw5EVZNaD1LYqM9-VcJ0mTmfbYw0v3NaOephmFAid7XTpvz3duJgjPlmyeiqQvNWtqeQTXVuPxWo7PfURKDau7ZM6gubR9L-B0AAP___7SNRA">