Skip to content

Commit 9666d31

Browse files
committed
Respond to code review feedback
- Remove reads of indirect `Place`s - Add comments explaining what the algorithm does
1 parent da8f3bb commit 9666d31

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/librustc_mir/transform/simplify.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
307307
fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
308308
trace!("running SimplifyLocals on {:?}", source);
309309

310+
// First, we're going to get a count of *actual* uses for every `Local`.
311+
// Take a look at `DeclMarker::visit_local()` to see exactly what is ignored.
310312
let mut used_locals = {
311313
let read_only_cache = read_only!(body);
312314
let mut marker = DeclMarker::new(body);
@@ -317,6 +319,11 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
317319

318320
let arg_count = body.arg_count;
319321

322+
// Next, we're going to remove any `Local` with zero actual uses. When we remove those
323+
// `Locals`, we're also going to subtract any uses of other `Locals` from the `used_locals`
324+
// count. For example, if we removed `_2 = discriminant(_1)`, then we'll subtract one from
325+
// `use_counts[_1]`. That in turn might make `_1` unused, so we loop until we hit a
326+
// fixedpoint where there are no more unused locals.
320327
loop {
321328
let mut remove_statements = RemoveStatements::new(&mut used_locals, arg_count, tcx);
322329
remove_statements.visit_body(body);
@@ -326,6 +333,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
326333
}
327334
}
328335

336+
// Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s.
329337
let map = make_local_map(&mut body.local_decls, used_locals, arg_count);
330338

331339
// Only bother running the `LocalUpdater` if we actually found locals to remove.
@@ -402,7 +410,7 @@ impl<'a, 'tcx> Visitor<'tcx> for DeclMarker<'a, 'tcx> {
402410

403411
fn can_skip_operand(o: &Operand<'_>) -> bool {
404412
match o {
405-
Operand::Copy(p) | Operand::Move(p) => !p.is_indirect(),
413+
Operand::Copy(_) | Operand::Move(_) => true,
406414
Operand::Constant(c) => can_skip_constant(c.literal),
407415
}
408416
}

0 commit comments

Comments
 (0)