1 use crate::{shim, util};
2 use required_consts::RequiredConstsVisitor;
3 use rustc_data_structures::fx::FxHashSet;
5 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
6 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
7 use rustc_index::vec::IndexVec;
8 use rustc_middle::mir::visit::Visitor as _;
9 use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPhase, Promoted};
10 use rustc_middle::ty::query::Providers;
11 use rustc_middle::ty::steal::Steal;
12 use rustc_middle::ty::{InstanceDef, TyCtxt, TypeFoldable};
13 use rustc_span::{Span, Symbol};
16 pub mod add_call_guards;
17 pub mod add_moves_for_packed_drops;
20 pub mod check_packed_ref;
21 pub mod check_unsafety;
22 pub mod cleanup_post_borrowck;
27 pub mod elaborate_drops;
31 pub mod instrument_coverage;
32 pub mod no_landing_pads;
34 pub mod promote_consts;
35 pub mod qualify_min_const_fn;
36 pub mod remove_noop_landing_pads;
37 pub mod required_consts;
40 pub mod simplify_branches;
42 pub mod uninhabited_enum_branching;
43 pub mod unreachable_prop;
46 pub(crate) fn provide(providers: &mut Providers<'_>) {
47 self::check_unsafety::provide(providers);
48 *providers = Providers {
53 mir_drops_elaborated_and_const_checked,
61 fn is_mir_available(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
62 tcx.mir_keys(def_id.krate).contains(&def_id.expect_local())
65 /// Finds the full set of `DefId`s within the current crate that have
66 /// MIR associated with them.
67 fn mir_keys(tcx: TyCtxt<'_>, krate: CrateNum) -> FxHashSet<LocalDefId> {
68 assert_eq!(krate, LOCAL_CRATE);
70 let mut set = FxHashSet::default();
72 // All body-owners have MIR associated with them.
73 set.extend(tcx.body_owners());
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> {
79 set: &'a mut FxHashSet<LocalDefId>,
81 impl<'a, 'tcx> Visitor<'tcx> for GatherCtors<'a, 'tcx> {
82 fn visit_variant_data(
84 v: &'tcx hir::VariantData<'tcx>,
86 _: &'tcx hir::Generics<'tcx>,
90 if let hir::VariantData::Tuple(_, hir_id) = *v {
91 self.set.insert(self.tcx.hir().local_def_id(hir_id));
93 intravisit::walk_struct_def(self, v)
95 type Map = intravisit::ErasedMap<'tcx>;
96 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
97 NestedVisitorMap::None
102 .visit_all_item_likes(&mut GatherCtors { tcx, set: &mut set }.as_deep_visitor());
107 /// Where a specific `mir::Body` comes from.
108 #[derive(Debug, Copy, Clone)]
109 pub struct MirSource<'tcx> {
110 pub instance: InstanceDef<'tcx>,
112 /// If `Some`, this is a promoted rvalue within the parent function.
113 pub promoted: Option<Promoted>,
116 impl<'tcx> MirSource<'tcx> {
117 pub fn item(def_id: DefId) -> Self {
118 MirSource { instance: InstanceDef::Item(def_id), promoted: None }
122 pub fn def_id(&self) -> DefId {
123 self.instance.def_id()
127 /// Generates a default name for the pass based on the name of the
129 pub fn default_name<T: ?Sized>() -> Cow<'static, str> {
130 let name = ::std::any::type_name::<T>();
131 if let Some(tail) = name.rfind(':') { Cow::from(&name[tail + 1..]) } else { Cow::from(name) }
134 /// A streamlined trait that you can implement to create a pass; the
135 /// pass will be named after the type, and it will consist of a main
136 /// loop that goes over each available MIR and applies `run_pass`.
137 pub trait MirPass<'tcx> {
138 fn name(&self) -> Cow<'_, str> {
139 default_name::<Self>()
142 fn run_pass(&self, tcx: TyCtxt<'tcx>, source: MirSource<'tcx>, body: &mut Body<'tcx>);
147 body: &mut Body<'tcx>,
148 instance: InstanceDef<'tcx>,
149 promoted: Option<Promoted>,
151 passes: &[&[&dyn MirPass<'tcx>]],
153 let phase_index = mir_phase.phase_index();
154 let source = MirSource { instance, promoted };
155 let validate = tcx.sess.opts.debugging_opts.validate_mir;
157 if body.phase >= mir_phase {
162 validate::Validator { when: format!("input to phase {:?}", mir_phase) }
163 .run_pass(tcx, source, body);
167 let mut run_pass = |pass: &dyn MirPass<'tcx>| {
168 let run_hooks = |body: &_, index, is_after| {
169 dump_mir::on_mir_pass(
171 &format_args!("{:03}-{:03}", phase_index, index),
178 run_hooks(body, index, false);
179 pass.run_pass(tcx, source, body);
180 run_hooks(body, index, true);
183 validate::Validator { when: format!("after {} in phase {:?}", pass.name(), mir_phase) }
184 .run_pass(tcx, source, body);
190 for pass_group in passes {
191 for pass in *pass_group {
196 body.phase = mir_phase;
198 if mir_phase == MirPhase::Optimized {
199 validate::Validator { when: format!("end of phase {:?}", mir_phase) }
200 .run_pass(tcx, source, body);
204 fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs {
205 let const_kind = tcx.hir().body_const_context(def_id.expect_local());
207 // No need to const-check a non-const `fn`.
208 if const_kind.is_none() {
209 return Default::default();
212 // N.B., this `borrow()` is guaranteed to be valid (i.e., the value
213 // cannot yet be stolen), because `mir_validated()`, which steals
214 // from `mir_const(), forces this query to execute before
215 // performing the steal.
216 let body = &tcx.mir_const(def_id).borrow();
218 if body.return_ty().references_error() {
219 tcx.sess.delay_span_bug(body.span, "mir_const_qualif: MIR had errors");
220 return Default::default();
224 check_consts::ConstCx { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) };
226 let mut validator = check_consts::validation::Validator::new(&ccx);
227 validator.check_body();
229 // We return the qualifs in the return place for every MIR body, even though it is only used
230 // when deciding to promote a reference to a `const` for now.
231 validator.qualifs_in_return_place()
234 /// Make MIR ready for const evaluation. This is run on all MIR, not just on consts!
235 fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> Steal<Body<'_>> {
236 let def_id = def_id.expect_local();
238 // Unsafety check uses the raw mir, so make sure it is run.
239 let _ = tcx.unsafety_check_result(def_id);
241 let mut body = tcx.mir_built(def_id).steal();
243 util::dump_mir(tcx, None, "mir_map", &0, MirSource::item(def_id.to_def_id()), &body, |_, _| {
250 InstanceDef::Item(def_id.to_def_id()),
255 &check_packed_ref::CheckPackedRef,
256 // What we need to do constant evaluation.
257 &simplify::SimplifyCfg::new("initial"),
258 &rustc_peek::SanityCheck,
261 tcx.alloc_steal_mir(body)
267 ) -> (Steal<Body<'tcx>>, Steal<IndexVec<Promoted, Body<'tcx>>>) {
268 // Ensure that we compute the `mir_const_qualif` for constants at
269 // this point, before we steal the mir-const result.
270 let _ = tcx.mir_const_qualif(def_id.to_def_id());
272 let mut body = tcx.mir_const(def_id.to_def_id()).steal();
274 let mut required_consts = Vec::new();
275 let mut required_consts_visitor = RequiredConstsVisitor::new(&mut required_consts);
276 for (bb, bb_data) in traversal::reverse_postorder(&body) {
277 required_consts_visitor.visit_basic_block_data(bb, bb_data);
279 body.required_consts = required_consts;
281 let promote_pass = promote_consts::PromoteTemps::default();
285 InstanceDef::Item(def_id.to_def_id()),
289 // What we need to run borrowck etc.
291 // FIXME(richkadel): is this the best place for the InstrumentCoverage pass?
292 &instrument_coverage::InstrumentCoverage,
293 &simplify::SimplifyCfg::new("qualify-consts"),
297 let promoted = promote_pass.promoted_fragments.into_inner();
298 (tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
301 fn mir_drops_elaborated_and_const_checked<'tcx>(
304 ) -> Steal<Body<'tcx>> {
305 // (Mir-)Borrowck uses `mir_validated`, so we have to force it to
306 // execute before we can steal.
307 tcx.ensure().mir_borrowck(def_id);
309 let (body, _) = tcx.mir_validated(def_id);
310 let mut body = body.steal();
312 run_post_borrowck_cleanup_passes(tcx, &mut body, def_id, None);
313 check_consts::post_drop_elaboration::check_live_drops(tcx, def_id, &body);
314 tcx.alloc_steal_mir(body)
317 /// After this series of passes, no lifetime analysis based on borrowing can be done.
318 fn run_post_borrowck_cleanup_passes<'tcx>(
320 body: &mut Body<'tcx>,
322 promoted: Option<Promoted>,
324 debug!("post_borrowck_cleanup({:?})", def_id);
326 let post_borrowck_cleanup: &[&dyn MirPass<'tcx>] = &[
327 // Remove all things only needed by analysis
328 &no_landing_pads::NoLandingPads::new(tcx),
329 &simplify_branches::SimplifyBranches::new("initial"),
330 &remove_noop_landing_pads::RemoveNoopLandingPads,
331 &cleanup_post_borrowck::CleanupNonCodegenStatements,
332 &simplify::SimplifyCfg::new("early-opt"),
333 // These next passes must be executed together
334 &add_call_guards::CriticalCallEdges,
335 &elaborate_drops::ElaborateDrops,
336 &no_landing_pads::NoLandingPads::new(tcx),
337 // AddMovesForPackedDrops needs to run after drop
339 &add_moves_for_packed_drops::AddMovesForPackedDrops,
340 // `AddRetag` needs to run after `ElaborateDrops`. Otherwise it should run fairly late,
341 // but before optimizations begin.
342 &add_retag::AddRetag,
343 &simplify::SimplifyCfg::new("elaborate-drops"),
349 InstanceDef::Item(def_id.to_def_id()),
352 &[post_borrowck_cleanup],
356 fn run_optimization_passes<'tcx>(
358 body: &mut Body<'tcx>,
360 promoted: Option<Promoted>,
362 let optimizations: &[&dyn MirPass<'tcx>] = &[
363 &unreachable_prop::UnreachablePropagation,
364 &uninhabited_enum_branching::UninhabitedEnumBranching,
365 &simplify::SimplifyCfg::new("after-uninhabited-enum-branching"),
367 // Lowering generator control-flow and variables has to happen before we do anything else
368 // to them. We do this inside the "optimizations" block so that it can benefit from
369 // optimizations that run before, that might be harder to do on the state machine than MIR
370 // with async primitives.
371 &generator::StateTransform,
372 &instcombine::InstCombine,
373 &const_prop::ConstProp,
374 &simplify_branches::SimplifyBranches::new("after-const-prop"),
375 // Run deaggregation here because:
376 // 1. Some codegen backends require it
377 // 2. It creates additional possibilities for some MIR optimizations to trigger
378 // FIXME(#70073): Why is this done here and not in `post_borrowck_cleanup`?
379 &deaggregator::Deaggregator,
380 &simplify_try::SimplifyArmIdentity,
381 &simplify_try::SimplifyBranchSame,
382 ©_prop::CopyPropagation,
383 &simplify_branches::SimplifyBranches::new("after-copy-prop"),
384 &remove_noop_landing_pads::RemoveNoopLandingPads,
385 &simplify::SimplifyCfg::new("after-remove-noop-landing-pads"),
386 &simplify::SimplifyCfg::new("final"),
387 &nrvo::RenameReturnPlace,
388 &simplify::SimplifyLocals,
391 let no_optimizations: &[&dyn MirPass<'tcx>] = &[
392 // Even if we don't do optimizations, we still have to lower generators for codegen.
393 &generator::StateTransform,
394 // FIXME(#70073): This pass is responsible for both optimization as well as some lints.
395 &const_prop::ConstProp,
396 // Even if we don't do optimizations, still run deaggregation because some backends assume
397 // that deaggregation always occurs.
398 &deaggregator::Deaggregator,
401 let pre_codegen_cleanup: &[&dyn MirPass<'tcx>] = &[
402 &add_call_guards::CriticalCallEdges,
403 // Dump the end result for testing and debugging purposes.
404 &dump_mir::Marker("PreCodegen"),
407 let mir_opt_level = tcx.sess.opts.debugging_opts.mir_opt_level;
413 InstanceDef::Item(def_id.to_def_id()),
417 if mir_opt_level > 0 { optimizations } else { no_optimizations },
423 fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
424 if tcx.is_constructor(def_id) {
425 // There's no reason to run all of the MIR passes on constructors when
426 // we can just output the MIR we want directly. This also saves const
427 // qualification and borrow checking the trouble of special casing
429 return shim::build_adt_ctor(tcx, def_id);
432 let def_id = def_id.expect_local();
434 let mut body = tcx.mir_drops_elaborated_and_const_checked(def_id).steal();
435 run_optimization_passes(tcx, &mut body, def_id, None);
437 debug_assert!(!body.has_free_regions(), "Free regions in optimized MIR");
442 fn promoted_mir(tcx: TyCtxt<'_>, def_id: DefId) -> IndexVec<Promoted, Body<'_>> {
443 if tcx.is_constructor(def_id) {
444 return IndexVec::new();
447 let def_id = def_id.expect_local();
449 tcx.ensure().mir_borrowck(def_id);
450 let (_, promoted) = tcx.mir_validated(def_id);
451 let mut promoted = promoted.steal();
453 for (p, mut body) in promoted.iter_enumerated_mut() {
454 run_post_borrowck_cleanup_passes(tcx, &mut body, def_id, Some(p));
455 run_optimization_passes(tcx, &mut body, def_id, Some(p));
458 debug_assert!(!promoted.has_free_regions(), "Free regions in promoted MIR");