Skip to content
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

Add a Use-Definition Chain implementation for the MIR. #62547

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
58 changes: 49 additions & 9 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1889,25 +1889,49 @@ impl<'tcx> Place<'tcx> {
}
}

/// Finds the innermost `PlaceBase` for this `Place`.
pub fn base(&self) -> &PlaceBase<'tcx> {
&self.base
}

/// Finds the innermost `PlaceBase` for this `Place`, or `None` if the target of this `Place`
/// is behind an indirection.
pub fn base_direct(&self) -> Option<&PlaceBase<'tcx>> {
self.iterate(|base, projections| {
for proj in projections {
if let PlaceElem::Deref = proj.elem {
return None;
}
}

Some(base)
})
}

/// Finds the innermost `Local` from this `Place`.
pub fn base_local(&self) -> Option<Local> {
self.base().local()
}

/// Recursively "iterates" over place components, generating a `PlaceBase` and
/// `Projections` list and invoking `op` with a `ProjectionsIter`.
pub fn iterate<R>(
&self,
op: impl FnOnce(&PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
pub fn iterate<'a, R: 'a>(
&'a self,
op: impl FnOnce(&'a PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
) -> R {
Place::iterate_over(&self.base, &self.projection, op)
}

pub fn iterate_over<R>(
place_base: &PlaceBase<'tcx>,
pub fn iterate_over<'a, R: 'a>(
place_base: &'a PlaceBase<'tcx>,
place_projection: &Option<Box<Projection<'tcx>>>,
op: impl FnOnce(&PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
op: impl FnOnce(&'a PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
) -> R {
fn iterate_over2<'tcx, R>(
place_base: &PlaceBase<'tcx>,
fn iterate_over2<'a, 'tcx, R: 'a>(
place_base: &'a PlaceBase<'tcx>,
place_projection: &Option<Box<Projection<'tcx>>>,
next: &Projections<'_, 'tcx>,
op: impl FnOnce(&PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
op: impl FnOnce(&'a PlaceBase<'tcx>, ProjectionsIter<'_, 'tcx>) -> R,
) -> R {
match place_projection {
None => {
Expand Down Expand Up @@ -1984,6 +2008,15 @@ impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
}
}

impl PlaceBase<'_> {
pub fn local(&self) -> Option<Local> {
match *self {
PlaceBase::Local(local) => Some(local),
PlaceBase::Static(_) => None,
}
}
}

/// A linked list of projections running up the stack; begins with the
/// innermost projection and extends to the outermost (e.g., `a.b.c`
/// would have the place `b` with a "next" pointer to `b.c`).
Expand Down Expand Up @@ -2209,6 +2242,13 @@ impl<'tcx> Operand<'tcx> {
Operand::Move(ref place) => Operand::Copy(place.clone()),
}
}

pub fn place(&self) -> Option<&Place<'tcx>> {
match self {
Operand::Copy(place) | Operand::Move(place) => Some(place),
Operand::Constant(_) => None,
}
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down
24 changes: 5 additions & 19 deletions src/librustc_mir/borrow_check/path_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::borrow_check::borrow_set::{BorrowSet, BorrowData, TwoPhaseActivation}
use crate::borrow_check::places_conflict;
use crate::borrow_check::AccessDepth;
use crate::dataflow::indexes::BorrowIndex;
use rustc::mir::{BasicBlock, Location, Body, Place, PlaceBase};
use rustc::mir::{ProjectionElem, BorrowKind};
use rustc::mir::{BasicBlock, BorrowKind, Location, Body, Place, PlaceBase};
use rustc::ty::{self, TyCtxt};
use rustc_data_structures::graph::dominators::Dominators;

Expand Down Expand Up @@ -132,21 +131,8 @@ pub(super) fn is_active<'tcx>(

/// Determines if a given borrow is borrowing local data
/// This is called for all Yield statements on movable generators
pub(super) fn borrow_of_local_data(place: &Place<'_>) -> bool {
place.iterate(|place_base, place_projection| {
match place_base {
PlaceBase::Static(..) => return false,
PlaceBase::Local(..) => {},
}

for proj in place_projection {
// Reborrow of already borrowed data is ignored
// Any errors will be caught on the initial borrow
if proj.elem == ProjectionElem::Deref {
return false;
}
}

true
})
pub(super) fn borrow_of_local_data<'tcx>(place: &Place<'tcx>) -> bool {
place.base_direct()
.and_then(PlaceBase::local)
.is_some()
}
20 changes: 2 additions & 18 deletions src/librustc_mir/dataflow/impls/borrowed_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a, 'tcx> BitDenotation<'tcx> for HaveBeenBorrowedLocals<'a, 'tcx> {
// Drop terminators borrows the location
TerminatorKind::Drop { location, .. } |
TerminatorKind::DropAndReplace { location, .. } => {
if let Some(local) = find_local(location) {
if let Some(local) = location.base_direct().and_then(PlaceBase::local) {
trans.gen(local);
}
}
Expand Down Expand Up @@ -92,28 +92,12 @@ struct BorrowedLocalsVisitor<'gk> {
trans: &'gk mut GenKillSet<Local>,
}

fn find_local(place: &Place<'_>) -> Option<Local> {
place.iterate(|place_base, place_projection| {
for proj in place_projection {
if proj.elem == ProjectionElem::Deref {
return None;
}
}

if let PlaceBase::Local(local) = place_base {
Some(*local)
} else {
None
}
})
}

impl<'tcx> Visitor<'tcx> for BorrowedLocalsVisitor<'_> {
fn visit_rvalue(&mut self,
rvalue: &Rvalue<'tcx>,
location: Location) {
if let Rvalue::Ref(_, _, ref place) = *rvalue {
if let Some(local) = find_local(place) {
if let Some(local) = place.base_direct().and_then(PlaceBase::local) {
self.trans.gen(local);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_mir/dataflow/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ use super::drop_flag_effects_for_function_entry;
use super::drop_flag_effects_for_location;
use super::on_lookup_result_bits;

mod reaching_defs;
mod use_def_chain;

pub use self::reaching_defs::ReachingDefinitions;
pub use self::use_def_chain::UseDefChain;

mod storage_liveness;

pub use self::storage_liveness::*;
Expand Down
Loading