]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/mod.rs
Merge remote-tracking branch 'origin/master' into gen
[rust.git] / src / librustc_mir / transform / mod.rs
1 // Copyright 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 build;
12 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
13 use rustc::mir::Mir;
14 use rustc::mir::transform::{MirPassIndex, MirSuite, MirSource,
15                             MIR_CONST, MIR_VALIDATED, MIR_OPTIMIZED};
16 use rustc::ty::{self, TyCtxt};
17 use rustc::ty::maps::Providers;
18 use rustc::ty::steal::Steal;
19 use rustc::hir;
20 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
21 use rustc::util::nodemap::DefIdSet;
22 use std::rc::Rc;
23 use syntax::ast;
24 use syntax_pos::{DUMMY_SP, Span};
25 use transform;
26
27 pub mod add_validation;
28 pub mod clean_end_regions;
29 pub mod simplify_branches;
30 pub mod simplify;
31 pub mod erase_regions;
32 pub mod no_landing_pads;
33 pub mod type_check;
34 pub mod borrow_check;
35 pub mod rustc_peek;
36 pub mod elaborate_drops;
37 pub mod add_call_guards;
38 pub mod promote_consts;
39 pub mod qualify_consts;
40 pub mod dump_mir;
41 pub mod deaggregator;
42 pub mod instcombine;
43 pub mod copy_prop;
44 pub mod generator;
45 pub mod inline;
46 pub mod nll;
47
48 pub(crate) fn provide(providers: &mut Providers) {
49     self::qualify_consts::provide(providers);
50     *providers = Providers {
51         mir_keys,
52         mir_const,
53         mir_validated,
54         optimized_mir,
55         is_mir_available,
56         ..*providers
57     };
58 }
59
60 fn is_mir_available<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool {
61     tcx.mir_keys(def_id.krate).contains(&def_id)
62 }
63
64 /// Finds the full set of def-ids within the current crate that have
65 /// MIR associated with them.
66 fn mir_keys<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, krate: CrateNum)
67                       -> Rc<DefIdSet> {
68     assert_eq!(krate, LOCAL_CRATE);
69
70     let mut set = DefIdSet();
71
72     // All body-owners have MIR associated with them.
73     set.extend(tcx.body_owners());
74
75     // Additionally, tuple struct/variant constructors have MIR, but
76     // they don't have a BodyId, so we need to build them separately.
77     struct GatherCtors<'a, 'tcx: 'a> {
78         tcx: TyCtxt<'a, 'tcx, 'tcx>,
79         set: &'a mut DefIdSet,
80     }
81     impl<'a, 'tcx> Visitor<'tcx> for GatherCtors<'a, 'tcx> {
82         fn visit_variant_data(&mut self,
83                               v: &'tcx hir::VariantData,
84                               _: ast::Name,
85                               _: &'tcx hir::Generics,
86                               _: ast::NodeId,
87                               _: Span) {
88             if let hir::VariantData::Tuple(_, node_id) = *v {
89                 self.set.insert(self.tcx.hir.local_def_id(node_id));
90             }
91             intravisit::walk_struct_def(self, v)
92         }
93         fn nested_visit_map<'b>(&'b mut self) -> NestedVisitorMap<'b, 'tcx> {
94             NestedVisitorMap::None
95         }
96     }
97     tcx.hir.krate().visit_all_item_likes(&mut GatherCtors {
98         tcx,
99         set: &mut set,
100     }.as_deep_visitor());
101
102     Rc::new(set)
103 }
104
105 fn mir_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
106     let mut mir = build::mir_build(tcx, def_id);
107     let source = MirSource::from_local_def_id(tcx, def_id);
108     transform::run_suite(tcx, source, MIR_CONST, &mut mir);
109     tcx.alloc_steal_mir(mir)
110 }
111
112 fn mir_validated<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Steal<Mir<'tcx>> {
113     let source = MirSource::from_local_def_id(tcx, def_id);
114     if let MirSource::Const(_) = source {
115         // Ensure that we compute the `mir_const_qualif` for constants at
116         // this point, before we steal the mir-const result. We don't
117         // directly need the result or `mir_const_qualif`, so we can just force it.
118         ty::queries::mir_const_qualif::force(tcx, DUMMY_SP, def_id);
119     }
120
121     let mut mir = tcx.mir_const(def_id).steal();
122     transform::run_suite(tcx, source, MIR_VALIDATED, &mut mir);
123     tcx.alloc_steal_mir(mir)
124 }
125
126 fn optimized_mir<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> &'tcx Mir<'tcx> {
127     // Borrowck uses `mir_validated`, so we have to force it to
128     // execute before we can steal.
129     ty::queries::borrowck::force(tcx, DUMMY_SP, def_id);
130
131     let mut mir = tcx.mir_validated(def_id).steal();
132     let source = MirSource::from_local_def_id(tcx, def_id);
133     transform::run_suite(tcx, source, MIR_OPTIMIZED, &mut mir);
134     tcx.alloc_mir(mir)
135 }
136
137 fn run_suite<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
138                        source: MirSource,
139                        suite: MirSuite,
140                        mir: &mut Mir<'tcx>)
141 {
142     let passes = tcx.mir_passes.passes(suite);
143
144     for (pass, index) in passes.iter().zip(0..) {
145         let pass_num = MirPassIndex(index);
146
147         for hook in tcx.mir_passes.hooks() {
148             hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, false);
149         }
150
151         pass.run_pass(tcx, source, mir);
152
153         for hook in tcx.mir_passes.hooks() {
154             hook.on_mir_pass(tcx, suite, pass_num, &pass.name(), source, &mir, true);
155         }
156     }
157 }