<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/62145>62145</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Inefficient codegen for checking top bits of a 64-bit integer
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dzaima
</td>
</tr>
</table>
<pre>
The code
```c
#include<stdint.h>
#include<stdbool.h>
void ext1(void);
void ext2(void);
static bool check(uint64_t x) {
return (x>>48) == 0xfff7;
}
void f(uint64_t a, uint64_t b) {
if (check(a)) ext1();
ext2();
if (check(b)) ext1();
}
```
compiles (x86-64, `-O3`; [compiler explorer](https://godbolt.org/z/dre88hevc)) to
```asm
f: # @f
push r15
push r14
push rbx
mov rbx, rsi
movabs r14, -281474976710656
movabs r15, -2533274790395904
shr rdi, 48
cmp edi, 65527
jne .LBB0_2
call ext1@PLT
.LBB0_2:
call ext2@PLT
and rbx, r14
cmp rbx, r15
jne .LBB0_3
pop rbx
pop r14
pop r15
jmp ext1@PLT # TAILCALL
.LBB0_3:
pop rbx
pop r14
pop r15
ret
```
which uses `(x>>48)==0xfff7` for one `check` call, and `(x&0xffff000000000000) == 0xfff7000000000000` for the other. This ends up loading those large constants within the function prologue (which increases the prologue/epilogue even more due to pushing out non-volatile registers), but only uses them for one of the cases (meaning that those constants don't even need to be loaded during the prologue).
Simpler example, but without the prologue issue: [compiler explorer](https://godbolt.org/z/K7nb6KrGn)
```c
void f(uint64_t a, uint64_t b) {
if (check(a) || check(b)) ext1();
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVk2PozgT_jXOpZQIbD4PHPIxeTWalt6Vtu8jA0XwLNjINunM_PqVDUlIunsPs4uiGHgeVz1VZRfmxoiTRCxIvCPxYcVH2ypd1L-46PmqVPXP4rVFqFSNJDiQYEuSYPpV8zNlQlbdWCNhe2NrIe2mJ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