]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/cache.rs
rename Allocation::retag -> with_tags_and_extra
[rust.git] / src / librustc / mir / cache.rs
1 use rustc_data_structures::indexed_vec::IndexVec;
2 use rustc_data_structures::sync::{RwLock, MappedReadGuard, ReadGuard};
3 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
4 use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
5 use crate::ich::StableHashingContext;
6 use crate::mir::{Body, BasicBlock};
7
8 #[derive(Clone, Debug)]
9 pub struct Cache {
10     predecessors: RwLock<Option<IndexVec<BasicBlock, Vec<BasicBlock>>>>
11 }
12
13
14 impl rustc_serialize::Encodable for Cache {
15     fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
16         Encodable::encode(&(), s)
17     }
18 }
19
20 impl rustc_serialize::Decodable for Cache {
21     fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
22         Decodable::decode(d).map(|_v: ()| Self::new())
23     }
24 }
25
26 impl<'a> HashStable<StableHashingContext<'a>> for Cache {
27     fn hash_stable<W: StableHasherResult>(&self,
28                                           _: &mut StableHashingContext<'a>,
29                                           _: &mut StableHasher<W>) {
30         // Do nothing.
31     }
32 }
33
34 impl Cache {
35     pub fn new() -> Self {
36         Cache {
37             predecessors: RwLock::new(None)
38         }
39     }
40
41     pub fn invalidate(&self) {
42         // FIXME: consider being more fine-grained
43         *self.predecessors.borrow_mut() = None;
44     }
45
46     pub fn predecessors(
47         &self,
48         body: &Body<'_>
49     ) -> MappedReadGuard<'_, IndexVec<BasicBlock, Vec<BasicBlock>>> {
50         if self.predecessors.borrow().is_none() {
51             *self.predecessors.borrow_mut() = Some(calculate_predecessors(body));
52         }
53
54         ReadGuard::map(self.predecessors.borrow(), |p| p.as_ref().unwrap())
55     }
56 }
57
58 fn calculate_predecessors(body: &Body<'_>) -> IndexVec<BasicBlock, Vec<BasicBlock>> {
59     let mut result = IndexVec::from_elem(vec![], body.basic_blocks());
60     for (bb, data) in body.basic_blocks().iter_enumerated() {
61         if let Some(ref term) = data.terminator {
62             for &tgt in term.successors() {
63                 result[tgt].push(bb);
64             }
65         }
66     }
67
68     result
69 }
70
71 CloneTypeFoldableAndLiftImpls! {
72     Cache,
73 }