Skip to content

Commit b365b5f

Browse files
committed
Propagate PlaceElem::Index.
1 parent bf84c73 commit b365b5f

25 files changed

+81
-35
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+33-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_const_eval::const_eval::CheckAlignment;
66
use rustc_const_eval::interpret::{ConstValue, ImmTy, Immediate, InterpCx, Scalar};
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_hir::def::DefKind;
9-
use rustc_middle::mir::visit::{MutVisitor, Visitor};
9+
use rustc_middle::mir::visit::{MutVisitor, NonMutatingUseContext, PlaceContext, Visitor};
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::layout::TyAndLayout;
1212
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -579,11 +579,29 @@ impl<'tcx> MutVisitor<'tcx> for CollectAndPatch<'tcx> {
579579
Operand::Copy(place) | Operand::Move(place) => {
580580
if let Some(value) = self.before_effect.get(&(location, *place)) {
581581
*operand = self.make_operand(value.clone());
582+
} else if !place.projection.is_empty() {
583+
self.super_operand(operand, location)
582584
}
583585
}
584586
_ => (),
585587
}
586588
}
589+
590+
fn process_projection_elem(
591+
&mut self,
592+
elem: PlaceElem<'tcx>,
593+
location: Location,
594+
) -> Option<PlaceElem<'tcx>> {
595+
if let PlaceElem::Index(local) = elem
596+
&& let Some(value) = self.before_effect.get(&(location, local.into()))
597+
&& let Ok(offset) = value.0.to_target_usize(&self.tcx)
598+
&& let Some(min_length) = offset.checked_add(1)
599+
{
600+
Some(PlaceElem::ConstantIndex { offset, min_length, from_end: false })
601+
} else {
602+
None
603+
}
604+
}
587605
}
588606

589607
pub(crate) struct OperandCollector<'tcx, 'map, 'a> {
@@ -594,17 +612,21 @@ pub(crate) struct OperandCollector<'tcx, 'map, 'a> {
594612

595613
impl<'tcx, 'map, 'a> Visitor<'tcx> for OperandCollector<'tcx, 'map, 'a> {
596614
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
597-
match operand {
598-
Operand::Copy(place) | Operand::Move(place) => {
599-
match self.state.get(place.as_ref(), self.map) {
600-
FlatSet::Top => (),
601-
FlatSet::Elem(value) => {
602-
self.visitor.before_effect.insert((location, *place), value);
603-
}
604-
FlatSet::Bottom => (),
605-
}
615+
if let Some(place) = operand.place() {
616+
if let FlatSet::Elem(value) = self.state.get(place.as_ref(), self.map) {
617+
self.visitor.before_effect.insert((location, place), value);
618+
} else if !place.projection.is_empty() {
619+
// Try to propagate into `Index` projections.
620+
self.super_operand(operand, location)
606621
}
607-
_ => (),
622+
}
623+
}
624+
625+
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, location: Location) {
626+
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy | NonMutatingUseContext::Move) = ctxt
627+
&& let FlatSet::Elem(value) = self.state.get(local.into(), self.map)
628+
{
629+
self.visitor.before_effect.insert((location, local.into()), value);
608630
}
609631
}
610632
}

tests/mir-opt/const_prop/array_index.main.ConstProp.32bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/array_index.main.ConstProp.32bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/array_index.main.ConstProp.64bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/array_index.main.ConstProp.64bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
}
4444

4545
bb1: {
46-
_5 = (*_1)[_6];
46+
- _5 = (*_1)[_6];
47+
+ _5 = (*_1)[3 of 4];
4748
StorageDead(_6);
4849
_0 = const ();
4950
StorageDead(_5);

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.32bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
}
4444

4545
bb1: {
46-
_5 = (*_1)[_6];
46+
- _5 = (*_1)[_6];
47+
+ _5 = (*_1)[3 of 4];
4748
StorageDead(_6);
4849
_0 = const ();
4950
StorageDead(_5);

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
}
4444

4545
bb1: {
46-
_5 = (*_1)[_6];
46+
- _5 = (*_1)[_6];
47+
+ _5 = (*_1)[3 of 4];
4748
StorageDead(_6);
4849
_0 = const ();
4950
StorageDead(_5);

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.ConstProp.64bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
}
4444

