Simplify aggregate projections.

This commit is contained in:
Camille GILLOT 2023-09-16 09:35:46 +00:00
parent 692e528647
commit 48d2157a89
9 changed files with 192 additions and 119 deletions

View File

@ -276,6 +276,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
}
fn insert_scalar(&mut self, scalar: Scalar, ty: Ty<'tcx>) -> VnIndex {
self.insert(Value::Constant(Const::from_scalar(self.tcx, scalar, ty)))
}
#[instrument(level = "trace", skip(self), ret)]
fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
use Value::*;
@ -483,12 +487,33 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
}
ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index),
ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty),
ProjectionElem::Field(f, ty) => {
if let Value::Aggregate(_, _, fields) = self.get(value) {
return Some(fields[f.as_usize()]);
} else if let Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant)) = self.get(value)
&& let Value::Aggregate(_, written_variant, fields) = self.get(*outer_value)
&& written_variant == read_variant
{
return Some(fields[f.as_usize()]);
}
ProjectionElem::Field(f, ty)
}
ProjectionElem::Index(idx) => {
let idx = self.locals[idx]?;
ProjectionElem::Index(idx)
}
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
match self.get(value) {
Value::Aggregate(ty, _, operands) if ty.is_array() => {
let offset = if from_end {
operands.len() - offset as usize
} else {
offset as usize
};
return operands.get(offset).copied();
}
_ => {}
};
ProjectionElem::ConstantIndex { offset, min_length, from_end }
}
ProjectionElem::Subslice { from, to, from_end } => {
@ -679,6 +704,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
Rvalue::Discriminant(ref mut place) => {
let place = self.simplify_place_value(place, location)?;
if let Some(discr) = self.simplify_discriminant(place) {
return Some(discr);
}
Value::Discriminant(place)
}
@ -688,6 +716,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
debug!(?value);
Some(self.insert(value))
}
fn simplify_discriminant(&mut self, place: VnIndex) -> Option<VnIndex> {
if let Value::Aggregate(enum_ty, variant, _) = *self.get(place)
&& enum_ty.is_enum()
{
let discr = self.ecx.discriminant_for_variant(enum_ty, variant).ok()?;
return Some(self.insert_scalar(discr.to_scalar(), discr.layout.ty));
}
None
}
}
fn op_to_prop_const<'tcx>(

View File

@ -115,8 +115,7 @@
- StorageLive(_18);
+ nop;
_18 = &mut _1;
- StorageLive(_19);
+ nop;
StorageLive(_19);
StorageLive(_20);
StorageLive(_21);
- _21 = move _18;
@ -124,11 +123,13 @@
+ _21 = _18;
+ _20 = S::<&mut impl Sized>(_18);
StorageDead(_21);
_19 = move (_20.0: &mut impl Sized);
- _19 = move (_20.0: &mut impl Sized);
+ _19 = _18;
StorageDead(_20);
StorageLive(_22);
StorageLive(_23);
_23 = &(*_19);
- _23 = &(*_19);
+ _23 = &(*_18);
_22 = opaque::<&impl Sized>(move _23) -> [return: bb9, unwind unreachable];
}
@ -137,7 +138,8 @@
StorageDead(_22);
StorageLive(_24);
StorageLive(_25);
_25 = &mut (*_19);
- _25 = &mut (*_19);
+ _25 = &mut (*_18);
_24 = opaque::<&mut impl Sized>(move _25) -> [return: bb10, unwind unreachable];
}
@ -146,7 +148,8 @@
StorageDead(_24);
StorageLive(_26);
StorageLive(_27);
_27 = &raw const (*_19);
- _27 = &raw const (*_19);
+ _27 = &raw const (*_18);
_26 = opaque::<*const impl Sized>(move _27) -> [return: bb11, unwind unreachable];
}
@ -155,7 +158,8 @@
StorageDead(_26);
StorageLive(_28);
StorageLive(_29);
_29 = &raw mut (*_19);
- _29 = &raw mut (*_19);
+ _29 = &raw mut (*_18);
_28 = opaque::<*mut impl Sized>(move _29) -> [return: bb12, unwind unreachable];
}
@ -163,9 +167,8 @@
StorageDead(_29);
StorageDead(_28);
_0 = const ();
- StorageDead(_19);
StorageDead(_19);
- StorageDead(_18);
+ nop;
+ nop;
drop(_1) -> [return: bb13, unwind unreachable];
}

