<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/128722>128722</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Suboptimal register use on x86-64
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
tavianator
</td>
</tr>
</table>
<pre>
Saw this while looking at https://github.com/llvm/llvm-project/issues/128441, but this seems like a more general issue. This C code
```c
int foo(int *a, int *b, int size) {
int ret = 0;
for (int i = 0; i < size; ++i) {
int diff = a[i] ^ b[i];
ret += diff;
if (!diff) {
break;
}
}
return ret;
}
```
[compiles](https://godbolt.org/z/dK74hY1r4) at all optimization levels to:
```asm
foo:
test edx, edx
jle .LBB0_1
mov ecx, edx
dec rcx
xor edx, edx
xor eax, eax
.LBB0_3:
mov r8d, eax
mov r9d, dword ptr [rdi + 4*rdx]
mov r10d, dword ptr [rsi + 4*rdx]
mov eax, r10d
xor eax, r9d
add eax, r8d
xor r10d, r9d
je .LBB0_5
lea r8, [rdx + 1]
cmp rcx, rdx
mov rdx, r8
jne .LBB0_3
.LBB0_5:
ret
.LBB0_1:
xor eax, eax
ret
```
It keeps moving the accumulator from `eax` to `r8d` and back. It also materializes the `xor` result twice when it doesn't need to. Ideally I think it would look more like this:
```asm
.LBB0_3:
mov r8d, dword ptr [rdi + 4*rdx]
xor r8d, dword ptr [rsi + 4*rdx]
je .LBB0_5
add eax, r8d
inc edx
cmp ecx, edx
jne .LBB0_3
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVcuO47gO_RplQ3Qgy69k4UVcdQMU7ux6NrMayBaTqCJbgSRX0vX1A8pOKs6kGw2MYUAPkkdHJEVK7_W-R6xYXrP8dSGHcLCuCvJDy14G6xaNVT-q7_IM4aA9nA_aIBhrj7rfgwxwCOHkWbphYsvEdq_DYWiWre2Y2BrzcR2-nZx9xzYws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