use crate::ir::cfg::BasicBlockId;
use crate::ir::StatementId;
use crate::ControlFlowGraph;
use index_vec::*;
use once_cell::unsync::OnceCell;
pub type StatementOwners = IndexVec<StatementId, Option<BasicBlockId>>;
#[derive(Clone, Debug)]
pub(crate) struct StatementOwnerCache {
cache: OnceCell<StatementOwners>,
pub(crate) stmt_count: usize,
}
impl StatementOwnerCache {
#[inline]
pub(super) fn new(stmt_count: usize) -> Self {
StatementOwnerCache {
cache: OnceCell::new(),
stmt_count,
}
}
#[inline]
pub(super) fn invalidate(&mut self) {
self.cache = OnceCell::new();
}
#[inline]
pub(super) fn compute(&self, cfg: &ControlFlowGraph) -> &StatementOwners {
self.cache.get_or_init(|| {
let mut owner = index_vec![None;self.stmt_count];
for (id, bb) in cfg.blocks.iter_enumerated() {
for stmt in bb.statements.iter().copied() {
owner[stmt] = Some(id);
}
}
owner
})
}
}