From 6bdfd49b2066c4ee9e8e55ded25c129f3d845f7f Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:14:07 +0200 Subject: [PATCH] fix: do not perform dead instruction elimination on mod,div unless rhs is constant (#3141) --- .../noirc_evaluator/src/ssa/ir/instruction.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ir/instruction.rs b/compiler/noirc_evaluator/src/ssa/ir/instruction.rs index f748aa040a1..badc1e82d50 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/instruction.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/instruction.rs @@ -239,10 +239,19 @@ impl Instruction { pub(crate) fn has_side_effects(&self, dfg: &DataFlowGraph) -> bool { use Instruction::*; - match self { - Binary(_) - | Cast(_, _) + Binary(binary) => { + if matches!(binary.operator, BinaryOp::Div | BinaryOp::Mod) { + if let Some(rhs) = dfg.get_numeric_constant(binary.rhs) { + rhs == FieldElement::zero() + } else { + true + } + } else { + false + } + } + Cast(_, _) | Not(_) | Truncate { .. } | Allocate