Skip to content

Commit 61f3c06

Browse files
committed
Expand match over binops.
1 parent 159cdd3 commit 61f3c06

File tree

1 file changed

+18
-15
lines changed
  • compiler/rustc_mir_transform/src

1 file changed

+18
-15
lines changed

compiler/rustc_mir_transform/src/gvn.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -956,12 +956,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
956956
}
957957
};
958958

959-
// Represent the values as `Ok(bits)` or `Err(VnIndex)`.
960-
let a = as_bits(lhs).ok_or(lhs);
961-
let b = as_bits(rhs).ok_or(rhs);
959+
// Represent the values as `Left(bits)` or `Right(VnIndex)`.
960+
use Either::{Left, Right};
961+
let a = as_bits(lhs).map_or(Right(lhs), Left);
962+
let b = as_bits(rhs).map_or(Right(rhs), Left);
962963
let result = match (op, a, b) {
963964
// Neutral elements.
964-
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Ok(0), Err(p))
965+
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Left(0), Right(p))
965966
| (
966967
BinOp::Add
967968
| BinOp::BitOr
@@ -970,28 +971,28 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
970971
| BinOp::Offset
971972
| BinOp::Shl
972973
| BinOp::Shr,
973-
Err(p),
974-
Ok(0),
974+
Right(p),
975+
Left(0),
975976
)
976-
| (BinOp::Mul, Ok(1), Err(p))
977-
| (BinOp::Mul | BinOp::Div, Err(p), Ok(1)) => p,
977+
| (BinOp::Mul, Left(1), Right(p))
978+
| (BinOp::Mul | BinOp::Div, Right(p), Left(1)) => p,
978979
// Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
979-
(BinOp::BitAnd, Err(p), Ok(ones)) | (BinOp::BitAnd, Ok(ones), Err(p))
980+
(BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
980981
if ones == layout.size.truncate(u128::MAX)
981982
|| (layout.ty.is_bool() && ones == 1) =>
982983
{
983984
p
984985
}
985986
// Absorbing elements.
986-
(BinOp::Mul | BinOp::BitAnd, _, Ok(0))
987-
| (BinOp::Rem, _, Ok(1))
987+
(BinOp::Mul | BinOp::BitAnd, _, Left(0))
988+
| (BinOp::Rem, _, Left(1))
988989
| (
989990
BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr,
990-
Ok(0),
991+
Left(0),
991992
_,
992993
) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
993994
// Attempt to simplify `x | ALL_ONES` to `ALL_ONES`.
994-
(BinOp::BitOr, _, Ok(ones)) | (BinOp::BitOr, Ok(ones), _)
995+
(BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
995996
if ones == layout.size.truncate(u128::MAX)
996997
|| (layout.ty.is_bool() && ones == 1) =>
997998
{
@@ -1005,8 +1006,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10051006
// - if both operands can be computed as bits, just compare the bits;
10061007
// - if we proved that both operands have the same value, we can insert true/false;
10071008
// - otherwise, do nothing, as we do not try to prove inequality.
1008-
(BinOp::Eq, a, b) if (a.is_ok() && b.is_ok()) || a == b => self.insert_bool(a == b),
1009-
(BinOp::Ne, a, b) if (a.is_ok() && b.is_ok()) || a == b => self.insert_bool(a != b),
1009+
(BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
1010+
(BinOp::Eq, a, b) if a == b => self.insert_bool(true),
1011+
(BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
1012+
(BinOp::Ne, a, b) if a == b => self.insert_bool(false),
10101013
_ => return None,
10111014
};
10121015

0 commit comments

Comments
 (0)