Skip to content

Commit

Permalink
Is it worth keeping the unlikely!?
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Jun 23, 2024
1 parent f5db46c commit a53dd2f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ macro_rules! uint_impl {
// Per <https://github.com/rust-lang/rust/pull/124114#issuecomment-2066173305>,
// LLVM is happy to re-form the intrinsic later if useful.

if intrinsics::add_with_overflow(self, rhs).1 {
if unlikely!(intrinsics::add_with_overflow(self, rhs).1) {
None
} else {
// SAFETY: Just checked it doesn't overflow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
debug n => _2;
let mut _0: u16;
scope 1 (inlined <u16 as Step>::forward) {
let mut _7: u16;
let mut _8: u16;
scope 2 {
}
scope 3 (inlined <u16 as Step>::forward_checked) {
scope 4 {
scope 6 (inlined core::num::<impl u16>::checked_add) {
let mut _5: (u16, bool);
let mut _6: bool;
let mut _7: bool;
}
}
scope 5 (inlined convert::num::ptr_try_from_impls::<impl TryFrom<usize> for u16>::try_from) {
Expand All @@ -32,45 +33,52 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
StorageLive(_4);
StorageLive(_3);
_3 = Gt(_2, const 65535_usize);
switchInt(move _3) -> [0: bb1, otherwise: bb4];
switchInt(move _3) -> [0: bb1, otherwise: bb5];
}

bb1: {
_4 = _2 as u16 (IntToInt);
StorageDead(_3);
StorageLive(_7);
StorageLive(_6);
StorageLive(_5);
_5 = AddWithOverflow(_1, _4);
_6 = (_5.1: bool);
switchInt(move _6) -> [0: bb2, otherwise: bb3];
_7 = unlikely(move _6) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_5);
StorageDead(_6);
goto -> bb6;
switchInt(move _7) -> [0: bb3, otherwise: bb4];
}

bb3: {
StorageDead(_5);
StorageDead(_6);
goto -> bb5;
StorageDead(_7);
goto -> bb7;
}

bb4: {
StorageDead(_3);
goto -> bb5;
StorageDead(_5);
StorageDead(_6);
StorageDead(_7);
goto -> bb6;
}

bb5: {
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb6, unwind unreachable];
StorageDead(_3);
goto -> bb6;
}

bb6: {
StorageLive(_7);
_7 = _2 as u16 (IntToInt);
_0 = Add(_1, _7);
StorageDead(_7);
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb7, unwind unreachable];
}

bb7: {
StorageLive(_8);
_8 = _2 as u16 (IntToInt);
_0 = Add(_1, _8);
StorageDead(_8);
StorageDead(_4);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
debug n => _2;
let mut _0: u16;
scope 1 (inlined <u16 as Step>::forward) {
let mut _7: u16;
let mut _8: u16;
scope 2 {
}
scope 3 (inlined <u16 as Step>::forward_checked) {
scope 4 {
scope 6 (inlined core::num::<impl u16>::checked_add) {
let mut _5: (u16, bool);
let mut _6: bool;
let mut _7: bool;
}
}
scope 5 (inlined convert::num::ptr_try_from_impls::<impl TryFrom<usize> for u16>::try_from) {
Expand All @@ -32,45 +33,52 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
StorageLive(_4);
StorageLive(_3);
_3 = Gt(_2, const 65535_usize);
switchInt(move _3) -> [0: bb1, otherwise: bb4];
switchInt(move _3) -> [0: bb1, otherwise: bb5];
}

bb1: {
_4 = _2 as u16 (IntToInt);
StorageDead(_3);
StorageLive(_7);
StorageLive(_6);
StorageLive(_5);
_5 = AddWithOverflow(_1, _4);
_6 = (_5.1: bool);
switchInt(move _6) -> [0: bb2, otherwise: bb3];
_7 = unlikely(move _6) -> [return: bb2, unwind unreachable];
}

bb2: {
StorageDead(_5);
StorageDead(_6);
goto -> bb6;
switchInt(move _7) -> [0: bb3, otherwise: bb4];
}

bb3: {
StorageDead(_5);
StorageDead(_6);
goto -> bb5;
StorageDead(_7);
goto -> bb7;
}

bb4: {
StorageDead(_3);
goto -> bb5;
StorageDead(_5);
StorageDead(_6);
StorageDead(_7);
goto -> bb6;
}

bb5: {
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb6, unwind continue];
StorageDead(_3);
goto -> bb6;
}

bb6: {
StorageLive(_7);
_7 = _2 as u16 (IntToInt);
_0 = Add(_1, _7);
StorageDead(_7);
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb7, unwind continue];
}

bb7: {
StorageLive(_8);
_8 = _2 as u16 (IntToInt);
_0 = Add(_1, _8);
StorageDead(_8);
StorageDead(_4);
return;
}
Expand Down

0 comments on commit a53dd2f

Please sign in to comment.