From 0e05280d75e43da007efd50ce49f4e1e8456bcbd Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 19 Feb 2023 15:50:00 -0500 Subject: Add an InstCombine for redundant casts --- compiler/rustc_mir_transform/src/instcombine.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'compiler/rustc_mir_transform/src/instcombine.rs') diff --git a/compiler/rustc_mir_transform/src/instcombine.rs b/compiler/rustc_mir_transform/src/instcombine.rs index 0534e688703..3896f0e57ea 100644 --- a/compiler/rustc_mir_transform/src/instcombine.rs +++ b/compiler/rustc_mir_transform/src/instcombine.rs @@ -30,6 +30,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine { ctx.combine_bool_cmp(&statement.source_info, rvalue); ctx.combine_ref_deref(&statement.source_info, rvalue); ctx.combine_len(&statement.source_info, rvalue); + ctx.combine_cast(&statement.source_info, rvalue); } _ => {} } @@ -142,6 +143,14 @@ impl<'tcx> InstCombineContext<'tcx, '_> { } } + fn combine_cast(&self, _source_info: &SourceInfo, rvalue: &mut Rvalue<'tcx>) { + if let Rvalue::Cast(_kind, operand, ty) = rvalue { + if operand.ty(self.local_decls, self.tcx) == *ty { + *rvalue = Rvalue::Use(operand.clone()); + } + } + } + fn combine_primitive_clone( &self, terminator: &mut Terminator<'tcx>, -- cgit 1.4.1-3-g733a5