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_data_structures::fx::FxHashMap;
14 use rustc_data_structures::graph::Graph;
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 // For the inputs (hir/foreign-metadata), we include hashes.
32 pub hashes: FxHashMap<&'query DepNode<DefId>, Fingerprint>,
35 impl<'q> Predecessors<'q> {
36 pub fn new(query: &'q DepGraphQuery<DefId>, hcx: &mut HashContext) -> Self {
39 let collect_for_metadata = tcx.sess.opts.debugging_opts.incremental_cc ||
40 tcx.sess.opts.debugging_opts.query_dep_graph;
42 // Find the set of "start nodes". These are nodes that we will
43 // possibly query later.
44 let is_output = |node: &DepNode<DefId>| -> bool {
46 DepNode::WorkProduct(_) => true,
47 DepNode::MetaData(ref def_id) => collect_for_metadata && def_id.is_local(),
49 // if -Z query-dep-graph is passed, save more extended data
50 // to enable better unit testing
51 DepNode::TypeckTables(_) |
52 DepNode::TransCrateItem(_) => tcx.sess.opts.debugging_opts.query_dep_graph,
58 // Reduce the graph to the most important nodes.
59 let compress::Reduction { graph, input_nodes } =
60 compress::reduce_graph(&query.graph, HashContext::is_hashable, is_output);
62 let mut hashes = FxHashMap();
63 for input_index in input_nodes {
64 let input = *graph.node_data(input_index);
65 debug!("computing hash for input node `{:?}`", input);
67 .or_insert_with(|| hcx.hash(input).unwrap());