about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-04-06 01:45:33 +0000
committerbors <bors@rust-lang.org>2025-04-06 01:45:33 +0000
commit1de931283df18f3af4922fe9a96adcc8936ac42e (patch)
tree84126385fac2b3c57bd42f802b7b94680d11ca3b
parentc2110769cd58cd3b0c31f308c8cfeab5e19340fd (diff)
parent5b596cd28b9ca38f8541aff1b05ec26c6207797b (diff)
Auto merge of #139411 - yotamofek:pr/mir_transform/instsimplify, r=compiler-errors
In `simplify_repeated_aggregate`, don't test first element against itself

r? `@saethlin`
Noticed that in `InstSimplifyContext::simplify_repeated_aggregate`, we're accidentally evaluating the first element's value twice, and then comparing it with itself, instead of just checking whether the rest of the elements are equal to the first one.
This will probably save very few cycles, but since `InstSimplify` is always enabled, this might improve perf by a bit.
-rw-r--r--compiler/rustc_mir_transform/src/instsimplify.rs6
1 files changed, 3 insertions, 3 deletions
diff --git a/compiler/rustc_mir_transform/src/instsimplify.rs b/compiler/rustc_mir_transform/src/instsimplify.rs
index da346dfc48c..2eff6b31372 100644
--- a/compiler/rustc_mir_transform/src/instsimplify.rs
+++ b/compiler/rustc_mir_transform/src/instsimplify.rs
@@ -78,20 +78,20 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
     /// GVN can also do this optimization, but GVN is only run at mir-opt-level 2 so having this in
     /// InstSimplify helps unoptimized builds.
     fn simplify_repeated_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
-        let Rvalue::Aggregate(box AggregateKind::Array(_), fields) = rvalue else {
+        let Rvalue::Aggregate(box AggregateKind::Array(_), fields) = &*rvalue else {
             return;
         };
         if fields.len() < 5 {
             return;
         }
-        let first = &fields[rustc_abi::FieldIdx::ZERO];
+        let (first, rest) = fields[..].split_first().unwrap();
         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(|field| {
+        if rest.iter().all(|field| {
             let Operand::Constant(field) = field else {
                 return false;
             };