Skip to content

Commit

Permalink
chore: remove SimplifyResult::None
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jul 20, 2023
1 parent 7b5915d commit 429ccd4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
8 changes: 2 additions & 6 deletions crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,8 @@ impl DataFlowGraph {
SimplifiedToMultiple(simplification)
}
SimplifyResult::Remove => InstructionRemoved,
result @ (SimplifyResult::SimplifiedToInstruction(_) | SimplifyResult::None) => {
let instruction = match result {
SimplifyResult::SimplifiedToInstruction(new_instruction) => new_instruction,
SimplifyResult::None => instruction,
_ => unreachable!("handled by above match statement"),
};
SimplifyResult::SimplifiedToInstruction(simplified_instruction) => {
let instruction = simplified_instruction.unwrap_or(instruction);
let id = self.make_instruction(instruction, ctrl_typevars);
self.blocks[block].insert_instruction(id);
if let Some(location) = location {
Expand Down
64 changes: 33 additions & 31 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 {
None
SimplifiedToInstruction(None)
}
}
_ => None,
_ => SimplifiedToInstruction(None),
}
}
Instruction::Constrain(value) => {
Expand All @@ -292,7 +292,7 @@ impl Instruction {
return Remove;
}
}
None
SimplifiedToInstruction(None)
}
Instruction::ArrayGet { array, index } => {
let array = dfg.get_array_constant(*array);
Expand All @@ -304,7 +304,7 @@ impl Instruction {
return SimplifiedTo(array[index]);
}
}
None
SimplifiedToInstruction(None)
}
Instruction::ArraySet { array, index, value } => {
let array = dfg.get_array_constant(*array);
Expand All @@ -318,15 +318,15 @@ impl Instruction {
return SimplifiedTo(new_array);
}
}
None
SimplifiedToInstruction(None)
}
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 {
None
SimplifiedToInstruction(None)
}
}
Instruction::Call { func, arguments } => simplify_call(*func, arguments, dfg),
Expand All @@ -338,11 +338,11 @@ impl Instruction {
return Remove;
}
}
None
SimplifiedToInstruction(None)
}
Instruction::Allocate { .. } | Instruction::Load { .. } | Instruction::Store { .. } => {
SimplifiedToInstruction(None)
}
Instruction::Allocate { .. } => None,
Instruction::Load { .. } => None,
Instruction::Store { .. } => None,
}
}
}
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()))
}
_ => None,
_ => SimplifiedToInstruction(None),
}
} else if *dst_typ == dfg.type_of_value(value) {
SimplifiedTo(value)
} else {
None
SimplifiedToInstruction(None)
}
}

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 None,
_ => return SimplifiedToInstruction(None),
};

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 {
None
SimplifiedToInstruction(None)
}
}
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 {
None
SimplifiedToInstruction(None)
}
}
Intrinsic::ArrayLen => {
Expand All @@ -430,7 +430,7 @@ fn simplify_call(func: ValueId, arguments: &[ValueId], dfg: &mut DataFlowGraph)
);
SimplifiedTo(slice_len)
} else {
None
SimplifiedToInstruction(None)
}
}
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 {
None
SimplifiedToInstruction(None)
}
}
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 {
None
SimplifiedToInstruction(None)
}
}
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 {
None
SimplifiedToInstruction(None)
}
}
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 {
None
SimplifiedToInstruction(None)
}
}
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 {
None
SimplifiedToInstruction(None)
}
}
Intrinsic::SliceRemove => {
Expand All @@ -496,10 +496,12 @@ 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 {
None
SimplifiedToInstruction(None)
}
}
Intrinsic::BlackBox(_) | Intrinsic::Println | Intrinsic::Sort => None,
Intrinsic::BlackBox(_) | Intrinsic::Println | Intrinsic::Sort => {
SimplifiedToInstruction(None)
}
}
}

Expand Down Expand Up @@ -673,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::None,
None => SimplifyResult::SimplifiedToInstruction(None),
};
}

Expand Down Expand Up @@ -740,7 +742,7 @@ impl Binary {
if operand_type == Type::bool() {
// Boolean AND is equivalent to multiplication, which is a cheaper operation.
let instruction = Instruction::binary(BinaryOp::Mul, self.lhs, self.rhs);
return SimplifyResult::SimplifiedToInstruction(instruction);
return SimplifyResult::SimplifiedToInstruction(Some(instruction));
}
}
BinaryOp::Or => {
Expand Down Expand Up @@ -768,7 +770,7 @@ impl Binary {
}
}
}
SimplifyResult::None
SimplifyResult::SimplifiedToInstruction(None)
}

/// Evaluate the two constants with the operation specified by self.operator.
Expand Down Expand Up @@ -915,12 +917,12 @@ pub(crate) enum SimplifyResult {
/// a function such as a tuple
SimplifiedToMultiple(Vec<ValueId>),

/// Replace this function with an simpler but equivalent function.
SimplifiedToInstruction(Instruction),

/// Remove the instruction, it is unnecessary
Remove,

/// Instruction could not be simplified
None,
/// Instruction cannot be simplified to a known value.
///
/// The instruction may be able to be replaced with simpler but equivalent instruction.
/// If so it will be included here.
SimplifiedToInstruction(Option<Instruction>),
}

0 comments on commit 429ccd4

Please sign in to comment.