Expand match over binops.
This commit is contained in:
parent
166fe54eba
commit
0cc2102c4a
1 changed files with 18 additions and 15 deletions
|
@ -966,12 +966,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Represent the values as `Ok(bits)` or `Err(VnIndex)`.
|
// Represent the values as `Left(bits)` or `Right(VnIndex)`.
|
||||||
let a = as_bits(lhs).ok_or(lhs);
|
use Either::{Left, Right};
|
||||||
let b = as_bits(rhs).ok_or(rhs);
|
let a = as_bits(lhs).map_or(Right(lhs), Left);
|
||||||
|
let b = as_bits(rhs).map_or(Right(rhs), Left);
|
||||||
let result = match (op, a, b) {
|
let result = match (op, a, b) {
|
||||||
// Neutral elements.
|
// Neutral elements.
|
||||||
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Ok(0), Err(p))
|
(BinOp::Add | BinOp::BitOr | BinOp::BitXor, Left(0), Right(p))
|
||||||
| (
|
| (
|
||||||
BinOp::Add
|
BinOp::Add
|
||||||
| BinOp::BitOr
|
| BinOp::BitOr
|
||||||
|
@ -980,28 +981,28 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
|
||||||
| BinOp::Offset
|
| BinOp::Offset
|
||||||
| BinOp::Shl
|
| BinOp::Shl
|
||||||
| BinOp::Shr,
|
| BinOp::Shr,
|
||||||
Err(p),
|
Right(p),
|
||||||
Ok(0),
|
Left(0),
|
||||||
)
|
)
|
||||||
| (BinOp::Mul, Ok(1), Err(p))
|
| (BinOp::Mul, Left(1), Right(p))
|
||||||
| (BinOp::Mul | BinOp::Div, Err(p), Ok(1)) => p,
|
| (BinOp::Mul | BinOp::Div, Right(p), Left(1)) => p,
|
||||||
// Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
|
// Attempt to simplify `x & ALL_ONES` to `x`, with `ALL_ONES` depending on type size.
|
||||||
(BinOp::BitAnd, Err(p), Ok(ones)) | (BinOp::BitAnd, Ok(ones), Err(p))
|
(BinOp::BitAnd, Right(p), Left(ones)) | (BinOp::BitAnd, Left(ones), Right(p))
|
||||||
if ones == layout.size.truncate(u128::MAX)
|
if ones == layout.size.truncate(u128::MAX)
|
||||||
|| (layout.ty.is_bool() && ones == 1) =>
|
|| (layout.ty.is_bool() && ones == 1) =>
|
||||||
{
|
{
|
||||||
p
|
p
|
||||||
}
|
}
|
||||||
// Absorbing elements.
|
// Absorbing elements.
|
||||||
(BinOp::Mul | BinOp::BitAnd, _, Ok(0))
|
(BinOp::Mul | BinOp::BitAnd, _, Left(0))
|
||||||
| (BinOp::Rem, _, Ok(1))
|
| (BinOp::Rem, _, Left(1))
|
||||||
| (
|
| (
|
||||||
BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr,
|
BinOp::Mul | BinOp::Div | BinOp::Rem | BinOp::BitAnd | BinOp::Shl | BinOp::Shr,
|
||||||
Ok(0),
|
Left(0),
|
||||||
_,
|
_,
|
||||||
) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
|
) => self.insert_scalar(Scalar::from_uint(0u128, layout.size), lhs_ty),
|
||||||
// Attempt to simplify `x | ALL_ONES` to `ALL_ONES`.
|
// Attempt to simplify `x | ALL_ONES` to `ALL_ONES`.
|
||||||
(BinOp::BitOr, _, Ok(ones)) | (BinOp::BitOr, Ok(ones), _)
|
(BinOp::BitOr, _, Left(ones)) | (BinOp::BitOr, Left(ones), _)
|
||||||
if ones == layout.size.truncate(u128::MAX)
|
if ones == layout.size.truncate(u128::MAX)
|
||||||
|| (layout.ty.is_bool() && ones == 1) =>
|
|| (layout.ty.is_bool() && ones == 1) =>
|
||||||
{
|
{
|
||||||
|
@ -1015,8 +1016,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
|
||||||
// - if both operands can be computed as bits, just compare the bits;
|
// - if both operands can be computed as bits, just compare the bits;
|
||||||
// - if we proved that both operands have the same value, we can insert true/false;
|
// - if we proved that both operands have the same value, we can insert true/false;
|
||||||
// - otherwise, do nothing, as we do not try to prove inequality.
|
// - otherwise, do nothing, as we do not try to prove inequality.
|
||||||
(BinOp::Eq, a, b) if (a.is_ok() && b.is_ok()) || a == b => self.insert_bool(a == b),
|
(BinOp::Eq, Left(a), Left(b)) => self.insert_bool(a == b),
|
||||||
(BinOp::Ne, a, b) if (a.is_ok() && b.is_ok()) || a == b => self.insert_bool(a != b),
|
(BinOp::Eq, a, b) if a == b => self.insert_bool(true),
|
||||||
|
(BinOp::Ne, Left(a), Left(b)) => self.insert_bool(a != b),
|
||||||
|
(BinOp::Ne, a, b) if a == b => self.insert_bool(false),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue