]> git.proxmox.com Git - rustc.git/blob - src/librustc_mir/transform/mod.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / librustc_mir / transform / mod.rs
1 use crate::{shim, util};
2 use rustc::hir::map::Map;
3 use rustc::mir::{BodyAndCache, ConstQualifs, MirPhase, Promoted};
4 use rustc::ty::query::Providers;
5 use rustc::ty::steal::Steal;
6 use rustc::ty::{InstanceDef, TyCtxt, TypeFoldable};
7 use rustc_hir as hir;
8 use rustc_hir::def_id::{CrateNum, DefId, DefIdSet, LOCAL_CRATE};
9 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
10 use rustc_index::vec::IndexVec;
11 use rustc_span::Span;
12 use std::borrow::Cow;
13 use syntax::ast;
14
15 pub mod add_call_guards;
16 pub mod add_moves_for_packed_drops;
17 pub mod add_retag;
18 pub mod check_consts;
19 pub mod check_unsafety;
20 pub mod cleanup_post_borrowck;
21 pub mod const_prop;
22 pub mod copy_prop;
23 pub mod deaggregator;
24 pub mod dump_mir;
25 pub mod elaborate_drops;
26 pub mod erase_regions;
27 pub mod generator;
28 pub mod inline;
29 pub mod instcombine;
30 pub mod no_landing_pads;
31 pub mod promote_consts;
32 pub mod qualify_min_const_fn;
33 pub mod remove_noop_landing_pads;
34 pub mod rustc_peek;
35 pub mod simplify;
36 pub mod simplify_branches;
37 pub mod simplify_try;
38 pub mod uninhabited_enum_branching;
39 pub mod unreachable_prop;
40
41 pub(crate) fn provide(providers: &mut Providers<'_>) {
42 self::check_unsafety::provide(providers);
43 *providers = Providers {
44 mir_keys,
45 mir_const,
46 mir_const_qualif,
47 mir_validated,
48 optimized_mir,
49 is_mir_available,
50 promoted_mir,
51 ..*providers
52 };
53 }
54
55 fn is_mir_available(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
56 tcx.mir_keys(def_id.krate).contains(&def_id)
57 }
58
59 /// Finds the full set of `DefId`s within the current crate that have
60 /// MIR associated with them.
61 fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> &DefIdSet {
62 assert_eq!(krate, LOCAL_CRATE);
63
64 let mut set = DefIdSet::default();
65
66 // All body-owners have MIR associated with them.
67 set.extend(tcx.body_owners());
68
69 // Additionally, tuple struct/variant constructors have MIR, but
70 // they don't have a BodyId, so we need to build them separately.
71 struct GatherCtors<'a, 'tcx> {
72 tcx: TyCtxt<'tcx>,
73 set: &'a mut DefIdSet,
74 }
75 impl<'a, 'tcx> Visitor<'tcx> for GatherCtors<'a, 'tcx> {
76 fn visit_variant_data(
77 &mut self,
78 v: &'tcx hir::VariantData<'tcx>,
79 _: ast::Name,
80 _: &'tcx hir::Generics<'tcx>,
81 _: hir::HirId,
82 _: Span,
83 ) {
84 if let hir::VariantData::Tuple(_, hir_id) = *v {
85 self.set.insert(self.tcx.hir().local_def_id(hir_id));
86 }
87 intravisit::walk_struct_def(self, v)
88 }
89 type Map = Map<'tcx>;
90 fn nested_visit_map<'b>(&'b mut self) -> NestedVisitorMap<'b, Self::Map> {
91 NestedVisitorMap::None
92 }
93 }
94 tcx.hir()
95 .krate()
96 .visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor());
97
98 tcx.arena.alloc(set)
99 }
100
101 /// Where a specific `mir::Body` comes from.
102 #[derive(Debug, Copy, Clone)]
103 pub struct MirSource<'tcx> {
104 pub instance: InstanceDef<'tcx>,
105
106 /// If `Some`, this is a promoted rvalue within the parent function.
107 pub promoted: Option<Promoted>,
108 }
109
110 impl<'tcx> MirSource<'tcx> {
111 pub fn item(def_id: DefId) -> Self {
112 MirSource { instance: InstanceDef::Item(def_id), promoted: None }
113 }
114
115 #[inline]
116 pub fn def_id(&self) -> DefId {
117 self.instance.def_id()
118 }
119 }
120
121 /// Generates a default name for the pass based on the name of the
122 /// type `T`.
123 pub fn default_name<T: ?Sized>() -> Cow<'static, str> {
124 let name = ::std::any::type_name::<T>();
125 if let Some(tail) = name.rfind(":") { Cow::from(&name[tail + 1..]) } else { Cow::from(name) }
126 }
127
128 /// A streamlined trait that you can implement to create a pass; the
129 /// pass will be named after the type, and it will consist of a main
130 /// loop that goes over each available MIR and applies `run_pass`.
131 pub trait MirPass<'tcx> {
132 fn name(&self) -> Cow<'_, str> {
133 default_name::<Self>()
134 }
135
136 fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>);
137 }
138
139 pub fn run_passes(
140 tcx: TyCtxt<'tcx>,
141 body: &mut BodyAndCache<'tcx>,
142 instance: InstanceDef<'tcx>,
143 promoted: Option<Promoted>,
144 mir_phase: MirPhase,
145 passes: &[&dyn MirPass<'tcx>],
146 ) {
147 let phase_index = mir_phase.phase_index();
148
149 if body.phase >= mir_phase {
150 return;
151 }
152
153 let source = MirSource { instance, promoted };
154 let mut index = 0;
155 let mut run_pass = |pass: &dyn MirPass<'tcx>| {
156 let run_hooks = |body: &_, index, is_after| {
157 dump_mir::on_mir_pass(
158 tcx,
159 &format_args!("{:03}-{:03}", phase_index, index),
160 &pass.name(),
161 source,
162 body,
163 is_after,
164 );
165 };
166 run_hooks(body, index, false);
167 pass.run_pass(tcx, source, body);
168 run_hooks(body, index, true);
169
170 index += 1;
171 };
172
173 for pass in passes {
174 run_pass(*pass);
175 }
176
177 body.phase = mir_phase;
178 }
179
180 fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs {
181 let const_kind = check_consts::ConstKind::for_item(tcx, def_id);
182
183 // No need to const-check a non-const `fn`.
184 if const_kind.is_none() {
185 return Default::default();
186 }
187
188 // N.B., this `borrow()` is guaranteed to be valid (i.e., the value
189 // cannot yet be stolen), because `mir_validated()`, which steals
190 // from `mir_const(), forces this query to execute before
191 // performing the steal.
192 let body = &tcx.mir_const(def_id).borrow();
193
194 if body.return_ty().references_error() {
195 tcx.sess.delay_span_bug(body.span, "mir_const_qualif: MIR had errors");
196 return Default::default();
197 }
198
199 let item = check_consts::Item {
200 body: body.unwrap_read_only(),
201 tcx,
202 def_id,
203 const_kind,
204 param_env: tcx.param_env(def_id),
205 };
206
207 let mut validator = check_consts::validation::Validator::new(&item);
208 validator.check_body();
209
210 // We return the qualifs in the return place for every MIR body, even though it is only used
211 // when deciding to promote a reference to a `const` for now.
212 validator.qualifs_in_return_place().into()
213 }
214
215 fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> &Steal<BodyAndCache<'_>> {
216 // Unsafety check uses the raw mir, so make sure it is run
217 let _ = tcx.unsafety_check_result(def_id);
218
219 let mut body = tcx.mir_built(def_id).steal();
220
221 util::dump_mir(tcx, None, "mir_map", &0, MirSource::item(def_id), &body, |_, _| Ok(()));
222
223 run_passes(
224 tcx,
225 &mut body,
226 InstanceDef::Item(def_id),
227 None,
228 MirPhase::Const,
229 &[
230 // What we need to do constant evaluation.
231 &simplify::SimplifyCfg::new("initial"),
232 &rustc_peek::SanityCheck,
233 ],
234 );
235 body.ensure_predecessors();
236 tcx.alloc_steal_mir(body)
237 }
238
239 fn mir_validated(
240 tcx: TyCtxt<'tcx>,
241 def_id: DefId,
242 ) -> (&'tcx Steal<BodyAndCache<'tcx>>, &'tcx Steal<IndexVec<Promoted, BodyAndCache<'tcx>>>) {
243 // Ensure that we compute the `mir_const_qualif` for constants at
244 // this point, before we steal the mir-const result.
245 let _ = tcx.mir_const_qualif(def_id);
246
247 let mut body = tcx.mir_const(def_id).steal();
248 let promote_pass = promote_consts::PromoteTemps::default();
249 run_passes(
250 tcx,
251 &mut body,
252 InstanceDef::Item(def_id),
253 None,
254 MirPhase::Validated,
255 &[
256 // What we need to run borrowck etc.
257 &promote_pass,
258 &simplify::SimplifyCfg::new("qualify-consts"),
259 ],
260 );
261
262 let promoted = promote_pass.promoted_fragments.into_inner();
263 (tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
264 }
265
266 fn run_optimization_passes<'tcx>(
267 tcx: TyCtxt<'tcx>,
268 body: &mut BodyAndCache<'tcx>,
269 def_id: DefId,
270 promoted: Option<Promoted>,
271 ) {
272 run_passes(
273 tcx,
274 body,
275 InstanceDef::Item(def_id),
276 promoted,
277 MirPhase::Optimized,
278 &[
279 // Remove all things only needed by analysis
280 &no_landing_pads::NoLandingPads::new(tcx),
281 &simplify_branches::SimplifyBranches::new("initial"),
282 &remove_noop_landing_pads::RemoveNoopLandingPads,
283 &cleanup_post_borrowck::CleanupNonCodegenStatements,
284 &simplify::SimplifyCfg::new("early-opt"),
285 // These next passes must be executed together
286 &add_call_guards::CriticalCallEdges,
287 &elaborate_drops::ElaborateDrops,
288 &no_landing_pads::NoLandingPads::new(tcx),
289 // AddMovesForPackedDrops needs to run after drop
290 // elaboration.
291 &add_moves_for_packed_drops::AddMovesForPackedDrops,
292 // AddRetag needs to run after ElaborateDrops, and it needs
293 // an AllCallEdges pass right before it. Otherwise it should
294 // run fairly late, but before optimizations begin.
295 &add_call_guards::AllCallEdges,
296 &add_retag::AddRetag,
297 &simplify::SimplifyCfg::new("elaborate-drops"),
298 // No lifetime analysis based on borrowing can be done from here on out.
299
300 // From here on out, regions are gone.
301 &erase_regions::EraseRegions,
302 // Optimizations begin.
303 &unreachable_prop::UnreachablePropagation,
304 &uninhabited_enum_branching::UninhabitedEnumBranching,
305 &simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
306 &inline::Inline,
307 // Lowering generator control-flow and variables
308 // has to happen before we do anything else to them.
309 &generator::StateTransform,
310 &instcombine::InstCombine,
311 &const_prop::ConstProp,
312 &simplify_branches::SimplifyBranches::new("after-const-prop"),
313 &deaggregator::Deaggregator,
314 &copy_prop::CopyPropagation,
315 &simplify_branches::SimplifyBranches::new("after-copy-prop"),
316 &remove_noop_landing_pads::RemoveNoopLandingPads,
317 &simplify::SimplifyCfg::new("after-remove-noop-landing-pads"),
318 &simplify_try::SimplifyArmIdentity,
319 &simplify_try::SimplifyBranchSame,
320 &simplify::SimplifyCfg::new("final"),
321 &simplify::SimplifyLocals,
322 &add_call_guards::CriticalCallEdges,
323 &dump_mir::Marker("PreCodegen"),
324 ],
325 );
326 }
327
328 fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &BodyAndCache<'_> {
329 if tcx.is_constructor(def_id) {
330 // There's no reason to run all of the MIR passes on constructors when
331 // we can just output the MIR we want directly. This also saves const
332 // qualification and borrow checking the trouble of special casing
333 // constructors.
334 return shim::build_adt_ctor(tcx, def_id);
335 }
336
337 // (Mir-)Borrowck uses `mir_validated`, so we have to force it to
338 // execute before we can steal.
339 tcx.ensure().mir_borrowck(def_id);
340
341 let (body, _) = tcx.mir_validated(def_id);
342 let mut body = body.steal();
343 run_optimization_passes(tcx, &mut body, def_id, None);
344 body.ensure_predecessors();
345 tcx.arena.alloc(body)
346 }
347
348 fn promoted_mir(tcx: TyCtxt<'_>, def_id: DefId) -> &IndexVec<Promoted, BodyAndCache<'_>> {
349 if tcx.is_constructor(def_id) {
350 return tcx.intern_promoted(IndexVec::new());
351 }
352
353 tcx.ensure().mir_borrowck(def_id);
354 let (_, promoted) = tcx.mir_validated(def_id);
355 let mut promoted = promoted.steal();
356
357 for (p, mut body) in promoted.iter_enumerated_mut() {
358 run_optimization_passes(tcx, &mut body, def_id, Some(p));
359 body.ensure_predecessors();
360 }
361
362 tcx.intern_promoted(promoted)
363 }