@@ -20,13 +20,6 @@ pub struct InstCombine;
2020
2121impl < ' tcx > MirPass < ' tcx > for InstCombine {
2222 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , body : & mut Body < ' tcx > ) {
23- // Check for fuel here before gathering the optimization list. If we're out of fuel,
24- // we don't want to take the time to pass over the MIR only to find optimizations
25- // we won't run.
26- if !tcx. consider_optimizing ( || format ! ( "InstCombine {:?} " , body. source. def_id( ) ) ) {
27- return ;
28- }
29-
3023 // First, find optimization opportunities. This is done in a pre-pass to keep the MIR
3124 // read-only so that we can do global analyses on the MIR in the process (e.g.
3225 // `Place::ty()`).
@@ -46,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
4639 tcx : TyCtxt < ' tcx > ,
4740}
4841
42+ impl < ' tcx > InstCombineVisitor < ' tcx > {
43+ fn should_combine ( & self , rvalue : & Rvalue < ' tcx > , location : Location ) -> bool {
44+ self . tcx . consider_optimizing ( || {
45+ format ! ( "InstCombine - Rvalue: {:?} Location: {:?}" , rvalue, location)
46+ } )
47+ }
48+ }
49+
4950impl < ' tcx > MutVisitor < ' tcx > for InstCombineVisitor < ' tcx > {
5051 fn tcx ( & self ) -> TyCtxt < ' tcx > {
5152 self . tcx
5253 }
5354
5455 fn visit_rvalue ( & mut self , rvalue : & mut Rvalue < ' tcx > , location : Location ) {
55- if self . optimizations . and_stars . remove ( & location) {
56+ if self . optimizations . and_stars . remove ( & location) && self . should_combine ( rvalue , location ) {
5657 debug ! ( "replacing `&*`: {:?}" , rvalue) ;
5758 let new_place = match rvalue {
5859 Rvalue :: Ref ( _, _, place) => {
@@ -74,18 +75,24 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
7475 }
7576
7677 if let Some ( constant) = self . optimizations . arrays_lengths . remove ( & location) {
77- debug ! ( "replacing `Len([_; N])`: {:?}" , rvalue) ;
78- * rvalue = Rvalue :: Use ( Operand :: Constant ( box constant) ) ;
78+ if self . should_combine ( rvalue, location) {
79+ debug ! ( "replacing `Len([_; N])`: {:?}" , rvalue) ;
80+ * rvalue = Rvalue :: Use ( Operand :: Constant ( box constant) ) ;
81+ }
7982 }
8083
8184 if let Some ( operand) = self . optimizations . unneeded_equality_comparison . remove ( & location) {
82- debug ! ( "replacing {:?} with {:?}" , rvalue, operand) ;
83- * rvalue = Rvalue :: Use ( operand) ;
85+ if self . should_combine ( rvalue, location) {
86+ debug ! ( "replacing {:?} with {:?}" , rvalue, operand) ;
87+ * rvalue = Rvalue :: Use ( operand) ;
88+ }
8489 }
8590
8691 if let Some ( place) = self . optimizations . unneeded_deref . remove ( & location) {
87- debug ! ( "unneeded_deref: replacing {:?} with {:?}" , rvalue, place) ;
88- * rvalue = Rvalue :: Use ( Operand :: Copy ( place) ) ;
92+ if self . should_combine ( rvalue, location) {
93+ debug ! ( "unneeded_deref: replacing {:?} with {:?}" , rvalue, place) ;
94+ * rvalue = Rvalue :: Use ( Operand :: Copy ( place) ) ;
95+ }
8996 }
9097
9198 self . super_rvalue ( rvalue, location)
0 commit comments