<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63149>63149</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization: Compare ignoring top bit
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Alcaro
</td>
</tr>
</table>
<pre>
```c++
#include <stdint.h>
bool isnan1(uint64_t val)
{
return (val & 0x7FFFFFFFFFFFFFFF) >= 0x7FF0000000000001;
}
bool isnan2(uint64_t val)
{
return val << 1 >= 0x7FF0000000000001 << 1;
}
```
https://godbolt.org/z/76vdrM7v7
Expected result: Former should be translated to latter (which in turn may be optimized further).
Actual result: Clang just emits a straightforward translation of both, including two movabs for the constants. GCC swaps the & for the rare btr instruction. MSVC recognizes they're equivalent and emits the same asm ...but it chooses the former.
With how weird modern processor performance is, I don't know if this proposed rewrite is faster, but it's a clear win on code size, at least. (This most likely affects all cpu archs, not just x86_64, but I didn't check.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVE2P2zYQ_TX0ZRBBpmx9HHRwvHGRw55atMeAIkcSsxRHJUfW7v76grI33RTbQwhCgjBv5j3OPFHFaAeP2IrjZ3F82KmFRwrtyWkVaNeReWlFmd-2FvJz2vmDyE9CFtZrtxgEUZwjG-s5G0Xx5R7enh2RAxu98nsh68V6Lg_fGK7KCdncgdW9IABAQF6CByHrq3IgZAn5c3X5eQnZQKIpHm7B_N3ai-JNXvXwsRD5K0I2FcVZFGfY_z_pD8gH7G-9u32OzHMUxUnIi5CXgUxHjjMKg5CXVyEvVXk14bG6Vu-1f3meUTMaCBgXx6I4wYXChAHiSIsz0CFwUD46lVBM4BQzhtTGdbR6BOthO86kXhKYZraTfUUD_RJ4xCBkk71nPGlelHvHd3bKD_B9iQw4WY6gIHJQdhi5p7CqYH4osOSBeuiIRyHPcPOI9QPwSjDRVXURegrAI4ImH1l5jhn8dj5DXNUct0Ca_BsoqIDQcQDrI4dFJ4YMHn__8wwBNQ3evuKW9SJkFRDw78VelUPPoLy5602FopoQVJwgy7JuYbAMeiSKt-zEN2H4qQ9_WR5hpBVWtMHARAaDhzmQxhgpwIwhZSmvEWxMx_0KhryQFcOTpxVsDzzamFJmitsI12A5oaFXkVPrz3ATI2SV-qodqgCr9UAeNBmEaF8xwRSDQxU5S4P9I5WdKDI4-4TuBVTfo06TcQ70vIAKetwkeeLb5J7r8lt5eCP8Csaam1Q9on7KhGx2pi1MUzRqh-2-rMu8PB7zZje2qi7L_tiUjeqOpurruukOTd_keaGb_KjVzrYyl0Ve5uX-WOwPdXbc99hXCmud182-PohDjpOyLnPuOiXH72yMC7ZlsT80O6c6dHG7g6T0uMIWFFKmKym0KedTtwxRHHJnI8d_q7Blh-2jjam7d2NvJtxsS9Oc3GMHT2HzIM3QWd4twbX_-Rctj0uXaZqEvKTq99enOdB31CzkZdMUhbxsmv8JAAD__4hGo_0">