Skip to content

Commit 0cc2102

Browse files
committed
Expand match over binops.
1 parent 166fe54 commit 0cc2102

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
@@ -966,12 +966,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
966966
}
967967
};
968968

969-
// Represent the values as `Ok(bits)` or `Err(VnIndex)`.
970-
let a = as_bits(lhs).ok_or(lhs);
971-
let b = as_bits(rhs).ok_or(rhs);
969+
// Represent the values as `Left(bits)` or `Right(VnIndex)`.
970+
use Either::{Left, Right};
971+
let a = as_bits(lhs).map_or(Right(lhs), Left);
972+
let b = as_bits(rhs).map_or(Right(rhs), Left);
972973
let result = match (op, a, b) {
973974
// Neutral elements.
974-
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Ok(0), Err(p))
975+
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Left(0), Right(p))
975976
| (
976977
BinOp::Add
977978
| BinOp::BitOr
@@ -980,28 +981,28 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
980981
| BinOp::Offset
981982
| BinOp::Shl
982983
| BinOp::Shr,
983-
Err(p),
984-
Ok(0),
984+
Right(p),
985+
Left(0),
985986
)
986-
| (BinOp::Mul, Ok(1), Err(p))
987-
| (BinOp::Mul | BinOp::Div, Err(p), Ok(1)) => p,
987+
| (BinOp::Mul, Left(1), Right(p))
988+
| (BinOp::Mul | BinOp::Div, Right(p), Left(1)) => p,
988989
// Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
989-
(BinOp::BitAnd, Err(p), Ok(ones)) | (BinOp::BitAnd, Ok(ones), Err(p))
990+
(BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
990991
if ones == layout.size.truncate(u128::MAX)
991992
|| (layout.ty.is_bool() && ones == 1) =>
992993
{
993994
p
994995
}
995996
// Absorbing elements.
996-
(BinOp::Mul | BinOp::BitAnd, _, Ok(0))
997-
| (BinOp::Rem, _, Ok(1))
997+
(BinOp::Mul | BinOp::BitAnd, _, Left(0))
998+
| (BinOp::Rem, _, Left(1))
998999
| (
9991000
BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr,
1000-
Ok(0),
1001+
Left(0),
10011002
_,
10021003
) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
10031004
// Attempt to simplify `x | ALL_ONES` to `ALL_ONES`.
1004-
(BinOp::BitOr, _, Ok(ones)) | (BinOp::BitOr, Ok(ones), _)
1005+
(BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
10051006
if ones == layout.size.truncate(u128::MAX)
10061007
|| (layout.ty.is_bool() && ones == 1) =>
10071008
{
@@ -1015,8 +1016,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
10151016
// - if both operands can be computed as bits, just compare the bits;
10161017
// - if we proved that both operands have the same value, we can insert true/false;
10171018
// - otherwise, do nothing, as we do not try to prove inequality.
1018-
(BinOp::Eq, a, b) if (a.is_ok() && b.is_ok()) || a == b => self.insert_bool(a == b),
1019-
(BinOp::Ne, a, b) if (a.is_ok() && b.is_ok()) || a == b => self.insert_bool(a != b),
1019+
(BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
1020+
(BinOp::Eq, a, b) if a == b => self.insert_bool(true),
1021+
(BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
1022+
(BinOp::Ne, a, b) if a == b => self.insert_bool(false),
10201023
_ => return None,
10211024
};
10221025

0 commit comments

Comments
 (0)