@@ -26,7 +26,7 @@ rustc_index::newtype_index! {
26
26
struct PreorderIndex { }
27
27
}
28
28
29
- pub fn dominator_tree < G : ControlFlowGraph > ( graph : G ) -> DominatorTree < G :: Node > {
29
+ pub fn dominators < G : ControlFlowGraph > ( graph : & G ) -> Dominators < G :: Node > {
30
30
// compute the post order index (rank) for each node
31
31
let mut post_order_rank = IndexVec :: from_elem_n ( 0 , graph. num_nodes ( ) ) ;
32
32
@@ -244,7 +244,10 @@ pub fn dominator_tree<G: ControlFlowGraph>(graph: G) -> DominatorTree<G::Node> {
244
244
245
245
let start_node = graph. start_node ( ) ;
246
246
immediate_dominators[ start_node] = None ;
247
- DominatorTree { start_node, post_order_rank, immediate_dominators }
247
+
248
+ let time = compute_access_time ( start_node, & immediate_dominators) ;
249
+
250
+ Dominators { start_node, post_order_rank, immediate_dominators, time }
248
251
}
249
252
250
253
/// Evaluate the link-eval virtual forest, providing the currently minimum semi
@@ -309,16 +312,17 @@ fn compress(
309
312
310
313
/// Tracks the list of dominators for each node.
311
314
#[ derive( Clone , Debug ) ]
312
- pub struct DominatorTree < N : Idx > {
315
+ pub struct Dominators < N : Idx > {
313
316
start_node : N ,
314
317
post_order_rank : IndexVec < N , usize > ,
315
318
// Even though we track only the immediate dominator of each node, it's
316
319
// possible to get its full list of dominators by looking up the dominator
317
320
// of each dominator. (See the `impl Iterator for Iter` definition).
318
321
immediate_dominators : IndexVec < N , Option < N > > ,
322
+ time : IndexVec < N , Time > ,
319
323
}
320
324
321
- impl < Node : Idx > DominatorTree < Node > {
325
+ impl < Node : Idx > Dominators < Node > {
322
326
/// Returns true if node is reachable from the start node.
323
327
pub fn is_reachable ( & self , node : Node ) -> bool {
324
328
node == self . start_node || self . immediate_dominators [ node] . is_some ( )
@@ -343,10 +347,22 @@ impl<Node: Idx> DominatorTree<Node> {
343
347
pub fn rank_partial_cmp ( & self , lhs : Node , rhs : Node ) -> Option < Ordering > {
344
348
self . post_order_rank [ rhs] . partial_cmp ( & self . post_order_rank [ lhs] )
345
349
}
350
+
351
+ /// Returns true if `a` dominates `b`.
352
+ ///
353
+ /// # Panics
354
+ ///
355
+ /// Panics if `b` is unreachable.
356
+ pub fn dominates ( & self , a : Node , b : Node ) -> bool {
357
+ let a = self . time [ a] ;
358
+ let b = self . time [ b] ;
359
+ assert ! ( b. start != 0 , "node {b:?} is not reachable" ) ;
360
+ a. start <= b. start && b. finish <= a. finish
361
+ }
346
362
}
347
363
348
364
pub struct Iter < ' dom , Node : Idx > {
349
- dom_tree : & ' dom DominatorTree < Node > ,
365
+ dom_tree : & ' dom Dominators < Node > ,
350
366
node : Option < Node > ,
351
367
}
352
368
@@ -363,11 +379,6 @@ impl<'dom, Node: Idx> Iterator for Iter<'dom, Node> {
363
379
}
364
380
}
365
381
366
- #[ derive( Clone , Debug ) ]
367
- pub struct Dominators < Node : Idx > {
368
- time : IndexVec < Node , Time > ,
369
- }
370
-
371
382
/// Describes the number of vertices discovered at the time when processing of a particular vertex
372
383
/// started and when it finished. Both values are zero for unreachable vertices.
373
384
#[ derive( Copy , Clone , Default , Debug ) ]
@@ -376,27 +387,10 @@ struct Time {
376
387
finish : u32 ,
377
388
}
378
389
379
- impl < Node : Idx > Dominators < Node > {
380
- pub fn dummy ( ) -> Self {
381
- Self { time : Default :: default ( ) }
382
- }
383
-
384
- /// Returns true if `a` dominates `b`.
385
- ///
386
- /// # Panics
387
- ///
388
- /// Panics if `b` is unreachable.
389
- pub fn dominates ( & self , a : Node , b : Node ) -> bool {
390
- let a = self . time [ a] ;
391
- let b = self . time [ b] ;
392
- assert ! ( b. start != 0 , "node {b:?} is not reachable" ) ;
393
- a. start <= b. start && b. finish <= a. finish
394
- }
395
- }
396
-
397
- pub fn dominators < N : Idx > ( tree : & DominatorTree < N > ) -> Dominators < N > {
398
- let DominatorTree { start_node, ref immediate_dominators, post_order_rank : _ } = * tree;
399
-
390
+ fn compute_access_time < N : Idx > (
391
+ start_node : N ,
392
+ immediate_dominators : & IndexSlice < N , Option < N > > ,
393
+ ) -> IndexVec < N , Time > {
400
394
// Transpose the dominator tree edges, so that child nodes of vertex v are stored in
401
395
// node[edges[v].start..edges[v].end].
402
396
let mut edges: IndexVec < N , std:: ops:: Range < u32 > > =
@@ -446,5 +440,5 @@ pub fn dominators<N: Idx>(tree: &DominatorTree<N>) -> Dominators<N> {
446
440
}
447
441
}
448
442
449
- Dominators { time }
443
+ time
450
444
}
0 commit comments