We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmp
add
sub
This rust fn: https://godbolt.org/z/W9WM6YPYf
#[no_mangle] pub fn last(xs: &[u8]) -> Option<&u8> { xs.last() }
produces this assembly:
last: add x8, x1, x0 cmp x1, #0 sub x8, x8, #1 csel x0, xzr, x8, eq ret
Equivalent C also produces the same assembly https://godbolt.org/z/c5dMbv63K
#include <stddef.h> #include <stdint.h> uint8_t* last(uint8_t* x0, size_t x1) { if (x1 == 0) { return NULL; } else { return x0 + x1 - 1; } }
By reassocating sub and add (from (x0 + x1) -1 to x0 + (x1 - 1)), we can save an instruction by reusing the overflow flag from the first sub:
(x0 + x1) -1
x0 + (x1 - 1)
tgt: // @tgt subs x8, x1, #1 add x8, x0, x8 csel x0, xzr, x8, lo ret
I could not convince clang to produce this assembly, even using __builtin_sub_overflow: https://godbolt.org/z/6vPWjx1zv
__builtin_sub_overflow
#include <stdbool.h> #include <stddef.h> #include <stdint.h> uint8_t* tgt(uint8_t* x0, size_t x1) { size_t sum; bool overflow = __builtin_sub_overflow(x1, 1, &sum); if (overflow) { return NULL; } else { return x0 + sum; } }
I could only produce this assembly by writing the LLVM IR manually: https://godbolt.org/z/hvjEEsG9r
define noundef i64 @tgt(i64 noundef %0, i64 noundef %1) { %4 = tail call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %1, i64 1) %overflow = extractvalue { i64, i1 } %4, 1 %diff = extractvalue {i64, i1} %4, 0 %sum = add i64 %0, %diff %ret = select i1 %overflow, i64 0, i64 %sum ret i64 %ret }
The text was updated successfully, but these errors were encountered:
@llvm/issue-subscribers-backend-aarch64
Author: Karl Meakin (Kmeakin)
https://godbolt.org/z/5EKvWeEYb
#[no_mangle] pub fn last(xs:&[u8])->Option<&u8>{ xs.last() }
last: add x8, x1, x0 cmp x1, #<!-- -->0 sub x8, x8, #<!-- -->1 csel x0, xzr, x8, eq ret
#include <stddef.h> #include <stdint.h> uint8_t* last(uint8_t* x0, size_t x1) { if (x1 == 0) { return NULL; } else { return x0 + x1 - 1; } }
By reassocating sub and add, we can save an instruction by reusing the overflow flag from the first sub:
tgt: // @<!-- -->tgt subs x8, x1, #<!-- -->1 add x8, x0, x8 csel x0, xzr, x8, lo ret
#include <stdbool.h> #include <stddef.h> #include <stdint.h> uint8_t* tgt(uint8_t* x0, size_t x1) { size_t sum; bool overflow = __builtin_sub_overflow(x1, 1, &sum); if (overflow) { return NULL; } else { return x0 + sum; } }
define noundef i64 @<!-- -->tgt(i64 noundef %0, i64 noundef %1) { %4 = tail call { i64, i1 } @<!-- -->llvm.usub.with.overflow.i64(i64 %1, i64 1) %overflow = extractvalue { i64, i1 } %4, 1 %diff = extractvalue {i64, i1} %4, 0 %sum = add i64 %0, %diff %ret = select i1 %overflow, i64 0, i64 %sum ret i64 %ret }
Sorry, something went wrong.
Successfully merging a pull request may close this issue.
This rust fn:
https://godbolt.org/z/W9WM6YPYf
produces this assembly:
Equivalent C also produces the same assembly
https://godbolt.org/z/c5dMbv63K
By reassocating
sub
andadd
(from(x0 + x1) -1
tox0 + (x1 - 1)
), we can save an instruction by reusing the overflow flag from the firstsub
:I could not convince clang to produce this assembly, even using
__builtin_sub_overflow
:https://godbolt.org/z/6vPWjx1zv
I could only produce this assembly by writing the LLVM IR manually:
https://godbolt.org/z/hvjEEsG9r
The text was updated successfully, but these errors were encountered: