mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-14 22:38:46 +00:00
HW6 (3)
This commit is contained in:
@@ -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<FunctionDefinition> 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::<Vec<_>>();
|
||||
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<FunctionDefinition> for DeadcodeInner {
|
||||
})
|
||||
.collect::<HashSet<_>>();
|
||||
|
||||
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<FunctionDefinition> for DeadcodeInner {
|
||||
|| !inst.has_no_side_effects()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
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<FunctionDefinition> 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<FunctionDefinition> 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<usize>,
|
||||
used_registers: &mut HashSet<RegisterId>,
|
||||
) {
|
||||
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<BlockId, HashS
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
.collect();
|
||||
arg.args = survived_args;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user