1#![feature(array_windows)]
3#![feature(assert_matches)]
4#![feature(box_patterns)]
5#![feature(const_type_name)]
6#![feature(cow_is_borrowed)]
7#![feature(file_buffered)]
8#![feature(gen_blocks)]
9#![feature(if_let_guard)]
10#![feature(impl_trait_in_assoc_type)]
11#![feature(try_blocks)]
12#![feature(yeet_expr)]
13use hir::ConstContext;
16use required_consts::RequiredConstsVisitor;
17use rustc_const_eval::check_consts::{self, ConstCx};
18use rustc_const_eval::util;
19use rustc_data_structures::fx::FxIndexSet;
20use rustc_data_structures::steal::Steal;
21use rustc_hir as hir;
22use rustc_hir::def::{CtorKind, DefKind};
23use rustc_hir::def_id::LocalDefId;
24use rustc_index::IndexVec;
25use rustc_middle::mir::{
26 AnalysisPhase, Body, CallSource, ClearCrossCrate, ConstOperand, ConstQualifs, LocalDecl,
27 MirPhase, Operand, Place, ProjectionElem, Promoted, RuntimePhase, Rvalue, START_BLOCK,
28 SourceInfo, Statement, StatementKind, TerminatorKind,
29};
30use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
31use rustc_middle::util::Providers;
32use rustc_middle::{bug, query, span_bug};
33use rustc_mir_build::builder::build_mir;
34use rustc_span::source_map::Spanned;
35use rustc_span::{DUMMY_SP, sym};
36use tracing::debug;
37
38#[macro_use]
39mod pass_manager;
40
41use std::sync::LazyLock;
42
43use pass_manager::{self as pm, Lint, MirLint, MirPass, WithMinOptLevel};
44
45mod check_pointers;
46mod cost_checker;
47mod cross_crate_inline;
48mod deduce_param_attrs;
49mod elaborate_drop;
50mod errors;
51mod ffi_unwind_calls;
52mod lint;
53mod lint_tail_expr_drop_order;
54mod liveness;
55mod patch;
56mod shim;
57mod ssa;
58
59macro_rules! declare_passes {
82 (
83 $(
84 $vis:vis mod $mod_name:ident : $($pass_name:ident $( { $($ident:ident),* } )?),+ $(,)?;
85 )*
86 ) => {
87 $(
88 $vis mod $mod_name;
89 $(
90 #[allow(unused_imports)]
92 use $mod_name::$pass_name as _;
93 )+
94 )*
95
96 static PASS_NAMES: LazyLock<FxIndexSet<&str>> = LazyLock::new(|| [
97 "PreCodegen",
99 $(
100 $(
101 stringify!($pass_name),
102 $(
103 $(
104 $mod_name::$pass_name::$ident.name(),
105 )*
106 )?
107 )+
108 )*
109 ].into_iter().collect());
110 };
111}
112
113declare_passes! {
114 mod abort_unwinding_calls : AbortUnwindingCalls;
115 mod add_call_guards : AddCallGuards { AllCallEdges, CriticalCallEdges };
116 mod add_moves_for_packed_drops : AddMovesForPackedDrops;
117 mod add_retag : AddRetag;
118 mod add_subtyping_projections : Subtyper;
119 mod check_inline : CheckForceInline;
120 mod check_call_recursion : CheckCallRecursion, CheckDropRecursion;
121 mod check_inline_always_target_features: CheckInlineAlwaysTargetFeature;
122 mod check_alignment : CheckAlignment;
123 mod check_enums : CheckEnums;
124 mod check_const_item_mutation : CheckConstItemMutation;
125 mod check_null : CheckNull;
126 mod check_packed_ref : CheckPackedRef;
127 pub mod cleanup_post_borrowck : CleanupPostBorrowck;
129
130 mod copy_prop : CopyProp;
131 mod coroutine : StateTransform;
132 mod coverage : InstrumentCoverage;
133 mod ctfe_limit : CtfeLimit;
134 mod dataflow_const_prop : DataflowConstProp;
135 mod dead_store_elimination : DeadStoreElimination {
136 Initial,
137 Final
138 };
139 mod deref_separator : Derefer;
140 mod dest_prop : DestinationPropagation;
141 pub mod dump_mir : Marker;
142 mod early_otherwise_branch : EarlyOtherwiseBranch;
143 mod erase_deref_temps : EraseDerefTemps;
144 mod elaborate_box_derefs : ElaborateBoxDerefs;
145 mod elaborate_drops : ElaborateDrops;
146 mod function_item_references : FunctionItemReferences;
147 mod gvn : GVN;
148 pub mod inline : Inline, ForceInline;
151 mod impossible_predicates : ImpossiblePredicates;
152 mod instsimplify : InstSimplify { BeforeInline, AfterSimplifyCfg };
153 mod jump_threading : JumpThreading;
154 mod known_panics_lint : KnownPanicsLint;
155 mod large_enums : EnumSizeOpt;
156 mod lower_intrinsics : LowerIntrinsics;
157 mod lower_slice_len : LowerSliceLenCalls;
158 mod match_branches : MatchBranchSimplification;
159 mod mentioned_items : MentionedItems;
160 mod multiple_return_terminators : MultipleReturnTerminators;
161 mod post_drop_elaboration : CheckLiveDrops;
162 mod prettify : ReorderBasicBlocks, ReorderLocals;
163 mod promote_consts : PromoteTemps;
164 mod ref_prop : ReferencePropagation;
165 mod remove_noop_landing_pads : RemoveNoopLandingPads;
166 mod remove_place_mention : RemovePlaceMention;
167 mod remove_storage_markers : RemoveStorageMarkers;
168 mod remove_uninit_drops : RemoveUninitDrops;
169 mod remove_unneeded_drops : RemoveUnneededDrops;
170 mod remove_zsts : RemoveZsts;
171 mod required_consts : RequiredConstsVisitor;
172 mod post_analysis_normalize : PostAnalysisNormalize;
173 mod sanity_check : SanityCheck;
174 pub mod simplify :
176 SimplifyCfg {
177 Initial,
178 PromoteConsts,
179 RemoveFalseEdges,
180 PostAnalysis,
181 PreOptimizations,
182 Final,
183 MakeShim,
184 AfterUnreachableEnumBranching
185 },
186 SimplifyLocals {
187 BeforeConstProp,
188 AfterGVN,
189 Final
190 };
191 mod simplify_branches : SimplifyConstCondition {
192 AfterInstSimplify,
193 AfterConstProp,
194 Final
195 };
196 mod simplify_comparison_integral : SimplifyComparisonIntegral;
197 mod single_use_consts : SingleUseConsts;
198 mod sroa : ScalarReplacementOfAggregates;
199 mod strip_debuginfo : StripDebugInfo;
200 mod unreachable_enum_branching : UnreachableEnumBranching;
201 mod unreachable_prop : UnreachablePropagation;
202 mod validate : Validator;
203}
204
205rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
206
207pub fn provide(providers: &mut Providers) {
208 coverage::query::provide(providers);
209 ffi_unwind_calls::provide(providers);
210 shim::provide(providers);
211 cross_crate_inline::provide(providers);
212 providers.queries = query::Providers {
213 mir_keys,
214 mir_built,
215 mir_const_qualif,
216 mir_promoted,
217 mir_drops_elaborated_and_const_checked,
218 mir_for_ctfe,
219 mir_coroutine_witnesses: coroutine::mir_coroutine_witnesses,
220 optimized_mir,
221 check_liveness: liveness::check_liveness,
222 is_mir_available,
223 is_ctfe_mir_available: is_mir_available,
224 mir_callgraph_cyclic: inline::cycle::mir_callgraph_cyclic,
225 mir_inliner_callees: inline::cycle::mir_inliner_callees,
226 promoted_mir,
227 deduced_param_attrs: deduce_param_attrs::deduced_param_attrs,
228 coroutine_by_move_body_def_id: coroutine::coroutine_by_move_body_def_id,
229 ..providers.queries
230 };
231}
232
233fn remap_mir_for_const_eval_select<'tcx>(
234 tcx: TyCtxt<'tcx>,
235 mut body: Body<'tcx>,
236 context: hir::Constness,
237) -> Body<'tcx> {
238 for bb in body.basic_blocks.as_mut().iter_mut() {
239 let terminator = bb.terminator.as_mut().expect("invalid terminator");
240 match terminator.kind {
241 TerminatorKind::Call {
242 func: Operand::Constant(box ConstOperand { ref const_, .. }),
243 ref mut args,
244 destination,
245 target,
246 unwind,
247 fn_span,
248 ..
249 } if let ty::FnDef(def_id, _) = *const_.ty().kind()
250 && tcx.is_intrinsic(def_id, sym::const_eval_select) =>
251 {
252 let Ok([tupled_args, called_in_const, called_at_rt]) = take_array(args) else {
253 unreachable!()
254 };
255 let ty = tupled_args.node.ty(&body.local_decls, tcx);
256 let fields = ty.tuple_fields();
257 let num_args = fields.len();
258 let func =
259 if context == hir::Constness::Const { called_in_const } else { called_at_rt };
260 let (method, place): (fn(Place<'tcx>) -> Operand<'tcx>, Place<'tcx>) =
261 match tupled_args.node {
262 Operand::Constant(_) => {
263 let local = body.local_decls.push(LocalDecl::new(ty, fn_span));
267 bb.statements.push(Statement::new(
268 SourceInfo::outermost(fn_span),
269 StatementKind::Assign(Box::new((
270 local.into(),
271 Rvalue::Use(tupled_args.node.clone()),
272 ))),
273 ));
274 (Operand::Move, local.into())
275 }
276 Operand::Move(place) => (Operand::Move, place),
277 Operand::Copy(place) => (Operand::Copy, place),
278 };
279 let place_elems = place.projection;
280 let arguments = (0..num_args)
281 .map(|x| {
282 let mut place_elems = place_elems.to_vec();
283 place_elems.push(ProjectionElem::Field(x.into(), fields[x]));
284 let projection = tcx.mk_place_elems(&place_elems);
285 let place = Place { local: place.local, projection };
286 Spanned { node: method(place), span: DUMMY_SP }
287 })
288 .collect();
289 terminator.kind = TerminatorKind::Call {
290 func: func.node,
291 args: arguments,
292 destination,
293 target,
294 unwind,
295 call_source: CallSource::Misc,
296 fn_span,
297 };
298 }
299 _ => {}
300 }
301 }
302 body
303}
304
305fn take_array<T, const N: usize>(b: &mut Box<[T]>) -> Result<[T; N], Box<[T]>> {
306 let b: Box<[T; N]> = std::mem::take(b).try_into()?;
307 Ok(*b)
308}
309
310fn is_mir_available(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
311 tcx.mir_keys(()).contains(&def_id)
312}
313
314fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
317 let mut set: FxIndexSet<_> = tcx.hir_body_owners().collect();
319
320 set.retain(|&def_id| !matches!(tcx.def_kind(def_id), DefKind::GlobalAsm));
323
324 for body_owner in tcx.hir_body_owners() {
327 if let DefKind::Closure = tcx.def_kind(body_owner)
328 && tcx.needs_coroutine_by_move_body_def_id(body_owner.to_def_id())
329 {
330 set.insert(tcx.coroutine_by_move_body_def_id(body_owner).expect_local());
331 }
332 }
333
334 for item in tcx.hir_crate_items(()).free_items() {
337 if let DefKind::Struct | DefKind::Enum = tcx.def_kind(item.owner_id) {
338 for variant in tcx.adt_def(item.owner_id).variants() {
339 if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
340 set.insert(ctor_def_id.expect_local());
341 }
342 }
343 }
344 }
345
346 set
347}
348
349fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
350 let body = &tcx.mir_built(def).borrow();
355 let ccx = check_consts::ConstCx::new(tcx, body);
356 match ccx.const_kind {
358 Some(ConstContext::Const { .. } | ConstContext::Static(_) | ConstContext::ConstFn) => {}
359 None => span_bug!(
360 tcx.def_span(def),
361 "`mir_const_qualif` should only be called on const fns and const items"
362 ),
363 }
364
365 if body.return_ty().references_error() {
366 tcx.dcx().span_delayed_bug(body.span, "mir_const_qualif: MIR had errors");
368 return Default::default();
369 }
370
371 let mut validator = check_consts::check::Checker::new(&ccx);
372 validator.check_body();
373
374 validator.qualifs_in_return_place()
377}
378
379fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
380 let mut body = build_mir(tcx, def);
381
382 pass_manager::dump_mir_for_phase_change(tcx, &body);
383
384 pm::run_passes(
385 tcx,
386 &mut body,
387 &[
388 &Lint(check_inline::CheckForceInline),
390 &Lint(check_call_recursion::CheckCallRecursion),
391 &Lint(check_inline_always_target_features::CheckInlineAlwaysTargetFeature),
394 &Lint(check_packed_ref::CheckPackedRef),
395 &Lint(check_const_item_mutation::CheckConstItemMutation),
396 &Lint(function_item_references::FunctionItemReferences),
397 &simplify::SimplifyCfg::Initial,
399 &Lint(sanity_check::SanityCheck),
400 ],
401 None,
402 pm::Optimizations::Allowed,
403 );
404 tcx.alloc_steal_mir(body)
405}
406
407fn mir_promoted(
409 tcx: TyCtxt<'_>,
410 def: LocalDefId,
411) -> (&Steal<Body<'_>>, &Steal<IndexVec<Promoted, Body<'_>>>) {
412 let const_qualifs = match tcx.def_kind(def) {
417 DefKind::Fn | DefKind::AssocFn | DefKind::Closure
418 if tcx.constness(def) == hir::Constness::Const
419 || tcx.is_const_default_method(def.to_def_id()) =>
420 {
421 tcx.mir_const_qualif(def)
422 }
423 DefKind::AssocConst
424 | DefKind::Const
425 | DefKind::Static { .. }
426 | DefKind::InlineConst
427 | DefKind::AnonConst => tcx.mir_const_qualif(def),
428 _ => ConstQualifs::default(),
429 };
430
431 tcx.ensure_done().has_ffi_unwind_calls(def);
433
434 if tcx.needs_coroutine_by_move_body_def_id(def.to_def_id()) {
436 tcx.ensure_done().coroutine_by_move_body_def_id(def);
437 }
438
439 let mut body = tcx.mir_built(def).steal();
440 if let Some(error_reported) = const_qualifs.tainted_by_errors {
441 body.tainted_by_errors = Some(error_reported);
442 }
443
444 RequiredConstsVisitor::compute_required_consts(&mut body);
447
448 let promote_pass = promote_consts::PromoteTemps::default();
450 pm::run_passes(
451 tcx,
452 &mut body,
453 &[&promote_pass, &simplify::SimplifyCfg::PromoteConsts, &coverage::InstrumentCoverage],
454 Some(MirPhase::Analysis(AnalysisPhase::Initial)),
455 pm::Optimizations::Allowed,
456 );
457
458 lint_tail_expr_drop_order::run_lint(tcx, def, &body);
459
460 let promoted = promote_pass.promoted_fragments.into_inner();
461 (tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
462}
463
464fn mir_for_ctfe(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &Body<'_> {
466 tcx.arena.alloc(inner_mir_for_ctfe(tcx, def_id))
467}
468
469fn inner_mir_for_ctfe(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
470 if tcx.is_constructor(def.to_def_id()) {
472 return shim::build_adt_ctor(tcx, def.to_def_id());
477 }
478
479 let body = tcx.mir_drops_elaborated_and_const_checked(def);
480 let body = match tcx.hir_body_const_context(def) {
481 Some(hir::ConstContext::Const { .. } | hir::ConstContext::Static(_)) => body.steal(),
484 Some(hir::ConstContext::ConstFn) => body.borrow().clone(),
485 None => bug!("`mir_for_ctfe` called on non-const {def:?}"),
486 };
487
488 let mut body = remap_mir_for_const_eval_select(tcx, body, hir::Constness::Const);
489 pm::run_passes(tcx, &mut body, &[&ctfe_limit::CtfeLimit], None, pm::Optimizations::Allowed);
490
491 body
492}
493
494fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
498 if tcx.is_coroutine(def.to_def_id()) {
499 tcx.ensure_done().mir_coroutine_witnesses(def);
500 }
501
502 let tainted_by_errors = if !tcx.is_synthetic_mir(def) {
504 tcx.mir_borrowck(tcx.typeck_root_def_id(def.to_def_id()).expect_local()).err()
505 } else {
506 None
507 };
508
509 let is_fn_like = tcx.def_kind(def).is_fn_like();
510 if is_fn_like {
511 if pm::should_run_pass(tcx, &inline::Inline, pm::Optimizations::Allowed)
513 || inline::ForceInline::should_run_pass_for_callee(tcx, def.to_def_id())
514 {
515 tcx.ensure_done().mir_inliner_callees(ty::InstanceKind::Item(def.to_def_id()));
516 }
517 }
518
519 tcx.ensure_done().check_liveness(def);
520
521 let (body, _) = tcx.mir_promoted(def);
522 let mut body = body.steal();
523
524 if let Some(error_reported) = tainted_by_errors {
525 body.tainted_by_errors = Some(error_reported);
526 }
527
528 let root = tcx.typeck_root_def_id(def.to_def_id());
533 match tcx.def_kind(root) {
534 DefKind::Fn
535 | DefKind::AssocFn
536 | DefKind::Static { .. }
537 | DefKind::Const
538 | DefKind::AssocConst => {
539 if let Err(guar) = tcx.ensure_ok().check_well_formed(root.expect_local()) {
540 body.tainted_by_errors = Some(guar);
541 }
542 }
543 _ => {}
544 }
545
546 run_analysis_to_runtime_passes(tcx, &mut body);
547
548 tcx.alloc_steal_mir(body)
549}
550
551pub fn run_analysis_to_runtime_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
554 assert!(body.phase == MirPhase::Analysis(AnalysisPhase::Initial));
555 let did = body.source.def_id();
556
557 debug!("analysis_mir_cleanup({:?})", did);
558 run_analysis_cleanup_passes(tcx, body);
559 assert!(body.phase == MirPhase::Analysis(AnalysisPhase::PostCleanup));
560
561 if check_consts::post_drop_elaboration::checking_enabled(&ConstCx::new(tcx, body)) {
563 pm::run_passes(
564 tcx,
565 body,
566 &[
567 &remove_uninit_drops::RemoveUninitDrops,
568 &simplify::SimplifyCfg::RemoveFalseEdges,
569 &Lint(post_drop_elaboration::CheckLiveDrops),
570 ],
571 None,
572 pm::Optimizations::Allowed,
573 );
574 }
575
576 debug!("runtime_mir_lowering({:?})", did);
577 run_runtime_lowering_passes(tcx, body);
578 assert!(body.phase == MirPhase::Runtime(RuntimePhase::Initial));
579
580 debug!("runtime_mir_cleanup({:?})", did);
581 run_runtime_cleanup_passes(tcx, body);
582 assert!(body.phase == MirPhase::Runtime(RuntimePhase::PostCleanup));
583}
584
585fn run_analysis_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
589 let passes: &[&dyn MirPass<'tcx>] = &[
590 &impossible_predicates::ImpossiblePredicates,
591 &cleanup_post_borrowck::CleanupPostBorrowck,
592 &remove_noop_landing_pads::RemoveNoopLandingPads,
593 &simplify::SimplifyCfg::PostAnalysis,
594 &deref_separator::Derefer,
595 ];
596
597 pm::run_passes(
598 tcx,
599 body,
600 passes,
601 Some(MirPhase::Analysis(AnalysisPhase::PostCleanup)),
602 pm::Optimizations::Allowed,
603 );
604}
605
606fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
608 let passes: &[&dyn MirPass<'tcx>] = &[
609 &add_call_guards::CriticalCallEdges,
611 &post_analysis_normalize::PostAnalysisNormalize,
613 &add_subtyping_projections::Subtyper,
615 &elaborate_drops::ElaborateDrops,
616 &Lint(check_call_recursion::CheckDropRecursion),
618 &abort_unwinding_calls::AbortUnwindingCalls,
622 &add_moves_for_packed_drops::AddMovesForPackedDrops,
625 &add_retag::AddRetag,
628 &erase_deref_temps::EraseDerefTemps,
629 &elaborate_box_derefs::ElaborateBoxDerefs,
630 &coroutine::StateTransform,
631 &Lint(known_panics_lint::KnownPanicsLint),
632 ];
633 pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
634}
635
636fn run_runtime_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
638 let passes: &[&dyn MirPass<'tcx>] = &[
639 &lower_intrinsics::LowerIntrinsics,
640 &remove_place_mention::RemovePlaceMention,
641 &simplify::SimplifyCfg::PreOptimizations,
642 ];
643
644 pm::run_passes(
645 tcx,
646 body,
647 passes,
648 Some(MirPhase::Runtime(RuntimePhase::PostCleanup)),
649 pm::Optimizations::Allowed,
650 );
651
652 for decl in &mut body.local_decls {
655 decl.local_info = ClearCrossCrate::Clear;
656 }
657}
658
659pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
660 fn o1<T>(x: T) -> WithMinOptLevel<T> {
661 WithMinOptLevel(1, x)
662 }
663
664 let def_id = body.source.def_id();
665 let optimizations = if tcx.def_kind(def_id).has_codegen_attrs()
666 && tcx.codegen_fn_attrs(def_id).optimize.do_not_optimize()
667 {
668 pm::Optimizations::Suppressed
669 } else {
670 pm::Optimizations::Allowed
671 };
672
673 pm::run_passes(
675 tcx,
676 body,
677 &[
678 &check_alignment::CheckAlignment,
680 &check_null::CheckNull,
681 &check_enums::CheckEnums,
682 &lower_slice_len::LowerSliceLenCalls,
687 &instsimplify::InstSimplify::BeforeInline,
690 &inline::ForceInline,
692 &inline::Inline,
694 &remove_storage_markers::RemoveStorageMarkers,
699 &remove_zsts::RemoveZsts,
701 &remove_unneeded_drops::RemoveUnneededDrops,
702 &unreachable_enum_branching::UnreachableEnumBranching,
705 &unreachable_prop::UnreachablePropagation,
706 &o1(simplify::SimplifyCfg::AfterUnreachableEnumBranching),
707 &multiple_return_terminators::MultipleReturnTerminators,
708 &instsimplify::InstSimplify::AfterSimplifyCfg,
712 &o1(simplify_branches::SimplifyConstCondition::AfterInstSimplify),
721 &ref_prop::ReferencePropagation,
722 &sroa::ScalarReplacementOfAggregates,
723 &simplify::SimplifyLocals::BeforeConstProp,
724 &dead_store_elimination::DeadStoreElimination::Initial,
725 &gvn::GVN,
726 &simplify::SimplifyLocals::AfterGVN,
727 &match_branches::MatchBranchSimplification,
728 &dataflow_const_prop::DataflowConstProp,
729 &single_use_consts::SingleUseConsts,
730 &o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
731 &jump_threading::JumpThreading,
732 &early_otherwise_branch::EarlyOtherwiseBranch,
733 &simplify_comparison_integral::SimplifyComparisonIntegral,
734 &o1(simplify_branches::SimplifyConstCondition::Final),
735 &o1(remove_noop_landing_pads::RemoveNoopLandingPads),
736 &o1(simplify::SimplifyCfg::Final),
737 &strip_debuginfo::StripDebugInfo,
739 ©_prop::CopyProp,
740 &dead_store_elimination::DeadStoreElimination::Final,
741 &dest_prop::DestinationPropagation,
742 &simplify::SimplifyLocals::Final,
743 &multiple_return_terminators::MultipleReturnTerminators,
744 &large_enums::EnumSizeOpt { discrepancy: 128 },
745 &add_call_guards::CriticalCallEdges,
747 &prettify::ReorderBasicBlocks,
749 &prettify::ReorderLocals,
750 &dump_mir::Marker("PreCodegen"),
752 ],
753 Some(MirPhase::Runtime(RuntimePhase::Optimized)),
754 optimizations,
755 );
756}
757
758fn optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> &Body<'_> {
760 tcx.arena.alloc(inner_optimized_mir(tcx, did))
761}
762
763fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
764 if tcx.is_constructor(did.to_def_id()) {
765 return shim::build_adt_ctor(tcx, did.to_def_id());
770 }
771
772 match tcx.hir_body_const_context(did) {
773 Some(hir::ConstContext::ConstFn) => tcx.ensure_done().mir_for_ctfe(did),
777 None => {}
778 Some(other) => panic!("do not use `optimized_mir` for constants: {other:?}"),
779 }
780 debug!("about to call mir_drops_elaborated...");
781 let body = tcx.mir_drops_elaborated_and_const_checked(did).steal();
782 let mut body = remap_mir_for_const_eval_select(tcx, body, hir::Constness::NotConst);
783
784 if body.tainted_by_errors.is_some() {
785 return body;
786 }
787
788 mentioned_items::MentionedItems.run_pass(tcx, &mut body);
792
793 if let TerminatorKind::Unreachable = body.basic_blocks[START_BLOCK].terminator().kind
797 && body.basic_blocks[START_BLOCK].statements.is_empty()
798 {
799 return body;
800 }
801
802 run_optimization_passes(tcx, &mut body);
803
804 body
805}
806
807fn promoted_mir(tcx: TyCtxt<'_>, def: LocalDefId) -> &IndexVec<Promoted, Body<'_>> {
810 if tcx.is_constructor(def.to_def_id()) {
811 return tcx.arena.alloc(IndexVec::new());
812 }
813
814 if !tcx.is_synthetic_mir(def) {
815 tcx.ensure_done().mir_borrowck(tcx.typeck_root_def_id(def.to_def_id()).expect_local());
816 }
817 let mut promoted = tcx.mir_promoted(def).1.steal();
818
819 for body in &mut promoted {
820 run_analysis_to_runtime_passes(tcx, body);
821 }
822
823 tcx.arena.alloc(promoted)
824}