| Issue |
238703
|
| Summary |
[X86] Missed register copy elimination due to suboptimal load ordering before tail call
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
Leo0506
|
Description
LLVM generates an unnecessary register-to-register copy before a tail call because of the ordering of two independent loads.
Reordering these loads allows the second load to write directly into the required argument register,
reducing the generated x86-64 assembly from 5 to 4 instructions.
Alive2 proof: https://alive2.llvm.org/ce/z/HgBR_z
```llvm
declare i32 @sink(ptr, ptr, ptr)
define i32 @src(ptr readonly %p, ptr readonly %q) {
%a = load ptr, ptr %q, align 8
%b = load ptr, ptr %p, align 8
%g = getelementptr inbounds i8, ptr %b, i64 16
%c = load ptr, ptr %g, align 8
%r = tail call i32 @sink(ptr %a, ptr %c, ptr %b)
ret i32 %r
}
```
Current assembly (5 instructions):
movq (%rsi), %rax
movq (%rdi), %rdx
movq 16(%rdx), %rsi
movq %rax, %rdi
jmp sink
Assembly after reordering (4 instructions)
Swapping the first two loads produces:
movq (%rdi), %rdx
movq (%rsi), %rdi
movq 16(%rdx), %rsi
jmp sink
On x86-64 System V, %rdi initially holds %p and must eventually hold the first argument to sink.
In the current ordering, %q is loaded first, so its result must be stored temporarily in %rax because %rdi is
still needed to load from %p. This introduces an additional movq %rax, %rdi.
Loading from %p first frees %rdi, allowing the subsequent load from %q to write directly into the required argument register.
Both loads only read memory, so reordering them preserves the program's behavior. However, LLVM preserves the less favorable ordering, missing an opportunity to eliminate the register copy
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs