diff --git a/src/mir/loop_builder.rs b/src/mir/loop_builder.rs index c3a62f35..116fba39 100644 --- a/src/mir/loop_builder.rs +++ b/src/mir/loop_builder.rs @@ -51,34 +51,6 @@ impl<'a> LoopBuilder<'a> { /// Find the source value of a Copy instruction in a given block /// If `dst` is defined by a Copy instruction `dst = copy src`, return Some(src) /// Otherwise return None - fn find_copy_source(&self, block_id: BasicBlockId, dst: ValueId) -> Option { - let func = self.parent_builder.current_function.as_ref()?; - let block = func.blocks.get(&block_id)?; - - let trace = std::env::var("NYASH_LOOP_TRACE").ok().as_deref() == Some("1"); - if trace { - eprintln!("[loop/copy-trace] searching for dst={:?} in block={:?}, {} instructions", - dst, block_id, block.instructions.len()); - } - - for (idx, inst) in block.instructions.iter().enumerate() { - if let MirInstruction::Copy { dst: inst_dst, src } = inst { - if trace { - eprintln!("[loop/copy-trace] inst#{}: %{} = copy %{}", idx, inst_dst.0, src.0); - } - if *inst_dst == dst { - if trace { - eprintln!("[loop/copy-trace] FOUND! dst={:?} src={:?}", dst, src); - } - return Some(*src); - } - } - } - if trace { - eprintln!("[loop/copy-trace] NOT FOUND for dst={:?}", dst); - } - None - } // ============================================================= // Control Helpers — break/continue/jumps/unreachable handling @@ -415,57 +387,6 @@ impl<'a> LoopBuilder<'a> { /// - ループキャリア(ループ本体で再代入される変数)と pinned 変数のみを PHI 対象とする。 /// - ループ不変のローカル(text_len / pattern_len など)は preheader 値をそのまま使い、 /// 不要な PHI を張らないことで SSA 破綻(同一 ValueId の二重定義)を防ぐ。 - fn prepare_loop_variables( - &mut self, - header_id: BasicBlockId, - preheader_id: BasicBlockId, - pre_vars_snapshot: &std::collections::HashMap, - assigned_vars: &[String], - ) -> Result, String> { - use std::sync::atomic::{AtomicUsize, Ordering}; - static CALL_COUNT: AtomicUsize = AtomicUsize::new(0); - let count = CALL_COUNT.fetch_add(1, Ordering::SeqCst); - - // Use the variable map captured at preheader (before switching to header), - // but filter to: - // - ループキャリア(assigned_vars に含まれる変数) - // - pinned 変数(__pin$*): 受信箱など、ループをまたいで値を運ぶ必要があるもの - let mut current_vars = std::collections::HashMap::new(); - for (name, &val) in pre_vars_snapshot.iter() { - if name.starts_with("__pin$") { - current_vars.insert(name.clone(), val); - continue; - } - if assigned_vars.iter().any(|v| v == name) { - current_vars.insert(name.clone(), val); - } - } - // Debug: print current_vars before prepare (guarded by env) - let dbg = std::env::var("NYASH_BUILDER_DEBUG").ok().as_deref() == Some("1"); - if dbg { - eprintln!("[DEBUG] prepare_loop_variables call #{}", count); - eprintln!("[DEBUG] current_vars = {:?}", current_vars); - eprintln!( - "[DEBUG] preheader_id = {:?}, header_id = {:?}", - preheader_id, header_id - ); - } - crate::mir::phi_core::loop_phi::save_block_snapshot( - &mut self.block_var_maps, - preheader_id, - ¤t_vars, - ); - let incs = crate::mir::phi_core::loop_phi::prepare_loop_variables_with( - self, - header_id, - preheader_id, - ¤t_vars, - )?; - // Defer variable rebinding to PHI IDs until after the loop condition is emitted. - // Store incomplete PHIs for later sealing and for rebinding after branch emission. - self.incomplete_phis.insert(header_id, incs.clone()); - Ok(incs) - } /// ブロックをシールし、不完全なPhi nodeを完成させる