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: replace boolean ANDs with multiplication #1954

Merged
merged 13 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ impl DataFlowGraph {
SimplifiedToMultiple(simplification)
}
SimplifyResult::Remove => InstructionRemoved,
SimplifyResult::None => {
result @ (SimplifyResult::SimplifiedToInstruction(_) | SimplifyResult::None) => {
let instruction = match result {
SimplifyResult::SimplifiedToInstruction(new_instruction) => new_instruction,
SimplifyResult::None => instruction,
_ => unreachable!("handled by above match statement"),
};
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
let id = self.make_instruction(instruction, ctrl_typevars);
self.blocks[block].insert_instruction(id);
if let Some(location) = location {
Expand Down
8 changes: 8 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,11 @@ impl Binary {
let zero = dfg.make_constant(FieldElement::zero(), operand_type);
return SimplifyResult::SimplifiedTo(zero);
}
if operand_type == Type::bool() {
// Boolean AND is equivalent to multiplication, which is a cheaper operation.
let instruction = Instruction::binary(BinaryOp::Mul, self.lhs, self.rhs);
return SimplifyResult::SimplifiedToInstruction(instruction);
}
}
BinaryOp::Or => {
if lhs_is_zero {
Expand Down Expand Up @@ -910,6 +915,9 @@ pub(crate) enum SimplifyResult {
/// a function such as a tuple
SimplifiedToMultiple(Vec<ValueId>),

/// Replace this function with an simpler but equivalent function.
SimplifiedToInstruction(Instruction),

/// Remove the instruction, it is unnecessary
Remove,

Expand Down