Skip to content

Commit

Permalink
Move MIR utility methods to extension traits
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Nov 27, 2021
1 parent d3b0189 commit 9ec0271
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 352 deletions.
8 changes: 4 additions & 4 deletions crates/flowistry/src/indexed/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{DefaultDomain, IndexSet, IndexedDomain, IndexedValue, ToIndex};
use crate::{mir::utils, to_index_impl};
use crate::{mir::utils::PlaceExt, to_index_impl};
use rustc_data_structures::fx::{FxHashMap as HashMap, FxHashSet as HashSet};
use rustc_index::vec::IndexVec;
use rustc_infer::infer::TyCtxtInferExt;
Expand Down Expand Up @@ -65,7 +65,7 @@ impl NormalizedPlaces<'tcx> {
})
.collect::<Vec<_>>();

utils::mk_place(place.local, &projection, tcx)
Place::make(place.local, &projection, tcx)
})
}
}
Expand Down Expand Up @@ -105,7 +105,7 @@ impl PlaceDomain<'tcx> {
.domain
.as_vec()
.iter_enumerated()
.filter(|(_, place)| utils::is_arg(**place, body))
.filter(|(_, place)| place.is_arg(body))
.map(|(index, _)| index)
.collect()
}
Expand Down Expand Up @@ -181,7 +181,7 @@ impl LocationDomain {
let (arg_places, arg_locations): (Vec<_>, Vec<_>) = place_domain
.as_vec()
.iter()
.filter(|place| utils::is_arg(**place, body))
.filter(|place| place.is_arg(body))
.enumerate()
.map(|(i, place)| {
(
Expand Down
15 changes: 8 additions & 7 deletions crates/flowistry/src/infoflow/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
aliases::Aliases,
borrowck_facts::get_body_with_borrowck_facts,
control_dependencies::ControlDependencies,
utils::{self, PlaceCollector},
utils::{self, OperandExt, PlaceCollector, PlaceExt},
},
};
use log::{debug, info};
Expand Down Expand Up @@ -89,7 +89,7 @@ impl TransferFunction<'_, '_, 'tcx> {

let terminator = body.basic_blocks()[block].terminator();
if let TerminatorKind::SwitchInt { discr, .. } = &terminator.kind {
if let Some(discr_place) = utils::operand_to_place(discr) {
if let Some(discr_place) = discr.to_place() {
add_deps(discr_place, &mut input_location_deps);
}
}
Expand Down Expand Up @@ -259,7 +259,7 @@ impl TransferFunction<'_, '_, 'tcx> {
}
}

if !utils::is_arg(child, body) || (mutated && !child.is_indirect()) {
if !child.is_arg(body) || (mutated && !child.is_indirect()) {
return None;
}

Expand All @@ -276,11 +276,11 @@ impl TransferFunction<'_, '_, 'tcx> {

let mut projection = parent_toplevel_arg.projection.to_vec();
projection.extend_from_slice(child.projection);
let parent_arg_projected = utils::mk_place(parent_toplevel_arg.local, &projection, tcx);
let parent_arg_projected = Place::make(parent_toplevel_arg.local, &projection, tcx);

let parent_arg_accessible = {
let mut sub_places = (0..=parent_arg_projected.projection.len()).rev().map(|i| {
utils::mk_place(
Place::make(
parent_arg_projected.local,
&parent_arg_projected.projection[..i],
tcx,
Expand Down Expand Up @@ -356,7 +356,8 @@ impl Visitor<'tcx> for TransferFunction<'a, 'b, 'tcx> {
}

let inputs_for_arg = |arg: Place<'tcx>| {
utils::interior_pointers(arg, tcx, self.analysis.body, self.analysis.def_id)
arg
.interior_pointers(tcx, self.analysis.body, self.analysis.def_id)
.into_values()
.map(|places| {
places
Expand Down Expand Up @@ -393,7 +394,7 @@ impl Visitor<'tcx> for TransferFunction<'a, 'b, 'tcx> {
}

TerminatorKind::DropAndReplace { place, value, .. } => {
if let Some(src) = utils::operand_to_place(value) {
if let Some(src) = value.to_place() {
self.apply_mutation(*place, &[src], location, true, false);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/flowistry/src/infoflow/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
impls::{LocationSet, PlaceIndex, PlaceSet},
IndexedDomain,
},
mir::utils,
mir::utils::PlaceExt,
source_map::{location_to_spans, HirSpanner},
};
use log::{debug, trace};
Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn compute_dependencies(
let mut places = new_place_set();
places.insert(*place);

for (_, ptrs) in utils::interior_pointers(*place, tcx, body, results.analysis.def_id) {
for (_, ptrs) in place.interior_pointers(tcx, body, results.analysis.def_id) {
for (place, _) in ptrs {
debug!(
"{:?} // {:?}",
Expand Down
7 changes: 2 additions & 5 deletions crates/flowistry/src/infoflow/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
block_timer,
indexed::impls::LocationDomain,
mir::{aliases::Aliases, control_dependencies::ControlDependencies, engine, utils},
mir::{aliases::Aliases, control_dependencies::ControlDependencies, engine, utils::BodyExt},
};
use log::debug;
use rustc_borrowck::consumers::BodyWithBorrowckFacts;
Expand Down Expand Up @@ -29,10 +29,7 @@ pub fn compute_flow<'a, 'tcx>(
) -> FlowResults<'a, 'tcx> {
BODY_STACK.with(|body_stack| {
body_stack.borrow_mut().push(body_id);
debug!(
"{}",
utils::mir_to_string(tcx, &body_with_facts.body).unwrap()
);
debug!("{}", body_with_facts.body.to_string(tcx).unwrap());

let def_id = tcx.hir().body_owner_def_id(body_id).to_def_id();
let aliases = Aliases::build(tcx, def_id, body_with_facts);
Expand Down
2 changes: 1 addition & 1 deletion crates/flowistry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ pub mod infoflow;
pub mod mir;
pub mod source_map;
pub mod test_utils;
pub mod timer;
pub mod timer;
31 changes: 16 additions & 15 deletions crates/flowistry/src/mir/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
impls::{NormalizedPlaces, PlaceDomain, PlaceIndex, PlaceSet},
IndexMatrix, IndexSetIteratorExt, IndexedDomain, ToIndex,
},
mir::utils::{self, PlaceRelation},
mir::utils::{self, PlaceExt, PlaceRelation},
};
use log::{debug, info, trace};
use rustc_borrowck::consumers::BodyWithBorrowckFacts;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Visitor<'tcx> for FindPlaces<'_, 'tcx> {
// this is needed for eval? not sure why locals wouldn't show up in the body as places,
// maybe optimized out or something
fn visit_local_decl(&mut self, local: Local, _local_decl: &LocalDecl<'tcx>) {
self.places.push(utils::local_to_place(local, self.tcx));
self.places.push(Place::from_local(local, self.tcx));
}

fn visit_place(&mut self, place: &Place<'tcx>, _context: PlaceContext, _location: Location) {
Expand Down Expand Up @@ -175,8 +175,9 @@ impl Aliases<'tcx> {
let _abstract_regions = body
.args_iter()
.map(|local| {
let arg = utils::local_to_place(local, tcx);
utils::interior_pointers(arg, tcx, body, def_id).into_keys()
Place::from_local(local, tcx)
.interior_pointers(tcx, body, def_id)
.into_keys()
})
.flatten()
.collect::<HashSet<_>>();
Expand Down Expand Up @@ -260,12 +261,13 @@ impl Aliases<'tcx> {
tcx: TyCtxt<'tcx>,
) -> HashSet<Place<'tcx>> {
let aliases = all_aliases.row(place).copied();
let ptr_deps = utils::pointers_in_place(place, tcx)
let ptr_deps = place
.pointers_in_projection(tcx)
.into_iter()
.map(|ptr| Self::place_deps(ptr, all_aliases, body, tcx))
.flatten();

let maybe_place = if utils::is_direct(place, body) {
let maybe_place = if place.is_direct(body) {
vec![place]
} else {
vec![]
Expand Down Expand Up @@ -299,7 +301,7 @@ impl Aliases<'tcx> {
for (place, aliases) in all_aliases.into_iter() {
let direct_aliases = aliases
.iter()
.filter(|alias| utils::is_direct(**alias, body))
.filter(|alias| alias.is_direct(body))
.copied()
.collect_indices(place_domain.clone());
aliases_map.union_into_row(place, &direct_aliases);
Expand All @@ -318,8 +320,7 @@ impl Aliases<'tcx> {
.iter_enumerated()
.filter_map(move |(idx, other_place)| {
let relation = PlaceRelation::of(*other_place, *place);
(relation.overlaps() && utils::is_direct(*other_place, body))
.then(move || (relation, idx))
(relation.overlaps() && other_place.is_direct(body)).then(move || (relation, idx))
})
.partition(|(relation, _)| match relation {
PlaceRelation::Sub => true,
Expand Down Expand Up @@ -358,7 +359,7 @@ impl Aliases<'tcx> {
let mut aliases = HashSet::default();
aliases.insert(place);

if utils::is_direct(place, body) {
if place.is_direct(body) {
return aliases;
}

Expand All @@ -371,7 +372,7 @@ impl Aliases<'tcx> {
.find(|(_, elem)| matches!(elem, ProjectionElem::Deref))
.unwrap();

let ptr = utils::mk_place(place.local, &place.projection[..deref_index], tcx);
let ptr = Place::make(place.local, &place.projection[..deref_index], tcx);
let projection_past_deref = &place.projection[deref_index + 1..];

let (region, orig_ty) = match ptr.ty(body.local_decls(), tcx).ty.kind() {
Expand All @@ -390,7 +391,7 @@ impl Aliases<'tcx> {
if TyS::same_type(orig_ty, loan_ty) {
let mut projection = loan.projection.to_vec();
projection.extend(projection_past_deref);
utils::mk_place(loan.local, &projection, tcx)
Place::make(loan.local, &projection, tcx)
} else {
*loan
}
Expand All @@ -417,7 +418,7 @@ impl Aliases<'tcx> {
// For every place p = *q, add q
let all_pointers = all_places
.iter()
.map(|place| utils::pointers_in_place(*place, tcx))
.map(|place| place.pointers_in_projection(tcx))
.flatten()
.collect::<Vec<_>>();
all_places.extend(all_pointers);
Expand Down Expand Up @@ -512,8 +513,8 @@ impl Aliases<'tcx> {
let all_locals = body.local_decls().indices();
let all_pointers = all_locals
.map(|local| {
let place = utils::local_to_place(local, tcx);
utils::interior_pointers(place, tcx, body, def_id)
let place = Place::from_local(local, tcx);
place.interior_pointers(tcx, body, def_id)
})
.flatten();
let mut region_to_pointers: HashMap<_, Vec<_>> = HashMap::default();
Expand Down
Loading

0 comments on commit 9ec0271

Please sign in to comment.