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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization for x86 when optimizing for size
        </td>
    </tr>

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

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

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

<pre>
    https://gcc.godbolt.org/z/sTs3E9EP1
```c++
struct stack
  {
    void** base;
    unsigned int top, cap;
 };

struct context
  {
    void* data;
    stack* st;
 };

int
clear_stack(context& ctx)
  {
    ctx.st->top = 0;
 return 0;
  }
```

This compiles to 14 bytes:
```asm
clear_stack(context&):
 mov    rax,QWORD PTR [rdi+0x8] # 48 8b 47 08
 mov    DWORD PTR [rax+0x8],0x0    # c7 40 08 00 00 00 00
 xor    eax,eax                    # 31 c0
 ret # c3
```

We notice that the EAX register is cleared at the end, which could have been done earlier, so this code would compile to 10 bytes:
```asm
clear_stack_2(context&):
 mov    rcx,QWORD PTR [rdi+0x8]    # 48 8b 4f 08
 xor    eax,eax                    # 31 c0
 mov    DWORD PTR [rcx+0x8],eax    # 89 41 08
 ret # c3
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVMGOozgQ_RpzKXVkyiTAgUPS6dxWOztqafY2MqYSvAs4sotupr9-ZUJPOqtWRruRpYCrXr2n91DpEOxpIKrEeifW-0SP3DpfdW3vxkBJ7ZofVct8DkJtBR4EHk7GrE6uqV3HK-dPAg9vAg_hOain8ulLKuReyK3YyMsxAnfxzLeB_WgYAmvz9-UGQOS790eAF2cbgVuBW6h1IKE-1MZhVtqAHRjYnQU-gtHna4_I9z9fbviMG5gmvscIjWZ9Q3cRiVsIfI_CDstc05H23xdU8U6JGzA8CSw_JTc8rQI_CPXE7gxC7UFeuTzx6IePNzP9rb0fpTy3NoBx_dl2FIAdpBnUP5jm6G5hOvT3VUfF7yjo3UtU6_Uk8PGPb79_3cOX568g1jvfWIE7ORVivQeBCrICihqyHGRxC97fwOKkBSbwUU4ytkS8ySGTIAuQ8udZBk3Oxy6aVZCe4JNfHKFSMPLq4WWsuuPbN4LBsTUE3GoGbgmetn-Cp5MNTB6iq9EmamAp09DEr--1taYF48augVa_ENREAzRuICDtO0s-dgUHfEmmIXidm5eQ5ozkf8noO_46JXM_pcWlJajjNaj_4--n4ZqbcJdJEVmUkKVXwl-EkzSVakpV6oSqNJcqV2mep0lbFRrLjcSjMWuTFlqluK7LfFMqTccUZZPYCiWqFLGQmJUqXeXpsSwwK2S5ro_qWItMUq9tt-q6lz4uscSGMFKVb7ICk07X1IV5ISIO9ApzUSDG_eiriHmox1MQmexs4HCdwpY7qn6zIVAD7sy2t2-arRvg6DxMxQZeWxreK3Y4zffBvlEy-u7fe9ZyO9Yr43qBh0ix_D2cvfuLDAs8zMKCwMMs_J8AAAD__1Uqm60">