<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/164495>164495</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] sub(X, umin(X, Y)) => usub_sat(X, Y) (where supported?)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
krzysz00
</td>
</tr>
</table>
<pre>
[Alive2 verification](https://alive2.llvm.org/ce/z/GHChPo)
This here pattern
```llvm
define i8 @src(i8 noundef %x, i8 noundef %y) {
%z = call i8 @llvm.umin.i8(i8 %x, i8 %y)
%z2 = sub i8 %x, %z
ret i8 %z2
}
define i8 @tgt(i8 noundef %x, i8 noundef %y) {
%z = call i8 @llvm.usub.sat(i8 %x, i8 %y)
ret i8 %z
}
```
shows up a bunch in code generate at least from IREE if not general MLIR projects (once reasoning was done, often MLIR side, to convert what used to be `smin`s into `umin`s).
I think this is a good candidate for an InstCombine, since it shrinks the IR and removes duplicate uses of `%x`. It also looks to me like most targets support usubsat so this is a safe transform in general.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVM2OrDYTfZpiU7rImP6BBYu-08P3tZRI0SiLZBUZXIAzYLdcpjvTTx8ZyNyeTVaRWm049aNTh-NSzKa3RBXsv8P-nKg5DM5X7_7xwQ8hksbpjxg7jeZGEm_kTWdaFYyzsD-DLIYQrgz5CWQNslZLWjqOtyl1vgdZtwSyfoCs__f_l-EXB7IEcQJx-nUwjAN5wqsKgbxdYTiI9RdbgDhp6owlNAXCTrBvQRamQOtmq6lDkPu_QL7gV-gDZIlw_A7ihPH9gZCfsVXjuPVZ6M2Tsakp1oZPjbYGn8Vyqea5wae8GFgyPIUNf8hI_3he5_jCO_ThP-TNc5OyCv9K_InWD1b_SAvixIO7M85XVNjMth3QWGydJuzJkleBUAUcSXHAzrsJL2-vr2g6tC5sKSP-_NPlDa_e_UltYARZONsSelLsrLE93hWjdpYiPdcFsmsFG71AwWHr7I18wPugAs5MOoINIRwET8bGA40NLgLzBoAs01XiC4bB2Pf4z2gYFfbOaWyV1UbHETrnUVm8WA4vbmrMSoVNpGkC8uCNfWcMA-HlDZXV6GlyN2LU83WMLqfIitF1kcEi9UGkeAmoRnY4OhfLHU6Eo3knnBwHDMr3FBh5vl6dj3PNDauA7J6YsuoIg1eWO-enqP6maproKtdlXqqEqux4yEQhil2RDFXZllTqrtSKuu4oj2UmNJVdk2f5TkpdJKaSQu4zITOZiaMsUpk1zY7K7ECa9kWWw07QpMz4eT0TwzxTlR12u3KfjKqhkZdFIKWlOy5RkDLuBV_Fom_N3HM0ouHAP9oEE8ZlgzxLvV8uDcjit6j68vm259-jS6PV8zPkr4tAf6yG_gxHO92X7bDJSBryGmSZzH6svu6c3oRhbtLWTSDrZW2sx7fNmyDrZRIGWW-j3ir5dwAAAP__5qqHyQ">