<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/107389>107389</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Don't insert idiv/div branch when targetting modern x86-64 machines
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
orlp
</td>
</tr>
</table>
<pre>
I was quite surprised to learn that LLVM inserts a branch when doing a 64-bit signed division, [even on modern systems](https://cpp.godbolt.org/z/b76xTEcjh):
```cpp
#include <cstdint>
int64_t div(int64_t a, int64_t b) {
return a / b;
}
```
```asm
div(long, long):
mov rcx, rdi
or rcx, rsi
mov rax, rdi
shr rcx, 32
je .LBB0_1
cqo
idiv rsi
ret
.LBB0_1:
xor edx, edx
div esi
ret
```
I don't think LLVM should do this when tuning for modern x86-64 systems, as the difference between 32- and 64-bit division is minimal on those systems, and (in my opinion) not worth inserting unavoidable potential branch mispredictions without the consent of the programmer for.
On uops.info it appears that the difference between 32-bit division and 64-bit division is minimal on Cannon Lake, **not** on Cascade Lake, and then once again minimal on Ice Lake and all later Intel architectures.
On AMD's side it appears the difference is minimal on Zen 3 and higher.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVE1v4zYQ_TX0ZRBDouSvgw5JvAYCZNFL0UMvC4ocSbNLkVpyZCf99QUlexOlWTQIPCLn6z3izagYqXWIldg8iM1xpUbufKh8sMOq9ua1eoKLivBzJEaIYxgCRTTAHiyq4IA7xfD8_NdXIBcxcAQFdVBOd3Dp0IHx5FpQsC3vamKYmhkwdKZI3gn5CGLzgGd04B303mBwEF8jYx_F5ijkvmMeoijuhTwJedLDsG69qb3ltQ-tkKd_hDzVu-3Ln1_0907IQwrNjiK7_W6z-V8Pw_VGFuS0HQ2CKB51ZEOORfHlfRY53pbfOOEUcn87qQT3dqiFPIDYPcwJAAABeQwOFAh5gloUV5fYHT9A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