refactor(joinir): Eliminate block allocation duplication with BlockAllocator
Problem: - joinir_block_converter.rs had 4 duplicate block allocation patterns - Lines 260-265, 487-489, 635-639, 740-748 all manually allocating BasicBlockIds - Total duplication: 27 lines across 4 sites Solution: - Use existing BlockAllocator (Phase 260 P0.2) for all block allocations - Replace manual patterns with allocate_two(), allocate_three(), allocate_n() Changes: - Site 1/4 (handle_conditional_method_call): 6 lines → 3 lines (-3) - Site 2/4 (handle_jump): 4 lines → 3 lines (-1) - Site 3/4 (handle_if_merge): 6 lines → 3 lines (-3) - Site 4/4 (handle_nested_if_merge): 11 lines → 8 lines (-3) Result: - ✅ 10 lines eliminated - ✅ Improved readability (clear intent) - ✅ Improved maintainability (SSOT for allocation) - ✅ No behavior change (all tests pass) - ✅ No regression: phase269_p1_2, phase259_p0, phase269_p0 PASS Follow-up potential: - MergeVariableHandlerBox duplication (lines 652-660, 667-675, 785-791, 800-806) - CallInstructionGeneratorBox duplication (lines 403-406, 511-514, 536-546) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@ -12,6 +12,7 @@ use crate::mir::{BasicBlockId, EffectMask, MirFunction, MirInstruction, MirType,
|
||||
use crate::mir::types::ConstValue;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use super::block_allocator::BlockAllocator; // Phase 269 P1.2+: Use BlockAllocator for deduplication
|
||||
use super::{convert_mir_like_inst, join_func_name, JoinIrVmBridgeError};
|
||||
|
||||
fn log_dbg(message: impl AsRef<str>) {
|
||||
@ -257,12 +258,10 @@ impl JoinIrBlockConverter {
|
||||
);
|
||||
|
||||
let cond_block = self.current_block_id;
|
||||
let then_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let else_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let merge_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
// Phase 269 P1.2+: Use BlockAllocator to eliminate duplication (Site 1/4)
|
||||
let mut allocator = BlockAllocator::new(self.next_block_id);
|
||||
let (then_block, else_block, merge_block) = allocator.allocate_three();
|
||||
self.next_block_id = allocator.peek_next();
|
||||
|
||||
// cond block: branch
|
||||
let branch_terminator = MirInstruction::Branch {
|
||||
@ -483,10 +482,10 @@ impl JoinIrBlockConverter {
|
||||
match cond {
|
||||
Some(cond_var) => {
|
||||
// Conditional jump → Branch + tail call to continuation
|
||||
let exit_block_id = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let continue_block_id = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
// Phase 269 P1.2+: Use BlockAllocator (Site 2/4)
|
||||
let mut allocator = BlockAllocator::new(self.next_block_id);
|
||||
let (exit_block_id, continue_block_id) = allocator.allocate_two();
|
||||
self.next_block_id = allocator.peek_next();
|
||||
|
||||
let branch_terminator = MirInstruction::Branch {
|
||||
condition: *cond_var,
|
||||
@ -631,12 +630,10 @@ impl JoinIrBlockConverter {
|
||||
);
|
||||
|
||||
let cond_block = self.current_block_id;
|
||||
let then_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let else_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let merge_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
// Phase 269 P1.2+: Use BlockAllocator (Site 3/4)
|
||||
let mut allocator = BlockAllocator::new(self.next_block_id);
|
||||
let (then_block, else_block, merge_block) = allocator.allocate_three();
|
||||
self.next_block_id = allocator.peek_next();
|
||||
|
||||
// cond block: branch
|
||||
let branch_terminator = MirInstruction::Branch {
|
||||
@ -732,20 +729,16 @@ impl JoinIrBlockConverter {
|
||||
);
|
||||
|
||||
let num_conds = conds.len();
|
||||
// Phase 269 P1.2+: Use BlockAllocator (Site 4/4)
|
||||
let mut allocator = BlockAllocator::new(self.next_block_id);
|
||||
|
||||
let mut level_blocks: Vec<BasicBlockId> = Vec::with_capacity(num_conds);
|
||||
level_blocks.push(self.current_block_id);
|
||||
// Allocate level 1..num_conds blocks
|
||||
level_blocks.extend(allocator.allocate_n(num_conds - 1));
|
||||
|
||||
for _ in 1..num_conds {
|
||||
level_blocks.push(BasicBlockId(self.next_block_id));
|
||||
self.next_block_id += 1;
|
||||
}
|
||||
|
||||
let then_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let final_else_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let merge_block = BasicBlockId(self.next_block_id);
|
||||
self.next_block_id += 1;
|
||||
let (then_block, final_else_block, merge_block) = allocator.allocate_three();
|
||||
self.next_block_id = allocator.peek_next();
|
||||
|
||||
// Pre-create level 1+ blocks
|
||||
for level in 1..num_conds {
|
||||
|
||||
Reference in New Issue
Block a user