Skip to content

Commit

Permalink
chore: abstract away SimplifyResult::SimplifiedToInstruction(None)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jul 20, 2023
1 parent 429ccd4 commit a7736eb
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -318,15 +318,15 @@ 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) {
let integer_modulus = 2_u128.pow(*bit_size);
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),
Expand All @@ -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()
}
}
}
Expand Down Expand Up @@ -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()
}
}

Expand All @@ -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<Vec<_>> =
Expand All @@ -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) => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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()
}
}
}
Expand Down Expand Up @@ -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(),
};
}

Expand Down Expand Up @@ -770,7 +770,7 @@ impl Binary {
}
}
}
SimplifyResult::SimplifiedToInstruction(None)
SimplifyResult::could_not_simplify()
}

/// Evaluate the two constants with the operation specified by self.operator.
Expand Down Expand Up @@ -926,3 +926,9 @@ pub(crate) enum SimplifyResult {
/// If so it will be included here.
SimplifiedToInstruction(Option<Instruction>),
}

impl SimplifyResult {
fn could_not_simplify() -> Self {
SimplifyResult::SimplifiedToInstruction(None)
}
}

0 comments on commit a7736eb

Please sign in to comment.