2017-02-08 03:24:49 -06:00
|
|
|
//! Inlining pass for MIR functions
|
2022-05-11 16:27:06 -05:00
|
|
|
use crate::deref_separator::deref_finder;
|
2021-02-23 18:00:00 -06:00
|
|
|
use rustc_attr::InlineAttr;
|
2022-04-30 11:10:33 -05:00
|
|
|
use rustc_const_eval::transform::validate::equal_up_to_regions;
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2020-09-21 18:21:26 -05:00
|
|
|
use rustc_index::vec::Idx;
|
2020-09-02 19:00:00 -05:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::mir::visit::*;
|
|
|
|
use rustc_middle::mir::*;
|
2020-11-10 18:00:00 -06:00
|
|
|
use rustc_middle::ty::subst::Subst;
|
2022-05-08 14:43:18 -05:00
|
|
|
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
|
2021-09-30 21:03:49 -05:00
|
|
|
use rustc_session::config::OptLevel;
|
2022-08-14 04:28:28 -05:00
|
|
|
use rustc_span::def_id::DefId;
|
2022-02-27 09:09:52 -06:00
|
|
|
use rustc_span::{hygiene::ExpnKind, ExpnData, LocalExpnId, Span};
|
2022-08-15 04:58:27 -05:00
|
|
|
use rustc_target::abi::VariantIdx;
|
2020-03-11 06:49:08 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
use super::simplify::{remove_dead_blocks, CfgSimplifier};
|
2020-12-31 18:53:25 -06:00
|
|
|
use crate::MirPass;
|
2017-11-18 17:02:41 -06:00
|
|
|
use std::iter;
|
2020-11-08 18:00:00 -06:00
|
|
|
use std::ops::{Range, RangeFrom};
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) mod cycle;
|
2020-12-29 10:21:52 -06:00
|
|
|
|
2017-02-08 03:24:49 -06:00
|
|
|
const INSTR_COST: usize = 5;
|
|
|
|
const CALL_PENALTY: usize = 25;
|
2020-03-11 13:18:22 -05:00
|
|
|
const LANDINGPAD_PENALTY: usize = 50;
|
|
|
|
const RESUME_PENALTY: usize = 45;
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
const UNKNOWN_SIZE_COST: usize = 10;
|
|
|
|
|
|
|
|
pub struct Inline;
|
|
|
|
|
2017-11-09 15:03:03 -06:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2017-05-01 15:03:05 -05:00
|
|
|
struct CallSite<'tcx> {
|
2020-02-08 13:28:19 -06:00
|
|
|
callee: Instance<'tcx>,
|
2020-11-10 18:00:00 -06:00
|
|
|
fn_sig: ty::PolyFnSig<'tcx>,
|
2020-11-08 18:00:00 -06:00
|
|
|
block: BasicBlock,
|
|
|
|
target: Option<BasicBlock>,
|
2020-02-08 12:24:42 -06:00
|
|
|
source_info: SourceInfo,
|
2017-05-01 11:47:00 -05:00
|
|
|
}
|
|
|
|
|
2019-08-04 15:20:00 -05:00
|
|
|
impl<'tcx> MirPass<'tcx> for Inline {
|
2021-12-02 11:17:32 -06:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
2022-07-06 07:44:47 -05:00
|
|
|
if let Some(enabled) = sess.opts.unstable_opts.inline_mir {
|
2021-12-02 11:17:32 -06:00
|
|
|
return enabled;
|
2021-01-02 11:40:15 -06:00
|
|
|
}
|
|
|
|
|
2021-09-30 21:03:49 -05:00
|
|
|
match sess.mir_opt_level() {
|
|
|
|
0 | 1 => false,
|
|
|
|
2 => {
|
|
|
|
(sess.opts.optimize == OptLevel::Default
|
|
|
|
|| sess.opts.optimize == OptLevel::Aggressive)
|
|
|
|
&& sess.opts.incremental == None
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
2021-12-02 11:17:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2020-12-29 10:21:52 -06:00
|
|
|
let span = trace_span!("inline", body = %tcx.def_path_str(body.source.def_id()));
|
|
|
|
let _guard = span.enter();
|
2020-11-08 18:00:00 -06:00
|
|
|
if inline(tcx, body) {
|
|
|
|
debug!("running simplify cfg on {:?}", body.source);
|
|
|
|
CfgSimplifier::new(body).simplify();
|
2021-05-01 16:56:48 -05:00
|
|
|
remove_dead_blocks(tcx, body);
|
2022-05-11 16:27:06 -05:00
|
|
|
deref_finder(tcx, body);
|
2020-11-08 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
|
2022-03-29 10:11:12 -05:00
|
|
|
let def_id = body.source.def_id().expect_local();
|
2020-11-08 18:00:00 -06:00
|
|
|
|
|
|
|
// Only do inlining into fn bodies.
|
2022-03-29 10:11:12 -05:00
|
|
|
if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() {
|
2020-11-08 18:00:00 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if body.source.promoted.is_some() {
|
|
|
|
return false;
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2021-12-13 12:25:36 -06:00
|
|
|
// Avoid inlining into generators, since their `optimized_mir` is used for layout computation,
|
|
|
|
// which can create a cycle, even when no attempt is made to inline the function in the other
|
|
|
|
// direction.
|
|
|
|
if body.generator.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-08 18:00:00 -06:00
|
|
|
|
2022-01-17 22:38:38 -06:00
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(def_id);
|
|
|
|
|
2020-11-08 18:00:00 -06:00
|
|
|
let mut this = Inliner {
|
|
|
|
tcx,
|
2022-01-17 22:38:38 -06:00
|
|
|
param_env,
|
|
|
|
codegen_fn_attrs: tcx.codegen_fn_attrs(def_id),
|
2020-11-08 18:00:00 -06:00
|
|
|
history: Vec::new(),
|
|
|
|
changed: false,
|
|
|
|
};
|
|
|
|
let blocks = BasicBlock::new(0)..body.basic_blocks().next_index();
|
|
|
|
this.process_blocks(body, blocks);
|
|
|
|
this.changed
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
struct Inliner<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-10-05 04:04:25 -05:00
|
|
|
param_env: ParamEnv<'tcx>,
|
2020-11-08 18:00:00 -06:00
|
|
|
/// Caller codegen attributes.
|
2020-09-02 19:00:00 -05:00
|
|
|
codegen_fn_attrs: &'tcx CodegenFnAttrs,
|
2022-08-14 04:28:28 -05:00
|
|
|
/// Stack of inlined instances.
|
|
|
|
/// We only check the `DefId` and not the substs because we want to
|
|
|
|
/// avoid inlining cases of polymorphic recursion.
|
|
|
|
/// The number of `DefId`s is finite, so checking history is enough
|
|
|
|
/// to ensure that we do not loop endlessly while inlining.
|
|
|
|
history: Vec<DefId>,
|
2020-11-08 18:00:00 -06:00
|
|
|
/// Indicates that the caller body has been modified.
|
|
|
|
changed: bool,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> Inliner<'tcx> {
|
2020-11-08 18:00:00 -06:00
|
|
|
fn process_blocks(&mut self, caller_body: &mut Body<'tcx>, blocks: Range<BasicBlock>) {
|
|
|
|
for bb in blocks {
|
2021-02-23 18:00:00 -06:00
|
|
|
let bb_data = &caller_body[bb];
|
|
|
|
if bb_data.is_cleanup {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-18 17:48:49 -06:00
|
|
|
let Some(callsite) = self.resolve_callsite(caller_body, bb, bb_data) else {
|
|
|
|
continue;
|
2020-11-08 18:00:00 -06:00
|
|
|
};
|
2021-02-23 18:00:00 -06:00
|
|
|
|
2020-12-29 10:21:52 -06:00
|
|
|
let span = trace_span!("process_blocks", %callsite.callee, ?bb);
|
|
|
|
let _guard = span.enter();
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
match self.try_inlining(caller_body, &callsite) {
|
|
|
|
Err(reason) => {
|
|
|
|
debug!("not-inlined {} [{}]", callsite.callee, reason);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Ok(new_blocks) => {
|
|
|
|
debug!("inlined {}", callsite.callee);
|
|
|
|
self.changed = true;
|
2022-08-14 04:28:28 -05:00
|
|
|
self.history.push(callsite.callee.def_id());
|
2021-02-23 18:00:00 -06:00
|
|
|
self.process_blocks(caller_body, new_blocks);
|
|
|
|
self.history.pop();
|
|
|
|
}
|
2020-11-01 18:00:00 -06:00
|
|
|
}
|
2021-02-23 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
/// Attempts to inline a callsite into the caller body. When successful returns basic blocks
|
|
|
|
/// containing the inlined body. Otherwise returns an error describing why inlining didn't take
|
|
|
|
/// place.
|
|
|
|
fn try_inlining(
|
|
|
|
&self,
|
|
|
|
caller_body: &mut Body<'tcx>,
|
|
|
|
callsite: &CallSite<'tcx>,
|
|
|
|
) -> Result<std::ops::Range<BasicBlock>, &'static str> {
|
|
|
|
let callee_attrs = self.tcx.codegen_fn_attrs(callsite.callee.def_id());
|
|
|
|
self.check_codegen_attributes(callsite, callee_attrs)?;
|
|
|
|
self.check_mir_is_available(caller_body, &callsite.callee)?;
|
|
|
|
let callee_body = self.tcx.instance_mir(callsite.callee.def);
|
|
|
|
self.check_mir_body(callsite, callee_body, callee_attrs)?;
|
|
|
|
|
|
|
|
if !self.tcx.consider_optimizing(|| {
|
2021-09-30 20:25:00 -05:00
|
|
|
format!("Inline {:?} into {:?}", callsite.callee, caller_body.source)
|
2021-02-23 18:00:00 -06:00
|
|
|
}) {
|
|
|
|
return Err("optimization fuel exhausted");
|
|
|
|
}
|
2020-11-05 18:00:00 -06:00
|
|
|
|
2022-06-03 11:44:46 -05:00
|
|
|
let Ok(callee_body) = callsite.callee.try_subst_mir_and_normalize_erasing_regions(
|
2021-02-23 18:00:00 -06:00
|
|
|
self.tcx,
|
|
|
|
self.param_env,
|
|
|
|
callee_body.clone(),
|
2022-06-03 11:44:46 -05:00
|
|
|
) else {
|
|
|
|
return Err("failed to normalize callee body");
|
|
|
|
};
|
2020-11-08 18:00:00 -06:00
|
|
|
|
2022-04-30 11:10:33 -05:00
|
|
|
// Check call signature compatibility.
|
|
|
|
// Normally, this shouldn't be required, but trait normalization failure can create a
|
|
|
|
// validation ICE.
|
|
|
|
let terminator = caller_body[callsite.block].terminator.as_ref().unwrap();
|
|
|
|
let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
|
|
|
|
let destination_ty = destination.ty(&caller_body.local_decls, self.tcx).ty;
|
|
|
|
let output_type = callee_body.return_ty();
|
|
|
|
if !equal_up_to_regions(self.tcx, self.param_env, output_type, destination_ty) {
|
|
|
|
trace!(?output_type, ?destination_ty);
|
|
|
|
return Err("failed to normalize return type");
|
|
|
|
}
|
|
|
|
if callsite.fn_sig.abi() == Abi::RustCall {
|
2022-07-02 16:40:33 -05:00
|
|
|
let (arg_tuple, skipped_args) = match &args[..] {
|
|
|
|
[arg_tuple] => (arg_tuple, 0),
|
|
|
|
[_, arg_tuple] => (arg_tuple, 1),
|
|
|
|
_ => bug!("Expected `rust-call` to have 1 or 2 args"),
|
|
|
|
};
|
2022-04-30 11:10:33 -05:00
|
|
|
|
2022-07-02 16:40:33 -05:00
|
|
|
let arg_tuple_ty = arg_tuple.ty(&caller_body.local_decls, self.tcx);
|
2022-04-30 11:10:33 -05:00
|
|
|
let ty::Tuple(arg_tuple_tys) = arg_tuple_ty.kind() else {
|
|
|
|
bug!("Closure arguments are not passed as a tuple");
|
|
|
|
};
|
|
|
|
|
2022-07-02 16:40:33 -05:00
|
|
|
for (arg_ty, input) in
|
|
|
|
arg_tuple_tys.iter().zip(callee_body.args_iter().skip(skipped_args))
|
|
|
|
{
|
2022-04-30 11:10:33 -05:00
|
|
|
let input_type = callee_body.local_decls[input].ty;
|
|
|
|
if !equal_up_to_regions(self.tcx, self.param_env, arg_ty, input_type) {
|
|
|
|
trace!(?arg_ty, ?input_type);
|
|
|
|
return Err("failed to normalize tuple argument type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (arg, input) in args.iter().zip(callee_body.args_iter()) {
|
|
|
|
let input_type = callee_body.local_decls[input].ty;
|
|
|
|
let arg_ty = arg.ty(&caller_body.local_decls, self.tcx);
|
|
|
|
if !equal_up_to_regions(self.tcx, self.param_env, arg_ty, input_type) {
|
|
|
|
trace!(?arg_ty, ?input_type);
|
|
|
|
return Err("failed to normalize argument type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
let old_blocks = caller_body.basic_blocks().next_index();
|
|
|
|
self.inline_call(caller_body, &callsite, callee_body);
|
|
|
|
let new_blocks = old_blocks..caller_body.basic_blocks().next_index();
|
2020-11-01 18:00:00 -06:00
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
Ok(new_blocks)
|
|
|
|
}
|
2020-11-01 18:00:00 -06:00
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
fn check_mir_is_available(
|
|
|
|
&self,
|
|
|
|
caller_body: &Body<'tcx>,
|
|
|
|
callee: &Instance<'tcx>,
|
|
|
|
) -> Result<(), &'static str> {
|
2021-05-13 02:28:56 -05:00
|
|
|
let caller_def_id = caller_body.source.def_id();
|
2022-04-02 16:28:09 -05:00
|
|
|
let callee_def_id = callee.def_id();
|
|
|
|
if callee_def_id == caller_def_id {
|
2021-02-23 18:00:00 -06:00
|
|
|
return Err("self-recursion");
|
2020-11-08 18:00:00 -06:00
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-12-29 10:21:52 -06:00
|
|
|
match callee.def {
|
|
|
|
InstanceDef::Item(_) => {
|
|
|
|
// If there is no MIR available (either because it was not in metadata or
|
|
|
|
// because it has no MIR because it's an extern function), then the inliner
|
|
|
|
// won't cause cycles on this.
|
2022-04-02 16:28:09 -05:00
|
|
|
if !self.tcx.is_mir_available(callee_def_id) {
|
2021-02-23 18:00:00 -06:00
|
|
|
return Err("item MIR unavailable");
|
2020-12-29 10:21:52 -06:00
|
|
|
}
|
2020-11-08 18:00:00 -06:00
|
|
|
}
|
2020-12-29 10:21:52 -06:00
|
|
|
// These have no own callable MIR.
|
2021-02-23 18:00:00 -06:00
|
|
|
InstanceDef::Intrinsic(_) | InstanceDef::Virtual(..) => {
|
|
|
|
return Err("instance without MIR (intrinsic / virtual)");
|
|
|
|
}
|
2020-12-29 10:21:52 -06:00
|
|
|
// This cannot result in an immediate cycle since the callee MIR is a shim, which does
|
|
|
|
// not get any optimizations run on it. Any subsequent inlining may cause cycles, but we
|
|
|
|
// do not need to catch this here, we can wait until the inliner decides to continue
|
|
|
|
// inlining a second time.
|
2022-07-19 18:57:44 -05:00
|
|
|
InstanceDef::VTableShim(_)
|
2020-12-29 10:21:52 -06:00
|
|
|
| InstanceDef::ReifyShim(_)
|
|
|
|
| InstanceDef::FnPtrShim(..)
|
|
|
|
| InstanceDef::ClosureOnceShim { .. }
|
|
|
|
| InstanceDef::DropGlue(..)
|
2021-02-23 18:00:00 -06:00
|
|
|
| InstanceDef::CloneShim(..) => return Ok(()),
|
2020-12-29 10:21:52 -06:00
|
|
|
}
|
|
|
|
|
2022-04-02 16:28:09 -05:00
|
|
|
if self.tcx.is_constructor(callee_def_id) {
|
2020-12-29 10:21:52 -06:00
|
|
|
trace!("constructors always have MIR");
|
|
|
|
// Constructor functions cannot cause a query cycle.
|
2021-02-23 18:00:00 -06:00
|
|
|
return Ok(());
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2022-04-02 16:28:09 -05:00
|
|
|
if callee_def_id.is_local() {
|
2021-02-23 18:00:00 -06:00
|
|
|
// Avoid a cycle here by only using `instance_mir` only if we have
|
2021-05-13 02:28:56 -05:00
|
|
|
// a lower `DefPathHash` than the callee. This ensures that the callee will
|
|
|
|
// not inline us. This trick even works with incremental compilation,
|
|
|
|
// since `DefPathHash` is stable.
|
2021-05-17 14:07:42 -05:00
|
|
|
if self.tcx.def_path_hash(caller_def_id).local_hash()
|
2022-04-02 16:28:09 -05:00
|
|
|
< self.tcx.def_path_hash(callee_def_id).local_hash()
|
2021-10-30 10:11:50 -05:00
|
|
|
{
|
2021-02-23 18:00:00 -06:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we know for sure that the function we're calling will itself try to
|
|
|
|
// call us, then we avoid inlining that function.
|
2021-05-13 02:28:56 -05:00
|
|
|
if self.tcx.mir_callgraph_reachable((*callee, caller_def_id.expect_local())) {
|
2021-02-23 18:00:00 -06:00
|
|
|
return Err("caller might be reachable from callee (query cycle avoidance)");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-11-08 18:00:00 -06:00
|
|
|
} else {
|
2020-12-29 10:21:52 -06:00
|
|
|
// This cannot result in an immediate cycle since the callee MIR is from another crate
|
|
|
|
// and is already optimized. Any subsequent inlining may cause cycles, but we do
|
|
|
|
// not need to catch this here, we can wait until the inliner decides to continue
|
|
|
|
// inlining a second time.
|
|
|
|
trace!("functions from other crates always have MIR");
|
2021-02-23 18:00:00 -06:00
|
|
|
Ok(())
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
fn resolve_callsite(
|
2019-12-22 16:42:04 -06:00
|
|
|
&self,
|
2021-02-23 18:00:00 -06:00
|
|
|
caller_body: &Body<'tcx>,
|
2019-12-22 16:42:04 -06:00
|
|
|
bb: BasicBlock,
|
|
|
|
bb_data: &BasicBlockData<'tcx>,
|
2018-11-06 21:31:09 -06:00
|
|
|
) -> Option<CallSite<'tcx>> {
|
|
|
|
// Only consider direct calls to functions
|
|
|
|
let terminator = bb_data.terminator();
|
2022-04-16 08:27:54 -05:00
|
|
|
if let TerminatorKind::Call { ref func, target, .. } = terminator.kind {
|
2020-11-10 18:00:00 -06:00
|
|
|
let func_ty = func.ty(caller_body, self.tcx);
|
|
|
|
if let ty::FnDef(def_id, substs) = *func_ty.kind() {
|
|
|
|
// To resolve an instance its substs have to be fully normalized.
|
2022-06-03 11:44:46 -05:00
|
|
|
let substs = self.tcx.try_normalize_erasing_regions(self.param_env, substs).ok()?;
|
2020-02-08 13:28:19 -06:00
|
|
|
let callee =
|
2020-11-10 18:00:00 -06:00
|
|
|
Instance::resolve(self.tcx, self.param_env, def_id, substs).ok().flatten()?;
|
2018-11-06 21:31:09 -06:00
|
|
|
|
2020-02-08 13:28:19 -06:00
|
|
|
if let InstanceDef::Virtual(..) | InstanceDef::Intrinsic(_) = callee.def {
|
2018-11-06 21:31:09 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-08-14 04:28:28 -05:00
|
|
|
if self.history.contains(&callee.def_id()) {
|
2021-12-13 06:46:30 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-05-08 14:43:18 -05:00
|
|
|
let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, substs);
|
2020-11-10 18:00:00 -06:00
|
|
|
|
2020-11-08 18:00:00 -06:00
|
|
|
return Some(CallSite {
|
|
|
|
callee,
|
2020-11-10 18:00:00 -06:00
|
|
|
fn_sig,
|
2020-11-08 18:00:00 -06:00
|
|
|
block: bb,
|
2022-04-16 08:27:54 -05:00
|
|
|
target,
|
2020-11-08 18:00:00 -06:00
|
|
|
source_info: terminator.source_info,
|
|
|
|
});
|
2018-11-06 21:31:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
/// Returns an error if inlining is not possible based on codegen attributes alone. A success
|
|
|
|
/// indicates that inlining decision should be based on other criteria.
|
|
|
|
fn check_codegen_attributes(
|
|
|
|
&self,
|
|
|
|
callsite: &CallSite<'tcx>,
|
|
|
|
callee_attrs: &CodegenFnAttrs,
|
2021-07-10 10:06:24 -05:00
|
|
|
) -> Result<(), &'static str> {
|
2021-09-30 21:03:49 -05:00
|
|
|
match callee_attrs.inline {
|
|
|
|
InlineAttr::Never => return Err("never inline hint"),
|
2021-12-13 11:03:22 -06:00
|
|
|
InlineAttr::Always | InlineAttr::Hint => {}
|
|
|
|
InlineAttr::None => {
|
2021-09-30 21:03:49 -05:00
|
|
|
if self.tcx.sess.mir_opt_level() <= 2 {
|
2021-12-13 11:03:22 -06:00
|
|
|
return Err("at mir-opt-level=2, only #[inline] is inlined");
|
2021-09-30 21:03:49 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 18:00:00 -06:00
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
// Only inline local functions if they would be eligible for cross-crate
|
|
|
|
// inlining. This is to ensure that the final crate doesn't have MIR that
|
|
|
|
// reference unexported symbols
|
|
|
|
if callsite.callee.def_id().is_local() {
|
|
|
|
let is_generic = callsite.callee.substs.non_erasable_generics().next().is_some();
|
|
|
|
if !is_generic && !callee_attrs.requests_inline() {
|
|
|
|
return Err("not exported");
|
|
|
|
}
|
2020-11-10 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
if callsite.fn_sig.c_variadic() {
|
|
|
|
return Err("C variadic");
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
if callee_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
|
|
|
|
return Err("naked");
|
2020-09-02 19:00:00 -05:00
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
if callee_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
|
|
|
|
return Err("cold");
|
2020-01-11 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
if callee_attrs.no_sanitize != self.codegen_fn_attrs.no_sanitize {
|
|
|
|
return Err("incompatible sanitizer set");
|
2021-02-04 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
if callee_attrs.instruction_set != self.codegen_fn_attrs.instruction_set {
|
|
|
|
return Err("incompatible instruction set");
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
for feature in &callee_attrs.target_features {
|
|
|
|
if !self.codegen_fn_attrs.target_features.contains(feature) {
|
|
|
|
return Err("incompatible target feature");
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:00:00 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns inlining decision that is based on the examination of callee MIR body.
|
|
|
|
/// Assumes that codegen attributes have been checked for compatibility already.
|
|
|
|
#[instrument(level = "debug", skip(self, callee_body))]
|
|
|
|
fn check_mir_body(
|
|
|
|
&self,
|
|
|
|
callsite: &CallSite<'tcx>,
|
|
|
|
callee_body: &Body<'tcx>,
|
|
|
|
callee_attrs: &CodegenFnAttrs,
|
|
|
|
) -> Result<(), &'static str> {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
|
|
|
|
let mut threshold = if callee_attrs.requests_inline() {
|
2022-07-06 07:44:47 -05:00
|
|
|
self.tcx.sess.opts.unstable_opts.inline_mir_hint_threshold.unwrap_or(100)
|
2020-11-09 18:00:00 -06:00
|
|
|
} else {
|
2022-07-06 07:44:47 -05:00
|
|
|
self.tcx.sess.opts.unstable_opts.inline_mir_threshold.unwrap_or(50)
|
2020-11-09 18:00:00 -06:00
|
|
|
};
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
// Give a bonus functions with a small number of blocks,
|
|
|
|
// We normally have two or three blocks for even
|
|
|
|
// very small functions.
|
2019-06-03 17:26:48 -05:00
|
|
|
if callee_body.basic_blocks().len() <= 3 {
|
2017-02-08 03:24:49 -06:00
|
|
|
threshold += threshold / 4;
|
|
|
|
}
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!(" final inline threshold = {}", threshold);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
// FIXME: Give a bonus to functions with only a single caller
|
2022-08-15 04:36:21 -05:00
|
|
|
let diverges = matches!(
|
|
|
|
callee_body.basic_blocks()[START_BLOCK].terminator().kind,
|
|
|
|
TerminatorKind::Unreachable | TerminatorKind::Call { target: None, .. }
|
|
|
|
);
|
|
|
|
if diverges && !matches!(callee_attrs.inline, InlineAttr::Always) {
|
|
|
|
return Err("callee diverges unconditionally");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut checker = CostChecker {
|
|
|
|
tcx: self.tcx,
|
|
|
|
param_env: self.param_env,
|
|
|
|
instance: callsite.callee,
|
|
|
|
callee_body,
|
|
|
|
cost: 0,
|
2022-08-15 04:58:27 -05:00
|
|
|
validation: Ok(()),
|
2022-08-15 04:36:21 -05:00
|
|
|
};
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2022-08-15 04:36:21 -05:00
|
|
|
// Traverse the MIR manually so we can account for the effects of inlining on the CFG.
|
2017-02-08 03:24:49 -06:00
|
|
|
let mut work_list = vec![START_BLOCK];
|
2019-06-03 17:26:48 -05:00
|
|
|
let mut visited = BitSet::new_empty(callee_body.basic_blocks().len());
|
2017-02-08 03:24:49 -06:00
|
|
|
while let Some(bb) = work_list.pop() {
|
2019-12-22 16:42:04 -06:00
|
|
|
if !visited.insert(bb.index()) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-15 04:36:21 -05:00
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let blk = &callee_body.basic_blocks()[bb];
|
2022-08-15 04:36:21 -05:00
|
|
|
checker.visit_basic_block_data(bb, blk);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let term = blk.terminator();
|
2022-08-15 04:36:21 -05:00
|
|
|
if let TerminatorKind::Drop { ref place, target, unwind }
|
|
|
|
| TerminatorKind::DropAndReplace { ref place, target, unwind, .. } = term.kind
|
|
|
|
{
|
|
|
|
work_list.push(target);
|
2021-08-29 19:23:33 -05:00
|
|
|
|
2022-08-15 04:36:21 -05:00
|
|
|
// If the place doesn't actually need dropping, treat it like a regular goto.
|
|
|
|
let ty = callsite.callee.subst_mir(self.tcx, &place.ty(callee_body, tcx).ty);
|
|
|
|
if ty.needs_drop(tcx, self.param_env) && let Some(unwind) = unwind {
|
|
|
|
work_list.push(unwind);
|
2021-08-29 19:23:33 -05:00
|
|
|
}
|
2022-08-15 04:36:21 -05:00
|
|
|
} else {
|
|
|
|
work_list.extend(term.successors())
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count up the cost of local variables and temps, if we know the size
|
|
|
|
// use that, otherwise we use a moderately-large dummy cost.
|
2019-06-03 17:26:48 -05:00
|
|
|
for v in callee_body.vars_and_temps_iter() {
|
2022-08-15 04:36:21 -05:00
|
|
|
checker.visit_local_decl(v, &callee_body.local_decls[v]);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2022-08-15 04:58:27 -05:00
|
|
|
// Abort if type validation found anything fishy.
|
|
|
|
checker.validation?;
|
|
|
|
|
2022-08-15 04:36:21 -05:00
|
|
|
let cost = checker.cost;
|
2021-02-23 18:00:00 -06:00
|
|
|
if let InlineAttr::Always = callee_attrs.inline {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
|
2021-02-23 18:00:00 -06:00
|
|
|
Ok(())
|
2021-09-30 21:03:49 -05:00
|
|
|
} else if cost <= threshold {
|
|
|
|
debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
|
|
|
|
Ok(())
|
2017-02-08 03:24:49 -06:00
|
|
|
} else {
|
2021-09-30 21:03:49 -05:00
|
|
|
debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
|
|
|
|
Err("cost above threshold")
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn inline_call(
|
|
|
|
&self,
|
2020-04-12 12:31:00 -05:00
|
|
|
caller_body: &mut Body<'tcx>,
|
2021-02-23 18:00:00 -06:00
|
|
|
callsite: &CallSite<'tcx>,
|
2020-04-12 12:31:00 -05:00
|
|
|
mut callee_body: Body<'tcx>,
|
2020-11-05 18:00:00 -06:00
|
|
|
) {
|
2020-11-08 18:00:00 -06:00
|
|
|
let terminator = caller_body[callsite.block].terminator.take().unwrap();
|
2017-02-08 03:24:49 -06:00
|
|
|
match terminator.kind {
|
2020-11-08 18:00:00 -06:00
|
|
|
TerminatorKind::Call { args, destination, cleanup, .. } => {
|
2017-02-08 03:24:49 -06:00
|
|
|
// If the call is something like `a[*i] = f(i)`, where
|
|
|
|
// `i : &mut usize`, then just duplicating the `a[*i]`
|
2017-12-01 06:31:47 -06:00
|
|
|
// Place could result in two different locations if `f`
|
2017-02-08 03:24:49 -06:00
|
|
|
// writes to `i`. To prevent this we need to create a temporary
|
2017-12-01 06:39:51 -06:00
|
|
|
// borrow of the place and pass the destination as `*temp` instead.
|
2020-03-31 10:19:29 -05:00
|
|
|
fn dest_needs_borrow(place: Place<'_>) -> bool {
|
2019-07-29 17:07:28 -05:00
|
|
|
for elem in place.projection.iter() {
|
|
|
|
match elem {
|
2019-12-22 16:42:04 -06:00
|
|
|
ProjectionElem::Deref | ProjectionElem::Index(_) => return true,
|
2019-07-29 17:07:28 -05:00
|
|
|
_ => {}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2019-07-29 17:07:28 -05:00
|
|
|
}
|
2019-05-27 15:00:44 -05:00
|
|
|
|
2019-12-11 13:50:03 -06:00
|
|
|
false
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2022-04-16 08:27:54 -05:00
|
|
|
let dest = if dest_needs_borrow(destination) {
|
|
|
|
trace!("creating temp for return destination");
|
|
|
|
let dest = Rvalue::Ref(
|
|
|
|
self.tcx.lifetimes.re_erased,
|
|
|
|
BorrowKind::Mut { allow_two_phase_borrow: false },
|
|
|
|
destination,
|
|
|
|
);
|
|
|
|
let dest_ty = dest.ty(caller_body, self.tcx);
|
|
|
|
let temp = Place::from(self.new_call_temp(caller_body, &callsite, dest_ty));
|
|
|
|
caller_body[callsite.block].statements.push(Statement {
|
|
|
|
source_info: callsite.source_info,
|
|
|
|
kind: StatementKind::Assign(Box::new((temp, dest))),
|
|
|
|
});
|
|
|
|
self.tcx.mk_place_deref(temp)
|
2017-02-08 03:24:49 -06:00
|
|
|
} else {
|
2022-04-16 08:27:54 -05:00
|
|
|
destination
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
|
2018-04-18 17:47:07 -05:00
|
|
|
// Copy the arguments if needed.
|
2020-11-10 18:00:00 -06:00
|
|
|
let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2022-02-27 09:09:52 -06:00
|
|
|
let mut expn_data = ExpnData::default(
|
|
|
|
ExpnKind::Inlined,
|
|
|
|
callsite.source_info.span,
|
|
|
|
self.tcx.sess.edition(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
expn_data.def_site = callee_body.span;
|
|
|
|
let expn_data =
|
2021-07-12 15:19:25 -05:00
|
|
|
self.tcx.with_stable_hashing_context(|hcx| LocalExpnId::fresh(expn_data, hcx));
|
2017-02-08 03:24:49 -06:00
|
|
|
let mut integrator = Integrator {
|
|
|
|
args: &args,
|
2020-09-21 18:21:26 -05:00
|
|
|
new_locals: Local::new(caller_body.local_decls.len())..,
|
|
|
|
new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
|
|
|
|
new_blocks: BasicBlock::new(caller_body.basic_blocks().len())..,
|
2017-02-08 03:24:49 -06:00
|
|
|
destination: dest,
|
2022-05-18 07:58:11 -05:00
|
|
|
callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
|
|
|
|
callsite,
|
2017-02-08 03:24:49 -06:00
|
|
|
cleanup_block: cleanup,
|
2019-08-05 20:11:55 -05:00
|
|
|
in_cleanup_block: false,
|
2019-10-20 15:11:04 -05:00
|
|
|
tcx: self.tcx,
|
2022-02-27 09:09:52 -06:00
|
|
|
expn_data,
|
2020-11-14 18:00:00 -06:00
|
|
|
always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
|
2020-09-21 18:21:26 -05:00
|
|
|
// Map all `Local`s, `SourceScope`s and `BasicBlock`s to new ones
|
|
|
|
// (or existing ones, in a few special cases) in the caller.
|
|
|
|
integrator.visit_body(&mut callee_body);
|
2020-09-21 16:33:34 -05:00
|
|
|
|
2020-11-14 18:00:00 -06:00
|
|
|
// If there are any locals without storage markers, give them storage only for the
|
|
|
|
// duration of the call.
|
|
|
|
for local in callee_body.vars_and_temps_iter() {
|
2022-07-29 19:00:00 -05:00
|
|
|
if !callee_body.local_decls[local].internal
|
|
|
|
&& integrator.always_live_locals.contains(local)
|
|
|
|
{
|
2020-11-14 18:00:00 -06:00
|
|
|
let new_local = integrator.map_local(local);
|
|
|
|
caller_body[callsite.block].statements.push(Statement {
|
|
|
|
source_info: callsite.source_info,
|
|
|
|
kind: StatementKind::StorageLive(new_local),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(block) = callsite.target {
|
|
|
|
// To avoid repeated O(n) insert, push any new statements to the end and rotate
|
|
|
|
// the slice once.
|
|
|
|
let mut n = 0;
|
|
|
|
for local in callee_body.vars_and_temps_iter().rev() {
|
2022-07-29 19:00:00 -05:00
|
|
|
if !callee_body.local_decls[local].internal
|
|
|
|
&& integrator.always_live_locals.contains(local)
|
|
|
|
{
|
2020-11-14 18:00:00 -06:00
|
|
|
let new_local = integrator.map_local(local);
|
|
|
|
caller_body[block].statements.push(Statement {
|
|
|
|
source_info: callsite.source_info,
|
|
|
|
kind: StatementKind::StorageDead(new_local),
|
|
|
|
});
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
caller_body[block].statements.rotate_right(n);
|
|
|
|
}
|
|
|
|
|
2020-09-21 18:21:26 -05:00
|
|
|
// Insert all of the (mapped) parts of the callee body into the caller.
|
2021-06-03 12:24:48 -05:00
|
|
|
caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
|
2021-07-17 17:01:59 -05:00
|
|
|
caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..));
|
|
|
|
caller_body.var_debug_info.append(&mut callee_body.var_debug_info);
|
2020-09-21 18:21:26 -05:00
|
|
|
caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..));
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-11-08 18:00:00 -06:00
|
|
|
caller_body[callsite.block].terminator = Some(Terminator {
|
2020-02-08 12:24:42 -06:00
|
|
|
source_info: callsite.source_info,
|
2020-09-21 18:21:26 -05:00
|
|
|
kind: TerminatorKind::Goto { target: integrator.map_block(START_BLOCK) },
|
|
|
|
});
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-11-04 18:00:00 -06:00
|
|
|
// Copy only unevaluated constants from the callee_body into the caller_body.
|
|
|
|
// Although we are only pushing `ConstKind::Unevaluated` consts to
|
|
|
|
// `required_consts`, here we may not only have `ConstKind::Unevaluated`
|
|
|
|
// because we are calling `subst_and_normalize_erasing_regions`.
|
|
|
|
caller_body.required_consts.extend(
|
2021-03-08 10:18:03 -06:00
|
|
|
callee_body.required_consts.iter().copied().filter(|&ct| {
|
|
|
|
match ct.literal.const_for_ty() {
|
2022-06-09 20:18:06 -05:00
|
|
|
Some(ct) => matches!(ct.kind(), ConstKind::Unevaluated(_)),
|
2021-03-08 10:18:03 -06:00
|
|
|
None => true,
|
|
|
|
}
|
2020-11-04 18:00:00 -06:00
|
|
|
}),
|
|
|
|
);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2020-11-05 18:00:00 -06:00
|
|
|
kind => bug!("unexpected terminator kind {:?}", kind),
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:06:06 -06:00
|
|
|
fn make_call_args(
|
|
|
|
&self,
|
|
|
|
args: Vec<Operand<'tcx>>,
|
|
|
|
callsite: &CallSite<'tcx>,
|
2020-04-12 12:31:00 -05:00
|
|
|
caller_body: &mut Body<'tcx>,
|
2020-11-10 18:00:00 -06:00
|
|
|
callee_body: &Body<'tcx>,
|
2017-11-27 13:01:30 -06:00
|
|
|
) -> Vec<Local> {
|
2017-02-08 03:24:49 -06:00
|
|
|
let tcx = self.tcx;
|
2017-11-10 08:06:06 -06:00
|
|
|
|
2017-11-18 17:02:41 -06:00
|
|
|
// There is a bit of a mismatch between the *caller* of a closure and the *callee*.
|
|
|
|
// The caller provides the arguments wrapped up in a tuple:
|
|
|
|
//
|
|
|
|
// tuple_tmp = (a, b, c)
|
|
|
|
// Fn::call(closure_ref, tuple_tmp)
|
|
|
|
//
|
|
|
|
// meanwhile the closure body expects the arguments (here, `a`, `b`, and `c`)
|
2018-05-08 08:10:16 -05:00
|
|
|
// as distinct arguments. (This is the "rust-call" ABI hack.) Normally, codegen has
|
|
|
|
// the job of unpacking this tuple. But here, we are codegen. =) So we want to create
|
2017-11-18 17:02:41 -06:00
|
|
|
// a vector like
|
|
|
|
//
|
|
|
|
// [closure_ref, tuple_tmp.0, tuple_tmp.1, tuple_tmp.2]
|
|
|
|
//
|
|
|
|
// Except for one tiny wrinkle: we don't actually want `tuple_tmp.0`. It's more convenient
|
|
|
|
// if we "spill" that into *another* temporary, so that we can map the argument
|
|
|
|
// variable in the callee MIR directly to an argument variable on our side.
|
|
|
|
// So we introduce temporaries like:
|
|
|
|
//
|
|
|
|
// tmp0 = tuple_tmp.0
|
|
|
|
// tmp1 = tuple_tmp.1
|
|
|
|
// tmp2 = tuple_tmp.2
|
|
|
|
//
|
|
|
|
// and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
|
2020-11-10 18:00:00 -06:00
|
|
|
if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
|
2017-11-10 08:06:06 -06:00
|
|
|
let mut args = args.into_iter();
|
2020-11-08 18:00:00 -06:00
|
|
|
let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
|
|
|
|
let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_body);
|
2017-11-10 08:06:06 -06:00
|
|
|
assert!(args.next().is_none());
|
|
|
|
|
2019-06-24 10:46:09 -05:00
|
|
|
let tuple = Place::from(tuple);
|
2021-10-15 20:45:14 -05:00
|
|
|
let ty::Tuple(tuple_tys) = tuple.ty(caller_body, tcx).ty.kind() else {
|
2017-11-10 08:06:06 -06:00
|
|
|
bug!("Closure arguments are not passed as a tuple");
|
|
|
|
};
|
|
|
|
|
2017-11-18 17:02:41 -06:00
|
|
|
// The `closure_ref` in our example above.
|
2017-11-27 13:01:30 -06:00
|
|
|
let closure_ref_arg = iter::once(self_);
|
2017-11-18 17:02:41 -06:00
|
|
|
|
2022-03-30 14:14:15 -05:00
|
|
|
// The `tmp0`, `tmp1`, and `tmp2` in our example above.
|
2019-12-22 16:42:04 -06:00
|
|
|
let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| {
|
|
|
|
// This is e.g., `tuple_tmp.0` in our example above.
|
2022-02-07 09:06:31 -06:00
|
|
|
let tuple_field = Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty));
|
2019-12-22 16:42:04 -06:00
|
|
|
|
|
|
|
// Spill to a local to make e.g., `tmp0`.
|
2020-11-08 18:00:00 -06:00
|
|
|
self.create_temp_if_necessary(tuple_field, callsite, caller_body)
|
2019-12-22 16:42:04 -06:00
|
|
|
});
|
2017-11-18 17:02:41 -06:00
|
|
|
|
|
|
|
closure_ref_arg.chain(tuple_tmp_args).collect()
|
2017-11-10 08:06:06 -06:00
|
|
|
} else {
|
|
|
|
args.into_iter()
|
2020-11-08 18:00:00 -06:00
|
|
|
.map(|a| self.create_temp_if_necessary(a, callsite, caller_body))
|
2017-11-10 08:06:06 -06:00
|
|
|
.collect()
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2017-11-10 11:05:29 -06:00
|
|
|
|
|
|
|
/// If `arg` is already a temporary, returns it. Otherwise, introduces a fresh
|
|
|
|
/// temporary `T` and an instruction `T = arg`, and returns `T`.
|
|
|
|
fn create_temp_if_necessary(
|
|
|
|
&self,
|
|
|
|
arg: Operand<'tcx>,
|
|
|
|
callsite: &CallSite<'tcx>,
|
2020-04-12 12:31:00 -05:00
|
|
|
caller_body: &mut Body<'tcx>,
|
2017-11-27 13:01:30 -06:00
|
|
|
) -> Local {
|
2020-11-08 18:00:00 -06:00
|
|
|
// Reuse the operand if it is a moved temporary.
|
2022-03-01 04:43:12 -06:00
|
|
|
if let Operand::Move(place) = &arg
|
|
|
|
&& let Some(local) = place.as_local()
|
|
|
|
&& caller_body.local_kind(local) == LocalKind::Temp
|
|
|
|
{
|
|
|
|
return local;
|
2017-11-10 11:05:29 -06:00
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-11-08 18:00:00 -06:00
|
|
|
// Otherwise, create a temporary for the argument.
|
2020-11-06 18:00:00 -06:00
|
|
|
trace!("creating temp for argument {:?}", arg);
|
2020-11-08 18:00:00 -06:00
|
|
|
let arg_ty = arg.ty(caller_body, self.tcx);
|
|
|
|
let local = self.new_call_temp(caller_body, callsite, arg_ty);
|
|
|
|
caller_body[callsite.block].statements.push(Statement {
|
2020-02-08 12:24:42 -06:00
|
|
|
source_info: callsite.source_info,
|
2021-08-04 22:36:38 -05:00
|
|
|
kind: StatementKind::Assign(Box::new((Place::from(local), Rvalue::Use(arg)))),
|
2020-09-02 19:00:00 -05:00
|
|
|
});
|
2020-11-08 18:00:00 -06:00
|
|
|
local
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Introduces a new temporary into the caller body that is live for the duration of the call.
|
|
|
|
fn new_call_temp(
|
|
|
|
&self,
|
|
|
|
caller_body: &mut Body<'tcx>,
|
|
|
|
callsite: &CallSite<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Local {
|
|
|
|
let local = caller_body.local_decls.push(LocalDecl::new(ty, callsite.source_info.span));
|
|
|
|
|
|
|
|
caller_body[callsite.block].statements.push(Statement {
|
2020-02-08 12:24:42 -06:00
|
|
|
source_info: callsite.source_info,
|
2020-11-08 18:00:00 -06:00
|
|
|
kind: StatementKind::StorageLive(local),
|
2020-09-02 19:00:00 -05:00
|
|
|
});
|
2020-11-08 18:00:00 -06:00
|
|
|
|
|
|
|
if let Some(block) = callsite.target {
|
|
|
|
caller_body[block].statements.insert(
|
|
|
|
0,
|
|
|
|
Statement {
|
|
|
|
source_info: callsite.source_info,
|
|
|
|
kind: StatementKind::StorageDead(local),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
local
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:11:55 -05:00
|
|
|
fn type_size_of<'tcx>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 16:11:55 -05:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<u64> {
|
2018-01-31 16:00:38 -06:00
|
|
|
tcx.layout_of(param_env.and(ty)).ok().map(|layout| layout.size.bytes())
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2022-08-15 04:36:21 -05:00
|
|
|
/// Verify that the callee body is compatible with the caller.
|
|
|
|
///
|
|
|
|
/// This visitor mostly computes the inlining cost,
|
|
|
|
/// but also needs to verify that types match because of normalization failure.
|
|
|
|
struct CostChecker<'b, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
|
|
|
cost: usize,
|
|
|
|
callee_body: &'b Body<'tcx>,
|
|
|
|
instance: ty::Instance<'tcx>,
|
2022-08-15 04:58:27 -05:00
|
|
|
validation: Result<(), &'static str>,
|
2022-08-15 04:36:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
|
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
|
|
|
|
// Don't count StorageLive/StorageDead in the inlining cost.
|
|
|
|
match statement.kind {
|
|
|
|
StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_)
|
|
|
|
| StatementKind::Deinit(_)
|
|
|
|
| StatementKind::Nop => {}
|
|
|
|
_ => self.cost += INSTR_COST,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_statement(statement, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
match terminator.kind {
|
|
|
|
TerminatorKind::Drop { ref place, unwind, .. }
|
|
|
|
| TerminatorKind::DropAndReplace { ref place, unwind, .. } => {
|
|
|
|
// If the place doesn't actually need dropping, treat it like a regular goto.
|
|
|
|
let ty = self.instance.subst_mir(tcx, &place.ty(self.callee_body, tcx).ty);
|
|
|
|
if ty.needs_drop(tcx, self.param_env) {
|
|
|
|
self.cost += CALL_PENALTY;
|
|
|
|
if unwind.is_some() {
|
|
|
|
self.cost += LANDINGPAD_PENALTY;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.cost += INSTR_COST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
|
|
|
|
let fn_ty = self.instance.subst_mir(tcx, &f.literal.ty());
|
|
|
|
self.cost += if let ty::FnDef(def_id, _) = *fn_ty.kind() && tcx.is_intrinsic(def_id) {
|
|
|
|
// Don't give intrinsics the extra penalty for calls
|
|
|
|
INSTR_COST
|
|
|
|
} else {
|
|
|
|
CALL_PENALTY
|
|
|
|
};
|
|
|
|
if cleanup.is_some() {
|
|
|
|
self.cost += LANDINGPAD_PENALTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert { cleanup, .. } => {
|
|
|
|
self.cost += CALL_PENALTY;
|
|
|
|
if cleanup.is_some() {
|
|
|
|
self.cost += LANDINGPAD_PENALTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Resume => self.cost += RESUME_PENALTY,
|
|
|
|
TerminatorKind::InlineAsm { cleanup, .. } => {
|
|
|
|
self.cost += INSTR_COST;
|
|
|
|
if cleanup.is_some() {
|
|
|
|
self.cost += LANDINGPAD_PENALTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => self.cost += INSTR_COST,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_terminator(terminator, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Count up the cost of local variables and temps, if we know the size
|
|
|
|
/// use that, otherwise we use a moderately-large dummy cost.
|
|
|
|
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let ptr_size = tcx.data_layout.pointer_size.bytes();
|
|
|
|
|
|
|
|
let ty = self.instance.subst_mir(tcx, &local_decl.ty);
|
|
|
|
// Cost of the var is the size in machine-words, if we know
|
|
|
|
// it.
|
|
|
|
if let Some(size) = type_size_of(tcx, self.param_env, ty) {
|
|
|
|
self.cost += ((size + ptr_size - 1) / ptr_size) as usize;
|
|
|
|
} else {
|
|
|
|
self.cost += UNKNOWN_SIZE_COST;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_local_decl(local, local_decl)
|
|
|
|
}
|
2022-08-15 04:58:27 -05:00
|
|
|
|
|
|
|
/// This method duplicates code from MIR validation in an attempt to detect type mismatches due
|
|
|
|
/// to normalization failure.
|
|
|
|
fn visit_projection_elem(
|
|
|
|
&mut self,
|
|
|
|
local: Local,
|
|
|
|
proj_base: &[PlaceElem<'tcx>],
|
|
|
|
elem: PlaceElem<'tcx>,
|
|
|
|
context: PlaceContext,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
if let ProjectionElem::Field(f, ty) = elem {
|
|
|
|
let parent = Place { local, projection: self.tcx.intern_place_elems(proj_base) };
|
|
|
|
let parent_ty = parent.ty(&self.callee_body.local_decls, self.tcx);
|
|
|
|
let check_equal = |this: &mut Self, f_ty| {
|
|
|
|
if !equal_up_to_regions(this.tcx, this.param_env, ty, f_ty) {
|
|
|
|
trace!(?ty, ?f_ty);
|
|
|
|
this.validation = Err("failed to normalize projection type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let kind = match parent_ty.ty.kind() {
|
|
|
|
&ty::Opaque(def_id, substs) => {
|
|
|
|
self.tcx.bound_type_of(def_id).subst(self.tcx, substs).kind()
|
|
|
|
}
|
|
|
|
kind => kind,
|
|
|
|
};
|
|
|
|
|
|
|
|
match kind {
|
|
|
|
ty::Tuple(fields) => {
|
|
|
|
let Some(f_ty) = fields.get(f.as_usize()) else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
check_equal(self, *f_ty);
|
|
|
|
}
|
|
|
|
ty::Adt(adt_def, substs) => {
|
|
|
|
let var = parent_ty.variant_index.unwrap_or(VariantIdx::from_u32(0));
|
|
|
|
let Some(field) = adt_def.variant(var).fields.get(f.as_usize()) else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
check_equal(self, field.ty(self.tcx, substs));
|
|
|
|
}
|
|
|
|
ty::Closure(_, substs) => {
|
|
|
|
let substs = substs.as_closure();
|
|
|
|
let Some(f_ty) = substs.upvar_tys().nth(f.as_usize()) else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
check_equal(self, f_ty);
|
|
|
|
}
|
|
|
|
&ty::Generator(def_id, substs, _) => {
|
|
|
|
let f_ty = if let Some(var) = parent_ty.variant_index {
|
|
|
|
let gen_body = if def_id == self.callee_body.source.def_id() {
|
|
|
|
self.callee_body
|
|
|
|
} else {
|
|
|
|
self.tcx.optimized_mir(def_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(layout) = gen_body.generator_layout() else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(&local) = layout.variant_fields[var].get(f) else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(&f_ty) = layout.field_tys.get(local) else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
f_ty
|
|
|
|
} else {
|
|
|
|
let Some(f_ty) = substs.as_generator().prefix_tys().nth(f.index()) else {
|
|
|
|
self.validation = Err("malformed MIR");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
f_ty
|
|
|
|
};
|
|
|
|
|
|
|
|
check_equal(self, f_ty);
|
|
|
|
}
|
|
|
|
_ => self.validation = Err("malformed MIR"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_projection_elem(local, proj_base, elem, context, location);
|
|
|
|
}
|
2022-08-15 04:36:21 -05:00
|
|
|
}
|
|
|
|
|
2017-02-08 03:24:49 -06:00
|
|
|
/**
|
|
|
|
* Integrator.
|
|
|
|
*
|
|
|
|
* Integrates blocks from the callee function into the calling function.
|
|
|
|
* Updates block indices, references to locals and other control flow
|
|
|
|
* stuff.
|
2019-06-11 16:11:55 -05:00
|
|
|
*/
|
2019-06-14 11:39:39 -05:00
|
|
|
struct Integrator<'a, 'tcx> {
|
2017-11-27 13:01:30 -06:00
|
|
|
args: &'a [Local],
|
2020-09-21 18:21:26 -05:00
|
|
|
new_locals: RangeFrom<Local>,
|
|
|
|
new_scopes: RangeFrom<SourceScope>,
|
|
|
|
new_blocks: RangeFrom<BasicBlock>,
|
2017-12-01 06:31:47 -06:00
|
|
|
destination: Place<'tcx>,
|
2022-05-18 07:58:11 -05:00
|
|
|
callsite_scope: SourceScopeData<'tcx>,
|
|
|
|
callsite: &'a CallSite<'tcx>,
|
2017-02-08 03:24:49 -06:00
|
|
|
cleanup_block: Option<BasicBlock>,
|
|
|
|
in_cleanup_block: bool,
|
2019-10-20 15:11:04 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-02-27 09:09:52 -06:00
|
|
|
expn_data: LocalExpnId,
|
2020-11-14 18:00:00 -06:00
|
|
|
always_live_locals: BitSet<Local>,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl Integrator<'_, '_> {
|
2020-09-21 18:21:26 -05:00
|
|
|
fn map_local(&self, local: Local) -> Local {
|
|
|
|
let new = if local == RETURN_PLACE {
|
|
|
|
self.destination.local
|
|
|
|
} else {
|
|
|
|
let idx = local.index() - 1;
|
|
|
|
if idx < self.args.len() {
|
|
|
|
self.args[idx]
|
|
|
|
} else {
|
|
|
|
Local::new(self.new_locals.start.index() + (idx - self.args.len()))
|
|
|
|
}
|
|
|
|
};
|
2020-11-06 18:00:00 -06:00
|
|
|
trace!("mapping local `{:?}` to `{:?}`", local, new);
|
2017-02-08 03:24:49 -06:00
|
|
|
new
|
|
|
|
}
|
|
|
|
|
2020-09-21 18:21:26 -05:00
|
|
|
fn map_scope(&self, scope: SourceScope) -> SourceScope {
|
|
|
|
let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
|
2020-11-06 18:00:00 -06:00
|
|
|
trace!("mapping scope `{:?}` to `{:?}`", scope, new);
|
2020-09-21 18:21:26 -05:00
|
|
|
new
|
|
|
|
}
|
2019-10-07 16:23:39 -05:00
|
|
|
|
2020-09-21 18:21:26 -05:00
|
|
|
fn map_block(&self, block: BasicBlock) -> BasicBlock {
|
|
|
|
let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
|
2020-11-06 18:00:00 -06:00
|
|
|
trace!("mapping block `{:?}` to `{:?}`", block, new);
|
2020-09-21 18:21:26 -05:00
|
|
|
new
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
2019-10-07 16:23:39 -05:00
|
|
|
}
|
2017-09-03 11:14:31 -05:00
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
|
2019-10-20 15:11:04 -05:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn visit_local(&mut self, local: &mut Local, _ctxt: PlaceContext, _location: Location) {
|
2020-09-21 18:21:26 -05:00
|
|
|
*local = self.map_local(*local);
|
|
|
|
}
|
|
|
|
|
2022-05-18 07:58:11 -05:00
|
|
|
fn visit_source_scope_data(&mut self, scope_data: &mut SourceScopeData<'tcx>) {
|
|
|
|
self.super_source_scope_data(scope_data);
|
|
|
|
if scope_data.parent_scope.is_none() {
|
|
|
|
// Attach the outermost callee scope as a child of the callsite
|
|
|
|
// scope, via the `parent_scope` and `inlined_parent_scope` chains.
|
|
|
|
scope_data.parent_scope = Some(self.callsite.source_info.scope);
|
|
|
|
assert_eq!(scope_data.inlined_parent_scope, None);
|
|
|
|
scope_data.inlined_parent_scope = if self.callsite_scope.inlined.is_some() {
|
|
|
|
Some(self.callsite.source_info.scope)
|
|
|
|
} else {
|
|
|
|
self.callsite_scope.inlined_parent_scope
|
|
|
|
};
|
|
|
|
|
|
|
|
// Mark the outermost callee scope as an inlined one.
|
|
|
|
assert_eq!(scope_data.inlined, None);
|
|
|
|
scope_data.inlined = Some((self.callsite.callee, self.callsite.source_info.span));
|
|
|
|
} else if scope_data.inlined_parent_scope.is_none() {
|
|
|
|
// Make it easy to find the scope with `inlined` set above.
|
|
|
|
scope_data.inlined_parent_scope = Some(self.map_scope(OUTERMOST_SOURCE_SCOPE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 18:21:26 -05:00
|
|
|
fn visit_source_scope(&mut self, scope: &mut SourceScope) {
|
|
|
|
*scope = self.map_scope(*scope);
|
2019-10-07 16:23:39 -05:00
|
|
|
}
|
2018-07-21 18:01:07 -05:00
|
|
|
|
2020-09-19 11:56:32 -05:00
|
|
|
fn visit_span(&mut self, span: &mut Span) {
|
|
|
|
// Make sure that all spans track the fact that they were inlined.
|
2022-02-27 09:09:52 -06:00
|
|
|
*span = span.fresh_expansion(self.expn_data);
|
2020-09-19 11:56:32 -05:00
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:55 -06:00
|
|
|
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
|
2020-11-06 18:00:00 -06:00
|
|
|
for elem in place.projection {
|
|
|
|
// FIXME: Make sure that return place is not used in an indexing projection, since it
|
|
|
|
// won't be rebased as it is supposed to be.
|
|
|
|
assert_ne!(ProjectionElem::Index(RETURN_PLACE), elem);
|
|
|
|
}
|
|
|
|
|
2019-12-11 13:50:03 -06:00
|
|
|
// If this is the `RETURN_PLACE`, we need to rebase any projections onto it.
|
|
|
|
let dest_proj_len = self.destination.projection.len();
|
|
|
|
if place.local == RETURN_PLACE && dest_proj_len > 0 {
|
|
|
|
let mut projs = Vec::with_capacity(dest_proj_len + place.projection.len());
|
|
|
|
projs.extend(self.destination.projection);
|
|
|
|
projs.extend(place.projection);
|
|
|
|
|
|
|
|
place.projection = self.tcx.intern_place_elems(&*projs);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2020-01-01 19:10:55 -06:00
|
|
|
// Handles integrating any locals that occur in the base
|
|
|
|
// or projections
|
|
|
|
self.super_place(place, context, location)
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
|
|
|
|
self.in_cleanup_block = data.is_cleanup;
|
|
|
|
self.super_basic_block_data(block, data);
|
|
|
|
self.in_cleanup_block = false;
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn visit_retag(&mut self, kind: &mut RetagKind, place: &mut Place<'tcx>, loc: Location) {
|
2018-12-11 12:54:38 -06:00
|
|
|
self.super_retag(kind, place, loc);
|
2018-10-24 06:47:48 -05:00
|
|
|
|
|
|
|
// We have to patch all inlined retags to be aware that they are no longer
|
|
|
|
// happening on function entry.
|
2018-12-11 12:54:38 -06:00
|
|
|
if *kind == RetagKind::FnEntry {
|
|
|
|
*kind = RetagKind::Default;
|
|
|
|
}
|
2018-10-24 06:47:48 -05:00
|
|
|
}
|
|
|
|
|
2020-11-14 18:00:00 -06:00
|
|
|
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, location: Location) {
|
|
|
|
if let StatementKind::StorageLive(local) | StatementKind::StorageDead(local) =
|
|
|
|
statement.kind
|
|
|
|
{
|
|
|
|
self.always_live_locals.remove(local);
|
|
|
|
}
|
|
|
|
self.super_statement(statement, location);
|
|
|
|
}
|
|
|
|
|
2020-05-31 05:13:29 -05:00
|
|
|
fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, loc: Location) {
|
2020-05-09 09:08:04 -05:00
|
|
|
// Don't try to modify the implicit `_0` access on return (`return` terminators are
|
|
|
|
// replaced down below anyways).
|
2020-05-31 05:13:29 -05:00
|
|
|
if !matches!(terminator.kind, TerminatorKind::Return) {
|
|
|
|
self.super_terminator(terminator, loc);
|
2020-05-09 09:08:04 -05:00
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-05-31 05:13:29 -05:00
|
|
|
match terminator.kind {
|
2019-12-22 16:42:04 -06:00
|
|
|
TerminatorKind::GeneratorDrop | TerminatorKind::Yield { .. } => bug!(),
|
|
|
|
TerminatorKind::Goto { ref mut target } => {
|
2020-09-21 18:21:26 -05:00
|
|
|
*target = self.map_block(*target);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
TerminatorKind::SwitchInt { ref mut targets, .. } => {
|
2020-10-10 10:36:04 -05:00
|
|
|
for tgt in targets.all_targets_mut() {
|
2020-09-21 18:21:26 -05:00
|
|
|
*tgt = self.map_block(*tgt);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
TerminatorKind::Drop { ref mut target, ref mut unwind, .. }
|
|
|
|
| TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
|
2020-09-21 18:21:26 -05:00
|
|
|
*target = self.map_block(*target);
|
2017-02-08 03:24:49 -06:00
|
|
|
if let Some(tgt) = *unwind {
|
2020-09-21 18:21:26 -05:00
|
|
|
*unwind = Some(self.map_block(tgt));
|
2017-02-08 03:24:49 -06:00
|
|
|
} else if !self.in_cleanup_block {
|
|
|
|
// Unless this drop is in a cleanup block, add an unwind edge to
|
2018-08-19 08:30:23 -05:00
|
|
|
// the original call's cleanup block
|
2017-02-08 03:24:49 -06:00
|
|
|
*unwind = self.cleanup_block;
|
|
|
|
}
|
|
|
|
}
|
2022-04-16 08:27:54 -05:00
|
|
|
TerminatorKind::Call { ref mut target, ref mut cleanup, .. } => {
|
|
|
|
if let Some(ref mut tgt) = *target {
|
2020-09-21 18:21:26 -05:00
|
|
|
*tgt = self.map_block(*tgt);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
if let Some(tgt) = *cleanup {
|
2020-09-21 18:21:26 -05:00
|
|
|
*cleanup = Some(self.map_block(tgt));
|
2017-02-08 03:24:49 -06:00
|
|
|
} else if !self.in_cleanup_block {
|
|
|
|
// Unless this call is in a cleanup block, add an unwind edge to
|
2018-08-19 08:30:23 -05:00
|
|
|
// the original call's cleanup block
|
2017-02-08 03:24:49 -06:00
|
|
|
*cleanup = self.cleanup_block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert { ref mut target, ref mut cleanup, .. } => {
|
2020-09-21 18:21:26 -05:00
|
|
|
*target = self.map_block(*target);
|
2017-02-08 03:24:49 -06:00
|
|
|
if let Some(tgt) = *cleanup {
|
2020-09-21 18:21:26 -05:00
|
|
|
*cleanup = Some(self.map_block(tgt));
|
2017-02-08 03:24:49 -06:00
|
|
|
} else if !self.in_cleanup_block {
|
|
|
|
// Unless this assert is in a cleanup block, add an unwind edge to
|
2018-08-19 08:30:23 -05:00
|
|
|
// the original call's cleanup block
|
2017-02-08 03:24:49 -06:00
|
|
|
*cleanup = self.cleanup_block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Return => {
|
2022-05-18 07:58:11 -05:00
|
|
|
terminator.kind = if let Some(tgt) = self.callsite.target {
|
2020-11-08 18:00:00 -06:00
|
|
|
TerminatorKind::Goto { target: tgt }
|
|
|
|
} else {
|
|
|
|
TerminatorKind::Unreachable
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
TerminatorKind::Resume => {
|
|
|
|
if let Some(tgt) = self.cleanup_block {
|
2020-05-31 05:13:29 -05:00
|
|
|
terminator.kind = TerminatorKind::Goto { target: tgt }
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
TerminatorKind::Abort => {}
|
|
|
|
TerminatorKind::Unreachable => {}
|
2020-06-02 02:15:24 -05:00
|
|
|
TerminatorKind::FalseEdge { ref mut real_target, ref mut imaginary_target } => {
|
2020-09-21 18:21:26 -05:00
|
|
|
*real_target = self.map_block(*real_target);
|
|
|
|
*imaginary_target = self.map_block(*imaginary_target);
|
2017-10-13 08:36:15 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
TerminatorKind::FalseUnwind { real_target: _, unwind: _ } =>
|
|
|
|
// see the ordering of passes in the optimized_mir query.
|
|
|
|
{
|
2018-01-25 00:45:45 -06:00
|
|
|
bug!("False unwinds should have been removed before inlining")
|
2019-12-22 16:42:04 -06:00
|
|
|
}
|
2021-08-29 19:23:33 -05:00
|
|
|
TerminatorKind::InlineAsm { ref mut destination, ref mut cleanup, .. } => {
|
2020-02-14 12:17:50 -06:00
|
|
|
if let Some(ref mut tgt) = *destination {
|
2020-09-21 18:21:26 -05:00
|
|
|
*tgt = self.map_block(*tgt);
|
2021-08-29 19:23:33 -05:00
|
|
|
} else if !self.in_cleanup_block {
|
|
|
|
// Unless this inline asm is in a cleanup block, add an unwind edge to
|
|
|
|
// the original call's cleanup block
|
|
|
|
*cleanup = self.cleanup_block;
|
2020-02-14 12:17:50 -06:00
|
|
|
}
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|