Skip to content

Commit 39118d6

Browse files
committed
Go back to Some instead of transmuting to it.
This adds a few more statements to `next`, but optimizes better in the loops (saving 2 blocks in `forward_loop`, for example)
1 parent 3a62c70 commit 39118d6

6 files changed

+205
-164
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ macro_rules! iterator {
173173
}
174174
self.ptr = ptr.add(1);
175175
}
176-
crate::intrinsics::transmute::<NonNull<T>, Option<$elem>>(ptr)
176+
Some({ptr}.$into_ref())
177177
}
178178
}
179179

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

+73-72
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 _31: std::option::Option<(usize, &T)>;
11-
let mut _34: &impl Fn(usize, &T);
12-
let mut _35: (usize, &T);
13-
let _36: ();
10+
let mut _32: std::option::Option<(usize, &T)>;
11+
let mut _35: &impl Fn(usize, &T);
12+
let mut _36: (usize, &T);
13+
let _37: ();
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 _32: usize;
20-
let _33: &T;
19+
let _33: usize;
20+
let _34: &T;
2121
scope 2 {
22-
debug i => _32;
23-
debug x => _33;
22+
debug i => _33;
23+
debug x => _34;
2424
}
2525
scope 19 (inlined <Enumerate<std::slice::Iter<'_, T>> as Iterator>::next) {
26-
let mut _25: std::option::Option<&T>;
27-
let mut _29: (usize, bool);
28-
let mut _30: (usize, &T);
26+
let mut _27: std::option::Option<&T>;
27+
let mut _30: (usize, bool);
28+
let mut _31: (usize, &T);
2929
scope 20 {
30-
let _28: usize;
30+
let _29: usize;
3131
scope 25 {
3232
}
3333
}
@@ -42,8 +42,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
4242
}
4343
}
4444
scope 26 (inlined <Option<&T> as Try>::branch) {
45-
let mut _26: isize;
46-
let _27: &T;
45+
let _28: &T;
4746
scope 27 {
4847
}
4948
}
@@ -53,6 +52,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
5352
let mut _19: bool;
5453
let mut _22: std::ptr::NonNull<T>;
5554
let mut _24: usize;
55+
let _26: &T;
5656
scope 30 {
5757
let _15: *const T;
5858
scope 31 {
@@ -85,6 +85,13 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
8585
scope 43 (inlined NonNull::<T>::as_ptr) {
8686
}
8787
}
88+
scope 44 (inlined NonNull::<T>::as_ref::<'_>) {
89+
let _25: *const T;
90+
scope 45 (inlined NonNull::<T>::as_ptr) {
91+
}
92+
scope 46 (inlined std::ptr::mut_ptr::<impl *mut T>::cast_const) {
93+
}
94+
}
8895
}
8996
}
9097
}
@@ -175,15 +182,16 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
175182
}
176183

177184
bb4: {
178-
StorageLive(_31);
179-
StorageLive(_28);
185+
StorageLive(_32);
180186
StorageLive(_29);
181-
StorageLive(_25);
187+
StorageLive(_30);
188+
StorageLive(_27);
182189
StorageLive(_14);
183190
StorageLive(_15);
184191
StorageLive(_23);
185192
StorageLive(_24);
186193
StorageLive(_16);
194+
StorageLive(_26);
187195
_14 = copy _11;
188196
_15 = copy _12;
189197
switchInt(const <T as std::mem::SizedTypeProperties>::IS_ZST) -> [0: bb5, otherwise: bb8];
@@ -214,103 +222,96 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
214222
StorageDead(_21);
215223
_11 = move _22;
216224
StorageDead(_22);
217-
goto -> bb11;
225+
goto -> bb13;
218226
}
219227

220228
bb7: {
221229
StorageDead(_19);
230+
StorageDead(_26);
222231
StorageDead(_16);
223232
StorageDead(_24);
224233
StorageDead(_23);
225234
StorageDead(_15);
226235
StorageDead(_14);
227-
StorageLive(_26);
228-
goto -> bb12;
236+
goto -> bb10;
229237
}
230238

231239
bb8: {
232240
_23 = copy _15 as usize (Transmute);
233-
switchInt(copy _23) -> [0: bb9, otherwise: bb10];
241+
switchInt(copy _23) -> [0: bb9, otherwise: bb12];
234242
}
235243

