<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/152061>152061</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Testing bit next to signbit does not constant fold when squaring signed integers
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ZERICO2005
</td>
</tr>
</table>
<pre>
https://godbolt.org/z/7bqs6W8an
```
bool func_B(int8_t x) {
return (x * x) & (1 << 30);
}
```
Assuming that `int` is 32 bits, then squaring `int8_t` should clear bits [15, 31] as the result will be [0, 16384]. I noticed that the code is not constant folded to return false when testing bit 30 (the bit next to the signbit). It also generates different llvm compared to testing bits 16-29* (`icmp samesign ugt` versus `icmp ne 0`).
Transforming `return (x * x) & (1 << 30)` to `return false` appears to be correct:
https://alive2.llvm.org/ce/z/Q6w5gy
*testing bit 15 does not constant fold, but I will address it in a separate issue.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVE1v4zYQ_TX0ZRCDGomyddDBSWogp6LFAgV6CShxJLOgSS-HSrL99cXIzm4atIcFBAgk5-PNe4-0zH6ORL0y98o8buxSTin3f_7y-9PDr6i12QzJfetPpVxY1QeFR4XHObkhhbJNeVZ4_FvhcTd85faPvY1KH1Srb58-DCkFmJY4Pt8r3PtY9s8F3hR2oHb3Sh8AADKVJUdQuH8DhYfbMbayU4GqH1T9ALVW2KlactTu8VOXA_Ny9nGGcrIFVKt9LKrV4BlqhMEXVvgA5UQR-Otis4Reo_bPayCf0hIcjIFsXuNBmfvKSFZdKfMIliUdMvESCrz6EGAgCdISU7X1vlHmcQtPEFPxI7krFMkZkyNBElOBMUUuNhaYUnASlN6nn2xggleBWIiLIBx8gVoLC1JGVpHeiuTIWmQbfFHYbeGpgA2cYKZI2RZicH6aKFMsEMLLGcZ0vth8bfihPEPV3mEnpCvcCyPj-QJszyTVYZlXcl4o88LwfhwJVtqx24oM-vAl28hTyucbrT-hZ6sF0Y-clQXZtZcL2cxyOgiFOdNYxH_68G8r2uBfCLcy5c2OI908-Vv7auZvV4wKDx9prQy4RP-hiag5LAWerhpb5zIxgy_gI1hgulghGDzzQjL_xvW16-rObqivdqapTVO1enPqjRka1K42aKrOdoTTbqgdYdNYZ9px2PgeNRq910brutV665rGWbMbp7HSrpusajSdrQ_fh9usXfvKoG6rTbADBV7vLWKk1ysmhSjXOPeSdDcsM6tGB8-Ff5QpvgTqv3zg491YN1P9DzlXe36_QevD4cDHQjNl3iw5fH4nfDktw3ZMZ4VHaX_73V1y-ksExeMKmhUeb1O99PhPAAAA__9YoW8o">