From 6f271dc49ce729a76eb702fcb6d15960aaf8c297 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 4 May 2023 16:26:09 +0000 Subject: [PATCH] Cache dominators. --- .../src/diagnostics/conflict_errors.rs | 4 ++-- compiler/rustc_borrowck/src/invalidation.rs | 2 +- compiler/rustc_borrowck/src/lib.rs | 13 ++++--------- compiler/rustc_codegen_ssa/src/mir/analyze.rs | 2 +- compiler/rustc_middle/src/mir/basic_blocks.rs | 10 ++++++---- compiler/rustc_mir_transform/src/ssa.rs | 7 +++++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 04b8174079a..da9241253ee 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -2382,7 +2382,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let mut back_edge_stack = Vec::new(); predecessor_locations(self.body, location).for_each(|predecessor| { - if location.dominates(predecessor, self.dominators()) { + if location.dominates(predecessor, self.dominators) { back_edge_stack.push(predecessor) } else { stack.push(predecessor); @@ -2494,7 +2494,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { let mut has_predecessor = false; predecessor_locations(self.body, location).for_each(|predecessor| { - if location.dominates(predecessor, self.dominators()) { + if location.dominates(predecessor, self.dominators) { back_edge_stack.push(predecessor) } else { stack.push(predecessor); diff --git a/compiler/rustc_borrowck/src/invalidation.rs b/compiler/rustc_borrowck/src/invalidation.rs index 863c92acdf4..036391d074d 100644 --- a/compiler/rustc_borrowck/src/invalidation.rs +++ b/compiler/rustc_borrowck/src/invalidation.rs @@ -46,7 +46,7 @@ struct InvalidationGenerator<'cx, 'tcx> { all_facts: &'cx mut AllFacts, location_table: &'cx LocationTable, body: &'cx Body<'tcx>, - dominators: Dominators, + dominators: &'cx Dominators, borrow_set: &'cx BorrowSet<'tcx>, } diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index eb25d454339..4412687b007 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -43,7 +43,6 @@ use rustc_target::abi::FieldIdx; use either::Either; use smallvec::SmallVec; -use std::cell::OnceCell; use std::cell::RefCell; use std::collections::BTreeMap; use std::ops::Deref; @@ -331,7 +330,7 @@ fn do_mir_borrowck<'tcx>( used_mut: Default::default(), used_mut_upvars: SmallVec::new(), borrow_set: Rc::clone(&borrow_set), - dominators: Default::default(), + dominators: promoted_body.basic_blocks.dominators(), upvars: Vec::new(), local_names: IndexVec::from_elem(None, &promoted_body.local_decls), region_names: RefCell::default(), @@ -360,7 +359,7 @@ fn do_mir_borrowck<'tcx>( used_mut: Default::default(), used_mut_upvars: SmallVec::new(), borrow_set: Rc::clone(&borrow_set), - dominators: Default::default(), + dominators: body.basic_blocks.dominators(), upvars, local_names, region_names: RefCell::default(), @@ -592,7 +591,7 @@ struct MirBorrowckCtxt<'cx, 'tcx> { borrow_set: Rc>, /// Dominators for MIR - dominators: OnceCell>, + dominators: &'cx Dominators, /// Information about upvars not necessarily preserved in types or MIR upvars: Vec>, @@ -1103,7 +1102,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { (Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => { // Reading from mere reservations of mutable-borrows is OK. - if !is_active(this.dominators(), borrow, location) { + if !is_active(this.dominators, borrow, location) { assert!(allow_two_phase_borrow(borrow.kind)); return Control::Continue; } @@ -2267,10 +2266,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option { path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body()) } - - fn dominators(&self) -> &Dominators { - self.dominators.get_or_init(|| self.body.basic_blocks.dominators()) - } } mod error { diff --git a/compiler/rustc_codegen_ssa/src/mir/analyze.rs b/compiler/rustc_codegen_ssa/src/mir/analyze.rs index 569599faa36..835074806e9 100644 --- a/compiler/rustc_codegen_ssa/src/mir/analyze.rs +++ b/compiler/rustc_codegen_ssa/src/mir/analyze.rs @@ -84,7 +84,7 @@ impl DefLocation { struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> { fx: &'mir FunctionCx<'a, 'tcx, Bx>, - dominators: Dominators, + dominators: &'mir Dominators, locals: IndexVec, } diff --git a/compiler/rustc_middle/src/mir/basic_blocks.rs b/compiler/rustc_middle/src/mir/basic_blocks.rs index f5157e18e6d..27a43c0a0e9 100644 --- a/compiler/rustc_middle/src/mir/basic_blocks.rs +++ b/compiler/rustc_middle/src/mir/basic_blocks.rs @@ -28,6 +28,8 @@ struct Cache { switch_sources: OnceCell, is_cyclic: OnceCell, postorder: OnceCell>, + dominator_tree: OnceCell>, + dominators: OnceCell>, } impl<'tcx> BasicBlocks<'tcx> { @@ -42,12 +44,12 @@ impl<'tcx> BasicBlocks<'tcx> { *self.cache.is_cyclic.get_or_init(|| graph::is_cyclic(self)) } - pub fn dominator_tree(&self) -> DominatorTree { - dominator_tree(&self) + pub fn dominator_tree(&self) -> &DominatorTree { + self.cache.dominator_tree.get_or_init(|| dominator_tree(&self)) } - pub fn dominators(&self) -> Dominators { - dominators(&self.dominator_tree()) + pub fn dominators(&self) -> &Dominators { + self.cache.dominators.get_or_init(|| dominators(self.dominator_tree())) } /// Returns predecessors for each basic block. diff --git a/compiler/rustc_mir_transform/src/ssa.rs b/compiler/rustc_mir_transform/src/ssa.rs index 2b404efccc7..c2b7e7c2d27 100644 --- a/compiler/rustc_mir_transform/src/ssa.rs +++ b/compiler/rustc_mir_transform/src/ssa.rs @@ -68,8 +68,11 @@ impl SsaLocals { let assignment_order = Vec::with_capacity(body.local_decls.len()); let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls); - let dominators = - if body.basic_blocks.len() > 2 { Some(body.basic_blocks.dominators()) } else { None }; + let dominators = if body.basic_blocks.len() > 2 { + Some(body.basic_blocks.dominators().clone()) + } else { + None + }; let dominators = SmallDominators { inner: dominators }; let direct_uses = IndexVec::from_elem(0, &body.local_decls);