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

    <tr>
        <th>Summary</th>
        <td>
            Failure to recognize `llvm.ssub.with.overflow` idiom
        </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>
    InstCombine should recognize code of the form `x == SMIN ? default : -x` and canonicalize it to use `llvm.ssub.with.overflow(0, x)`

https://godbolt.org/z/MqfvKh74G
```rust
#[no_mangle]
#[inline(never)]
pub fn src1(x: i32) -> (i32, bool) {
 x.overflowing_neg()
}

#[no_mangle]
#[inline(never)]
pub fn tgt1(x: i32) -> (i32, bool) {
 0i32.overflowing_sub(x)
}

#[no_mangle]
#[inline(never)]
pub fn src2(x: i32) -> Option<i32> {
 x.checked_neg()
}

#[no_mangle]
#[inline(never)]
pub fn tgt2(x: i32) -> Option<i32> {
 0i32.checked_sub(x)
}

#[no_mangle]
#[inline(never)]
pub fn src3(x: i32) -> Option<i32> {
 x.checked_abs()
}

#[no_mangle]
#[inline(never)]
pub fn tgt3(x: i32) -> Option<i32> {
    if x >= 0 {
        Some(x)
    } else {
        0i32.checked_sub(x)
 }
}

#[no_mangle]
#[inline(never)]
pub fn src4(x: i32) -> (i32, bool) {
 x.overflowing_abs()
}

#[no_mangle]
#[inline(never)]
pub fn tgt4(x: i32) -> (i32, bool) {
    if x >= 0 {
        (x, false)
 } else {
        0i32.overflowing_sub(x)
    }
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vd-PozYQ_muGl1EiM04gPPCwmxzV6XTtw_0BJwMDuGfsFJtcun99ZbLb_aFqq3QbhECM7ZlvvkHfp7zXvWUuYXsP20Oi5jC4qfwysvqhbVK79s_ys_Vh78ZaW0Y_uNm0OHHjeqsfGBvXMroOw8DYuWlEyMQZQR5AHvDb18-_IsgKW-7UbAKCvMPVGTKByrbYKOusbpSJiXTA4HD2HDMYcxrX3s_1-qcOw9qdeOqM-wm0E0B7PAMVkAkQBxB3l-cQwtGDvAOqgKretbUzYe2mHqh6AKq-_tGdvgz55pfHQ5m43NPsw2OIJGzvrfs-Ktsbhu3hZVxboy0D7SyfeIr1n9aPc42dRT81KdDuHFvUkoAKXIH8hEC75XOPtXMmhiG_v5zE89-dadt_t9wD7WLqS9388LLBD8ILfbgantCSXiH0c73kuA1EPzX0TxB_OwbtLMh9DEbIL_hrBm5-cHtz7q4EtjD3hO3mrMn_ypqq_U1ZuxIYIuoOo3p8iuohXi_F65sb-RWXMQj5AdlE4Xi7_d0x4HO7__dANh8TgluP5Wp4_z6YC7N77JTx_Iri90bznrY8TvYtBU-6nbSlbAtZqITLNCe52WV5TslQtjLLOtlt802-LXjbyrRNU6ZCpSmrutgmuiRBG7FLKd2lhUzXrciypk6lzLs67ziFjeBRabNeXMhNfaK9n7lMBRWbPDGqZuMXwySKW0DeaetDc7FIoEgiEI3ae25X7hj0qB_U8tMTRZOdynhsVc-9h40w2gf_XCvoYLislDbzxNESn732HWPMBOpWuzGZJ1O-8UIdhrleN24Eqha8l9fqOLnfuQlA1dKfB6oeWzyV9FcAAAD__xqIO90">