Simplify unary operations.

This commit is contained in:
Camille GILLOT 2023-03-25 22:33:35 +00:00
parent 666030c51b
commit 5fc23ad8e6
6 changed files with 371 additions and 18 deletions

View file

@ -817,6 +817,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
Rvalue::UnaryOp(op, ref mut arg) => {
let arg = self.simplify_operand(arg, location)?;
if let Some(value) = self.simplify_unary(op, arg) {
return Some(value);
}
Value::UnaryOp(op, arg)
}
Rvalue::Discriminant(ref mut place) => {
@ -916,6 +919,23 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
Some(self.insert(Value::Aggregate(ty, variant_index, fields)))
}
#[instrument(level = "trace", skip(self), ret)]
fn simplify_unary(&mut self, op: UnOp, value: VnIndex) -> Option<VnIndex> {
let value = match (op, self.get(value)) {
(UnOp::Not, Value::UnaryOp(UnOp::Not, inner)) => return Some(*inner),
(UnOp::Neg, Value::UnaryOp(UnOp::Neg, inner)) => return Some(*inner),
(UnOp::Not, Value::BinaryOp(BinOp::Eq, lhs, rhs)) => {
Value::BinaryOp(BinOp::Ne, *lhs, *rhs)
}
(UnOp::Not, Value::BinaryOp(BinOp::Ne, lhs, rhs)) => {
Value::BinaryOp(BinOp::Eq, *lhs, *rhs)
}
_ => return None,
};
Some(self.insert(value))
}
#[instrument(level = "trace", skip(self), ret)]
fn simplify_binary(
&mut self,