2022-04-28 11:31:08 +08:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
|
|
|
use rustc_data_structures::sync::OnceCell;
|
2019-09-26 05:30:10 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2022-06-14 14:52:01 +10:00
|
|
|
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
|
2016-03-11 13:14:51 +13:00
|
|
|
|
2016-09-19 23:50:00 +03:00
|
|
|
use super::*;
|
2016-03-11 13:14:51 +13:00
|
|
|
|
|
|
|
/// Preorder traversal of a graph.
|
|
|
|
///
|
2022-02-02 19:28:01 -05:00
|
|
|
/// Preorder traversal is when each node is visited after at least one of its predecessors. If you
|
2022-03-30 15:14:15 -04:00
|
|
|
/// are familiar with some basic graph theory, then this performs a depth first search and returns
|
2022-02-02 19:28:01 -05:00
|
|
|
/// nodes in order of discovery time.
|
2016-03-11 13:14:51 +13:00
|
|
|
///
|
2016-04-13 16:13:24 +05:30
|
|
|
/// ```text
|
|
|
|
///
|
2016-03-11 13:14:51 +13:00
|
|
|
/// A
|
|
|
|
/// / \
|
|
|
|
/// / \
|
|
|
|
/// B C
|
|
|
|
/// \ /
|
|
|
|
/// \ /
|
|
|
|
/// D
|
2016-04-13 16:13:24 +05:30
|
|
|
/// ```
|
2016-03-11 13:14:51 +13:00
|
|
|
///
|
|
|
|
/// A preorder traversal of this graph is either `A B D C` or `A C D B`
|
|
|
|
#[derive(Clone)]
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct Preorder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'a Body<'tcx>,
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
visited: BitSet<BasicBlock>,
|
2016-03-11 13:14:51 +13:00
|
|
|
worklist: Vec<BasicBlock>,
|
2018-10-23 01:54:02 +09:00
|
|
|
root_is_start_block: bool,
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Preorder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
pub fn new(body: &'a Body<'tcx>, root: BasicBlock) -> Preorder<'a, 'tcx> {
|
2016-03-11 13:14:51 +13:00
|
|
|
let worklist = vec![root];
|
|
|
|
|
|
|
|
Preorder {
|
2019-06-03 18:26:48 -04:00
|
|
|
body,
|
|
|
|
visited: BitSet::new_empty(body.basic_blocks().len()),
|
2017-07-03 11:19:51 -07:00
|
|
|
worklist,
|
2018-10-23 01:54:02 +09:00
|
|
|
root_is_start_block: root == START_BLOCK,
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
pub fn preorder<'a, 'tcx>(body: &'a Body<'tcx>) -> Preorder<'a, 'tcx> {
|
|
|
|
Preorder::new(body, START_BLOCK)
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
|
|
|
|
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
|
|
|
while let Some(idx) = self.worklist.pop() {
|
2018-07-22 19:23:39 +03:00
|
|
|
if !self.visited.insert(idx) {
|
2016-03-11 13:14:51 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
let data = &self.body[idx];
|
2016-03-11 13:14:51 +13:00
|
|
|
|
|
|
|
if let Some(ref term) = data.terminator {
|
2018-07-26 17:11:10 +02:00
|
|
|
self.worklist.extend(term.successors());
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return Some((idx, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2018-03-20 05:33:59 -04:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
// All the blocks, minus the number of blocks we've visited.
|
2019-06-03 18:26:48 -04:00
|
|
|
let upper = self.body.basic_blocks().len() - self.visited.count();
|
2018-03-20 05:33:59 -04:00
|
|
|
|
2018-10-23 01:54:02 +09:00
|
|
|
let lower = if self.root_is_start_block {
|
|
|
|
// We will visit all remaining blocks exactly once.
|
|
|
|
upper
|
|
|
|
} else {
|
|
|
|
self.worklist.len()
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, Some(upper))
|
2018-03-20 05:33:59 -04:00
|
|
|
}
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Postorder traversal of a graph.
|
|
|
|
///
|
2022-02-02 19:28:01 -05:00
|
|
|
/// Postorder traversal is when each node is visited after all of its successors, except when the
|
|
|
|
/// successor is only reachable by a back-edge. If you are familiar with some basic graph theory,
|
|
|
|
/// then this performs a depth first search and returns nodes in order of completion time.
|
2016-03-11 13:14:51 +13:00
|
|
|
///
|
2016-04-13 16:13:24 +05:30
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
///
|
2016-03-11 13:14:51 +13:00
|
|
|
/// A
|
|
|
|
/// / \
|
|
|
|
/// / \
|
|
|
|
/// B C
|
|
|
|
/// \ /
|
|
|
|
/// \ /
|
|
|
|
/// D
|
2016-04-13 16:13:24 +05:30
|
|
|
/// ```
|
2016-03-11 13:14:51 +13:00
|
|
|
///
|
|
|
|
/// A Postorder traversal of this graph is `D B C A` or `D C B A`
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct Postorder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'a Body<'tcx>,
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 15:07:25 +10:00
|
|
|
visited: BitSet<BasicBlock>,
|
2018-10-23 01:54:02 +09:00
|
|
|
visit_stack: Vec<(BasicBlock, Successors<'a>)>,
|
|
|
|
root_is_start_block: bool,
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Postorder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
pub fn new(body: &'a Body<'tcx>, root: BasicBlock) -> Postorder<'a, 'tcx> {
|
2016-03-11 13:14:51 +13:00
|
|
|
let mut po = Postorder {
|
2019-06-03 18:26:48 -04:00
|
|
|
body,
|
|
|
|
visited: BitSet::new_empty(body.basic_blocks().len()),
|
2018-10-23 01:54:02 +09:00
|
|
|
visit_stack: Vec::new(),
|
|
|
|
root_is_start_block: root == START_BLOCK,
|
2016-03-11 13:14:51 +13:00
|
|
|
};
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
let data = &po.body[root];
|
2016-03-11 13:14:51 +13:00
|
|
|
|
|
|
|
if let Some(ref term) = data.terminator {
|
2018-07-22 19:23:39 +03:00
|
|
|
po.visited.insert(root);
|
2018-04-27 14:02:09 +03:00
|
|
|
po.visit_stack.push((root, term.successors()));
|
2016-03-11 13:14:51 +13:00
|
|
|
po.traverse_successor();
|
|
|
|
}
|
|
|
|
|
|
|
|
po
|
|
|
|
}
|
|
|
|
|
|
|
|
fn traverse_successor(&mut self) {
|
|
|
|
// This is quite a complex loop due to 1. the borrow checker not liking it much
|
|
|
|
// and 2. what exactly is going on is not clear
|
|
|
|
//
|
|
|
|
// It does the actual traversal of the graph, while the `next` method on the iterator
|
|
|
|
// just pops off of the stack. `visit_stack` is a stack containing pairs of nodes and
|
2018-11-11 20:52:36 +07:00
|
|
|
// iterators over the successors of those nodes. Each iteration attempts to get the next
|
2016-03-11 13:14:51 +13:00
|
|
|
// node from the top of the stack, then pushes that node and an iterator over the
|
|
|
|
// successors to the top of the stack. This loop only grows `visit_stack`, stopping when
|
|
|
|
// we reach a child that has no children that we haven't already visited.
|
|
|
|
//
|
|
|
|
// For a graph that looks like this:
|
|
|
|
//
|
|
|
|
// A
|
|
|
|
// / \
|
|
|
|
// / \
|
|
|
|
// B C
|
|
|
|
// | |
|
|
|
|
// | |
|
|
|
|
// D |
|
|
|
|
// \ /
|
|
|
|
// \ /
|
|
|
|
// E
|
|
|
|
//
|
|
|
|
// The state of the stack starts out with just the root node (`A` in this case);
|
|
|
|
// [(A, [B, C])]
|
|
|
|
//
|
2018-11-11 20:52:36 +07:00
|
|
|
// When the first call to `traverse_successor` happens, the following happens:
|
2016-03-11 13:14:51 +13:00
|
|
|
//
|
|
|
|
// [(B, [D]), // `B` taken from the successors of `A`, pushed to the
|
|
|
|
// // top of the stack along with the successors of `B`
|
|
|
|
// (A, [C])]
|
|
|
|
//
|
|
|
|
// [(D, [E]), // `D` taken from successors of `B`, pushed to stack
|
|
|
|
// (B, []),
|
|
|
|
// (A, [C])]
|
|
|
|
//
|
|
|
|
// [(E, []), // `E` taken from successors of `D`, pushed to stack
|
|
|
|
// (D, []),
|
|
|
|
// (B, []),
|
|
|
|
// (A, [C])]
|
|
|
|
//
|
|
|
|
// Now that the top of the stack has no successors we can traverse, each item will
|
2018-08-19 15:30:23 +02:00
|
|
|
// be popped off during iteration until we get back to `A`. This yields [E, D, B].
|
2016-03-11 13:14:51 +13:00
|
|
|
//
|
2016-03-30 14:46:02 +13:00
|
|
|
// When we yield `B` and call `traverse_successor`, we push `C` to the stack, but
|
2016-03-11 13:14:51 +13:00
|
|
|
// since we've already visited `E`, that child isn't added to the stack. The last
|
|
|
|
// two iterations yield `C` and finally `A` for a final traversal of [E, D, B, C, A]
|
|
|
|
loop {
|
|
|
|
let bb = if let Some(&mut (_, ref mut iter)) = self.visit_stack.last_mut() {
|
2022-05-17 08:41:01 +08:00
|
|
|
if let Some(bb) = iter.next() {
|
2016-03-11 13:14:51 +13:00
|
|
|
bb
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2018-07-22 19:23:39 +03:00
|
|
|
if self.visited.insert(bb) {
|
2019-06-03 18:26:48 -04:00
|
|
|
if let Some(term) = &self.body[bb].terminator {
|
2018-04-27 14:02:09 +03:00
|
|
|
self.visit_stack.push((bb, term.successors()));
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
pub fn postorder<'a, 'tcx>(body: &'a Body<'tcx>) -> Postorder<'a, 'tcx> {
|
|
|
|
Postorder::new(body, START_BLOCK)
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for Postorder<'a, 'tcx> {
|
|
|
|
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
|
|
|
let next = self.visit_stack.pop();
|
|
|
|
if next.is_some() {
|
|
|
|
self.traverse_successor();
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
next.map(|(bb, _)| (bb, &self.body[bb]))
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
2018-03-20 05:33:59 -04:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
// All the blocks, minus the number of blocks we've visited.
|
2019-06-03 18:26:48 -04:00
|
|
|
let upper = self.body.basic_blocks().len() - self.visited.count();
|
2018-03-20 05:33:59 -04:00
|
|
|
|
2018-10-23 01:54:02 +09:00
|
|
|
let lower = if self.root_is_start_block {
|
|
|
|
// We will visit all remaining blocks exactly once.
|
|
|
|
upper
|
|
|
|
} else {
|
|
|
|
self.visit_stack.len()
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, Some(upper))
|
2018-03-20 05:33:59 -04:00
|
|
|
}
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reverse postorder traversal of a graph
|
|
|
|
///
|
|
|
|
/// Reverse postorder is the reverse order of a postorder traversal.
|
|
|
|
/// This is different to a preorder traversal and represents a natural
|
2017-08-15 21:45:21 +02:00
|
|
|
/// linearization of control-flow.
|
2016-03-11 13:14:51 +13:00
|
|
|
///
|
2016-04-13 16:13:24 +05:30
|
|
|
/// ```text
|
|
|
|
///
|
2016-03-11 13:14:51 +13:00
|
|
|
/// A
|
|
|
|
/// / \
|
|
|
|
/// / \
|
|
|
|
/// B C
|
|
|
|
/// \ /
|
|
|
|
/// \ /
|
|
|
|
/// D
|
2016-04-13 16:13:24 +05:30
|
|
|
/// ```
|
2016-03-11 13:14:51 +13:00
|
|
|
///
|
|
|
|
/// A reverse postorder traversal of this graph is either `A B C D` or `A C B D`
|
2018-11-27 02:59:49 +00:00
|
|
|
/// Note that for a graph containing no loops (i.e., A DAG), this is equivalent to
|
2016-03-11 13:14:51 +13:00
|
|
|
/// a topological sort.
|
|
|
|
///
|
|
|
|
/// Construction of a `ReversePostorder` traversal requires doing a full
|
|
|
|
/// postorder traversal of the graph, therefore this traversal should be
|
|
|
|
/// constructed as few times as possible. Use the `reset` method to be able
|
|
|
|
/// to re-use the traversal
|
|
|
|
#[derive(Clone)]
|
2019-06-14 19:39:39 +03:00
|
|
|
pub struct ReversePostorder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'a Body<'tcx>,
|
2016-03-11 13:14:51 +13:00
|
|
|
blocks: Vec<BasicBlock>,
|
|
|
|
idx: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ReversePostorder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
pub fn new(body: &'a Body<'tcx>, root: BasicBlock) -> ReversePostorder<'a, 'tcx> {
|
|
|
|
let blocks: Vec<_> = Postorder::new(body, root).map(|(bb, _)| bb).collect();
|
2016-03-11 13:14:51 +13:00
|
|
|
|
|
|
|
let len = blocks.len();
|
|
|
|
|
|
|
|
ReversePostorder { body, blocks, idx: len }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for ReversePostorder<'a, 'tcx> {
|
|
|
|
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
|
|
|
if self.idx == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.idx -= 1;
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
self.blocks.get(self.idx).map(|&bb| (bb, &self.body[bb]))
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
2018-03-20 05:33:59 -04:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.idx, Some(self.idx))
|
|
|
|
}
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
2018-03-20 05:33:59 -04:00
|
|
|
|
|
|
|
impl<'a, 'tcx> ExactSizeIterator for ReversePostorder<'a, 'tcx> {}
|
2020-07-08 09:47:14 -07:00
|
|
|
|
|
|
|
/// Returns an iterator over all basic blocks reachable from the `START_BLOCK` in no particular
|
|
|
|
/// order.
|
|
|
|
///
|
|
|
|
/// This is clearer than writing `preorder` in cases where the order doesn't matter.
|
|
|
|
pub fn reachable<'a, 'tcx>(
|
|
|
|
body: &'a Body<'tcx>,
|
|
|
|
) -> impl 'a + Iterator<Item = (BasicBlock, &'a BasicBlockData<'tcx>)> {
|
|
|
|
preorder(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a `BitSet` containing all basic blocks reachable from the `START_BLOCK`.
|
2021-12-15 19:32:30 -05:00
|
|
|
pub fn reachable_as_bitset<'tcx>(body: &Body<'tcx>) -> BitSet<BasicBlock> {
|
2020-07-08 09:47:14 -07:00
|
|
|
let mut iter = preorder(body);
|
|
|
|
(&mut iter).for_each(drop);
|
|
|
|
iter.visited
|
|
|
|
}
|
2022-04-28 11:31:08 +08:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReversePostorderIter<'a, 'tcx> {
|
|
|
|
body: &'a Body<'tcx>,
|
2022-06-03 20:42:42 +04:00
|
|
|
blocks: &'a [BasicBlock],
|
2022-04-28 11:31:08 +08:00
|
|
|
idx: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for ReversePostorderIter<'a, 'tcx> {
|
|
|
|
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
|
|
|
if self.idx == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.idx -= 1;
|
|
|
|
|
|
|
|
self.blocks.get(self.idx).map(|&bb| (bb, &self.body[bb]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.idx, Some(self.idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ExactSizeIterator for ReversePostorderIter<'a, 'tcx> {}
|
|
|
|
|
|
|
|
pub fn reverse_postorder<'a, 'tcx>(body: &'a Body<'tcx>) -> ReversePostorderIter<'a, 'tcx> {
|
|
|
|
let blocks = body.postorder_cache.compute(body);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-06-03 20:42:42 +04:00
|
|
|
/// Returns the `&[BasicBlocks]` represents the postorder graph for this MIR.
|
2022-04-28 11:31:08 +08:00
|
|
|
#[inline]
|
2022-06-03 20:42:42 +04:00
|
|
|
pub(super) fn compute(&self, body: &Body<'_>) -> &[BasicBlock] {
|
2022-04-28 11:31:08 +08:00
|
|
|
self.cache.get_or_init(|| Postorder::new(body, START_BLOCK).map(|(bb, _)| bb).collect())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:52:01 +10:00
|
|
|
impl<S: Encoder> Encodable<S> for PostorderCache {
|
2022-04-28 11:31:08 +08:00
|
|
|
#[inline]
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
fn encode(&self, _s: &mut S) {}
|
2022-04-28 11:31:08 +08:00
|
|
|
}
|
|
|
|
|
2022-06-14 14:52:01 +10:00
|
|
|
impl<D: Decoder> Decodable<D> for PostorderCache {
|
2022-04-28 11:31:08 +08:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-17 11:05:17 +01:00
|
|
|
TrivialTypeTraversalAndLiftImpls! {
|
2022-04-28 11:31:08 +08:00
|
|
|
PostorderCache,
|
|
|
|
}
|