by muizelaar · 9y ago · 0 comments
C:
int foo(int *x, int *y) {
*x = 0;
*y = 1;
return *x;
}
gives: foo(int*, int*): # @foo(int*, int*)
mov dword ptr [rdi], 0
mov dword ptr [rsi], 1
mov eax, dword ptr [rdi]
ret
where as in Rust: fn foo(x: &mut i32, y: &mut i32) -> i32 {
*x = 0;
*y = 1;
*x
}
which compiles to: example::foo:
mov dword ptr [rdi], 0
mov dword ptr [rsi], 1
xor eax, eax
retDoes LLVM implement optimization that only Rust can use, or at least those that cannot be used from C/C++?
LLVM is adding some semantics specific to non-C or C++ languages. I’m on my phone so I can’t link you, but they’re adding an intrinsic related to infinite loops because languages like Rust have different semantics here.
int foo(int *restrict x, int *restrict y) {
*x = 0;
*y = 1;
return *x;
}
Compiles to: foo:
movl $0, (%rdi)
movl $1, (%rsi)
xorl %eax, %eax
ret