1 //! Inlining pass for MIR functions
3 use rustc_attr as attr;
4 use rustc_index::bit_set::BitSet;
5 use rustc_index::vec::Idx;
6 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
7 use rustc_middle::mir::visit::*;
8 use rustc_middle::mir::*;
9 use rustc_middle::ty::subst::Subst;
10 use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
11 use rustc_target::spec::abi::Abi;
13 use super::simplify::{remove_dead_blocks, CfgSimplifier};
14 use crate::transform::MirPass;
15 use std::collections::VecDeque;
17 use std::ops::RangeFrom;
19 const DEFAULT_THRESHOLD: usize = 50;
20 const HINT_THRESHOLD: usize = 100;
22 const INSTR_COST: usize = 5;
23 const CALL_PENALTY: usize = 25;
24 const LANDINGPAD_PENALTY: usize = 50;
25 const RESUME_PENALTY: usize = 45;
27 const UNKNOWN_SIZE_COST: usize = 10;
31 #[derive(Copy, Clone, Debug)]
32 struct CallSite<'tcx> {
33 callee: Instance<'tcx>,
35 source_info: SourceInfo,
38 impl<'tcx> MirPass<'tcx> for Inline {
39 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
40 if tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
41 if tcx.sess.opts.debugging_opts.instrument_coverage {
42 // The current implementation of source code coverage injects code region counters
43 // into the MIR, and assumes a 1-to-1 correspondence between MIR and source-code-
45 debug!("function inlining is disabled when compiling with `instrument_coverage`");
49 param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
50 codegen_fn_attrs: tcx.codegen_fn_attrs(body.source.def_id()),
58 struct Inliner<'tcx> {
60 param_env: ParamEnv<'tcx>,
61 codegen_fn_attrs: &'tcx CodegenFnAttrs,
65 fn run_pass(&self, caller_body: &mut Body<'tcx>) {
66 // Keep a queue of callsites to try inlining on. We take
67 // advantage of the fact that queries detect cycles here to
68 // allow us to try and fetch the fully optimized MIR of a
69 // call; if it succeeds, we can inline it and we know that
70 // they do not call us. Otherwise, we just don't try to
73 // We use a queue so that we inline "broadly" before we inline
74 // in depth. It is unclear if this is the best heuristic,
75 // really, but that's true of all the heuristics in this
78 let mut callsites = VecDeque::new();
80 let def_id = caller_body.source.def_id();
82 // Only do inlining into fn bodies.
83 let self_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
84 if self.tcx.hir().body_owner_kind(self_hir_id).is_fn_or_closure()
85 && caller_body.source.promoted.is_none()
87 for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated() {
88 if let Some(callsite) = self.get_valid_function_call(bb, bb_data, caller_body) {
89 callsites.push_back(callsite);
96 let mut changed = false;
97 while let Some(callsite) = callsites.pop_front() {
98 debug!("checking whether to inline callsite {:?}", callsite);
100 if let InstanceDef::Item(_) = callsite.callee.def {
101 if !self.tcx.is_mir_available(callsite.callee.def_id()) {
102 debug!("checking whether to inline callsite {:?} - MIR unavailable", callsite,);
107 let callee_body = if let Some(callee_def_id) = callsite.callee.def_id().as_local() {
108 let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id);
109 // Avoid a cycle here by only using `instance_mir` only if we have
110 // a lower `HirId` than the callee. This ensures that the callee will
111 // not inline us. This trick only works without incremental compilation.
112 // So don't do it if that is enabled. Also avoid inlining into generators,
113 // since their `optimized_mir` is used for layout computation, which can
114 // create a cycle, even when no attempt is made to inline the function
115 // in the other direction.
116 if !self.tcx.dep_graph.is_fully_enabled()
117 && self_hir_id < callee_hir_id
118 && caller_body.generator_kind.is_none()
120 self.tcx.instance_mir(callsite.callee.def)
125 // This cannot result in a cycle since the callee MIR is from another crate
126 // and is already optimized.
127 self.tcx.instance_mir(callsite.callee.def)
130 let callee_body: &Body<'tcx> = &*callee_body;
132 let callee_body = if self.consider_optimizing(callsite, callee_body) {
133 self.tcx.subst_and_normalize_erasing_regions(
134 &callsite.callee.substs,
142 // Copy only unevaluated constants from the callee_body into the caller_body.
143 // Although we are only pushing `ConstKind::Unevaluated` consts to
144 // `required_consts`, here we may not only have `ConstKind::Unevaluated`
145 // because we are calling `subst_and_normalize_erasing_regions`.
146 caller_body.required_consts.extend(callee_body.required_consts.iter().copied().filter(
147 |&constant| matches!(constant.literal.val, ConstKind::Unevaluated(_, _, _)),
150 let start = caller_body.basic_blocks().len();
151 debug!("attempting to inline callsite {:?} - body={:?}", callsite, callee_body);
152 if !self.inline_call(callsite, caller_body, callee_body) {
153 debug!("attempting to inline callsite {:?} - failure", callsite);
156 debug!("attempting to inline callsite {:?} - success", callsite);
158 // Add callsites from inlined function
159 for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated().skip(start) {
160 if let Some(new_callsite) = self.get_valid_function_call(bb, bb_data, caller_body) {
161 // Don't inline the same function multiple times.
162 if callsite.callee != new_callsite.callee {
163 callsites.push_back(new_callsite);
171 // Simplify if we inlined anything.
173 debug!("running simplify cfg on {:?}", caller_body.source);
174 CfgSimplifier::new(caller_body).simplify();
175 remove_dead_blocks(caller_body);
179 fn get_valid_function_call(
182 bb_data: &BasicBlockData<'tcx>,
183 caller_body: &Body<'tcx>,
184 ) -> Option<CallSite<'tcx>> {
185 // Don't inline calls that are in cleanup blocks.
186 if bb_data.is_cleanup {
190 // Only consider direct calls to functions
191 let terminator = bb_data.terminator();
192 if let TerminatorKind::Call { func: ref op, .. } = terminator.kind {
193 if let ty::FnDef(callee_def_id, substs) = *op.ty(caller_body, self.tcx).kind() {
194 // To resolve an instance its substs have to be fully normalized, so
196 let normalized_substs = self.tcx.normalize_erasing_regions(self.param_env, substs);
198 Instance::resolve(self.tcx, self.param_env, callee_def_id, normalized_substs)
202 if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
206 return Some(CallSite { callee, bb, source_info: terminator.source_info });
213 fn consider_optimizing(&self, callsite: CallSite<'tcx>, callee_body: &Body<'tcx>) -> bool {
214 debug!("consider_optimizing({:?})", callsite);
215 self.should_inline(callsite, callee_body)
216 && self.tcx.consider_optimizing(|| {
217 format!("Inline {:?} into {:?}", callee_body.span, callsite)
221 fn should_inline(&self, callsite: CallSite<'tcx>, callee_body: &Body<'tcx>) -> bool {
222 debug!("should_inline({:?})", callsite);
225 // Cannot inline generators which haven't been transformed yet
226 if callee_body.yield_ty.is_some() {
227 debug!(" yield ty present - not inlining");
231 let codegen_fn_attrs = tcx.codegen_fn_attrs(callsite.callee.def_id());
233 let self_features = &self.codegen_fn_attrs.target_features;
234 let callee_features = &codegen_fn_attrs.target_features;
235 if callee_features.iter().any(|feature| !self_features.contains(feature)) {
236 debug!("`callee has extra target features - not inlining");
240 let self_no_sanitize =
241 self.codegen_fn_attrs.no_sanitize & self.tcx.sess.opts.debugging_opts.sanitizer;
242 let callee_no_sanitize =
243 codegen_fn_attrs.no_sanitize & self.tcx.sess.opts.debugging_opts.sanitizer;
244 if self_no_sanitize != callee_no_sanitize {
245 debug!("`callee has incompatible no_sanitize attribute - not inlining");
249 let hinted = match codegen_fn_attrs.inline {
250 // Just treat inline(always) as a hint for now,
251 // there are cases that prevent inlining that we
252 // need to check for first.
253 attr::InlineAttr::Always => true,
254 attr::InlineAttr::Never => {
255 debug!("`#[inline(never)]` present - not inlining");
258 attr::InlineAttr::Hint => true,
259 attr::InlineAttr::None => false,
262 // Only inline local functions if they would be eligible for cross-crate
263 // inlining. This is to ensure that the final crate doesn't have MIR that
264 // reference unexported symbols
265 if callsite.callee.def_id().is_local() {
266 if callsite.callee.substs.non_erasable_generics().count() == 0 && !hinted {
267 debug!(" callee is an exported function - not inlining");
272 let mut threshold = if hinted { HINT_THRESHOLD } else { DEFAULT_THRESHOLD };
274 // Significantly lower the threshold for inlining cold functions
275 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
279 // Give a bonus functions with a small number of blocks,
280 // We normally have two or three blocks for even
281 // very small functions.
282 if callee_body.basic_blocks().len() <= 3 {
283 threshold += threshold / 4;
285 debug!(" final inline threshold = {}", threshold);
287 // FIXME: Give a bonus to functions with only a single caller
288 let mut first_block = true;
291 // Traverse the MIR manually so we can account for the effects of
292 // inlining on the CFG.
293 let mut work_list = vec![START_BLOCK];
294 let mut visited = BitSet::new_empty(callee_body.basic_blocks().len());
295 while let Some(bb) = work_list.pop() {
296 if !visited.insert(bb.index()) {
299 let blk = &callee_body.basic_blocks()[bb];
301 for stmt in &blk.statements {
302 // Don't count StorageLive/StorageDead in the inlining cost.
304 StatementKind::StorageLive(_)
305 | StatementKind::StorageDead(_)
306 | StatementKind::Nop => {}
307 _ => cost += INSTR_COST,
310 let term = blk.terminator();
311 let mut is_drop = false;
313 TerminatorKind::Drop { ref place, target, unwind }
314 | TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => {
316 work_list.push(target);
317 // If the place doesn't actually need dropping, treat it like
319 let ty = place.ty(callee_body, tcx).subst(tcx, callsite.callee.substs).ty;
320 if ty.needs_drop(tcx, self.param_env) {
321 cost += CALL_PENALTY;
322 if let Some(unwind) = unwind {
323 cost += LANDINGPAD_PENALTY;
324 work_list.push(unwind);
331 TerminatorKind::Unreachable | TerminatorKind::Call { destination: None, .. }
334 // If the function always diverges, don't inline
335 // unless the cost is zero
339 TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
340 if let ty::FnDef(def_id, _) = *f.literal.ty.kind() {
341 // Don't give intrinsics the extra penalty for calls
342 let f = tcx.fn_sig(def_id);
343 if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic {
346 cost += CALL_PENALTY;
349 cost += CALL_PENALTY;
351 if cleanup.is_some() {
352 cost += LANDINGPAD_PENALTY;
355 TerminatorKind::Assert { cleanup, .. } => {
356 cost += CALL_PENALTY;
358 if cleanup.is_some() {
359 cost += LANDINGPAD_PENALTY;
362 TerminatorKind::Resume => cost += RESUME_PENALTY,
363 _ => cost += INSTR_COST,
367 for &succ in term.successors() {
368 work_list.push(succ);
375 // Count up the cost of local variables and temps, if we know the size
376 // use that, otherwise we use a moderately-large dummy cost.
378 let ptr_size = tcx.data_layout.pointer_size.bytes();
380 for v in callee_body.vars_and_temps_iter() {
381 let v = &callee_body.local_decls[v];
382 let ty = v.ty.subst(tcx, callsite.callee.substs);
383 // Cost of the var is the size in machine-words, if we know
385 if let Some(size) = type_size_of(tcx, self.param_env, ty) {
386 cost += (size / ptr_size) as usize;
388 cost += UNKNOWN_SIZE_COST;
392 if let attr::InlineAttr::Always = codegen_fn_attrs.inline {
393 debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
396 if cost <= threshold {
397 debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
400 debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
408 callsite: CallSite<'tcx>,
409 caller_body: &mut Body<'tcx>,
410 mut callee_body: Body<'tcx>,
412 let terminator = caller_body[callsite.bb].terminator.take().unwrap();
413 match terminator.kind {
414 // FIXME: Handle inlining of diverging calls
415 TerminatorKind::Call { args, destination: Some(destination), cleanup, .. } => {
416 debug!("inlined {:?} into {:?}", callsite.callee, caller_body.source);
418 // If the call is something like `a[*i] = f(i)`, where
419 // `i : &mut usize`, then just duplicating the `a[*i]`
420 // Place could result in two different locations if `f`
421 // writes to `i`. To prevent this we need to create a temporary
422 // borrow of the place and pass the destination as `*temp` instead.
423 fn dest_needs_borrow(place: Place<'_>) -> bool {
424 for elem in place.projection.iter() {
426 ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
434 let dest = if dest_needs_borrow(destination.0) {
435 debug!("creating temp for return destination");
436 let dest = Rvalue::Ref(
437 self.tcx.lifetimes.re_erased,
438 BorrowKind::Mut { allow_two_phase_borrow: false },
442 let ty = dest.ty(caller_body, self.tcx);
444 let temp = LocalDecl::new(ty, callsite.source_info.span);
446 let tmp = caller_body.local_decls.push(temp);
447 let tmp = Place::from(tmp);
449 let stmt = Statement {
450 source_info: callsite.source_info,
451 kind: StatementKind::Assign(box (tmp, dest)),
453 caller_body[callsite.bb].statements.push(stmt);
454 self.tcx.mk_place_deref(tmp)
459 let return_block = destination.1;
461 // Copy the arguments if needed.
462 let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, return_block);
464 let mut integrator = Integrator {
466 new_locals: Local::new(caller_body.local_decls.len())..,
467 new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
468 new_blocks: BasicBlock::new(caller_body.basic_blocks().len())..,
471 cleanup_block: cleanup,
472 in_cleanup_block: false,
476 // Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
477 // (or existing ones, in a few special cases) in the caller.
478 integrator.visit_body(&mut callee_body);
480 for scope in &mut callee_body.source_scopes {
481 // FIXME(eddyb) move this into a `fn visit_scope_data` in `Integrator`.
482 if scope.parent_scope.is_none() {
483 let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope];
485 // Attach the outermost callee scope as a child of the callsite
486 // scope, via the `parent_scope` and `inlined_parent_scope` chains.
487 scope.parent_scope = Some(callsite.source_info.scope);
488 assert_eq!(scope.inlined_parent_scope, None);
489 scope.inlined_parent_scope = if callsite_scope.inlined.is_some() {
490 Some(callsite.source_info.scope)
492 callsite_scope.inlined_parent_scope
495 // Mark the outermost callee scope as an inlined one.
496 assert_eq!(scope.inlined, None);
497 scope.inlined = Some((callsite.callee, callsite.source_info.span));
498 } else if scope.inlined_parent_scope.is_none() {
499 // Make it easy to find the scope with `inlined` set above.
500 scope.inlined_parent_scope =
501 Some(integrator.map_scope(OUTERMOST_SOURCE_SCOPE));
505 // Insert all of the (mapped) parts of the callee body into the caller.
506 caller_body.local_decls.extend(
507 // FIXME(eddyb) make `Range<Local>` iterable so that we can use
508 // `callee_body.local_decls.drain(callee_body.vars_and_temps())`
510 .vars_and_temps_iter()
511 .map(|local| callee_body.local_decls[local].clone()),
513 caller_body.source_scopes.extend(callee_body.source_scopes.drain(..));
514 caller_body.var_debug_info.extend(callee_body.var_debug_info.drain(..));
515 caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..));
517 caller_body[callsite.bb].terminator = Some(Terminator {
518 source_info: callsite.source_info,
519 kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
525 caller_body[callsite.bb].terminator =
526 Some(Terminator { source_info: terminator.source_info, kind });
534 args: Vec<Operand<'tcx>>,
535 callsite: &CallSite<'tcx>,
536 caller_body: &mut Body<'tcx>,
537 return_block: BasicBlock,
541 // There is a bit of a mismatch between the *caller* of a closure and the *callee*.
542 // The caller provides the arguments wrapped up in a tuple:
544 // tuple_tmp = (a, b, c)
545 // Fn::call(closure_ref, tuple_tmp)
547 // meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
548 // as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
549 // the job of unpacking this tuple. But here, we are codegen. =) So we want to create
552 // [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
554 // Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
555 // if we "spill" that into *another* temporary, so that we can map the argument
556 // variable in the callee MIR directly to an argument variable on our side.
557 // So we introduce temporaries like:
559 // tmp0 = tuple_tmp.0
560 // tmp1 = tuple_tmp.1
561 // tmp2 = tuple_tmp.2
563 // and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
564 // FIXME(eddyb) make this check for `"rust-call"` ABI combined with
565 // `callee_body.spread_arg == None`, instead of special-casing closures.
566 if tcx.is_closure(callsite.callee.def_id()) {
567 let mut args = args.into_iter();
568 let self_ = self.create_temp_if_necessary(
569 args.next().unwrap(),
574 let tuple = self.create_temp_if_necessary(
575 args.next().unwrap(),
580 assert!(args.next().is_none());
582 let tuple = Place::from(tuple);
583 let tuple_tys = if let ty::Tuple(s) = tuple.ty(caller_body, tcx).ty.kind() {
586 bug!("Closure arguments are not passed as a tuple");
589 // The `closure_ref` in our example above.
590 let closure_ref_arg = iter::once(self_);
592 // The `tmp0`, `tmp1`, and `tmp2` in our example abonve.
593 let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
594 // This is e.g., `tuple_tmp.0` in our example above.
596 Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty.expect_ty()));
598 // Spill to a local to make e.g., `tmp0`.
599 self.create_temp_if_necessary(tuple_field, callsite, caller_body, return_block)
602 closure_ref_arg.chain(tuple_tmp_args).collect()
605 .map(|a| self.create_temp_if_necessary(a, callsite, caller_body, return_block))
610 /// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh
611 /// temporary `T` and an instruction `T = arg`, and returns `T`.
612 fn create_temp_if_necessary(
615 callsite: &CallSite<'tcx>,
616 caller_body: &mut Body<'tcx>,
617 return_block: BasicBlock,
619 // FIXME: Analysis of the usage of the arguments to avoid
620 // unnecessary temporaries.
622 if let Operand::Move(place) = &arg {
623 if let Some(local) = place.as_local() {
624 if caller_body.local_kind(local) == LocalKind::Temp {
625 // Reuse the operand if it's a temporary already
631 debug!("creating temp for argument {:?}", arg);
632 // Otherwise, create a temporary for the arg
633 let arg = Rvalue::Use(arg);
635 let ty = arg.ty(caller_body, self.tcx);
637 let arg_tmp = LocalDecl::new(ty, callsite.source_info.span);
638 let arg_tmp = caller_body.local_decls.push(arg_tmp);
640 caller_body[callsite.bb].statements.push(Statement {
641 source_info: callsite.source_info,
642 kind: StatementKind::StorageLive(arg_tmp),
644 caller_body[callsite.bb].statements.push(Statement {
645 source_info: callsite.source_info,
646 kind: StatementKind::Assign(box (Place::from(arg_tmp), arg)),
648 caller_body[return_block].statements.insert(
651 source_info: callsite.source_info,
652 kind: StatementKind::StorageDead(arg_tmp),
660 fn type_size_of<'tcx>(
662 param_env: ty::ParamEnv<'tcx>,
665 tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
671 * Integrates blocks from the callee function into the calling function.
672 * Updates block indices, references to locals and other control flow
675 struct Integrator<'a, 'tcx> {
677 new_locals: RangeFrom<Local>,
678 new_scopes: RangeFrom<SourceScope>,
679 new_blocks: RangeFrom<BasicBlock>,
680 destination: Place<'tcx>,
681 return_block: BasicBlock,
682 cleanup_block: Option<BasicBlock>,
683 in_cleanup_block: bool,
687 impl<'a, 'tcx> Integrator<'a, 'tcx> {
688 fn map_local(&self, local: Local) -> Local {
689 let new = if local == RETURN_PLACE {
690 self.destination.local
692 let idx = local.index() - 1;
693 if idx < self.args.len() {
696 Local::new(self.new_locals.start.index() + (idx - self.args.len()))
699 debug!("mapping local `{:?}` to `{:?}`", local, new);
703 fn map_scope(&self, scope: SourceScope) -> SourceScope {
704 let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
705 debug!("mapping scope `{:?}` to `{:?}`", scope, new);
709 fn map_block(&self, block: BasicBlock) -> BasicBlock {
710 let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
711 debug!("mapping block `{:?}` to `{:?}`", block, new);
716 impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
717 fn tcx(&self) -> TyCtxt<'tcx> {
721 fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
722 *local = self.map_local(*local);
725 fn visit_source_scope(&mut self, scope: &mut SourceScope) {
726 *scope = self.map_scope(*scope);
729 fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
730 // If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
731 let dest_proj_len = self.destination.projection.len();
732 if place.local == RETURN_PLACE && dest_proj_len > 0 {
733 let mut projs = Vec::with_capacity(dest_proj_len + place.projection.len());
734 projs.extend(self.destination.projection);
735 projs.extend(place.projection);
737 place.projection = self.tcx.intern_place_elems(&*projs);
739 // Handles integrating any locals that occur in the base
741 self.super_place(place, context, location)
744 fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
745 self.in_cleanup_block = data.is_cleanup;
746 self.super_basic_block_data(block, data);
747 self.in_cleanup_block = false;
750 fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
751 self.super_retag(kind, place, loc);
753 // We have to patch all inlined retags to be aware that they are no longer
754 // happening on function entry.
755 if *kind == RetagKind::FnEntry {
756 *kind = RetagKind::Default;
760 fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
761 // Don't try to modify the implicit `_0` access on return (`return` terminators are
762 // replaced down below anyways).
763 if !matches!(terminator.kind, TerminatorKind::Return) {
764 self.super_terminator(terminator, loc);
767 match terminator.kind {
768 TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
769 TerminatorKind::Goto { ref mut target } => {
770 *target = self.map_block(*target);
772 TerminatorKind::SwitchInt { ref mut targets, .. } => {
773 for tgt in targets.all_targets_mut() {
774 *tgt = self.map_block(*tgt);
777 TerminatorKind::Drop { ref mut target, ref mut unwind, .. }
778 | TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
779 *target = self.map_block(*target);
780 if let Some(tgt) = *unwind {
781 *unwind = Some(self.map_block(tgt));
782 } else if !self.in_cleanup_block {
783 // Unless this drop is in a cleanup block, add an unwind edge to
784 // the original call's cleanup block
785 *unwind = self.cleanup_block;
788 TerminatorKind::Call { ref mut destination, ref mut cleanup, .. } => {
789 if let Some((_, ref mut tgt)) = *destination {
790 *tgt = self.map_block(*tgt);
792 if let Some(tgt) = *cleanup {
793 *cleanup = Some(self.map_block(tgt));
794 } else if !self.in_cleanup_block {
795 // Unless this call is in a cleanup block, add an unwind edge to
796 // the original call's cleanup block
797 *cleanup = self.cleanup_block;
800 TerminatorKind::Assert { ref mut target, ref mut cleanup, .. } => {
801 *target = self.map_block(*target);
802 if let Some(tgt) = *cleanup {
803 *cleanup = Some(self.map_block(tgt));
804 } else if !self.in_cleanup_block {
805 // Unless this assert is in a cleanup block, add an unwind edge to
806 // the original call's cleanup block
807 *cleanup = self.cleanup_block;
810 TerminatorKind::Return => {
811 terminator.kind = TerminatorKind::Goto { target: self.return_block };
813 TerminatorKind::Resume => {
814 if let Some(tgt) = self.cleanup_block {
815 terminator.kind = TerminatorKind::Goto { target: tgt }
818 TerminatorKind::Abort => {}
819 TerminatorKind::Unreachable => {}
820 TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
821 *real_target = self.map_block(*real_target);
822 *imaginary_target = self.map_block(*imaginary_target);
824 TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
825 // see the ordering of passes in the optimized_mir query.
827 bug!("False unwinds should have been removed before inlining")
829 TerminatorKind::InlineAsm { ref mut destination, .. } => {
830 if let Some(ref mut tgt) = *destination {
831 *tgt = self.map_block(*tgt);