Skip to content

Handle discriminant in DataflowConstProp #107411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve value_analysis API.
  • Loading branch information
cjgillot committed Feb 6, 2023
commit 9af191f86f2c81ec5613ae35ab1a3b2ac3edbdee
25 changes: 18 additions & 7 deletions compiler/rustc_mir_dataflow/src/value_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,20 +735,31 @@ impl Map {
}

/// Locates the given place, if it exists in the tree.
pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
pub fn find_extra(
&self,
place: PlaceRef<'_>,
extra: impl IntoIterator<Item = TrackElem>,
) -> Option<PlaceIndex> {
let mut index = *self.locals.get(place.local)?.as_ref()?;

for &elem in place.projection {
index = self.apply(index, elem.try_into().ok()?)?;
}
for elem in extra {
index = self.apply(index, elem)?;
}

Some(index)
}

/// Locates the given place, if it exists in the tree.
pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
self.find_extra(place, [])
}

/// Locates the given place and applies `Discriminant`, if it exists in the tree.
pub fn find_discr(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
let index = self.find(place)?;
self.apply(index, TrackElem::Discriminant)
self.find_extra(place, [TrackElem::Discriminant])
}

/// Iterate over all direct children.
Expand All @@ -763,14 +774,14 @@ impl Map {
///
/// `tail_elem` allows to support discriminants that are not a place in MIR, but that we track
/// as such.
fn for_each_aliasing_place(
pub fn for_each_aliasing_place(
&self,
place: PlaceRef<'_>,
tail_elem: Option<TrackElem>,
f: &mut impl FnMut(PlaceIndex),
) {
let Some(&Some(mut index)) = self.locals.get(place.local) else {
// The local is not tracked at all, nothing to invalidate.
// The local is not tracked at all, so it does not alias anything.
return;
};
let elems = place
Expand All @@ -782,7 +793,7 @@ impl Map {
let Ok(elem) = elem else { return };
let sub = self.apply(index, elem);
if let TrackElem::Variant(..) | TrackElem::Discriminant = elem {
// Writing to an enum variant field invalidates the other variants and the discriminant.
// Enum variant fields and enum discriminants alias each another.
self.for_each_variant_sibling(index, sub, f);
}
if let Some(sub) = sub {
Expand All @@ -795,7 +806,7 @@ impl Map {
}

/// Invoke the given function on all the descendants of the given place, except one branch.
pub fn for_each_variant_sibling(
fn for_each_variant_sibling(
&self,
parent: PlaceIndex,
preserved_child: Option<PlaceIndex>,
Expand Down