HW4 Refactoring

This commit is contained in:
static
2025-06-06 13:01:18 +00:00
parent a4eefb8723
commit f596bf4701

View File

@@ -84,10 +84,7 @@ impl Optimize<FunctionDefinition> for Mem2regInner {
let block = code.blocks.get_mut(&bid).unwrap();
block.phinodes.push(code.allocations[aid].clone());
let _ = phinode_indexes.insert((aid, bid), block.phinodes.len() - 1);
phinode_allocs
.entry(bid)
.or_insert_with(Vec::new)
.push((aid, code.allocations[aid].deref().clone()));
phinode_allocs.entry(bid).or_insert_with(Vec::new).push(aid);
}
}
@@ -148,7 +145,7 @@ fn mark_as_inpromotable(inpromotables: &mut HashSet<usize>, operand: &Operand) {
type PhinodeInfo<'a> = (
&'a HashMap<(usize, BlockId), usize>,
&'a HashMap<BlockId, Vec<(usize, Dtype)>>,
&'a HashMap<BlockId, Vec<usize>>,
);
fn traverse_po(
@@ -157,7 +154,7 @@ fn traverse_po(
inpromotables: &HashSet<usize>,
domtree: &Domtree,
(phinode_indexes, phinode_allocs): PhinodeInfo<'_>,
mut block_stacks: HashMap<BlockId, HashMap<usize, Vec<Operand>>>,
mut block_stacks: HashMap<BlockId, HashMap<usize, Operand>>,
replaces: &mut HashMap<RegisterId, Operand>,
) {
let block = code.blocks.get_mut(&bid).unwrap();
@@ -167,7 +164,7 @@ fn traverse_po(
.enumerate()
.filter(|(aid, _)| !inpromotables.contains(aid))
.map(|(aid, dtype)| {
let initial_value = find_end_value(
let initial_value = find_latest_value(
aid,
dtype.deref().clone(),
bid,
@@ -175,7 +172,7 @@ fn traverse_po(
phinode_indexes,
&block_stacks,
);
(aid, vec![initial_value])
(aid, initial_value)
})
.collect::<HashMap<_, _>>();
let block_stack = block_stacks.entry(bid).or_insert(block_stack);
@@ -185,17 +182,15 @@ fn traverse_po(
Instruction::Store { ptr, value } => {
if let Some((RegisterId::Local { aid }, _)) = ptr.get_register() {
if let Some(value_stack) = block_stack.get_mut(aid) {
value_stack.push(value.clone());
*value_stack = value.clone();
}
}
}
Instruction::Load { ptr } => {
if let Some((RegisterId::Local { aid }, _)) = ptr.get_register() {
if let Some(value_stack) = block_stack.get(aid) {
let _unused = replaces.insert(
RegisterId::temp(bid, i),
value_stack.last().unwrap().clone(),
);
let _unused =
replaces.insert(RegisterId::temp(bid, i), value_stack.clone());
}
}
}
@@ -237,24 +232,24 @@ fn traverse_po(
}
}
fn find_end_value(
fn find_latest_value(
aid: usize,
dtype: Dtype,
bid: BlockId,
domtree: &Domtree,
phinode_indexes: &HashMap<(usize, BlockId), usize>,
block_stacks: &HashMap<BlockId, HashMap<usize, Vec<Operand>>>,
block_stacks: &HashMap<BlockId, HashMap<usize, Operand>>,
) -> Operand {
if let Some(block_stack) = block_stacks.get(&bid) {
if let Some(value_stack) = block_stack.get(&aid) {
return value_stack.last().unwrap().clone();
return value_stack.clone();
}
}
if let Some(phinode_index) = phinode_indexes.get(&(aid, bid)) {
Operand::register(RegisterId::arg(bid, *phinode_index), dtype)
} else if let Some(bid_idom) = domtree.idom(&bid) {
find_end_value(
find_latest_value(
aid,
dtype,
*bid_idom,
@@ -269,12 +264,12 @@ fn find_end_value(
fn fill_jump_args(
arg: &mut JumpArg,
phinode_allocs: &HashMap<BlockId, Vec<(usize, Dtype)>>,
block_stack: &HashMap<usize, Vec<Operand>>,
phinode_allocs: &HashMap<BlockId, Vec<usize>>,
block_stack: &HashMap<usize, Operand>,
) {
if let Some(phinode_allocs) = phinode_allocs.get(&arg.bid) {
for (aid, dtype) in phinode_allocs {
arg.args.push(block_stack[aid].last().unwrap().clone());
for aid in phinode_allocs {
arg.args.push(block_stack[aid].clone());
}
}
}