Skip to content

Commit a53dd2f

Browse files
committed
Is it worth keeping the unlikely!?
1 parent f5db46c commit a53dd2f

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

library/core/src/num/uint_macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ macro_rules! uint_impl {
462462
// Per <https://github.com/rust-lang/rust/pull/124114#issuecomment-2066173305>,
463463
// LLVM is happy to re-form the intrinsic later if useful.
464464

465-
if intrinsics::add_with_overflow(self, rhs).1 {
465+
if unlikely!(intrinsics::add_with_overflow(self, rhs).1) {
466466
None
467467
} else {
468468
// SAFETY: Just checked it doesn't overflow

tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir

+22-14
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
55
debug n => _2;
66
let mut _0: u16;
77
scope 1 (inlined <u16 as Step>::forward) {
8-
let mut _7: u16;
8+
let mut _8: u16;
99
scope 2 {
1010
}
1111
scope 3 (inlined <u16 as Step>::forward_checked) {
1212
scope 4 {
1313
scope 6 (inlined core::num::<impl u16>::checked_add) {
1414
let mut _5: (u16, bool);
1515
let mut _6: bool;
16+
let mut _7: bool;
1617
}
1718
}
1819
scope 5 (inlined convert::num::ptr_try_from_impls::<impl TryFrom<usize> for u16>::try_from) {
@@ -32,45 +33,52 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
3233
StorageLive(_4);
3334
StorageLive(_3);
3435
_3 = Gt(_2, const 65535_usize);
35-
switchInt(move _3) -> [0: bb1, otherwise: bb4];
36+
switchInt(move _3) -> [0: bb1, otherwise: bb5];
3637
}
3738

3839
bb1: {
3940
_4 = _2 as u16 (IntToInt);
4041
StorageDead(_3);
42+
StorageLive(_7);
4143
StorageLive(_6);
4244
StorageLive(_5);
4345
_5 = AddWithOverflow(_1, _4);
4446
_6 = (_5.1: bool);
45-
switchInt(move _6) -> [0: bb2, otherwise: bb3];
47+
_7 = unlikely(move _6) -> [return: bb2, unwind unreachable];
4648
}
4749

4850
bb2: {
49-
StorageDead(_5);
50-
StorageDead(_6);
51-
goto -> bb6;
51+
switchInt(move _7) -> [0: bb3, otherwise: bb4];
5252
}
5353

5454
bb3: {
5555
StorageDead(_5);
5656
StorageDead(_6);
57-
goto -> bb5;
57+
StorageDead(_7);
58+
goto -> bb7;
5859
}
5960

6061
bb4: {
61-
StorageDead(_3);
62-
goto -> bb5;
62+
StorageDead(_5);
63+
StorageDead(_6);
64+
StorageDead(_7);
65+
goto -> bb6;
6366
}
6467

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

6973
bb6: {
70-
StorageLive(_7);
71-
_7 = _2 as u16 (IntToInt);
72-
_0 = Add(_1, _7);
73-
StorageDead(_7);
74+
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb7, unwind unreachable];
75+
}
76+
77+
bb7: {
78+
StorageLive(_8);
79+
_8 = _2 as u16 (IntToInt);
80+
_0 = Add(_1, _8);
81+
StorageDead(_8);
7482
StorageDead(_4);
7583
return;
7684
}

tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir

+22-14
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
55
debug n => _2;
66
let mut _0: u16;
77
scope 1 (inlined <u16 as Step>::forward) {
8-
let mut _7: u16;
8+
let mut _8: u16;
99
scope 2 {
1010
}
1111
scope 3 (inlined <u16 as Step>::forward_checked) {
1212
scope 4 {
1313
scope 6 (inlined core::num::<impl u16>::checked_add) {
1414
let mut _5: (u16, bool);
1515
let mut _6: bool;
16+
let mut _7: bool;
1617
}
1718
}
1819
scope 5 (inlined convert::num::ptr_try_from_impls::<impl TryFrom<usize> for u16>::try_from) {
@@ -32,45 +33,52 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
3233
StorageLive(_4);
3334
StorageLive(_3);
3435
_3 = Gt(_2, const 65535_usize);
35-
switchInt(move _3) -> [0: bb1, otherwise: bb4];
36+
switchInt(move _3) -> [0: bb1, otherwise: bb5];
3637
}
3738

3839
bb1: {
3940
_4 = _2 as u16 (IntToInt);
4041
StorageDead(_3);
42+
StorageLive(_7);
4143
StorageLive(_6);
4244
StorageLive(_5);
4345
_5 = AddWithOverflow(_1, _4);
4446
_6 = (_5.1: bool);
45-
switchInt(move _6) -> [0: bb2, otherwise: bb3];
47+
_7 = unlikely(move _6) -> [return: bb2, unwind unreachable];
4648
}
4749

4850
bb2: {
49-
StorageDead(_5);
50-
StorageDead(_6);
51-
goto -> bb6;
51+
switchInt(move _7) -> [0: bb3, otherwise: bb4];
5252
}
5353

5454
bb3: {
5555
StorageDead(_5);
5656
StorageDead(_6);
57-
goto -> bb5;
57+
StorageDead(_7);
58+
goto -> bb7;
5859
}
5960

6061
bb4: {
61-
StorageDead(_3);
62-
goto -> bb5;
62+
StorageDead(_5);
63+
StorageDead(_6);
64+
StorageDead(_7);
65+
goto -> bb6;
6366
}
6467

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

6973
bb6: {
70-
StorageLive(_7);
71-
_7 = _2 as u16 (IntToInt);
72-
_0 = Add(_1, _7);
73-
StorageDead(_7);
74+
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb7, unwind continue];
75+
}
76+
77+
bb7: {
78+
StorageLive(_8);
79+
_8 = _2 as u16 (IntToInt);
80+
_0 = Add(_1, _8);
81+
StorageDead(_8);
7482
StorageDead(_4);
7583
return;
7684
}

0 commit comments

Comments
 (0)