Remove small dominators optimization from SsaLocals
The optimization is now part of the general implementation.
This commit is contained in:
parent
0528d378b6
commit
6c348b77fe
1 changed files with 23 additions and 41 deletions
|
@ -27,49 +27,12 @@ pub struct SsaLocals {
|
||||||
direct_uses: IndexVec<Local, u32>,
|
direct_uses: IndexVec<Local, u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// We often encounter MIR bodies with 1 or 2 basic blocks. In those cases, it's unnecessary to
|
|
||||||
/// actually compute dominators, we can just compare block indices because bb0 is always the first
|
|
||||||
/// block, and in any body all other blocks are always dominated by bb0.
|
|
||||||
struct SmallDominators<'a> {
|
|
||||||
inner: Option<&'a Dominators<BasicBlock>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SmallDominators<'_> {
|
|
||||||
fn dominates(&self, first: Location, second: Location) -> bool {
|
|
||||||
if first.block == second.block {
|
|
||||||
first.statement_index <= second.statement_index
|
|
||||||
} else if let Some(inner) = &self.inner {
|
|
||||||
inner.dominates(first.block, second.block)
|
|
||||||
} else {
|
|
||||||
first.block < second.block
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_dominates(&mut self, set: &mut Set1<LocationExtended>, loc: Location) {
|
|
||||||
let assign_dominates = match *set {
|
|
||||||
Set1::Empty | Set1::Many => false,
|
|
||||||
Set1::One(LocationExtended::Arg) => true,
|
|
||||||
Set1::One(LocationExtended::Plain(assign)) => {
|
|
||||||
self.dominates(assign.successor_within_block(), loc)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// We are visiting a use that is not dominated by an assignment.
|
|
||||||
// Either there is a cycle involved, or we are reading for uninitialized local.
|
|
||||||
// Bail out.
|
|
||||||
if !assign_dominates {
|
|
||||||
*set = Set1::Many;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SsaLocals {
|
impl SsaLocals {
|
||||||
pub fn new<'tcx>(body: &Body<'tcx>) -> SsaLocals {
|
pub fn new<'tcx>(body: &Body<'tcx>) -> SsaLocals {
|
||||||
let assignment_order = Vec::with_capacity(body.local_decls.len());
|
let assignment_order = Vec::with_capacity(body.local_decls.len());
|
||||||
|
|
||||||
let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
|
let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
|
||||||
let dominators =
|
let dominators = body.basic_blocks.dominators();
|
||||||
if body.basic_blocks.len() > 2 { Some(body.basic_blocks.dominators()) } else { None };
|
|
||||||
let dominators = SmallDominators { inner: dominators };
|
|
||||||
|
|
||||||
let direct_uses = IndexVec::from_elem(0, &body.local_decls);
|
let direct_uses = IndexVec::from_elem(0, &body.local_decls);
|
||||||
let mut visitor = SsaVisitor { assignments, assignment_order, dominators, direct_uses };
|
let mut visitor = SsaVisitor { assignments, assignment_order, dominators, direct_uses };
|
||||||
|
@ -231,12 +194,31 @@ enum LocationExtended {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SsaVisitor<'a> {
|
struct SsaVisitor<'a> {
|
||||||
dominators: SmallDominators<'a>,
|
dominators: &'a Dominators<BasicBlock>,
|
||||||
assignments: IndexVec<Local, Set1<LocationExtended>>,
|
assignments: IndexVec<Local, Set1<LocationExtended>>,
|
||||||
assignment_order: Vec<Local>,
|
assignment_order: Vec<Local>,
|
||||||
direct_uses: IndexVec<Local, u32>,
|
direct_uses: IndexVec<Local, u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SsaVisitor<'_> {
|
||||||
|
fn check_dominates(&mut self, local: Local, loc: Location) {
|
||||||
|
let set = &mut self.assignments[local];
|
||||||
|
let assign_dominates = match *set {
|
||||||
|
Set1::Empty | Set1::Many => false,
|
||||||
|
Set1::One(LocationExtended::Arg) => true,
|
||||||
|
Set1::One(LocationExtended::Plain(assign)) => {
|
||||||
|
assign.successor_within_block().dominates(loc, self.dominators)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// We are visiting a use that is not dominated by an assignment.
|
||||||
|
// Either there is a cycle involved, or we are reading for uninitialized local.
|
||||||
|
// Bail out.
|
||||||
|
if !assign_dominates {
|
||||||
|
*set = Set1::Many;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'tcx> Visitor<'tcx> for SsaVisitor<'_> {
|
impl<'tcx> Visitor<'tcx> for SsaVisitor<'_> {
|
||||||
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
|
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
|
||||||
match ctxt {
|
match ctxt {
|
||||||
|
@ -254,7 +236,7 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'_> {
|
||||||
self.assignments[local] = Set1::Many;
|
self.assignments[local] = Set1::Many;
|
||||||
}
|
}
|
||||||
PlaceContext::NonMutatingUse(_) => {
|
PlaceContext::NonMutatingUse(_) => {
|
||||||
self.dominators.check_dominates(&mut self.assignments[local], loc);
|
self.check_dominates(local, loc);
|
||||||
self.direct_uses[local] += 1;
|
self.direct_uses[local] += 1;
|
||||||
}
|
}
|
||||||
PlaceContext::NonUse(_) => {}
|
PlaceContext::NonUse(_) => {}
|
||||||
|
@ -269,7 +251,7 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'_> {
|
||||||
let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
|
let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
|
||||||
|
|
||||||
self.visit_projection(place.as_ref(), new_ctxt, loc);
|
self.visit_projection(place.as_ref(), new_ctxt, loc);
|
||||||
self.dominators.check_dominates(&mut self.assignments[place.local], loc);
|
self.check_dominates(place.local, loc);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue