1 //! Inlining pass for MIR functions
2 use crate::deref_separator::deref_finder;
3 use rustc_attr::InlineAttr;
4 use rustc_const_eval::transform::validate::equal_up_to_regions;
5 use rustc_index::bit_set::BitSet;
6 use rustc_index::vec::Idx;
7 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
8 use rustc_middle::mir::visit::*;
9 use rustc_middle::mir::*;
10 use rustc_middle::ty::subst::Subst;
11 use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
12 use rustc_session::config::OptLevel;
13 use rustc_span::{hygiene::ExpnKind, ExpnData, LocalExpnId, Span};
14 use rustc_target::spec::abi::Abi;
16 use super::simplify::{remove_dead_blocks, CfgSimplifier};
19 use std::ops::{Range, RangeFrom};
23 const INSTR_COST: usize = 5;
24 const CALL_PENALTY: usize = 25;
25 const LANDINGPAD_PENALTY: usize = 50;
26 const RESUME_PENALTY: usize = 45;
28 const UNKNOWN_SIZE_COST: usize = 10;
32 #[derive(Copy, Clone, Debug)]
33 struct CallSite<'tcx> {
34 callee: Instance<'tcx>,
35 fn_sig: ty::PolyFnSig<'tcx>,
37 target: Option<BasicBlock>,
38 source_info: SourceInfo,
41 impl<'tcx> MirPass<'tcx> for Inline {
42 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
43 if let Some(enabled) = sess.opts.debugging_opts.inline_mir {
47 match sess.mir_opt_level() {
50 (sess.opts.optimize == OptLevel::Default
51 || sess.opts.optimize == OptLevel::Aggressive)
52 && sess.opts.incremental == None
58 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
59 let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
60 let _guard = span.enter();
61 if inline(tcx, body) {
62 debug!("running simplify cfg on {:?}", body.source);
63 CfgSimplifier::new(body).simplify();
64 remove_dead_blocks(tcx, body);
65 deref_finder(tcx, body);
70 fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
71 let def_id = body.source.def_id().expect_local();
73 // Only do inlining into fn bodies.
74 if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() {
77 if body.source.promoted.is_some() {
80 // Avoid inlining into generators, since their `optimized_mir` is used for layout computation,
81 // which can create a cycle, even when no attempt is made to inline the function in the other
83 if body.generator.is_some() {
87 let param_env = tcx.param_env_reveal_all_normalized(def_id);
89 let mut this = Inliner {
92 codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
96 let blocks = BasicBlock::new(0)..body.basic_blocks().next_index();
97 this.process_blocks(body, blocks);
101 struct Inliner<'tcx> {
103 param_env: ParamEnv<'tcx>,
104 /// Caller codegen attributes.
105 codegen_fn_attrs: &'tcx CodegenFnAttrs,
106 /// Stack of inlined Instances.
107 history: Vec<ty::Instance<'tcx>>,
108 /// Indicates that the caller body has been modified.
112 impl<'tcx> Inliner<'tcx> {
113 fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) {
115 let bb_data = &caller_body[bb];
116 if bb_data.is_cleanup {
120 let Some(callsite) = self.resolve_callsite(caller_body, bb, bb_data) else {
124 let span = trace_span!("process_blocks", %callsite.callee, ?bb);
125 let _guard = span.enter();
127 match self.try_inlining(caller_body, &callsite) {
129 debug!("not-inlined {} [{}]", callsite.callee, reason);
133 debug!("inlined {}", callsite.callee);
135 self.history.push(callsite.callee);
136 self.process_blocks(caller_body, new_blocks);
143 /// Attempts to inline a callsite into the caller body. When successful returns basic blocks
144 /// containing the inlined body. Otherwise returns an error describing why inlining didn't take
148 caller_body: &mut Body<'tcx>,
149 callsite: &CallSite<'tcx>,
150 ) -> Result<std::ops::Range<BasicBlock>, &'static str> {
151 let callee_attrs = self.tcx.codegen_fn_attrs(callsite.callee.def_id());
152 self.check_codegen_attributes(callsite, callee_attrs)?;
153 self.check_mir_is_available(caller_body, &callsite.callee)?;
154 let callee_body = self.tcx.instance_mir(callsite.callee.def);
155 self.check_mir_body(callsite, callee_body, callee_attrs)?;
157 if !self.tcx.consider_optimizing(|| {
158 format!("Inline {:?} into {:?}", callsite.callee, caller_body.source)
160 return Err("optimization fuel exhausted");
163 let Ok(callee_body) = callsite.callee.try_subst_mir_and_normalize_erasing_regions(
168 return Err("failed to normalize callee body");
171 // Check call signature compatibility.
172 // Normally, this shouldn't be required, but trait normalization failure can create a
174 let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
175 let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
176 let destination_ty = destination.ty(&caller_body.local_decls, self.tcx).ty;
177 let output_type = callee_body.return_ty();
178 if !equal_up_to_regions(self.tcx, self.param_env, output_type, destination_ty) {
179 trace!(?output_type, ?destination_ty);
180 return Err("failed to normalize return type");
182 if callsite.fn_sig.abi() == Abi::RustCall {
183 let (arg_tuple, skipped_args) = match &args[..] {
184 [arg_tuple] => (arg_tuple, 0),
185 [_, arg_tuple] => (arg_tuple, 1),
186 _ => bug!("Expected `rust-call` to have 1 or 2 args"),
189 let arg_tuple_ty = arg_tuple.ty(&caller_body.local_decls, self.tcx);
190 let ty::Tuple(arg_tuple_tys) = arg_tuple_ty.kind() else {
191 bug!("Closure arguments are not passed as a tuple");
194 for (arg_ty, input) in
195 arg_tuple_tys.iter().zip(callee_body.args_iter().skip(skipped_args))
197 let input_type = callee_body.local_decls[input].ty;
198 if !equal_up_to_regions(self.tcx, self.param_env, arg_ty, input_type) {
199 trace!(?arg_ty, ?input_type);
200 return Err("failed to normalize tuple argument type");
204 for (arg, input) in args.iter().zip(callee_body.args_iter()) {
205 let input_type = callee_body.local_decls[input].ty;
206 let arg_ty = arg.ty(&caller_body.local_decls, self.tcx);
207 if !equal_up_to_regions(self.tcx, self.param_env, arg_ty, input_type) {
208 trace!(?arg_ty, ?input_type);
209 return Err("failed to normalize argument type");
214 let old_blocks = caller_body.basic_blocks().next_index();
215 self.inline_call(caller_body, &callsite, callee_body);
216 let new_blocks = old_blocks..caller_body.basic_blocks().next_index();
221 fn check_mir_is_available(
223 caller_body: &Body<'tcx>,
224 callee: &Instance<'tcx>,
225 ) -> Result<(), &'static str> {
226 let caller_def_id = caller_body.source.def_id();
227 let callee_def_id = callee.def_id();
228 if callee_def_id == caller_def_id {
229 return Err("self-recursion");
233 InstanceDef::Item(_) => {
234 // If there is no MIR available (either because it was not in metadata or
235 // because it has no MIR because it's an extern function), then the inliner
236 // won't cause cycles on this.
237 if !self.tcx.is_mir_available(callee_def_id) {
238 return Err("item MIR unavailable");
241 // These have no own callable MIR.
242 InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => {
243 return Err("instance without MIR (intrinsic / virtual)");
245 // This cannot result in an immediate cycle since the callee MIR is a shim, which does
246 // not get any optimizations run on it. Any subsequent inlining may cause cycles, but we
247 // do not need to catch this here, we can wait until the inliner decides to continue
248 // inlining a second time.
249 InstanceDef::VtableShim(_)
250 | InstanceDef::ReifyShim(_)
251 | InstanceDef::FnPtrShim(..)
252 | InstanceDef::ClosureOnceShim { .. }
253 | InstanceDef::DropGlue(..)
254 | InstanceDef::CloneShim(..) => return Ok(()),
257 if self.tcx.is_constructor(callee_def_id) {
258 trace!("constructors always have MIR");
259 // Constructor functions cannot cause a query cycle.
263 if callee_def_id.is_local() {
264 // Avoid a cycle here by only using `instance_mir` only if we have
265 // a lower `DefPathHash` than the callee. This ensures that the callee will
266 // not inline us. This trick even works with incremental compilation,
267 // since `DefPathHash` is stable.
268 if self.tcx.def_path_hash(caller_def_id).local_hash()
269 < self.tcx.def_path_hash(callee_def_id).local_hash()
274 // If we know for sure that the function we're calling will itself try to
275 // call us, then we avoid inlining that function.
276 if self.tcx.mir_callgraph_reachable((*callee, caller_def_id.expect_local())) {
277 return Err("caller might be reachable from callee (query cycle avoidance)");
282 // This cannot result in an immediate cycle since the callee MIR is from another crate
283 // and is already optimized. Any subsequent inlining may cause cycles, but we do
284 // not need to catch this here, we can wait until the inliner decides to continue
285 // inlining a second time.
286 trace!("functions from other crates always have MIR");
293 caller_body: &Body<'tcx>,
295 bb_data: &BasicBlockData<'tcx>,
296 ) -> Option<CallSite<'tcx>> {
297 // Only consider direct calls to functions
298 let terminator = bb_data.terminator();
299 if let TerminatorKind::Call { ref func, target, .. } = terminator.kind {
300 let func_ty = func.ty(caller_body, self.tcx);
301 if let ty::FnDef(def_id, substs) = *func_ty.kind() {
302 // To resolve an instance its substs have to be fully normalized.
303 let substs = self.tcx.try_normalize_erasing_regions(self.param_env, substs).ok()?;
305 Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
307 if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
311 if self.history.contains(&callee) {
315 let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, substs);
317 return Some(CallSite {
322 source_info: terminator.source_info,
330 /// Returns an error if inlining is not possible based on codegen attributes alone. A success
331 /// indicates that inlining decision should be based on other criteria.
332 fn check_codegen_attributes(
334 callsite: &CallSite<'tcx>,
335 callee_attrs: &CodegenFnAttrs,
336 ) -> Result<(), &'static str> {
337 match callee_attrs.inline {
338 InlineAttr::Never => return Err("never inline hint"),
339 InlineAttr::Always | InlineAttr::Hint => {}
340 InlineAttr::None => {
341 if self.tcx.sess.mir_opt_level() <= 2 {
342 return Err("at mir-opt-level=2, only #[inline] is inlined");
347 // Only inline local functions if they would be eligible for cross-crate
348 // inlining. This is to ensure that the final crate doesn't have MIR that
349 // reference unexported symbols
350 if callsite.callee.def_id().is_local() {
351 let is_generic = callsite.callee.substs.non_erasable_generics().next().is_some();
352 if !is_generic && !callee_attrs.requests_inline() {
353 return Err("not exported");
357 if callsite.fn_sig.c_variadic() {
358 return Err("C variadic");
361 if callee_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
365 if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
369 if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize {
370 return Err("incompatible sanitizer set");
373 if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set {
374 return Err("incompatible instruction set");
377 for feature in &callee_attrs.target_features {
378 if !self.codegen_fn_attrs.target_features.contains(feature) {
379 return Err("incompatible target feature");
386 /// Returns inlining decision that is based on the examination of callee MIR body.
387 /// Assumes that codegen attributes have been checked for compatibility already.
388 #[instrument(level = "debug", skip(self, callee_body))]
391 callsite: &CallSite<'tcx>,
392 callee_body: &Body<'tcx>,
393 callee_attrs: &CodegenFnAttrs,
394 ) -> Result<(), &'static str> {
397 let mut threshold = if callee_attrs.requests_inline() {
398 self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold.unwrap_or(100)
400 self.tcx.sess.opts.debugging_opts.inline_mir_threshold.unwrap_or(50)
403 // Give a bonus functions with a small number of blocks,
404 // We normally have two or three blocks for even
405 // very small functions.
406 if callee_body.basic_blocks().len() <= 3 {
407 threshold += threshold / 4;
409 debug!(" final inline threshold = {}", threshold);
411 // FIXME: Give a bonus to functions with only a single caller
412 let mut first_block = true;
415 // Traverse the MIR manually so we can account for the effects of
416 // inlining on the CFG.
417 let mut work_list = vec![START_BLOCK];
418 let mut visited = BitSet::new_empty(callee_body.basic_blocks().len());
419 while let Some(bb) = work_list.pop() {
420 if !visited.insert(bb.index()) {
423 let blk = &callee_body.basic_blocks()[bb];
425 for stmt in &blk.statements {
426 // Don't count StorageLive/StorageDead in the inlining cost.
428 StatementKind::StorageLive(_)
429 | StatementKind::StorageDead(_)
430 | StatementKind::Deinit(_)
431 | StatementKind::Nop => {}
432 _ => cost += INSTR_COST,
435 let term = blk.terminator();
436 let mut is_drop = false;
438 TerminatorKind::Drop { ref place, target, unwind }
439 | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => {
441 work_list.push(target);
442 // If the place doesn't actually need dropping, treat it like
444 let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty);
445 if ty.needs_drop(tcx, self.param_env) {
446 cost += CALL_PENALTY;
447 if let Some(unwind) = unwind {
448 cost += LANDINGPAD_PENALTY;
449 work_list.push(unwind);
456 TerminatorKind::Unreachable | TerminatorKind::Call { target: None, .. }
459 // If the function always diverges, don't inline
460 // unless the cost is zero
464 TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
465 if let ty::FnDef(def_id, _) =
466 *callsite.callee.subst_mir(self.tcx, &f.literal.ty()).kind()
468 // Don't give intrinsics the extra penalty for calls
469 if tcx.is_intrinsic(def_id) {
472 cost += CALL_PENALTY;
475 cost += CALL_PENALTY;
477 if cleanup.is_some() {
478 cost += LANDINGPAD_PENALTY;
481 TerminatorKind::Assert { cleanup, .. } => {
482 cost += CALL_PENALTY;
484 if cleanup.is_some() {
485 cost += LANDINGPAD_PENALTY;
488 TerminatorKind::Resume => cost += RESUME_PENALTY,
489 TerminatorKind::InlineAsm { cleanup, .. } => {
492 if cleanup.is_some() {
493 cost += LANDINGPAD_PENALTY;
496 _ => cost += INSTR_COST,
500 for succ in term.successors() {
501 work_list.push(succ);
508 // Count up the cost of local variables and temps, if we know the size
509 // use that, otherwise we use a moderately-large dummy cost.
511 let ptr_size = tcx.data_layout.pointer_size.bytes();
513 for v in callee_body.vars_and_temps_iter() {
514 let ty = callsite.callee.subst_mir(self.tcx, &callee_body.local_decls[v].ty);
515 // Cost of the var is the size in machine-words, if we know
517 if let Some(size) = type_size_of(tcx, self.param_env, ty) {
518 cost += ((size + ptr_size - 1) / ptr_size) as usize;
520 cost += UNKNOWN_SIZE_COST;
524 if let InlineAttr::Always = callee_attrs.inline {
525 debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
527 } else if cost <= threshold {
528 debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
531 debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
532 Err("cost above threshold")
538 caller_body: &mut Body<'tcx>,
539 callsite: &CallSite<'tcx>,
540 mut callee_body: Body<'tcx>,
542 let terminator = caller_body[callsite.block].terminator.take().unwrap();
543 match terminator.kind {
544 TerminatorKind::Call { args, destination, cleanup, .. } => {
545 // If the call is something like `a[*i] = f(i)`, where
546 // `i : &mut usize`, then just duplicating the `a[*i]`
547 // Place could result in two different locations if `f`
548 // writes to `i`. To prevent this we need to create a temporary
549 // borrow of the place and pass the destination as `*temp` instead.
550 fn dest_needs_borrow(place: Place<'_>) -> bool {
551 for elem in place.projection.iter() {
553 ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
561 let dest = if dest_needs_borrow(destination) {
562 trace!("creating temp for return destination");
563 let dest = Rvalue::Ref(
564 self.tcx.lifetimes.re_erased,
565 BorrowKind::Mut { allow_two_phase_borrow: false },
568 let dest_ty = dest.ty(caller_body, self.tcx);
569 let temp = Place::from(self.new_call_temp(caller_body, &callsite, dest_ty));
570 caller_body[callsite.block].statements.push(Statement {
571 source_info: callsite.source_info,
572 kind: StatementKind::Assign(Box::new((temp, dest))),
574 self.tcx.mk_place_deref(temp)
579 // Copy the arguments if needed.
580 let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);
582 let mut expn_data = ExpnData::default(
584 callsite.source_info.span,
585 self.tcx.sess.edition(),
589 expn_data.def_site = callee_body.span;
591 LocalExpnId::fresh(expn_data, self.tcx.create_stable_hashing_context());
592 let mut integrator = Integrator {
594 new_locals: Local::new(caller_body.local_decls.len())..,
595 new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
596 new_blocks: BasicBlock::new(caller_body.basic_blocks().len())..,
598 callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
600 cleanup_block: cleanup,
601 in_cleanup_block: false,
604 always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
607 // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
608 // (or existing ones, in a few special cases) in the caller.
609 integrator.visit_body(&mut callee_body);
611 // If there are any locals without storage markers, give them storage only for the
612 // duration of the call.
613 for local in callee_body.vars_and_temps_iter() {
614 if integrator.always_live_locals.contains(local) {
615 let new_local = integrator.map_local(local);
616 caller_body[callsite.block].statements.push(Statement {
617 source_info: callsite.source_info,
618 kind: StatementKind::StorageLive(new_local),
622 if let Some(block) = callsite.target {
623 // To avoid repeated O(n) insert, push any new statements to the end and rotate
626 for local in callee_body.vars_and_temps_iter().rev() {
627 if integrator.always_live_locals.contains(local) {
628 let new_local = integrator.map_local(local);
629 caller_body[block].statements.push(Statement {
630 source_info: callsite.source_info,
631 kind: StatementKind::StorageDead(new_local),
636 caller_body[block].statements.rotate_right(n);
639 // Insert all of the (mapped) parts of the callee body into the caller.
640 caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
641 caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..));
642 caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
643 caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..));
645 caller_body[callsite.block].terminator = Some(Terminator {
646 source_info: callsite.source_info,
647 kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
650 // Copy only unevaluated constants from the callee_body into the caller_body.
651 // Although we are only pushing `ConstKind::Unevaluated` consts to
652 // `required_consts`, here we may not only have `ConstKind::Unevaluated`
653 // because we are calling `subst_and_normalize_erasing_regions`.
654 caller_body.required_consts.extend(
655 callee_body.required_consts.iter().copied().filter(|&ct| {
656 match ct.literal.const_for_ty() {
657 Some(ct) => matches!(ct.kind(), ConstKind::Unevaluated(_)),
663 kind => bug!("unexpected terminator kind {:?}", kind),
669 args: Vec<Operand<'tcx>>,
670 callsite: &CallSite<'tcx>,
671 caller_body: &mut Body<'tcx>,
672 callee_body: &Body<'tcx>,
676 // There is a bit of a mismatch between the *caller* of a closure and the *callee*.
677 // The caller provides the arguments wrapped up in a tuple:
679 // tuple_tmp = (a, b, c)
680 // Fn::call(closure_ref, tuple_tmp)
682 // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
683 // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
684 // the job of unpacking this tuple. But here, we are codegen. =) So we want to create
687 // [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
689 // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
690 // if we "spill" that into *another* temporary, so that we can map the argument
691 // variable in the callee MIR directly to an argument variable on our side.
692 // So we introduce temporaries like:
694 // tmp0 = tuple_tmp.0
695 // tmp1 = tuple_tmp.1
696 // tmp2 = tuple_tmp.2
698 // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
699 if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
700 let mut args = args.into_iter();
701 let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
702 let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
703 assert!(args.next().is_none());
705 let tuple = Place::from(tuple);
706 let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
707 bug!("Closure arguments are not passed as a tuple");
710 // The `closure_ref` in our example above.
711 let closure_ref_arg = iter::once(self_);
713 // The `tmp0`, `tmp1`, and `tmp2` in our example above.
714 let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
715 // This is e.g., `tuple_tmp.0` in our example above.
716 let tuple_field = Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty));
718 // Spill to a local to make e.g., `tmp0`.
719 self.create_temp_if_necessary(tuple_field, callsite, caller_body)
722 closure_ref_arg.chain(tuple_tmp_args).collect()
725 .map(|a| self.create_temp_if_necessary(a, callsite, caller_body))
730 /// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh
731 /// temporary `T` and an instruction `T = arg`, and returns `T`.
732 fn create_temp_if_necessary(
735 callsite: &CallSite<'tcx>,
736 caller_body: &mut Body<'tcx>,
738 // Reuse the operand if it is a moved temporary.
739 if let Operand::Move(place) = &arg
740 && let Some(local) = place.as_local()
741 && caller_body.local_kind(local) == LocalKind::Temp
746 // Otherwise, create a temporary for the argument.
747 trace!("creating temp for argument {:?}", arg);
748 let arg_ty = arg.ty(caller_body, self.tcx);
749 let local = self.new_call_temp(caller_body, callsite, arg_ty);
750 caller_body[callsite.block].statements.push(Statement {
751 source_info: callsite.source_info,
752 kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))),
757 /// Introduces a new temporary into the caller body that is live for the duration of the call.
760 caller_body: &mut Body<'tcx>,
761 callsite: &CallSite<'tcx>,
764 let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
766 caller_body[callsite.block].statements.push(Statement {
767 source_info: callsite.source_info,
768 kind: StatementKind::StorageLive(local),
771 if let Some(block) = callsite.target {
772 caller_body[block].statements.insert(
775 source_info: callsite.source_info,
776 kind: StatementKind::StorageDead(local),
785 fn type_size_of<'tcx>(
787 param_env: ty::ParamEnv<'tcx>,
790 tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
796 * Integrates blocks from the callee function into the calling function.
797 * Updates block indices, references to locals and other control flow
800 struct Integrator<'a, 'tcx> {
802 new_locals: RangeFrom<Local>,
803 new_scopes: RangeFrom<SourceScope>,
804 new_blocks: RangeFrom<BasicBlock>,
805 destination: Place<'tcx>,
806 callsite_scope: SourceScopeData<'tcx>,
807 callsite: &'a CallSite<'tcx>,
808 cleanup_block: Option<BasicBlock>,
809 in_cleanup_block: bool,
811 expn_data: LocalExpnId,
812 always_live_locals: BitSet<Local>,
815 impl Integrator<'_, '_> {
816 fn map_local(&self, local: Local) -> Local {
817 let new = if local == RETURN_PLACE {
818 self.destination.local
820 let idx = local.index() - 1;
821 if idx < self.args.len() {
824 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
827 trace!("mapping local `{:?}` to `{:?}`", local, new);
831 fn map_scope(&self, scope: SourceScope) -> SourceScope {
832 let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
833 trace!("mapping scope `{:?}` to `{:?}`", scope, new);
837 fn map_block(&self, block: BasicBlock) -> BasicBlock {
838 let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
839 trace!("mapping block `{:?}` to `{:?}`", block, new);
844 impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
845 fn tcx(&self) -> TyCtxt<'tcx> {
849 fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
850 *local = self.map_local(*local);
853 fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
854 self.super_source_scope_data(scope_data);
855 if scope_data.parent_scope.is_none() {
856 // Attach the outermost callee scope as a child of the callsite
857 // scope, via the `parent_scope` and `inlined_parent_scope` chains.
858 scope_data.parent_scope = Some(self.callsite.source_info.scope);
859 assert_eq!(scope_data.inlined_parent_scope, None);
860 scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
861 Some(self.callsite.source_info.scope)
863 self.callsite_scope.inlined_parent_scope
866 // Mark the outermost callee scope as an inlined one.
867 assert_eq!(scope_data.inlined, None);
868 scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
869 } else if scope_data.inlined_parent_scope.is_none() {
870 // Make it easy to find the scope with `inlined` set above.
871 scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
875 fn visit_source_scope(&mut self, scope: &mut SourceScope) {
876 *scope = self.map_scope(*scope);
879 fn visit_span(&mut self, span: &mut Span) {
880 // Make sure that all spans track the fact that they were inlined.
881 *span = span.fresh_expansion(self.expn_data);
884 fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
885 for elem in place.projection {
886 // FIXME: Make sure that return place is not used in an indexing projection, since it
887 // won't be rebased as it is supposed to be.
888 assert_ne!(ProjectionElem::Index(RETURN_PLACE), elem);
891 // If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
892 let dest_proj_len = self.destination.projection.len();
893 if place.local == RETURN_PLACE && dest_proj_len > 0 {
894 let mut projs = Vec::with_capacity(dest_proj_len + place.projection.len());
895 projs.extend(self.destination.projection);
896 projs.extend(place.projection);
898 place.projection = self.tcx.intern_place_elems(&*projs);
900 // Handles integrating any locals that occur in the base
902 self.super_place(place, context, location)
905 fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
906 self.in_cleanup_block = data.is_cleanup;
907 self.super_basic_block_data(block, data);
908 self.in_cleanup_block = false;
911 fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
912 self.super_retag(kind, place, loc);
914 // We have to patch all inlined retags to be aware that they are no longer
915 // happening on function entry.
916 if *kind == RetagKind::FnEntry {
917 *kind = RetagKind::Default;
921 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
922 if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
925 self.always_live_locals.remove(local);
927 self.super_statement(statement, location);
930 fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
931 // Don't try to modify the implicit `_0` access on return (`return` terminators are
932 // replaced down below anyways).
933 if !matches!(terminator.kind, TerminatorKind::Return) {
934 self.super_terminator(terminator, loc);
937 match terminator.kind {
938 TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
939 TerminatorKind::Goto { ref mut target } => {
940 *target = self.map_block(*target);
942 TerminatorKind::SwitchInt { ref mut targets, .. } => {
943 for tgt in targets.all_targets_mut() {
944 *tgt = self.map_block(*tgt);
947 TerminatorKind::Drop { ref mut target, ref mut unwind, .. }
948 | TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
949 *target = self.map_block(*target);
950 if let Some(tgt) = *unwind {
951 *unwind = Some(self.map_block(tgt));
952 } else if !self.in_cleanup_block {
953 // Unless this drop is in a cleanup block, add an unwind edge to
954 // the original call's cleanup block
955 *unwind = self.cleanup_block;
958 TerminatorKind::Call { ref mut target, ref mut cleanup, .. } => {
959 if let Some(ref mut tgt) = *target {
960 *tgt = self.map_block(*tgt);
962 if let Some(tgt) = *cleanup {
963 *cleanup = Some(self.map_block(tgt));
964 } else if !self.in_cleanup_block {
965 // Unless this call is in a cleanup block, add an unwind edge to
966 // the original call's cleanup block
967 *cleanup = self.cleanup_block;
970 TerminatorKind::Assert { ref mut target, ref mut cleanup, .. } => {
971 *target = self.map_block(*target);
972 if let Some(tgt) = *cleanup {
973 *cleanup = Some(self.map_block(tgt));
974 } else if !self.in_cleanup_block {
975 // Unless this assert is in a cleanup block, add an unwind edge to
976 // the original call's cleanup block
977 *cleanup = self.cleanup_block;
980 TerminatorKind::Return => {
981 terminator.kind = if let Some(tgt) = self.callsite.target {
982 TerminatorKind::Goto { target: tgt }
984 TerminatorKind::Unreachable
987 TerminatorKind::Resume => {
988 if let Some(tgt) = self.cleanup_block {
989 terminator.kind = TerminatorKind::Goto { target: tgt }
992 TerminatorKind::Abort => {}
993 TerminatorKind::Unreachable => {}
994 TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
995 *real_target = self.map_block(*real_target);
996 *imaginary_target = self.map_block(*imaginary_target);
998 TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
999 // see the ordering of passes in the optimized_mir query.
1001 bug!("False unwinds should have been removed before inlining")
1003 TerminatorKind::InlineAsm { ref mut destination, ref mut cleanup, .. } => {
1004 if let Some(ref mut tgt) = *destination {
1005 *tgt = self.map_block(*tgt);
1006 } else if !self.in_cleanup_block {
1007 // Unless this inline asm is in a cleanup block, add an unwind edge to
1008 // the original call's cleanup block
1009 *cleanup = self.cleanup_block;