@@ -41,6 +41,12 @@ impl<'a, 'tcx> Preorder<'a, 'tcx> {
4141 }
4242}
4343
44+ /// Preorder traversal of a graph.
45+ ///
46+ /// This function creates an iterator over the `Body`'s basic blocks, that
47+ /// returns basic blocks in a preorder.
48+ ///
49+ /// See [`Preorder`]'s docs to learn what is preorder traversal.
4450pub fn preorder < ' a , ' tcx > ( body : & ' a Body < ' tcx > ) -> Preorder < ' a , ' tcx > {
4551 Preorder :: new ( body, START_BLOCK )
4652}
@@ -213,10 +219,14 @@ impl<'tcx> Iterator for Postorder<'_, 'tcx> {
213219 }
214220}
215221
216- /// Creates an iterator over the `Body`'s basic blocks, that:
222+ /// Postorder traversal of a graph.
223+ ///
224+ /// This function creates an iterator over the `Body`'s basic blocks, that:
217225/// - returns basic blocks in a postorder,
218226/// - traverses the `BasicBlocks` CFG cache's reverse postorder backwards, and does not cache the
219227/// postorder itself.
228+ ///
229+ /// See [`Postorder`]'s docs to learn what is postorder traversal.
220230pub fn postorder < ' a , ' tcx > (
221231 body : & ' a Body < ' tcx > ,
222232) -> impl Iterator < Item = ( BasicBlock , & ' a BasicBlockData < ' tcx > ) > + ExactSizeIterator + DoubleEndedIterator
@@ -241,9 +251,30 @@ pub fn reachable_as_bitset(body: &Body<'_>) -> BitSet<BasicBlock> {
241251 iter. visited
242252}
243253
244- /// Creates an iterator over the `Body`'s basic blocks, that:
254+ /// Reverse postorder traversal of a graph.
255+ ///
256+ /// This function creates an iterator over the `Body`'s basic blocks, that:
245257/// - returns basic blocks in a reverse postorder,
246258/// - makes use of the `BasicBlocks` CFG cache's reverse postorder.
259+ ///
260+ /// Reverse postorder is the reverse order of a postorder traversal.
261+ /// This is different to a preorder traversal and represents a natural
262+ /// linearization of control-flow.
263+ ///
264+ /// ```text
265+ ///
266+ /// A
267+ /// / \
268+ /// / \
269+ /// B C
270+ /// \ /
271+ /// \ /
272+ /// D
273+ /// ```
274+ ///
275+ /// A reverse postorder traversal of this graph is either `A B C D` or `A C B D`
276+ /// Note that for a graph containing no loops (i.e., A DAG), this is equivalent to
277+ /// a topological sort.
247278pub fn reverse_postorder < ' a , ' tcx > (
248279 body : & ' a Body < ' tcx > ,
249280) -> impl Iterator < Item = ( BasicBlock , & ' a BasicBlockData < ' tcx > ) > + ExactSizeIterator + DoubleEndedIterator
0 commit comments