Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify boolean in a mul of a mul #6951

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -39,9 +39,9 @@
Or,
/// Bitwise xor (^)
Xor,
/// Bitshift left (<<)

Check warning on line 42 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Bitshift)
Shl,
/// Bitshift right (>>)

Check warning on line 44 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Bitshift)
Shr,
}

Expand Down Expand Up @@ -131,11 +131,39 @@
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
Loading