@@ -823,18 +823,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
823823 return self . simplify_cast ( kind, value, to, location) ;
824824 }
825825 Rvalue :: BinaryOp ( op, box ( ref mut lhs, ref mut rhs) ) => {
826- let ty = lhs. ty ( self . local_decls , self . tcx ) ;
827- let lhs = self . simplify_operand ( lhs, location) ;
828- let rhs = self . simplify_operand ( rhs, location) ;
829- // Only short-circuit options after we called `simplify_operand`
830- // on both operands for side effect.
831- let lhs = lhs?;
832- let rhs = rhs?;
833-
834- if let Some ( value) = self . simplify_binary ( op, ty, lhs, rhs) {
835- return Some ( value) ;
836- }
837- Value :: BinaryOp ( op, lhs, rhs)
826+ return self . simplify_binary ( op, lhs, rhs, location) ;
838827 }
839828 Rvalue :: UnaryOp ( op, ref mut arg_op) => {
840829 return self . simplify_unary ( op, arg_op, location) ;
@@ -1059,6 +1048,52 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10591048
10601049 #[ instrument( level = "trace" , skip( self ) , ret) ]
10611050 fn simplify_binary (
1051+ & mut self ,
1052+ op : BinOp ,
1053+ lhs_operand : & mut Operand < ' tcx > ,
1054+ rhs_operand : & mut Operand < ' tcx > ,
1055+ location : Location ,
1056+ ) -> Option < VnIndex > {
1057+
1058+ let lhs = self . simplify_operand ( lhs_operand, location) ;
1059+ let rhs = self . simplify_operand ( rhs_operand, location) ;
1060+ // Only short-circuit options after we called `simplify_operand`
1061+ // on both operands for side effect.
1062+ let mut lhs = lhs?;
1063+ let mut rhs = rhs?;
1064+
1065+ let lhs_ty = lhs_operand. ty ( self . local_decls , self . tcx ) ;
1066+
1067+ // If we're comparing pointers, remove `PtrToPtr` casts if the from
1068+ // types of both casts and the metadata all match.
1069+ if let BinOp :: Eq | BinOp :: Ne | BinOp :: Lt | BinOp :: Le | BinOp :: Gt | BinOp :: Ge = op
1070+ && lhs_ty. is_any_ptr ( )
1071+ && let Value :: Cast { kind : CastKind :: PtrToPtr , value : lhs_value, from : lhs_from, .. } =
1072+ self . get ( lhs)
1073+ && let Value :: Cast { kind : CastKind :: PtrToPtr , value : rhs_value, from : rhs_from, .. } =
1074+ self . get ( rhs)
1075+ && lhs_from == rhs_from
1076+ && lhs_from. pointee_metadata_ty_or_projection ( self . tcx )
1077+ == lhs_ty. pointee_metadata_ty_or_projection ( self . tcx )
1078+ {
1079+ lhs = * lhs_value;
1080+ rhs = * rhs_value;
1081+ if let Some ( op) = self . try_as_operand ( lhs, location) {
1082+ * lhs_operand = op;
1083+ }
1084+ if let Some ( op) = self . try_as_operand ( rhs, location) {
1085+ * rhs_operand = op;
1086+ }
1087+ }
1088+
1089+ if let Some ( value) = self . simplify_binary_inner ( op, lhs_ty, lhs, rhs) {
1090+ return Some ( value) ;
1091+ }
1092+ let value = Value :: BinaryOp ( op, lhs, rhs) ;
1093+ Some ( self . insert ( value) )
1094+ }
1095+
1096+ fn simplify_binary_inner (
10621097 & mut self ,
10631098 op : BinOp ,
10641099 lhs_ty : Ty < ' tcx > ,
0 commit comments