blob: 27b3b5e13667e44bd47b52e2f93fab83538373f1 [file] [log] [blame]
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +02001use rustc_data_structures::fx::FxHashMap;
ThiƩbaud Weksteen5bd94c12021-01-06 15:18:42 +01002use rustc_data_structures::graph::implementation::{Direction, Graph, NodeIndex, INCOMING};
Chris Wailes32f78352021-07-20 14:04:55 -07003use rustc_index::vec::IndexVec;
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +02004
Chris Wailes32f78352021-07-20 14:04:55 -07005use super::{DepKind, DepNode, DepNodeIndex};
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +02006
7pub struct DepGraphQuery<K> {
8 pub graph: Graph<DepNode<K>, ()>,
9 pub indices: FxHashMap<DepNode<K>, NodeIndex>,
Chris Wailes32f78352021-07-20 14:04:55 -070010 pub dep_index_to_index: IndexVec<DepNodeIndex, Option<NodeIndex>>,
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020011}
12
13impl<K: DepKind> DepGraphQuery<K> {
Chris Wailes32f78352021-07-20 14:04:55 -070014 pub fn new(prev_node_count: usize) -> DepGraphQuery<K> {
15 let node_count = prev_node_count + prev_node_count / 4;
16 let edge_count = 6 * node_count;
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020017
Chris Wailes32f78352021-07-20 14:04:55 -070018 let graph = Graph::with_capacity(node_count, edge_count);
19 let indices = FxHashMap::default();
20 let dep_index_to_index = IndexVec::new();
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020021
Chris Wailes32f78352021-07-20 14:04:55 -070022 DepGraphQuery { graph, indices, dep_index_to_index }
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020023 }
24
Chris Wailes32f78352021-07-20 14:04:55 -070025 pub fn push(&mut self, index: DepNodeIndex, node: DepNode<K>, edges: &[DepNodeIndex]) {
26 let source = self.graph.add_node(node);
27 if index.index() >= self.dep_index_to_index.len() {
28 self.dep_index_to_index.resize(index.index() + 1, None);
29 }
30 self.dep_index_to_index[index] = Some(source);
31 self.indices.insert(node, source);
32
33 for &target in edges.iter() {
34 let target = self.dep_index_to_index[target];
35 // We may miss the edges that are pushed while the `DepGraphQuery` is being accessed.
36 // Skip them to issues.
37 if let Some(target) = target {
38 self.graph.add_edge(source, target, ());
39 }
40 }
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020041 }
42
43 pub fn nodes(&self) -> Vec<&DepNode<K>> {
44 self.graph.all_nodes().iter().map(|n| &n.data).collect()
45 }
46
47 pub fn edges(&self) -> Vec<(&DepNode<K>, &DepNode<K>)> {
48 self.graph
49 .all_edges()
50 .iter()
51 .map(|edge| (edge.source(), edge.target()))
52 .map(|(s, t)| (self.graph.node_data(s), self.graph.node_data(t)))
53 .collect()
54 }
55
56 fn reachable_nodes(&self, node: &DepNode<K>, direction: Direction) -> Vec<&DepNode<K>> {
57 if let Some(&index) = self.indices.get(node) {
58 self.graph.depth_traverse(index, direction).map(|s| self.graph.node_data(s)).collect()
59 } else {
60 vec![]
61 }
62 }
63
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020064 /// All nodes that can reach `node`.
65 pub fn transitive_predecessors(&self, node: &DepNode<K>) -> Vec<&DepNode<K>> {
66 self.reachable_nodes(node, INCOMING)
67 }
Jeff Vander Stoepad6790c2020-06-24 15:34:31 +020068}