Closed
Description
Consider this code: https://godbolt.org/z/QpiBK3 .
Code:
pub fn f(a: &mut i32, b: &mut i32) -> (i32, i32, i32) {
let c = *a;
let d = *b;
*a = 0;
(c, d, *b)
}
Asm (1.30):
example::f:
movl (%rsi), %eax
movl (%rdx), %ecx
movl $0, (%rsi)
movl (%rdx), %edx
movl %eax, (%rdi)
movl %ecx, 4(%rdi)
movl %edx, 8(%rdi)
movq %rdi, %rax
retq
We read from memory at line let d = *b
(from %rdx
) and then we read from memory again at line (c, d, *b)
(again from %rdx
).
But in Rust (unlike C and C++) we know that two mutable references cannot point to same location. So, we missed optimization. This is regression