Skip to content

Commit 6f271dc

Browse files
committed
Cache dominators.
1 parent aa1267f commit 6f271dc

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
23822382
let mut back_edge_stack = Vec::new();
23832383

23842384
predecessor_locations(self.body, location).for_each(|predecessor| {
2385-
if location.dominates(predecessor, self.dominators()) {
2385+
if location.dominates(predecessor, self.dominators) {
23862386
back_edge_stack.push(predecessor)
23872387
} else {
23882388
stack.push(predecessor);
@@ -2494,7 +2494,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24942494

24952495
let mut has_predecessor = false;
24962496
predecessor_locations(self.body, location).for_each(|predecessor| {
2497-
if location.dominates(predecessor, self.dominators()) {
2497+
if location.dominates(predecessor, self.dominators) {
24982498
back_edge_stack.push(predecessor)
24992499
} else {
25002500
stack.push(predecessor);

compiler/rustc_borrowck/src/invalidation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct InvalidationGenerator<'cx, 'tcx> {
4646
all_facts: &'cx mut AllFacts,
4747
location_table: &'cx LocationTable,
4848
body: &'cx Body<'tcx>,
49-
dominators: Dominators<BasicBlock>,
49+
dominators: &'cx Dominators<BasicBlock>,
5050
borrow_set: &'cx BorrowSet<'tcx>,
5151
}
5252

compiler/rustc_borrowck/src/lib.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use rustc_target::abi::FieldIdx;
4343

4444
use either::Either;
4545
use smallvec::SmallVec;
46-
use std::cell::OnceCell;
4746
use std::cell::RefCell;
4847
use std::collections::BTreeMap;
4948
use std::ops::Deref;
@@ -331,7 +330,7 @@ fn do_mir_borrowck<'tcx>(
331330
used_mut: Default::default(),
332331
used_mut_upvars: SmallVec::new(),
333332
borrow_set: Rc::clone(&borrow_set),
334-
dominators: Default::default(),
333+
dominators: promoted_body.basic_blocks.dominators(),
335334
upvars: Vec::new(),
336335
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
337336
region_names: RefCell::default(),
@@ -360,7 +359,7 @@ fn do_mir_borrowck<'tcx>(
360359
used_mut: Default::default(),
361360
used_mut_upvars: SmallVec::new(),
362361
borrow_set: Rc::clone(&borrow_set),
363-
dominators: Default::default(),
362+
dominators: body.basic_blocks.dominators(),
364363
upvars,
365364
local_names,
366365
region_names: RefCell::default(),
@@ -592,7 +591,7 @@ struct MirBorrowckCtxt<'cx, 'tcx> {
592591
borrow_set: Rc<BorrowSet<'tcx>>,
593592

594593
/// Dominators for MIR
595-
dominators: OnceCell<Dominators<BasicBlock>>,
594+
dominators: &'cx Dominators<BasicBlock>,
596595

597596
/// Information about upvars not necessarily preserved in types or MIR
598597
upvars: Vec<Upvar<'tcx>>,
@@ -1103,7 +1102,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11031102

11041103
(Read(kind), BorrowKind::Unique | BorrowKind::Mut { .. }) => {
11051104
// Reading from mere reservations of mutable-borrows is OK.
1106-
if !is_active(this.dominators(), borrow, location) {
1105+
if !is_active(this.dominators, borrow, location) {
11071106
assert!(allow_two_phase_borrow(borrow.kind));
11081107
return Control::Continue;
11091108
}
@@ -2267,10 +2266,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22672266
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<FieldIdx> {
22682267
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
22692268
}
2270-
2271-
fn dominators(&self) -> &Dominators<BasicBlock> {
2272-
self.dominators.get_or_init(|| self.body.basic_blocks.dominators())
2273-
}
22742269
}
22752270

22762271
mod error {

compiler/rustc_codegen_ssa/src/mir/analyze.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl DefLocation {
8484

8585
struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
8686
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
87-
dominators: Dominators<mir::BasicBlock>,
87+
dominators: &'mir Dominators<mir::BasicBlock>,
8888
locals: IndexVec<mir::Local, LocalKind>,
8989
}
9090

compiler/rustc_middle/src/mir/basic_blocks.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ struct Cache {
2828
switch_sources: OnceCell<SwitchSources>,
2929
is_cyclic: OnceCell<bool>,
3030
postorder: OnceCell<Vec<BasicBlock>>,
31+
dominator_tree: OnceCell<DominatorTree<BasicBlock>>,
32+
dominators: OnceCell<Dominators<BasicBlock>>,
3133
}
3234

3335
impl<'tcx> BasicBlocks<'tcx> {
@@ -42,12 +44,12 @@ impl<'tcx> BasicBlocks<'tcx> {
4244
*self.cache.is_cyclic.get_or_init(|| graph::is_cyclic(self))
4345
}
4446

45-
pub fn dominator_tree(&self) -> DominatorTree<BasicBlock> {
46-
dominator_tree(&self)
47+
pub fn dominator_tree(&self) -> &DominatorTree<BasicBlock> {
48+
self.cache.dominator_tree.get_or_init(|| dominator_tree(&self))
4749
}
4850

49-
pub fn dominators(&self) -> Dominators<BasicBlock> {
50-
dominators(&self.dominator_tree())
51+
pub fn dominators(&self) -> &Dominators<BasicBlock> {
52+
self.cache.dominators.get_or_init(|| dominators(self.dominator_tree()))
5153
}
5254

5355
/// Returns predecessors for each basic block.

compiler/rustc_mir_transform/src/ssa.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ impl SsaLocals {
6868
let assignment_order = Vec::with_capacity(body.local_decls.len());
6969

7070
let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
71-
let dominators =
72-
if body.basic_blocks.len() > 2 { Some(body.basic_blocks.dominators()) } else { None };
71+
let dominators = if body.basic_blocks.len() > 2 {
72+
Some(body.basic_blocks.dominators().clone())
73+
} else {
74+
None
75+
};
7376
let dominators = SmallDominators { inner: dominators };
7477

7578
let direct_uses = IndexVec::from_elem(0, &body.local_decls);

0 commit comments

Comments
 (0)