From 7ab1ba95debdacdd4fc205510a0bb402b4428e11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Christopher=20K=C3=B6hl?= Date: Wed, 5 Oct 2022 22:01:33 +0200 Subject: [PATCH] Remove `Unknown` state in favor of `Value(Top)` --- .../rustc_mir_dataflow/src/value_analysis.rs | 31 +++++++++++-------- .../src/dataflow_const_prop.rs | 10 ++---- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_mir_dataflow/src/value_analysis.rs b/compiler/rustc_mir_dataflow/src/value_analysis.rs index 766a659adca5..924d3d72a524 100644 --- a/compiler/rustc_mir_dataflow/src/value_analysis.rs +++ b/compiler/rustc_mir_dataflow/src/value_analysis.rs @@ -183,15 +183,15 @@ pub trait ValueAnalysis<'tcx> { .map() .find(place.as_ref()) .map(ValueOrPlaceOrRef::Ref) - .unwrap_or(ValueOrPlaceOrRef::Unknown), + .unwrap_or(ValueOrPlaceOrRef::top()), Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => { state.flood(place.as_ref(), self.map()); - ValueOrPlaceOrRef::Unknown + ValueOrPlaceOrRef::top() } Rvalue::CopyForDeref(place) => { self.handle_operand(&Operand::Copy(*place), state).into() } - _ => ValueOrPlaceOrRef::Unknown, + _ => ValueOrPlaceOrRef::top(), } } @@ -218,7 +218,7 @@ pub trait ValueAnalysis<'tcx> { self.map() .find(place.as_ref()) .map(ValueOrPlace::Place) - .unwrap_or(ValueOrPlace::Unknown) + .unwrap_or(ValueOrPlace::top()) } } } @@ -511,9 +511,6 @@ impl State { self.assign_place_idx(target_deref, source, map); } } - ValueOrPlaceOrRef::Unknown => { - self.flood_idx(target, map); - } } } @@ -756,27 +753,35 @@ impl<'a> Iterator for Children<'a> { } } } - -// FIXME: See if we can get rid of `Unknown`. pub enum ValueOrPlace { Value(V), Place(PlaceIndex), - Unknown, +} + +impl ValueOrPlace { + pub fn top() -> Self { + ValueOrPlace::Value(V::top()) + } } pub enum ValueOrPlaceOrRef { Value(V), Place(PlaceIndex), - Ref(PlaceIndex), - Unknown, + Ref(PlaceIndex) } +impl ValueOrPlaceOrRef { + pub fn top() -> Self { + ValueOrPlaceOrRef::Value(V::top()) + } +} + + impl From> for ValueOrPlaceOrRef { fn from(x: ValueOrPlace) -> Self { match x { ValueOrPlace::Value(value) => ValueOrPlaceOrRef::Value(value), ValueOrPlace::Place(place) => ValueOrPlaceOrRef::Place(place), - ValueOrPlace::Unknown => ValueOrPlaceOrRef::Unknown, } } } diff --git a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs index 334af0c6e8e6..a6f5f71d0919 100644 --- a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs +++ b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs @@ -105,8 +105,8 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> { .ecx .misc_cast(&operand, *ty) .map(|result| ValueOrPlaceOrRef::Value(self.wrap_immediate(result, *ty))) - .unwrap_or(ValueOrPlaceOrRef::Unknown), - _ => ValueOrPlaceOrRef::Unknown, + .unwrap_or(ValueOrPlaceOrRef::top()), + _ => ValueOrPlaceOrRef::top(), } } Rvalue::BinaryOp(op, box (left, right)) => { @@ -156,7 +156,6 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'tcx> { let value = match self.handle_operand(discr, state) { ValueOrPlace::Value(value) => value, ValueOrPlace::Place(place) => state.get_idx(place, self.map()), - ValueOrPlace::Unknown => FlatSet::Top, }; let result = match value { FlatSet::Top => FlatSet::Top, @@ -241,7 +240,6 @@ impl<'tcx> ConstAnalysis<'tcx> { let value = match self.handle_operand(op, state) { ValueOrPlace::Value(value) => value, ValueOrPlace::Place(place) => state.get_idx(place, &self.map), - ValueOrPlace::Unknown => FlatSet::Top, }; match value { FlatSet::Top => FlatSet::Top, @@ -384,9 +382,7 @@ impl<'tcx, 'map, 'a> Visitor<'tcx> for OperandCollector<'tcx, 'map, 'a> { FlatSet::Elem(value) => { self.visitor.before_effect.insert((location, *place), value); } - FlatSet::Bottom => { - // This only happens if this location is unreachable. - } + FlatSet::Bottom => (), } } _ => (),