]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/inline.rs
Auto merge of #84944 - lcnr:obligation-dedup, r=jackh726
[rust.git] / compiler / rustc_mir_transform / src / inline.rs
1 //! Inlining pass for MIR functions
2
3 use rustc_attr::InlineAttr;
4 use rustc_hir as hir;
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;
15
16 use super::simplify::{remove_dead_blocks, CfgSimplifier};
17 use crate::MirPass;
18 use std::iter;
19 use std::ops::{Range, RangeFrom};
20
21 crate mod cycle;
22
23 const INSTR_COST: usize = 5;
24 const CALL_PENALTY: usize = 25;
25 const LANDINGPAD_PENALTY: usize = 50;
26 const RESUME_PENALTY: usize = 45;
27
28 const UNKNOWN_SIZE_COST: usize = 10;
29
30 pub struct Inline;
31
32 #[derive(Copy, Clone, Debug)]
33 struct CallSite<'tcx> {
34     callee: Instance<'tcx>,
35     fn_sig: ty::PolyFnSig<'tcx>,
36     block: BasicBlock,
37     target: Option<BasicBlock>,
38     source_info: SourceInfo,
39 }
40
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 {
44             return enabled;
45         }
46
47         sess.opts.mir_opt_level() >= 3
48     }
49
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);
57         }
58     }
59 }
60
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());
64
65     // Only do inlining into fn bodies.
66     if !tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() {
67         return false;
68     }
69     if body.source.promoted.is_some() {
70         return false;
71     }
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
74     // direction.
75     if body.generator.is_some() {
76         return false;
77     }
78
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(
81         tcx,
82         def_id,
83         param_env,
84         ObligationCause::misc(body.span, hir_id),
85     );
86
87     let mut this = Inliner {
88         tcx,
89         param_env,
90         codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
91         hir_id,
92         history: Vec::new(),
93         changed: false,
94     };
95     let blocks = BasicBlock::new(0)..body.basic_blocks().next_index();
96     this.process_blocks(body, blocks);
97     this.changed
98 }
99
100 struct Inliner<'tcx> {
101     tcx: TyCtxt<'tcx>,
102     param_env: ParamEnv<'tcx>,
103     /// Caller codegen attributes.
104     codegen_fn_attrs: &'tcx CodegenFnAttrs,
105     /// Caller HirID.
106     hir_id: hir::HirId,
107     /// Stack of inlined Instances.
108     history: Vec<ty::Instance<'tcx>>,
109     /// Indicates that the caller body has been modified.
110     changed: bool,
111 }
112
113 impl<'tcx> Inliner<'tcx> {
114     fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) {
115         for bb in blocks {
116             let bb_data = &caller_body[bb];
117             if bb_data.is_cleanup {
118                 continue;
119             }
120
121             let Some(callsite) = self.resolve_callsite(caller_body, bb, bb_data) else {
122                 continue;
123             };
124
125             let span = trace_span!("process_blocks", %callsite.callee, ?bb);
126             let _guard = span.enter();
127
128             match self.try_inlining(caller_body, &callsite) {
129                 Err(reason) => {
130                     debug!("not-inlined {} [{}]", callsite.callee, reason);
131                     continue;
132                 }
133                 Ok(new_blocks) => {
134                     debug!("inlined {}", callsite.callee);
135                     self.changed = true;
136                     self.history.push(callsite.callee);
137                     self.process_blocks(caller_body, new_blocks);
138                     self.history.pop();
139                 }
140             }
141         }
142     }
143
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
146     /// place.
147     fn try_inlining(
148         &self,
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)?;
157
158         if !self.tcx.consider_optimizing(|| {
159             format!("Inline {:?} into {:?}", callsite.callee, caller_body.source)
160         }) {
161             return Err("optimization fuel exhausted");
162         }
163
164         let callee_body = callsite.callee.subst_mir_and_normalize_erasing_regions(
165             self.tcx,
166             self.param_env,
167             callee_body.clone(),
168         );
169
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();
173
174         Ok(new_blocks)
175     }
176
177     fn check_mir_is_available(
178         &self,
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");
184         }
185
186         match callee.def {
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");
193                 }
194             }
195             // These have no own callable MIR.
196             InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => {
197                 return Err("instance without MIR (intrinsic / virtual)");
198             }
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(()),
209         }
210
211         if self.tcx.is_constructor(callee.def_id()) {
212             trace!("constructors always have MIR");
213             // Constructor functions cannot cause a query cycle.
214             return Ok(());
215         }
216
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()
224             {
225                 return Ok(());
226             }
227
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.
230             if self
231                 .tcx
232                 .mir_callgraph_reachable((*callee, caller_body.source.def_id().expect_local()))
233             {
234                 return Err("caller might be reachable from callee (query cycle avoidance)");
235             }
236
237             Ok(())
238         } else {
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");
244             Ok(())
245         }
246     }
247
248     fn resolve_callsite(
249         &self,
250         caller_body: &Body<'tcx>,
251         bb: BasicBlock,
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);
261                 let callee =
262                     Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
263
264                 if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
265                     return None;
266                 }
267
268                 let fn_sig = self.tcx.fn_sig(def_id).subst(self.tcx, substs);
269
270                 return Some(CallSite {
271                     callee,
272                     fn_sig,
273                     block: bb,
274                     target: destination.map(|(_, target)| target),
275                     source_info: terminator.source_info,
276                 });
277             }
278         }
279
280         None
281     }
282
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(
286         &self,
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");
292         }
293
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");
301             }
302         }
303
304         if callsite.fn_sig.c_variadic() {
305             return Err("C variadic");
306         }
307
308         if callee_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
309             return Err("naked");
310         }
311
312         if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
313             return Err("cold");
314         }
315
316         if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize {
317             return Err("incompatible sanitizer set");
318         }
319
320         if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set {
321             return Err("incompatible instruction set");
322         }
323
324         for feature in &callee_attrs.target_features {
325             if !self.codegen_fn_attrs.target_features.contains(feature) {
326                 return Err("incompatible target feature");
327             }
328         }
329
330         Ok(())
331     }
332
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))]
336     fn check_mir_body(
337         &self,
338         callsite: &CallSite<'tcx>,
339         callee_body: &Body<'tcx>,
340         callee_attrs: &CodegenFnAttrs,
341     ) -> Result<(), &'static str> {
342         let tcx = self.tcx;
343
344         let mut threshold = if callee_attrs.requests_inline() {
345             self.tcx.sess.opts.debugging_opts.inline_mir_hint_threshold.unwrap_or(100)
346         } else {
347             self.tcx.sess.opts.debugging_opts.inline_mir_threshold.unwrap_or(50)
348         };
349
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;
355         }
356         debug!("    final inline threshold = {}", threshold);
357
358         // FIXME: Give a bonus to functions with only a single caller
359         let mut first_block = true;
360         let mut cost = 0;
361
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()) {
368                 continue;
369             }
370             let blk = &callee_body.basic_blocks()[bb];
371
372             for stmt in &blk.statements {
373                 // Don't count StorageLive/StorageDead in the inlining cost.
374                 match stmt.kind {
375                     StatementKind::StorageLive(_)
376                     | StatementKind::StorageDead(_)
377                     | StatementKind::Nop => {}
378                     _ => cost += INSTR_COST,
379                 }
380             }
381             let term = blk.terminator();
382             let mut is_drop = false;
383             match term.kind {
384                 TerminatorKind::Drop { ref place, target, unwind }
385                 | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => {
386                     is_drop = true;
387                     work_list.push(target);
388                     // If the place doesn't actually need dropping, treat it like
389                     // a regular goto.
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);
396                         }
397                     } else {
398                         cost += INSTR_COST;
399                     }
400                 }
401
402                 TerminatorKind::Unreachable | TerminatorKind::Call { destination: None, .. }
403                     if first_block =>
404                 {
405                     // If the function always diverges, don't inline
406                     // unless the cost is zero
407                     threshold = 0;
408                 }
409
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()
413                     {
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)
417                         {
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");
422                             }
423                         }
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 {
427                             cost += INSTR_COST;
428                         } else {
429                             cost += CALL_PENALTY;
430                         }
431                     } else {
432                         cost += CALL_PENALTY;
433                     }
434                     if cleanup.is_some() {
435                         cost += LANDINGPAD_PENALTY;
436                     }
437                 }
438                 TerminatorKind::Assert { cleanup, .. } => {
439                     cost += CALL_PENALTY;
440
441                     if cleanup.is_some() {
442                         cost += LANDINGPAD_PENALTY;
443                     }
444                 }
445                 TerminatorKind::Resume => cost += RESUME_PENALTY,
446                 TerminatorKind::InlineAsm { cleanup, .. } => {
447                     cost += INSTR_COST;
448
449                     if cleanup.is_some() {
450                         cost += LANDINGPAD_PENALTY;
451                     }
452                 }
453                 _ => cost += INSTR_COST,
454             }
455
456             if !is_drop {
457                 for &succ in term.successors() {
458                     work_list.push(succ);
459                 }
460             }
461
462             first_block = false;
463         }
464
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.
467
468         let ptr_size = tcx.data_layout.pointer_size.bytes();
469
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
473             // it.
474             if let Some(size) = type_size_of(tcx, self.param_env, ty) {
475                 cost += ((size + ptr_size - 1) / ptr_size) as usize;
476             } else {
477                 cost += UNKNOWN_SIZE_COST;
478             }
479         }
480
481         if let InlineAttr::Always = callee_attrs.inline {
482             debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
483             Ok(())
484         } else {
485             if cost <= threshold {
486                 debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
487                 Ok(())
488             } else {
489                 debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
490                 Err("cost above threshold")
491             }
492         }
493     }
494
495     fn inline_call(
496         &self,
497         caller_body: &mut Body<'tcx>,
498         callsite: &CallSite<'tcx>,
499         mut callee_body: Body<'tcx>,
500     ) {
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() {
511                         match elem {
512                             ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
513                             _ => {}
514                         }
515                     }
516
517                     false
518                 }
519
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 },
526                             destination_place,
527                         );
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))),
533                         });
534                         self.tcx.mk_place_deref(temp)
535                     } else {
536                         destination_place
537                     }
538                 } else {
539                     trace!("creating temp for return place");
540                     Place::from(self.new_call_temp(caller_body, &callsite, callee_body.return_ty()))
541                 };
542
543                 // Copy the arguments if needed.
544                 let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);
545
546                 let mut expn_data = ExpnData::default(
547                     ExpnKind::Inlined,
548                     callsite.source_info.span,
549                     self.tcx.sess.edition(),
550                     None,
551                     None,
552                 );
553                 expn_data.def_site = callee_body.span;
554                 let expn_data =
555                     LocalExpnId::fresh(expn_data, self.tcx.create_stable_hashing_context());
556                 let mut integrator = Integrator {
557                     args: &args,
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())..,
561                     destination: dest,
562                     return_block: callsite.target,
563                     cleanup_block: cleanup,
564                     in_cleanup_block: false,
565                     tcx: self.tcx,
566                     expn_data,
567                     always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
568                 };
569
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);
573
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];
578
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)
585                         } else {
586                             callsite_scope.inlined_parent_scope
587                         };
588
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));
596                     }
597                 }
598
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),
607                         });
608                     }
609                 }
610                 if let Some(block) = callsite.target {
611                     // To avoid repeated O(n) insert, push any new statements to the end and rotate
612                     // the slice once.
613                     let mut n = 0;
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),
620                             });
621                             n += 1;
622                         }
623                     }
624                     caller_body[block].statements.rotate_right(n);
625                 }
626
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(..));
632
633                 caller_body[callsite.block].terminator = Some(Terminator {
634                     source_info: callsite.source_info,
635                     kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
636                 });
637
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(_)),
646                             None => true,
647                         }
648                     }),
649                 );
650             }
651             kind => bug!("unexpected terminator kind {:?}", kind),
652         }
653     }
654
655     fn make_call_args(
656         &self,
657         args: Vec<Operand<'tcx>>,
658         callsite: &CallSite<'tcx>,
659         caller_body: &mut Body<'tcx>,
660         callee_body: &Body<'tcx>,
661     ) -> Vec<Local> {
662         let tcx = self.tcx;
663
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:
666         //
667         //     tuple_tmp = (a, b, c)
668         //     Fn::call(closure_ref, tuple_tmp)
669         //
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
673         // a vector like
674         //
675         //     [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
676         //
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:
681         //
682         //     tmp0 = tuple_tmp.0
683         //     tmp1 = tuple_tmp.1
684         //     tmp2 = tuple_tmp.2
685         //
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());
692
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");
696             };
697
698             // The `closure_ref` in our example above.
699             let closure_ref_arg = iter::once(self_);
700
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));
705
706                 // Spill to a local to make e.g., `tmp0`.
707                 self.create_temp_if_necessary(tuple_field, callsite, caller_body)
708             });
709
710             closure_ref_arg.chain(tuple_tmp_args).collect()
711         } else {
712             args.into_iter()
713                 .map(|a| self.create_temp_if_necessary(a, callsite, caller_body))
714                 .collect()
715         }
716     }
717
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(
721         &self,
722         arg: Operand<'tcx>,
723         callsite: &CallSite<'tcx>,
724         caller_body: &mut Body<'tcx>,
725     ) -> Local {
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
730         {
731             return local;
732         }
733
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)))),
741         });
742         local
743     }
744
745     /// Introduces a new temporary into the caller body that is live for the duration of the call.
746     fn new_call_temp(
747         &self,
748         caller_body: &mut Body<'tcx>,
749         callsite: &CallSite<'tcx>,
750         ty: Ty<'tcx>,
751     ) -> Local {
752         let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
753
754         caller_body[callsite.block].statements.push(Statement {
755             source_info: callsite.source_info,
756             kind: StatementKind::StorageLive(local),
757         });
758
759         if let Some(block) = callsite.target {
760             caller_body[block].statements.insert(
761                 0,
762                 Statement {
763                     source_info: callsite.source_info,
764                     kind: StatementKind::StorageDead(local),
765                 },
766             );
767         }
768
769         local
770     }
771 }
772
773 fn type_size_of<'tcx>(
774     tcx: TyCtxt<'tcx>,
775     param_env: ty::ParamEnv<'tcx>,
776     ty: Ty<'tcx>,
777 ) -> Option<u64> {
778     tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
779 }
780
781 /**
782  * Integrator.
783  *
784  * Integrates blocks from the callee function into the calling function.
785  * Updates block indices, references to locals and other control flow
786  * stuff.
787 */
788 struct Integrator<'a, 'tcx> {
789     args: &'a [Local],
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,
797     tcx: TyCtxt<'tcx>,
798     expn_data: LocalExpnId,
799     always_live_locals: BitSet<Local>,
800 }
801
802 impl Integrator<'_, '_> {
803     fn map_local(&self, local: Local) -> Local {
804         let new = if local == RETURN_PLACE {
805             self.destination.local
806         } else {
807             let idx = local.index() - 1;
808             if idx < self.args.len() {
809                 self.args[idx]
810             } else {
811                 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
812             }
813         };
814         trace!("mapping local `{:?}` to `{:?}`", local, new);
815         new
816     }
817
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);
821         new
822     }
823
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);
827         new
828     }
829 }
830
831 impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
832     fn tcx(&self) -> TyCtxt<'tcx> {
833         self.tcx
834     }
835
836     fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
837         *local = self.map_local(*local);
838     }
839
840     fn visit_source_scope(&mut self, scope: &mut SourceScope) {
841         *scope = self.map_scope(*scope);
842     }
843
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);
847     }
848
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);
854         }
855
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);
862
863             place.projection = self.tcx.intern_place_elems(&*projs);
864         }
865         // Handles integrating any locals that occur in the base
866         // or projections
867         self.super_place(place, context, location)
868     }
869
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;
874     }
875
876     fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
877         self.super_retag(kind, place, loc);
878
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;
883         }
884     }
885
886     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
887         if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
888             statement.kind
889         {
890             self.always_live_locals.remove(local);
891         }
892         self.super_statement(statement, location);
893     }
894
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);
900         }
901
902         match terminator.kind {
903             TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
904             TerminatorKind::Goto { ref mut target } => {
905                 *target = self.map_block(*target);
906             }
907             TerminatorKind::SwitchInt { ref mut targets, .. } => {
908                 for tgt in targets.all_targets_mut() {
909                     *tgt = self.map_block(*tgt);
910                 }
911             }
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;
921                 }
922             }
923             TerminatorKind::Call { ref mut destination, ref mut cleanup, .. } => {
924                 if let Some((_, ref mut tgt)) = *destination {
925                     *tgt = self.map_block(*tgt);
926                 }
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;
933                 }
934             }
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;
943                 }
944             }
945             TerminatorKind::Return => {
946                 terminator.kind = if let Some(tgt) = self.return_block {
947                     TerminatorKind::Goto { target: tgt }
948                 } else {
949                     TerminatorKind::Unreachable
950                 }
951             }
952             TerminatorKind::Resume => {
953                 if let Some(tgt) = self.cleanup_block {
954                     terminator.kind = TerminatorKind::Goto { target: tgt }
955                 }
956             }
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);
962             }
963             TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
964             // see the ordering of passes in the optimized_mir query.
965             {
966                 bug!("False unwinds should have been removed before inlining")
967             }
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;
975                 }
976             }
977         }
978     }
979 }