Skip to content

Commit

Permalink
chore(ssa refactor): Remove extra 'enable_side_effects' instructions (#…
Browse files Browse the repository at this point in the history
…1828)

Remove extra 'enable_side_effects' instructions
  • Loading branch information
jfecher authored Jun 26, 2023
1 parent e068fd4 commit 22be6be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 8 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor/ir/dfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl DataFlowGraph {
ctrl_typevars: Option<Vec<Type>>,
) -> InsertInstructionResult {
use InsertInstructionResult::*;
match instruction.simplify(self) {
match instruction.simplify(self, block) {
SimplifyResult::SimplifiedTo(simplification) => SimplifiedTo(simplification),
SimplifyResult::Remove => InstructionRemoved,
SimplifyResult::None => {
Expand Down Expand Up @@ -371,6 +371,12 @@ impl std::ops::Index<InstructionId> for DataFlowGraph {
}
}

impl std::ops::IndexMut<InstructionId> for DataFlowGraph {
fn index_mut(&mut self, id: InstructionId) -> &mut Self::Output {
&mut self.instructions[id]
}
}

impl std::ops::Index<ValueId> for DataFlowGraph {
type Output = Value;
fn index(&self, id: ValueId) -> &Self::Output {
Expand All @@ -387,7 +393,7 @@ impl std::ops::Index<BasicBlockId> for DataFlowGraph {

impl std::ops::IndexMut<BasicBlockId> for DataFlowGraph {
/// Get a mutable reference to a function's basic block for the given id.
fn index_mut(&mut self, id: BasicBlockId) -> &mut BasicBlock {
fn index_mut(&mut self, id: BasicBlockId) -> &mut Self::Output {
&mut self.blocks[id]
}
}
Expand Down
16 changes: 14 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ impl Instruction {

/// Try to simplify this instruction. If the instruction can be simplified to a known value,
/// that value is returned. Otherwise None is returned.
pub(crate) fn simplify(&self, dfg: &mut DataFlowGraph) -> SimplifyResult {
///
/// The `block` parameter indicates the block this new instruction will be inserted into
/// after this call.
pub(crate) fn simplify(&self, dfg: &mut DataFlowGraph, block: BasicBlockId) -> SimplifyResult {
use SimplifyResult::*;
match self {
Instruction::Binary(binary) => binary.simplify(dfg),
Expand Down Expand Up @@ -309,10 +312,19 @@ impl Instruction {
}
}
Instruction::Call { func, arguments } => simplify_call(*func, arguments, dfg),
Instruction::EnableSideEffects { condition } => {
if let Some(last) = dfg[block].instructions().last().copied() {
let last = &mut dfg[last];
if matches!(last, Instruction::EnableSideEffects { .. }) {
*last = Instruction::EnableSideEffects { condition: *condition };
return Remove;
}
}
None
}
Instruction::Allocate { .. } => None,
Instruction::Load { .. } => None,
Instruction::Store { .. } => None,
Instruction::EnableSideEffects { .. } => None,
}
}
}
Expand Down

0 comments on commit 22be6be

Please sign in to comment.