rustc_middle/mir/
basic_blocks.rs
1use std::sync::OnceLock;
2
3use rustc_data_structures::fx::FxHashMap;
4use rustc_data_structures::graph;
5use rustc_data_structures::graph::dominators::{Dominators, dominators};
6use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
7use rustc_index::{IndexSlice, IndexVec};
8use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
9use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
10use smallvec::SmallVec;
11
12use crate::mir::traversal::Postorder;
13use crate::mir::{BasicBlock, BasicBlockData, START_BLOCK, Terminator, TerminatorKind};
14
15#[derive(Clone, TyEncodable, TyDecodable, Debug, HashStable, TypeFoldable, TypeVisitable)]
16pub struct BasicBlocks<'tcx> {
17 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
18 cache: Cache,
19}
20
21type Predecessors = IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>>;
23
24type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[SwitchTargetValue; 1]>>;
32
33#[derive(Debug, Clone, Copy)]
34pub enum SwitchTargetValue {
35 Normal(u128),
37 Otherwise,
39}
40
41#[derive(Clone, Default, Debug)]
42struct Cache {
43 predecessors: OnceLock<Predecessors>,
44 switch_sources: OnceLock<SwitchSources>,
45 reverse_postorder: OnceLock<Vec<BasicBlock>>,
46 dominators: OnceLock<Dominators<BasicBlock>>,
47}
48
49impl<'tcx> BasicBlocks<'tcx> {
50 #[inline]
51 pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>) -> Self {
52 BasicBlocks { basic_blocks, cache: Cache::default() }
53 }
54
55 pub fn dominators(&self) -> &Dominators<BasicBlock> {
56 self.cache.dominators.get_or_init(|| dominators(self))
57 }
58
59 #[inline]
61 pub fn predecessors(&self) -> &Predecessors {
62 self.cache.predecessors.get_or_init(|| {
63 let mut preds = IndexVec::from_elem(SmallVec::new(), &self.basic_blocks);
64 for (bb, data) in self.basic_blocks.iter_enumerated() {
65 if let Some(term) = &data.terminator {
66 for succ in term.successors() {
67 preds[succ].push(bb);
68 }
69 }
70 }
71 preds
72 })
73 }
74
75 #[inline]
81 pub fn reverse_postorder(&self) -> &[BasicBlock] {
82 self.cache.reverse_postorder.get_or_init(|| {
83 let mut rpo: Vec<_> = Postorder::new(&self.basic_blocks, START_BLOCK, None).collect();
84 rpo.reverse();
85 rpo
86 })
87 }
88
89 #[inline]
92 pub fn switch_sources(&self) -> &SwitchSources {
93 self.cache.switch_sources.get_or_init(|| {
94 let mut switch_sources: SwitchSources = FxHashMap::default();
95 for (bb, data) in self.basic_blocks.iter_enumerated() {
96 if let Some(Terminator {
97 kind: TerminatorKind::SwitchInt { targets, .. }, ..
98 }) = &data.terminator
99 {
100 for (value, target) in targets.iter() {
101 switch_sources
102 .entry((target, bb))
103 .or_default()
104 .push(SwitchTargetValue::Normal(value));
105 }
106 switch_sources
107 .entry((targets.otherwise(), bb))
108 .or_default()
109 .push(SwitchTargetValue::Otherwise);
110 }
111 }
112 switch_sources
113 })
114 }
115
116 #[inline]
118 pub fn as_mut(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
119 self.invalidate_cfg_cache();
120 &mut self.basic_blocks
121 }
122
123 #[inline]
135 pub fn as_mut_preserves_cfg(&mut self) -> &mut IndexVec<BasicBlock, BasicBlockData<'tcx>> {
136 &mut self.basic_blocks
137 }
138
139 pub fn invalidate_cfg_cache(&mut self) {
145 self.cache = Cache::default();
146 }
147}
148
149impl<'tcx> std::ops::Deref for BasicBlocks<'tcx> {
150 type Target = IndexSlice<BasicBlock, BasicBlockData<'tcx>>;
151
152 #[inline]
153 fn deref(&self) -> &IndexSlice<BasicBlock, BasicBlockData<'tcx>> {
154 &self.basic_blocks
155 }
156}
157
158impl<'tcx> graph::DirectedGraph for BasicBlocks<'tcx> {
159 type Node = BasicBlock;
160
161 #[inline]
162 fn num_nodes(&self) -> usize {
163 self.basic_blocks.len()
164 }
165}
166
167impl<'tcx> graph::StartNode for BasicBlocks<'tcx> {
168 #[inline]
169 fn start_node(&self) -> Self::Node {
170 START_BLOCK
171 }
172}
173
174impl<'tcx> graph::Successors for BasicBlocks<'tcx> {
175 #[inline]
176 fn successors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
177 self.basic_blocks[node].terminator().successors()
178 }
179}
180
181impl<'tcx> graph::Predecessors for BasicBlocks<'tcx> {
182 #[inline]
183 fn predecessors(&self, node: Self::Node) -> impl Iterator<Item = Self::Node> {
184 self.predecessors()[node].iter().copied()
185 }
186}
187
188TrivialTypeTraversalImpls! { Cache }
190
191impl<S: Encoder> Encodable<S> for Cache {
192 #[inline]
193 fn encode(&self, _s: &mut S) {}
194}
195
196impl<D: Decoder> Decodable<D> for Cache {
197 #[inline]
198 fn decode(_: &mut D) -> Self {
199 Default::default()
200 }
201}
202
203impl<CTX> HashStable<CTX> for Cache {
204 #[inline]
205 fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {}
206}