]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/persist/preds/mod.rs
Auto merge of #39523 - alexcrichton:fpic, r=aturon
[rust.git] / src / librustc_incremental / persist / preds / mod.rs
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.
4 //
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.
10
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;
15
16 use super::hash::*;
17 use ich::Fingerprint;
18
19 mod compress;
20
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
28     // of the graph down.
29     pub reduced_graph: Graph<&'query DepNode<DefId>, ()>,
30
31     // For the inputs (hir/foreign-metadata), we include hashes.
32     pub hashes: FxHashMap<&'query DepNode<DefId>, Fingerprint>,
33 }
34
35 impl<'q> Predecessors<'q> {
36     pub fn new(query: &'q DepGraphQuery<DefId>, hcx: &mut HashContext) -> Self {
37         let tcx = hcx.tcx;
38
39         let collect_for_metadata = tcx.sess.opts.debugging_opts.incremental_cc ||
40             tcx.sess.opts.debugging_opts.query_dep_graph;
41
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 {
45             match *node {
46                 DepNode::WorkProduct(_) => true,
47                 DepNode::MetaData(ref def_id) => collect_for_metadata && def_id.is_local(),
48
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,
53
54                 _ => false,
55             }
56         };
57
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);
61
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);
66             hashes.entry(input)
67                   .or_insert_with(|| hcx.hash(input).unwrap());
68         }
69
70         Predecessors {
71             reduced_graph: graph,
72             hashes: hashes,
73         }
74     }
75 }