From a7736eb418944864ff9a67b07aea01e7ba0bdb17 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Thu, 20 Jul 2023 19:02:27 +0000 Subject: [PATCH] chore: abstract away `SimplifyResult::SimplifiedToInstruction(None)` --- .../src/ssa_refactor/ir/instruction.rs | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs index 2d1c211e63..a5dd58f6d9 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs @@ -280,10 +280,10 @@ impl Instruction { if let Instruction::Not(value) = &dfg[*instruction] { SimplifiedTo(*value) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } - _ => SimplifiedToInstruction(None), + _ => SimplifyResult::could_not_simplify(), } } Instruction::Constrain(value) => { @@ -292,7 +292,7 @@ impl Instruction { return Remove; } } - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } Instruction::ArrayGet { array, index } => { let array = dfg.get_array_constant(*array); @@ -304,7 +304,7 @@ impl Instruction { return SimplifiedTo(array[index]); } } - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } Instruction::ArraySet { array, index, value } => { let array = dfg.get_array_constant(*array); @@ -318,7 +318,7 @@ impl Instruction { return SimplifiedTo(new_array); } } - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } Instruction::Truncate { value, bit_size, .. } => { if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) { @@ -326,7 +326,7 @@ impl Instruction { let truncated = numeric_constant.to_u128() % integer_modulus; SimplifiedTo(dfg.make_constant(truncated.into(), typ)) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Instruction::Call { func, arguments } => simplify_call(*func, arguments, dfg), @@ -338,10 +338,10 @@ impl Instruction { return Remove; } } - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } Instruction::Allocate { .. } | Instruction::Load { .. } | Instruction::Store { .. } => { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } } @@ -376,12 +376,12 @@ fn simplify_cast(value: ValueId, dst_typ: &Type, dfg: &mut DataFlowGraph) -> Sim let truncated = FieldElement::from_be_bytes_reduce(&truncated.to_bytes_be()); SimplifiedTo(dfg.make_constant(truncated, dst_typ.clone())) } - _ => SimplifiedToInstruction(None), + _ => SimplifyResult::could_not_simplify(), } } else if *dst_typ == dfg.type_of_value(value) { SimplifiedTo(value) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } @@ -391,7 +391,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) use SimplifyResult::*; let intrinsic = match &dfg[func] { Value::Intrinsic(intrinsic) => *intrinsic, - _ => return SimplifiedToInstruction(None), + _ => return SimplifyResult::could_not_simplify(), }; let constant_args: Option> = @@ -404,7 +404,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let limb_count = constant_args[1].to_u128() as u32; SimplifiedTo(constant_to_radix(endian, field, 2, limb_count, dfg)) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::ToRadix(endian) => { @@ -414,7 +414,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let limb_count = constant_args[2].to_u128() as u32; SimplifiedTo(constant_to_radix(endian, field, radix, limb_count, dfg)) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::ArrayLen => { @@ -430,7 +430,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) ); SimplifiedTo(slice_len) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::SlicePushBack => { @@ -440,7 +440,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let new_slice = dfg.make_array(slice, element_type); SimplifiedTo(new_slice) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::SlicePushFront => { @@ -450,7 +450,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let new_slice = dfg.make_array(slice, element_type); SimplifiedTo(new_slice) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::SlicePopBack => { @@ -461,7 +461,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let new_slice = dfg.make_array(slice, element_type); SimplifiedToMultiple(vec![new_slice, elem]) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::SlicePopFront => { @@ -472,7 +472,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let new_slice = dfg.make_array(slice, element_type); SimplifiedToMultiple(vec![elem, new_slice]) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::SliceInsert => { @@ -485,7 +485,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let new_slice = dfg.make_array(slice, element_type); SimplifiedTo(new_slice) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::SliceRemove => { @@ -496,11 +496,11 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph) let new_slice = dfg.make_array(slice, element_type); SimplifiedToMultiple(vec![new_slice, removed_elem]) } else { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } Intrinsic::BlackBox(_) | Intrinsic::Println | Intrinsic::Sort => { - SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } } } @@ -675,7 +675,7 @@ impl Binary { if let (Some(lhs), Some(rhs)) = (lhs, rhs) { return match self.eval_constants(dfg, lhs, rhs, operand_type) { Some(value) => SimplifyResult::SimplifiedTo(value), - None => SimplifyResult::SimplifiedToInstruction(None), + None => SimplifyResult::could_not_simplify(), }; } @@ -770,7 +770,7 @@ impl Binary { } } } - SimplifyResult::SimplifiedToInstruction(None) + SimplifyResult::could_not_simplify() } /// Evaluate the two constants with the operation specified by self.operator. @@ -926,3 +926,9 @@ pub(crate) enum SimplifyResult { /// If so it will be included here. SimplifiedToInstruction(Option), } + +impl SimplifyResult { + fn could_not_simplify() -> Self { + SimplifyResult::SimplifiedToInstruction(None) + } +}