@@ -307,6 +307,8 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
307
307
fn run_pass ( & self , tcx : TyCtxt < ' tcx > , source : MirSource < ' tcx > , body : & mut BodyAndCache < ' tcx > ) {
308
308
trace ! ( "running SimplifyLocals on {:?}" , source) ;
309
309
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.
310
312
let mut used_locals = {
311
313
let read_only_cache = read_only ! ( body) ;
312
314
let mut marker = DeclMarker :: new ( body) ;
@@ -317,6 +319,11 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
317
319
318
320
let arg_count = body. arg_count ;
319
321
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.
320
327
loop {
321
328
let mut remove_statements = RemoveStatements :: new ( & mut used_locals, arg_count, tcx) ;
322
329
remove_statements. visit_body ( body) ;
@@ -326,6 +333,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals {
326
333
}
327
334
}
328
335
336
+ // Finally, we'll actually do the work of shrinking `body.local_decls` and remapping the `Local`s.
329
337
let map = make_local_map ( & mut body. local_decls , used_locals, arg_count) ;
330
338
331
339
// 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> {
402
410
403
411
fn can_skip_operand ( o : & Operand < ' _ > ) -> bool {
404
412
match o {
405
- Operand :: Copy ( p ) | Operand :: Move ( p ) => !p . is_indirect ( ) ,
413
+ Operand :: Copy ( _ ) | Operand :: Move ( _ ) => true ,
406
414
Operand :: Constant ( c) => can_skip_constant ( c. literal ) ,
407
415
}
408
416
}
0 commit comments