<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/162717>162717</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Failure to match signed sub overflow test idiom
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sh1boot
</td>
</tr>
</table>
<pre>
Checking for signed integer overflow after an addItion seems to work fine, but this subtraction test gives bad code on multiple architectures (every one I tried, both 32-bit and 64-bit):
```c++
bool idiomatic_check_sub(int x, int y) {
int64_t result = int64_t(x) - int64_t(y);
return !(INT_MIN <= result && result <= INT_MAX);
}
bool expected_check_sub(int x, int y) {
int result;
return __builtin_ssub_overflow(x, y, &result);
}
```
https://godbolt.org/z/a91sjPzGz
I haven't checked but this issue may generalise across overflow tests for multiple destination data types. And presumably there's another pattern which people would use when there's no larger temporary type, but I have tested any of those yet.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVMGO4jgQ_RpzKTUyTkjIIQeGESsOO9rDHvaGKnFBPOPYkV2GTn_9ygGmZ-e2kiVMper5vfIrY4zm6ohasf0itl9XmHjwoY3DpvOeV53Xc3sYqP9h3BUuPsCSrsE4pisF8DcKF-vvgBemAOgAtT6x8Q4i0RiBPdx9-AEX40ioA3SJgQcTIaaOA_ZLKlNkuJobRehQQ-81gXcwJstmsgQY-sEw9ZwCRRBqRzcKM3hHcAIOhvQC7XmAQr11hgGdhqrMW6EaUeyFXFYlH6sX6ktect95b8Fo40dk05_7rPUcUyfUzjiG9wycN7NQDYg6lwBADlXlmSFQTJZBFF9fIaF27zn37ZfAvJB41QbiFBwItRFqd_r29_nP0zcQxSGDvPBUJVT1ib58W1L3__zEEvXXh6xFA71P1DPp_yPhecCT2pPX-dwlY9m4c4ypO79u-KHrkFEOmd-z9DcyrwYLuR-Yp5hbr45CHa9ed97y2oerUMcPoY7YbOL3vz7--HiIOMGAN3JC1QyLBNKfZjExJoIRZ7iSo4DWRALsg4_x04LZRXEx6U_jaIpsHC4m08gIPE8U1wB7p2HKEkbs7Aw8UCCh6gjofP4DEzJTcHAfTD_ARD7D3X2yGlIkuA_kfqlyHiyGPBBM4-QDhnk56uX4h7iFIWlAN4O_AA8-EszE65VuC90UDa6o3dSVlMV2VzWroVWXokGNcqd3F1nqStbU4bbq9W5bygL7lWmVVNuNlM1mt93IZl2XVDdYoqrKUkosRClpRGPX1t7G3P3V0st2U6l6U68sdmTjMv1KObo_Oi2Uyo9BaHPRW5euUZTSmsjxE4YNW2qPaGwKlMd8RO6H1_sQU_ffi3nMGKxSsO1vzjA8pG7d-1GoY4Z__rxNwX-nnoU6LqSiUMcn61ur_g0AAP__WaKJKQ">