Skip to content

Commit

Permalink
chore: factor out a function to replace a set of ValueIds
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Sep 2, 2023
1 parent 848907a commit e160f84
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions crates/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ impl Context {

// If a copy of this instruction exists earlier in the block, then reuse the previous results.
if let Some(cached_results) = instruction_result_cache.get(&instruction) {
for (old_result, new_result) in old_results.iter().zip(cached_results) {
dfg.set_value_from_id(*old_result, *new_result);
}
Self::replace_result_ids(dfg, &old_results, cached_results);
return;
}

Expand All @@ -103,9 +101,7 @@ impl Context {
if instruction.is_pure(dfg) {
instruction_result_cache.insert(instruction, new_results.clone());
}
for (old_result, new_result) in old_results.iter().zip(new_results) {
dfg.set_value_from_id(*old_result, new_result);
}
Self::replace_result_ids(dfg, &old_results, &new_results);
}

/// Fetches an [`Instruction`] by its [`InstructionId`] and fully resolves its inputs.
Expand Down Expand Up @@ -145,6 +141,17 @@ impl Context {

new_results
}

/// Replaces a set of [`ValueId`]s inside the [`DataFlowGraph`] with another.
fn replace_result_ids(
dfg: &mut DataFlowGraph,
old_results: &[ValueId],
new_results: &[ValueId],
) {
for (old_result, new_result) in old_results.iter().zip(new_results) {
dfg.set_value_from_id(*old_result, *new_result);
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit e160f84

Please sign in to comment.