@@ -20,13 +20,6 @@ pub struct InstCombine;
20
20
21
21
impl < ' tcx > MirPass < ' tcx > for InstCombine {
22
22
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
-
30
23
// First, find optimization opportunities. This is done in a pre-pass to keep the MIR
31
24
// read-only so that we can do global analyses on the MIR in the process (e.g.
32
25
// `Place::ty()`).
@@ -46,13 +39,21 @@ pub struct InstCombineVisitor<'tcx> {
46
39
tcx : TyCtxt < ' tcx > ,
47
40
}
48
41
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
+
49
50
impl < ' tcx > MutVisitor < ' tcx > for InstCombineVisitor < ' tcx > {
50
51
fn tcx ( & self ) -> TyCtxt < ' tcx > {
51
52
self . tcx
52
53
}
53
54
54
55
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 ) {
56
57
debug ! ( "replacing `&*`: {:?}" , rvalue) ;
57
58
let new_place = match rvalue {
58
59
Rvalue :: Ref ( _, _, place) => {
@@ -74,18 +75,24 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
74
75
}
75
76
76
77
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
+ }
79
82
}
80
83
81
84
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
+ }
84
89
}
85
90
86
91
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
+ }
89
96
}
90
97
91
98
self . super_rvalue ( rvalue, location)
0 commit comments