@@ -66,7 +66,7 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
6666 * rvalue = Rvalue :: Use ( Operand :: Constant ( box constant) ) ;
6767 }
6868
69- if let Some ( operand) = self . optimizations . unneeded_not_equal . remove ( & location) {
69+ if let Some ( operand) = self . optimizations . unneeded_equality_comparison . remove ( & location) {
7070 debug ! ( "replacing {:?} with {:?}" , rvalue, operand) ;
7171 * rvalue = Rvalue :: Use ( operand) ;
7272 }
@@ -87,14 +87,39 @@ impl OptimizationFinder<'b, 'tcx> {
8787 OptimizationFinder { body, tcx, optimizations : OptimizationList :: default ( ) }
8888 }
8989
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 (
91115 & self ,
92116 l : & Operand < ' tcx > ,
93117 r : & ' a Operand < ' tcx > ,
118+ const_to_find : bool ,
94119 ) -> Option < & ' a Operand < ' tcx > > {
95120 let const_ = l. constant ( ) ?;
96121 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 )
98123 {
99124 if r. place ( ) . is_some ( ) {
100125 return Some ( r) ;
@@ -128,17 +153,7 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
128153 }
129154 }
130155
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) ;
142157
143158 self . super_rvalue ( rvalue, location)
144159 }
@@ -148,5 +163,5 @@ impl Visitor<'tcx> for OptimizationFinder<'b, 'tcx> {
148163struct OptimizationList < ' tcx > {
149164 and_stars : FxHashSet < Location > ,
150165 arrays_lengths : FxHashMap < Location , Constant < ' tcx > > ,
151- unneeded_not_equal : FxHashMap < Location , Operand < ' tcx > > ,
166+ unneeded_equality_comparison : FxHashMap < Location , Operand < ' tcx > > ,
152167}
0 commit comments