From ff9b22742dce1c377486c00effe608110cc23373 Mon Sep 17 00:00:00 2001 From: static Date: Sun, 1 Jun 2025 17:16:21 +0000 Subject: [PATCH] HW6 (2) --- src/opt/deadcode.rs | 86 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 13 deletions(-) diff --git a/src/opt/deadcode.rs b/src/opt/deadcode.rs index 72ae5ca..f9f1d22 100644 --- a/src/opt/deadcode.rs +++ b/src/opt/deadcode.rs @@ -29,11 +29,6 @@ impl Optimize for DeadcodeInner { Instruction::Store { ptr, value } => { mark_as_used(ptr, &mut used_allocations, &mut used_registers); mark_as_used(value, &mut used_allocations, &mut used_registers); - // mark_as_used( - // &Operand::register(RegisterId::temp(*bid, i), inst.dtype()), - // &mut used_allocations, - // &mut used_registers, - // ); } Instruction::Load { ptr } => { mark_as_used(ptr, &mut used_allocations, &mut used_registers) @@ -43,11 +38,6 @@ impl Optimize for DeadcodeInner { for arg in args { mark_as_used(arg, &mut used_allocations, &mut used_registers); } - // mark_as_used( - // &Operand::register(RegisterId::temp(*bid, i), inst.dtype()), - // &mut used_allocations, - // &mut used_registers, - // ); } Instruction::TypeCast { value, .. } => { mark_as_used(value, &mut used_allocations, &mut used_registers) @@ -102,6 +92,7 @@ impl Optimize for DeadcodeInner { .filter(|(aid, _)| used_allocations.contains(aid)) .collect::>(); if survived_allocations.len() != code.allocations.len() { + println!("hi~!"); for (new_aid, (old_aid, dtype)) in survived_allocations.iter().enumerate() { let _unused = replaces.insert( RegisterId::local(*old_aid), @@ -118,6 +109,8 @@ impl Optimize for DeadcodeInner { .collect(); } + let mut survived_phinodess = HashMap::new(); + for (bid, block) in &mut code.blocks { let nop_instructions = block .instructions @@ -159,14 +152,64 @@ impl Optimize for DeadcodeInner { .map(|(_, inst)| inst.clone()) .collect(); } + + let survived_phinodes = block + .phinodes + .iter() + .enumerate() + .filter_map(|(pid, dtype)| { + if *bid == code.bid_init || used_registers.contains(&RegisterId::arg(*bid, pid)) + { + Some((pid, dtype.clone())) + } else { + None + } + }) + .collect::>(); + let _unused = survived_phinodess + .insert(*bid, survived_phinodes.iter().map(|(i, _)| *i).collect()); + + if survived_phinodes.len() != block.phinodes.len() { + for (new_pid, (old_pid, dtype)) in survived_phinodes.iter().enumerate() { + let _unused = replaces.insert( + RegisterId::arg(*bid, *old_pid), + Operand::register(RegisterId::arg(*bid, new_pid), dtype.deref().clone()), + ); + } + + block.phinodes = survived_phinodes + .into_iter() + .map(|(_, dtype)| dtype) + .collect(); + } } + println!("replaces: {replaces:?}"); + for (bid, block) in code.blocks.iter_mut() { - let mut changed = false; for inst in block.instructions.iter_mut() { - changed = replace_instruction_operands(inst, &replaces) || changed; + let _ = replace_instruction_operands(inst, &replaces); + } + let _ = replace_exit_operands(&mut block.exit, &replaces); + + match &mut block.exit { + BlockExit::Jump { arg } => { + kill_jump_args(arg, &survived_phinodess); + } + BlockExit::ConditionalJump { + arg_then, arg_else, .. + } => { + kill_jump_args(arg_then, &survived_phinodess); + kill_jump_args(arg_else, &survived_phinodess); + } + BlockExit::Switch { default, cases, .. } => { + kill_jump_args(default, &survived_phinodess); + for (_, arg) in cases.iter_mut() { + kill_jump_args(arg, &survived_phinodess); + } + } + _ => (), } - changed = replace_exit_operands(&mut block.exit, &replaces) || changed; } !replaces.is_empty() @@ -200,3 +243,20 @@ fn mark_jump_args_as_used( mark_as_used(arg, used_allocations, used_registers); } } + +fn kill_jump_args(arg: &mut JumpArg, survived_phinodess: &HashMap>) { + let survived_phinodes = &survived_phinodess[&arg.bid]; + let survived_args = arg + .args + .iter() + .enumerate() + .filter_map(|(i, operand)| { + if survived_phinodes.contains(&i) { + Some(operand.clone()) + } else { + None + } + }) + .collect::>(); + arg.args = survived_args; +}