]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/mono.rs
remove implementation detail from doc
[rust.git] / src / librustc / mir / mono.rs
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.
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 syntax::ast::NodeId;
12 use syntax::symbol::InternedString;
13 use ty::Instance;
14 use util::nodemap::FxHashMap;
15 use rustc_data_structures::stable_hasher::{HashStable, StableHasherResult,
16                                            StableHasher};
17 use ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
18
19 #[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
20 pub enum MonoItem<'tcx> {
21     Fn(Instance<'tcx>),
22     Static(NodeId),
23     GlobalAsm(NodeId),
24 }
25
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);
31
32         match *self {
33             MonoItem::Fn(ref instance) => {
34                 instance.hash_stable(hcx, hasher);
35             }
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);
40                 })
41             }
42         }
43     }
44 }
45
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.
51     name: InternedString,
52     items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
53 }
54
55 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
56 pub enum Linkage {
57     External,
58     AvailableExternally,
59     LinkOnceAny,
60     LinkOnceODR,
61     WeakAny,
62     WeakODR,
63     Appending,
64     Internal,
65     Private,
66     ExternalWeak,
67     Common,
68 }
69
70 impl_stable_hash_for!(enum self::Linkage {
71     External,
72     AvailableExternally,
73     LinkOnceAny,
74     LinkOnceODR,
75     WeakAny,
76     WeakODR,
77     Appending,
78     Internal,
79     Private,
80     ExternalWeak,
81     Common
82 });
83
84 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
85 pub enum Visibility {
86     Default,
87     Hidden,
88     Protected,
89 }
90
91 impl_stable_hash_for!(enum self::Visibility {
92     Default,
93     Hidden,
94     Protected
95 });
96
97 impl<'tcx> CodegenUnit<'tcx> {
98     pub fn new(name: InternedString) -> CodegenUnit<'tcx> {
99         CodegenUnit {
100             name: name,
101             items: FxHashMap(),
102         }
103     }
104
105     pub fn name(&self) -> &InternedString {
106         &self.name
107     }
108
109     pub fn set_name(&mut self, name: InternedString) {
110         self.name = name;
111     }
112
113     pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
114         &self.items
115     }
116
117     pub fn items_mut(&mut self)
118         -> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>
119     {
120         &mut self.items
121     }
122 }
123
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>) {
128         let CodegenUnit {
129             ref items,
130             name,
131         } = *self;
132
133         name.hash_stable(hcx, hasher);
134
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)
140         }).collect();
141
142         items.sort_unstable_by_key(|i| i.0);
143         items.hash_stable(hcx, hasher);
144     }
145 }
146
147 #[derive(Clone, Default)]
148 pub struct Stats {
149     pub n_glues_created: usize,
150     pub n_null_glues: usize,
151     pub n_real_glues: usize,
152     pub n_fns: 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)>,
159 }
160
161 impl_stable_hash_for!(struct self::Stats {
162     n_glues_created,
163     n_null_glues,
164     n_real_glues,
165     n_fns,
166     n_inlines,
167     n_closures,
168     n_llvm_insns,
169     llvm_insns,
170     fn_stats
171 });
172
173 impl 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;
182
183         for (k, v) in stats.llvm_insns {
184             *self.llvm_insns.entry(k).or_insert(0) += v;
185         }
186         self.fn_stats.extend(stats.fn_stats);
187     }
188 }
189