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

    <tr>
        <th>Summary</th>
        <td>
            Bad optimization - results change at -O1
        </td>
    </tr>

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

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

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

<pre>
    Demonstration: https://godbolt.org/z/4c35Tncq8

LLVM IR that seems to miscompile:
```
define void @__sample__main(ptr %input_ptrs, ptr %output_ptrs) {
entry:
  %0 = alloca i8, align 1
  %1 = load ptr, ptr %input_ptrs, align 8
  %x.i.i = load i8, ptr %1, align 1
  %2 = sdiv i8 %x.i.i, -1
  store i8 %2, ptr %0, align 1
  %3 = load ptr, ptr %output_ptrs, align 8
  %y.i.i = load i8, ptr %0, align 1
  %4 = sext i8 %y.i.i to i16
  %5 = and i16 %4, 511
  store i16 %5, ptr %3, align 2
  ret void
}
```

Incorrectly optimized IR:
```
define void @__sample__main(ptr nocapture readonly %input_ptrs, ptr nocapture readonly %output_ptrs) local_unnamed_addr #0 {
entry:
  %0 = load ptr, ptr %input_ptrs, align 8
  %x.i.i = load i8, ptr %0, align 1
  %1 = sub nsw i8 0, %x.i.i
  %2 = load ptr, ptr %output_ptrs, align 8
  %3 = sext i8 %1 to i16
  %4 = and i16 %3, 511
  store i16 %4, ptr %2, align 2
 ret void
}
```

Not quite clear on what the issue is, but the output definitely changes.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslV-L2zgUxT-N_HJJsCX534MfZjYEBrq7UJZ9DYp1J9EiS650PW366RfZmUmaZsr-KQQFy-eec2X9LKsYzcEhdqx8ZOUmUxMdfegwmF5F8iHbe33qNjh4FykoMt4x8QBHojEy8cD4lvHtweu9t7T24cD49ivjW9mL8g_Xf2pYvmH5wzJ--PDnr_D0EeioCCLiEIE8DCb2fhiNxeS3yKv8_JsvNT4bh_DijQYm890uqmG0uNsNyjjGm5ECMF4aN060GylExn-B86Sf6DLbAqsfF090FE5vgZCkOTCxAWWt7xWYJpkoaw4OiitRMYusVzolXAV9m74UNleFX9ZmbS7Fi_-5trifxWd51OYFTPPmkbSrN1naIzzf5leW-X1L8V773zyne_2fftD_O2Fy6R-_0LnBxYM8mKK60pXLg3c6zc-Fya8sbhe53CyvgsUlmL-KA9KMyhmlenOXqWV8cr0PAXuyJ_AjmcF8RQ1PH_8Pic73aqQpIARU2jt7ug_nfd0Nr4lFu5ucUwPqndI6LVvk_4Djn4zoO1u8vA5x2oOLn9Muz7o3VG9Z_i_giVuKiu8JkrcEiR8RJK_i-fcE_RuAfvMEnyZDCL1FFcA7-JxONzoimBinNKaE_bTMLauFmSNDaE_QH5U7YFxnuhO6Fa3KsCvqXFRF20qeHTulZCH2pejFc5PXDdZN38rnupWi0RVXeWY6nnNRcF4VrSwlX-eFbitZ53Ulq7ZukckcB2Xs2tqXIR3S2dxaV1cyl5lVe7Tx9fwPXRKt9tMhMplbEyleysiQxe5R6deXZf4ewAoCxslSPC8GFMHq9yKbgu1uPhSGjtN-3fuB8W2yPf-txuD_wp4Y386tRca3c3d_BwAA__94fOtD">