From 307c60843ce853cf6883f5330e71cde35838d78b Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 15 Nov 2020 22:34:23 +0100 Subject: [PATCH 1/3] Do not call super_rvalue if not needed --- compiler/rustc_mir/src/transform/instcombine.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir/src/transform/instcombine.rs b/compiler/rustc_mir/src/transform/instcombine.rs index 59b7db24319..b36633fbfb3 100644 --- a/compiler/rustc_mir/src/transform/instcombine.rs +++ b/compiler/rustc_mir/src/transform/instcombine.rs @@ -81,7 +81,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> { *rvalue = Rvalue::Use(Operand::Copy(place)); } - self.super_rvalue(rvalue, location) + // We do not call super_rvalue as we are not interested in any other parts of the tree } } @@ -285,7 +285,7 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> { self.find_unneeded_equality_comparison(rvalue, location); - self.super_rvalue(rvalue, location) + // We do not call super_rvalue as we are not interested in any other parts of the tree } } From 3d5a1e330fb48c5f64b078cf755cbb44ee73343e Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 15 Nov 2020 22:34:50 +0100 Subject: [PATCH 2/3] Only go through the body if something can be optimized --- compiler/rustc_mir/src/transform/instcombine.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir/src/transform/instcombine.rs b/compiler/rustc_mir/src/transform/instcombine.rs index b36633fbfb3..04ea2bb542e 100644 --- a/compiler/rustc_mir/src/transform/instcombine.rs +++ b/compiler/rustc_mir/src/transform/instcombine.rs @@ -29,8 +29,10 @@ impl<'tcx> MirPass<'tcx> for InstCombine { optimization_finder.optimizations }; - // Then carry out those optimizations. - MutVisitor::visit_body(&mut InstCombineVisitor { optimizations, tcx }, body); + if !optimizations.is_empty() { + // Then carry out those optimizations. + MutVisitor::visit_body(&mut InstCombineVisitor { optimizations, tcx }, body); + } } } @@ -296,3 +298,12 @@ struct OptimizationList<'tcx> { unneeded_equality_comparison: FxHashMap>, unneeded_deref: FxHashMap>, } + +impl<'tcx> OptimizationList<'tcx> { + fn is_empty(&self) -> bool { + self.and_stars.is_empty() + && self.arrays_lengths.is_empty() + && self.unneeded_equality_comparison.is_empty() + && self.unneeded_deref.is_empty() + } +} From 0010fc8fec235d36d9264de07dbad4508eda0a8e Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Mon, 28 Dec 2020 23:19:35 +0100 Subject: [PATCH 3/3] use exhaustive pattern match to prevent future bugs --- compiler/rustc_mir/src/transform/instcombine.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir/src/transform/instcombine.rs b/compiler/rustc_mir/src/transform/instcombine.rs index 04ea2bb542e..47695985386 100644 --- a/compiler/rustc_mir/src/transform/instcombine.rs +++ b/compiler/rustc_mir/src/transform/instcombine.rs @@ -301,9 +301,18 @@ struct OptimizationList<'tcx> { impl<'tcx> OptimizationList<'tcx> { fn is_empty(&self) -> bool { - self.and_stars.is_empty() - && self.arrays_lengths.is_empty() - && self.unneeded_equality_comparison.is_empty() - && self.unneeded_deref.is_empty() + match self { + OptimizationList { + and_stars, + arrays_lengths, + unneeded_equality_comparison, + unneeded_deref, + } => { + and_stars.is_empty() + && arrays_lengths.is_empty() + && unneeded_equality_comparison.is_empty() + && unneeded_deref.is_empty() + } + } } }