<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64305>64305</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization in Euclid Remainder
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
zommiommy
</td>
</tr>
</table>
<pre>
Hi, @vigna, @progval, and I, noticed a missed optimization in the computation of Euclid remainder:
```C
int div_euclid(int num) {
int q = num / 8;
if (num % 8 < 0)
return 8 > 0 ? q - 1 : q + 1;
return q;
}
int rem_euclid(int num) {
int r = num % 8;
if (r < 0) {
return r + (8 < 0 ? -8 : 8);
}
return r;
}
```
https://godbolt.org/z/fKr1xsWYT
while div_euclid is correctly optimized to:
```asm
mov eax, edi
sar eax, 3
ret
```
while rem_euclid is correctly optimized to:
```asm
lea eax, [rdi + 7]
test edi, edi
cmovns eax, edi
and eax, -8
mov ecx, edi
sub ecx, eax
neg eax
test ecx, ecx
lea eax, [rdi + rax + 8]
cmovns eax, ecx
ret
```
instead of:
```asm
mov eax, edi
and eax, 7
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyclE9vozwQxj_NcBklMnYIcODQNEXvq9VeViut9rQy2CReYZzaJk376VcGktI01f6RkLAf28PvmTHDnVO7TsoCkg0k24j3fm9s8WK0Vkbr56gy4rn4TwG9R1iRo9p1fBofrNkdeRtmvBP4fxh0xqtaCuSolXNSoDl4pdUL98p0qDr0e4m10Yfej5Jp8KGvWyXQSs1VJ6QFdgdkC-QO1mR87se56jwKdfwhhwNAsyB0vQaaI6SbcRMiYtAfEdg2rCLQEjNg8_UGgWbjWoIZArtHAjSfdljpe9sN-gMSBFbiIy4wRmB3IS7dYPwm3nTg8SJCup0GF3Ar9Z-B2xl4chPcXoBnpycGO-ABzSZXA_0iG9CzYHEe7oI5M2FvmDjXYZzuvT-4UCRaAi13RlSm9Utjd0DLF6Bl88nGJ_ft-9d5Cp72qpWz6qFyWBtrZe3b5_MtkQK9eV9-7vSoaHMMpCj5KVw2KdSoO27nOhtVK_1N_jnQa1X-EghHqZV8_mVINlaooQQpJFP6vHR-2CPUG-pam2Pn3rsJP9Ms5iK7Ml9fme-rNzo_jXond6-TC8O0qT79jt_y0_DOLj6uec8xPsqz6pyXXKBp_qGkIQmTln70mUgUTOQs55Es4nVOspwm6SraFyLNYlbFlWzSNF-vKV_lTZLnrGZSpESwSBWUUEYyEseU0oQu2bqRLFuxjJOK0qaBFQntqF227VGHux0p53pZrFeMJFHLK9m6oWVS2sknHBaB0tBBbRHOLKp-52BFWuW8e43ilW9l8fl2b5wa4ZdzI4x62xZXf5vy-75a1kYDLUPU6bU4WPNT1h5oObA4oOXA-isAAP__sB2ngw">