236244
bb9: {
245+
StorageDead(_26);
237246
StorageDead(_16);
238247
StorageDead(_24);
239248
StorageDead(_23);
240249
StorageDead(_15);
241250
StorageDead(_14);
242-
StorageLive(_26);
243-
goto -> bb12;
251+
goto -> bb10;
244252
}
245253

246254
bb10: {
247-
_24 = SubUnchecked(copy _23, const 1_usize);
248-
_12 = copy _24 as *const T (Transmute);
249-
goto -> bb11;
250-
}
251-
252-
bb11: {
253-
_25 = copy _14 as std::option::Option<&T> (Transmute);
254-
StorageDead(_16);
255-
StorageDead(_24);
256-
StorageDead(_23);
257-
StorageDead(_15);
258-
StorageDead(_14);
259-
StorageLive(_26);
260-
_26 = discriminant(_25);
261-
switchInt(move _26) -> [0: bb12, 1: bb14, otherwise: bb17];
262-
}
263-
264-
bb12: {
265-
StorageDead(_26);
266-
StorageDead(_25);
255+
StorageDead(_27);
256+
StorageDead(_30);
267257
StorageDead(_29);
268-
StorageDead(_28);
269-
StorageDead(_31);
258+
StorageDead(_32);
270259
StorageDead(_11);
271260
StorageDead(_12);
272261
StorageDead(_13);
273-
drop(_2) -> [return: bb13, unwind unreachable];
262+
drop(_2) -> [return: bb11, unwind unreachable];
274263
}
275264

276-
bb13: {
265+
bb11: {
277266
return;
278267
}
279268

280-
bb14: {
281-
_27 = move ((_25 as Some).0: &T);
282-
StorageDead(_26);
269+
bb12: {
270+
_24 = SubUnchecked(copy _23, const 1_usize);
271+
_12 = copy _24 as *const T (Transmute);
272+
goto -> bb13;
273+
}
274+
275+
bb13: {
276+
StorageLive(_25);
277+
_25 = copy _14 as *const T (Transmute);
278+
_26 = &(*_25);
283279
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];
280+
_27 = Option::<&T>::Some(copy _26);
281+
StorageDead(_26);
282+
StorageDead(_16);
283+
StorageDead(_24);
284+
StorageDead(_23);
285+
StorageDead(_15);
286+
StorageDead(_14);
287+
_28 = move ((_27 as Some).0: &T);
288+
StorageDead(_27);
289+
_29 = copy _13;
290+
_30 = AddWithOverflow(copy _13, const 1_usize);
291+
assert(!move (_30.1: bool), "attempt to compute `{} + {}`, which would overflow", copy _13, const 1_usize) -> [success: bb14, unwind unreachable];
287292
}
288293

289-
bb15: {
290-
_13 = move (_29.0: usize);
291-
StorageLive(_30);
292-
_30 = (copy _28, copy _27);
293-
_31 = Option::<(usize, &T)>::Some(move _30);
294+
bb14: {
295+
_13 = move (_30.0: usize);
296+
StorageLive(_31);
297+
_31 = (copy _29, copy _28);
298+
_32 = Option::<(usize, &T)>::Some(move _31);
299+
StorageDead(_31);
294300
StorageDead(_30);
295301
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;
302+
_33 = copy (((_32 as Some).0: (usize, &T)).0: usize);
303+
_34 = copy (((_32 as Some).0: (usize, &T)).1: &T);
301304
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];
305+
_35 = &_2;
306+
StorageLive(_36);
307+
_36 = (copy _33, copy _34);
308+
_37 = <impl Fn(usize, &T) as Fn<(usize, &T)>>::call(move _35, move _36) -> [return: bb15, unwind unreachable];
304309
}
305310

306-
bb16: {
311+
bb15: {
312+
StorageDead(_36);
307313
StorageDead(_35);
308-
StorageDead(_34);
309-
StorageDead(_31);
314+
StorageDead(_32);
310315
goto -> bb4;
311316
}
312-
313-
bb17: {
314-
unreachable;
315-
}
316317
}

0 commit comments

Comments
 (0)