View File

@ -115,8 +115,7 @@
- StorageLive(_18);
+ nop;
_18 = &mut _1;
- StorageLive(_19);
+ nop;
StorageLive(_19);
StorageLive(_20);
StorageLive(_21);
- _21 = move _18;
@ -124,11 +123,13 @@
+ _21 = _18;
+ _20 = S::<&mut impl Sized>(_18);
StorageDead(_21);
_19 = move (_20.0: &mut impl Sized);
- _19 = move (_20.0: &mut impl Sized);
+ _19 = _18;
StorageDead(_20);
StorageLive(_22);
StorageLive(_23);
_23 = &(*_19);
- _23 = &(*_19);
+ _23 = &(*_18);
_22 = opaque::<&impl Sized>(move _23) -> [return: bb9, unwind: bb14];
}
@ -137,7 +138,8 @@
StorageDead(_22);
StorageLive(_24);
StorageLive(_25);
_25 = &mut (*_19);
- _25 = &mut (*_19);
+ _25 = &mut (*_18);
_24 = opaque::<&mut impl Sized>(move _25) -> [return: bb10, unwind: bb14];
}
@ -146,7 +148,8 @@
StorageDead(_24);
StorageLive(_26);
StorageLive(_27);
_27 = &raw const (*_19);
- _27 = &raw const (*_19);
+ _27 = &raw const (*_18);
_26 = opaque::<*const impl Sized>(move _27) -> [return: bb11, unwind: bb14];
}
@ -155,7 +158,8 @@
StorageDead(_26);
StorageLive(_28);
StorageLive(_29);
_29 = &raw mut (*_19);
- _29 = &raw mut (*_19);
+ _29 = &raw mut (*_18);
_28 = opaque::<*mut impl Sized>(move _29) -> [return: bb12, unwind: bb14];
}
@ -163,9 +167,8 @@
StorageDead(_29);
StorageDead(_28);
_0 = const ();
- StorageDead(_19);
StorageDead(_19);
- StorageDead(_18);
+ nop;
+ nop;
drop(_1) -> [return: bb13, unwind: bb15];
}

View File

