diff --git a/src/mir/basic_block.rs b/src/mir/basic_block.rs index 37d5040e..88953c72 100644 --- a/src/mir/basic_block.rs +++ b/src/mir/basic_block.rs @@ -102,6 +102,20 @@ impl BasicBlock { } } + /// Add instruction before terminator (for edge-copy in PHI-off mode) + /// If no terminator exists, behaves like add_instruction() + pub fn add_instruction_before_terminator(&mut self, instruction: MirInstruction) { + // Update effect mask + self.effects = self.effects | instruction.effects(); + + // Non-terminator instructions always go into instructions vec + if !self.is_terminator(&instruction) { + self.instructions.push(instruction); + } else { + panic!("Cannot add terminator via add_instruction_before_terminator"); + } + } + /// Check if an instruction is a terminator fn is_terminator(&self, instruction: &MirInstruction) -> bool { matches!( diff --git a/src/runner/json_v0_bridge/lowering/merge.rs b/src/runner/json_v0_bridge/lowering/merge.rs index 3a20a312..9628dae0 100644 --- a/src/runner/json_v0_bridge/lowering/merge.rs +++ b/src/runner/json_v0_bridge/lowering/merge.rs @@ -35,11 +35,12 @@ pub(super) fn merge_values( } let dst = f.next_value_id(); if no_phi { + // Insert edge-copy BEFORE terminator (critical fix for ValueId undefined errors) if let Some(bb) = f.get_block_mut(pred_a) { - bb.add_instruction(MirInstruction::Copy { dst, src: val_a }); + bb.add_instruction_before_terminator(MirInstruction::Copy { dst, src: val_a }); } if let Some(bb) = f.get_block_mut(pred_b) { - bb.add_instruction(MirInstruction::Copy { dst, src: val_b }); + bb.add_instruction_before_terminator(MirInstruction::Copy { dst, src: val_b }); } } else if let Some(bb) = f.get_block_mut(merge_bb) { bb.insert_instruction_after_phis(MirInstruction::Phi {