|
| 1 | +use crate::transform::{MirPass, MirSource}; |
| 2 | +use rustc::ty::{TyCtxt, Ty}; |
| 3 | +use rustc::mir::*; |
| 4 | +use rustc_target::abi::VariantIdx; |
| 5 | + |
| 6 | +/// Simplifies arms of form `Variant(x) => x` to just `x`. |
| 7 | +/// |
| 8 | +/// This is done by transforming basic blocks where the statements match: |
| 9 | +/// |
| 10 | +/// ```rust |
| 11 | +/// _LOCAL_TMP = ((_LOCAL_1 as Variant ).FIELD: TY ); |
| 12 | +/// ((_LOCAL_0 as Variant).FIELD: TY) = move _LOCAL_TMP; |
| 13 | +/// discriminant(_LOCAL_0) = VAR_IDX; |
| 14 | +/// ``` |
| 15 | +/// |
| 16 | +/// into: |
| 17 | +/// |
| 18 | +/// ```rust |
| 19 | +/// _LOCAL_0 = move _LOCAL_1 |
| 20 | +/// ``` |
| 21 | +pub struct SimplifyArmIdentity; |
| 22 | + |
| 23 | +impl<'tcx> MirPass<'tcx> for SimplifyArmIdentity { |
| 24 | + fn run_pass(&self, _tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) { |
| 25 | + for (idx, bb) in body.basic_blocks_mut().iter_mut().enumerate() { |
| 26 | + debug!("SimplifyArmIdentity - bb{} = {:?}", idx, bb); |
| 27 | + |
| 28 | + // Need 3 statements: |
| 29 | + let (s0, s1, s2) = match &mut *bb.statements { |
| 30 | + [s0, s1, s2] => (s0, s1, s2), |
| 31 | + _ => continue, |
| 32 | + }; |
| 33 | + debug!("SimplifyArmIdentity - found three stmts"); |
| 34 | + |
| 35 | + // Pattern match on the form we want: |
| 36 | + let (local_tmp_s0, local_1, vf_s0) = match match_get_variant_field(s0) { |
| 37 | + None => continue, |
| 38 | + Some(x) => x, |
| 39 | + }; |
| 40 | + debug!("SimplifyArmIdentity - get"); |
| 41 | + let (local_tmp_s1, local_0, vf_s1) = match match_set_variant_field(s1) { |
| 42 | + None => continue, |
| 43 | + Some(x) => x, |
| 44 | + }; |
| 45 | + debug!("SimplifyArmIdentity - set"); |
| 46 | + if local_tmp_s0 != local_tmp_s1 || vf_s0 != vf_s1 { |
| 47 | + continue; |
| 48 | + } |
| 49 | + match match_set_discr(s2) { |
| 50 | + Some((local, var_idx)) if local == local_0 && var_idx == vf_s0.var_idx => {} |
| 51 | + _ => continue, |
| 52 | + } |
| 53 | + debug!("SimplifyArmIdentity - set_discr"); |
| 54 | + |
| 55 | + // Right shape; transform! |
| 56 | + match &mut s0.kind { |
| 57 | + StatementKind::Assign(box (place, rvalue)) => { |
| 58 | + *place = local_0.into(); |
| 59 | + *rvalue = Rvalue::Use(Operand::Move(local_1.into())); |
| 60 | + } |
| 61 | + _ => unreachable!(), |
| 62 | + } |
| 63 | + s1.kind = StatementKind::Nop; |
| 64 | + s2.kind = StatementKind::Nop; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/// Match on: |
| 70 | +/// ```rust |
| 71 | +/// _LOCAL_INTO = ((_LOCAL_FROM as Variant).FIELD: TY); |
| 72 | +/// ``` |
| 73 | +fn match_get_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> { |
| 74 | + match &stmt.kind { |
| 75 | + StatementKind::Assign(box (place_into, rvalue_from)) => match rvalue_from { |
| 76 | + Rvalue::Use(Operand::Copy(pf)) | Rvalue::Use(Operand::Move(pf)) => { |
| 77 | + let local_into = place_into.as_local()?; |
| 78 | + let (local_from, vf) = match_variant_field_place(&pf)?; |
| 79 | + Some((local_into, local_from, vf)) |
| 80 | + } |
| 81 | + _ => None, |
| 82 | + }, |
| 83 | + _ => None, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// Match on: |
| 88 | +/// ```rust |
| 89 | +/// ((_LOCAL_FROM as Variant).FIELD: TY) = move _LOCAL_INTO; |
| 90 | +/// ``` |
| 91 | +fn match_set_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> { |
| 92 | + match &stmt.kind { |
| 93 | + StatementKind::Assign(box (place_from, rvalue_into)) => match rvalue_into { |
| 94 | + Rvalue::Use(Operand::Move(place_into)) => { |
| 95 | + let local_into = place_into.as_local()?; |
| 96 | + let (local_from, vf) = match_variant_field_place(&place_from)?; |
| 97 | + Some((local_into, local_from, vf)) |
| 98 | + } |
| 99 | + _ => None, |
| 100 | + }, |
| 101 | + _ => None, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/// Match on: |
| 106 | +/// ```rust |
| 107 | +/// discriminant(_LOCAL_TO_SET) = VAR_IDX; |
| 108 | +/// ``` |
| 109 | +fn match_set_discr<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, VariantIdx)> { |
| 110 | + match &stmt.kind { |
| 111 | + StatementKind::SetDiscriminant { place, variant_index } => Some(( |
| 112 | + place.as_local()?, |
| 113 | + *variant_index |
| 114 | + )), |
| 115 | + _ => None, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#[derive(PartialEq)] |
| 120 | +struct VarField<'tcx> { |
| 121 | + field: Field, |
| 122 | + field_ty: Ty<'tcx>, |
| 123 | + var_idx: VariantIdx, |
| 124 | +} |
| 125 | + |
| 126 | +/// Match on `((_LOCAL as Variant).FIELD: TY)`. |
| 127 | +fn match_variant_field_place<'tcx>(place: &Place<'tcx>) -> Option<(Local, VarField<'tcx>)> { |
| 128 | + match place.as_ref() { |
| 129 | + PlaceRef { |
| 130 | + base: &PlaceBase::Local(local), |
| 131 | + projection: &[ProjectionElem::Downcast(_, var_idx), ProjectionElem::Field(field, ty)], |
| 132 | + } => Some((local, VarField { field, field_ty: ty, var_idx })), |
| 133 | + _ => None, |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +pub struct SimplifyBranchSame; |
| 138 | + |
| 139 | +impl<'tcx> MirPass<'tcx> for SimplifyBranchSame { |
| 140 | + fn run_pass(&self, _tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, _body: &mut Body<'tcx>) { |
| 141 | + //debug!("SimplifyBranchSame - simplifying {:?}", _body); |
| 142 | + } |
| 143 | +} |
0 commit comments