Skip to content

Commit 6b2ca84

Browse files
committed
Auto merge of rust-lang#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
2 parents 43bec40 + fd63bf7 commit 6b2ca84

File tree

5 files changed

+72
-93
lines changed

5 files changed

+72
-93
lines changed

src/librustc_mir/transform/add_retag.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,11 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
8686
.skip(1)
8787
.take(arg_count)
8888
.map(|(local, _)| Place::from(local))
89-
.filter(needs_retag)
90-
.collect::<Vec<_>>();
89+
.filter(needs_retag);
9190
// Emit their retags.
9291
basic_blocks[START_BLOCK].statements.splice(
9392
0..0,
94-
places.into_iter().map(|place| Statement {
93+
places.map(|place| Statement {
9594
source_info,
9695
kind: StatementKind::Retag(RetagKind::FnEntry, box (place)),
9796
}),
@@ -101,29 +100,24 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
101100
// PART 2
102101
// Retag return values of functions. Also escape-to-raw the argument of `drop`.
103102
// We collect the return destinations because we cannot mutate while iterating.
104-
let mut returns: Vec<(SourceInfo, Place<'tcx>, BasicBlock)> = Vec::new();
105-
for block_data in basic_blocks.iter_mut() {
106-
match block_data.terminator().kind {
107-
TerminatorKind::Call { ref destination, .. } => {
108-
// Remember the return destination for later
109-
if let Some(ref destination) = destination {
110-
if needs_retag(&destination.0) {
111-
returns.push((
112-
block_data.terminator().source_info,
113-
destination.0,
114-
destination.1,
115-
));
116-
}
103+
let returns = basic_blocks
104+
.iter_mut()
105+
.filter_map(|block_data| {
106+
match block_data.terminator().kind {
107+
TerminatorKind::Call { destination: Some(ref destination), .. }
108+
if needs_retag(&destination.0) =>
109+
{
110+
// Remember the return destination for later
111+
Some((block_data.terminator().source_info, destination.0, destination.1))
117112
}
118-
}
119-
TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => {
113+
120114
// `Drop` is also a call, but it doesn't return anything so we are good.
121-
}
122-
_ => {
115+
TerminatorKind::Drop { .. } | TerminatorKind::DropAndReplace { .. } => None,
123116
// Not a block ending in a Call -> ignore.
117+
_ => None,
124118
}
125-
}
126-
}
119+
})
120+
.collect::<Vec<_>>();
127121
// Now we go over the returns we collected to retag the return values.
128122
for (source_info, dest_place, dest_block) in returns {
129123
basic_blocks[dest_block].statements.insert(

src/librustc_mir/transform/deaggregator.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@ impl<'tcx> MirPass<'tcx> for Deaggregator {
1212
for bb in basic_blocks {
1313
bb.expand_statements(|stmt| {
1414
// FIXME(eddyb) don't match twice on `stmt.kind` (post-NLL).
15-
if let StatementKind::Assign(box (_, ref rhs)) = stmt.kind {
16-
if let Rvalue::Aggregate(ref kind, _) = *rhs {
17-
// FIXME(#48193) Deaggregate arrays when it's cheaper to do so.
18-
if let AggregateKind::Array(_) = **kind {
19-
return None;
20-
}
21-
} else {
15+
match stmt.kind {
16+
// FIXME(#48193) Deaggregate arrays when it's cheaper to do so.
17+
StatementKind::Assign(box (
18+
_,
19+
Rvalue::Aggregate(box AggregateKind::Array(_), _),
20+
)) => {
2221
return None;
2322
}
24-
} else {
25-
return None;
23+
StatementKind::Assign(box (_, Rvalue::Aggregate(_, _))) => {}
24+
_ => return None,
2625
}
2726

2827
let stmt = stmt.replace_nop();
2928
let source_info = stmt.source_info;
3029
let (lhs, kind, operands) = match stmt.kind {
31-
StatementKind::Assign(box (lhs, rvalue)) => match rvalue {
32-
Rvalue::Aggregate(kind, operands) => (lhs, kind, operands),
33-
_ => bug!(),
34-
},
30+
StatementKind::Assign(box (lhs, Rvalue::Aggregate(kind, operands))) => {
31+
(lhs, kind, operands)
32+
}
3533
_ => bug!(),
3634
};
3735

src/librustc_mir/transform/simplify_try.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,14 @@ fn get_arm_identity_info<'a, 'tcx>(
246246
tmp_assigned_vars.insert(*r);
247247
}
248248

249-
let mut dbg_info_to_adjust = Vec::new();
250-
for (i, var_info) in debug_info.iter().enumerate() {
251-
if tmp_assigned_vars.contains(var_info.place.local) {
252-
dbg_info_to_adjust.push(i);
253-
}
254-
}
249+
let dbg_info_to_adjust: Vec<_> =
250+
debug_info
251+
.iter()
252+
.enumerate()
253+
.filter_map(|(i, var_info)| {
254+
if tmp_assigned_vars.contains(var_info.place.local) { Some(i) } else { None }
255+
})
256+
.collect();
255257

256258
Some(ArmIdentityInfo {
257259
local_temp_0: local_tmp_s0,
@@ -461,14 +463,14 @@ fn match_get_variant_field<'tcx>(
461463
stmt: &Statement<'tcx>,
462464
) -> Option<(Local, Local, VarField<'tcx>, &'tcx List<PlaceElem<'tcx>>)> {
463465
match &stmt.kind {
464-
StatementKind::Assign(box (place_into, rvalue_from)) => match rvalue_from {
465-
Rvalue::Use(Operand::Copy(pf) | Operand::Move(pf)) => {
466-
let local_into = place_into.as_local()?;
467-
let (local_from, vf) = match_variant_field_place(*pf)?;
468-
Some((local_into, local_from, vf, pf.projection))
469-
}
470-
_ => None,
471-
},
466+
StatementKind::Assign(box (
467+
place_into,
468+
Rvalue::Use(Operand::Copy(pf) | Operand::Move(pf)),
469+
)) => {
470+
let local_into = place_into.as_local()?;
471+
let (local_from, vf) = match_variant_field_place(*pf)?;
472+
Some((local_into, local_from, vf, pf.projection))
473+
}
472474
_ => None,
473475
}
474476
}
@@ -479,14 +481,11 @@ fn match_get_variant_field<'tcx>(
479481
/// ```
480482
fn match_set_variant_field<'tcx>(stmt: &Statement<'tcx>) -> Option<(Local, Local, VarField<'tcx>)> {
481483
match &stmt.kind {
482-
StatementKind::Assign(box (place_from, rvalue_into)) => match rvalue_into {
483-
Rvalue::Use(Operand::Move(place_into)) => {
484-
let local_into = place_into.as_local()?;
485-
let (local_from, vf) = match_variant_field_place(*place_from)?;
486-
Some((local_into, local_from, vf))
487-
}
488-
_ => None,
489-
},
484+
StatementKind::Assign(box (place_from, Rvalue::Use(Operand::Move(place_into)))) => {
485+
let local_into = place_into.as_local()?;
486+
let (local_from, vf) = match_variant_field_place(*place_from)?;
487+
Some((local_into, local_from, vf))
488+
}
490489
_ => None,
491490
}
492491
}

src/librustc_mir/transform/uninhabited_enum_branching.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,18 @@ impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
9999
if let TerminatorKind::SwitchInt { values, targets, .. } =
100100
&mut body.basic_blocks_mut()[bb].terminator_mut().kind
101101
{
102-
let vals = &*values;
103-
let zipped = vals.iter().zip(targets.iter());
104-
105-
let mut matched_values = Vec::with_capacity(allowed_variants.len());
106-
let mut matched_targets = Vec::with_capacity(allowed_variants.len() + 1);
107-
108-
for (val, target) in zipped {
109-
if allowed_variants.contains(val) {
110-
matched_values.push(*val);
111-
matched_targets.push(*target);
112-
} else {
113-
trace!("eliminating {:?} -> {:?}", val, target);
114-
}
115-
}
116-
117-
// handle the "otherwise" branch
118-
matched_targets.push(targets.pop().unwrap());
119-
120-
*values = matched_values.into();
121-
*targets = matched_targets;
102+
// take otherwise out early
103+
let otherwise = targets.pop().unwrap();
104+
assert_eq!(targets.len(), values.len());
105+
let mut i = 0;
106+
targets.retain(|_| {
107+
let keep = allowed_variants.contains(&values[i]);
108+
i += 1;
109+
keep
110+
});
111+
targets.push(otherwise);
112+
113+
values.to_mut().retain(|var| allowed_variants.contains(var));
122114
} else {
123115
unreachable!()
124116
}

src/librustc_mir/transform/unreachable_prop.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,13 @@ fn remove_successors<F>(
6767
where
6868
F: Fn(BasicBlock) -> bool,
6969
{
70-
match *terminator_kind {
71-
TerminatorKind::Goto { target } if predicate(target) => Some(TerminatorKind::Unreachable),
70+
let terminator = match *terminator_kind {
71+
TerminatorKind::Goto { target } if predicate(target) => TerminatorKind::Unreachable,
7272
TerminatorKind::SwitchInt { ref discr, switch_ty, ref values, ref targets } => {
7373
let original_targets_len = targets.len();
7474
let (otherwise, targets) = targets.split_last().unwrap();
75-
let retained = values
76-
.iter()
77-
.zip(targets.iter())
78-
.filter(|(_, &t)| !predicate(t))
79-
.collect::<Vec<_>>();
80-
let mut values = retained.iter().map(|&(v, _)| *v).collect::<Vec<_>>();
81-
let mut targets = retained.iter().map(|&(_, d)| *d).collect::<Vec<_>>();
75+
let (mut values, mut targets): (Vec<_>, Vec<_>) =
76+
values.iter().zip(targets.iter()).filter(|(_, &t)| !predicate(t)).unzip();
8277

8378
if !predicate(*otherwise) {
8479
targets.push(*otherwise);
@@ -89,20 +84,21 @@ where
8984
let retained_targets_len = targets.len();
9085

9186
if targets.is_empty() {
92-
Some(TerminatorKind::Unreachable)
87+
TerminatorKind::Unreachable
9388
} else if targets.len() == 1 {
94-
Some(TerminatorKind::Goto { target: targets[0] })
89+
TerminatorKind::Goto { target: targets[0] }
9590
} else if original_targets_len != retained_targets_len {
96-
Some(TerminatorKind::SwitchInt {
91+
TerminatorKind::SwitchInt {
9792
discr: discr.clone(),
9893
switch_ty,
9994
values: Cow::from(values),
10095
targets,
101-
})
96+
}
10297
} else {
103-
None
98+
return None;
10499
}
105100
}
106-
_ => None,
107-
}
101+
_ => return None,
102+
};
103+
Some(terminator)
108104
}

0 commit comments

Comments
 (0)