Skip to content

Commit

Permalink
Add an InstSimplify for repetitive array expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Jan 9, 2025
1 parent fb546ee commit d767dd8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
ctx.simplify_ref_deref(rvalue);
ctx.simplify_ptr_aggregate(rvalue);
ctx.simplify_cast(rvalue);
ctx.simplify_repeated_aggregate(rvalue);
}
_ => {}
}
Expand All @@ -68,6 +69,32 @@ struct InstSimplifyContext<'a, 'tcx> {
}

impl<'tcx> InstSimplifyContext<'_, 'tcx> {
fn simplify_repeated_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
let Rvalue::Aggregate(box AggregateKind::Array(_), fields) = rvalue else {
return;
};
if fields.len() < 5 {
return;
}
let first = &fields[rustc_abi::FieldIdx::ZERO];
let Operand::Constant(first) = first else {
return;
};
let Ok(first_val) = first.const_.eval(self.tcx, self.typing_env, first.span) else {
return;
};
if fields.iter().all(|v| {
let Operand::Constant(v) = v else {
return false;
};
let v = v.const_.eval(self.tcx, self.typing_env, v.span);
v == Ok(first_val)
}) {
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
*rvalue = Rvalue::Repeat(Operand::Constant(first.clone()), len);
}
}

/// Transform boolean comparisons into logical operations.
fn simplify_bool_cmp(&self, rvalue: &mut Rvalue<'tcx>) {
match rvalue {
Expand Down

0 comments on commit d767dd8

Please sign in to comment.