2017-02-08 03:24:49 -06:00
|
|
|
//! Inlining pass for MIR functions
|
|
|
|
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_attr as attr;
|
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_index::bit_set::BitSet;
|
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
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::*;
|
|
|
|
use rustc_middle::ty::subst::{Subst, SubstsRef};
|
2020-04-05 12:09:45 -05:00
|
|
|
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
|
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-10-04 13:01:38 -05:00
|
|
|
use crate::transform::MirPass;
|
2017-05-01 15:03:05 -05:00
|
|
|
use std::collections::VecDeque;
|
2017-11-18 17:02:41 -06:00
|
|
|
use std::iter;
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
const DEFAULT_THRESHOLD: usize = 50;
|
|
|
|
const HINT_THRESHOLD: usize = 100;
|
|
|
|
|
|
|
|
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> {
|
|
|
|
callee: DefId,
|
2019-02-09 08:11:53 -06:00
|
|
|
substs: SubstsRef<'tcx>,
|
2017-05-01 15:03:05 -05:00
|
|
|
bb: BasicBlock,
|
|
|
|
location: SourceInfo,
|
2017-05-01 11:47:00 -05:00
|
|
|
}
|
|
|
|
|
2019-08-04 15:20:00 -05:00
|
|
|
impl<'tcx> MirPass<'tcx> for Inline {
|
2020-10-04 13:01:38 -05:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
2017-05-01 15:03:05 -05:00
|
|
|
if tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
|
2020-06-22 01:29:08 -05:00
|
|
|
if tcx.sess.opts.debugging_opts.instrument_coverage {
|
|
|
|
// The current implementation of source code coverage injects code region counters
|
|
|
|
// into the MIR, and assumes a 1-to-1 correspondence between MIR and source-code-
|
|
|
|
// based function.
|
|
|
|
debug!("function inlining is disabled when compiling with `instrument_coverage`");
|
|
|
|
} else {
|
2020-10-05 04:04:25 -05:00
|
|
|
Inliner {
|
|
|
|
tcx,
|
|
|
|
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
|
|
|
|
codegen_fn_attrs: tcx.codegen_fn_attrs(body.source.def_id()),
|
|
|
|
}
|
|
|
|
.run_pass(body);
|
2020-06-22 01:29:08 -05:00
|
|
|
}
|
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-09-02 19:00:00 -05:00
|
|
|
codegen_fn_attrs: &'tcx CodegenFnAttrs,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl Inliner<'tcx> {
|
2020-04-12 12:31:00 -05:00
|
|
|
fn run_pass(&self, caller_body: &mut Body<'tcx>) {
|
2017-05-01 15:03:05 -05:00
|
|
|
// Keep a queue of callsites to try inlining on. We take
|
|
|
|
// advantage of the fact that queries detect cycles here to
|
|
|
|
// allow us to try and fetch the fully optimized MIR of a
|
|
|
|
// call; if it succeeds, we can inline it and we know that
|
|
|
|
// they do not call us. Otherwise, we just don't try to
|
|
|
|
// inline.
|
|
|
|
//
|
|
|
|
// We use a queue so that we inline "broadly" before we inline
|
2017-05-02 05:33:11 -05:00
|
|
|
// in depth. It is unclear if this is the best heuristic,
|
|
|
|
// really, but that's true of all the heuristics in this
|
|
|
|
// file. =)
|
2017-05-01 15:03:05 -05:00
|
|
|
|
|
|
|
let mut callsites = VecDeque::new();
|
|
|
|
|
2020-10-04 13:01:38 -05:00
|
|
|
let def_id = caller_body.source.def_id();
|
|
|
|
|
2017-05-01 15:03:05 -05:00
|
|
|
// Only do inlining into fn bodies.
|
2020-10-04 13:01:38 -05:00
|
|
|
let self_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
|
|
|
if self.tcx.hir().body_owner_kind(self_hir_id).is_fn_or_closure()
|
|
|
|
&& caller_body.source.promoted.is_none()
|
|
|
|
{
|
2019-11-06 11:00:46 -06:00
|
|
|
for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated() {
|
2020-10-05 04:04:25 -05:00
|
|
|
if let Some(callsite) = self.get_valid_function_call(bb, bb_data, caller_body) {
|
2018-11-06 21:31:09 -06:00
|
|
|
callsites.push_back(callsite);
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2017-11-10 05:31:51 -06:00
|
|
|
} else {
|
|
|
|
return;
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut local_change;
|
|
|
|
let mut changed = false;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
local_change = false;
|
2017-05-01 15:03:05 -05:00
|
|
|
while let Some(callsite) = callsites.pop_front() {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("checking whether to inline callsite {:?}", callsite);
|
2017-05-02 05:08:13 -05:00
|
|
|
if !self.tcx.is_mir_available(callsite.callee) {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("checking whether to inline callsite {:?} - MIR unavailable", callsite);
|
2017-05-01 15:03:05 -05:00
|
|
|
continue;
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-04-16 14:36:32 -05:00
|
|
|
let callee_body = if let Some(callee_def_id) = callsite.callee.as_local() {
|
2020-08-12 05:22:56 -05:00
|
|
|
let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id);
|
2019-01-24 13:05:19 -06:00
|
|
|
// Avoid a cycle here by only using `optimized_mir` only if we have
|
2020-04-18 08:26:45 -05:00
|
|
|
// a lower `HirId` than the callee. This ensures that the callee will
|
2019-01-24 13:05:19 -06:00
|
|
|
// not inline us. This trick only works without incremental compilation.
|
2020-09-01 19:00:00 -05:00
|
|
|
// So don't do it if that is enabled. Also 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 !self.tcx.dep_graph.is_fully_enabled()
|
|
|
|
&& self_hir_id < callee_hir_id
|
|
|
|
&& caller_body.generator_kind.is_none()
|
|
|
|
{
|
2019-01-24 13:05:19 -06:00
|
|
|
self.tcx.optimized_mir(callsite.callee)
|
|
|
|
} else {
|
|
|
|
continue;
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2019-01-24 13:05:19 -06:00
|
|
|
} else {
|
|
|
|
// This cannot result in a cycle since the callee MIR is from another crate
|
|
|
|
// and is already optimized.
|
|
|
|
self.tcx.optimized_mir(callsite.callee)
|
|
|
|
};
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let callee_body = if self.consider_optimizing(callsite, callee_body) {
|
2019-01-24 13:05:19 -06:00
|
|
|
self.tcx.subst_and_normalize_erasing_regions(
|
|
|
|
&callsite.substs,
|
2020-10-05 04:04:25 -05:00
|
|
|
self.param_env,
|
2019-06-03 17:26:48 -05:00
|
|
|
callee_body,
|
2019-01-24 13:05:19 -06:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
continue;
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
|
2020-04-05 12:09:45 -05:00
|
|
|
// Copy only unevaluated constants from the callee_body into the caller_body.
|
2020-04-22 09:16:06 -05:00
|
|
|
// 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(
|
|
|
|
callee_body.required_consts.iter().copied().filter(|&constant| {
|
|
|
|
matches!(constant.literal.val, ConstKind::Unevaluated(_, _, _))
|
|
|
|
}),
|
|
|
|
);
|
2020-04-05 12:09:45 -05:00
|
|
|
|
2019-11-06 11:00:46 -06:00
|
|
|
let start = caller_body.basic_blocks().len();
|
2019-06-03 17:26:48 -05:00
|
|
|
debug!("attempting to inline callsite {:?} - body={:?}", callsite, callee_body);
|
2019-11-06 11:00:46 -06:00
|
|
|
if !self.inline_call(callsite, caller_body, callee_body) {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("attempting to inline callsite {:?} - failure", callsite);
|
2017-02-08 03:24:49 -06:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("attempting to inline callsite {:?} - success", callsite);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
// Add callsites from inlined function
|
2019-12-22 16:42:04 -06:00
|
|
|
for (bb, bb_data) in caller_body.basic_blocks().iter_enumerated().skip(start) {
|
|
|
|
if let Some(new_callsite) =
|
2020-10-05 04:04:25 -05:00
|
|
|
self.get_valid_function_call(bb, bb_data, caller_body)
|
2019-12-22 16:42:04 -06:00
|
|
|
{
|
2018-11-06 21:31:09 -06:00
|
|
|
// Don't inline the same function multiple times.
|
|
|
|
if callsite.callee != new_callsite.callee {
|
|
|
|
callsites.push_back(new_callsite);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local_change = true;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !local_change {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 15:03:05 -05:00
|
|
|
// Simplify if we inlined anything.
|
|
|
|
if changed {
|
2020-10-04 13:01:38 -05:00
|
|
|
debug!("running simplify cfg on {:?}", caller_body.source);
|
2019-11-06 11:00:46 -06:00
|
|
|
CfgSimplifier::new(caller_body).simplify();
|
|
|
|
remove_dead_blocks(caller_body);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn get_valid_function_call(
|
|
|
|
&self,
|
|
|
|
bb: BasicBlock,
|
|
|
|
bb_data: &BasicBlockData<'tcx>,
|
|
|
|
caller_body: &Body<'tcx>,
|
2018-11-06 21:31:09 -06:00
|
|
|
) -> Option<CallSite<'tcx>> {
|
|
|
|
// Don't inline calls that are in cleanup blocks.
|
2019-12-22 16:42:04 -06:00
|
|
|
if bb_data.is_cleanup {
|
|
|
|
return None;
|
|
|
|
}
|
2018-11-06 21:31:09 -06:00
|
|
|
|
|
|
|
// Only consider direct calls to functions
|
|
|
|
let terminator = bb_data.terminator();
|
|
|
|
if let TerminatorKind::Call { func: ref op, .. } = terminator.kind {
|
2020-08-02 17:49:11 -05:00
|
|
|
if let ty::FnDef(callee_def_id, substs) = *op.ty(caller_body, self.tcx).kind() {
|
2020-10-04 09:40:06 -05:00
|
|
|
// To resolve an instance its substs have to be fully normalized, so
|
|
|
|
// we do this here.
|
|
|
|
let normalized_substs = self.tcx.normalize_erasing_regions(self.param_env, substs);
|
|
|
|
let instance =
|
|
|
|
Instance::resolve(self.tcx, self.param_env, callee_def_id, normalized_substs)
|
|
|
|
.ok()
|
|
|
|
.flatten()?;
|
2018-11-06 21:31:09 -06:00
|
|
|
|
|
|
|
if let InstanceDef::Virtual(..) = instance.def {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(CallSite {
|
|
|
|
callee: instance.def_id(),
|
|
|
|
substs: instance.substs,
|
|
|
|
bb,
|
2019-12-22 16:42:04 -06:00
|
|
|
location: terminator.source_info,
|
2018-11-06 21:31:09 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn consider_optimizing(&self, callsite: CallSite<'tcx>, callee_body: &Body<'tcx>) -> bool {
|
2018-10-21 10:58:39 -05:00
|
|
|
debug!("consider_optimizing({:?})", callsite);
|
2019-06-03 17:26:48 -05:00
|
|
|
self.should_inline(callsite, callee_body)
|
2019-12-22 16:42:04 -06:00
|
|
|
&& self.tcx.consider_optimizing(|| {
|
|
|
|
format!("Inline {:?} into {:?}", callee_body.span, callsite)
|
|
|
|
})
|
2018-10-21 10:58:39 -05:00
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn should_inline(&self, callsite: CallSite<'tcx>, callee_body: &Body<'tcx>) -> bool {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("should_inline({:?})", callsite);
|
2017-02-08 03:24:49 -06:00
|
|
|
let tcx = self.tcx;
|
|
|
|
|
2016-12-26 07:34:03 -06:00
|
|
|
// Cannot inline generators which haven't been transformed yet
|
2019-06-03 17:26:48 -05:00
|
|
|
if callee_body.yield_ty.is_some() {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!(" yield ty present - not inlining");
|
2016-12-26 07:34:03 -06:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2018-05-08 08:10:16 -05:00
|
|
|
let codegen_fn_attrs = tcx.codegen_fn_attrs(callsite.callee);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-10-27 17:16:46 -05:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::TRACK_CALLER) {
|
|
|
|
debug!("`#[track_caller]` present - not inlining");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:00:00 -05:00
|
|
|
let self_features = &self.codegen_fn_attrs.target_features;
|
|
|
|
let callee_features = &codegen_fn_attrs.target_features;
|
|
|
|
if callee_features.iter().any(|feature| !self_features.contains(feature)) {
|
|
|
|
debug!("`callee has extra target features - not inlining");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-02 19:00:00 -05:00
|
|
|
let self_no_sanitize =
|
|
|
|
self.codegen_fn_attrs.no_sanitize & self.tcx.sess.opts.debugging_opts.sanitizer;
|
|
|
|
let callee_no_sanitize =
|
|
|
|
codegen_fn_attrs.no_sanitize & self.tcx.sess.opts.debugging_opts.sanitizer;
|
|
|
|
if self_no_sanitize != callee_no_sanitize {
|
|
|
|
debug!("`callee has incompatible no_sanitize attribute - not inlining");
|
2020-06-13 19:00:00 -05:00
|
|
|
return false;
|
2020-01-11 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2018-05-08 08:10:16 -05:00
|
|
|
let hinted = match codegen_fn_attrs.inline {
|
2017-02-08 03:24:49 -06:00
|
|
|
// Just treat inline(always) as a hint for now,
|
|
|
|
// there are cases that prevent inlining that we
|
|
|
|
// need to check for first.
|
|
|
|
attr::InlineAttr::Always => true,
|
2017-11-09 15:03:03 -06:00
|
|
|
attr::InlineAttr::Never => {
|
2019-07-06 02:48:03 -05:00
|
|
|
debug!("`#[inline(never)]` present - not inlining");
|
2019-12-22 16:42:04 -06:00
|
|
|
return false;
|
2017-11-09 15:03:03 -06:00
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
attr::InlineAttr::Hint => true,
|
|
|
|
attr::InlineAttr::None => false,
|
|
|
|
};
|
|
|
|
|
2017-02-08 11:31:47 -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
|
2017-02-08 03:24:49 -06:00
|
|
|
if callsite.callee.is_local() {
|
2019-02-19 19:19:13 -06:00
|
|
|
if callsite.substs.non_erasable_generics().count() == 0 && !hinted {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!(" callee is an exported function - not inlining");
|
2017-02-08 03:24:49 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut threshold = if hinted { HINT_THRESHOLD } else { DEFAULT_THRESHOLD };
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
// Significantly lower the threshold for inlining cold functions
|
2018-05-08 08:10:16 -05:00
|
|
|
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
|
2017-02-08 03:24:49 -06:00
|
|
|
threshold /= 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
let mut first_block = true;
|
|
|
|
let mut cost = 0;
|
|
|
|
|
|
|
|
// Traverse the MIR manually so we can account for the effects of
|
|
|
|
// inlining on the CFG.
|
|
|
|
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;
|
|
|
|
}
|
2019-06-03 17:26:48 -05:00
|
|
|
let blk = &callee_body.basic_blocks()[bb];
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
for stmt in &blk.statements {
|
|
|
|
// Don't count StorageLive/StorageDead in the inlining cost.
|
|
|
|
match stmt.kind {
|
2019-12-22 16:42:04 -06:00
|
|
|
StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_)
|
|
|
|
| StatementKind::Nop => {}
|
|
|
|
_ => cost += INSTR_COST,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let term = blk.terminator();
|
|
|
|
let mut is_drop = false;
|
|
|
|
match term.kind {
|
2020-06-10 02:56:54 -05:00
|
|
|
TerminatorKind::Drop { ref place, target, unwind }
|
|
|
|
| TerminatorKind::DropAndReplace { ref place, target, unwind, .. } => {
|
2017-02-08 03:24:49 -06:00
|
|
|
is_drop = true;
|
|
|
|
work_list.push(target);
|
2020-06-10 02:56:54 -05:00
|
|
|
// If the place doesn't actually need dropping, treat it like
|
2017-02-08 03:24:49 -06:00
|
|
|
// a regular goto.
|
2020-06-10 02:56:54 -05:00
|
|
|
let ty = place.ty(callee_body, tcx).subst(tcx, callsite.substs).ty;
|
2020-10-05 04:04:25 -05:00
|
|
|
if ty.needs_drop(tcx, self.param_env) {
|
2017-02-08 03:24:49 -06:00
|
|
|
cost += CALL_PENALTY;
|
|
|
|
if let Some(unwind) = unwind {
|
2020-03-11 13:18:22 -05:00
|
|
|
cost += LANDINGPAD_PENALTY;
|
2017-02-08 03:24:49 -06:00
|
|
|
work_list.push(unwind);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cost += INSTR_COST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
TerminatorKind::Unreachable | TerminatorKind::Call { destination: None, .. }
|
|
|
|
if first_block =>
|
|
|
|
{
|
2017-02-08 03:24:49 -06:00
|
|
|
// If the function always diverges, don't inline
|
|
|
|
// unless the cost is zero
|
|
|
|
threshold = 0;
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:18:22 -05:00
|
|
|
TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
|
2020-08-02 17:49:11 -05:00
|
|
|
if let ty::FnDef(def_id, _) = *f.literal.ty.kind() {
|
2017-02-08 03:24:49 -06:00
|
|
|
// Don't give intrinsics the extra penalty for calls
|
2017-05-13 09:11:52 -05:00
|
|
|
let f = tcx.fn_sig(def_id);
|
2017-02-08 03:24:49 -06:00
|
|
|
if f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic {
|
|
|
|
cost += INSTR_COST;
|
|
|
|
} else {
|
|
|
|
cost += CALL_PENALTY;
|
|
|
|
}
|
2020-03-11 13:18:22 -05:00
|
|
|
} else {
|
|
|
|
cost += CALL_PENALTY;
|
|
|
|
}
|
|
|
|
if cleanup.is_some() {
|
|
|
|
cost += LANDINGPAD_PENALTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert { cleanup, .. } => {
|
|
|
|
cost += CALL_PENALTY;
|
|
|
|
|
|
|
|
if cleanup.is_some() {
|
|
|
|
cost += LANDINGPAD_PENALTY;
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-11 13:18:22 -05:00
|
|
|
TerminatorKind::Resume => cost += RESUME_PENALTY,
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => cost += INSTR_COST,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !is_drop {
|
2018-04-27 06:02:09 -05:00
|
|
|
for &succ in term.successors() {
|
2017-02-08 03:24:49 -06:00
|
|
|
work_list.push(succ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
first_block = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count up the cost of local variables and temps, if we know the size
|
|
|
|
// use that, otherwise we use a moderately-large dummy cost.
|
|
|
|
|
|
|
|
let ptr_size = tcx.data_layout.pointer_size.bytes();
|
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
for v in callee_body.vars_and_temps_iter() {
|
|
|
|
let v = &callee_body.local_decls[v];
|
2017-02-08 03:24:49 -06:00
|
|
|
let ty = v.ty.subst(tcx, callsite.substs);
|
|
|
|
// Cost of the var is the size in machine-words, if we know
|
|
|
|
// it.
|
2020-10-05 04:04:25 -05:00
|
|
|
if let Some(size) = type_size_of(tcx, self.param_env, ty) {
|
2017-02-08 03:24:49 -06:00
|
|
|
cost += (size / ptr_size) as usize;
|
|
|
|
} else {
|
|
|
|
cost += UNKNOWN_SIZE_COST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-08 08:10:16 -05:00
|
|
|
if let attr::InlineAttr::Always = codegen_fn_attrs.inline {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("INLINING {:?} because inline(always) [cost={}]", callsite, cost);
|
2017-02-08 03:24:49 -06:00
|
|
|
true
|
|
|
|
} else {
|
2017-11-09 15:03:03 -06:00
|
|
|
if cost <= threshold {
|
|
|
|
debug!("INLINING {:?} [cost={} <= threshold={}]", callsite, cost, threshold);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
debug!("NOT inlining {:?} [cost={} > threshold={}]", callsite, cost, threshold);
|
|
|
|
false
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn inline_call(
|
|
|
|
&self,
|
|
|
|
callsite: CallSite<'tcx>,
|
2020-04-12 12:31:00 -05:00
|
|
|
caller_body: &mut Body<'tcx>,
|
|
|
|
mut callee_body: Body<'tcx>,
|
2019-12-22 16:42:04 -06:00
|
|
|
) -> bool {
|
2019-10-03 23:55:28 -05:00
|
|
|
let terminator = caller_body[callsite.bb].terminator.take().unwrap();
|
2017-02-08 03:24:49 -06:00
|
|
|
match terminator.kind {
|
|
|
|
// FIXME: Handle inlining of diverging calls
|
|
|
|
TerminatorKind::Call { args, destination: Some(destination), cleanup, .. } => {
|
2020-10-04 13:01:38 -05:00
|
|
|
debug!("inlined {:?} into {:?}", callsite.callee, caller_body.source);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-11-06 11:00:46 -06:00
|
|
|
let mut local_map = IndexVec::with_capacity(callee_body.local_decls.len());
|
|
|
|
let mut scope_map = IndexVec::with_capacity(callee_body.source_scopes.len());
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-11-06 11:00:46 -06:00
|
|
|
for mut scope in callee_body.source_scopes.iter().cloned() {
|
2017-02-08 03:24:49 -06:00
|
|
|
if scope.parent_scope.is_none() {
|
|
|
|
scope.parent_scope = Some(callsite.location.scope);
|
2019-11-26 11:55:32 -06:00
|
|
|
// FIXME(eddyb) is this really needed?
|
|
|
|
// (also note that it's always overwritten below)
|
2019-11-06 11:00:46 -06:00
|
|
|
scope.span = callee_body.span;
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2019-11-26 11:55:32 -06:00
|
|
|
// FIXME(eddyb) this doesn't seem right at all.
|
|
|
|
// The inlined source scopes should probably be annotated as
|
|
|
|
// such, but also contain all of the original information.
|
2017-02-08 03:24:49 -06:00
|
|
|
scope.span = callsite.location.span;
|
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let idx = caller_body.source_scopes.push(scope);
|
2017-02-08 03:24:49 -06:00
|
|
|
scope_map.push(idx);
|
|
|
|
}
|
|
|
|
|
2019-11-06 11:00:46 -06:00
|
|
|
for loc in callee_body.vars_and_temps_iter() {
|
|
|
|
let mut local = callee_body.local_decls[loc].clone();
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
local.source_info.scope = scope_map[local.source_info.scope];
|
2018-05-29 13:31:33 -05:00
|
|
|
local.source_info.span = callsite.location.span;
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let idx = caller_body.local_decls.push(local);
|
2017-02-08 03:24:49 -06:00
|
|
|
local_map.push(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-03-31 10:19:29 -05:00
|
|
|
let dest = if dest_needs_borrow(destination.0) {
|
2019-07-06 02:48:03 -05:00
|
|
|
debug!("creating temp for return destination");
|
2017-02-08 03:24:49 -06:00
|
|
|
let dest = Rvalue::Ref(
|
2019-04-25 16:05:04 -05:00
|
|
|
self.tcx.lifetimes.re_erased,
|
2018-01-15 05:47:26 -06:00
|
|
|
BorrowKind::Mut { allow_two_phase_borrow: false },
|
2019-12-22 16:42:04 -06:00
|
|
|
destination.0,
|
|
|
|
);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-04-12 12:28:41 -05:00
|
|
|
let ty = dest.ty(caller_body, self.tcx);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-05-05 19:17:38 -05:00
|
|
|
let temp = LocalDecl::new(ty, callsite.location.span);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let tmp = caller_body.local_decls.push(temp);
|
2019-06-24 10:46:09 -05:00
|
|
|
let tmp = Place::from(tmp);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let stmt = Statement {
|
|
|
|
source_info: callsite.location,
|
2020-01-22 09:30:15 -06:00
|
|
|
kind: StatementKind::Assign(box (tmp, dest)),
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
2019-12-22 16:42:04 -06:00
|
|
|
caller_body[callsite.bb].statements.push(stmt);
|
2019-10-20 20:04:59 -05:00
|
|
|
self.tcx.mk_place_deref(tmp)
|
2017-02-08 03:24:49 -06:00
|
|
|
} else {
|
|
|
|
destination.0
|
|
|
|
};
|
|
|
|
|
|
|
|
let return_block = destination.1;
|
|
|
|
|
2018-04-18 17:47:07 -05:00
|
|
|
// Copy the arguments if needed.
|
2020-09-02 19:00:00 -05:00
|
|
|
let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, return_block);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let bb_len = caller_body.basic_blocks().len();
|
2017-02-08 03:24:49 -06:00
|
|
|
let mut integrator = Integrator {
|
|
|
|
block_idx: bb_len,
|
|
|
|
args: &args,
|
2017-08-07 00:54:09 -05:00
|
|
|
local_map,
|
|
|
|
scope_map,
|
2017-02-08 03:24:49 -06:00
|
|
|
destination: dest,
|
2017-08-07 00:54:09 -05:00
|
|
|
return_block,
|
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,
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
|
2018-05-16 10:58:54 -05:00
|
|
|
for mut var_debug_info in callee_body.var_debug_info.drain(..) {
|
|
|
|
integrator.visit_var_debug_info(&mut var_debug_info);
|
|
|
|
caller_body.var_debug_info.push(var_debug_info);
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-11-06 11:00:46 -06:00
|
|
|
for (bb, mut block) in callee_body.basic_blocks_mut().drain_enumerated(..) {
|
2017-02-08 03:24:49 -06:00
|
|
|
integrator.visit_basic_block_data(bb, &mut block);
|
2019-06-03 17:26:48 -05:00
|
|
|
caller_body.basic_blocks_mut().push(block);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let terminator = Terminator {
|
|
|
|
source_info: callsite.location,
|
2019-12-22 16:42:04 -06:00
|
|
|
kind: TerminatorKind::Goto { target: BasicBlock::new(bb_len) },
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
|
2019-10-04 08:44:24 -05:00
|
|
|
caller_body[callsite.bb].terminator = Some(terminator);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
kind => {
|
2019-12-22 16:42:04 -06:00
|
|
|
caller_body[callsite.bb].terminator =
|
|
|
|
Some(Terminator { source_info: terminator.source_info, kind });
|
2017-02-08 03:24:49 -06:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-02 19:00:00 -05:00
|
|
|
return_block: BasicBlock,
|
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]`.
|
2017-11-10 11:20:53 -06:00
|
|
|
if tcx.is_closure(callsite.callee) {
|
2017-11-10 08:06:06 -06:00
|
|
|
let mut args = args.into_iter();
|
2020-09-02 19:00:00 -05:00
|
|
|
let self_ = self.create_temp_if_necessary(
|
|
|
|
args.next().unwrap(),
|
|
|
|
callsite,
|
|
|
|
caller_body,
|
|
|
|
return_block,
|
|
|
|
);
|
|
|
|
let tuple = self.create_temp_if_necessary(
|
|
|
|
args.next().unwrap(),
|
|
|
|
callsite,
|
|
|
|
caller_body,
|
|
|
|
return_block,
|
|
|
|
);
|
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);
|
2020-08-02 17:49:11 -05:00
|
|
|
let tuple_tys = if let ty::Tuple(s) = tuple.ty(caller_body, tcx).ty.kind() {
|
2017-11-10 08:06:06 -06:00
|
|
|
s
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
|
|
|
// The `tmp0`, `tmp1`, and `tmp2` in our example abonve.
|
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.
|
|
|
|
let tuple_field =
|
2020-04-15 17:00:22 -05:00
|
|
|
Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty.expect_ty()));
|
2019-12-22 16:42:04 -06:00
|
|
|
|
|
|
|
// Spill to a local to make e.g., `tmp0`.
|
2020-09-02 19:00:00 -05:00
|
|
|
self.create_temp_if_necessary(tuple_field, callsite, caller_body, return_block)
|
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-09-02 19:00:00 -05:00
|
|
|
.map(|a| self.create_temp_if_necessary(a, callsite, caller_body, return_block))
|
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>,
|
2020-09-02 19:00:00 -05:00
|
|
|
return_block: BasicBlock,
|
2017-11-27 13:01:30 -06:00
|
|
|
) -> Local {
|
2017-02-08 03:24:49 -06:00
|
|
|
// FIXME: Analysis of the usage of the arguments to avoid
|
|
|
|
// unnecessary temporaries.
|
2017-11-10 11:05:29 -06:00
|
|
|
|
2019-10-20 15:09:36 -05:00
|
|
|
if let Operand::Move(place) = &arg {
|
|
|
|
if let Some(local) = place.as_local() {
|
2019-11-06 11:00:46 -06:00
|
|
|
if caller_body.local_kind(local) == LocalKind::Temp {
|
2019-10-20 15:09:36 -05:00
|
|
|
// Reuse the operand if it's a temporary already
|
|
|
|
return local;
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2017-11-10 11:05:29 -06:00
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2019-07-06 02:48:03 -05:00
|
|
|
debug!("creating temp for argument {:?}", arg);
|
2017-11-10 11:05:29 -06:00
|
|
|
// Otherwise, create a temporary for the arg
|
|
|
|
let arg = Rvalue::Use(arg);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-04-12 12:28:41 -05:00
|
|
|
let ty = arg.ty(caller_body, self.tcx);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-05-05 19:17:38 -05:00
|
|
|
let arg_tmp = LocalDecl::new(ty, callsite.location.span);
|
2019-11-06 11:00:46 -06:00
|
|
|
let arg_tmp = caller_body.local_decls.push(arg_tmp);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2020-09-02 19:00:00 -05:00
|
|
|
caller_body[callsite.bb].statements.push(Statement {
|
|
|
|
source_info: callsite.location,
|
|
|
|
kind: StatementKind::StorageLive(arg_tmp),
|
|
|
|
});
|
|
|
|
caller_body[callsite.bb].statements.push(Statement {
|
2017-11-10 11:05:29 -06:00
|
|
|
source_info: callsite.location,
|
2019-12-22 16:42:04 -06:00
|
|
|
kind: StatementKind::Assign(box (Place::from(arg_tmp), arg)),
|
2020-09-02 19:00:00 -05:00
|
|
|
});
|
|
|
|
caller_body[return_block].statements.insert(
|
|
|
|
0,
|
|
|
|
Statement { source_info: callsite.location, kind: StatementKind::StorageDead(arg_tmp) },
|
|
|
|
);
|
|
|
|
|
2017-11-10 11:05:29 -06:00
|
|
|
arg_tmp
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-02-08 03:24:49 -06:00
|
|
|
block_idx: usize,
|
2017-11-27 13:01:30 -06:00
|
|
|
args: &'a [Local],
|
2017-02-08 03:24:49 -06:00
|
|
|
local_map: IndexVec<Local, Local>,
|
2018-05-28 06:16:09 -05:00
|
|
|
scope_map: IndexVec<SourceScope, SourceScope>,
|
2017-12-01 06:31:47 -06:00
|
|
|
destination: Place<'tcx>,
|
2017-02-08 03:24:49 -06:00
|
|
|
return_block: BasicBlock,
|
|
|
|
cleanup_block: Option<BasicBlock>,
|
|
|
|
in_cleanup_block: bool,
|
2019-10-20 15:11:04 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Integrator<'a, 'tcx> {
|
|
|
|
fn update_target(&self, tgt: BasicBlock) -> BasicBlock {
|
|
|
|
let new = BasicBlock::new(tgt.index() + self.block_idx);
|
2019-07-06 02:48:03 -05:00
|
|
|
debug!("updating target `{:?}`, new: `{:?}`", tgt, new);
|
2017-02-08 03:24:49 -06:00
|
|
|
new
|
|
|
|
}
|
|
|
|
|
2020-01-13 22:26:11 -06:00
|
|
|
fn make_integrate_local(&self, local: Local) -> Local {
|
|
|
|
if local == RETURN_PLACE {
|
2019-12-11 13:50:03 -06:00
|
|
|
return self.destination.local;
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
2019-10-07 16:23:39 -05:00
|
|
|
|
2017-09-03 11:14:31 -05:00
|
|
|
let idx = local.index() - 1;
|
|
|
|
if idx < self.args.len() {
|
2019-10-07 16:23:39 -05:00
|
|
|
return self.args[idx];
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
2019-10-07 16:23:39 -05:00
|
|
|
|
|
|
|
self.local_map[Local::new(idx - self.args.len())]
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
2019-10-07 16:23:39 -05:00
|
|
|
}
|
2017-09-03 11:14:31 -05:00
|
|
|
|
2019-10-07 16:23:39 -05:00
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, '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-01-13 22:26:11 -06:00
|
|
|
*local = self.make_integrate_local(*local);
|
2019-10-07 16:23:39 -05:00
|
|
|
}
|
2018-07-21 18:01:07 -05:00
|
|
|
|
2020-01-01 19:10:55 -06:00
|
|
|
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
|
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-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 } => {
|
2017-02-08 03:24:49 -06:00
|
|
|
*target = self.update_target(*target);
|
|
|
|
}
|
|
|
|
TerminatorKind::SwitchInt { ref mut targets, .. } => {
|
2020-10-10 10:36:04 -05:00
|
|
|
for tgt in targets.all_targets_mut() {
|
2017-02-08 03:24:49 -06:00
|
|
|
*tgt = self.update_target(*tgt);
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
TerminatorKind::Drop { ref mut target, ref mut unwind, .. }
|
|
|
|
| TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
|
2017-02-08 03:24:49 -06:00
|
|
|
*target = self.update_target(*target);
|
|
|
|
if let Some(tgt) = *unwind {
|
|
|
|
*unwind = Some(self.update_target(tgt));
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Call { ref mut destination, ref mut cleanup, .. } => {
|
|
|
|
if let Some((_, ref mut tgt)) = *destination {
|
|
|
|
*tgt = self.update_target(*tgt);
|
|
|
|
}
|
|
|
|
if let Some(tgt) = *cleanup {
|
|
|
|
*cleanup = Some(self.update_target(tgt));
|
|
|
|
} 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, .. } => {
|
|
|
|
*target = self.update_target(*target);
|
|
|
|
if let Some(tgt) = *cleanup {
|
|
|
|
*cleanup = Some(self.update_target(tgt));
|
|
|
|
} 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 => {
|
2020-05-31 05:13:29 -05:00
|
|
|
terminator.kind = TerminatorKind::Goto { target: self.return_block };
|
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 } => {
|
2017-10-13 08:36:15 -05:00
|
|
|
*real_target = self.update_target(*real_target);
|
2019-05-06 09:18:57 -05:00
|
|
|
*imaginary_target = self.update_target(*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
|
|
|
}
|
2020-02-14 12:17:50 -06:00
|
|
|
TerminatorKind::InlineAsm { ref mut destination, .. } => {
|
|
|
|
if let Some(ref mut tgt) = *destination {
|
|
|
|
*tgt = self.update_target(*tgt);
|
|
|
|
}
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 06:16:09 -05:00
|
|
|
fn visit_source_scope(&mut self, scope: &mut SourceScope) {
|
2017-02-08 03:24:49 -06:00
|
|
|
*scope = self.scope_map[*scope];
|
|
|
|
}
|
|
|
|
}
|