Skip to content
Merged
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
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn simplify(
use SimplifyResult::*;

match instruction {
Instruction::Binary(binary) => simplify_binary(binary, dfg),
Instruction::Binary(binary) => simplify_binary(binary, dfg, block, call_stack),
Instruction::Cast(value, typ) => simplify_cast(*value, *typ, dfg),
Instruction::Not(value) => {
match &dfg[*value] {
Expand Down
42 changes: 41 additions & 1 deletion compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/binary.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
use acvm::{AcirField as _, FieldElement};

use crate::ssa::ir::{
basic_block::BasicBlockId,
dfg::DataFlowGraph,
instruction::{
Binary, BinaryOp, Instruction,
binary::{BinaryEvaluationResult, eval_constant_binary_op},
},
types::NumericType,
};
use noirc_errors::call_stack::CallStackId;

use super::SimplifyResult;

/// Try to simplify this binary instruction, returning the new value if possible.
pub(super) fn simplify_binary(binary: &Binary, dfg: &mut DataFlowGraph) -> SimplifyResult {
pub(super) fn simplify_binary(
binary: &Binary,
dfg: &mut DataFlowGraph,
block: BasicBlockId,
call_stack: CallStackId,
) -> SimplifyResult {
let lhs = binary.lhs;
let rhs = binary.rhs;

Expand Down Expand Up @@ -211,6 +218,17 @@ pub(super) fn simplify_binary(binary: &Binary, dfg: &mut DataFlowGraph) -> Simpl
lhs,
zero,
));
} else if lhs_is_zero && dfg.runtime.is_acir() {
// `0 < rhs` for unsigned values is the same as `rhs != 0`,
// which is slightly more performant in ACIR
let zero = dfg.make_constant(FieldElement::zero(), lhs_type);
let instruction =
Instruction::Binary(Binary { lhs: rhs, rhs: zero, operator: BinaryOp::Eq });
let eq = dfg
.insert_instruction_and_results(instruction, block, None, call_stack)
.first();
let neq = Instruction::Not(eq);
return SimplifyResult::SimplifiedToInstruction(neq);
}
}
}
Expand Down Expand Up @@ -341,4 +359,26 @@ mod tests {
}
");
}

#[test]
fn simplifies_zero_less_than_unsigned_value_to_not_equals_in_acir() {
let src = "
acir(inline) fn main f0 {
b0(v0: u8):
v1 = lt u8 0, v0
return v1
}
";

let ssa = Ssa::from_str_simplifying(src).unwrap();

assert_ssa_snapshot!(ssa, @r"
acir(inline) fn main f0 {
b0(v0: u8):
v2 = eq v0, u8 0
v3 = not v2
return v3
}
");
}
}
Loading