Skip to content

Commit

Permalink
Replace LocationExtended with DefLocation in SsaLocals
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiasko committed Oct 5, 2023
1 parent 82ae063 commit 827dc2a
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_middle::mir::*;

pub struct SsaLocals {
/// Assignments to each local. This defines whether the local is SSA.
assignments: IndexVec<Local, Set1<LocationExtended>>,
assignments: IndexVec<Local, Set1<DefLocation>>,
/// We visit the body in reverse postorder, to ensure each local is assigned before it is used.
/// We remember the order in which we saw the assignments to compute the SSA values in a single
/// pass.
Expand All @@ -38,7 +38,7 @@ impl SsaLocals {
let mut visitor = SsaVisitor { assignments, assignment_order, dominators, direct_uses };

for local in body.args_iter() {
visitor.assignments[local] = Set1::One(LocationExtended::Arg);
visitor.assignments[local] = Set1::One(DefLocation::Argument);
}

// For SSA assignments, a RPO visit will see the assignment before it sees any use.
Expand Down Expand Up @@ -94,8 +94,8 @@ impl SsaLocals {
location: Location,
) -> bool {
match self.assignments[local] {
Set1::One(LocationExtended::Arg) => true,
Set1::One(LocationExtended::Plain(ass)) => {
Set1::One(DefLocation::Argument) => true,
Set1::One(DefLocation::Body(ass)) => {
if ass.block == location.block {
ass.statement_index < location.statement_index
} else {
Expand All @@ -111,7 +111,7 @@ impl SsaLocals {
body: &'a Body<'tcx>,
) -> impl Iterator<Item = (Local, &'a Rvalue<'tcx>, Location)> + 'a {
self.assignment_order.iter().filter_map(|&local| {
if let Set1::One(LocationExtended::Plain(loc)) = self.assignments[local] {
if let Set1::One(DefLocation::Body(loc)) = self.assignments[local] {
// `loc` must point to a direct assignment to `local`.
let Either::Left(stmt) = body.stmt_at(loc) else { bug!() };
let Some((target, rvalue)) = stmt.kind.as_assign() else { bug!() };
Expand All @@ -129,7 +129,7 @@ impl SsaLocals {
mut f: impl FnMut(Local, &mut Rvalue<'tcx>, Location),
) {
for &local in &self.assignment_order {
if let Set1::One(LocationExtended::Plain(loc)) = self.assignments[local] {
if let Set1::One(DefLocation::Body(loc)) = self.assignments[local] {
// `loc` must point to a direct assignment to `local`.
let bbs = basic_blocks.as_mut_preserves_cfg();
let bb = &mut bbs[loc.block];
Expand Down Expand Up @@ -187,15 +187,9 @@ impl SsaLocals {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum LocationExtended {
Plain(Location),
Arg,
}

struct SsaVisitor<'a> {
dominators: &'a Dominators<BasicBlock>,
assignments: IndexVec<Local, Set1<LocationExtended>>,
assignments: IndexVec<Local, Set1<DefLocation>>,
assignment_order: Vec<Local>,
direct_uses: IndexVec<Local, u32>,
}
Expand All @@ -205,8 +199,8 @@ impl SsaVisitor<'_> {
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)) => {
Set1::One(DefLocation::Argument) => true,
Set1::One(DefLocation::Body(assign)) => {
assign.successor_within_block().dominates(loc, self.dominators)
}
};
Expand Down Expand Up @@ -262,7 +256,7 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'_> {

fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, loc: Location) {
if let Some(local) = place.as_local() {
self.assignments[local].insert(LocationExtended::Plain(loc));
self.assignments[local].insert(DefLocation::Body(loc));
if let Set1::One(_) = self.assignments[local] {
// Only record if SSA-like, to avoid growing the vector needlessly.
self.assignment_order.push(local);
Expand Down Expand Up @@ -338,7 +332,7 @@ fn compute_copy_classes(ssa: &mut SsaLocals, body: &Body<'_>) {
#[derive(Debug)]
pub(crate) struct StorageLiveLocals {
/// Set of "StorageLive" statements for each local.
storage_live: IndexVec<Local, Set1<LocationExtended>>,
storage_live: IndexVec<Local, Set1<DefLocation>>,
}

impl StorageLiveLocals {
Expand All @@ -348,13 +342,13 @@ impl StorageLiveLocals {
) -> StorageLiveLocals {
let mut storage_live = IndexVec::from_elem(Set1::Empty, &body.local_decls);
for local in always_storage_live_locals.iter() {
storage_live[local] = Set1::One(LocationExtended::Arg);
storage_live[local] = Set1::One(DefLocation::Argument);
}
for (block, bbdata) in body.basic_blocks.iter_enumerated() {
for (statement_index, statement) in bbdata.statements.iter().enumerate() {
if let StatementKind::StorageLive(local) = statement.kind {
storage_live[local]
.insert(LocationExtended::Plain(Location { block, statement_index }));
.insert(DefLocation::Body(Location { block, statement_index }));
}
}
}
Expand Down

0 comments on commit 827dc2a

Please sign in to comment.