@ -112,7 +112,8 @@
StorageDead(_5);
StorageLive(_7);
StorageLive(_8);
StorageLive(_9);
- StorageLive(_9);
+ nop;
StorageLive(_10);
StorageLive(_11);
_11 = &(*_1);
@ -122,7 +123,8 @@
bb3: {
StorageDead(_11);
_9 = &_10;
StorageLive(_12);
- StorageLive(_12);
+ nop;
StorageLive(_13);
StorageLive(_14);
- _14 = &(*_4);
@ -133,20 +135,25 @@
bb4: {
StorageDead(_14);
_12 = &_13;
_8 = (move _9, move _12);
StorageDead(_12);
StorageDead(_9);
- StorageLive(_15);
- _8 = (move _9, move _12);
- StorageDead(_12);
- StorageDead(_9);
+ _8 = (_9, _12);
+ nop;
_15 = (_8.0: &*const u8);
- StorageLive(_16);
+ nop;
_16 = (_8.1: &*const u8);
StorageLive(_15);
- _15 = (_8.0: &*const u8);
+ _15 = _9;
StorageLive(_16);
- _16 = (_8.1: &*const u8);
+ _16 = _12;
StorageLive(_17);
StorageLive(_18);
_18 = (*_15);
- _18 = (*_15);
+ _18 = (*_9);
StorageLive(_19);
_19 = (*_16);
- _19 = (*_16);
+ _19 = (*_12);
_17 = Eq(move _18, move _19);
switchInt(move _17) -> [0: bb6, otherwise: bb5];
}
@ -156,10 +163,8 @@
StorageDead(_18);
_7 = const ();
StorageDead(_17);
- StorageDead(_16);
- StorageDead(_15);
+ nop;
+ nop;
StorageDead(_16);
StorageDead(_15);
StorageDead(_13);
StorageDead(_10);
StorageDead(_8);
@ -190,13 +195,15 @@
+ _23 = const core::panicking::AssertKind::Eq;
StorageLive(_24);
- StorageLive(_25);
- _25 = &(*_15);
+ nop;
_25 = &(*_15);
+ _25 = &(*_9);
_24 = &(*_25);
StorageLive(_26);
- StorageLive(_27);
- _27 = &(*_16);
+ nop;
_27 = &(*_16);
+ _27 = &(*_12);
_26 = &(*_27);
StorageLive(_28);
_28 = Option::<Arguments<'_>>::None;
@ -209,7 +216,8 @@
StorageDead(_31);
StorageLive(_33);
StorageLive(_34);
StorageLive(_35);
- StorageLive(_35);
+ nop;
StorageLive(_36);
StorageLive(_37);
_37 = &(*_1);
@ -219,7 +227,8 @@
bb8: {
StorageDead(_37);
_35 = &_36;
StorageLive(_38);
- StorageLive(_38);
+ nop;
StorageLive(_39);
StorageLive(_40);
_40 = &(*_29);
@ -229,20 +238,25 @@
bb9: {
StorageDead(_40);
_38 = &_39;
_34 = (move _35, move _38);
StorageDead(_38);
StorageDead(_35);
- StorageLive(_41);
- _34 = (move _35, move _38);
- StorageDead(_38);
- StorageDead(_35);
+ _34 = (_35, _38);
+ nop;
_41 = (_34.0: &*const u8);
- StorageLive(_42);
+ nop;
_42 = (_34.1: &*const u8);
StorageLive(_41);
- _41 = (_34.0: &*const u8);
+ _41 = _35;
StorageLive(_42);
- _42 = (_34.1: &*const u8);
+ _42 = _38;
StorageLive(_43);
StorageLive(_44);
_44 = (*_41);
- _44 = (*_41);
+ _44 = (*_35);
StorageLive(_45);
_45 = (*_42);
- _45 = (*_42);
+ _45 = (*_38);
_43 = Eq(move _44, move _45);
switchInt(move _43) -> [0: bb11, otherwise: bb10];
}
@ -252,10 +266,8 @@
StorageDead(_44);
_33 = const ();
StorageDead(_43);
- StorageDead(_42);
- StorageDead(_41);
+ nop;
+ nop;
StorageDead(_42);
StorageDead(_41);
StorageDead(_39);
StorageDead(_36);
StorageDead(_34);
@ -282,13 +294,15 @@
+ _49 = const core::panicking::AssertKind::Eq;
StorageLive(_50);
- StorageLive(_51);
- _51 = &(*_41);
+ nop;
_51 = &(*_41);
+ _51 = &(*_35);
_50 = &(*_51);
StorageLive(_52);
- StorageLive(_53);
- _53 = &(*_42);
+ nop;
_53 = &(*_42);
+ _53 = &(*_38);
_52 = &(*_53);
StorageLive(_54);
_54 = Option::<Arguments<'_>>::None;

View File

@ -112,7 +112,8 @@
StorageDead(_5);
StorageLive(_7);
StorageLive(_8);
StorageLive(_9);
- StorageLive(_9);
+ nop;
StorageLive(_10);
StorageLive(_11);
_11 = &(*_1);
@ -122,7 +123,8 @@
bb3: {
StorageDead(_11);
_9 = &_10;
StorageLive(_12);
- StorageLive(_12);
+ nop;
StorageLive(_13);
StorageLive(_14);
- _14 = &(*_4);
@ -133,20 +135,25 @@
bb4: {
StorageDead(_14);
_12 = &_13;
_8 = (move _9, move _12);
StorageDead(_12);
StorageDead(_9);
- StorageLive(_15);
- _8 = (move _9, move _12);
- StorageDead(_12);
- StorageDead(_9);
+ _8 = (_9, _12);
+ nop;
_15 = (_8.0: &*const u8);
- StorageLive(_16);
+ nop;
_16 = (_8.1: &*const u8);
StorageLive(_15);
- _15 = (_8.0: &*const u8);
+ _15 = _9;
StorageLive(_16);
- _16 = (_8.1: &*const u8);
+ _16 = _12;
StorageLive(_17);
StorageLive(_18);
_18 = (*_15);
- _18 = (*_15);
+ _18 = (*_9);
StorageLive(_19);
_19 = (*_16);
- _19 = (*_16);
+ _19 = (*_12);
_17 = Eq(move _18, move _19);
switchInt(move _17) -> [0: bb6, otherwise: bb5];
}
@ -156,10 +163,8 @@
StorageDead(_18);
_7 = const ();
StorageDead(_17);
- StorageDead(_16);
- StorageDead(_15);
+ nop;
+ nop;
StorageDead(_16);
StorageDead(_15);
StorageDead(_13);
StorageDead(_10);
StorageDead(_8);
@ -190,13 +195,15 @@
+ _23 = const core::panicking::AssertKind::Eq;
StorageLive(_24);
- StorageLive(_25);
- _25 = &(*_15);
+ nop;
_25 = &(*_15);
+ _25 = &(*_9);
_24 = &(*_25);
StorageLive(_26);
- StorageLive(_27);
- _27 = &(*_16);
+ nop;
_27 = &(*_16);
+ _27 = &(*_12);
_26 = &(*_27);
StorageLive(_28);
_28 = Option::<Arguments<'_>>::None;
@ -209,7 +216,8 @@
StorageDead(_31);
StorageLive(_33);
StorageLive(_34);
StorageLive(_35);
- StorageLive(_35);
+ nop;
StorageLive(_36);
StorageLive(_37);
_37 = &(*_1);
@ -219,7 +227,8 @@
bb8: {
StorageDead(_37);
_35 = &_36;
StorageLive(_38);
- StorageLive(_38);
+ nop;
StorageLive(_39);
StorageLive(_40);
_40 = &(*_29);
@ -229,20 +238,25 @@
bb9: {
StorageDead(_40);
_38 = &_39;
_34 = (move _35, move _38);
StorageDead(_38);
StorageDead(_35);
- StorageLive(_41);
- _34 = (move _35, move _38);
- StorageDead(_38);
- StorageDead(_35);
+ _34 = (_35, _38);
+ nop;
_41 = (_34.0: &*const u8);
- StorageLive(_42);
+ nop;
_42 = (_34.1: &*const u8);
StorageLive(_41);
- _41 = (_34.0: &*const u8);
+ _41 = _35;
StorageLive(_42);
- _42 = (_34.1: &*const u8);
+ _42 = _38;
StorageLive(_43);
StorageLive(_44);
_44 = (*_41);
- _44 = (*_41);
+ _44 = (*_35);
StorageLive(_45);
_45 = (*_42);
- _45 = (*_42);
+ _45 = (*_38);
_43 = Eq(move _44, move _45);
switchInt(move _43) -> [0: bb11, otherwise: bb10];
}
@ -252,10 +266,8 @@
StorageDead(_44);
_33 = const ();
StorageDead(_43);
- StorageDead(_42);
- StorageDead(_41);
+ nop;
+ nop;
StorageDead(_42);
StorageDead(_41);
StorageDead(_39);
StorageDead(_36);
StorageDead(_34);
@ -282,13 +294,15 @@
+ _49 = const core::panicking::AssertKind::Eq;
StorageLive(_50);
- StorageLive(_51);
- _51 = &(*_41);
+ nop;
_51 = &(*_41);
+ _51 = &(*_35);
_50 = &(*_51);
StorageLive(_52);
- StorageLive(_53);
- _53 = &(*_42);
+ nop;
_53 = &(*_42);
+ _53 = &(*_38);
_52 = &(*_53);
StorageLive(_54);
_54 = Option::<Arguments<'_>>::None;

View File

@ -442,8 +442,7 @@
+ nop;
StorageDead(_52);
StorageLive(_55);
- StorageLive(_56);
+ nop;
StorageLive(_56);
StorageLive(_57);
StorageLive(_58);
_58 = _1;
@ -452,13 +451,12 @@
StorageDead(_58);
- _56 = (_57.0: u64);
- _55 = opaque::<u64>(move _56) -> [return: bb16, unwind unreachable];
+ _56 = (_53.0: u64);
+ _55 = opaque::<u64>(_56) -> [return: bb16, unwind unreachable];
+ _56 = _1;
+ _55 = opaque::<u64>(_1) -> [return: bb16, unwind unreachable];
}
bb16: {
- StorageDead(_56);
+ nop;
StorageDead(_56);
StorageDead(_57);
StorageDead(_55);
StorageLive(_59);
@ -729,8 +727,8 @@
StorageDead(_127);
- _125 = (_126.0: u64);
- _124 = opaque::<u64>(move _125) -> [return: bb30, unwind unreachable];
+ _125 = _56;
+ _124 = opaque::<u64>(_56) -> [return: bb30, unwind unreachable];
+ _125 = _1;
+ _124 = opaque::<u64>(_1) -> [return: bb30, unwind unreachable];
}
bb30: {

View File

@ -442,8 +442,7 @@
+ nop;
StorageDead(_52);
StorageLive(_55);
- StorageLive(_56);
+ nop;
StorageLive(_56);
StorageLive(_57);
StorageLive(_58);
_58 = _1;
@ -452,13 +451,12 @@
StorageDead(_58);
- _56 = (_57.0: u64);
- _55 = opaque::<u64>(move _56) -> [return: bb16, unwind continue];
+ _56 = (_53.0: u64);
+ _55 = opaque::<u64>(_56) -> [return: bb16, unwind continue];
+ _56 = _1;
+ _55 = opaque::<u64>(_1) -> [return: bb16, unwind continue];
}
bb16: {
- StorageDead(_56);
+ nop;
StorageDead(_56);
StorageDead(_57);
StorageDead(_55);
StorageLive(_59);
@ -729,8 +727,8 @@
StorageDead(_127);
- _125 = (_126.0: u64);
- _124 = opaque::<u64>(move _125) -> [return: bb30, unwind continue];
+ _125 = _56;
+ _124 = opaque::<u64>(_56) -> [return: bb30, unwind continue];
+ _125 = _1;
+ _124 = opaque::<u64>(_1) -> [return: bb30, unwind continue];
}
bb30: {

View File

@ -20,8 +20,10 @@
- _2 = Option::<T>::Some(move _3);
+ _2 = Option::<T>::Some(_1);
StorageDead(_3);
_4 = discriminant(_2);
switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2];
- _4 = discriminant(_2);
- switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2];
+ _4 = const 1_isize;
+ switchInt(const 1_isize) -> [0: bb1, 1: bb3, otherwise: bb2];
}
bb1: {
@ -34,12 +36,12 @@
}
bb3: {
- StorageLive(_5);
+ nop;
_5 = ((_2 as Some).0: T);
_0 = _5;
- StorageDead(_5);
+ nop;
StorageLive(_5);
- _5 = ((_2 as Some).0: T);
- _0 = _5;
+ _5 = _1;
+ _0 = _1;
StorageDead(_5);
StorageDead(_2);
return;
}

View File

@ -20,8 +20,10 @@
- _2 = Option::<T>::Some(move _3);
+ _2 = Option::<T>::Some(_1);
StorageDead(_3);
_4 = discriminant(_2);
switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2];
- _4 = discriminant(_2);
- switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2];
+ _4 = const 1_isize;
+ switchInt(const 1_isize) -> [0: bb1, 1: bb3, otherwise: bb2];
}
bb1: {
@ -34,12 +36,12 @@
}
bb3: {
- StorageLive(_5);
+ nop;
_5 = ((_2 as Some).0: T);
_0 = _5;
- StorageDead(_5);
+ nop;
StorageLive(_5);
- _5 = ((_2 as Some).0: T);
- _0 = _5;
+ _5 = _1;
+ _0 = _1;
StorageDead(_5);
StorageDead(_2);
return;
}