about summary refs log tree commit diff
path: root/compiler/rustc_errors/src/codes.rs
diff options
context:
space:
mode:
authorLeón Orell Valerian Liehr <me@fmease.dev>2024-05-15 14:21:38 +0200
committerGitHub <noreply@github.com>2024-05-15 14:21:38 +0200
commit4f7d9d4ad815b6a48e1cd41fcc63e37875d3341d (patch)
tree8ab84521fcece7a26ba09c6243df5a67adea381e /compiler/rustc_errors/src/codes.rs
parent2804d4223b2cea91dcd7e168573e619a6c1c7ca5 (diff)
parent7fde7308bf751315e0f4fabe32258916b166e34e (diff)
downloadrust-4f7d9d4ad815b6a48e1cd41fcc63e37875d3341d.tar.gz
rust-4f7d9d4ad815b6a48e1cd41fcc63e37875d3341d.zip
Rollup merge of #125038 - ivan-shrimp:checked_sub, r=joboet
Invert comparison in `uN::checked_sub`

After #124114, LLVM no longer combines the comparison and subtraction in `uN::checked_sub` when either operand is a constant (demo: https://rust.godbolt.org/z/MaeoYbsP1). The difference is more pronounced when the expression is slightly more complex (https://rust.godbolt.org/z/4rPavsYdc).

This is due to the use of `>=` here:

https://github.com/rust-lang/rust/blob/ee97564e3a9f9ac8c65103abb37c6aa48d95bfa2/library/core/src/num/uint_macros.rs#L581-L593

For constant `C`, LLVM eagerly converts `a >= C` into `a > C - 1`, but the backend can only combine `a < C` with `a - C`, not `C - 1 < a` and `a - C`: https://github.com/llvm/llvm-project/blob/e586556e375fc5c4f7e76b5c299cb981f2016108/llvm/lib/CodeGen/CodeGenPrepare.cpp#L1697-L1742

This PR[^1] simply inverts the `>=` into `<` to restore the LLVM magic, and somewhat align this with the implementation of `uN::overflowing_sub` from #103299.

When the result is stored as an `Option` (rather than being branched/cmoved on), the discriminant is `self >= rhs`. This PR doesn't affect the codegen (and relevant tests) of that since LLVM will negate `self < rhs` to `self >= rhs` when necessary.

[^1]: Note to `self`: My very first contribution to publicly-used code. Hopefully like what I should learn to always be, tiny and humble.
Diffstat (limited to 'compiler/rustc_errors/src/codes.rs')
0 files changed, 0 insertions, 0 deletions