1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::dep_graph::{DepGraphQuery, DepNode};
12 use rustc::hir::def_id::DefId;
13 use rustc::ich::Fingerprint;
14 use rustc_data_structures::fx::FxHashMap;
15 use rustc_data_structures::graph::{Graph, NodeIndex};
21 /// A data-structure that makes it easy to enumerate the hashable
22 /// predecessors of any given dep-node.
23 pub struct Predecessors<'query> {
24 // A reduced version of the input graph that contains fewer nodes.
25 // This is intended to keep all of the base inputs (i.e., HIR
26 // nodes) and all of the "work-products" we may care about
27 // later. Other nodes may be retained if it keeps the overall size
29 pub reduced_graph: Graph<&'query DepNode<DefId>, ()>,
31 // These are output nodes that have no incoming edges. We have to
32 // track these specially because, when we load the data back up
33 // again, we want to make sure and recreate these nodes (we want
34 // to recreate the nodes where all incoming edges are clean; but
35 // since we ordinarily just serialize edges, we wind up just
36 // forgetting that bootstrap outputs even exist in that case.)
37 pub bootstrap_outputs: Vec<&'query DepNode<DefId>>,
39 // For the inputs (hir/foreign-metadata), we include hashes.
40 pub hashes: FxHashMap<&'query DepNode<DefId>, Fingerprint>,
43 impl<'q> Predecessors<'q> {
44 pub fn new(query: &'q DepGraphQuery<DefId>, hcx: &mut HashContext) -> Self {
47 let collect_for_metadata = tcx.sess.opts.debugging_opts.incremental_cc ||
48 tcx.sess.opts.debugging_opts.query_dep_graph;
50 // Find the set of "start nodes". These are nodes that we will
51 // possibly query later.
52 let is_output = |node: &DepNode<DefId>| -> bool {
54 DepNode::WorkProduct(_) => true,
55 DepNode::MetaData(ref def_id) => collect_for_metadata && def_id.is_local(),
57 // if -Z query-dep-graph is passed, save more extended data
58 // to enable better unit testing
59 DepNode::TypeckTables(_) |
60 DepNode::TransCrateItem(_) => tcx.sess.opts.debugging_opts.query_dep_graph,
66 // Reduce the graph to the most important nodes.
67 let compress::Reduction { graph, input_nodes } =
68 compress::reduce_graph(&query.graph, HashContext::is_hashable, |n| is_output(n));
70 let mut hashes = FxHashMap();
71 for input_index in input_nodes {
72 let input = *graph.node_data(input_index);
73 debug!("computing hash for input node `{:?}`", input);
75 .or_insert_with(|| hcx.hash(input).unwrap());
78 let bootstrap_outputs: Vec<&'q DepNode<DefId>> =
79 (0 .. graph.len_nodes())
81 .filter(|&n| graph.incoming_edges(n).next().is_none())
82 .map(|n| *graph.node_data(n))
83 .filter(|n| is_output(n))
88 bootstrap_outputs: bootstrap_outputs,