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
55 changes: 43 additions & 12 deletions compiler/noirc_evaluator/src/ssa/ir/dfg/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,29 +463,60 @@ mod tests {
#[test]
fn simplifies_or_when_one_side_is_all_1s() {
let test_cases = vec![
("u128", "340282366920938463463374607431768211455"),
("u64", "18446744073709551615"),
("u32", "4294967295"),
("u16", "65535"),
("u8", "255"),
("u128", u128::MAX.to_string()),
("u64", u64::MAX.to_string()),
("u32", u32::MAX.to_string()),
("u16", u16::MAX.to_string()),
("u8", u8::MAX.to_string()),
];
const SRC_TEMPLATE: &str = "
acir(inline) pure fn main f0 {
b0(v1: {typ}):
v2 = or {typ} {max}, v1
return v2
b0(v1: {typ}):
v2 = or {typ} {max}, v1
return v2
}
";

const EXPECTED_TEMPLATE: &str = "
acir(inline) pure fn main f0 {
b0(v1: {typ}):
return {typ} {max}
b0(v1: {typ}):
return {typ} {max}
}
";
for (typ, max) in test_cases {
let src = SRC_TEMPLATE.replace("{typ}", typ).replace("{max}", &max);
let expected = EXPECTED_TEMPLATE.replace("{typ}", typ).replace("{max}", &max);
let ssa: Ssa = Ssa::from_str_simplifying(&src).unwrap();
assert_normalized_ssa_equals(ssa, &expected);
}
}

#[test]
fn simplifies_noop_bitwise_and_truncation() {
let test_cases = vec![
("u128", u128::MAX.to_string()),
("u64", u64::MAX.to_string()),
("u32", u32::MAX.to_string()),
("u16", u16::MAX.to_string()),
("u8", u8::MAX.to_string()),
];
const SRC_TEMPLATE: &str = "
acir(inline) pure fn main f0 {
b0(v1: {typ}):
v2 = and {typ} {max}, v1
return v2
}
";

const EXPECTED_TEMPLATE: &str = "
acir(inline) pure fn main f0 {
b0(v1: {typ}):
return v1
}
";
for (typ, max) in test_cases {
let src = SRC_TEMPLATE.replace("{typ}", typ).replace("{max}", max);
let expected = EXPECTED_TEMPLATE.replace("{typ}", typ).replace("{max}", max);
let src = SRC_TEMPLATE.replace("{typ}", typ).replace("{max}", &max);
let expected = EXPECTED_TEMPLATE.replace("{typ}", typ);
let ssa: Ssa = Ssa::from_str_simplifying(&src).unwrap();
assert_normalized_ssa_equals(ssa, &expected);
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/noirc_evaluator/src/ssa/ir/dfg/simplify/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,11 @@ pub(super) fn simplify_binary(binary: &Binary, dfg: &mut DataFlowGraph) -> Simpl
(Some(bitmask), None) | (None, Some(bitmask)) => {
// This substitution requires the bitmask to retain all of the lower bits.
// The bitmask must then be one less than a power of 2.
let bitmask_plus_one = bitmask.to_u128() + 1;
if bitmask_plus_one.is_power_of_two() {
let bitmask = bitmask.to_u128();
if bitmask == u128::MAX || (bitmask + 1).is_power_of_two() {
let value = if lhs_value.is_some() { rhs } else { lhs };
let bit_size = bitmask_plus_one.ilog2();
let bit_size =
if bitmask == u128::MAX { 128 } else { (bitmask + 1).ilog2() };
let max_bit_size = lhs_type.bit_size();

if bit_size == max_bit_size {
Expand Down
Loading