Skip to content

Commit 9811408

Browse files
committed
Auto merge of #135274 - saethlin:array-repeats, r=<try>
Add an InstSimplify for repetitive array expressions r? ghost Let's see if this even perfs well.
2 parents e26ff2f + d767dd8 commit 9811408

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

compiler/rustc_mir_transform/src/instsimplify.rs

+27
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
4848
ctx.simplify_ref_deref(rvalue);
4949
ctx.simplify_ptr_aggregate(rvalue);
5050
ctx.simplify_cast(rvalue);
51+
ctx.simplify_repeated_aggregate(rvalue);
5152
}
5253
_ => {}
5354
}
@@ -68,6 +69,32 @@ struct InstSimplifyContext<'a, 'tcx> {
6869
}
6970

7071
impl<'tcx> InstSimplifyContext<'_, 'tcx> {
72+
fn simplify_repeated_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
73+
let Rvalue::Aggregate(box AggregateKind::Array(_), fields) = rvalue else {
74+
return;
75+
};
76+
if fields.len() < 5 {
77+
return;
78+
}
79+
let first = &fields[rustc_abi::FieldIdx::ZERO];
80+
let Operand::Constant(first) = first else {
81+
return;
82+
};
83+
let Ok(first_val) = first.const_.eval(self.tcx, self.typing_env, first.span) else {
84+
return;
85+
};
86+
if fields.iter().all(|v| {
87+
let Operand::Constant(v) = v else {
88+
return false;
89+
};
90+
let v = v.const_.eval(self.tcx, self.typing_env, v.span);
91+
v == Ok(first_val)
92+
}) {
93+
let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
94+
*rvalue = Rvalue::Repeat(Operand::Constant(first.clone()), len);
95+
}
96+
}
97+
7198
/// Transform boolean comparisons into logical operations.
7299
fn simplify_bool_cmp(&self, rvalue: &mut Rvalue<'tcx>) {
73100
match rvalue {

0 commit comments

Comments
 (0)