Skip to content
Closed
Show file tree
Hide file tree
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
65 changes: 61 additions & 4 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use acvm::{acir::AcirField, FieldElement};
use serde::{Deserialize, Serialize};

use crate::ssa::ir::value::Value;

use super::{
DataFlowGraph, Instruction, InstructionResultType, NumericType, SimplifyResult, Type, ValueId,
};
Expand Down Expand Up @@ -114,6 +116,12 @@ impl Binary {
if rhs_is_zero {
return SimplifyResult::SimplifiedTo(self.lhs);
}

let lhs = dfg.resolve(self.lhs);
let rhs = dfg.resolve(self.rhs);
if let Some(value) = check_for_noop_value_merge(dfg, lhs, rhs) {
return SimplifyResult::SimplifiedTo(value);
}
}
BinaryOp::Sub => {
if rhs_is_zero {
Expand All @@ -131,11 +139,13 @@ 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
{

let lhs = dfg.resolve(self.lhs);
let rhs = dfg.resolve(self.rhs);

if lhs == rhs && dfg.get_value_max_num_bits(lhs) == 1 {
// Squaring a boolean value is a noop.
return SimplifyResult::SimplifiedTo(self.lhs);
return SimplifyResult::SimplifiedTo(lhs);
}
}
BinaryOp::Div => {
Expand Down Expand Up @@ -297,6 +307,53 @@ impl Binary {
}
}

/// Checks for `(c as _) * a + ((not c) as _) * a`
/// which is equivalent to `if c then a else a` = `a`
fn check_for_noop_value_merge(dfg: &DataFlowGraph, lhs: ValueId, rhs: ValueId) -> Option<ValueId> {
let (rhs_cond, rhs_value, rhs_is_not) = match source_instruction(dfg, rhs)? {
Instruction::Binary(Binary { lhs, rhs, operator: BinaryOp::Mul }) => {
match source_instruction(dfg, *lhs)? {
Instruction::Cast(cond, _) => match source_instruction(dfg, *cond) {
Some(Instruction::Not(cond)) => (*cond, *rhs, true),
_ => (*cond, *rhs, false),
},
_ => return None,
}
}
_ => return None,
};

if !dfg.type_of_value(rhs_cond).is_bool() {
return None;
}

let (lhs_cond, lhs_value, lhs_is_not) = match source_instruction(dfg, lhs)? {
Instruction::Binary(Binary { lhs, rhs, operator: BinaryOp::Mul }) => {
match source_instruction(dfg, *lhs)? {
Instruction::Cast(cond, _) => match source_instruction(dfg, *cond) {
Some(Instruction::Not(cond)) if !rhs_is_not => (*cond, *rhs, true),
_ => (*cond, *rhs, false),
},
_ => return None,
}
}
_ => return None,
};

if lhs_cond == rhs_cond && lhs_value == rhs_value && (lhs_is_not ^ rhs_is_not) {
Some(lhs_value)
} else {
None
}
}

fn source_instruction(dfg: &DataFlowGraph, instruction_result: ValueId) -> Option<&Instruction> {
match &dfg[instruction_result] {
Value::Instruction { instruction, .. } => Some(&dfg[*instruction]),
_ => None,
}
}

/// Evaluate a binary operation with constant arguments.
fn eval_constant_binary_op(
lhs: FieldElement,
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ impl Type {
matches!(self, Type::Numeric(NumericType::Unsigned { .. }))
}

/// Returns true if this type is a boolean (u1)
pub(crate) fn is_bool(&self) -> bool {
matches!(self, Type::Numeric(NumericType::Unsigned { bit_size: 1 }))
}

/// Create a new signed integer type with the given amount of bits.
pub(crate) fn signed(bit_size: u32) -> Type {
Type::Numeric(NumericType::Signed { bit_size })
Expand Down