1 // Copyright 2017 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 syntax::ast::NodeId;
12 use syntax::symbol::InternedString;
14 use util::nodemap::FxHashMap;
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasherResult,
17 use ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
19 #[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
20 pub enum MonoItem<'tcx> {
26 impl<'tcx> HashStable<StableHashingContext<'tcx>> for MonoItem<'tcx> {
27 fn hash_stable<W: StableHasherResult>(&self,
28 hcx: &mut StableHashingContext<'tcx>,
29 hasher: &mut StableHasher<W>) {
30 ::std::mem::discriminant(self).hash_stable(hcx, hasher);
33 MonoItem::Fn(ref instance) => {
34 instance.hash_stable(hcx, hasher);
36 MonoItem::Static(node_id) |
37 MonoItem::GlobalAsm(node_id) => {
38 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
39 node_id.hash_stable(hcx, hasher);
46 pub struct CodegenUnit<'tcx> {
47 /// A name for this CGU. Incremental compilation requires that
48 /// name be unique amongst **all** crates. Therefore, it should
49 /// contain something unique to this crate (e.g., a module path)
50 /// as well as the crate name and disambiguator.
52 items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
55 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
70 impl_stable_hash_for!(enum self::Linkage {
84 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
91 impl_stable_hash_for!(enum self::Visibility {
97 impl<'tcx> CodegenUnit<'tcx> {
98 pub fn new(name: InternedString) -> CodegenUnit<'tcx> {
105 pub fn name(&self) -> &InternedString {
109 pub fn set_name(&mut self, name: InternedString) {
113 pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
117 pub fn items_mut(&mut self)
118 -> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>
124 impl<'tcx> HashStable<StableHashingContext<'tcx>> for CodegenUnit<'tcx> {
125 fn hash_stable<W: StableHasherResult>(&self,
126 hcx: &mut StableHashingContext<'tcx>,
127 hasher: &mut StableHasher<W>) {
133 name.hash_stable(hcx, hasher);
135 let mut items: Vec<(Fingerprint, _)> = items.iter().map(|(trans_item, &attrs)| {
136 let mut hasher = StableHasher::new();
137 trans_item.hash_stable(hcx, &mut hasher);
138 let trans_item_fingerprint = hasher.finish();
139 (trans_item_fingerprint, attrs)
142 items.sort_unstable_by_key(|i| i.0);
143 items.hash_stable(hcx, hasher);
147 #[derive(Clone, Default)]
149 pub n_glues_created: usize,
150 pub n_null_glues: usize,
151 pub n_real_glues: usize,
153 pub n_inlines: usize,
154 pub n_closures: usize,
155 pub n_llvm_insns: usize,
156 pub llvm_insns: FxHashMap<String, usize>,
157 // (ident, llvm-instructions)
158 pub fn_stats: Vec<(String, usize)>,
161 impl_stable_hash_for!(struct self::Stats {
174 pub fn extend(&mut self, stats: Stats) {
175 self.n_glues_created += stats.n_glues_created;
176 self.n_null_glues += stats.n_null_glues;
177 self.n_real_glues += stats.n_real_glues;
178 self.n_fns += stats.n_fns;
179 self.n_inlines += stats.n_inlines;
180 self.n_closures += stats.n_closures;
181 self.n_llvm_insns += stats.n_llvm_insns;
183 for (k, v) in stats.llvm_insns {
184 *self.llvm_insns.entry(k).or_insert(0) += v;
186 self.fn_stats.extend(stats.fn_stats);