diff --git a/src/mir/phi_core/conservative.rs b/src/mir/phi_core/conservative.rs index a14d7609..e6b201af 100644 --- a/src/mir/phi_core/conservative.rs +++ b/src/mir/phi_core/conservative.rs @@ -7,12 +7,44 @@ //! Phase 25.1q: Conservative PHI戦略の一元化 //! - phi.rs の Conservative PHI ロジック(L23-117)を統一 //! - void emission、predecessor fallback の一貫性保証 +//! +//! # Phase 35-39 PHI削減計画 +//! +//! ## 概要 +//! +//! Conservative ∘ Elimination = Minimal SSA の理論に基づく保守的PHI生成。 +//! 非対称分岐(else なし)での void emission と predecessor fallback を扱う。 +//! +//! ## 🔜 Phase 40で部分削除予定(Level 2、30行) +//! +//! - ConservativeMerge struct inline (30行) +//! - **Phase 40-4**: JoinIrConservativeAnalyzerに吸収 +//! - **保持**: 理論コメント、Conservative ∘ Elimination説明 +//! - **削除**: 構造体定義、実装コード +//! +//! ## ⏳ Phase 41+で完全削除予定(Level 3、約138行) +//! +//! - 残りのconservative logic全て +//! - **削除条件**: JoinIR Verifier完全移行 +//! - **難易度**: MEDIUM-HIGH +//! +//! ## 参照 +//! +//! - Phase 37分析: `docs/.../phase-37-if-phi-reduction/conservative_responsibility_table.md` +//! - Phase 39設計: `docs/.../phase-39-if-phi-level2/joinir_extension_design.md` use crate::mir::ValueId; use std::collections::{BTreeMap, HashSet}; // Phase 25.1: BTreeMap化 /// Conservative PHI 戦略による変数マージ分析 +/// +/// # Phase 40削減計画 +/// +/// - **Phase 40-4**: JoinIrConservativeAnalyzerにインライン化 +/// - **削減効果**: 30行(構造体定義+実装) +/// - **保持**: Conservative ∘ Elimination理論コメント pub struct ConservativeMerge { + // TODO(Phase 40-4): JoinIrConservativeAnalyzerに移行予定 /// 全ブランチに存在する変数のユニオン(Conservative戦略) pub all_vars: HashSet, /// 実際に変更された変数のセット(デバッグ/ヒント用) @@ -26,11 +58,15 @@ impl ConservativeMerge { /// * `pre_if` - if文前のスナップショット /// * `then_end` - then-branch終了時の変数マップ /// * `else_end_opt` - else-branch終了時の変数マップ(Noneの場合はempty else) + /// + /// # Phase 40-2で使用される(compute_modified_names削除時) pub fn analyze( pre_if: &BTreeMap, // Phase 25.1: BTreeMap化 then_end: &BTreeMap, // Phase 25.1: BTreeMap化 else_end_opt: &Option>, // Phase 25.1: BTreeMap化 ) -> Self { + // TODO(Phase 40-2/40-4): JoinIR Verifierに移行 + let mut all_vars = HashSet::new(); all_vars.extend(pre_if.keys().cloned()); all_vars.extend(then_end.keys().cloned()); diff --git a/src/mir/phi_core/if_phi.rs b/src/mir/phi_core/if_phi.rs index 139abfca..9497ea6d 100644 --- a/src/mir/phi_core/if_phi.rs +++ b/src/mir/phi_core/if_phi.rs @@ -4,6 +4,60 @@ * Public thin wrappers that mirror the semantics of existing builder::phi * helpers. Implemented locally to avoid depending on private submodules. * Behavior is identical to the current in-tree logic. + * + * # Phase 35-39 PHI削減計画 + * + * このファイルは段階的に縮退・削除される。各関数のPhase別削除計画を以下に示す。 + * + * ## ✅ Phase 38で削除済み(Level 1、90行) + * + * - `merge_modified_with_control` (51行, dead code, 呼び出し0) + * - `extract_assigned_var` (39行, JoinIR AST lowering replacement) + * + * **if_phi.rs**: 315行 → 225行(28.6%削減達成) + * + * ## 🔜 Phase 40で削除予定(Level 2、115行) + * + * - `collect_assigned_vars` (32行) + * - **削除条件**: array_ext.filter実装完了(if-in-loop AST lowering) + * - **置換**: JoinIR Frontend `extract_if_in_loop_modified_vars()` + * - **callsites**: loop_builder.rs:1069, 1075 + * - **Phase**: Phase 40-1 + * + * - `compute_modified_names` (26行) + * - **削除条件**: JoinIR Verifier conservative migration完了 + * - **置換**: `JoinIrConservativeAnalyzer` + * - **callsites**: conservative.rs:42 + * - **Phase**: Phase 40-2 + * + * - `merge_with_reset_at_merge_with` (27行) + * - **削除条件**: Full IfMerge with reset semantics実装 + * - **置換**: IfMerge reset extension + * - **Phase**: Phase 40-3 + * + * - conservative.rs struct inline (30行) + * - **Phase**: Phase 40-4 + * + * **予想**: if_phi.rs: 225行 → 110行(51%削減) + * + * ## ⏳ Phase 41+で削除予定(Level 3、約300行) + * + * - `merge_modified_at_merge_with` (63行, コアPHI生成ロジック) + * - **削除条件**: Stage-1/Stage-B/selfhost完全JoinIRカバレッジ + * - **難易度**: HIGH + * - **依存**: 全If/Loop統合、PhiMergeOps trait置換 + * + * - その他複雑PHI生成関数(約237行) + * - **削除条件**: Level 3基準達成(JoinIR 92%カバレッジ) + * + * **予想**: if_phi.rs: 110行 → ほぼ削除 or 最小ユーティリティのみ + * + * ## 参照ドキュメント + * + * - 全体計画: `docs/private/roadmap2/phases/phi-reduction-series/INDEX.md` + * - Phase 37設計: `docs/private/roadmap2/phases/phase-37-if-phi-reduction/` + * - Phase 39詳細: `docs/private/roadmap2/phases/phase-39-if-phi-level2/` + * - Phase 40ロードマップ: `docs/.../phase-39-if-phi-level2/deletion_sequence_detailed.md` */ use crate::ast::ASTNode; @@ -35,9 +89,45 @@ pub fn infer_type_from_phi( } +// ======================================== +// Phase 40削除予定(Level 2) +// ======================================== + +/// ループ内if文の代入変数収集 +/// /// Collect all variable names that are assigned within the given AST subtree. /// Useful for computing PHI merge candidates across branches/blocks. +/// +/// # Phase 40削減計画 +/// +/// - **削除予定**: Phase 40-1(array_ext.filter実装後) +/// - **置換先**: JoinIR Frontend `extract_if_in_loop_modified_vars()` +/// - **削除条件**: +/// - ✅ array_ext.filter via JoinIR Frontend実装完了 +/// - ✅ A/Bテスト(旧PHIパス vs JoinIRパス)PASS +/// - ✅ この関数の全callsiteでJoinIR置換確認 +/// - **callsites**: 2箇所 +/// - loop_builder.rs:1069 (if-in-loop pattern) +/// - loop_builder.rs:1075 (if-in-loop pattern) +/// - **削減効果**: 32行 +/// +/// # Migration Path +/// +/// ```rust,ignore +/// // Before (Phase 38以前) +/// let vars = if_phi::collect_assigned_vars(loop_body, var_map); +/// +/// // After (Phase 40) +/// // JoinIR Frontend AST loweringで自動収集 +/// // ast_lowerer.extract_if_in_loop_modified_vars()が代替 +/// ``` +/// +/// # See Also +/// - Design: `docs/.../phase-39-if-phi-level2/joinir_extension_design.md` +/// - Replacement: `src/mir/join_ir/frontend/ast_lowerer.rs::extract_if_in_loop_modified_vars()` pub fn collect_assigned_vars(ast: &ASTNode, out: &mut std::collections::BTreeSet) { + // TODO(Phase 40-1): この関数は削除予定 + // JoinIR Frontend if-in-loop AST loweringに置き換え match ast { ASTNode::Assignment { target, .. } => { if let ASTNode::Variable { name, .. } = target.as_ref() { @@ -71,13 +161,45 @@ pub fn collect_assigned_vars(ast: &ASTNode, out: &mut std::collections::BTreeSet } } +/// 保守的修正名前計算 +/// /// Compute the set of variable names whose values changed in either branch /// relative to the pre-if snapshot. +/// +/// # Purpose +/// Conservative PHI生成戦略で修正される変数名を計算。 +/// 非対称分岐(else なし)でのvoid emission用。 +/// +/// # Phase 40削減計画 +/// +/// - **削除予定**: Phase 40-2(JoinIR Verifier conservative migration後) +/// - **置換先**: `JoinIrConservativeAnalyzer` +/// - **削除条件**: +/// - ✅ ConservativeMerge logic → JoinIR Verifier migration完了 +/// - ✅ Conservative value propagation in JoinIR実装 +/// - **callsites**: 1箇所 +/// - conservative.rs:42 (ConservativeMerge::analyze) +/// - **削減効果**: 26行 +/// +/// # Migration Path +/// +/// ```rust,ignore +/// // Before +/// let modified = if_phi::compute_modified_names(if_expr); +/// +/// // After (Phase 40) +/// // JoinIR Verifier内でconservative分析 +/// let analyzer = JoinIrConservativeAnalyzer::new(); +/// let modified = analyzer.compute_modified_names(join_ir_func); +/// ``` pub fn compute_modified_names( pre_if_snapshot: &BTreeMap, then_map_end: &BTreeMap, else_map_end_opt: &Option>, ) -> Vec { + // TODO(Phase 40-2): JoinIR Verifierに移行予定 + // JoinIrConservativeAnalyzerに置き換え + use std::collections::BTreeSet; // 決定的順序のためBTreeSet使用 let mut names: BTreeSet<&str> = BTreeSet::new(); @@ -122,8 +244,31 @@ pub trait PhiMergeOps { } } +// ======================================== +// Phase 41+削除予定(Level 3、HIGH難易度) +// ======================================== + +/// 複雑PHI生成(コアロジック) +/// /// Merge variables modified in branches at the merge block using provided ops. /// Handles both two-pred and single-pred (reachable) cases gracefully. +/// +/// # Purpose +/// If/Loop統合時の複雑なPHI生成。PhiMergeOps traitのコア実装。 +/// +/// # Phase 41+削減計画 +/// +/// - **削除予定**: Phase 41+(Stage-1/Stage-B/selfhost完全カバレッジ後) +/// - **難易度**: HIGH +/// - **削除条件**: +/// - ❌ JoinIR Frontend 92%カバレッジ達成 +/// - ❌ PhiMergeOps trait完全置換 +/// - ❌ Stage-1/Stage-B/selfhost大半のIf/Loop動作 +/// - **削減効果**: 63行 +/// +/// # Migration Path +/// +/// Phase 41+でJoinIR Frontend完全実装後に設計予定。 pub fn merge_modified_at_merge_with( ops: &mut O, merge_bb: crate::mir::BasicBlockId, @@ -136,6 +281,9 @@ pub fn merge_modified_at_merge_with( else_map_end_opt: &Option>, skip_var: Option<&str>, ) -> Result<(), String> { + // TODO(Phase 41+): Stage-1/Stage-B完全実装が前提 + // JoinIR Frontend完全カバレッジ後に削除 + let trace = std::env::var("NYASH_IF_TRACE").ok().as_deref() == Some("1"); let changed = compute_modified_names(pre_if_snapshot, then_map_end, else_map_end_opt); for name in changed { @@ -192,9 +340,17 @@ pub fn merge_modified_at_merge_with( Ok(()) } +/// PHI生成(reset semantics付き) +/// /// Convenience wrapper: reset variable map (via a caller-provided closure) /// then perform merge at the merge block. Keeps caller simple while /// avoiding tying phi_core to concrete builder internals. +/// +/// # Phase 40削減計画 +/// +/// - **削除予定**: Phase 40-3(Full IfMerge with reset実装後) +/// - **置換先**: IfMerge reset extension +/// - **削減効果**: 27行 pub fn merge_with_reset_at_merge_with( ops: &mut O, merge_bb: crate::mir::BasicBlockId, @@ -208,6 +364,8 @@ pub fn merge_with_reset_at_merge_with( reset_vars: impl FnOnce(), skip_var: Option<&str>, ) -> Result<(), String> { + // TODO(Phase 40-3): IfMerge reset semantics拡張で置き換え + reset_vars(); merge_modified_at_merge_with( ops,