Rollup merge of #72820 - jonas-schievink:instcombine-uninit, r=oli-obk

InstCombine: Don't optimize `&mut *x` into `x`

Fixes https://github.com/rust-lang/rust/issues/72797
This commit is contained in:
Dylan DPC 2020-06-03 02:39:03 +02:00 committed by GitHub
commit 9c3ac0c9bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 14 deletions

View File

@ -1,11 +1,12 @@
//! Performs various peephole optimizations.
use crate::transform::{MirPass, MirSource};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::Mutability;
use rustc_index::vec::Idx;
use rustc_middle::mir::visit::{MutVisitor, Visitor};
use rustc_middle::mir::{
Body, Constant, Local, Location, Mutability, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
Body, Constant, Local, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue,
};
use rustc_middle::ty::{self, TyCtxt};
use std::mem;
@ -39,7 +40,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
}
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
if let Some(mtbl) = self.optimizations.and_stars.remove(&location) {
if self.optimizations.and_stars.remove(&location) {
debug!("replacing `&*`: {:?}", rvalue);
let new_place = match rvalue {
Rvalue::Ref(_, _, place) => {
@ -57,10 +58,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
}
_ => bug!("Detected `&*` but didn't find `&*`!"),
};
*rvalue = Rvalue::Use(match mtbl {
Mutability::Mut => Operand::Move(new_place),
Mutability::Not => Operand::Copy(new_place),
});
*rvalue = Rvalue::Use(Operand::Copy(new_place))
}
if let Some(constant) = self.optimizations.arrays_lengths.remove(&location) {
@ -93,8 +91,8 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
{
// The dereferenced place must have type `&_`.
let ty = Place::ty_from(local, proj_base, self.body, self.tcx).ty;
if let ty::Ref(_, _, mtbl) = ty.kind {
self.optimizations.and_stars.insert(location, mtbl);
if let ty::Ref(_, _, Mutability::Not) = ty.kind {
self.optimizations.and_stars.insert(location);
}
}
}
@ -114,6 +112,6 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
#[derive(Default)]
struct OptimizationList<'tcx> {
and_stars: FxHashMap<Location, Mutability>,
and_stars: FxHashSet<Location>,
arrays_lengths: FxHashMap<Location, Constant<'tcx>>,
}

View File

@ -8,6 +8,7 @@ fn a(_1: &mut [T]) -> &mut [T] {
let mut _4: &mut [T]; // in scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:6
scope 1 {
debug self => _4; // in scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
let mut _5: &mut [T]; // in scope 1 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
}
bb0: {
@ -15,7 +16,10 @@ fn a(_1: &mut [T]) -> &mut [T] {
StorageLive(_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
StorageLive(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:6
_4 = &mut (*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:6
_3 = move _4; // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
StorageLive(_5); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
_5 = &mut (*_4); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
_3 = &mut (*_5); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
StorageDead(_5); // scope 1 at $SRC_DIR/libcore/convert/mod.rs:LL:COL
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:14: 3:15
_0 = &mut (*_2); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:3:5: 3:15

View File

@ -9,6 +9,7 @@ fn b(_1: &mut std::boxed::Box<T>) -> &mut T {
scope 1 {
debug self => _4; // in scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
let mut _5: &mut T; // in scope 1 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
let mut _6: &mut T; // in scope 1 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
}
bb0: {
@ -17,8 +18,11 @@ fn b(_1: &mut std::boxed::Box<T>) -> &mut T {
StorageLive(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:6
_4 = &mut (*_1); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:6
StorageLive(_5); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
_5 = &mut (*(*_4)); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
_3 = move _5; // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
StorageLive(_6); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
_6 = &mut (*(*_4)); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
_5 = &mut (*_6); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
_3 = &mut (*_5); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
StorageDead(_6); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
StorageDead(_5); // scope 1 at $SRC_DIR/liballoc/boxed.rs:LL:COL
_2 = &mut (*_3); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:5: 8:15
StorageDead(_4); // scope 0 at $DIR/issue-58867-inline-as-ref-as-mut.rs:8:14: 8:15

View File

@ -26,12 +26,17 @@
// + span: $DIR/nrvo-simple.rs:3:20: 3:21
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
StorageLive(_3); // scope 1 at $DIR/nrvo-simple.rs:4:5: 4:19
StorageLive(_5); // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
StorageLive(_6); // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
- _6 = &mut _2; // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
+ _6 = &mut _0; // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
_3 = move _1(move _6) -> bb1; // scope 1 at $DIR/nrvo-simple.rs:4:5: 4:19
_5 = &mut (*_6); // scope 1 at $DIR/nrvo-simple.rs:4:10: 4:18
_3 = move _1(move _5) -> bb1; // scope 1 at $DIR/nrvo-simple.rs:4:5: 4:19
}
bb1: {
StorageDead(_5); // scope 1 at $DIR/nrvo-simple.rs:4:18: 4:19
StorageDead(_6); // scope 1 at $DIR/nrvo-simple.rs:4:19: 4:20
StorageDead(_3); // scope 1 at $DIR/nrvo-simple.rs:4:19: 4:20
- _0 = _2; // scope 1 at $DIR/nrvo-simple.rs:5:5: 5:8
- StorageDead(_2); // scope 0 at $DIR/nrvo-simple.rs:6:1: 6:2