3 ms·
LLVM does implement these sorts of optimizations: C: int foo(int *x, int *y) { *x = 0; *y = 1; return *x; } gives: foo(int*, int*): # @foo
by muizelaar 9y ago
LLVM does implement these sorts of optimizations:
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
ret
- andoma 9y agoFor the record, gcc and clang does this as well when using the restrict keyword. (I'm still a big fan of Rust though) 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
- wolf550e 9y agoThat optimization is useful to C and C++ code, they use "restrict": https://en.wikipedia.org/wiki/Restrict https://en.wikipedia.org/wiki/Restrict Does LLVM implement optimization that only Rust can use, or at least those that cannot be used from C/C++?
- steveklabnik 9y ago&mut T pointers are basically restrict by default. 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.
- Manishearth 9y agoNot anymore, LLVM was buggy about this and we had to remove this.
- steveklabnik 9y agoThat’s why I said “basically”, it’s been fixed upstream and will be turned on again soon.
- deleted 9y ago[deleted]