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

feat: improve SSA type-awareness in EQ and MUL instructions #4691

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
Or,
/// Bitwise xor (^)
Xor,
/// Bitshift left (<<)

Check warning on line 41 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 43 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 @@ -130,6 +130,11 @@
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
{
return SimplifyResult::SimplifiedTo(self.lhs);
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}
}
BinaryOp::Div => {
if rhs_is_one {
Expand Down Expand Up @@ -164,6 +169,36 @@
let one = dfg.make_constant(FieldElement::one(), Type::bool());
return SimplifyResult::SimplifiedTo(one);
}

if operand_type.is_unsigned() {
// If we're comparing a variable against a constant value which lies outside of the range of
// values which the variable's type can take, we can assume that the equality will be false.
match (lhs, rhs) {
(Some(lhs), None) => {
let max_possible_value =
2u128.pow(dfg.get_value_max_num_bits(self.rhs)) - 1;
if lhs > max_possible_value.into() {
let zero = dfg.make_constant(FieldElement::zero(), Type::bool());
return SimplifyResult::SimplifiedTo(zero);
}
}

(None, Some(rhs)) => {
let max_possible_value =
2u128.pow(dfg.get_value_max_num_bits(self.lhs)) - 1;
if rhs > max_possible_value.into() {
let zero = dfg.make_constant(FieldElement::zero(), Type::bool());
return SimplifyResult::SimplifiedTo(zero);
}
}

(None, None) => (),
(Some(_), Some(_)) => {
unreachable!("Constant binary instructions should be handled above")
}
}
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
}

if operand_type == Type::bool() {
// Simplify forms of `(boolean == true)` into `boolean`
if lhs_is_one {
Expand Down Expand Up @@ -274,7 +309,7 @@
return SimplifyResult::SimplifiedTo(zero);
}

// `two_pow_rhs` is limited to be at most `2 ^ {operand_bitsize - 1}` so it fits in `operand_type`.

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bitsize)
let two_pow_rhs = FieldElement::from(2u128).pow(&rhs_const);
let two_pow_rhs = dfg.make_constant(two_pow_rhs, operand_type);
return SimplifyResult::SimplifiedToInstruction(Instruction::binary(
Expand Down
Loading