mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-15 15:08:52 +00:00
HW3 (4)
This commit is contained in:
@@ -77,35 +77,20 @@ impl Optimize<FunctionDefinition> for SimplifyCfgMerge {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut merged_from = HashMap::new();
|
|
||||||
let mut merged_to = HashMap::new();
|
let mut merged_to = HashMap::new();
|
||||||
let mut replaces = HashMap::new();
|
let mut replaces = HashMap::new();
|
||||||
|
|
||||||
for (bid_from, block_from) in &code.blocks {
|
for (bid_from, block_from) in &code.blocks {
|
||||||
if let BlockExit::Jump { arg } = &block_from.exit {
|
if let BlockExit::Jump { arg } = &block_from.exit {
|
||||||
let bid_to = arg.bid;
|
let bid_to = arg.bid;
|
||||||
if *bid_from != bid_to && indegrees.get(&bid_to) == Some(&1) {
|
if *bid_from != bid_to
|
||||||
let bid_from = *merged_to.get(bid_from).unwrap_or(bid_from);
|
&& !merged_to.contains_key(bid_from)
|
||||||
merged_from
|
&& indegrees.get(&bid_to) == Some(&1)
|
||||||
.entry(bid_from)
|
{
|
||||||
.or_insert(Vec::new())
|
|
||||||
.push(bid_to);
|
|
||||||
let _ = merged_to.insert(bid_to, bid_from);
|
|
||||||
|
|
||||||
if let Some(mut bids_merged) = merged_from.remove(&bid_to) {
|
|
||||||
merged_from
|
|
||||||
.get_mut(&bid_from)
|
|
||||||
.unwrap()
|
|
||||||
.append(&mut bids_merged);
|
|
||||||
merged_to.values_mut().for_each(|merged_to| {
|
|
||||||
if *merged_to == bid_to {
|
|
||||||
*merged_to = bid_from;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let block_to = &code.blocks[&bid_to];
|
let block_to = &code.blocks[&bid_to];
|
||||||
for (i, (a, p)) in izip!(&arg.args, block_to.phinodes.iter()).enumerate() {
|
let _ = merged_to.insert(bid_to, *bid_from);
|
||||||
|
|
||||||
|
for (i, (a, p)) in izip!(&arg.args, &block_to.phinodes).enumerate() {
|
||||||
let from = RegisterId::arg(bid_to, i);
|
let from = RegisterId::arg(bid_to, i);
|
||||||
let _unused = replaces.insert(from, a.clone());
|
let _unused = replaces.insert(from, a.clone());
|
||||||
}
|
}
|
||||||
@@ -113,30 +98,22 @@ impl Optimize<FunctionDefinition> for SimplifyCfgMerge {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (bid_merge_to, bids_merge_from) in &merged_from {
|
for (bid_to, bid_from) in &merged_to {
|
||||||
let blocks_merge_from = bids_merge_from
|
let block_to = code.blocks.remove(bid_to).unwrap();
|
||||||
.iter()
|
let block_from = code.blocks.get_mut(bid_from).unwrap();
|
||||||
.map(|bid| code.blocks.get(bid).unwrap().clone())
|
let len = block_from.instructions.len();
|
||||||
.collect::<Vec<_>>();
|
|
||||||
let block_merge_to = code.blocks.get_mut(bid_merge_to).unwrap();
|
|
||||||
let mut i = block_merge_to.instructions.len();
|
|
||||||
|
|
||||||
for (bid, block) in bids_merge_from.iter().zip(blocks_merge_from) {
|
for (i, inst) in block_to.instructions.into_iter().enumerate() {
|
||||||
for (j, inst) in block.instructions.iter().enumerate() {
|
let dtype = inst.dtype();
|
||||||
let dtype = inst.dtype();
|
block_from.instructions.push(inst);
|
||||||
block_merge_to.instructions.push(inst.clone());
|
|
||||||
|
|
||||||
let from = RegisterId::temp(*bid, j);
|
let from = RegisterId::temp(*bid_to, i);
|
||||||
let to = Operand::register(RegisterId::temp(*bid_merge_to, i), dtype);
|
let to = Operand::register(RegisterId::temp(*bid_from, len + i), dtype);
|
||||||
let _unused = replaces.insert(from, to);
|
let _unused = replaces.insert(from, to);
|
||||||
i += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
block_merge_to.exit = block.exit;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
code.blocks.retain(|bid, _| !merged_to.contains_key(bid));
|
block_from.exit = block_to.exit;
|
||||||
|
}
|
||||||
|
|
||||||
for (bid, block) in &mut code.blocks {
|
for (bid, block) in &mut code.blocks {
|
||||||
for inst in block.instructions.iter_mut() {
|
for inst in block.instructions.iter_mut() {
|
||||||
|
|||||||
Reference in New Issue
Block a user