summary refs log tree commit diff
path: root/compiler/rustc_middle/src/mir/switch_sources.rs
blob: 7f62b4d0dbab947efe98704f35f870ba18f96feb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! Lazily compute the inverse of each `SwitchInt`'s switch targets. Modeled after
//! `Predecessors`/`PredecessorCache`.

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_index::vec::IndexVec;
use rustc_serialize as serialize;
use smallvec::SmallVec;

use crate::mir::{BasicBlock, BasicBlockData, Terminator, TerminatorKind};

pub type SwitchSources = IndexVec<BasicBlock, IndexVec<BasicBlock, SmallVec<[Option<u128>; 1]>>>;

#[derive(Clone, Debug)]
pub(super) struct SwitchSourceCache {
    cache: OnceCell<SwitchSources>,
}

impl SwitchSourceCache {
    #[inline]
    pub(super) fn new() -> Self {
        SwitchSourceCache { cache: OnceCell::new() }
    }

    /// Invalidates the switch source cache.
    #[inline]
    pub(super) fn invalidate(&mut self) {
        self.cache = OnceCell::new();
    }

    /// Returns the switch sources for this MIR.
    #[inline]
    pub(super) fn compute(
        &self,
        basic_blocks: &IndexVec<BasicBlock, BasicBlockData<'_>>,
    ) -> &SwitchSources {
        self.cache.get_or_init(|| {
            let mut switch_sources = IndexVec::from_elem(
                IndexVec::from_elem(SmallVec::new(), basic_blocks),
                basic_blocks,
            );
            for (bb, data) in basic_blocks.iter_enumerated() {
                if let Some(Terminator {
                    kind: TerminatorKind::SwitchInt { targets, .. }, ..
                }) = &data.terminator
                {
                    for (value, target) in targets.iter() {
                        switch_sources[target][bb].push(Some(value));
                    }
                    switch_sources[targets.otherwise()][bb].push(None);
                }
            }

            switch_sources
        })
    }
}

impl<S: serialize::Encoder> serialize::Encodable<S> for SwitchSourceCache {
    #[inline]
    fn encode(&self, s: &mut S) -> Result<(), S::Error> {
        s.emit_unit()
    }
}

impl<D: serialize::Decoder> serialize::Decodable<D> for SwitchSourceCache {
    #[inline]
    fn decode(_: &mut D) -> Self {
        Self::new()
    }
}

impl<CTX> HashStable<CTX> for SwitchSourceCache {
    #[inline]
    fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {
        // do nothing
    }
}

TrivialTypeFoldableAndLiftImpls! {
    SwitchSourceCache,
}