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};
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;
15 pub mod add_call_guards;
16 pub mod add_moves_for_packed_drops;
19 pub mod check_unsafety;
20 pub mod cleanup_post_borrowck;
25 pub mod elaborate_drops;
26 pub mod erase_regions;
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;
36 pub mod simplify_branches;
38 pub mod uninhabited_enum_branching;
39 pub mod unreachable_prop;
41 pub(crate) fn provide(providers: &mut Providers<'_>) {
42 self::check_unsafety::provide(providers);
43 *providers = Providers {
55 fn is_mir_available(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
56 tcx.mir_keys(def_id.krate).contains(&def_id)
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);
64 let mut set = DefIdSet::default();
66 // All body-owners have MIR associated with them.
67 set.extend(tcx.body_owners());
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> {
73 set: &'a mut DefIdSet,
75 impl<'a, 'tcx> Visitor<'tcx> for GatherCtors<'a, 'tcx> {
76 fn visit_variant_data(
78 v: &'tcx hir::VariantData<'tcx>,
80 _: &'tcx hir::Generics<'tcx>,
84 if let hir::VariantData::Tuple(_, hir_id) = *v {
85 self.set.insert(self.tcx.hir().local_def_id(hir_id));
87 intravisit::walk_struct_def(self, v)
90 fn nested_visit_map<'b>(&'b mut self) -> NestedVisitorMap<'b, Self::Map> {
91 NestedVisitorMap::None
96 .visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor());
101 /// Where a specific `mir::Body` comes from.
102 #[derive(Debug, Copy, Clone)]
103 pub struct MirSource<'tcx> {
104 pub instance: InstanceDef<'tcx>,
106 /// If `Some`, this is a promoted rvalue within the parent function.
107 pub promoted: Option<Promoted>,
110 impl<'tcx> MirSource<'tcx> {
111 pub fn item(def_id: DefId) -> Self {
112 MirSource { instance: InstanceDef::Item(def_id), promoted: None }
116 pub fn def_id(&self) -> DefId {
117 self.instance.def_id()
121 /// Generates a default name for the pass based on the name of the
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) }
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>()
136 fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>);
141 body: &mut BodyAndCache<'tcx>,
142 instance: InstanceDef<'tcx>,
143 promoted: Option<Promoted>,
145 passes: &[&dyn MirPass<'tcx>],
147 let phase_index = mir_phase.phase_index();
149 if body.phase >= mir_phase {
153 let source = MirSource { instance, promoted };
155 let mut run_pass = |pass: &dyn MirPass<'tcx>| {
156 let run_hooks = |body: &_, index, is_after| {
157 dump_mir::on_mir_pass(
159 &format_args!("{:03}-{:03}", phase_index, index),
166 run_hooks(body, index, false);
167 pass.run_pass(tcx, source, body);
168 run_hooks(body, index, true);
177 body.phase = mir_phase;
180 fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs {
181 let const_kind = check_consts::ConstKind::for_item(tcx, def_id);
183 // No need to const-check a non-const `fn`.
184 if const_kind.is_none() {
185 return Default::default();
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();
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();
199 let item = check_consts::Item {
200 body: body.unwrap_read_only(),
204 param_env: tcx.param_env(def_id),
207 let mut validator = check_consts::validation::Validator::new(&item);
208 validator.check_body();
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()
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);
219 let mut body = tcx.mir_built(def_id).steal();
221 util::dump_mir(tcx, None, "mir_map", &0, MirSource::item(def_id), &body, |_, _| Ok(()));
226 InstanceDef::Item(def_id),
230 // What we need to do constant evaluation.
231 &simplify::SimplifyCfg::new("initial"),
232 &rustc_peek::SanityCheck,
235 body.ensure_predecessors();
236 tcx.alloc_steal_mir(body)
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);
247 let mut body = tcx.mir_const(def_id).steal();
248 let promote_pass = promote_consts::PromoteTemps::default();
252 InstanceDef::Item(def_id),
256 // What we need to run borrowck etc.
258 &simplify::SimplifyCfg::new("qualify-consts"),
262 let promoted = promote_pass.promoted_fragments.into_inner();
263 (tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
266 fn run_optimization_passes<'tcx>(
268 body: &mut BodyAndCache<'tcx>,
270 promoted: Option<Promoted>,
275 InstanceDef::Item(def_id),
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
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.
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"),
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 ©_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"),
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
334 return shim::build_adt_ctor(tcx, def_id);
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);
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)
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());
353 tcx.ensure().mir_borrowck(def_id);
354 let (_, promoted) = tcx.mir_validated(def_id);
355 let mut promoted = promoted.steal();
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();
362 tcx.intern_promoted(promoted)