|
1 |
| -use petgraph::{visit::EdgeRef, Direction, Graph}; |
| 1 | +use std::hash::Hash; |
| 2 | + |
| 3 | +use petgraph::{ |
| 4 | + visit::{ControlFlow, DfsEvent, EdgeRef, IntoNeighbors, Time, VisitMap, Visitable}, |
| 5 | + Direction, Graph, |
| 6 | +}; |
2 | 7 | use rustc_hash::FxHashSet;
|
3 | 8 |
|
4 | 9 | use crate::BasicBlockId;
|
|
56 | 61 |
|
57 | 62 | final_states
|
58 | 63 | }
|
| 64 | + |
| 65 | +/// Copied from petgraph's `dfsvisit`. |
| 66 | +/// Return if the expression is a break value, execute the provided statement |
| 67 | +/// if it is a prune value. |
| 68 | +macro_rules! try_control { |
| 69 | + ($e:expr, $p:stmt) => { |
| 70 | + try_control!($e, $p, ()); |
| 71 | + }; |
| 72 | + ($e:expr, $p:stmt, $q:stmt) => { |
| 73 | + match $e { |
| 74 | + x => |
| 75 | + { |
| 76 | + #[allow(clippy::redundant_else)] |
| 77 | + if x.should_break() { |
| 78 | + return x; |
| 79 | + } else if x.should_prune() { |
| 80 | + $p |
| 81 | + } else { |
| 82 | + $q |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +/// Similar to `depth_first_search` but uses a `HashSet` underneath. Ideal for small subgraphs. |
| 90 | +pub fn set_depth_first_search<G, I, F, C, N>(graph: G, starts: I, mut visitor: F) -> C |
| 91 | +where |
| 92 | + N: Copy + PartialEq + Eq + Hash, |
| 93 | + G: IntoNeighbors + Visitable<NodeId = N>, |
| 94 | + I: IntoIterator<Item = G::NodeId>, |
| 95 | + F: FnMut(DfsEvent<G::NodeId>) -> C, |
| 96 | + C: ControlFlow, |
| 97 | +{ |
| 98 | + let time = &mut Time(0); |
| 99 | + let discovered = &mut FxHashSet::<G::NodeId>::default(); |
| 100 | + let finished = &mut FxHashSet::<G::NodeId>::default(); |
| 101 | + |
| 102 | + for start in starts { |
| 103 | + try_control!( |
| 104 | + dfs_visitor(graph, start, &mut visitor, discovered, finished, time), |
| 105 | + unreachable!() |
| 106 | + ); |
| 107 | + } |
| 108 | + C::continuing() |
| 109 | +} |
| 110 | + |
| 111 | +fn dfs_visitor<G, M, F, C>( |
| 112 | + graph: G, |
| 113 | + u: G::NodeId, |
| 114 | + visitor: &mut F, |
| 115 | + discovered: &mut M, |
| 116 | + finished: &mut M, |
| 117 | + time: &mut Time, |
| 118 | +) -> C |
| 119 | +where |
| 120 | + G: IntoNeighbors + Visitable, |
| 121 | + M: VisitMap<G::NodeId>, |
| 122 | + F: FnMut(DfsEvent<G::NodeId>) -> C, |
| 123 | + C: ControlFlow, |
| 124 | +{ |
| 125 | + if !discovered.visit(u) { |
| 126 | + return C::continuing(); |
| 127 | + } |
| 128 | + |
| 129 | + try_control!( |
| 130 | + visitor(DfsEvent::Discover(u, time_post_inc(time))), |
| 131 | + {}, |
| 132 | + for v in graph.neighbors(u) { |
| 133 | + if !discovered.is_visited(&v) { |
| 134 | + try_control!(visitor(DfsEvent::TreeEdge(u, v)), continue); |
| 135 | + try_control!( |
| 136 | + dfs_visitor(graph, v, visitor, discovered, finished, time), |
| 137 | + unreachable!() |
| 138 | + ); |
| 139 | + } else if !finished.is_visited(&v) { |
| 140 | + try_control!(visitor(DfsEvent::BackEdge(u, v)), continue); |
| 141 | + } else { |
| 142 | + try_control!(visitor(DfsEvent::CrossForwardEdge(u, v)), continue); |
| 143 | + } |
| 144 | + } |
| 145 | + ); |
| 146 | + let first_finish = finished.visit(u); |
| 147 | + debug_assert!(first_finish); |
| 148 | + try_control!( |
| 149 | + visitor(DfsEvent::Finish(u, time_post_inc(time))), |
| 150 | + panic!("Pruning on the `DfsEvent::Finish` is not supported!") |
| 151 | + ); |
| 152 | + C::continuing() |
| 153 | +} |
| 154 | + |
| 155 | +fn time_post_inc(x: &mut Time) -> Time { |
| 156 | + let v = *x; |
| 157 | + x.0 += 1; |
| 158 | + v |
| 159 | +} |
0 commit comments