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: avoid branch condition negations in ValueMerger #6073

Closed
wants to merge 11 commits into from
Closed
43 changes: 25 additions & 18 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl<'a> ValueMerger<'a> {
dfg: &mut DataFlowGraph,
block: BasicBlockId,
then_condition: ValueId,
else_condition: ValueId,
_else_condition: ValueId,
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
Expand All @@ -114,31 +114,38 @@ impl<'a> ValueMerger<'a> {
// We must cast the bool conditions to the actual numeric type used by each value.
let then_condition = dfg
.insert_instruction_and_results(
Instruction::Cast(then_condition, then_type),
block,
None,
call_stack.clone(),
)
.first();
let else_condition = dfg
.insert_instruction_and_results(
Instruction::Cast(else_condition, else_type),
Instruction::Cast(then_condition, Type::field()),
block,
None,
call_stack.clone(),
)
.first();

let mul = Instruction::binary(BinaryOp::Mul, then_condition, then_value);
let then_value =
dfg.insert_instruction_and_results(mul, block, None, call_stack.clone()).first();
let then_field = Instruction::Cast(then_value, Type::field());
let then_field_value =
dfg.insert_instruction_and_results(then_field, block, None, call_stack.clone()).first();

let else_field = Instruction::Cast(else_value, Type::field());
let else_field_value =
dfg.insert_instruction_and_results(else_field, block, None, call_stack.clone()).first();

let diff = Instruction::binary(BinaryOp::Sub, then_field_value, else_field_value);
let diff_value =
dfg.insert_instruction_and_results(diff, block, None, call_stack.clone()).first();

let mul = Instruction::binary(BinaryOp::Mul, else_condition, else_value);
let else_value =
dfg.insert_instruction_and_results(mul, block, None, call_stack.clone()).first();
let conditional_diff = Instruction::binary(BinaryOp::Mul, then_condition, diff_value);
let conditional_diff_value = dfg
.insert_instruction_and_results(conditional_diff, block, None, call_stack.clone())
.first();

let merged_field =
Instruction::binary(BinaryOp::Add, else_field_value, conditional_diff_value);
let merged_field_value = dfg
.insert_instruction_and_results(merged_field, block, None, call_stack.clone())
.first();

let add = Instruction::binary(BinaryOp::Add, then_value, else_value);
dfg.insert_instruction_and_results(add, block, None, call_stack).first()
let merged = Instruction::Cast(merged_field_value, then_type);
dfg.insert_instruction_and_results(merged, block, None, call_stack).first()
}

/// Given an if expression that returns an array: `if c { array1 } else { array2 }`,
Expand Down
Loading