4545
bb1: {
46-
_5 = (*_1)[_6];
46+
- _5 = (*_1)[_6];
47+
+ _5 = (*_1)[3 of 4];
4748
StorageDead(_6);
4849
_0 = const ();
4950
StorageDead(_5);

tests/mir-opt/const_prop/large_array_index.main.ConstProp.32bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/large_array_index.main.ConstProp.32bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/large_array_index.main.ConstProp.64bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/large_array_index.main.ConstProp.64bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828

2929
bb1: {
30-
_1 = _2[_3];
30+
- _1 = _2[_3];
31+
+ _1 = _2[2 of 3];
3132
StorageDead(_3);
3233
StorageDead(_2);
3334
_0 = const ();

tests/mir-opt/const_prop/repeat.main.ConstProp.32bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
}
3030

3131
bb1: {
32-
_2 = _3[_4];
32+
- _2 = _3[_4];
33+
+ _2 = _3[2 of 3];
3334
_1 = Add(move _2, const 0_u32);
3435
StorageDead(_2);
3536
StorageDead(_4);

tests/mir-opt/const_prop/repeat.main.ConstProp.32bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
}
3030

3131
bb1: {
32-
_2 = _3[_4];
32+
- _2 = _3[_4];
33+
+ _2 = _3[2 of 3];
3334
_1 = Add(move _2, const 0_u32);
3435
StorageDead(_2);
3536
StorageDead(_4);

tests/mir-opt/const_prop/repeat.main.ConstProp.64bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
}
3030

3131
bb1: {
32-
_2 = _3[_4];
32+
- _2 = _3[_4];
33+
+ _2 = _3[2 of 3];
3334
_1 = Add(move _2, const 0_u32);
3435
StorageDead(_2);
3536
StorageDead(_4);

tests/mir-opt/const_prop/repeat.main.ConstProp.64bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
}
3030

3131
bb1: {
32-
_2 = _3[_4];
32+
- _2 = _3[_4];
33+
+ _2 = _3[2 of 3];
3334
_1 = Add(move _2, const 0_u32);
3435
StorageDead(_2);
3536
StorageDead(_4);

tests/mir-opt/const_prop/slice_len.main.ConstProp.32bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
}
3535

3636
bb1: {
37-
_1 = (*_2)[_6];
37+
- _1 = (*_2)[_6];
38+
+ _1 = (*_2)[1 of 2];
3839
StorageDead(_6);
3940
StorageDead(_4);
4041
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.ConstProp.32bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
}
3535

3636
bb1: {
37-
_1 = (*_2)[_6];
37+
- _1 = (*_2)[_6];
38+
+ _1 = (*_2)[1 of 2];
3839
StorageDead(_6);
3940
StorageDead(_4);
4041
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.ConstProp.64bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
}
3535

3636
bb1: {
37-
_1 = (*_2)[_6];
37+
- _1 = (*_2)[_6];
38+
+ _1 = (*_2)[1 of 2];
3839
StorageDead(_6);
3940
StorageDead(_4);
4041
StorageDead(_2);

tests/mir-opt/const_prop/slice_len.main.ConstProp.64bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
}
3535

3636
bb1: {
37-
_1 = (*_2)[_6];
37+
- _1 = (*_2)[_6];
38+
+ _1 = (*_2)[1 of 2];
3839
StorageDead(_6);
3940
StorageDead(_4);
4041
StorageDead(_2);

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.32bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
}
4646

4747
bb2: {
48-
_3 = _4[_5];
48+
- _3 = _4[_5];
49+
+ _3 = _4[3 of 4];
4950
StorageDead(_5);
5051
StorageDead(_4);
5152
_9 = const 42_u32;

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.32bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
}
4646

4747
bb2: {
48-
_3 = _4[_5];
48+
- _3 = _4[_5];
49+
+ _3 = _4[3 of 4];
4950
StorageDead(_5);
5051
StorageDead(_4);
5152
_9 = const 42_u32;

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.64bit.panic-abort.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
}
4646

4747
bb2: {
48-
_3 = _4[_5];
48+
- _3 = _4[_5];
49+
+ _3 = _4[3 of 4];
4950
StorageDead(_5);
5051
StorageDead(_4);
5152
_9 = const 42_u32;

tests/mir-opt/pre-codegen/optimizes_into_variable.main.ConstProp.64bit.panic-unwind.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
}
4646

4747
bb2: {
48-
_3 = _4[_5];
48+
- _3 = _4[_5];
49+
+ _3 = _4[3 of 4];
4950
StorageDead(_5);
5051
StorageDead(_4);
5152
_9 = const 42_u32;

0 commit comments

Comments
 (0)