use crate::{Inst, Optimizer};
use std::collections::HashMap;
impl Optimizer {
pub(in crate::optimization) fn elim_store_load(&mut self, insts: Vec<Inst>) -> Vec<Inst> {
let mut new_insts = Vec::with_capacity(insts.len());
let mut memory_map = HashMap::new();
for inst in insts {
match &inst {
Inst::Store {
src,
dst_ptr,
dst_offset,
..
} => {
memory_map.insert((*dst_ptr, dst_offset.clone()), *src);
new_insts.push(inst);
}
Inst::Load {
src_ptr,
src_offset,
dst,
..
} => {
if let Some(original_value) = memory_map.get(&(*src_ptr, src_offset.clone())) {
self.value_replace_map.insert(*dst, *original_value);
} else {
new_insts.push(inst);
}
}
_ => new_insts.push(inst),
}
}
new_insts
}
}