mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-16 15:38:48 +00:00
HW6 (2)
This commit is contained in:
@@ -29,11 +29,6 @@ impl Optimize<FunctionDefinition> for DeadcodeInner {
|
|||||||
Instruction::Store { ptr, value } => {
|
Instruction::Store { ptr, value } => {
|
||||||
mark_as_used(ptr, &mut used_allocations, &mut used_registers);
|
mark_as_used(ptr, &mut used_allocations, &mut used_registers);
|
||||||
mark_as_used(value, &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 } => {
|
Instruction::Load { ptr } => {
|
||||||
mark_as_used(ptr, &mut used_allocations, &mut used_registers)
|
mark_as_used(ptr, &mut used_allocations, &mut used_registers)
|
||||||
@@ -43,11 +38,6 @@ impl Optimize<FunctionDefinition> for DeadcodeInner {
|
|||||||
for arg in args {
|
for arg in args {
|
||||||
mark_as_used(arg, &mut used_allocations, &mut used_registers);
|
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, .. } => {
|
Instruction::TypeCast { value, .. } => {
|
||||||
mark_as_used(value, &mut used_allocations, &mut used_registers)
|
mark_as_used(value, &mut used_allocations, &mut used_registers)
|
||||||
@@ -102,6 +92,7 @@ impl Optimize<FunctionDefinition> for DeadcodeInner {
|
|||||||
.filter(|(aid, _)| used_allocations.contains(aid))
|
.filter(|(aid, _)| used_allocations.contains(aid))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if survived_allocations.len() != code.allocations.len() {
|
if survived_allocations.len() != code.allocations.len() {
|
||||||
|
println!("hi~!");
|
||||||
for (new_aid, (old_aid, dtype)) in survived_allocations.iter().enumerate() {
|
for (new_aid, (old_aid, dtype)) in survived_allocations.iter().enumerate() {
|
||||||
let _unused = replaces.insert(
|
let _unused = replaces.insert(
|
||||||
RegisterId::local(*old_aid),
|
RegisterId::local(*old_aid),
|
||||||
@@ -118,6 +109,8 @@ impl Optimize<FunctionDefinition> for DeadcodeInner {
|
|||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut survived_phinodess = HashMap::new();
|
||||||
|
|
||||||
for (bid, block) in &mut code.blocks {
|
for (bid, block) in &mut code.blocks {
|
||||||
let nop_instructions = block
|
let nop_instructions = block
|
||||||
.instructions
|
.instructions
|
||||||
@@ -159,14 +152,64 @@ impl Optimize<FunctionDefinition> for DeadcodeInner {
|
|||||||
.map(|(_, inst)| inst.clone())
|
.map(|(_, inst)| inst.clone())
|
||||||
.collect();
|
.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::<Vec<_>>();
|
||||||
|
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() {
|
for (bid, block) in code.blocks.iter_mut() {
|
||||||
let mut changed = false;
|
|
||||||
for inst in block.instructions.iter_mut() {
|
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()
|
!replaces.is_empty()
|
||||||
@@ -200,3 +243,20 @@ fn mark_jump_args_as_used(
|
|||||||
mark_as_used(arg, used_allocations, used_registers);
|
mark_as_used(arg, used_allocations, used_registers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn kill_jump_args(arg: &mut JumpArg, survived_phinodess: &HashMap<BlockId, HashSet<usize>>) {
|
||||||
|
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::<Vec<_>>();
|
||||||
|
arg.args = survived_args;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user