@@ -66,7 +66,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
66
66
* rvalue = Rvalue :: Use ( Operand :: Constant ( box constant) ) ;
67
67
}
68
68
69
- if let Some ( operand) = self . optimizations . unneeded_not_equal . remove ( & location) {
69
+ if let Some ( operand) = self . optimizations . unneeded_equality_comparison . remove ( & location) {
70
70
debug ! ( "replacing {:?} with {:?}" , rvalue, operand) ;
71
71
* rvalue = Rvalue :: Use ( operand) ;
72
72
}
@@ -87,14 +87,39 @@ impl OptimizationFinder<'b, 'tcx> {
87
87
OptimizationFinder { body, tcx, optimizations : OptimizationList :: default ( ) }
88
88
}
89
89
90
- fn find_operand_in_ne_false_pattern (
90
+ fn find_unneeded_equality_comparison ( & mut self , rvalue : & Rvalue < ' tcx > , location : Location ) {
91
+ // find Ne(_place, false) or Ne(false, _place)
92
+ // or Eq(_place, true) or Eq(true, _place)
93
+ if let Rvalue :: BinaryOp ( op, l, r) = rvalue {
94
+ let const_to_find = if * op == BinOp :: Ne {
95
+ false
96
+ } else if * op == BinOp :: Eq {
97
+ true
98
+ } else {
99
+ return ;
100
+ } ;
101
+ // (const, _place)
102
+ if let Some ( o) = self . find_operand_in_equality_comparison_pattern ( l, r, const_to_find) {
103
+ self . optimizations . unneeded_equality_comparison . insert ( location, o. clone ( ) ) ;
104
+ }
105
+ // (_place, const)
106
+ else if let Some ( o) =
107
+ self . find_operand_in_equality_comparison_pattern ( r, l, const_to_find)
108
+ {
109
+ self . optimizations . unneeded_equality_comparison . insert ( location, o. clone ( ) ) ;
110
+ }
111
+ }
112
+ }
113
+
114
+ fn find_operand_in_equality_comparison_pattern (
91
115
& self ,
92
116
l : & Operand < ' tcx > ,
93
117
r : & ' a Operand < ' tcx > ,
118
+ const_to_find : bool ,
94
119
) -> Option < & ' a Operand < ' tcx > > {
95
120
let const_ = l. constant ( ) ?;
96
121
if const_. literal . ty == self . tcx . types . bool
97
- && const_. literal . val . try_to_bool ( ) == Some ( false )
122
+ && const_. literal . val . try_to_bool ( ) == Some ( const_to_find )
98
123
{
99
124
if r. place ( ) . is_some ( ) {
100
125
return Some ( r) ;
@@ -128,17 +153,7 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
128
153
}
129
154
}
130
155
131
- // find Ne(_place, false) or Ne(false, _place)
132
- if let Rvalue :: BinaryOp ( BinOp :: Ne , l, r) = rvalue {
133
- // (false, _place)
134
- if let Some ( o) = self . find_operand_in_ne_false_pattern ( l, r) {
135
- self . optimizations . unneeded_not_equal . insert ( location, o. clone ( ) ) ;
136
- }
137
- // (_place, false)
138
- else if let Some ( o) = self . find_operand_in_ne_false_pattern ( r, l) {
139
- self . optimizations . unneeded_not_equal . insert ( location, o. clone ( ) ) ;
140
- }
141
- }
156
+ self . find_unneeded_equality_comparison ( rvalue, location) ;
142
157
143
158
self . super_rvalue ( rvalue, location)
144
159
}
@@ -148,5 +163,5 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
148
163
struct OptimizationList < ' tcx > {
149
164
and_stars : FxHashSet < Location > ,
150
165
arrays_lengths : FxHashMap < Location , Constant < ' tcx > > ,
151
- unneeded_not_equal : FxHashMap < Location , Operand < ' tcx > > ,
166
+ unneeded_equality_comparison : FxHashMap < Location , Operand < ' tcx > > ,
152
167
}
0 commit comments