Skip to content

Commit

Permalink
feat: replace boolean ANDs with multiplication (#1954)
Browse files Browse the repository at this point in the history
* feat: replace boolean `AND`s with multiplication

* chore: move optimisation to live within ssa-gen

* chore: fill out message in `unreachable`

* chore: remove `SimplifyResult::None`

* chore: abstract away `SimplifyResult::SimplifiedToInstruction(None)`

* Revert "chore: abstract away `SimplifyResult::SimplifiedToInstruction(None)`"

This reverts commit a7736eb.

* Revert "chore: remove `SimplifyResult::None`"

This reverts commit 429ccd4.

* chore: add `SimplifyResult.instruction()`
  • Loading branch information
TomAFrench authored Aug 2, 2023
1 parent 292724f commit 435ab35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 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,8 @@ impl DataFlowGraph {
SimplifiedToMultiple(simplification)
}
SimplifyResult::Remove => InstructionRemoved,
SimplifyResult::None => {
result @ (SimplifyResult::SimplifiedToInstruction(_) | SimplifyResult::None) => {
let instruction = result.instruction().unwrap_or(instruction);
let id = self.make_instruction(instruction, ctrl_typevars);
self.blocks[block].insert_instruction(id);
if let Some(location) = location {
Expand Down
17 changes: 17 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ impl Binary {
if dfg.resolve(self.lhs) == dfg.resolve(self.rhs) {
return SimplifyResult::SimplifiedTo(self.lhs);
}
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 @@ -898,9 +903,21 @@ 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,

/// Instruction could not be simplified
None,
}

impl SimplifyResult {
pub(crate) fn instruction(self) -> Option<Instruction> {
match self {
SimplifyResult::SimplifiedToInstruction(instruction) => Some(instruction),
_ => None,
}
}
}

0 comments on commit 435ab35

Please sign in to comment.