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

    <tr>
        <th>Summary</th>
        <td>
            [missed optimization] repeat load operation when returning a struct
        </td>
    </tr>

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

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

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

<pre>
    I have found a test case (reduced from https://github.com/llvm/llvm-project/issues/83812) which showes a missed optimization of clang-17.0.6 -O2
```
struct S0 {
   int  f0;
 int  f1;
   long long  f2;
};

int copy = 0;
struct S0 global_s = {1, 1, 1};
struct S0 value = {2, 2, 2};
int other = 1;

struct S0 get_struct() { return value; }

void func_bad(struct S0 d) {
  struct S0 local_s;
  global_s = local_s = get_struct();
  copy = local_s.f1;
  if (copy)
 --other;
}
```
clang-17~15 fail to reuse the value loaded from `global_s` while clang-14 could. here's the comparison of generated binaries:
clang-17: ( https://godbolt.org/z/Gx98vzoYP )
```
0000000000001140 <func_bad>:
    1140:       movaps 0x2ef9(%rip),%xmm0        # 4040 <value>
    1147:       mov 0x2ef2(%rip),%rax        # 4040 <value>
func_bad():
    114e:       shr $0x20,%rax
    1152:       movaps %xmm0,0x2ed7(%rip)        # 4030 <global_s>
    1159:       mov    %eax,0x2ef9(%rip)        # 4058 <copy>
 115f:  je     1167 <func_bad+0x27>
    1161:       decl   0x2ee9(%rip)        # 4050 <other>
```
clang-14:
```
0000000000401120 <func_bad>:
 401120:        movdqa 0x2f18(%rip),%xmm0        # 404040 <value>
func_bad():
 401128:        movq   %xmm0,%rax
  40112d:       shr    $0x20,%rax
 401131:        movdqa %xmm0,0x2ef7(%rip)        # 404030 <global_s>
 401139:        mov    %eax,0x2f19(%rip)        # 404058 <copy>
  40113f:       je 401148 <func_bad+0x28>
  401141:       addl   $0xffffffff,0x2f08(%rip)        # 404050 <other>
```

we can see that in clang-17's output the load operation at 0x1147 could be removed, and it's better to reuse the value in %xmm0 just like clang-14's output dose at 0x401128.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVs1u4zYQfhr6MohBUpIlH3yIk3XRUwv01NOCEkcWU1r0kpTjzaHPXpCS9eNNsi0qBIxFznzz92k4wjl1bBF3JNuT7HklOt8Yu3ssndFoV6WR33e_QiMuCLXpWgkCPDoPlXAIhBcWZVehhNqaEzTenx1JHgk_EH44Kt905boyJ8IPWl9u_x7O1rxg5Qk_KOc6dIQfiqRgnPAtvDaqasA15hUdCDgp51CCOXt1Um_CK9OCqaHSoj0-sHxN1xt4-I0T-kzoI9nQ4S--Om-7ysMfFEi-77cAQLUeoKYkuW31G2zaANCmPfYL1Hw8IPnz9DuuQbUy5-9AkmeYICfDR21Kob-6KEDyPSP8CYZlhjYpXITu8CbNg-CwzKSDVeMbtFGO3fk0M47-a_9GeBFyS_I9WPSdbXs7JNlDAJ5pX4ySUHdt9bUUkvBiQpMDwi1J04k2VYhxlsBF2MNx_H3v0kxnzOMgv16URNWBbEEmaPWbDw8xC4v6vMeDG1n-ZhnUQmnwBix2DsE3OGRcGyFvLCYbeguAbGhgpMYb41KoTKflGhq0SHjuIkZlTmdhlevJecQWrfAooVStsArjJ7FwhSSPIZ77D8bI0mi_NvZI-OGN8MMv121xeTN__g5j2HfB0dnDWEqBJE9j_ZIvo2UAgHAeN7YncxFnB_TKsd7GUmRWnYMN_kR4dj2dKAwP4QmktAceWPNlAZmPkD0e_xHPiuvP4Wasi8xYOo69FddYIDylV05H6LlcxhcBDrEQ_hRck_nctaVHSfRorPsyxmw7xRg1MhTXAXSZvyVoVgTQyNoRkLGs7tFeEHr4Tb6oGt_TK8_vXNiwXklipQFipvEzyzGc4fv48tlnkY6Z_pBZKWWMf8ys_njMkPwmgns1K_4dsf4LF6KpYjT1rS_GUOIlG6KonFgTRd8lTpBM2ML_JW_qT3jzMXMi7MfEqdkn5XufOj3kxJ7w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