Skip to content

Commit fc87640

Browse files
committed
gen Locals with NodeKind::LocalWithRefs in liveness analysis
1 parent 5168a8a commit fc87640

File tree

1 file changed

+73
-50
lines changed

1 file changed

+73
-50
lines changed

compiler/rustc_mir_dataflow/src/impls/live_borrows.rs

Lines changed: 73 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BorrowDependencies<'a, 'tcx> {
265265
src_node_idx,
266266
self.dep_graph.node(src_node_idx).data,
267267
node_idx,
268-
borrowed_local
268+
place.local
269269
);
270270

271271
self.dep_graph.add_edge(src_node_idx, node_idx, ());
@@ -359,42 +359,8 @@ impl<'mir, 'tcx> BorrowedLocalsResults<'mir, 'tcx>
359359
where
360360
'tcx: 'mir,
361361
{
362-
fn new(
363-
tcx: TyCtxt<'tcx>,
364-
body: &'mir Body<'tcx>,
365-
borrows_analysis_results: Results<'tcx, LiveBorrows<'mir, 'tcx>>,
366-
) -> Self {
367-
let mut borrow_deps = BorrowDependencies::new(body.local_decls(), tcx);
368-
borrow_deps.visit_body(body);
369-
370-
if cfg!(debug_assertions) {
371-
let dep_graph = &borrow_deps.dep_graph;
372-
373-
debug!(
374-
"nodes: {:#?}",
375-
dep_graph
376-
.all_nodes()
377-
.clone()
378-
.into_iter()
379-
.enumerate()
380-
.map(|(i, node)| (i, node.data))
381-
.collect::<Vec<_>>()
382-
);
383-
384-
debug!("edges:");
385-
for edge in dep_graph.all_edges() {
386-
let src_node_idx = edge.source();
387-
let src_node = dep_graph.node(src_node_idx);
388-
let target_node_idx = edge.target();
389-
let target_node = dep_graph.node(target_node_idx);
390-
debug!(
391-
"{:?}({:?}) -> {:?}({:?}) ({:?})",
392-
src_node_idx, src_node.data, target_node_idx, target_node.data, edge.data
393-
)
394-
}
395-
}
396-
397-
let dep_graph = &borrow_deps.dep_graph;
362+
fn new(borrows_analysis_results: Results<'tcx, LiveBorrows<'mir, 'tcx>>) -> Self {
363+
let dep_graph = &borrows_analysis_results.analysis.borrow_deps.dep_graph;
398364
let borrowed_local_to_locals_to_keep_alive = Self::get_locals_to_keep_alive_map(dep_graph);
399365
Self { borrows_analysis_results, borrowed_local_to_locals_to_keep_alive }
400366
}
@@ -467,11 +433,41 @@ pub fn get_borrowed_locals_results<'mir, 'tcx>(
467433
tcx: TyCtxt<'tcx>,
468434
) -> BorrowedLocalsResults<'mir, 'tcx> {
469435
debug!("body: {:#?}", body);
470-
let live_borrows = LiveBorrows::new(body, tcx);
436+
437+
let mut borrow_deps = BorrowDependencies::new(body.local_decls(), tcx);
438+
borrow_deps.visit_body(body);
439+
440+
if cfg!(debug_assertions) {
441+
let dep_graph = &borrow_deps.dep_graph;
442+
443+
debug!(
444+
"nodes: {:#?}",
445+
dep_graph
446+
.all_nodes()
447+
.clone()
448+
.into_iter()
449+
.enumerate()
450+
.map(|(i, node)| (i, node.data))
451+
.collect::<Vec<_>>()
452+
);
453+
454+
debug!("edges:");
455+
for edge in dep_graph.all_edges() {
456+
let src_node_idx = edge.source();
457+
let src_node = dep_graph.node(src_node_idx);
458+
let target_node_idx = edge.target();
459+
let target_node = dep_graph.node(target_node_idx);
460+
debug!(
461+
"{:?}({:?}) -> {:?}({:?}) ({:?})",
462+
src_node_idx, src_node.data, target_node_idx, target_node.data, edge.data
463+
)
464+
}
465+
}
466+
let live_borrows = LiveBorrows::new(body, tcx, borrow_deps);
471467
let results =
472468
live_borrows.into_engine(tcx, body).pass_name("borrowed_locals").iterate_to_fixpoint();
473469

474-
BorrowedLocalsResults::new(tcx, body, results)
470+
BorrowedLocalsResults::new(results)
475471
}
476472

477473
pub struct BorrowedLocalsResultsCursor<'a, 'mir, 'tcx> {
@@ -544,18 +540,31 @@ impl<'a, 'mir, 'tcx> BorrowedLocalsResultsCursor<'a, 'mir, 'tcx> {
544540
}
545541

546542
/// Performs a liveness analysis for borrows and raw pointers.
547-
pub struct LiveBorrows<'a, 'tcx> {
548-
body: &'a Body<'tcx>,
543+
pub struct LiveBorrows<'mir, 'tcx> {
544+
body: &'mir Body<'tcx>,
549545
tcx: TyCtxt<'tcx>,
546+
borrow_deps: BorrowDependencies<'mir, 'tcx>,
550547
}
551548

552-
impl<'a, 'tcx> LiveBorrows<'a, 'tcx> {
553-
fn new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
554-
LiveBorrows { body, tcx }
549+
impl<'mir, 'tcx> LiveBorrows<'mir, 'tcx> {
550+
fn new(
551+
body: &'mir Body<'tcx>,
552+
tcx: TyCtxt<'tcx>,
553+
borrow_deps: BorrowDependencies<'mir, 'tcx>,
554+
) -> Self {
555+
LiveBorrows { body, tcx, borrow_deps }
555556
}
556557

557-
fn transfer_function<'b, T>(&self, trans: &'b mut T) -> TransferFunction<'a, 'b, 'tcx, T> {
558-
TransferFunction { body: self.body, tcx: self.tcx, _trans: trans }
558+
fn transfer_function<'b, T>(
559+
&self,
560+
trans: &'b mut T,
561+
) -> TransferFunction<'mir, 'b, '_, 'tcx, T> {
562+
TransferFunction {
563+
body: self.body,
564+
tcx: self.tcx,
565+
_trans: trans,
566+
borrow_deps: &self.borrow_deps,
567+
}
559568
}
560569
}
561570

@@ -607,13 +616,14 @@ impl<'a, 'tcx> GenKillAnalysis<'tcx> for LiveBorrows<'a, 'tcx> {
607616
}
608617

609618
/// A `Visitor` that defines the transfer function for `MaybeBorrowedLocals`.
610-
struct TransferFunction<'a, 'b, 'tcx, T> {
619+
struct TransferFunction<'a, 'b, 'c, 'tcx, T> {
611620
body: &'a Body<'tcx>,
612621
tcx: TyCtxt<'tcx>,
613622
_trans: &'b mut T,
623+
borrow_deps: &'c BorrowDependencies<'a, 'tcx>,
614624
}
615625

616-
impl<'a, 'tcx, T> Visitor<'tcx> for TransferFunction<'a, '_, 'tcx, T>
626+
impl<'a, 'tcx, T> Visitor<'tcx> for TransferFunction<'a, '_, '_, 'tcx, T>
617627
where
618628
T: GenKill<Local>,
619629
{
@@ -670,9 +680,22 @@ where
670680
match local_ty.kind() {
671681
ty::Ref(..) | ty::RawPtr(..) => {
672682
debug!("gen {:?}", local);
673-
self._trans.gen(place.local);
683+
self._trans.gen(local);
684+
}
685+
_ => {
686+
if let Some(node_idx) = self.borrow_deps.locals_to_node_indexes.get(&local) {
687+
let node = self.borrow_deps.dep_graph.node(*node_idx);
688+
689+
// these are `Local`s that contain references/pointers or are raw pointers
690+
// that were assigned to raw pointers, which were cast to usize. Hence we
691+
// need to treat them as uses of the references/pointers that they
692+
// refer/correspond to.
693+
if let NodeKind::LocalWithRefs(_) = node.data {
694+
debug!("gen {:?}", local);
695+
self._trans.gen(local);
696+
}
697+
}
674698
}
675-
_ => {}
676699
}
677700

678701
self.super_place(place, context, location);

0 commit comments

Comments
 (0)