Skip to content

Commit df889c9

Browse files
committed
Rename assign_idx methods.
1 parent 67a8c16 commit df889c9

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

compiler/rustc_mir_dataflow/src/value_analysis.rs

+32-23
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! - The bottom state denotes uninitialized memory. Because we are only doing a sound approximation
2525
//! of the actual execution, we can also use this state for places where access would be UB.
2626
//!
27-
//! - The assignment logic in `State::assign_place_idx` assumes that the places are non-overlapping,
27+
//! - The assignment logic in `State::insert_place_idx` assumes that the places are non-overlapping,
2828
//! or identical. Note that this refers to place expressions, not memory locations.
2929
//!
3030
//! - Currently, places that have their reference taken cannot be tracked. Although this would be
@@ -470,59 +470,68 @@ impl<V: Clone + HasTop + HasBottom> State<V> {
470470
self.flood_discr_with(place, map, V::top())
471471
}
472472

473+
/// Low-level method that assigns to a place.
474+
/// This does nothing if the place is not tracked.
475+
///
476+
/// The target place must have been flooded before calling this method.
477+
pub fn insert_idx(&mut self, target: PlaceIndex, result: ValueOrPlace<V>, map: &Map) {
478+
match result {
479+
ValueOrPlace::Value(value) => self.insert_value_idx(target, value, map),
480+
ValueOrPlace::Place(source) => self.insert_place_idx(target, source, map),
481+
}
482+
}
483+
484+
/// Low-level method that assigns a value to a place.
485+
/// This does nothing if the place is not tracked.
486+
///
487+
/// The target place must have been flooded before calling this method.
488+
pub fn insert_value_idx(&mut self, target: PlaceIndex, value: V, map: &Map) {
489+
let StateData::Reachable(values) = &mut self.0 else { return };
490+
if let Some(value_index) = map.places[target].value_index {
491+
values[value_index] = value;
492+
}
493+
}
494+
473495
/// Copies `source` to `target`, including all tracked places beneath.
474496
///
475497
/// If `target` contains a place that is not contained in `source`, it will be overwritten with
476498
/// Top. Also, because this will copy all entries one after another, it may only be used for
477499
/// places that are non-overlapping or identical.
478500
///
479501
/// The target place must have been flooded before calling this method.
480-
fn assign_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map) {
502+
fn insert_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map) {
481503
let StateData::Reachable(values) = &mut self.0 else { return };
482504

483-
// If both places are tracked, we copy the value to the target. If the target is tracked,
484-
// but the source is not, we have to invalidate the value in target. If the target is not
485-
// tracked, then we don't have to do anything.
505+
// If both places are tracked, we copy the value to the target.
506+
// If the target is tracked, but the source is not, we do nothing, as invalidation has
507+
// already been performed.
486508
if let Some(target_value) = map.places[target].value_index {
487509
if let Some(source_value) = map.places[source].value_index {
488510
values[target_value] = values[source_value].clone();
489-
} else {
490-
values[target_value] = V::top();
491511
}
492512
}
493513
for target_child in map.children(target) {
494514
// Try to find corresponding child and recurse. Reasoning is similar as above.
495515
let projection = map.places[target_child].proj_elem.unwrap();
496516
if let Some(source_child) = map.projections.get(&(source, projection)) {
497-
self.assign_place_idx(target_child, *source_child, map);
517+
self.insert_place_idx(target_child, *source_child, map);
498518
}
499519
}
500520
}
501521

522+
/// Helper method to interpret `target = result`.
502523
pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map) {
503524
self.flood(target, map);
504525
if let Some(target) = map.find(target) {
505-
self.assign_idx(target, result, map);
526+
self.insert_idx(target, result, map);
506527
}
507528
}
508529

530+
/// Helper method for assignments to a discriminant.
509531
pub fn assign_discr(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map) {
510532
self.flood_discr(target, map);
511533
if let Some(target) = map.find_discr(target) {
512-
self.assign_idx(target, result, map);
513-
}
514-
}
515-
516-
/// The target place must have been flooded before calling this method.
517-
pub fn assign_idx(&mut self, target: PlaceIndex, result: ValueOrPlace<V>, map: &Map) {
518-
match result {
519-
ValueOrPlace::Value(value) => {
520-
let StateData::Reachable(values) = &mut self.0 else { return };
521-
if let Some(value_index) = map.places[target].value_index {
522-
values[value_index] = value;
523-
}
524-
}
525-
ValueOrPlace::Place(source) => self.assign_place_idx(target, source, map),
534+
self.insert_idx(target, result, map);
526535
}
527536
}
528537

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
144144
.apply(target, TrackElem::Field(Field::from_usize(field_index)))
145145
{
146146
let result = self.handle_operand(operand, state);
147-
state.assign_idx(field, result, self.map());
147+
state.insert_idx(field, result, self.map());
148148
}
149149
}
150150
}
@@ -153,12 +153,13 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
153153
{
154154
let enum_ty = target.ty(self.local_decls, self.tcx).ty;
155155
if let Some(discr_val) = self.eval_disciminant(enum_ty, variant_index) {
156-
state.assign_idx(discr_idx, ValueOrPlace::Value(FlatSet::Elem(discr_val)), &self.map);
156+
state.insert_value_idx(discr_idx, FlatSet::Elem(discr_val), &self.map);
157157
}
158158
}
159159
}
160160
}
161161
Rvalue::CheckedBinaryOp(op, box (left, right)) => {
162+
// Flood everything now, so we can use `insert_value_idx` directly later.
162163
state.flood(target.as_ref(), self.map());
163164

164165
let target = self.map().find(target.as_ref());
@@ -172,7 +173,8 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
172173
let (val, overflow) = self.binary_op(state, *op, left, right);
173174

174175
if let Some(value_target) = value_target {
175-
state.assign_idx(value_target, ValueOrPlace::Value(val), self.map());
176+
// We have flooded `target` earlier.
177+
state.insert_value_idx(value_target, val, self.map());
176178
}
177179
if let Some(overflow_target) = overflow_target {
178180
let overflow = match overflow {
@@ -187,11 +189,8 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
187189
}
188190
FlatSet::Bottom => FlatSet::Bottom,
189191
};
190-
state.assign_idx(
191-
overflow_target,
192-
ValueOrPlace::Value(overflow),
193-
self.map(),
194-
);
192+
// We have flooded `target` earlier.
193+
state.insert_value_idx(overflow_target, overflow, self.map());
195194
}
196195
}
197196
}

0 commit comments

Comments
 (0)