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.
|
|
|
|
///
|
2020-01-28 23:06:58 +01:00
|
|
|
/// Preorder traversal is when each node is visited before any of its
|
2016-03-11 13:14:51 +13:00
|
|
|
/// successors
|
|
|
|
///
|
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.
|
|
|
|
///
|
2020-01-28 23:06:58 +01:00
|
|
|
/// Postorder traversal is when each node is visited after all of its
|
2016-03-11 13:14:51 +13:00
|
|
|
/// successors, except when the successor is only reachable by a back-edge
|
|
|
|
///
|
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() {
|
2018-04-27 14:02:09 +03: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>,
|
2019-12-22 17:42:04 -05:00
|
|
|
idx: usize,
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2019-12-22 17:42:04 -05:00
|
|
|
let blocks: Vec<_> = Postorder::new(body, root).map(|(bb, _)| bb).collect();
|
2016-03-11 13:14:51 +13:00
|
|
|
|
|
|
|
let len = blocks.len();
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
ReversePostorder { body, blocks, idx: len }
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
pub fn reverse_postorder<'a, 'tcx>(body: &'a Body<'tcx>) -> ReversePostorder<'a, 'tcx> {
|
|
|
|
ReversePostorder::new(body, START_BLOCK)
|
2016-03-11 13:14:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Iterator for ReversePostorder<'a, 'tcx> {
|
|
|
|
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
|
2019-12-22 17:42:04 -05:00
|
|
|
if self.idx == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2016-03-11 13:14:51 +13:00
|
|
|
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`.
|
|
|
|
pub fn reachable_as_bitset(body: &Body<'tcx>) -> BitSet<BasicBlock> {
|
|
|
|
let mut iter = preorder(body);
|
|
|
|
(&mut iter).for_each(drop);
|
|
|
|
iter.visited
|
|
|
|
}
|