]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/inline.rs
Auto merge of #93397 - joshtriplett:sort-floats, r=Amanieu
[rust.git] / compiler / rustc_mir_transform / src / inline.rs
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;
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 pub(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.unstable_opts.inline_mir {
44             return enabled;
45         }
46
47         match sess.mir_opt_level() {
48             0 | 1 => false,
49             2 => {
50                 (sess.opts.optimize == OptLevel::Default
51                     || sess.opts.optimize == OptLevel::Aggressive)
52                     && sess.opts.incremental == None
53             }
54             _ => true,
55         }
56     }
57
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);
66         }
67     }
68 }
69
70 fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
71     let def_id = body.source.def_id().expect_local();
72
73     // Only do inlining into fn bodies.
74     if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() {
75         return false;
76     }
77     if body.source.promoted.is_some() {
78         return false;
79     }
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
82     // direction.
83     if body.generator.is_some() {
84         return false;
85     }
86
87     let param_env = tcx.param_env_reveal_all_normalized(def_id);
88
89     let mut this = Inliner {
90         tcx,
91         param_env,
92         codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
93         history: Vec::new(),
94         changed: false,
95     };
96     let blocks = BasicBlock::new(0)..body.basic_blocks().next_index();
97     this.process_blocks(body, blocks);
98     this.changed
99 }
100
101 struct Inliner<'tcx> {
102     tcx: TyCtxt<'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.
109     changed: bool,
110 }
111
112 impl<'tcx> Inliner<'tcx> {
113     fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) {
114         for bb in blocks {
115             let bb_data = &caller_body[bb];
116             if bb_data.is_cleanup {
117                 continue;
118             }
119
120             let Some(callsite) = self.resolve_callsite(caller_body, bb, bb_data) else {
121                 continue;
122             };
123
124             let span = trace_span!("process_blocks", %callsite.callee, ?bb);
125             let _guard = span.enter();
126
127             match self.try_inlining(caller_body, &callsite) {
128                 Err(reason) => {
129                     debug!("not-inlined {} [{}]", callsite.callee, reason);
130                     continue;
131                 }
132                 Ok(new_blocks) => {
133                     debug!("inlined {}", callsite.callee);
134                     self.changed = true;
135                     self.history.push(callsite.callee);
136                     self.process_blocks(caller_body, new_blocks);
137                     self.history.pop();
138                 }
139             }
140         }
141     }
142
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
145     /// place.
146     fn try_inlining(
147         &self,
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)?;
156
157         if !self.tcx.consider_optimizing(|| {
158             format!("Inline {:?} into {:?}", callsite.callee, caller_body.source)
159         }) {
160             return Err("optimization fuel exhausted");
161         }
162
163         let Ok(callee_body) = callsite.callee.try_subst_mir_and_normalize_erasing_regions(
164             self.tcx,
165             self.param_env,
166             callee_body.clone(),
167         ) else {
168             return Err("failed to normalize callee body");
169         };
170
171         // Check call signature compatibility.
172         // Normally, this shouldn't be required, but trait normalization failure can create a
173         // validation ICE.
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");
181         }
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"),
187             };
188
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");
192             };
193
194             for (arg_ty, input) in
195                 arg_tuple_tys.iter().zip(callee_body.args_iter().skip(skipped_args))
196             {
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");
201                 }
202             }
203         } else {
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");
210                 }
211             }
212         }
213
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();
217
218         Ok(new_blocks)
219     }
220
221     fn check_mir_is_available(
222         &self,
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");
230         }
231
232         match callee.def {
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");
239                 }
240             }
241             // These have no own callable MIR.
242             InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => {
243                 return Err("instance without MIR (intrinsic / virtual)");
244             }
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(()),
255         }
256
257         if self.tcx.is_constructor(callee_def_id) {
258             trace!("constructors always have MIR");
259             // Constructor functions cannot cause a query cycle.
260             return Ok(());
261         }
262
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()
270             {
271                 return Ok(());
272             }
273
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)");
278             }
279
280             Ok(())
281         } else {
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");
287             Ok(())
288         }
289     }
290
291     fn resolve_callsite(
292         &self,
293         caller_body: &Body<'tcx>,
294         bb: BasicBlock,
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()?;
304                 let callee =
305                     Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
306
307                 if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
308                     return None;
309                 }
310
311                 if self.history.contains(&callee) {
312                     return None;
313                 }
314
315                 let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, substs);
316
317                 return Some(CallSite {
318                     callee,
319                     fn_sig,
320                     block: bb,
321                     target,
322                     source_info: terminator.source_info,
323                 });
324             }
325         }
326
327         None
328     }
329
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(
333         &self,
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");
343                 }
344             }
345         }
346
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");
354             }
355         }
356
357         if callsite.fn_sig.c_variadic() {
358             return Err("C variadic");
359         }
360
361         if callee_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
362             return Err("naked");
363         }
364
365         if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
366             return Err("cold");
367         }
368
369         if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize {
370             return Err("incompatible sanitizer set");
371         }
372
373         if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set {
374             return Err("incompatible instruction set");
375         }
376
377         for feature in &callee_attrs.target_features {
378             if !self.codegen_fn_attrs.target_features.contains(feature) {
379                 return Err("incompatible target feature");
380             }
381         }
382
383         Ok(())
384     }
385
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))]
389     fn check_mir_body(
390         &self,
391         callsite: &CallSite<'tcx>,
392         callee_body: &Body<'tcx>,
393         callee_attrs: &CodegenFnAttrs,
394     ) -> Result<(), &'static str> {
395         let tcx = self.tcx;
396
397         let mut threshold = if callee_attrs.requests_inline() {
398             self.tcx.sess.opts.unstable_opts.inline_mir_hint_threshold.unwrap_or(100)
399         } else {
400             self.tcx.sess.opts.unstable_opts.inline_mir_threshold.unwrap_or(50)
401         };
402
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;
408         }
409         debug!("    final inline threshold = {}", threshold);
410
411         // FIXME: Give a bonus to functions with only a single caller
412         let mut first_block = true;
413         let mut cost = 0;
414
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()) {
421                 continue;
422             }
423             let blk = &callee_body.basic_blocks()[bb];
424
425             for stmt in &blk.statements {
426                 // Don't count StorageLive/StorageDead in the inlining cost.
427                 match stmt.kind {
428                     StatementKind::StorageLive(_)
429                     | StatementKind::StorageDead(_)
430                     | StatementKind::Deinit(_)
431                     | StatementKind::Nop => {}
432                     _ => cost += INSTR_COST,
433                 }
434             }
435             let term = blk.terminator();
436             let mut is_drop = false;
437             match term.kind {
438                 TerminatorKind::Drop { ref place, target, unwind }
439                 | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => {
440                     is_drop = true;
441                     work_list.push(target);
442                     // If the place doesn't actually need dropping, treat it like
443                     // a regular goto.
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);
450                         }
451                     } else {
452                         cost += INSTR_COST;
453                     }
454                 }
455
456                 TerminatorKind::Unreachable | TerminatorKind::Call { target: None, .. }
457                     if first_block =>
458                 {
459                     // If the function always diverges, don't inline
460                     // unless the cost is zero
461                     threshold = 0;
462                 }
463
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()
467                     {
468                         // Don't give intrinsics the extra penalty for calls
469                         if tcx.is_intrinsic(def_id) {
470                             cost += INSTR_COST;
471                         } else {
472                             cost += CALL_PENALTY;
473                         }
474                     } else {
475                         cost += CALL_PENALTY;
476                     }
477                     if cleanup.is_some() {
478                         cost += LANDINGPAD_PENALTY;
479                     }
480                 }
481                 TerminatorKind::Assert { cleanup, .. } => {
482                     cost += CALL_PENALTY;
483
484                     if cleanup.is_some() {
485                         cost += LANDINGPAD_PENALTY;
486                     }
487                 }
488                 TerminatorKind::Resume => cost += RESUME_PENALTY,
489                 TerminatorKind::InlineAsm { cleanup, .. } => {
490                     cost += INSTR_COST;
491
492                     if cleanup.is_some() {
493                         cost += LANDINGPAD_PENALTY;
494                     }
495                 }
496                 _ => cost += INSTR_COST,
497             }
498
499             if !is_drop {
500                 for succ in term.successors() {
501                     work_list.push(succ);
502                 }
503             }
504
505             first_block = false;
506         }
507
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.
510
511         let ptr_size = tcx.data_layout.pointer_size.bytes();
512
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
516             // it.
517             if let Some(size) = type_size_of(tcx, self.param_env, ty) {
518                 cost += ((size + ptr_size - 1) / ptr_size) as usize;
519             } else {
520                 cost += UNKNOWN_SIZE_COST;
521             }
522         }
523
524         if let InlineAttr::Always = callee_attrs.inline {
525             debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
526             Ok(())
527         } else if cost <= threshold {
528             debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
529             Ok(())
530         } else {
531             debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
532             Err("cost above threshold")
533         }
534     }
535
536     fn inline_call(
537         &self,
538         caller_body: &mut Body<'tcx>,
539         callsite: &CallSite<'tcx>,
540         mut callee_body: Body<'tcx>,
541     ) {
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() {
552                         match elem {
553                             ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
554                             _ => {}
555                         }
556                     }
557
558                     false
559                 }
560
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 },
566                         destination,
567                     );
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))),
573                     });
574                     self.tcx.mk_place_deref(temp)
575                 } else {
576                     destination
577                 };
578
579                 // Copy the arguments if needed.
580                 let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);
581
582                 let mut expn_data = ExpnData::default(
583                     ExpnKind::Inlined,
584                     callsite.source_info.span,
585                     self.tcx.sess.edition(),
586                     None,
587                     None,
588                 );
589                 expn_data.def_site = callee_body.span;
590                 let expn_data =
591                     self.tcx.with_stable_hashing_context(|hcx| LocalExpnId::fresh(expn_data, hcx));
592                 let mut integrator = Integrator {
593                     args: &args,
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())..,
597                     destination: dest,
598                     callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
599                     callsite,
600                     cleanup_block: cleanup,
601                     in_cleanup_block: false,
602                     tcx: self.tcx,
603                     expn_data,
604                     always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
605                 };
606
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);
610
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),
619                         });
620                     }
621                 }
622                 if let Some(block) = callsite.target {
623                     // To avoid repeated O(n) insert, push any new statements to the end and rotate
624                     // the slice once.
625                     let mut n = 0;
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),
632                             });
633                             n += 1;
634                         }
635                     }
636                     caller_body[block].statements.rotate_right(n);
637                 }
638
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(..));
644
645                 caller_body[callsite.block].terminator = Some(Terminator {
646                     source_info: callsite.source_info,
647                     kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
648                 });
649
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(_)),
658                             None => true,
659                         }
660                     }),
661                 );
662             }
663             kind => bug!("unexpected terminator kind {:?}", kind),
664         }
665     }
666
667     fn make_call_args(
668         &self,
669         args: Vec<Operand<'tcx>>,
670         callsite: &CallSite<'tcx>,
671         caller_body: &mut Body<'tcx>,
672         callee_body: &Body<'tcx>,
673     ) -> Vec<Local> {
674         let tcx = self.tcx;
675
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:
678         //
679         //     tuple_tmp = (a, b, c)
680         //     Fn::call(closure_ref, tuple_tmp)
681         //
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
685         // a vector like
686         //
687         //     [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
688         //
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:
693         //
694         //     tmp0 = tuple_tmp.0
695         //     tmp1 = tuple_tmp.1
696         //     tmp2 = tuple_tmp.2
697         //
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());
704
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");
708             };
709
710             // The `closure_ref` in our example above.
711             let closure_ref_arg = iter::once(self_);
712
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));
717
718                 // Spill to a local to make e.g., `tmp0`.
719                 self.create_temp_if_necessary(tuple_field, callsite, caller_body)
720             });
721
722             closure_ref_arg.chain(tuple_tmp_args).collect()
723         } else {
724             args.into_iter()
725                 .map(|a| self.create_temp_if_necessary(a, callsite, caller_body))
726                 .collect()
727         }
728     }
729
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(
733         &self,
734         arg: Operand<'tcx>,
735         callsite: &CallSite<'tcx>,
736         caller_body: &mut Body<'tcx>,
737     ) -> Local {
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
742         {
743             return local;
744         }
745
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)))),
753         });
754         local
755     }
756
757     /// Introduces a new temporary into the caller body that is live for the duration of the call.
758     fn new_call_temp(
759         &self,
760         caller_body: &mut Body<'tcx>,
761         callsite: &CallSite<'tcx>,
762         ty: Ty<'tcx>,
763     ) -> Local {
764         let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
765
766         caller_body[callsite.block].statements.push(Statement {
767             source_info: callsite.source_info,
768             kind: StatementKind::StorageLive(local),
769         });
770
771         if let Some(block) = callsite.target {
772             caller_body[block].statements.insert(
773                 0,
774                 Statement {
775                     source_info: callsite.source_info,
776                     kind: StatementKind::StorageDead(local),
777                 },
778             );
779         }
780
781         local
782     }
783 }
784
785 fn type_size_of<'tcx>(
786     tcx: TyCtxt<'tcx>,
787     param_env: ty::ParamEnv<'tcx>,
788     ty: Ty<'tcx>,
789 ) -> Option<u64> {
790     tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
791 }
792
793 /**
794  * Integrator.
795  *
796  * Integrates blocks from the callee function into the calling function.
797  * Updates block indices, references to locals and other control flow
798  * stuff.
799 */
800 struct Integrator<'a, 'tcx> {
801     args: &'a [Local],
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,
810     tcx: TyCtxt<'tcx>,
811     expn_data: LocalExpnId,
812     always_live_locals: BitSet<Local>,
813 }
814
815 impl Integrator<'_, '_> {
816     fn map_local(&self, local: Local) -> Local {
817         let new = if local == RETURN_PLACE {
818             self.destination.local
819         } else {
820             let idx = local.index() - 1;
821             if idx < self.args.len() {
822                 self.args[idx]
823             } else {
824                 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
825             }
826         };
827         trace!("mapping local `{:?}` to `{:?}`", local, new);
828         new
829     }
830
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);
834         new
835     }
836
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);
840         new
841     }
842 }
843
844 impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
845     fn tcx(&self) -> TyCtxt<'tcx> {
846         self.tcx
847     }
848
849     fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
850         *local = self.map_local(*local);
851     }
852
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)
862             } else {
863                 self.callsite_scope.inlined_parent_scope
864             };
865
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));
872         }
873     }
874
875     fn visit_source_scope(&mut self, scope: &mut SourceScope) {
876         *scope = self.map_scope(*scope);
877     }
878
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);
882     }
883
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);
889         }
890
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);
897
898             place.projection = self.tcx.intern_place_elems(&*projs);
899         }
900         // Handles integrating any locals that occur in the base
901         // or projections
902         self.super_place(place, context, location)
903     }
904
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;
909     }
910
911     fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
912         self.super_retag(kind, place, loc);
913
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;
918         }
919     }
920
921     fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
922         if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
923             statement.kind
924         {
925             self.always_live_locals.remove(local);
926         }
927         self.super_statement(statement, location);
928     }
929
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);
935         }
936
937         match terminator.kind {
938             TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
939             TerminatorKind::Goto { ref mut target } => {
940                 *target = self.map_block(*target);
941             }
942             TerminatorKind::SwitchInt { ref mut targets, .. } => {
943                 for tgt in targets.all_targets_mut() {
944                     *tgt = self.map_block(*tgt);
945                 }
946             }
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;
956                 }
957             }
958             TerminatorKind::Call { ref mut target, ref mut cleanup, .. } => {
959                 if let Some(ref mut tgt) = *target {
960                     *tgt = self.map_block(*tgt);
961                 }
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;
968                 }
969             }
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;
978                 }
979             }
980             TerminatorKind::Return => {
981                 terminator.kind = if let Some(tgt) = self.callsite.target {
982                     TerminatorKind::Goto { target: tgt }
983                 } else {
984                     TerminatorKind::Unreachable
985                 }
986             }
987             TerminatorKind::Resume => {
988                 if let Some(tgt) = self.cleanup_block {
989                     terminator.kind = TerminatorKind::Goto { target: tgt }
990                 }
991             }
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);
997             }
998             TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
999             // see the ordering of passes in the optimized_mir query.
1000             {
1001                 bug!("False unwinds should have been removed before inlining")
1002             }
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;
1010                 }
1011             }
1012         }
1013     }
1014 }