Refactor basic blocks control flow caches

This commit is contained in:
Tomasz Miąsko
2023-01-16 00:00:00 +00:00
parent 159ba8a92c
commit a6235a2006
6 changed files with 74 additions and 296 deletions

View File

@@ -1,7 +1,4 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_index::bit_set::BitSet;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use super::*;
@@ -339,50 +336,3 @@ pub fn reverse_postorder<'a, 'tcx>(body: &'a Body<'tcx>) -> ReversePostorderIter
let len = blocks.len();
ReversePostorderIter { body, blocks, idx: len }
}
#[derive(Clone, Debug)]
pub(super) struct PostorderCache {
cache: OnceCell<Vec<BasicBlock>>,
}
impl PostorderCache {
#[inline]
pub(super) fn new() -> Self {
PostorderCache { cache: OnceCell::new() }
}
/// Invalidates the postorder cache.
#[inline]
pub(super) fn invalidate(&mut self) {
self.cache = OnceCell::new();
}
/// Returns the `&[BasicBlocks]` represents the postorder graph for this MIR.
#[inline]
pub(super) fn compute(&self, body: &IndexVec<BasicBlock, BasicBlockData<'_>>) -> &[BasicBlock] {
self.cache.get_or_init(|| Postorder::new(body, START_BLOCK).map(|(bb, _)| bb).collect())
}
}
impl<S: Encoder> Encodable<S> for PostorderCache {
#[inline]
fn encode(&self, _s: &mut S) {}
}
impl<D: Decoder> Decodable<D> for PostorderCache {
#[inline]
fn decode(_: &mut D) -> Self {
Self::new()
}
}
impl<CTX> HashStable<CTX> for PostorderCache {
#[inline]
fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {
// do nothing
}
}
TrivialTypeTraversalAndLiftImpls! {
PostorderCache,
}