|
| 1 | +use crate::MirPass; |
| 2 | +use rustc_middle::mir::visit::MutVisitor; |
| 3 | +use rustc_middle::mir::*; |
| 4 | +use rustc_middle::ty::{FnDef, TyCtxt}; |
| 5 | + |
| 6 | +/// Within-block replacement empowers ConstProp, DataflowConstProp, and DestProp. (based on looking |
| 7 | +/// at the tests) |
| 8 | +/// |
| 9 | +/// SwitchInt replacement causes some basic blocks which only contain a goto to be deleted. (based |
| 10 | +/// on looking at core and alloc) |
| 11 | +/// It also makes EarlyOtherwiseBranch much more effective, but that pass is currently marked as |
| 12 | +/// unsound so this effect is not useful yet. |
| 13 | +/// |
| 14 | +/// At time of writing, Assert replacement has no effect. Most likely we don't often need the |
| 15 | +/// asserted predicate for anything else, and aren't smart enough to optimize into a form that |
| 16 | +/// could use it anyway. |
| 17 | +/// |
| 18 | +/// Enabling this pass for Call arguments breaks the moved-from local reuse optimization that |
| 19 | +/// Inline does, so without DestinationPropagation, modifying call arguments is just a regression. |
| 20 | +/// Except for calls to intrinsics, because those cannot be inlined. |
| 21 | +pub struct MoveToCopy; |
| 22 | + |
| 23 | +impl<'tcx> MirPass<'tcx> for MoveToCopy { |
| 24 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 25 | + let mut visitor = MoveToCopyVisitor { tcx }; |
| 26 | + let mut call_visitor = MoveToCopyCallVisitor { tcx, local_decls: &body.local_decls }; |
| 27 | + for (block, block_data) in body.basic_blocks.as_mut().iter_enumerated_mut() { |
| 28 | + for (statement_index, statement) in block_data.statements.iter_mut().enumerate() { |
| 29 | + visitor.visit_statement(statement, Location { block, statement_index }); |
| 30 | + } |
| 31 | + let Some(terminator) = &mut block_data.terminator else { |
| 32 | + continue; |
| 33 | + }; |
| 34 | + match &terminator.kind { |
| 35 | + TerminatorKind::SwitchInt { .. } | TerminatorKind::Assert { .. } => { |
| 36 | + visitor.visit_terminator( |
| 37 | + terminator, |
| 38 | + Location { block, statement_index: block_data.statements.len() }, |
| 39 | + ); |
| 40 | + } |
| 41 | + TerminatorKind::Call { func, .. } => { |
| 42 | + let func_ty = func.ty(call_visitor.local_decls, tcx); |
| 43 | + let is_intrinsic = if let FnDef(def_id, _) = *func_ty.kind() { |
| 44 | + tcx.is_intrinsic(def_id) |
| 45 | + } else { |
| 46 | + false |
| 47 | + }; |
| 48 | + if is_intrinsic || tcx.sess.mir_opt_level() >= 3 { |
| 49 | + call_visitor.visit_terminator( |
| 50 | + terminator, |
| 51 | + Location { block, statement_index: block_data.statements.len() }, |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + _ => {} |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +struct MoveToCopyCallVisitor<'a, 'tcx> { |
| 62 | + tcx: TyCtxt<'tcx>, |
| 63 | + local_decls: &'a LocalDecls<'tcx>, |
| 64 | +} |
| 65 | + |
| 66 | +impl<'a, 'tcx> MutVisitor<'tcx> for MoveToCopyCallVisitor<'a, 'tcx> { |
| 67 | + fn tcx(&self) -> TyCtxt<'tcx> { |
| 68 | + self.tcx |
| 69 | + } |
| 70 | + |
| 71 | + fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) { |
| 72 | + if let Operand::Move(place) = operand { |
| 73 | + let ty = place.ty(self.local_decls, self.tcx).ty; |
| 74 | + if ty.is_numeric() || ty.is_bool() || ty.is_char() { |
| 75 | + *operand = Operand::Copy(*place); |
| 76 | + } |
| 77 | + } |
| 78 | + self.super_operand(operand, location); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +struct MoveToCopyVisitor<'tcx> { |
| 83 | + tcx: TyCtxt<'tcx>, |
| 84 | +} |
| 85 | + |
| 86 | +impl<'tcx> MutVisitor<'tcx> for MoveToCopyVisitor<'tcx> { |
| 87 | + fn tcx(&self) -> TyCtxt<'tcx> { |
| 88 | + self.tcx |
| 89 | + } |
| 90 | + |
| 91 | + fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) { |
| 92 | + if let Operand::Move(place) = operand { |
| 93 | + *operand = Operand::Copy(*place); |
| 94 | + } |
| 95 | + self.super_operand(operand, location); |
| 96 | + } |
| 97 | +} |
0 commit comments