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