|
| 1 | +use rustc_index::bit_set::DenseBitSet; |
| 2 | +use rustc_middle::mir::*; |
| 3 | +use rustc_middle::ty::TyCtxt; |
| 4 | +use rustc_mir_dataflow::{Analysis, GenKill}; |
| 5 | + |
| 6 | +use super::simplify::simplify_cfg; |
| 7 | + |
| 8 | +pub(crate) struct RemoveDeadDrops; |
| 9 | + |
| 10 | +use rustc_middle::mir::visit::*; |
| 11 | + |
| 12 | +struct MaybeInitializedLocals; |
| 13 | + |
| 14 | +/// This is conservative: If any part of a local is initialized, we mark it |
| 15 | +/// initialized, while we only mark uninitialized if the whole local is moved |
| 16 | +/// from or StorageDead. |
| 17 | +struct TransferFunction<'a, T> { |
| 18 | + trans: &'a mut T, |
| 19 | +} |
| 20 | + |
| 21 | +impl<'tcx, T: GenKill<Local>> Visitor<'tcx> for TransferFunction<'_, T> { |
| 22 | + fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) { |
| 23 | + self.super_place(place, context, location); |
| 24 | + |
| 25 | + if context.is_place_assignment() { |
| 26 | + self.trans.gen_(place.local); |
| 27 | + } else if matches!( |
| 28 | + context, |
| 29 | + PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) |
| 30 | + ) { |
| 31 | + self.trans.kill(place.local); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) { |
| 36 | + self.super_operand(operand, location); |
| 37 | + |
| 38 | + if let Operand::Move(place) = operand { |
| 39 | + if place.projection.is_empty() { |
| 40 | + self.trans.kill(place.local); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<'tcx> Analysis<'tcx> for MaybeInitializedLocals { |
| 47 | + type Domain = DenseBitSet<Local>; |
| 48 | + const NAME: &'static str = "maybe_initialized_locals"; |
| 49 | + |
| 50 | + fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain { |
| 51 | + DenseBitSet::new_empty(body.local_decls.len()) |
| 52 | + } |
| 53 | + |
| 54 | + fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) { |
| 55 | + for arg in 1..=body.arg_count { |
| 56 | + state.insert(Local::from_usize(arg)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn apply_primary_statement_effect( |
| 61 | + &self, |
| 62 | + state: &mut Self::Domain, |
| 63 | + stmt: &Statement<'tcx>, |
| 64 | + location: Location, |
| 65 | + ) { |
| 66 | + TransferFunction { trans: state }.visit_statement(stmt, location); |
| 67 | + } |
| 68 | + |
| 69 | + fn apply_primary_terminator_effect<'mir>( |
| 70 | + &self, |
| 71 | + state: &mut Self::Domain, |
| 72 | + terminator: &'mir Terminator<'tcx>, |
| 73 | + location: Location, |
| 74 | + ) -> TerminatorEdges<'mir, 'tcx> { |
| 75 | + TransferFunction { trans: state }.visit_terminator(terminator, location); |
| 76 | + terminator.edges() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl<'tcx> crate::MirPass<'tcx> for RemoveDeadDrops { |
| 81 | + fn is_required(&self) -> bool { |
| 82 | + true |
| 83 | + } |
| 84 | + |
| 85 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 86 | + let mut maybe_init_cursor = MaybeInitializedLocals |
| 87 | + .iterate_to_fixpoint(tcx, body, None) |
| 88 | + .into_results_cursor(body); |
| 89 | + |
| 90 | + let mut dead_drops = Vec::new(); |
| 91 | + |
| 92 | + for (block, data) in body.basic_blocks.iter_enumerated() { |
| 93 | + if let Some(terminator) = &data.terminator |
| 94 | + && let TerminatorKind::Drop { place, target, .. } = &terminator.kind |
| 95 | + { |
| 96 | + let term_location = Location { block, statement_index: data.statements.len() }; |
| 97 | + maybe_init_cursor.seek_before_primary_effect(term_location); |
| 98 | + |
| 99 | + let is_dead = !maybe_init_cursor.get().contains(place.local); |
| 100 | + if is_dead { |
| 101 | + dead_drops.push((block, *target)); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if !dead_drops.is_empty() { |
| 107 | + for (block, target) in dead_drops { |
| 108 | + if let Some(terminator) = &mut body.basic_blocks.as_mut()[block].terminator { |
| 109 | + terminator.kind = TerminatorKind::Goto { target }; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Removing drop terminators may simplify the CFG, so run cleanup. |
| 114 | + simplify_cfg(tcx, body); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments