@@ -41,6 +41,12 @@ impl<'a, 'tcx> Preorder<'a, 'tcx> {
41
41
}
42
42
}
43
43
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.
44
50
pub fn preorder < ' a , ' tcx > ( body : & ' a Body < ' tcx > ) -> Preorder < ' a , ' tcx > {
45
51
Preorder :: new ( body, START_BLOCK )
46
52
}
@@ -213,10 +219,14 @@ impl<'tcx> Iterator for Postorder<'_, 'tcx> {
213
219
}
214
220
}
215
221
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:
217
225
/// - returns basic blocks in a postorder,
218
226
/// - traverses the `BasicBlocks` CFG cache's reverse postorder backwards, and does not cache the
219
227
/// postorder itself.
228
+ ///
229
+ /// See [`Postorder`]'s docs to learn what is postorder traversal.
220
230
pub fn postorder < ' a , ' tcx > (
221
231
body : & ' a Body < ' tcx > ,
222
232
) -> impl Iterator < Item = ( BasicBlock , & ' a BasicBlockData < ' tcx > ) > + ExactSizeIterator + DoubleEndedIterator
@@ -241,9 +251,30 @@ pub fn reachable_as_bitset(body: &Body<'_>) -> BitSet<BasicBlock> {
241
251
iter. visited
242
252
}
243
253
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:
245
257
/// - returns basic blocks in a reverse postorder,
246
258
/// - 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.
247
278
pub fn reverse_postorder < ' a , ' tcx > (
248
279
body : & ' a Body < ' tcx > ,
249
280
) -> impl Iterator < Item = ( BasicBlock , & ' a BasicBlockData < ' tcx > ) > + ExactSizeIterator + DoubleEndedIterator
0 commit comments