diff options
| author | bors <bors@rust-lang.org> | 2024-06-25 02:50:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-06-25 02:50:37 +0000 |
| commit | fc555cd832ee743ff5410c35de2b0dd59ec4322e (patch) | |
| tree | 421b3f2a5fd13889a9b4a09cb6bd35823be6af1a /tests/codegen | |
| parent | 5b270e1198e911247244b035a6f06ce3af0a4420 (diff) | |
| parent | ec9e35618dd9d4b55282b340aa29fb8c78691aca (diff) | |
| download | rust-fc555cd832ee743ff5410c35de2b0dd59ec4322e.tar.gz rust-fc555cd832ee743ff5410c35de2b0dd59ec4322e.zip | |
Auto merge of #126852 - scottmcm:more-checked-math-tweaks, r=Amanieu
Also get `add nuw` from `uN::checked_add`
When I was doing this for `checked_{sub,shl,shr}`, it was mentioned https://github.com/rust-lang/rust/pull/124114#issuecomment-2066173305 that it'd be worth trying for `checked_add` too.
It makes a particularly-big difference for `x.checked_add(C)`, as doing this means that LLVM removes the intrinsic and does it as a normal `x <= MAX - C` instead.
cc `@DianQK` who had commented about `checked_add` related to https://github.com/rust-lang/hashbrown/issues/509 before
cc https://github.com/llvm/llvm-project/issues/80637 for how LLVM is unlikely to do this itself
Diffstat (limited to 'tests/codegen')
| -rw-r--r-- | tests/codegen/checked_math.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/tests/codegen/checked_math.rs b/tests/codegen/checked_math.rs index 41016e3b7be..75df5866d6e 100644 --- a/tests/codegen/checked_math.rs +++ b/tests/codegen/checked_math.rs @@ -84,3 +84,17 @@ pub fn checked_shr_signed(a: i32, b: u32) -> Option<i32> { // CHECK: ret { i32, i32 } %[[R1]] a.checked_shr(b) } + +// CHECK-LABEL: @checked_add_one_unwrap_unsigned +// CHECK-SAME: (i32 noundef %x) +#[no_mangle] +pub fn checked_add_one_unwrap_unsigned(x: u32) -> u32 { + // CHECK: %[[IS_MAX:.+]] = icmp eq i32 %x, -1 + // CHECK: br i1 %[[IS_MAX]], label %[[NONE_BB:.+]], label %[[SOME_BB:.+]] + // CHECK: [[SOME_BB]]: + // CHECK: %[[R:.+]] = add nuw i32 %x, 1 + // CHECK: ret i32 %[[R]] + // CHECK: [[NONE_BB]]: + // CHECK: call {{.+}}unwrap_failed + x.checked_add(1).unwrap() +} |
