1 //! Inlining pass for MIR functions
3 use rustc_attr::InlineAttr;
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::traits::ObligationCause;
11 use rustc_middle::ty::subst::Subst;
12 use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
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 sess.opts.mir_opt_level() >= 3
50 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
51 let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
52 let _guard = span.enter();
53 if inline(tcx, body) {
54 debug!("running simplify cfg on {:?}", body.source);
55 CfgSimplifier::new(body).simplify();
56 remove_dead_blocks(tcx, body);
61 fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
62 let def_id = body.source.def_id();
63 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
65 // Only do inlining into fn bodies.
66 if !tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() {
69 if body.source.promoted.is_some() {
72 // Avoid inlining into generators, since their `optimized_mir` is used for layout computation,
73 // which can create a cycle, even when no attempt is made to inline the function in the other
75 if body.generator.is_some() {
79 let param_env = tcx.param_env_reveal_all_normalized(def_id);
80 let param_env = rustc_trait_selection::traits::normalize_param_env_or_error(
84 ObligationCause::misc(body.span, hir_id),
87 let mut this = Inliner {
90 codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
95 let blocks = BasicBlock::new(0)..body.basic_blocks().next_index();
96 this.process_blocks(body, blocks);
100 struct Inliner<'tcx> {
102 param_env: ParamEnv<'tcx>,
103 /// Caller codegen attributes.
104 codegen_fn_attrs: &'tcx CodegenFnAttrs,
107 /// Stack of inlined Instances.
108 history: Vec<ty::Instance<'tcx>>,
109 /// Indicates that the caller body has been modified.
113 impl<'tcx> Inliner<'tcx> {
114 fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) {
116 let bb_data = &caller_body[bb];
117 if bb_data.is_cleanup {
121 let Some(callsite) = self.resolve_callsite(caller_body, bb, bb_data) else {
125 let span = trace_span!("process_blocks", %callsite.callee, ?bb);
126 let _guard = span.enter();
128 match self.try_inlining(caller_body, &callsite) {
130 debug!("not-inlined {} [{}]", callsite.callee, reason);
134 debug!("inlined {}", callsite.callee);
136 self.history.push(callsite.callee);
137 self.process_blocks(caller_body, new_blocks);
144 /// Attempts to inline a callsite into the caller body. When successful returns basic blocks
145 /// containing the inlined body. Otherwise returns an error describing why inlining didn't take
149 caller_body: &mut Body<'tcx>,
150 callsite: &CallSite<'tcx>,
151 ) -> Result<std::ops::Range<BasicBlock>, &'static str> {
152 let callee_attrs = self.tcx.codegen_fn_attrs(callsite.callee.def_id());
153 self.check_codegen_attributes(callsite, callee_attrs)?;
154 self.check_mir_is_available(caller_body, &callsite.callee)?;
155 let callee_body = self.tcx.instance_mir(callsite.callee.def);
156 self.check_mir_body(callsite, callee_body, callee_attrs)?;
158 if !self.tcx.consider_optimizing(|| {
159 format!("Inline {:?} into {:?}", callsite.callee, caller_body.source)
161 return Err("optimization fuel exhausted");
164 let callee_body = callsite.callee.subst_mir_and_normalize_erasing_regions(
170 let old_blocks = caller_body.basic_blocks().next_index();
171 self.inline_call(caller_body, &callsite, callee_body);
172 let new_blocks = old_blocks..caller_body.basic_blocks().next_index();
177 fn check_mir_is_available(
179 caller_body: &Body<'tcx>,
180 callee: &Instance<'tcx>,
181 ) -> Result<(), &'static str> {
182 if callee.def_id() == caller_body.source.def_id() {
183 return Err("self-recursion");
187 InstanceDef::Item(_) => {
188 // If there is no MIR available (either because it was not in metadata or
189 // because it has no MIR because it's an extern function), then the inliner
190 // won't cause cycles on this.
191 if !self.tcx.is_mir_available(callee.def_id()) {
192 return Err("item MIR unavailable");
195 // These have no own callable MIR.
196 InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => {
197 return Err("instance without MIR (intrinsic / virtual)");
199 // This cannot result in an immediate cycle since the callee MIR is a shim, which does
200 // not get any optimizations run on it. Any subsequent inlining may cause cycles, but we
201 // do not need to catch this here, we can wait until the inliner decides to continue
202 // inlining a second time.
203 InstanceDef::VtableShim(_)
204 | InstanceDef::ReifyShim(_)
205 | InstanceDef::FnPtrShim(..)
206 | InstanceDef::ClosureOnceShim { .. }
207 | InstanceDef::DropGlue(..)
208 | InstanceDef::CloneShim(..) => return Ok(()),
211 if self.tcx.is_constructor(callee.def_id()) {
212 trace!("constructors always have MIR");
213 // Constructor functions cannot cause a query cycle.
217 if let Some(callee_def_id) = callee.def_id().as_local() {
218 let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id);
219 // Avoid a cycle here by only using `instance_mir` only if we have
220 // a lower `HirId` than the callee. This ensures that the callee will
221 // not inline us. This trick only works without incremental compilation.
222 // So don't do it if that is enabled.
223 if !self.tcx.dep_graph.is_fully_enabled() && self.hir_id.index() < callee_hir_id.index()
228 // If we know for sure that the function we're calling will itself try to
229 // call us, then we avoid inlining that function.
232 .mir_callgraph_reachable((*callee, caller_body.source.def_id().expect_local()))
234 return Err("caller might be reachable from callee (query cycle avoidance)");
239 // This cannot result in an immediate cycle since the callee MIR is from another crate
240 // and is already optimized. Any subsequent inlining may cause cycles, but we do
241 // not need to catch this here, we can wait until the inliner decides to continue
242 // inlining a second time.
243 trace!("functions from other crates always have MIR");
250 caller_body: &Body<'tcx>,
252 bb_data: &BasicBlockData<'tcx>,
253 ) -> Option<CallSite<'tcx>> {
254 // Only consider direct calls to functions
255 let terminator = bb_data.terminator();
256 if let TerminatorKind::Call { ref func, ref destination, .. } = terminator.kind {
257 let func_ty = func.ty(caller_body, self.tcx);
258 if let ty::FnDef(def_id, substs) = *func_ty.kind() {
259 // To resolve an instance its substs have to be fully normalized.
260 let substs = self.tcx.normalize_erasing_regions(self.param_env, substs);
262 Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
264 if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
268 let fn_sig = self.tcx.fn_sig(def_id).subst(self.tcx, substs);
270 return Some(CallSite {
274 target: destination.map(|(_, target)| target),
275 source_info: terminator.source_info,
283 /// Returns an error if inlining is not possible based on codegen attributes alone. A success
284 /// indicates that inlining decision should be based on other criteria.
285 fn check_codegen_attributes(
287 callsite: &CallSite<'tcx>,
288 callee_attrs: &CodegenFnAttrs,
289 ) -> Result<(), &'static str> {
290 if let InlineAttr::Never = callee_attrs.inline {
291 return Err("never inline hint");
294 // Only inline local functions if they would be eligible for cross-crate
295 // inlining. This is to ensure that the final crate doesn't have MIR that
296 // reference unexported symbols
297 if callsite.callee.def_id().is_local() {
298 let is_generic = callsite.callee.substs.non_erasable_generics().next().is_some();
299 if !is_generic && !callee_attrs.requests_inline() {
300 return Err("not exported");
304 if callsite.fn_sig.c_variadic() {
305 return Err("C variadic");
308 if callee_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
312 if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
316 if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize {
317 return Err("incompatible sanitizer set");
320 if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set {
321 return Err("incompatible instruction set");
324 for feature in &callee_attrs.target_features {
325 if !self.codegen_fn_attrs.target_features.contains(feature) {
326 return Err("incompatible target feature");
333 /// Returns inlining decision that is based on the examination of callee MIR body.
334 /// Assumes that codegen attributes have been checked for compatibility already.
335 #[instrument(level = "debug", skip(self, callee_body))]
338 callsite: &CallSite<'tcx>,
339 callee_body: &Body<'tcx>,
340 callee_attrs: &CodegenFnAttrs,
341 ) -> Result<(), &'static str> {
344 let mut threshold = if callee_attrs.requests_inline() {
345 self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold.unwrap_or(100)
347 self.tcx.sess.opts.debugging_opts.inline_mir_threshold.unwrap_or(50)
350 // Give a bonus functions with a small number of blocks,
351 // We normally have two or three blocks for even
352 // very small functions.
353 if callee_body.basic_blocks().len() <= 3 {
354 threshold += threshold / 4;
356 debug!(" final inline threshold = {}", threshold);
358 // FIXME: Give a bonus to functions with only a single caller
359 let mut first_block = true;
362 // Traverse the MIR manually so we can account for the effects of
363 // inlining on the CFG.
364 let mut work_list = vec![START_BLOCK];
365 let mut visited = BitSet::new_empty(callee_body.basic_blocks().len());
366 while let Some(bb) = work_list.pop() {
367 if !visited.insert(bb.index()) {
370 let blk = &callee_body.basic_blocks()[bb];
372 for stmt in &blk.statements {
373 // Don't count StorageLive/StorageDead in the inlining cost.
375 StatementKind::StorageLive(_)
376 | StatementKind::StorageDead(_)
377 | StatementKind::Nop => {}
378 _ => cost += INSTR_COST,
381 let term = blk.terminator();
382 let mut is_drop = false;
384 TerminatorKind::Drop { ref place, target, unwind }
385 | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => {
387 work_list.push(target);
388 // If the place doesn't actually need dropping, treat it like
390 let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty);
391 if ty.needs_drop(tcx, self.param_env) {
392 cost += CALL_PENALTY;
393 if let Some(unwind) = unwind {
394 cost += LANDINGPAD_PENALTY;
395 work_list.push(unwind);
402 TerminatorKind::Unreachable | TerminatorKind::Call { destination: None, .. }
405 // If the function always diverges, don't inline
406 // unless the cost is zero
410 TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
411 if let ty::FnDef(def_id, substs) =
412 *callsite.callee.subst_mir(self.tcx, &f.literal.ty()).kind()
414 let substs = self.tcx.normalize_erasing_regions(self.param_env, substs);
415 if let Ok(Some(instance)) =
416 Instance::resolve(self.tcx, self.param_env, def_id, substs)
418 if callsite.callee.def_id() == instance.def_id() {
419 return Err("self-recursion");
420 } else if self.history.contains(&instance) {
421 return Err("already inlined");
424 // Don't give intrinsics the extra penalty for calls
425 let f = tcx.fn_sig(def_id);
426 if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic {
429 cost += CALL_PENALTY;
432 cost += CALL_PENALTY;
434 if cleanup.is_some() {
435 cost += LANDINGPAD_PENALTY;
438 TerminatorKind::Assert { cleanup, .. } => {
439 cost += CALL_PENALTY;
441 if cleanup.is_some() {
442 cost += LANDINGPAD_PENALTY;
445 TerminatorKind::Resume => cost += RESUME_PENALTY,
446 TerminatorKind::InlineAsm { cleanup, .. } => {
449 if cleanup.is_some() {
450 cost += LANDINGPAD_PENALTY;
453 _ => cost += INSTR_COST,
457 for &succ in term.successors() {
458 work_list.push(succ);
465 // Count up the cost of local variables and temps, if we know the size
466 // use that, otherwise we use a moderately-large dummy cost.
468 let ptr_size = tcx.data_layout.pointer_size.bytes();
470 for v in callee_body.vars_and_temps_iter() {
471 let ty = callsite.callee.subst_mir(self.tcx, &callee_body.local_decls[v].ty);
472 // Cost of the var is the size in machine-words, if we know
474 if let Some(size) = type_size_of(tcx, self.param_env, ty) {
475 cost += ((size + ptr_size - 1) / ptr_size) as usize;
477 cost += UNKNOWN_SIZE_COST;
481 if let InlineAttr::Always = callee_attrs.inline {
482 debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
485 if cost <= threshold {
486 debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
489 debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
490 Err("cost above threshold")
497 caller_body: &mut Body<'tcx>,
498 callsite: &CallSite<'tcx>,
499 mut callee_body: Body<'tcx>,
501 let terminator = caller_body[callsite.block].terminator.take().unwrap();
502 match terminator.kind {
503 TerminatorKind::Call { args, destination, cleanup, .. } => {
504 // If the call is something like `a[*i] = f(i)`, where
505 // `i : &mut usize`, then just duplicating the `a[*i]`
506 // Place could result in two different locations if `f`
507 // writes to `i`. To prevent this we need to create a temporary
508 // borrow of the place and pass the destination as `*temp` instead.
509 fn dest_needs_borrow(place: Place<'_>) -> bool {
510 for elem in place.projection.iter() {
512 ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
520 let dest = if let Some((destination_place, _)) = destination {
521 if dest_needs_borrow(destination_place) {
522 trace!("creating temp for return destination");
523 let dest = Rvalue::Ref(
524 self.tcx.lifetimes.re_erased,
525 BorrowKind::Mut { allow_two_phase_borrow: false },
528 let dest_ty = dest.ty(caller_body, self.tcx);
529 let temp = Place::from(self.new_call_temp(caller_body, &callsite, dest_ty));
530 caller_body[callsite.block].statements.push(Statement {
531 source_info: callsite.source_info,
532 kind: StatementKind::Assign(Box::new((temp, dest))),
534 self.tcx.mk_place_deref(temp)
539 trace!("creating temp for return place");
540 Place::from(self.new_call_temp(caller_body, &callsite, callee_body.return_ty()))
543 // Copy the arguments if needed.
544 let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);
546 let mut expn_data = ExpnData::default(
548 callsite.source_info.span,
549 self.tcx.sess.edition(),
553 expn_data.def_site = callee_body.span;
555 LocalExpnId::fresh(expn_data, self.tcx.create_stable_hashing_context());
556 let mut integrator = Integrator {
558 new_locals: Local::new(caller_body.local_decls.len())..,
559 new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
560 new_blocks: BasicBlock::new(caller_body.basic_blocks().len())..,
562 return_block: callsite.target,
563 cleanup_block: cleanup,
564 in_cleanup_block: false,
567 always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
570 // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
571 // (or existing ones, in a few special cases) in the caller.
572 integrator.visit_body(&mut callee_body);
574 for scope in &mut callee_body.source_scopes {
575 // FIXME(eddyb) move this into a `fn visit_scope_data` in `Integrator`.
576 if scope.parent_scope.is_none() {
577 let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope];
579 // Attach the outermost callee scope as a child of the callsite
580 // scope, via the `parent_scope` and `inlined_parent_scope` chains.
581 scope.parent_scope = Some(callsite.source_info.scope);
582 assert_eq!(scope.inlined_parent_scope, None);
583 scope.inlined_parent_scope = if callsite_scope.inlined.is_some() {
584 Some(callsite.source_info.scope)
586 callsite_scope.inlined_parent_scope
589 // Mark the outermost callee scope as an inlined one.
590 assert_eq!(scope.inlined, None);
591 scope.inlined = Some((callsite.callee, callsite.source_info.span));
592 } else if scope.inlined_parent_scope.is_none() {
593 // Make it easy to find the scope with `inlined` set above.
594 scope.inlined_parent_scope =
595 Some(integrator.map_scope(OUTERMOST_SOURCE_SCOPE));
599 // If there are any locals without storage markers, give them storage only for the
600 // duration of the call.
601 for local in callee_body.vars_and_temps_iter() {
602 if integrator.always_live_locals.contains(local) {
603 let new_local = integrator.map_local(local);
604 caller_body[callsite.block].statements.push(Statement {
605 source_info: callsite.source_info,
606 kind: StatementKind::StorageLive(new_local),
610 if let Some(block) = callsite.target {
611 // To avoid repeated O(n) insert, push any new statements to the end and rotate
614 for local in callee_body.vars_and_temps_iter().rev() {
615 if integrator.always_live_locals.contains(local) {
616 let new_local = integrator.map_local(local);
617 caller_body[block].statements.push(Statement {
618 source_info: callsite.source_info,
619 kind: StatementKind::StorageDead(new_local),
624 caller_body[block].statements.rotate_right(n);
627 // Insert all of the (mapped) parts of the callee body into the caller.
628 caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
629 caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..));
630 caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
631 caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..));
633 caller_body[callsite.block].terminator = Some(Terminator {
634 source_info: callsite.source_info,
635 kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
638 // Copy only unevaluated constants from the callee_body into the caller_body.
639 // Although we are only pushing `ConstKind::Unevaluated` consts to
640 // `required_consts`, here we may not only have `ConstKind::Unevaluated`
641 // because we are calling `subst_and_normalize_erasing_regions`.
642 caller_body.required_consts.extend(
643 callee_body.required_consts.iter().copied().filter(|&ct| {
644 match ct.literal.const_for_ty() {
645 Some(ct) => matches!(ct.val(), ConstKind::Unevaluated(_)),
651 kind => bug!("unexpected terminator kind {:?}", kind),
657 args: Vec<Operand<'tcx>>,
658 callsite: &CallSite<'tcx>,
659 caller_body: &mut Body<'tcx>,
660 callee_body: &Body<'tcx>,
664 // There is a bit of a mismatch between the *caller* of a closure and the *callee*.
665 // The caller provides the arguments wrapped up in a tuple:
667 // tuple_tmp = (a, b, c)
668 // Fn::call(closure_ref, tuple_tmp)
670 // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
671 // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
672 // the job of unpacking this tuple. But here, we are codegen. =) So we want to create
675 // [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
677 // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
678 // if we "spill" that into *another* temporary, so that we can map the argument
679 // variable in the callee MIR directly to an argument variable on our side.
680 // So we introduce temporaries like:
682 // tmp0 = tuple_tmp.0
683 // tmp1 = tuple_tmp.1
684 // tmp2 = tuple_tmp.2
686 // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
687 if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
688 let mut args = args.into_iter();
689 let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
690 let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
691 assert!(args.next().is_none());
693 let tuple = Place::from(tuple);
694 let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
695 bug!("Closure arguments are not passed as a tuple");
698 // The `closure_ref` in our example above.
699 let closure_ref_arg = iter::once(self_);
701 // The `tmp0`, `tmp1`, and `tmp2` in our example abonve.
702 let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
703 // This is e.g., `tuple_tmp.0` in our example above.
704 let tuple_field = Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty));
706 // Spill to a local to make e.g., `tmp0`.
707 self.create_temp_if_necessary(tuple_field, callsite, caller_body)
710 closure_ref_arg.chain(tuple_tmp_args).collect()
713 .map(|a| self.create_temp_if_necessary(a, callsite, caller_body))
718 /// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh
719 /// temporary `T` and an instruction `T = arg`, and returns `T`.
720 fn create_temp_if_necessary(
723 callsite: &CallSite<'tcx>,
724 caller_body: &mut Body<'tcx>,
726 // Reuse the operand if it is a moved temporary.
727 if let Operand::Move(place) = &arg
728 && let Some(local) = place.as_local()
729 && caller_body.local_kind(local) == LocalKind::Temp
734 // Otherwise, create a temporary for the argument.
735 trace!("creating temp for argument {:?}", arg);
736 let arg_ty = arg.ty(caller_body, self.tcx);
737 let local = self.new_call_temp(caller_body, callsite, arg_ty);
738 caller_body[callsite.block].statements.push(Statement {
739 source_info: callsite.source_info,
740 kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))),
745 /// Introduces a new temporary into the caller body that is live for the duration of the call.
748 caller_body: &mut Body<'tcx>,
749 callsite: &CallSite<'tcx>,
752 let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
754 caller_body[callsite.block].statements.push(Statement {
755 source_info: callsite.source_info,
756 kind: StatementKind::StorageLive(local),
759 if let Some(block) = callsite.target {
760 caller_body[block].statements.insert(
763 source_info: callsite.source_info,
764 kind: StatementKind::StorageDead(local),
773 fn type_size_of<'tcx>(
775 param_env: ty::ParamEnv<'tcx>,
778 tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
784 * Integrates blocks from the callee function into the calling function.
785 * Updates block indices, references to locals and other control flow
788 struct Integrator<'a, 'tcx> {
790 new_locals: RangeFrom<Local>,
791 new_scopes: RangeFrom<SourceScope>,
792 new_blocks: RangeFrom<BasicBlock>,
793 destination: Place<'tcx>,
794 return_block: Option<BasicBlock>,
795 cleanup_block: Option<BasicBlock>,
796 in_cleanup_block: bool,
798 expn_data: LocalExpnId,
799 always_live_locals: BitSet<Local>,
802 impl Integrator<'_, '_> {
803 fn map_local(&self, local: Local) -> Local {
804 let new = if local == RETURN_PLACE {
805 self.destination.local
807 let idx = local.index() - 1;
808 if idx < self.args.len() {
811 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
814 trace!("mapping local `{:?}` to `{:?}`", local, new);
818 fn map_scope(&self, scope: SourceScope) -> SourceScope {
819 let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
820 trace!("mapping scope `{:?}` to `{:?}`", scope, new);
824 fn map_block(&self, block: BasicBlock) -> BasicBlock {
825 let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
826 trace!("mapping block `{:?}` to `{:?}`", block, new);
831 impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
832 fn tcx(&self) -> TyCtxt<'tcx> {
836 fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
837 *local = self.map_local(*local);
840 fn visit_source_scope(&mut self, scope: &mut SourceScope) {
841 *scope = self.map_scope(*scope);
844 fn visit_span(&mut self, span: &mut Span) {
845 // Make sure that all spans track the fact that they were inlined.
846 *span = span.fresh_expansion(self.expn_data);
849 fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
850 for elem in place.projection {
851 // FIXME: Make sure that return place is not used in an indexing projection, since it
852 // won't be rebased as it is supposed to be.
853 assert_ne!(ProjectionElem::Index(RETURN_PLACE), elem);
856 // If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
857 let dest_proj_len = self.destination.projection.len();
858 if place.local == RETURN_PLACE && dest_proj_len > 0 {
859 let mut projs = Vec::with_capacity(dest_proj_len + place.projection.len());
860 projs.extend(self.destination.projection);
861 projs.extend(place.projection);
863 place.projection = self.tcx.intern_place_elems(&*projs);
865 // Handles integrating any locals that occur in the base
867 self.super_place(place, context, location)
870 fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
871 self.in_cleanup_block = data.is_cleanup;
872 self.super_basic_block_data(block, data);
873 self.in_cleanup_block = false;
876 fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
877 self.super_retag(kind, place, loc);
879 // We have to patch all inlined retags to be aware that they are no longer
880 // happening on function entry.
881 if *kind == RetagKind::FnEntry {
882 *kind = RetagKind::Default;
886 fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
887 if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
890 self.always_live_locals.remove(local);
892 self.super_statement(statement, location);
895 fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
896 // Don't try to modify the implicit `_0` access on return (`return` terminators are
897 // replaced down below anyways).
898 if !matches!(terminator.kind, TerminatorKind::Return) {
899 self.super_terminator(terminator, loc);
902 match terminator.kind {
903 TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
904 TerminatorKind::Goto { ref mut target } => {
905 *target = self.map_block(*target);
907 TerminatorKind::SwitchInt { ref mut targets, .. } => {
908 for tgt in targets.all_targets_mut() {
909 *tgt = self.map_block(*tgt);
912 TerminatorKind::Drop { ref mut target, ref mut unwind, .. }
913 | TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
914 *target = self.map_block(*target);
915 if let Some(tgt) = *unwind {
916 *unwind = Some(self.map_block(tgt));
917 } else if !self.in_cleanup_block {
918 // Unless this drop is in a cleanup block, add an unwind edge to
919 // the original call's cleanup block
920 *unwind = self.cleanup_block;
923 TerminatorKind::Call { ref mut destination, ref mut cleanup, .. } => {
924 if let Some((_, ref mut tgt)) = *destination {
925 *tgt = self.map_block(*tgt);
927 if let Some(tgt) = *cleanup {
928 *cleanup = Some(self.map_block(tgt));
929 } else if !self.in_cleanup_block {
930 // Unless this call is in a cleanup block, add an unwind edge to
931 // the original call's cleanup block
932 *cleanup = self.cleanup_block;
935 TerminatorKind::Assert { ref mut target, ref mut cleanup, .. } => {
936 *target = self.map_block(*target);
937 if let Some(tgt) = *cleanup {
938 *cleanup = Some(self.map_block(tgt));
939 } else if !self.in_cleanup_block {
940 // Unless this assert is in a cleanup block, add an unwind edge to
941 // the original call's cleanup block
942 *cleanup = self.cleanup_block;
945 TerminatorKind::Return => {
946 terminator.kind = if let Some(tgt) = self.return_block {
947 TerminatorKind::Goto { target: tgt }
949 TerminatorKind::Unreachable
952 TerminatorKind::Resume => {
953 if let Some(tgt) = self.cleanup_block {
954 terminator.kind = TerminatorKind::Goto { target: tgt }
957 TerminatorKind::Abort => {}
958 TerminatorKind::Unreachable => {}
959 TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
960 *real_target = self.map_block(*real_target);
961 *imaginary_target = self.map_block(*imaginary_target);
963 TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
964 // see the ordering of passes in the optimized_mir query.
966 bug!("False unwinds should have been removed before inlining")
968 TerminatorKind::InlineAsm { ref mut destination, ref mut cleanup, .. } => {
969 if let Some(ref mut tgt) = *destination {
970 *tgt = self.map_block(*tgt);
971 } else if !self.in_cleanup_block {
972 // Unless this inline asm is in a cleanup block, add an unwind edge to
973 // the original call's cleanup block
974 *cleanup = self.cleanup_block;