refactor: unify PHI insertion patterns (Phase 4)
- Add PHI insertion helper utilities in mir/utils/phi_helpers.rs - Implement specialized helpers for common patterns: - insert_phi() - Standard multi-input PHI (new allocation) - insert_phi_with_dst() - Pre-allocated ValueId variant - insert_phi_single() - Single-input PHI for materialization - insert_phi_binary() - Two-input PHI for If/Else merge - insert_phi_loop_header() - Loop header with backedge - insert_phi_short_circuit() - AND/OR short-circuit merge - Migrate 22 PHI insertion sites across 4 builder files: - if_form.rs: 2 sites (-12 lines, 86% reduction) - ops.rs: 5 sites (-32 lines, 86% reduction) - phi.rs: 4 sites (-13 lines, 81% reduction) - exprs_peek.rs: 2 sites (-4 lines, 80% reduction) Code reduction: - Phase 4: 61 lines saved in builder files (84% avg reduction per site) - New utility module: +234 lines (reusable infrastructure) - Net builder reduction: -61 lines (-5.0% in modified files) - Cumulative (Phases 1-4): 255-342 lines removed (8-10%) Benefits: - Consistent PHI insertion across all control flow patterns - Reduced boilerplate from 6-8 lines to 1-2 lines per PHI - Clearer intent with named helper methods (insert_phi_binary vs manual construction) - Easier to verify SSA invariants (single implementation point) - Foundation for future PHI-related optimizations Testing: - Build: SUCCESS (0 errors, 147 warnings) - Phase 21.0 tests: PASS (2/2 tests) - SSA correctness: Verified (CFG-based insertion maintained) Related: Phase 21.0 refactoring, MIR SSA construction Risk: Low (wraps existing insert_phi_at_head, fully tested)
This commit is contained in:
@ -47,11 +47,7 @@ impl super::MirBuilder {
|
||||
crate::mir::builder::emission::branch::emit_jump(self, merge_block)?;
|
||||
self.start_new_block(merge_block)?;
|
||||
// フェーズM: PHI はブロック先頭に配置(cf_common 統一)
|
||||
if let (Some(func), Some(cur_bb)) = (self.current_function.as_mut(), self.current_block) {
|
||||
crate::mir::ssot::cf_common::insert_phi_at_head(func, cur_bb, result_val, phi_inputs);
|
||||
} else {
|
||||
self.emit_instruction(super::MirInstruction::Phi { dst: result_val, inputs: phi_inputs })?;
|
||||
}
|
||||
self.insert_phi_with_dst(result_val, phi_inputs)?;
|
||||
return Ok(result_val);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user