diff --git a/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs b/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs index 96a24583837..4bf01844b56 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs @@ -592,11 +592,8 @@ impl<'f> PerFunctionContext<'f> { /// This is expected to contain any loads which were replaced and any stores which are /// no longer needed. fn remove_instructions(&mut self) { - // The order we iterate blocks in is not important - for block in self.post_order.as_slice() { - self.inserter.function.dfg[*block] - .instructions_mut() - .retain(|instruction| !self.instructions_to_remove.contains(instruction)); + for instruction in &self.instructions_to_remove { + self.inserter.function.dfg.remove_instruction(*instruction); } } diff --git a/compiler/noirc_evaluator/src/ssa/opt/rc.rs b/compiler/noirc_evaluator/src/ssa/opt/rc.rs index e25ad350145..86aa9840542 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/rc.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/rc.rs @@ -64,7 +64,9 @@ impl Function { context.find_rcs_in_entry_block(self); context.scan_for_array_sets(self); let to_remove = context.find_rcs_to_remove(self); - remove_instructions(to_remove, self); + for instruction in to_remove { + self.dfg.remove_instruction(instruction); + } } } @@ -141,16 +143,6 @@ pub(crate) fn pop_rc_for( Some(rcs.remove(position)) } -fn remove_instructions(to_remove: HashSet, function: &mut Function) { - if !to_remove.is_empty() { - for block in function.reachable_blocks() { - function.dfg[block] - .instructions_mut() - .retain(|instruction| !to_remove.contains(instruction)); - } - } -} - #[cfg(test)] mod test { use std::sync::Arc;