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

    <tr>
        <th>Summary</th>
        <td>
            Missed peephole opt: `sub nuw 15, x` => `xor x, 15`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:instcombine,
            missed-optimization
      </td>
    </tr>

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

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

<pre>
    This transformation is beneficial because materializing the `15` argument to `sub` requires an extra instruction, but the `15` argument to `xor` can fit directly into the `xor` instruction

Rust example (https://godbolt.org/z/h7aTc5sca):
```rust
#![feature(unchecked_math)]

#[no_mangle]
pub fn src(x: u8) -> u8 {
    unsafe { 15_u8.unchecked_sub(x) }
}

#[no_mangle]
pub fn tgt(x: u8) -> u8 {
    x ^ 15
}
```

alive proof: https://alive2.llvm.org/ce/z/YKCPCA
```llvm
define noundef i8 @src(i8 noundef %x) unnamed_addr #0 {
  %_0 = sub nuw i8 15, %x
  ret i8 %_0
}

define noundef i8 @tgt(i8 noundef %x) unnamed_addr #0 {
 %_0 = xor i8 %x, 15
  ret i8 %_0
}

```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVE2P2zYQ_TWjy2ANaSRa8kGH3XV8CQoURS49GRQ1sthIpMqP1NlfX1Cyd7eLoEgAwwaGM_Me36Of9F5fDHML4gnEMZMxjNa1n2eWX7XJOtt_b7-M2mNw0vjBulkGbQ1qjx0bHrTScsKOlYyecZaBnZaTftHmgmFkhH1eCNjnKN0lzmwCBpuKPnap6vjvqB17lAb5GpxEbXxwUSUQoGfsYvjfPVfrUlVJg4MO2GvHKkzfUZtg74O3nveb8yPkj9v3H9EH5Kucl4kRqBlDWDyUj0AnoNPF9p2dws66C9DpBeg01vKLEl5JoENq21bt8-3jog-3EpVABYingWWIjoGaaNTI6iv351mGMc2L43sqaUQ8GXuepblM_Hq6xA4Hg94poOYK5SPGBuiAD1B-wtgg1E9bIyJiNF4OnGpYiHNsdm-oSfS0gA4I9R25_hUK4RJ-gsIVQXzCQnxEuIv0HlBO-hvj4qwd0tb_qr8e0m6avs03BxTfbPjz8_Pvzx_VT41bqedBG0Zjo-l5QN0gVPkmoG5ey0BiVSMaI2fuz7LvHQKV-fv7AIlzjlAe0ccOTfwnbStEep3r_K3NcVhhUvcPpf0hpU3QX6T0xuhq3Q30mvjcJf8JNh_MyPq27A_lQWbcFnVRiFrktcjGtmtKNVRlTvkgWOxVOfSqorrcF9QplVOmW8qpykuqqSiLQuyqRu57caCKm7LjWkCV8yz19Gpjpr2P3Db7pq6ySXY8-TV-iFb_ysf0T1V27rRhIAJ6BqJZe8_9g12CnvWL3PKBUmS5No09dPHiocon7YN_gwo6TNz-tg7jwryMdmK0S0iPbcuh1dLNz2vKCSiP6VFvwYF3Xfd5Ft3UfkgHHcbY7ZSdgU4r-e3nYXH2L1YB6LTe1QOd1uv-GwAA__9ObJRr">