2019-09-26 05:30:10 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
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,
|
2022-07-05 00:00:00 +00:00
|
|
|
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.
|
2022-07-05 00:00:00 +00: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> {
|
2023-03-31 00:32:44 -07:00
|
|
|
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'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> {
|
2022-07-04 00:00:00 +00:00
|
|
|
pub fn new(
|
2023-03-31 00:32:44 -07:00
|
|
|
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
|
2022-07-04 00:00:00 +00:00
|
|
|
root: BasicBlock,
|
|
|
|
|
) -> Postorder<'a, 'tcx> {
|
2016-03-11 13:14:51 +13:00
|
|
|
let mut po = Postorder {
|
2022-07-04 00:00:00 +00:00
|
|
|
basic_blocks,
|
|
|
|
|
visited: BitSet::new_empty(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
|
|
|
};
|
|
|
|
|
|
2022-07-04 00:00:00 +00:00
|
|
|
let data = &po.basic_blocks[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
|
|
|
|
|
// | |
|
|
|
|
|
// | |
|
2023-05-21 01:31:17 -07:00
|
|
|
// | D
|
2016-03-11 13:14:51 +13:00
|
|
|
// \ /
|
|
|
|
|
// \ /
|
|
|
|
|
// 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
|
|
|
//
|
2023-05-21 01:31:17 -07:00
|
|
|
// [(C, [D]), // `C` taken from the successors of `A`, pushed to the
|
|
|
|
|
// // top of the stack along with the successors of `C`
|
|
|
|
|
// (A, [B])]
|
2016-03-11 13:14:51 +13:00
|
|
|
//
|
2023-05-21 01:31:17 -07:00
|
|
|
// [(D, [E]), // `D` taken from successors of `C`, pushed to stack
|
|
|
|
|
// (C, []),
|
|
|
|
|
// (A, [B])]
|
2016-03-11 13:14:51 +13:00
|
|
|
//
|
|
|
|
|
// [(E, []), // `E` taken from successors of `D`, pushed to stack
|
|
|
|
|
// (D, []),
|
2023-05-21 01:31:17 -07:00
|
|
|
// (C, []),
|
|
|
|
|
// (A, [B])]
|
2016-03-11 13:14:51 +13:00
|
|
|
//
|
|
|
|
|
// Now that the top of the stack has no successors we can traverse, each item will
|
2023-05-21 01:31:17 -07:00
|
|
|
// be popped off during iteration until we get back to `A`. This yields [E, D, C].
|
2016-03-11 13:14:51 +13:00
|
|
|
//
|
2023-05-21 01:31:17 -07:00
|
|
|
// When we yield `C` and call `traverse_successor`, we push `B` 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
|
2023-05-21 01:31:17 -07:00
|
|
|
// two iterations yield `B` and finally `A` for a final traversal of [E, D, C, B, A]
|
|
|
|
|
while let Some(&mut (_, ref mut iter)) = self.visit_stack.last_mut() && let Some(bb) = iter.next_back() {
|
2018-07-22 19:23:39 +03:00
|
|
|
if self.visited.insert(bb) {
|
2022-07-04 00:00:00 +00:00
|
|
|
if let Some(term) = &self.basic_blocks[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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-04 00:00:00 +00:00
|
|
|
next.map(|(bb, _)| (bb, &self.basic_blocks[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.
|
2022-07-04 00:00:00 +00:00
|
|
|
let upper = self.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
|
|
|
}
|
|
|
|
|
|
2023-06-14 20:00:23 +00:00
|
|
|
/// Creates an iterator over the `Body`'s basic blocks, that:
|
|
|
|
|
/// - returns basic blocks in a postorder,
|
|
|
|
|
/// - traverses the `BasicBlocks` CFG cache's reverse postorder backwards, and does not cache the
|
|
|
|
|
/// postorder itself.
|
2023-06-18 09:16:40 +00:00
|
|
|
pub fn postorder<'a, 'tcx>(
|
2023-06-14 20:00:23 +00:00
|
|
|
body: &'a Body<'tcx>,
|
2023-06-18 09:16:40 +00:00
|
|
|
) -> impl Iterator<Item = (BasicBlock, &'a BasicBlockData<'tcx>)> + ExactSizeIterator + DoubleEndedIterator
|
|
|
|
|
{
|
|
|
|
|
reverse_postorder(body).rev()
|
2023-06-14 20:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
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`.
|
2022-12-20 22:10:40 +01:00
|
|
|
pub fn reachable_as_bitset(body: &Body<'_>) -> 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
|
|
|
|
2023-06-14 19:59:41 +00:00
|
|
|
/// Creates an iterator over the `Body`'s basic blocks, that:
|
|
|
|
|
/// - returns basic blocks in a reverse postorder,
|
|
|
|
|
/// - makes use of the `BasicBlocks` CFG cache's reverse postorder.
|
|
|
|
|
pub fn reverse_postorder<'a, 'tcx>(
|
2022-04-28 11:31:08 +08:00
|
|
|
body: &'a Body<'tcx>,
|
2023-06-18 09:16:40 +00:00
|
|
|
) -> impl Iterator<Item = (BasicBlock, &'a BasicBlockData<'tcx>)> + ExactSizeIterator + DoubleEndedIterator
|
|
|
|
|
{
|
2023-06-14 19:59:41 +00:00
|
|
|
body.basic_blocks.reverse_postorder().iter().map(|&bb| (bb, &body.basic_blocks[bb]))
|
2022-04-28 11:31:08 +08:00
|
|
|
}
|