diff --git a/src/opt/deadcode.rs b/src/opt/deadcode.rs index f9f1d22..9588281 100644 --- a/src/opt/deadcode.rs +++ b/src/opt/deadcode.rs @@ -1,6 +1,5 @@ use core::ops::Deref; use std::collections::{HashMap, HashSet}; -use std::ops::DerefMut; use crate::ir::*; use crate::opt::opt_utils::*; @@ -81,18 +80,15 @@ impl Optimize for DeadcodeInner { } } - println!("{used_allocations:?} /// {used_registers:?}"); - - let mut replaces = HashMap::new(); - let survived_allocations = code .allocations .iter() .enumerate() .filter(|(aid, _)| used_allocations.contains(aid)) .collect::>(); + let mut replaces = HashMap::new(); + 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), @@ -122,6 +118,13 @@ impl Optimize for DeadcodeInner { }) .collect::>(); + for iid_nop in &nop_instructions { + let _unused = replaces.insert( + RegisterId::temp(*bid, *iid_nop), + Operand::constant(Constant::unit()), + ); + } + let survived_instructions = block .instructions .iter() @@ -132,6 +135,7 @@ impl Optimize for DeadcodeInner { || !inst.has_no_side_effects() }) .collect::>(); + if survived_instructions.len() != block.instructions.len() { for (new_iid, (old_iid, inst)) in survived_instructions.iter().enumerate() { let _unused = replaces.insert( @@ -140,13 +144,6 @@ impl Optimize for DeadcodeInner { ); } - for iid_nop in &nop_instructions { - let _unused = replaces.insert( - RegisterId::temp(*bid, *iid_nop), - Operand::constant(Constant::unit()), - ); - } - block.instructions = survived_instructions .into_iter() .map(|(_, inst)| inst.clone()) @@ -184,8 +181,6 @@ impl Optimize for DeadcodeInner { } } - println!("replaces: {replaces:?}"); - for (bid, block) in code.blocks.iter_mut() { for inst in block.instructions.iter_mut() { let _ = replace_instruction_operands(inst, &replaces); @@ -221,16 +216,12 @@ fn mark_as_used( used_allocations: &mut HashSet, used_registers: &mut HashSet, ) { - match operand { - Operand::Register { rid, .. } => match rid { - RegisterId::Local { aid } => { - let _ = used_allocations.insert(*aid); - } - _ => { - let _ = used_registers.insert(*rid); - } - }, - _ => (), + if let Some((rid, _)) = operand.get_register() { + if let RegisterId::Local { aid } = rid { + let _ = used_allocations.insert(*aid); + } else { + let _ = used_registers.insert(*rid); + } } } @@ -257,6 +248,6 @@ fn kill_jump_args(arg: &mut JumpArg, survived_phinodess: &HashMap>(); + .collect(); arg.args = survived_args; }