rustc_mir_transform/
inline.rs

1//! Inlining pass for MIR functions.
2
3use std::assert_matches::debug_assert_matches;
4use std::iter;
5use std::ops::{Range, RangeFrom};
6
7use rustc_abi::{ExternAbi, FieldIdx};
8use rustc_attr_parsing::{InlineAttr, OptimizeAttr};
9use rustc_hir::def::DefKind;
10use rustc_hir::def_id::DefId;
11use rustc_index::Idx;
12use rustc_index::bit_set::DenseBitSet;
13use rustc_middle::bug;
14use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
15use rustc_middle::mir::visit::*;
16use rustc_middle::mir::*;
17use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
18use rustc_session::config::{DebugInfo, OptLevel};
19use rustc_span::source_map::Spanned;
20use tracing::{debug, instrument, trace, trace_span};
21
22use crate::cost_checker::{CostChecker, is_call_like};
23use crate::deref_separator::deref_finder;
24use crate::simplify::simplify_cfg;
25use crate::validate::validate_types;
26use crate::{check_inline, util};
27
28pub(crate) mod cycle;
29
30const HISTORY_DEPTH_LIMIT: usize = 20;
31const TOP_DOWN_DEPTH_LIMIT: usize = 5;
32
33#[derive(Clone, Debug)]
34struct CallSite<'tcx> {
35    callee: Instance<'tcx>,
36    fn_sig: ty::PolyFnSig<'tcx>,
37    block: BasicBlock,
38    source_info: SourceInfo,
39}
40
41// Made public so that `mir_drops_elaborated_and_const_checked` can be overridden
42// by custom rustc drivers, running all the steps by themselves. See #114628.
43pub struct Inline;
44
45impl<'tcx> crate::MirPass<'tcx> for Inline {
46    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
47        if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
48            return enabled;
49        }
50
51        match sess.mir_opt_level() {
52            0 | 1 => false,
53            2 => {
54                (sess.opts.optimize == OptLevel::More || sess.opts.optimize == OptLevel::Aggressive)
55                    && sess.opts.incremental == None
56            }
57            _ => true,
58        }
59    }
60
61    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
62        let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
63        let _guard = span.enter();
64        if inline::<NormalInliner<'tcx>>(tcx, body) {
65            debug!("running simplify cfg on {:?}", body.source);
66            simplify_cfg(body);
67            deref_finder(tcx, body);
68        }
69    }
70
71    fn is_required(&self) -> bool {
72        false
73    }
74}
75
76pub struct ForceInline;
77
78impl ForceInline {
79    pub fn should_run_pass_for_callee<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
80        matches!(tcx.codegen_fn_attrs(def_id).inline, InlineAttr::Force { .. })
81    }
82}
83
84impl<'tcx> crate::MirPass<'tcx> for ForceInline {
85    fn is_enabled(&self, _: &rustc_session::Session) -> bool {
86        true
87    }
88
89    fn can_be_overridden(&self) -> bool {
90        false
91    }
92
93    fn is_required(&self) -> bool {
94        true
95    }
96
97    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
98        let span = trace_span!("force_inline", body = %tcx.def_path_str(body.source.def_id()));
99        let _guard = span.enter();
100        if inline::<ForceInliner<'tcx>>(tcx, body) {
101            debug!("running simplify cfg on {:?}", body.source);
102            simplify_cfg(body);
103            deref_finder(tcx, body);
104        }
105    }
106}
107
108trait Inliner<'tcx> {
109    fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self;
110
111    fn tcx(&self) -> TyCtxt<'tcx>;
112    fn typing_env(&self) -> ty::TypingEnv<'tcx>;
113    fn history(&self) -> &[DefId];
114    fn caller_def_id(&self) -> DefId;
115
116    /// Has the caller body been changed?
117    fn changed(self) -> bool;
118
119    /// Should inlining happen for a given callee?
120    fn should_inline_for_callee(&self, def_id: DefId) -> bool;
121
122    fn check_codegen_attributes_extra(
123        &self,
124        callee_attrs: &CodegenFnAttrs,
125    ) -> Result<(), &'static str>;
126
127    fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool;
128
129    /// Returns inlining decision that is based on the examination of callee MIR body.
130    /// Assumes that codegen attributes have been checked for compatibility already.
131    fn check_callee_mir_body(
132        &self,
133        callsite: &CallSite<'tcx>,
134        callee_body: &Body<'tcx>,
135        callee_attrs: &CodegenFnAttrs,
136    ) -> Result<(), &'static str>;
137
138    /// Called when inlining succeeds.
139    fn on_inline_success(
140        &mut self,
141        callsite: &CallSite<'tcx>,
142        caller_body: &mut Body<'tcx>,
143        new_blocks: std::ops::Range<BasicBlock>,
144    );
145
146    /// Called when inlining failed or was not performed.
147    fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str);
148}
149
150struct ForceInliner<'tcx> {
151    tcx: TyCtxt<'tcx>,
152    typing_env: ty::TypingEnv<'tcx>,
153    /// `DefId` of caller.
154    def_id: DefId,
155    /// Stack of inlined instances.
156    /// We only check the `DefId` and not the args because we want to
157    /// avoid inlining cases of polymorphic recursion.
158    /// The number of `DefId`s is finite, so checking history is enough
159    /// to ensure that we do not loop endlessly while inlining.
160    history: Vec<DefId>,
161    /// Indicates that the caller body has been modified.
162    changed: bool,
163}
164
165impl<'tcx> Inliner<'tcx> for ForceInliner<'tcx> {
166    fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
167        Self { tcx, typing_env: body.typing_env(tcx), def_id, history: Vec::new(), changed: false }
168    }
169
170    fn tcx(&self) -> TyCtxt<'tcx> {
171        self.tcx
172    }
173
174    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
175        self.typing_env
176    }
177
178    fn history(&self) -> &[DefId] {
179        &self.history
180    }
181
182    fn caller_def_id(&self) -> DefId {
183        self.def_id
184    }
185
186    fn changed(self) -> bool {
187        self.changed
188    }
189
190    fn should_inline_for_callee(&self, def_id: DefId) -> bool {
191        ForceInline::should_run_pass_for_callee(self.tcx(), def_id)
192    }
193
194    fn check_codegen_attributes_extra(
195        &self,
196        callee_attrs: &CodegenFnAttrs,
197    ) -> Result<(), &'static str> {
198        debug_assert_matches!(callee_attrs.inline, InlineAttr::Force { .. });
199        Ok(())
200    }
201
202    fn check_caller_mir_body(&self, _: &Body<'tcx>) -> bool {
203        true
204    }
205
206    #[instrument(level = "debug", skip(self, callee_body))]
207    fn check_callee_mir_body(
208        &self,
209        _: &CallSite<'tcx>,
210        callee_body: &Body<'tcx>,
211        callee_attrs: &CodegenFnAttrs,
212    ) -> Result<(), &'static str> {
213        if callee_body.tainted_by_errors.is_some() {
214            return Err("body has errors");
215        }
216
217        let caller_attrs = self.tcx().codegen_fn_attrs(self.caller_def_id());
218        if callee_attrs.instruction_set != caller_attrs.instruction_set
219            && callee_body
220                .basic_blocks
221                .iter()
222                .any(|bb| matches!(bb.terminator().kind, TerminatorKind::InlineAsm { .. }))
223        {
224            // During the attribute checking stage we allow a callee with no
225            // instruction_set assigned to count as compatible with a function that does
226            // assign one. However, during this stage we require an exact match when any
227            // inline-asm is detected. LLVM will still possibly do an inline later on
228            // if the no-attribute function ends up with the same instruction set anyway.
229            Err("cannot move inline-asm across instruction sets")
230        } else {
231            Ok(())
232        }
233    }
234
235    fn on_inline_success(
236        &mut self,
237        callsite: &CallSite<'tcx>,
238        caller_body: &mut Body<'tcx>,
239        new_blocks: std::ops::Range<BasicBlock>,
240    ) {
241        self.changed = true;
242
243        self.history.push(callsite.callee.def_id());
244        process_blocks(self, caller_body, new_blocks);
245        self.history.pop();
246    }
247
248    fn on_inline_failure(&self, callsite: &CallSite<'tcx>, reason: &'static str) {
249        let tcx = self.tcx();
250        let InlineAttr::Force { attr_span, reason: justification } =
251            tcx.codegen_fn_attrs(callsite.callee.def_id()).inline
252        else {
253            bug!("called on item without required inlining");
254        };
255
256        let call_span = callsite.source_info.span;
257        tcx.dcx().emit_err(crate::errors::ForceInlineFailure {
258            call_span,
259            attr_span,
260            caller_span: tcx.def_span(self.def_id),
261            caller: tcx.def_path_str(self.def_id),
262            callee_span: tcx.def_span(callsite.callee.def_id()),
263            callee: tcx.def_path_str(callsite.callee.def_id()),
264            reason,
265            justification: justification.map(|sym| crate::errors::ForceInlineJustification { sym }),
266        });
267    }
268}
269
270struct NormalInliner<'tcx> {
271    tcx: TyCtxt<'tcx>,
272    typing_env: ty::TypingEnv<'tcx>,
273    /// `DefId` of caller.
274    def_id: DefId,
275    /// Stack of inlined instances.
276    /// We only check the `DefId` and not the args because we want to
277    /// avoid inlining cases of polymorphic recursion.
278    /// The number of `DefId`s is finite, so checking history is enough
279    /// to ensure that we do not loop endlessly while inlining.
280    history: Vec<DefId>,
281    /// How many (multi-call) callsites have we inlined for the top-level call?
282    ///
283    /// We need to limit this in order to prevent super-linear growth in MIR size.
284    top_down_counter: usize,
285    /// Indicates that the caller body has been modified.
286    changed: bool,
287    /// Indicates that the caller is #[inline] and just calls another function,
288    /// and thus we can inline less into it as it'll be inlined itself.
289    caller_is_inline_forwarder: bool,
290}
291
292impl<'tcx> NormalInliner<'tcx> {
293    fn past_depth_limit(&self) -> bool {
294        self.history.len() > HISTORY_DEPTH_LIMIT || self.top_down_counter > TOP_DOWN_DEPTH_LIMIT
295    }
296}
297
298impl<'tcx> Inliner<'tcx> for NormalInliner<'tcx> {
299    fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> Self {
300        let typing_env = body.typing_env(tcx);
301        let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
302
303        Self {
304            tcx,
305            typing_env,
306            def_id,
307            history: Vec::new(),
308            top_down_counter: 0,
309            changed: false,
310            caller_is_inline_forwarder: matches!(
311                codegen_fn_attrs.inline,
312                InlineAttr::Hint | InlineAttr::Always | InlineAttr::Force { .. }
313            ) && body_is_forwarder(body),
314        }
315    }
316
317    fn tcx(&self) -> TyCtxt<'tcx> {
318        self.tcx
319    }
320
321    fn caller_def_id(&self) -> DefId {
322        self.def_id
323    }
324
325    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
326        self.typing_env
327    }
328
329    fn history(&self) -> &[DefId] {
330        &self.history
331    }
332
333    fn changed(self) -> bool {
334        self.changed
335    }
336
337    fn should_inline_for_callee(&self, _: DefId) -> bool {
338        true
339    }
340
341    fn check_codegen_attributes_extra(
342        &self,
343        callee_attrs: &CodegenFnAttrs,
344    ) -> Result<(), &'static str> {
345        if self.past_depth_limit() && matches!(callee_attrs.inline, InlineAttr::None) {
346            Err("Past depth limit so not inspecting unmarked callee")
347        } else {
348            Ok(())
349        }
350    }
351
352    fn check_caller_mir_body(&self, body: &Body<'tcx>) -> bool {
353        // Avoid inlining into coroutines, since their `optimized_mir` is used for layout computation,
354        // which can create a cycle, even when no attempt is made to inline the function in the other
355        // direction.
356        if body.coroutine.is_some() {
357            return false;
358        }
359
360        true
361    }
362
363    #[instrument(level = "debug", skip(self, callee_body))]
364    fn check_callee_mir_body(
365        &self,
366        callsite: &CallSite<'tcx>,
367        callee_body: &Body<'tcx>,
368        callee_attrs: &CodegenFnAttrs,
369    ) -> Result<(), &'static str> {
370        let tcx = self.tcx();
371
372        if let Some(_) = callee_body.tainted_by_errors {
373            return Err("body has errors");
374        }
375
376        if self.past_depth_limit() && callee_body.basic_blocks.len() > 1 {
377            return Err("Not inlining multi-block body as we're past a depth limit");
378        }
379
380        let mut threshold = if self.caller_is_inline_forwarder || self.past_depth_limit() {
381            tcx.sess.opts.unstable_opts.inline_mir_forwarder_threshold.unwrap_or(30)
382        } else if tcx.cross_crate_inlinable(callsite.callee.def_id()) {
383            tcx.sess.opts.unstable_opts.inline_mir_hint_threshold.unwrap_or(100)
384        } else {
385            tcx.sess.opts.unstable_opts.inline_mir_threshold.unwrap_or(50)
386        };
387
388        // Give a bonus functions with a small number of blocks,
389        // We normally have two or three blocks for even
390        // very small functions.
391        if callee_body.basic_blocks.len() <= 3 {
392            threshold += threshold / 4;
393        }
394        debug!("    final inline threshold = {}", threshold);
395
396        // FIXME: Give a bonus to functions with only a single caller
397
398        let mut checker =
399            CostChecker::new(tcx, self.typing_env(), Some(callsite.callee), callee_body);
400
401        checker.add_function_level_costs();
402
403        // Traverse the MIR manually so we can account for the effects of inlining on the CFG.
404        let mut work_list = vec![START_BLOCK];
405        let mut visited = DenseBitSet::new_empty(callee_body.basic_blocks.len());
406        while let Some(bb) = work_list.pop() {
407            if !visited.insert(bb.index()) {
408                continue;
409            }
410
411            let blk = &callee_body.basic_blocks[bb];
412            checker.visit_basic_block_data(bb, blk);
413
414            let term = blk.terminator();
415            let caller_attrs = tcx.codegen_fn_attrs(self.caller_def_id());
416            if let TerminatorKind::Drop { ref place, target, unwind, replace: _ } = term.kind {
417                work_list.push(target);
418
419                // If the place doesn't actually need dropping, treat it like a regular goto.
420                let ty = callsite
421                    .callee
422                    .instantiate_mir(tcx, ty::EarlyBinder::bind(&place.ty(callee_body, tcx).ty));
423                if ty.needs_drop(tcx, self.typing_env())
424                    && let UnwindAction::Cleanup(unwind) = unwind
425                {
426                    work_list.push(unwind);
427                }
428            } else if callee_attrs.instruction_set != caller_attrs.instruction_set
429                && matches!(term.kind, TerminatorKind::InlineAsm { .. })
430            {
431                // During the attribute checking stage we allow a callee with no
432                // instruction_set assigned to count as compatible with a function that does
433                // assign one. However, during this stage we require an exact match when any
434                // inline-asm is detected. LLVM will still possibly do an inline later on
435                // if the no-attribute function ends up with the same instruction set anyway.
436                return Err("cannot move inline-asm across instruction sets");
437            } else if let TerminatorKind::TailCall { .. } = term.kind {
438                // FIXME(explicit_tail_calls): figure out how exactly functions containing tail
439                // calls can be inlined (and if they even should)
440                return Err("can't inline functions with tail calls");
441            } else {
442                work_list.extend(term.successors())
443            }
444        }
445
446        // N.B. We still apply our cost threshold to #[inline(always)] functions.
447        // That attribute is often applied to very large functions that exceed LLVM's (very
448        // generous) inlining threshold. Such functions are very poor MIR inlining candidates.
449        // Always inlining #[inline(always)] functions in MIR, on net, slows down the compiler.
450        let cost = checker.cost();
451        if cost <= threshold {
452            debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
453            Ok(())
454        } else {
455            debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
456            Err("cost above threshold")
457        }
458    }
459
460    fn on_inline_success(
461        &mut self,
462        callsite: &CallSite<'tcx>,
463        caller_body: &mut Body<'tcx>,
464        new_blocks: std::ops::Range<BasicBlock>,
465    ) {
466        self.changed = true;
467
468        let new_calls_count = new_blocks
469            .clone()
470            .filter(|&bb| is_call_like(caller_body.basic_blocks[bb].terminator()))
471            .count();
472        if new_calls_count > 1 {
473            self.top_down_counter += 1;
474        }
475
476        self.history.push(callsite.callee.def_id());
477        process_blocks(self, caller_body, new_blocks);
478        self.history.pop();
479
480        if self.history.is_empty() {
481            self.top_down_counter = 0;
482        }
483    }
484
485    fn on_inline_failure(&self, _: &CallSite<'tcx>, _: &'static str) {}
486}
487
488fn inline<'tcx, T: Inliner<'tcx>>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
489    let def_id = body.source.def_id();
490
491    // Only do inlining into fn bodies.
492    if !tcx.hir_body_owner_kind(def_id).is_fn_or_closure() {
493        return false;
494    }
495
496    let mut inliner = T::new(tcx, def_id, body);
497    if !inliner.check_caller_mir_body(body) {
498        return false;
499    }
500
501    let blocks = START_BLOCK..body.basic_blocks.next_index();
502    process_blocks(&mut inliner, body, blocks);
503    inliner.changed()
504}
505
506fn process_blocks<'tcx, I: Inliner<'tcx>>(
507    inliner: &mut I,
508    caller_body: &mut Body<'tcx>,
509    blocks: Range<BasicBlock>,
510) {
511    for bb in blocks {
512        let bb_data = &caller_body[bb];
513        if bb_data.is_cleanup {
514            continue;
515        }
516
517        let Some(callsite) = resolve_callsite(inliner, caller_body, bb, bb_data) else {
518            continue;
519        };
520
521        let span = trace_span!("process_blocks", %callsite.callee, ?bb);
522        let _guard = span.enter();
523
524        match try_inlining(inliner, caller_body, &callsite) {
525            Err(reason) => {
526                debug!("not-inlined {} [{}]", callsite.callee, reason);
527                inliner.on_inline_failure(&callsite, reason);
528            }
529            Ok(new_blocks) => {
530                debug!("inlined {}", callsite.callee);
531                inliner.on_inline_success(&callsite, caller_body, new_blocks);
532            }
533        }
534    }
535}
536
537fn resolve_callsite<'tcx, I: Inliner<'tcx>>(
538    inliner: &I,
539    caller_body: &Body<'tcx>,
540    bb: BasicBlock,
541    bb_data: &BasicBlockData<'tcx>,
542) -> Option<CallSite<'tcx>> {
543    let tcx = inliner.tcx();
544    // Only consider direct calls to functions
545    let terminator = bb_data.terminator();
546
547    // FIXME(explicit_tail_calls): figure out if we can inline tail calls
548    if let TerminatorKind::Call { ref func, fn_span, .. } = terminator.kind {
549        let func_ty = func.ty(caller_body, tcx);
550        if let ty::FnDef(def_id, args) = *func_ty.kind() {
551            if !inliner.should_inline_for_callee(def_id) {
552                debug!("not enabled");
553                return None;
554            }
555
556            // To resolve an instance its args have to be fully normalized.
557            let args = tcx.try_normalize_erasing_regions(inliner.typing_env(), args).ok()?;
558            let callee =
559                Instance::try_resolve(tcx, inliner.typing_env(), def_id, args).ok().flatten()?;
560
561            if let InstanceKind::Virtual(..) | InstanceKind::Intrinsic(_) = callee.def {
562                return None;
563            }
564
565            if inliner.history().contains(&callee.def_id()) {
566                return None;
567            }
568
569            let fn_sig = tcx.fn_sig(def_id).instantiate(tcx, args);
570
571            // Additionally, check that the body that we're inlining actually agrees
572            // with the ABI of the trait that the item comes from.
573            if let InstanceKind::Item(instance_def_id) = callee.def
574                && tcx.def_kind(instance_def_id) == DefKind::AssocFn
575                && let instance_fn_sig = tcx.fn_sig(instance_def_id).skip_binder()
576                && instance_fn_sig.abi() != fn_sig.abi()
577            {
578                return None;
579            }
580
581            let source_info = SourceInfo { span: fn_span, ..terminator.source_info };
582
583            return Some(CallSite { callee, fn_sig, block: bb, source_info });
584        }
585    }
586
587    None
588}
589
590/// Attempts to inline a callsite into the caller body. When successful returns basic blocks
591/// containing the inlined body. Otherwise returns an error describing why inlining didn't take
592/// place.
593fn try_inlining<'tcx, I: Inliner<'tcx>>(
594    inliner: &I,
595    caller_body: &mut Body<'tcx>,
596    callsite: &CallSite<'tcx>,
597) -> Result<std::ops::Range<BasicBlock>, &'static str> {
598    let tcx = inliner.tcx();
599    check_mir_is_available(inliner, caller_body, callsite.callee)?;
600
601    let callee_attrs = tcx.codegen_fn_attrs(callsite.callee.def_id());
602    check_inline::is_inline_valid_on_fn(tcx, callsite.callee.def_id())?;
603    check_codegen_attributes(inliner, callsite, callee_attrs)?;
604    inliner.check_codegen_attributes_extra(callee_attrs)?;
605
606    let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
607    let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
608    let destination_ty = destination.ty(&caller_body.local_decls, tcx).ty;
609    for arg in args {
610        if !arg.node.ty(&caller_body.local_decls, tcx).is_sized(tcx, inliner.typing_env()) {
611            // We do not allow inlining functions with unsized params. Inlining these functions
612            // could create unsized locals, which are unsound and being phased out.
613            return Err("call has unsized argument");
614        }
615    }
616
617    let callee_body = try_instance_mir(tcx, callsite.callee.def)?;
618    check_inline::is_inline_valid_on_body(tcx, callee_body)?;
619    inliner.check_callee_mir_body(callsite, callee_body, callee_attrs)?;
620
621    let Ok(callee_body) = callsite.callee.try_instantiate_mir_and_normalize_erasing_regions(
622        tcx,
623        inliner.typing_env(),
624        ty::EarlyBinder::bind(callee_body.clone()),
625    ) else {
626        debug!("failed to normalize callee body");
627        return Err("implementation limitation -- could not normalize callee body");
628    };
629
630    // Normally, this shouldn't be required, but trait normalization failure can create a
631    // validation ICE.
632    if !validate_types(tcx, inliner.typing_env(), &callee_body, &caller_body).is_empty() {
633        debug!("failed to validate callee body");
634        return Err("implementation limitation -- callee body failed validation");
635    }
636
637    // Check call signature compatibility.
638    // Normally, this shouldn't be required, but trait normalization failure can create a
639    // validation ICE.
640    let output_type = callee_body.return_ty();
641    if !util::sub_types(tcx, inliner.typing_env(), output_type, destination_ty) {
642        trace!(?output_type, ?destination_ty);
643        return Err("implementation limitation -- return type mismatch");
644    }
645    if callsite.fn_sig.abi() == ExternAbi::RustCall {
646        let (self_arg, arg_tuple) = match &args[..] {
647            [arg_tuple] => (None, arg_tuple),
648            [self_arg, arg_tuple] => (Some(self_arg), arg_tuple),
649            _ => bug!("Expected `rust-call` to have 1 or 2 args"),
650        };
651
652        let self_arg_ty = self_arg.map(|self_arg| self_arg.node.ty(&caller_body.local_decls, tcx));
653
654        let arg_tuple_ty = arg_tuple.node.ty(&caller_body.local_decls, tcx);
655        let arg_tys = if callee_body.spread_arg.is_some() {
656            std::slice::from_ref(&arg_tuple_ty)
657        } else {
658            let ty::Tuple(arg_tuple_tys) = *arg_tuple_ty.kind() else {
659                bug!("Closure arguments are not passed as a tuple");
660            };
661            arg_tuple_tys.as_slice()
662        };
663
664        for (arg_ty, input) in
665            self_arg_ty.into_iter().chain(arg_tys.iter().copied()).zip(callee_body.args_iter())
666        {
667            let input_type = callee_body.local_decls[input].ty;
668            if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
669                trace!(?arg_ty, ?input_type);
670                debug!("failed to normalize tuple argument type");
671                return Err("implementation limitation");
672            }
673        }
674    } else {
675        for (arg, input) in args.iter().zip(callee_body.args_iter()) {
676            let input_type = callee_body.local_decls[input].ty;
677            let arg_ty = arg.node.ty(&caller_body.local_decls, tcx);
678            if !util::sub_types(tcx, inliner.typing_env(), input_type, arg_ty) {
679                trace!(?arg_ty, ?input_type);
680                debug!("failed to normalize argument type");
681                return Err("implementation limitation -- arg mismatch");
682            }
683        }
684    }
685
686    let old_blocks = caller_body.basic_blocks.next_index();
687    inline_call(inliner, caller_body, callsite, callee_body);
688    let new_blocks = old_blocks..caller_body.basic_blocks.next_index();
689
690    Ok(new_blocks)
691}
692
693fn check_mir_is_available<'tcx, I: Inliner<'tcx>>(
694    inliner: &I,
695    caller_body: &Body<'tcx>,
696    callee: Instance<'tcx>,
697) -> Result<(), &'static str> {
698    let caller_def_id = caller_body.source.def_id();
699    let callee_def_id = callee.def_id();
700    if callee_def_id == caller_def_id {
701        return Err("self-recursion");
702    }
703
704    match callee.def {
705        InstanceKind::Item(_) => {
706            // If there is no MIR available (either because it was not in metadata or
707            // because it has no MIR because it's an extern function), then the inliner
708            // won't cause cycles on this.
709            if !inliner.tcx().is_mir_available(callee_def_id) {
710                debug!("item MIR unavailable");
711                return Err("implementation limitation -- MIR unavailable");
712            }
713        }
714        // These have no own callable MIR.
715        InstanceKind::Intrinsic(_) | InstanceKind::Virtual(..) => {
716            debug!("instance without MIR (intrinsic / virtual)");
717            return Err("implementation limitation -- cannot inline intrinsic");
718        }
719
720        // FIXME(#127030): `ConstParamHasTy` has bad interactions with
721        // the drop shim builder, which does not evaluate predicates in
722        // the correct param-env for types being dropped. Stall resolving
723        // the MIR for this instance until all of its const params are
724        // substituted.
725        InstanceKind::DropGlue(_, Some(ty)) if ty.has_type_flags(TypeFlags::HAS_CT_PARAM) => {
726            debug!("still needs substitution");
727            return Err("implementation limitation -- HACK for dropping polymorphic type");
728        }
729
730        // This cannot result in an immediate cycle since the callee MIR is a shim, which does
731        // not get any optimizations run on it. Any subsequent inlining may cause cycles, but we
732        // do not need to catch this here, we can wait until the inliner decides to continue
733        // inlining a second time.
734        InstanceKind::VTableShim(_)
735        | InstanceKind::ReifyShim(..)
736        | InstanceKind::FnPtrShim(..)
737        | InstanceKind::ClosureOnceShim { .. }
738        | InstanceKind::ConstructCoroutineInClosureShim { .. }
739        | InstanceKind::DropGlue(..)
740        | InstanceKind::CloneShim(..)
741        | InstanceKind::ThreadLocalShim(..)
742        | InstanceKind::FnPtrAddrShim(..)
743        | InstanceKind::AsyncDropGlueCtorShim(..) => return Ok(()),
744    }
745
746    if inliner.tcx().is_constructor(callee_def_id) {
747        trace!("constructors always have MIR");
748        // Constructor functions cannot cause a query cycle.
749        return Ok(());
750    }
751
752    if callee_def_id.is_local()
753        && !inliner
754            .tcx()
755            .is_lang_item(inliner.tcx().parent(caller_def_id), rustc_hir::LangItem::FnOnce)
756    {
757        // If we know for sure that the function we're calling will itself try to
758        // call us, then we avoid inlining that function.
759        if inliner.tcx().mir_callgraph_reachable((callee, caller_def_id.expect_local())) {
760            debug!("query cycle avoidance");
761            return Err("caller might be reachable from callee");
762        }
763
764        Ok(())
765    } else {
766        // This cannot result in an immediate cycle since the callee MIR is from another crate
767        // and is already optimized. Any subsequent inlining may cause cycles, but we do
768        // not need to catch this here, we can wait until the inliner decides to continue
769        // inlining a second time.
770        trace!("functions from other crates always have MIR");
771        Ok(())
772    }
773}
774
775/// Returns an error if inlining is not possible based on codegen attributes alone. A success
776/// indicates that inlining decision should be based on other criteria.
777fn check_codegen_attributes<'tcx, I: Inliner<'tcx>>(
778    inliner: &I,
779    callsite: &CallSite<'tcx>,
780    callee_attrs: &CodegenFnAttrs,
781) -> Result<(), &'static str> {
782    let tcx = inliner.tcx();
783    if let InlineAttr::Never = callee_attrs.inline {
784        return Err("never inline attribute");
785    }
786
787    if let OptimizeAttr::DoNotOptimize = callee_attrs.optimize {
788        return Err("has DoNotOptimize attribute");
789    }
790
791    inliner.check_codegen_attributes_extra(callee_attrs)?;
792
793    // Reachability pass defines which functions are eligible for inlining. Generally inlining
794    // other functions is incorrect because they could reference symbols that aren't exported.
795    let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
796    if !is_generic && !tcx.cross_crate_inlinable(callsite.callee.def_id()) {
797        return Err("not exported");
798    }
799
800    let codegen_fn_attrs = tcx.codegen_fn_attrs(inliner.caller_def_id());
801    if callee_attrs.no_sanitize != codegen_fn_attrs.no_sanitize {
802        return Err("incompatible sanitizer set");
803    }
804
805    // Two functions are compatible if the callee has no attribute (meaning
806    // that it's codegen agnostic), or sets an attribute that is identical
807    // to this function's attribute.
808    if callee_attrs.instruction_set.is_some()
809        && callee_attrs.instruction_set != codegen_fn_attrs.instruction_set
810    {
811        return Err("incompatible instruction set");
812    }
813
814    let callee_feature_names = callee_attrs.target_features.iter().map(|f| f.name);
815    let this_feature_names = codegen_fn_attrs.target_features.iter().map(|f| f.name);
816    if callee_feature_names.ne(this_feature_names) {
817        // In general it is not correct to inline a callee with target features that are a
818        // subset of the caller. This is because the callee might contain calls, and the ABI of
819        // those calls depends on the target features of the surrounding function. By moving a
820        // `Call` terminator from one MIR body to another with more target features, we might
821        // change the ABI of that call!
822        return Err("incompatible target features");
823    }
824
825    Ok(())
826}
827
828fn inline_call<'tcx, I: Inliner<'tcx>>(
829    inliner: &I,
830    caller_body: &mut Body<'tcx>,
831    callsite: &CallSite<'tcx>,
832    mut callee_body: Body<'tcx>,
833) {
834    let tcx = inliner.tcx();
835    let terminator = caller_body[callsite.block].terminator.take().unwrap();
836    let TerminatorKind::Call { func, args, destination, unwind, target, .. } = terminator.kind
837    else {
838        bug!("unexpected terminator kind {:?}", terminator.kind);
839    };
840
841    let return_block = if let Some(block) = target {
842        // Prepare a new block for code that should execute when call returns. We don't use
843        // target block directly since it might have other predecessors.
844        let data = BasicBlockData::new(
845            Some(Terminator {
846                source_info: terminator.source_info,
847                kind: TerminatorKind::Goto { target: block },
848            }),
849            caller_body[block].is_cleanup,
850        );
851        Some(caller_body.basic_blocks_mut().push(data))
852    } else {
853        None
854    };
855
856    // If the call is something like `a[*i] = f(i)`, where
857    // `i : &mut usize`, then just duplicating the `a[*i]`
858    // Place could result in two different locations if `f`
859    // writes to `i`. To prevent this we need to create a temporary
860    // borrow of the place and pass the destination as `*temp` instead.
861    fn dest_needs_borrow(place: Place<'_>) -> bool {
862        for elem in place.projection.iter() {
863            match elem {
864                ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
865                _ => {}
866            }
867        }
868
869        false
870    }
871
872    let dest = if dest_needs_borrow(destination) {
873        trace!("creating temp for return destination");
874        let dest = Rvalue::Ref(
875            tcx.lifetimes.re_erased,
876            BorrowKind::Mut { kind: MutBorrowKind::Default },
877            destination,
878        );
879        let dest_ty = dest.ty(caller_body, tcx);
880        let temp = Place::from(new_call_temp(caller_body, callsite, dest_ty, return_block));
881        caller_body[callsite.block].statements.push(Statement {
882            source_info: callsite.source_info,
883            kind: StatementKind::Assign(Box::new((temp, dest))),
884        });
885        tcx.mk_place_deref(temp)
886    } else {
887        destination
888    };
889
890    // Always create a local to hold the destination, as `RETURN_PLACE` may appear
891    // where a full `Place` is not allowed.
892    let (remap_destination, destination_local) = if let Some(d) = dest.as_local() {
893        (false, d)
894    } else {
895        (
896            true,
897            new_call_temp(caller_body, callsite, destination.ty(caller_body, tcx).ty, return_block),
898        )
899    };
900
901    // Copy the arguments if needed.
902    let args = make_call_args(inliner, args, callsite, caller_body, &callee_body, return_block);
903
904    let mut integrator = Integrator {
905        args: &args,
906        new_locals: Local::new(caller_body.local_decls.len())..,
907        new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
908        new_blocks: BasicBlock::new(caller_body.basic_blocks.len())..,
909        destination: destination_local,
910        callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
911        callsite,
912        cleanup_block: unwind,
913        in_cleanup_block: false,
914        return_block,
915        tcx,
916        always_live_locals: DenseBitSet::new_filled(callee_body.local_decls.len()),
917    };
918
919    // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
920    // (or existing ones, in a few special cases) in the caller.
921    integrator.visit_body(&mut callee_body);
922
923    // If there are any locals without storage markers, give them storage only for the
924    // duration of the call.
925    for local in callee_body.vars_and_temps_iter() {
926        if integrator.always_live_locals.contains(local) {
927            let new_local = integrator.map_local(local);
928            caller_body[callsite.block].statements.push(Statement {
929                source_info: callsite.source_info,
930                kind: StatementKind::StorageLive(new_local),
931            });
932        }
933    }
934    if let Some(block) = return_block {
935        // To avoid repeated O(n) insert, push any new statements to the end and rotate
936        // the slice once.
937        let mut n = 0;
938        if remap_destination {
939            caller_body[block].statements.push(Statement {
940                source_info: callsite.source_info,
941                kind: StatementKind::Assign(Box::new((
942                    dest,
943                    Rvalue::Use(Operand::Move(destination_local.into())),
944                ))),
945            });
946            n += 1;
947        }
948        for local in callee_body.vars_and_temps_iter().rev() {
949            if integrator.always_live_locals.contains(local) {
950                let new_local = integrator.map_local(local);
951                caller_body[block].statements.push(Statement {
952                    source_info: callsite.source_info,
953                    kind: StatementKind::StorageDead(new_local),
954                });
955                n += 1;
956            }
957        }
958        caller_body[block].statements.rotate_right(n);
959    }
960
961    // Insert all of the (mapped) parts of the callee body into the caller.
962    caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
963    caller_body.source_scopes.append(&mut callee_body.source_scopes);
964    if tcx
965        .sess
966        .opts
967        .unstable_opts
968        .inline_mir_preserve_debug
969        .unwrap_or(tcx.sess.opts.debuginfo != DebugInfo::None)
970    {
971        // Note that we need to preserve these in the standard library so that
972        // people working on rust can build with or without debuginfo while
973        // still getting consistent results from the mir-opt tests.
974        caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
975    }
976    caller_body.basic_blocks_mut().append(callee_body.basic_blocks_mut());
977
978    caller_body[callsite.block].terminator = Some(Terminator {
979        source_info: callsite.source_info,
980        kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
981    });
982
983    // Copy required constants from the callee_body into the caller_body. Although we are only
984    // pushing unevaluated consts to `required_consts`, here they may have been evaluated
985    // because we are calling `instantiate_and_normalize_erasing_regions` -- so we filter again.
986    caller_body.required_consts.as_mut().unwrap().extend(
987        callee_body.required_consts().into_iter().filter(|ct| ct.const_.is_required_const()),
988    );
989    // Now that we incorporated the callee's `required_consts`, we can remove the callee from
990    // `mentioned_items` -- but we have to take their `mentioned_items` in return. This does
991    // some extra work here to save the monomorphization collector work later. It helps a lot,
992    // since monomorphization can avoid a lot of work when the "mentioned items" are similar to
993    // the actually used items. By doing this we can entirely avoid visiting the callee!
994    // We need to reconstruct the `required_item` for the callee so that we can find and
995    // remove it.
996    let callee_item = MentionedItem::Fn(func.ty(caller_body, tcx));
997    let caller_mentioned_items = caller_body.mentioned_items.as_mut().unwrap();
998    if let Some(idx) = caller_mentioned_items.iter().position(|item| item.node == callee_item) {
999        // We found the callee, so remove it and add its items instead.
1000        caller_mentioned_items.remove(idx);
1001        caller_mentioned_items.extend(callee_body.mentioned_items());
1002    } else {
1003        // If we can't find the callee, there's no point in adding its items. Probably it
1004        // already got removed by being inlined elsewhere in the same function, so we already
1005        // took its items.
1006    }
1007}
1008
1009fn make_call_args<'tcx, I: Inliner<'tcx>>(
1010    inliner: &I,
1011    args: Box<[Spanned<Operand<'tcx>>]>,
1012    callsite: &CallSite<'tcx>,
1013    caller_body: &mut Body<'tcx>,
1014    callee_body: &Body<'tcx>,
1015    return_block: Option<BasicBlock>,
1016) -> Box<[Local]> {
1017    let tcx = inliner.tcx();
1018
1019    // There is a bit of a mismatch between the *caller* of a closure and the *callee*.
1020    // The caller provides the arguments wrapped up in a tuple:
1021    //
1022    //     tuple_tmp = (a, b, c)
1023    //     Fn::call(closure_ref, tuple_tmp)
1024    //
1025    // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
1026    // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
1027    // the job of unpacking this tuple. But here, we are codegen. =) So we want to create
1028    // a vector like
1029    //
1030    //     [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
1031    //
1032    // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
1033    // if we "spill" that into *another* temporary, so that we can map the argument
1034    // variable in the callee MIR directly to an argument variable on our side.
1035    // So we introduce temporaries like:
1036    //
1037    //     tmp0 = tuple_tmp.0
1038    //     tmp1 = tuple_tmp.1
1039    //     tmp2 = tuple_tmp.2
1040    //
1041    // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
1042    if callsite.fn_sig.abi() == ExternAbi::RustCall && callee_body.spread_arg.is_none() {
1043        // FIXME(edition_2024): switch back to a normal method call.
1044        let mut args = <_>::into_iter(args);
1045        let self_ = create_temp_if_necessary(
1046            inliner,
1047            args.next().unwrap().node,
1048            callsite,
1049            caller_body,
1050            return_block,
1051        );
1052        let tuple = create_temp_if_necessary(
1053            inliner,
1054            args.next().unwrap().node,
1055            callsite,
1056            caller_body,
1057            return_block,
1058        );
1059        assert!(args.next().is_none());
1060
1061        let tuple = Place::from(tuple);
1062        let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
1063            bug!("Closure arguments are not passed as a tuple");
1064        };
1065
1066        // The `closure_ref` in our example above.
1067        let closure_ref_arg = iter::once(self_);
1068
1069        // The `tmp0`, `tmp1`, and `tmp2` in our example above.
1070        let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
1071            // This is e.g., `tuple_tmp.0` in our example above.
1072            let tuple_field = Operand::Move(tcx.mk_place_field(tuple, FieldIdx::new(i), ty));
1073
1074            // Spill to a local to make e.g., `tmp0`.
1075            create_temp_if_necessary(inliner, tuple_field, callsite, caller_body, return_block)
1076        });
1077
1078        closure_ref_arg.chain(tuple_tmp_args).collect()
1079    } else {
1080        args.into_iter()
1081            .map(|a| create_temp_if_necessary(inliner, a.node, callsite, caller_body, return_block))
1082            .collect()
1083    }
1084}
1085
1086/// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh temporary `T` and an
1087/// instruction `T = arg`, and returns `T`.
1088fn create_temp_if_necessary<'tcx, I: Inliner<'tcx>>(
1089    inliner: &I,
1090    arg: Operand<'tcx>,
1091    callsite: &CallSite<'tcx>,
1092    caller_body: &mut Body<'tcx>,
1093    return_block: Option<BasicBlock>,
1094) -> Local {
1095    // Reuse the operand if it is a moved temporary.
1096    if let Operand::Move(place) = &arg
1097        && let Some(local) = place.as_local()
1098        && caller_body.local_kind(local) == LocalKind::Temp
1099    {
1100        return local;
1101    }
1102
1103    // Otherwise, create a temporary for the argument.
1104    trace!("creating temp for argument {:?}", arg);
1105    let arg_ty = arg.ty(caller_body, inliner.tcx());
1106    let local = new_call_temp(caller_body, callsite, arg_ty, return_block);
1107    caller_body[callsite.block].statements.push(Statement {
1108        source_info: callsite.source_info,
1109        kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))),
1110    });
1111    local
1112}
1113
1114/// Introduces a new temporary into the caller body that is live for the duration of the call.
1115fn new_call_temp<'tcx>(
1116    caller_body: &mut Body<'tcx>,
1117    callsite: &CallSite<'tcx>,
1118    ty: Ty<'tcx>,
1119    return_block: Option<BasicBlock>,
1120) -> Local {
1121    let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
1122
1123    caller_body[callsite.block].statements.push(Statement {
1124        source_info: callsite.source_info,
1125        kind: StatementKind::StorageLive(local),
1126    });
1127
1128    if let Some(block) = return_block {
1129        caller_body[block].statements.insert(
1130            0,
1131            Statement {
1132                source_info: callsite.source_info,
1133                kind: StatementKind::StorageDead(local),
1134            },
1135        );
1136    }
1137
1138    local
1139}
1140
1141/**
1142 * Integrator.
1143 *
1144 * Integrates blocks from the callee function into the calling function.
1145 * Updates block indices, references to locals and other control flow
1146 * stuff.
1147*/
1148struct Integrator<'a, 'tcx> {
1149    args: &'a [Local],
1150    new_locals: RangeFrom<Local>,
1151    new_scopes: RangeFrom<SourceScope>,
1152    new_blocks: RangeFrom<BasicBlock>,
1153    destination: Local,
1154    callsite_scope: SourceScopeData<'tcx>,
1155    callsite: &'a CallSite<'tcx>,
1156    cleanup_block: UnwindAction,
1157    in_cleanup_block: bool,
1158    return_block: Option<BasicBlock>,
1159    tcx: TyCtxt<'tcx>,
1160    always_live_locals: DenseBitSet<Local>,
1161}
1162
1163impl Integrator<'_, '_> {
1164    fn map_local(&self, local: Local) -> Local {
1165        let new = if local == RETURN_PLACE {
1166            self.destination
1167        } else {
1168            let idx = local.index() - 1;
1169            if idx < self.args.len() {
1170                self.args[idx]
1171            } else {
1172                Local::new(self.new_locals.start.index() + (idx - self.args.len()))
1173            }
1174        };
1175        trace!("mapping local `{:?}` to `{:?}`", local, new);
1176        new
1177    }
1178
1179    fn map_scope(&self, scope: SourceScope) -> SourceScope {
1180        let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
1181        trace!("mapping scope `{:?}` to `{:?}`", scope, new);
1182        new
1183    }
1184
1185    fn map_block(&self, block: BasicBlock) -> BasicBlock {
1186        let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
1187        trace!("mapping block `{:?}` to `{:?}`", block, new);
1188        new
1189    }
1190
1191    fn map_unwind(&self, unwind: UnwindAction) -> UnwindAction {
1192        if self.in_cleanup_block {
1193            match unwind {
1194                UnwindAction::Cleanup(_) | UnwindAction::Continue => {
1195                    bug!("cleanup on cleanup block");
1196                }
1197                UnwindAction::Unreachable | UnwindAction::Terminate(_) => return unwind,
1198            }
1199        }
1200
1201        match unwind {
1202            UnwindAction::Unreachable | UnwindAction::Terminate(_) => unwind,
1203            UnwindAction::Cleanup(target) => UnwindAction::Cleanup(self.map_block(target)),
1204            // Add an unwind edge to the original call's cleanup block
1205            UnwindAction::Continue => self.cleanup_block,
1206        }
1207    }
1208}
1209
1210impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
1211    fn tcx(&self) -> TyCtxt<'tcx> {
1212        self.tcx
1213    }
1214
1215    fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
1216        *local = self.map_local(*local);
1217    }
1218
1219    fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
1220        self.super_source_scope_data(scope_data);
1221        if scope_data.parent_scope.is_none() {
1222            // Attach the outermost callee scope as a child of the callsite
1223            // scope, via the `parent_scope` and `inlined_parent_scope` chains.
1224            scope_data.parent_scope = Some(self.callsite.source_info.scope);
1225            assert_eq!(scope_data.inlined_parent_scope, None);
1226            scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
1227                Some(self.callsite.source_info.scope)
1228            } else {
1229                self.callsite_scope.inlined_parent_scope
1230            };
1231
1232            // Mark the outermost callee scope as an inlined one.
1233            assert_eq!(scope_data.inlined, None);
1234            scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
1235        } else if scope_data.inlined_parent_scope.is_none() {
1236            // Make it easy to find the scope with `inlined` set above.
1237            scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
1238        }
1239    }
1240
1241    fn visit_source_scope(&mut self, scope: &mut SourceScope) {
1242        *scope = self.map_scope(*scope);
1243    }
1244
1245    fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
1246        self.in_cleanup_block = data.is_cleanup;
1247        self.super_basic_block_data(block, data);
1248        self.in_cleanup_block = false;
1249    }
1250
1251    fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
1252        self.super_retag(kind, place, loc);
1253
1254        // We have to patch all inlined retags to be aware that they are no longer
1255        // happening on function entry.
1256        if *kind == RetagKind::FnEntry {
1257            *kind = RetagKind::Default;
1258        }
1259    }
1260
1261    fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
1262        if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
1263            statement.kind
1264        {
1265            self.always_live_locals.remove(local);
1266        }
1267        self.super_statement(statement, location);
1268    }
1269
1270    fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
1271        // Don't try to modify the implicit `_0` access on return (`return` terminators are
1272        // replaced down below anyways).
1273        if !matches!(terminator.kind, TerminatorKind::Return) {
1274            self.super_terminator(terminator, loc);
1275        } else {
1276            self.visit_source_info(&mut terminator.source_info);
1277        }
1278
1279        match terminator.kind {
1280            TerminatorKind::CoroutineDrop | TerminatorKind::Yield { .. } => bug!(),
1281            TerminatorKind::Goto { ref mut target } => {
1282                *target = self.map_block(*target);
1283            }
1284            TerminatorKind::SwitchInt { ref mut targets, .. } => {
1285                for tgt in targets.all_targets_mut() {
1286                    *tgt = self.map_block(*tgt);
1287                }
1288            }
1289            TerminatorKind::Drop { ref mut target, ref mut unwind, .. } => {
1290                *target = self.map_block(*target);
1291                *unwind = self.map_unwind(*unwind);
1292            }
1293            TerminatorKind::TailCall { .. } => {
1294                // check_mir_body forbids tail calls
1295                unreachable!()
1296            }
1297            TerminatorKind::Call { ref mut target, ref mut unwind, .. } => {
1298                if let Some(ref mut tgt) = *target {
1299                    *tgt = self.map_block(*tgt);
1300                }
1301                *unwind = self.map_unwind(*unwind);
1302            }
1303            TerminatorKind::Assert { ref mut target, ref mut unwind, .. } => {
1304                *target = self.map_block(*target);
1305                *unwind = self.map_unwind(*unwind);
1306            }
1307            TerminatorKind::Return => {
1308                terminator.kind = if let Some(tgt) = self.return_block {
1309                    TerminatorKind::Goto { target: tgt }
1310                } else {
1311                    TerminatorKind::Unreachable
1312                }
1313            }
1314            TerminatorKind::UnwindResume => {
1315                terminator.kind = match self.cleanup_block {
1316                    UnwindAction::Cleanup(tgt) => TerminatorKind::Goto { target: tgt },
1317                    UnwindAction::Continue => TerminatorKind::UnwindResume,
1318                    UnwindAction::Unreachable => TerminatorKind::Unreachable,
1319                    UnwindAction::Terminate(reason) => TerminatorKind::UnwindTerminate(reason),
1320                };
1321            }
1322            TerminatorKind::UnwindTerminate(_) => {}
1323            TerminatorKind::Unreachable => {}
1324            TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
1325                *real_target = self.map_block(*real_target);
1326                *imaginary_target = self.map_block(*imaginary_target);
1327            }
1328            TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
1329            // see the ordering of passes in the optimized_mir query.
1330            {
1331                bug!("False unwinds should have been removed before inlining")
1332            }
1333            TerminatorKind::InlineAsm { ref mut targets, ref mut unwind, .. } => {
1334                for tgt in targets.iter_mut() {
1335                    *tgt = self.map_block(*tgt);
1336                }
1337                *unwind = self.map_unwind(*unwind);
1338            }
1339        }
1340    }
1341}
1342
1343#[instrument(skip(tcx), level = "debug")]
1344fn try_instance_mir<'tcx>(
1345    tcx: TyCtxt<'tcx>,
1346    instance: InstanceKind<'tcx>,
1347) -> Result<&'tcx Body<'tcx>, &'static str> {
1348    if let ty::InstanceKind::DropGlue(_, Some(ty))
1349    | ty::InstanceKind::AsyncDropGlueCtorShim(_, Some(ty)) = instance
1350        && let ty::Adt(def, args) = ty.kind()
1351    {
1352        let fields = def.all_fields();
1353        for field in fields {
1354            let field_ty = field.ty(tcx, args);
1355            if field_ty.has_param() && field_ty.has_aliases() {
1356                return Err("cannot build drop shim for polymorphic type");
1357            }
1358        }
1359    }
1360    Ok(tcx.instance_mir(instance))
1361}
1362
1363fn body_is_forwarder(body: &Body<'_>) -> bool {
1364    let TerminatorKind::Call { target, .. } = body.basic_blocks[START_BLOCK].terminator().kind
1365    else {
1366        return false;
1367    };
1368    if let Some(target) = target {
1369        let TerminatorKind::Return = body.basic_blocks[target].terminator().kind else {
1370            return false;
1371        };
1372    }
1373
1374    let max_blocks = if !body.is_polymorphic {
1375        2
1376    } else if target.is_none() {
1377        3
1378    } else {
1379        4
1380    };
1381    if body.basic_blocks.len() > max_blocks {
1382        return false;
1383    }
1384
1385    body.basic_blocks.iter_enumerated().all(|(bb, bb_data)| {
1386        bb == START_BLOCK
1387            || matches!(
1388                bb_data.terminator().kind,
1389                TerminatorKind::Return
1390                    | TerminatorKind::Drop { .. }
1391                    | TerminatorKind::UnwindResume
1392                    | TerminatorKind::UnwindTerminate(_)
1393            )
1394    })
1395}