|
| 1 | +//! Phase 4: Merge single-predecessor Jump chains until fixed point. |
| 2 | +//! |
| 3 | +//! For each block A with terminator `Jump { target: B, args }` where: |
| 4 | +//! - A != B (self-loop guard) |
| 5 | +//! - B has exactly one predecessor (A) |
| 6 | +//! - B is not the entry block |
| 7 | +//! |
| 8 | +//! Lower B's params as Let bindings (parallel-copy semantics), then |
| 9 | +//! merge B's body and spans into A. |
| 10 | +//! |
| 11 | +//! Runs to fixed point for transitive chains (A → B → C all merge into A). |
| 12 | +//! After fixed point, runs a final compaction to remove dead blocks. |
| 13 | +
|
| 14 | +use rustc_hash::FxHashSet; |
| 15 | + |
| 16 | +use crate::graph::compute_pred_counts; |
| 17 | +use crate::ir::{ArcFunction, ArcInstr, ArcTerminator, ArcValue, ArcVarId, ValueRepr}; |
| 18 | + |
| 19 | +use super::compact::compact_blocks; |
| 20 | + |
| 21 | +/// Merge single-predecessor Jump chains until fixed point. |
| 22 | +pub(crate) fn merge_jump_chains(func: &mut ArcFunction) { |
| 23 | + let mut dead: FxHashSet<usize> = FxHashSet::default(); |
| 24 | + |
| 25 | + loop { |
| 26 | + let mut changed = false; |
| 27 | + let pred_counts = compute_pred_counts(func); |
| 28 | + |
| 29 | + for a_idx in 0..func.blocks.len() { |
| 30 | + if dead.contains(&a_idx) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + let (b_idx, jump_args) = { |
| 35 | + let ArcTerminator::Jump { target, args } = &func.blocks[a_idx].terminator else { |
| 36 | + continue; |
| 37 | + }; |
| 38 | + let b_idx = target.index(); |
| 39 | + |
| 40 | + // Self-loop guard. |
| 41 | + if a_idx == b_idx { |
| 42 | + continue; |
| 43 | + } |
| 44 | + // B must have exactly one predecessor. |
| 45 | + if pred_counts[b_idx] != 1 { |
| 46 | + continue; |
| 47 | + } |
| 48 | + // B must not be the entry block. |
| 49 | + if b_idx == func.entry.index() { |
| 50 | + continue; |
| 51 | + } |
| 52 | + // B must not already be dead. |
| 53 | + if dead.contains(&b_idx) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + (b_idx, args.clone()) |
| 58 | + }; |
| 59 | + |
| 60 | + let b_params = func.blocks[b_idx].params.clone(); |
| 61 | + |
| 62 | + // Arity check: Jump args must match target block params. |
| 63 | + debug_assert_eq!( |
| 64 | + b_params.len(), |
| 65 | + jump_args.len(), |
| 66 | + "Jump args/params arity mismatch: block {a_idx} → block {b_idx}", |
| 67 | + ); |
| 68 | + if b_params.len() != jump_args.len() { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + // Lower parallel-copy semantics: block params → Let bindings. |
| 73 | + lower_parallel_copy(func, a_idx, &b_params, &jump_args); |
| 74 | + |
| 75 | + // Remap COW annotations: B's entries → A's coordinates. |
| 76 | + let offset = func.blocks[a_idx].body.len(); |
| 77 | + func.cow_annotations.remap_block_merge(b_idx, a_idx, offset); |
| 78 | + |
| 79 | + // Merge B's body into A. |
| 80 | + let b_body: Vec<ArcInstr> = func.blocks[b_idx].body.drain(..).collect(); |
| 81 | + func.blocks[a_idx].body.extend(b_body); |
| 82 | + |
| 83 | + // Merge B's spans into A. |
| 84 | + let b_spans: Vec<Option<ori_ir::Span>> = func.spans[b_idx].drain(..).collect(); |
| 85 | + func.spans[a_idx].extend(b_spans); |
| 86 | + |
| 87 | + // Replace A's terminator with B's. |
| 88 | + let b_term = std::mem::replace( |
| 89 | + &mut func.blocks[b_idx].terminator, |
| 90 | + ArcTerminator::Unreachable, |
| 91 | + ); |
| 92 | + func.blocks[a_idx].terminator = b_term; |
| 93 | + |
| 94 | + // Mark B as dead. |
| 95 | + dead.insert(b_idx); |
| 96 | + changed = true; |
| 97 | + } |
| 98 | + |
| 99 | + if !changed { |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Final compaction: remove dead blocks. |
| 105 | + if !dead.is_empty() { |
| 106 | + compact_blocks(func); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/// Lower block-param parallel-copy semantics to sequential Let bindings. |
| 111 | +/// |
| 112 | +/// Jump args are parallel phi inputs — all args are read before any param |
| 113 | +/// is written. When no arg aliases a target param, direct Let is safe. |
| 114 | +/// When overlap exists (e.g., swap: `Jump { args: [p1, p0] }` → params |
| 115 | +/// `[p0, p1]`), we use fresh temps to avoid clobbering. |
| 116 | +fn lower_parallel_copy( |
| 117 | + func: &mut ArcFunction, |
| 118 | + block_idx: usize, |
| 119 | + params: &[(ArcVarId, ori_types::Idx)], |
| 120 | + args: &[ArcVarId], |
| 121 | +) { |
| 122 | + if params.is_empty() { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + // Check for overlap: does any arg alias a target param? |
| 127 | + let param_vars: FxHashSet<ArcVarId> = params.iter().map(|(v, _)| *v).collect(); |
| 128 | + let has_overlap = args.iter().any(|a| param_vars.contains(a)); |
| 129 | + |
| 130 | + if has_overlap { |
| 131 | + // Slow path: copy all args to fresh temps first, then temps to params. |
| 132 | + // Use fresh_var_repr to preserve repr metadata for ref-typed params. |
| 133 | + let temps: Vec<ArcVarId> = args |
| 134 | + .iter() |
| 135 | + .zip(params.iter()) |
| 136 | + .map(|(arg, (_, ty))| { |
| 137 | + let repr = func.var_repr(*arg).unwrap_or(ValueRepr::Scalar); |
| 138 | + func.fresh_var_repr(*ty, repr) |
| 139 | + }) |
| 140 | + .collect(); |
| 141 | + |
| 142 | + // Phase 1: args → temps. |
| 143 | + for ((&arg, temp), (_, ty)) in args.iter().zip(temps.iter()).zip(params.iter()) { |
| 144 | + func.blocks[block_idx].body.push(ArcInstr::Let { |
| 145 | + dst: *temp, |
| 146 | + ty: *ty, |
| 147 | + value: ArcValue::Var(arg), |
| 148 | + }); |
| 149 | + func.spans[block_idx].push(None); |
| 150 | + } |
| 151 | + |
| 152 | + // Phase 2: temps → params. |
| 153 | + for ((param_var, param_ty), temp) in params.iter().zip(temps.iter()) { |
| 154 | + func.blocks[block_idx].body.push(ArcInstr::Let { |
| 155 | + dst: *param_var, |
| 156 | + ty: *param_ty, |
| 157 | + value: ArcValue::Var(*temp), |
| 158 | + }); |
| 159 | + func.spans[block_idx].push(None); |
| 160 | + } |
| 161 | + } else { |
| 162 | + // Fast path: no aliasing, direct Let is safe. |
| 163 | + for ((param_var, param_ty), &arg) in params.iter().zip(args.iter()) { |
| 164 | + func.blocks[block_idx].body.push(ArcInstr::Let { |
| 165 | + dst: *param_var, |
| 166 | + ty: *param_ty, |
| 167 | + value: ArcValue::Var(arg), |
| 168 | + }); |
| 169 | + func.spans[block_idx].push(None); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments