Skip to content

Commit

Permalink
chore: simplify boolean in a mul of a mul (#6951)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
  • Loading branch information
guipublic and TomAFrench authored Jan 6, 2025
1 parent ab8807d commit 04714a7
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,39 @@ impl Binary {
let zero = dfg.make_constant(FieldElement::zero(), operand_type);
return SimplifyResult::SimplifiedTo(zero);
}
if dfg.resolve(self.lhs) == dfg.resolve(self.rhs)
&& dfg.get_value_max_num_bits(self.lhs) == 1
{
if dfg.get_value_max_num_bits(self.lhs) == 1 {
// Squaring a boolean value is a noop.
return SimplifyResult::SimplifiedTo(self.lhs);
if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) {
return SimplifyResult::SimplifiedTo(self.lhs);
}
// b*(b*x) = b*x if b is boolean
if let super::Value::Instruction { instruction, .. } = &dfg[self.rhs] {
if let Instruction::Binary(Binary { lhs, rhs, operator }) =
dfg[*instruction]
{
if operator == BinaryOp::Mul
&& (dfg.resolve(self.lhs) == dfg.resolve(lhs)
|| dfg.resolve(self.lhs) == dfg.resolve(rhs))
{
return SimplifyResult::SimplifiedTo(self.rhs);
}
}
}
}
// (b*x)*b = b*x if b is boolean
if dfg.get_value_max_num_bits(self.rhs) == 1 {
if let super::Value::Instruction { instruction, .. } = &dfg[self.lhs] {
if let Instruction::Binary(Binary { lhs, rhs, operator }) =
dfg[*instruction]
{
if operator == BinaryOp::Mul
&& (dfg.resolve(self.rhs) == dfg.resolve(lhs)
|| dfg.resolve(self.rhs) == dfg.resolve(rhs))
{
return SimplifyResult::SimplifiedTo(self.lhs);
}
}
}
}
}
BinaryOp::Div => {
Expand Down

0 comments on commit 04714a7

Please sign in to comment.