Skip to content

Commit 9c3ac0c

Browse files
authored
Rollup merge of #72820 - jonas-schievink:instcombine-uninit, r=oli-obk
InstCombine: Don't optimize `&mut *x` into `x` Fixes #72797
2 parents b478964 + 309661e commit 9c3ac0c

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

src/librustc_mir/transform/instcombine.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Performs various peephole optimizations.
22
33
use crate::transform::{MirPass, MirSource};
4-
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5+
use rustc_hir::Mutability;
56
use rustc_index::vec::Idx;
67
use rustc_middle::mir::visit::{MutVisitor, Visitor};
78
use rustc_middle::mir::{
8-
Body, Constant, Local, Location, Mutability, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
9+
Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
910
};
1011
use rustc_middle::ty::{self, TyCtxt};
1112
use std::mem;
@@ -39,7 +40,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
3940
}
4041

4142
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
42-
if let Some(mtbl) = self.optimizations.and_stars.remove(&location) {
43+
if self.optimizations.and_stars.remove(&location) {
4344
debug!("replacing `&*`: {:?}", rvalue);
4445
let new_place = match rvalue {
4546
Rvalue::Ref(_, _, place) => {
@@ -57,10 +58,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
5758
}
5859
_ => bug!("Detected `&*` but didn't find `&*`!"),
5960
};
60-
*rvalue = Rvalue::Use(match mtbl {
61-
Mutability::Mut => Operand::Move(new_place),
62-
Mutability::Not => Operand::Copy(new_place),
63-
});
61+
*rvalue = Rvalue::Use(Operand::Copy(new_place))
6462
}
6563

6664
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
@@ -93,8 +91,8 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
9391
{
9492
// The dereferenced place must have type `&_`.
9593
let ty = Place::ty_from(local, proj_base, self.body, self.tcx).ty;
96-
if let ty::Ref(_, _, mtbl) = ty.kind {
97-
self.optimizations.and_stars.insert(location, mtbl);
94+
if let ty::Ref(_, _, Mutability::Not) = ty.kind {
95+
self.optimizations.and_stars.insert(location);
9896
}
9997
}
10098
}
@@ -114,6 +112,6 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
114112

115113
#[derive(Default)]
116114
struct OptimizationList<'tcx> {
117-
and_stars: FxHashMap<Location, Mutability>,
115+
and_stars: FxHashSet<Location>,
118116
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
119117
}

src/test/mir-opt/inline/issue-58867-inline-as-ref-as-mut/rustc.a.Inline.after.mir

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ fn a(_1: &mut [T]) -> &mut [T] {
88
let mut _4: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:6
99
scope 1 {
1010
debug self => _4; // in scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
11+
let mut _5: &mut [T]; // in scope 1 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
1112
}
1213

1314
bb0: {
1415
StorageLive(_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
1516
StorageLive(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
1617
StorageLive(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:6
1718
_4 = &mut (*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:6
18-
_3 = move _4; // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
19+
StorageLive(_5); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
20+
_5 = &mut (*_4); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
21+
_3 = &mut (*_5); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
22+
StorageDead(_5); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
1923
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
2024
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:14: 3:15
2125
_0 = &mut (*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15

src/test/mir-opt/inline/issue-58867-inline-as-ref-as-mut/rustc.b.Inline.after.mir

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn b(_1: &mut std::boxed::Box<T>) -> &mut T {
99
scope 1 {
1010
debug self => _4; // in scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
1111
let mut _5: &mut T; // in scope 1 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
12+
let mut _6: &mut T; // in scope 1 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
1213
}
1314

1415
bb0: {
@@ -17,8 +18,11 @@ fn b(_1: &mut std::boxed::Box<T>) -> &mut T {
1718
StorageLive(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:6
1819
_4 = &mut (*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:6
1920
StorageLive(_5); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
20-
_5 = &mut (*(*_4)); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
21-
_3 = move _5; // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
21+
StorageLive(_6); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
22+
_6 = &mut (*(*_4)); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
23+
_5 = &mut (*_6); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
24+
_3 = &mut (*_5); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
25+
StorageDead(_6); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
2226
StorageDead(_5); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
2327
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
2428
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:14: 8:15

src/test/mir-opt/nrvo-simple/rustc.nrvo.RenameReturnPlace.diff

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@
2626
// + span: $DIR/nrvo-simple.rs:3:20: 3:21
2727
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
2828
StorageLive(_3); // scope 1 at $DIR/nrvo-simple.rs:4:5: 4:19
29+
StorageLive(_5); // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
30+
StorageLive(_6); // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
2931
- _6 = &mut _2; // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
3032
+ _6 = &mut _0; // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
31-
_3 = move _1(move _6) -> bb1; // scope 1 at $DIR/nrvo-simple.rs:4:5: 4:19
33+
_5 = &mut (*_6); // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
34+
_3 = move _1(move _5) -> bb1; // scope 1 at $DIR/nrvo-simple.rs:4:5: 4:19
3235
}
3336

3437
bb1: {
38+
StorageDead(_5); // scope 1 at $DIR/nrvo-simple.rs:4:18: 4:19
39+
StorageDead(_6); // scope 1 at $DIR/nrvo-simple.rs:4:19: 4:20
3540
StorageDead(_3); // scope 1 at $DIR/nrvo-simple.rs:4:19: 4:20
3641
- _0 = _2; // scope 1 at $DIR/nrvo-simple.rs:5:5: 5:8
3742
- StorageDead(_2); // scope 0 at $DIR/nrvo-simple.rs:6:1: 6:2

0 commit comments

Comments
 (0)