diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-01-09 22:53:14 -0800 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2025-01-11 15:56:53 -0800 |
| commit | 6e34369ef6ec53f571565298b0d0de489f343acc (patch) | |
| tree | 57abe5c44d75de3a854b0cc71471fafe843a3de1 /compiler/rustc_mir_transform/src/instsimplify.rs | |
| parent | eb54a50837ad4bcc9842924f27e7287ca66e294c (diff) | |
| download | rust-6e34369ef6ec53f571565298b0d0de489f343acc.tar.gz rust-6e34369ef6ec53f571565298b0d0de489f343acc.zip | |
[mir-opt] simplify `Repeat`s that don't actually repeat the operand
Diffstat (limited to 'compiler/rustc_mir_transform/src/instsimplify.rs')
| -rw-r--r-- | compiler/rustc_mir_transform/src/instsimplify.rs | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/compiler/rustc_mir_transform/src/instsimplify.rs b/compiler/rustc_mir_transform/src/instsimplify.rs index f74a577c598..20e2e3e8ba2 100644 --- a/compiler/rustc_mir_transform/src/instsimplify.rs +++ b/compiler/rustc_mir_transform/src/instsimplify.rs @@ -49,6 +49,7 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify { ctx.simplify_ptr_aggregate(rvalue); ctx.simplify_cast(rvalue); ctx.simplify_repeated_aggregate(rvalue); + ctx.simplify_repeat_once(rvalue); } _ => {} } @@ -207,6 +208,18 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> { } } + /// Simplify `[x; 1]` to just `[x]`. + fn simplify_repeat_once(&self, rvalue: &mut Rvalue<'tcx>) { + if let Rvalue::Repeat(operand, count) = rvalue + && let Some(1) = count.try_to_target_usize(self.tcx) + { + *rvalue = Rvalue::Aggregate( + Box::new(AggregateKind::Array(operand.ty(self.local_decls, self.tcx))), + [operand.clone()].into(), + ); + } + } + fn simplify_primitive_clone( &self, terminator: &mut Terminator<'tcx>, |
