Skip to content

Commit 2524a6a

Browse files
committed
Save another BB by using SubUnchecked instead of a call to arith_offset
Probably reasonable anyway since it more obviously drops provenance.
1 parent 4392415 commit 2524a6a

7 files changed

+256
-320
lines changed

library/core/src/slice/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::iter::{
1111
use crate::marker::PhantomData;
1212
use crate::mem::{self, SizedTypeProperties};
1313
use crate::num::NonZero;
14-
use crate::ptr::{NonNull, null, without_provenance, without_provenance_mut};
14+
use crate::ptr::{NonNull, without_provenance, without_provenance_mut};
1515
use crate::{cmp, fmt};
1616

1717
#[stable(feature = "boxed_slice_into_iter", since = "1.80.0")]

library/core/src/slice/iter/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,13 @@ macro_rules! iterator {
162162
// SAFETY: Type invariants.
163163
unsafe {
164164
if T::IS_ZST {
165-
let byte_end = end_or_len as *const u8;
166-
if byte_end == null() {
165+
let len = end_or_len.addr();
166+
if len == 0 {
167167
return None;
168168
}
169-
self.end_or_len = byte_end.wrapping_sub(1) as _;
169+
self.end_or_len = without_provenance_mut(len.unchecked_sub(1));
170170
} else {
171-
if ptr == crate::intrinsics::transmute::<*const T, NonNull<T>>(end_or_len) {
171+
if ptr == crate::intrinsics::transmute::<$ptr, NonNull<T>>(end_or_len) {
172172
return None;
173173
}
174174
self.ptr = ptr.add(1);

tests/mir-opt/pre-codegen/slice_iter.enumerated_loop.PreCodegen.after.panic-abort.mir

+85-97
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
77
let mut _11: std::ptr::NonNull<T>;
88
let mut _12: *const T;
99
let mut _13: usize;
10-
let mut _33: std::option::Option<(usize, &T)>;
11-
let mut _36: &impl Fn(usize, &T);
12-
let mut _37: (usize, &T);
13-
let _38: ();
10+
let mut _31: std::option::Option<(usize, &T)>;
11+
let mut _34: &impl Fn(usize, &T);
12+
let mut _35: (usize, &T);
13+
let _36: ();
1414
scope 1 {
1515
debug (((iter: Enumerate<std::slice::Iter<'_, T>>).0: std::slice::Iter<'_, T>).0: std::ptr::NonNull<T>) => _11;
1616
debug (((iter: Enumerate<std::slice::Iter<'_, T>>).0: std::slice::Iter<'_, T>).1: *const T) => _12;
1717
debug (((iter: Enumerate<std::slice::Iter<'_, T>>).0: std::slice::Iter<'_, T>).2: std::marker::PhantomData<&T>) => const ZeroSized: PhantomData<&T>;
1818
debug ((iter: Enumerate<std::slice::Iter<'_, T>>).1: usize) => _13;
19-
let _34: usize;
20-
let _35: &T;
19+
let _32: usize;
20+
let _33: &T;
2121
scope 2 {
22-
debug i => _34;
23-
debug x => _35;
22+
debug i => _32;
23+
debug x => _33;
2424
}
2525
scope 19 (inlined <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next) {
26-
let mut _27: std::option::Option<&T>;
27-
let mut _31: (usize, bool);
28-
let mut _32: (usize, &T);
26+
let mut _25: std::option::Option<&T>;
27+
let mut _29: (usize, bool);
28+
let mut _30: (usize, &T);
2929
scope 20 {
30-
let _30: usize;
30+
let _28: usize;
3131
scope 25 {
3232
}
3333
}
@@ -42,8 +42,8 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
4242
}
4343
}
4444
scope 26 (inlined <Option<&T> as Try>::branch) {
45-
let mut _28: isize;
46-
let _29: &T;
45+
let mut _26: isize;
46+
let _27: &T;
4747
scope 27 {
4848
}
4949
}
@@ -52,43 +52,37 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
5252
let _16: std::ptr::NonNull<T>;
5353
let mut _19: bool;
5454
let mut _22: std::ptr::NonNull<T>;
55-
let mut _24: bool;
56-
let mut _25: *const u8;
57-
let mut _26: *const T;
55+
let mut _24: usize;
5856
scope 30 {
5957
let _15: *const T;
6058
scope 31 {
61-
let _23: *const u8;
59+
let _23: usize;
6260
scope 32 {
63-
scope 33 (inlined null::<u8>) {
64-
scope 34 (inlined without_provenance::<()>) {
65-
scope 35 (inlined without_provenance_mut::<()>) {
61+
scope 35 (inlined core::num::<impl usize>::unchecked_sub) {
62+
scope 36 (inlined core::ub_checks::check_language_ub) {
63+
scope 37 (inlined core::ub_checks::check_language_ub::runtime) {
6664
}
6765
}
68-
scope 36 (inlined std::ptr::from_raw_parts::<u8, ()>) {
69-
}
7066
}
71-
scope 37 (inlined std::ptr::const_ptr::<impl *const u8>::wrapping_sub) {
72-
scope 38 (inlined core::num::<impl isize>::wrapping_neg) {
73-
scope 39 (inlined core::num::<impl isize>::wrapping_sub) {
74-
}
75-
}
76-
scope 40 (inlined std::ptr::const_ptr::<impl *const u8>::wrapping_offset) {
77-
}
67+
scope 38 (inlined without_provenance_mut::<T>) {
7868
}
7969
}
80-
scope 41 (inlined <NonNull<T> as PartialEq>::eq) {
70+
scope 33 (inlined std::ptr::const_ptr::<impl *const T>::addr) {
71+
scope 34 (inlined std::ptr::const_ptr::<impl *const T>::cast::<()>) {
72+
}
73+
}
74+
scope 39 (inlined <NonNull<T> as PartialEq>::eq) {
8175
let mut _17: *mut T;
8276
let mut _18: *mut T;
83-
scope 42 (inlined NonNull::<T>::as_ptr) {
77+
scope 40 (inlined NonNull::<T>::as_ptr) {
8478
}
85-
scope 43 (inlined NonNull::<T>::as_ptr) {
79+
scope 41 (inlined NonNull::<T>::as_ptr) {
8680
}
8781
}
88-
scope 44 (inlined NonNull::<T>::add) {
82+
scope 42 (inlined NonNull::<T>::add) {
8983
let mut _20: *const T;
9084
let mut _21: *const T;
91-
scope 45 (inlined NonNull::<T>::as_ptr) {
85+
scope 43 (inlined NonNull::<T>::as_ptr) {
9286
}
9387
}
9488
}
@@ -181,13 +175,14 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
181175
}
182176

183177
bb4: {
184-
StorageLive(_33);
185-
StorageLive(_30);
186178
StorageLive(_31);
187-
StorageLive(_27);
179+
StorageLive(_28);
180+
StorageLive(_29);
181+
StorageLive(_25);
188182
StorageLive(_14);
189183
StorageLive(_15);
190184
StorageLive(_23);
185+
StorageLive(_24);
191186
StorageLive(_16);
192187
_14 = copy _11;
193188
_15 = copy _12;
@@ -225,104 +220,97 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
225220
bb7: {
226221
StorageDead(_19);
227222
StorageDead(_16);
223+
StorageDead(_24);
228224
StorageDead(_23);
229225
StorageDead(_15);
230226
StorageDead(_14);
231-
StorageLive(_28);
232-
goto -> bb17;
227+
StorageLive(_26);
228+
goto -> bb12;
233229
}
234230

235231
bb8: {
236-
_23 = copy _15 as *const u8 (PtrToPtr);
237-
StorageLive(_24);
238-
_24 = Eq(copy _23, const {0x0 as *const u8});
239-
switchInt(move _24) -> [0: bb9, otherwise: bb16];
232+
_23 = copy _15 as usize (Transmute);
233+
switchInt(copy _23) -> [0: bb9, otherwise: bb10];
240234
}
241235

242236
bb9: {
237+
StorageDead(_16);
243238
StorageDead(_24);
244-
StorageLive(_25);
245-
_25 = arith_offset::<u8>(move _23, const -1_isize) -> [return: bb10, unwind unreachable];
239+
StorageDead(_23);
240+
StorageDead(_15);
241+
StorageDead(_14);
242+
StorageLive(_26);
243+
goto -> bb12;
246244
}
247245

248246
bb10: {
249-
_26 = move _25 as *const T (PtrToPtr);
250-
StorageDead(_25);
251-
_12 = copy _26;
247+
_24 = SubUnchecked(copy _23, const 1_usize);
248+
_12 = copy _24 as *const T (Transmute);
252249
goto -> bb11;
253250
}
254251

255252
bb11: {
256-
_27 = copy _14 as std::option::Option<&T> (Transmute);
253+
_25 = copy _14 as std::option::Option<&T> (Transmute);
257254
StorageDead(_16);
255+
StorageDead(_24);
258256
StorageDead(_23);
259257
StorageDead(_15);
260258
StorageDead(_14);
261-
StorageLive(_28);
262-
_28 = discriminant(_27);
263-
switchInt(move _28) -> [0: bb17, 1: bb12, otherwise: bb15];
259+
StorageLive(_26);
260+
_26 = discriminant(_25);
261+
switchInt(move _26) -> [0: bb12, 1: bb14, otherwise: bb17];
264262
}
265263

266264
bb12: {
267-
_29 = move ((_27 as Some).0: &T);
265+
StorageDead(_26);
266+
StorageDead(_25);
267+
StorageDead(_29);
268268
StorageDead(_28);
269-
StorageDead(_27);
270-
_30 = copy _13;
271-
_31 = AddWithOverflow(copy _13, const 1_usize);
272-
assert(!move (_31.1: bool), "attempt to compute `{} + {}`, which would overflow", copy _13, const 1_usize) -> [success: bb13, unwind unreachable];
269+
StorageDead(_31);
270+
StorageDead(_11);
271+
StorageDead(_12);
272+
StorageDead(_13);
273+
drop(_2) -> [return: bb13, unwind unreachable];
273274
}
274275

275276
bb13: {
276-
_13 = move (_31.0: usize);
277-
StorageLive(_32);
278-
_32 = (copy _30, copy _29);
279-
_33 = Option::<(usize, &T)>::Some(move _32);
280-
StorageDead(_32);
281-
StorageDead(_31);
282-
StorageDead(_30);
283-
_34 = copy (((_33 as Some).0: (usize, &T)).0: usize);
284-
_35 = copy (((_33 as Some).0: (usize, &T)).1: &T);
285-
StorageLive(_36);
286-
_36 = &_2;
287-
StorageLive(_37);
288-
_37 = (copy _34, copy _35);
289-
_38 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _36, move _37) -> [return: bb14, unwind unreachable];
277+
return;
290278
}
291279

292280
bb14: {
293-
StorageDead(_37);
294-
StorageDead(_36);
295-
StorageDead(_33);
296-
goto -> bb4;
281+
_27 = move ((_25 as Some).0: &T);
282+
StorageDead(_26);
283+
StorageDead(_25);
284+
_28 = copy _13;
285+
_29 = AddWithOverflow(copy _13, const 1_usize);
286+
assert(!move (_29.1: bool), "attempt to compute `{} + {}`, which would overflow", copy _13, const 1_usize) -> [success: bb15, unwind unreachable];
297287
}
298288

299289
bb15: {
300-
unreachable;
290+
_13 = move (_29.0: usize);
291+
StorageLive(_30);
292+
_30 = (copy _28, copy _27);
293+
_31 = Option::<(usize, &T)>::Some(move _30);
294+
StorageDead(_30);
295+
StorageDead(_29);
296+
StorageDead(_28);
297+
_32 = copy (((_31 as Some).0: (usize, &T)).0: usize);
298+
_33 = copy (((_31 as Some).0: (usize, &T)).1: &T);
299+
StorageLive(_34);
300+
_34 = &_2;
301+
StorageLive(_35);
302+
_35 = (copy _32, copy _33);
303+
_36 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _34, move _35) -> [return: bb16, unwind unreachable];
301304
}
302305

303306
bb16: {
304-
StorageDead(_24);
305-
StorageDead(_16);
306-
StorageDead(_23);
307-
StorageDead(_15);
308-
StorageDead(_14);
309-
StorageLive(_28);
310-
goto -> bb17;
311-
}
312-
313-
bb17: {
314-
StorageDead(_28);
315-
StorageDead(_27);
307+
StorageDead(_35);
308+
StorageDead(_34);
316309
StorageDead(_31);
317-
StorageDead(_30);
318-
StorageDead(_33);
319-
StorageDead(_11);
320-
StorageDead(_12);
321-
StorageDead(_13);
322-
drop(_2) -> [return: bb18, unwind unreachable];
310+
goto -> bb4;
323311
}
324312

325-
bb18: {
326-
return;
313+
bb17: {
314+
unreachable;
327315
}
328316
}

0 commit comments

Comments
 (0)