Auto merge of #75503 - JulianKnodt:opt_opt, r=oli-obk

Clean up some mir transform passes

I noticed a few places where there were intermediates being created
in MIR optimization passes, so I removed them.

I also changed some `Some(..)` into just `..` and wrap `Some(..)` at the function end, doing early returns for `None`.

I was generally looking for some easy optimizations in theses passes, and hopefully these should improve the runtime of these passes by a tinnnyyyyy bit.

r? @oli-obk
This commit is contained in:
bors 2020-08-14 09:23:22 +00:00
commit 6b2ca8457a
5 changed files with 71 additions and 92 deletions

View File

@ -86,12 +86,11 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
.skip(1) .skip(1)
.take(arg_count) .take(arg_count)
.map(|(local, _)| Place::from(local)) .map(|(local, _)| Place::from(local))
.filter(needs_retag) .filter(needs_retag);
.collect::<Vec<_>>();
// Emit their retags. // Emit their retags.
basic_blocks[START_BLOCK].statements.splice( basic_blocks[START_BLOCK].statements.splice(
0..0, 0..0,
places.into_iter().map(|place| Statement { places.map(|place| Statement {
source_info, source_info,
kind: StatementKind::Retag(RetagKind::FnEntry, box (place)), kind: StatementKind::Retag(RetagKind::FnEntry, box (place)),
}), }),
@ -101,29 +100,24 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
// PART 2 // PART 2
// Retag return values of functions. Also escape-to-raw the argument of `drop`. // Retag return values of functions. Also escape-to-raw the argument of `drop`.
// We collect the return destinations because we cannot mutate while iterating. // We collect the return destinations because we cannot mutate while iterating.
let mut returns: Vec<(SourceInfo, Place<'tcx>, BasicBlock)> = Vec::new(); let returns = basic_blocks
for block_data in basic_blocks.iter_mut() { .iter_mut()
match block_data.terminator().kind { .filter_map(|block_data| {
TerminatorKind::Call { ref destination, .. } => { match block_data.terminator().kind {
// Remember the return destination for later TerminatorKind::Call { destination: Some(ref destination), .. }
if let Some(ref destination) = destination { if needs_retag(&destination.0) =>
if needs_retag(&destination.0) { {
returns.push(( // Remember the return destination for later
block_data.terminator().source_info, Some((block_data.terminator().source_info, destination.0, destination.1))
destination.0,
destination.1,
));
}
} }
}
TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => {
// `Drop` is also a call, but it doesn't return anything so we are good. // `Drop` is also a call, but it doesn't return anything so we are good.
} TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => None,
_ => {
// Not a block ending in a Call -> ignore. // Not a block ending in a Call -> ignore.
_ => None,
} }
} })
} .collect::<Vec<_>>();
// Now we go over the returns we collected to retag the return values. // Now we go over the returns we collected to retag the return values.
for (source_info, dest_place, dest_block) in returns { for (source_info, dest_place, dest_block) in returns {
basic_blocks[dest_block].statements.insert( basic_blocks[dest_block].statements.insert(

View File

@ -12,26 +12,24 @@ impl<'tcx> MirPass<'tcx> for Deaggregator {
for bb in basic_blocks { for bb in basic_blocks {
bb.expand_statements(|stmt| { bb.expand_statements(|stmt| {
// FIXME(eddyb) don't match twice on `stmt.kind` (post-NLL). // FIXME(eddyb) don't match twice on `stmt.kind` (post-NLL).
if let StatementKind::Assign(box (_, ref rhs)) = stmt.kind { match stmt.kind {
if let Rvalue::Aggregate(ref kind, _) = *rhs { // FIXME(#48193) Deaggregate arrays when it's cheaper to do so.
// FIXME(#48193) Deaggregate arrays when it's cheaper to do so. StatementKind::Assign(box (
if let AggregateKind::Array(_) = **kind { _,
return None; Rvalue::Aggregate(box AggregateKind::Array(_), _),
} )) => {
} else {
return None; return None;
} }
} else { StatementKind::Assign(box (_, Rvalue::Aggregate(_, _))) => {}
return None; _ => return None,
} }
let stmt = stmt.replace_nop(); let stmt = stmt.replace_nop();
let source_info = stmt.source_info; let source_info = stmt.source_info;
let (lhs, kind, operands) = match stmt.kind { let (lhs, kind, operands) = match stmt.kind {
StatementKind::Assign(box (lhs, rvalue)) => match rvalue { StatementKind::Assign(box (lhs, Rvalue::Aggregate(kind, operands))) => {
Rvalue::Aggregate(kind, operands) => (lhs, kind, operands), (lhs, kind, operands)
_ => bug!(), }
},
_ => bug!(), _ => bug!(),
}; };

View File

@ -246,12 +246,14 @@ fn get_arm_identity_info<'a, 'tcx>(
tmp_assigned_vars.insert(*r); tmp_assigned_vars.insert(*r);
} }
let mut dbg_info_to_adjust = Vec::new(); let dbg_info_to_adjust: Vec<_> =
for (i, var_info) in debug_info.iter().enumerate() { debug_info
if tmp_assigned_vars.contains(var_info.place.local) { .iter()
dbg_info_to_adjust.push(i); .enumerate()
} .filter_map(|(i, var_info)| {
} if tmp_assigned_vars.contains(var_info.place.local) { Some(i) } else { None }
})
.collect();
Some(ArmIdentityInfo { Some(ArmIdentityInfo {
local_temp_0: local_tmp_s0, local_temp_0: local_tmp_s0,
@ -461,14 +463,14 @@ fn match_get_variant_field<'tcx>(
stmt: &Statement<'tcx>, stmt: &Statement<'tcx>,
) -> Option<(Local, Local, VarField<'tcx>, &'tcx List<PlaceElem<'tcx>>)> { ) -> Option<(Local, Local, VarField<'tcx>, &'tcx List<PlaceElem<'tcx>>)> {
match &stmt.kind { match &stmt.kind {
StatementKind::Assign(box (place_into, rvalue_from)) => match rvalue_from { StatementKind::Assign(box (
Rvalue::Use(Operand::Copy(pf) | Operand::Move(pf)) => { place_into,
let local_into = place_into.as_local()?; Rvalue::Use(Operand::Copy(pf) | Operand::Move(pf)),
let (local_from, vf) = match_variant_field_place(*pf)?; )) => {
Some((local_into, local_from, vf, pf.projection)) let local_into = place_into.as_local()?;
} let (local_from, vf) = match_variant_field_place(*pf)?;
_ => None, Some((local_into, local_from, vf, pf.projection))
}, }
_ => None, _ => None,
} }
} }
@ -479,14 +481,11 @@ fn match_get_variant_field<'tcx>(
/// ``` /// ```
fn match_set_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> { fn match_set_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> {
match &stmt.kind { match &stmt.kind {
StatementKind::Assign(box (place_from, rvalue_into)) => match rvalue_into { StatementKind::Assign(box (place_from, Rvalue::Use(Operand::Move(place_into)))) => {
Rvalue::Use(Operand::Move(place_into)) => { let local_into = place_into.as_local()?;
let local_into = place_into.as_local()?; let (local_from, vf) = match_variant_field_place(*place_from)?;
let (local_from, vf) = match_variant_field_place(*place_from)?; Some((local_into, local_from, vf))
Some((local_into, local_from, vf)) }
}
_ => None,
},
_ => None, _ => None,
} }
} }

View File

@ -99,26 +99,18 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
if let TerminatorKind::SwitchInt { values, targets, .. } = if let TerminatorKind::SwitchInt { values, targets, .. } =
&mut body.basic_blocks_mut()[bb].terminator_mut().kind &mut body.basic_blocks_mut()[bb].terminator_mut().kind
{ {
let vals = &*values; // take otherwise out early
let zipped = vals.iter().zip(targets.iter()); let otherwise = targets.pop().unwrap();
assert_eq!(targets.len(), values.len());
let mut i = 0;
targets.retain(|_| {
let keep = allowed_variants.contains(&values[i]);
i += 1;
keep
});
targets.push(otherwise);
let mut matched_values = Vec::with_capacity(allowed_variants.len()); values.to_mut().retain(|var| allowed_variants.contains(var));
let mut matched_targets = Vec::with_capacity(allowed_variants.len() + 1);
for (val, target) in zipped {
if allowed_variants.contains(val) {
matched_values.push(*val);
matched_targets.push(*target);
} else {
trace!("eliminating {:?} -> {:?}", val, target);
}
}
// handle the "otherwise" branch
matched_targets.push(targets.pop().unwrap());
*values = matched_values.into();
*targets = matched_targets;
} else { } else {
unreachable!() unreachable!()
} }

View File

@ -67,18 +67,13 @@ fn remove_successors<F>(
where where
F: Fn(BasicBlock) -> bool, F: Fn(BasicBlock) -> bool,
{ {
match *terminator_kind { let terminator = match *terminator_kind {
TerminatorKind::Goto { target } if predicate(target) => Some(TerminatorKind::Unreachable), TerminatorKind::Goto { target } if predicate(target) => TerminatorKind::Unreachable,
TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => { TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
let original_targets_len = targets.len(); let original_targets_len = targets.len();
let (otherwise, targets) = targets.split_last().unwrap(); let (otherwise, targets) = targets.split_last().unwrap();
let retained = values let (mut values, mut targets): (Vec<_>, Vec<_>) =
.iter() values.iter().zip(targets.iter()).filter(|(_, &t)| !predicate(t)).unzip();
.zip(targets.iter())
.filter(|(_, &t)| !predicate(t))
.collect::<Vec<_>>();
let mut values = retained.iter().map(|&(v, _)| *v).collect::<Vec<_>>();
let mut targets = retained.iter().map(|&(_, d)| *d).collect::<Vec<_>>();
if !predicate(*otherwise) { if !predicate(*otherwise) {
targets.push(*otherwise); targets.push(*otherwise);
@ -89,20 +84,21 @@ where
let retained_targets_len = targets.len(); let retained_targets_len = targets.len();
if targets.is_empty() { if targets.is_empty() {
Some(TerminatorKind::Unreachable) TerminatorKind::Unreachable
} else if targets.len() == 1 { } else if targets.len() == 1 {
Some(TerminatorKind::Goto { target: targets[0] }) TerminatorKind::Goto { target: targets[0] }
} else if original_targets_len != retained_targets_len { } else if original_targets_len != retained_targets_len {
Some(TerminatorKind::SwitchInt { TerminatorKind::SwitchInt {
discr: discr.clone(), discr: discr.clone(),
switch_ty, switch_ty,
values: Cow::from(values), values: Cow::from(values),
targets, targets,
}) }
} else { } else {
None return None;
} }
} }
_ => None, _ => return None,
} };
Some(terminator)
} }