2017-02-08 03:24:49 -06:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Inlining pass for MIR functions
|
|
|
|
|
2017-11-10 11:20:35 -06:00
|
|
|
use rustc::hir;
|
2018-05-08 08:10:16 -05:00
|
|
|
use rustc::hir::CodegenFnAttrFlags;
|
2017-04-25 17:23:33 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-02-08 03:24:49 -06:00
|
|
|
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 00:07:25 -05:00
|
|
|
use rustc_data_structures::bit_set::BitSet;
|
2017-02-08 03:24:49 -06:00
|
|
|
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
|
|
|
|
|
|
|
|
use rustc::mir::*;
|
|
|
|
use rustc::mir::visit::*;
|
2018-11-06 21:31:09 -06:00
|
|
|
use rustc::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
|
2017-02-08 03:24:49 -06:00
|
|
|
use rustc::ty::subst::{Subst,Substs};
|
|
|
|
|
2017-05-01 15:03:05 -05:00
|
|
|
use std::collections::VecDeque;
|
2017-11-18 17:02:41 -06:00
|
|
|
use std::iter;
|
2017-11-10 11:20:35 -06:00
|
|
|
use transform::{MirPass, MirSource};
|
2017-02-08 03:24:49 -06:00
|
|
|
use super::simplify::{remove_dead_blocks, CfgSimplifier};
|
|
|
|
|
2017-02-08 11:31:47 -06:00
|
|
|
use syntax::{attr};
|
2018-04-25 11:30:39 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
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;
|
|
|
|
|
|
|
|
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,
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
bb: BasicBlock,
|
|
|
|
location: SourceInfo,
|
2017-05-01 11:47:00 -05:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:03:05 -05:00
|
|
|
impl MirPass for Inline {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
source: MirSource,
|
|
|
|
mir: &mut Mir<'tcx>) {
|
|
|
|
if tcx.sess.opts.debugging_opts.mir_opt_level >= 2 {
|
|
|
|
Inliner { tcx, source }.run_pass(mir);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 12:46:35 -05:00
|
|
|
struct Inliner<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2017-05-01 15:03:05 -05:00
|
|
|
source: MirSource,
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
|
2017-05-01 12:46:35 -05:00
|
|
|
impl<'a, 'tcx> Inliner<'a, 'tcx> {
|
2017-05-01 15:03:05 -05:00
|
|
|
fn run_pass(&self, caller_mir: &mut Mir<'tcx>) {
|
|
|
|
// 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();
|
|
|
|
|
2017-11-10 11:20:35 -06:00
|
|
|
let param_env = self.tcx.param_env(self.source.def_id);
|
2017-11-10 05:31:51 -06:00
|
|
|
|
2017-05-01 15:03:05 -05:00
|
|
|
// Only do inlining into fn bodies.
|
2017-11-10 11:20:35 -06:00
|
|
|
let id = self.tcx.hir.as_local_node_id(self.source.def_id).unwrap();
|
|
|
|
let body_owner_kind = self.tcx.hir.body_owner_kind(id);
|
2018-11-06 21:31:09 -06:00
|
|
|
|
2017-11-10 11:20:35 -06:00
|
|
|
if let (hir::BodyOwnerKind::Fn, None) = (body_owner_kind, self.source.promoted) {
|
2017-11-10 05:31:51 -06:00
|
|
|
|
2017-05-01 15:03:05 -05:00
|
|
|
for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated() {
|
2018-11-06 21:31:09 -06:00
|
|
|
if let Some(callsite) = self.get_valid_function_call(bb,
|
|
|
|
bb_data,
|
|
|
|
caller_mir,
|
|
|
|
param_env) {
|
|
|
|
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
|
|
|
|
2018-06-13 08:44:43 -05:00
|
|
|
let callee_mir = match self.tcx.try_optimized_mir(callsite.location.span,
|
|
|
|
callsite.callee) {
|
2018-10-21 10:58:39 -05:00
|
|
|
Ok(callee_mir) if self.consider_optimizing(callsite, callee_mir) => {
|
2018-03-09 11:53:17 -06:00
|
|
|
self.tcx.subst_and_normalize_erasing_regions(
|
|
|
|
&callsite.substs,
|
|
|
|
param_env,
|
|
|
|
callee_mir,
|
|
|
|
)
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
2017-08-24 09:00:44 -05:00
|
|
|
Ok(_) => continue,
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2017-08-24 09:00:44 -05:00
|
|
|
Err(mut bug) => {
|
|
|
|
// FIXME(#43542) shouldn't have to cancel an error
|
|
|
|
bug.cancel();
|
|
|
|
continue
|
|
|
|
}
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let start = caller_mir.basic_blocks().len();
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!("attempting to inline callsite {:?} - mir={:?}", callsite, callee_mir);
|
2017-04-28 18:29:16 -05:00
|
|
|
if !self.inline_call(callsite, caller_mir, callee_mir) {
|
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
|
|
|
|
for (bb, bb_data) in caller_mir.basic_blocks().iter_enumerated().skip(start) {
|
2018-11-06 21:31:09 -06:00
|
|
|
if let Some(new_callsite) = self.get_valid_function_call(bb,
|
|
|
|
bb_data,
|
|
|
|
caller_mir,
|
|
|
|
param_env) {
|
|
|
|
// 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 {
|
|
|
|
debug!("Running simplify cfg on {:?}", self.source);
|
2017-04-28 18:29:16 -05:00
|
|
|
CfgSimplifier::new(caller_mir).simplify();
|
|
|
|
remove_dead_blocks(caller_mir);
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 21:31:09 -06:00
|
|
|
fn get_valid_function_call(&self,
|
|
|
|
bb: BasicBlock,
|
|
|
|
bb_data: &BasicBlockData<'tcx>,
|
|
|
|
caller_mir: &Mir<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
|
|
|
) -> Option<CallSite<'tcx>> {
|
|
|
|
// Don't inline calls that are in cleanup blocks.
|
|
|
|
if bb_data.is_cleanup { return None; }
|
|
|
|
|
|
|
|
// Only consider direct calls to functions
|
|
|
|
let terminator = bb_data.terminator();
|
|
|
|
if let TerminatorKind::Call { func: ref op, .. } = terminator.kind {
|
|
|
|
if let ty::FnDef(callee_def_id, substs) = op.ty(caller_mir, self.tcx).sty {
|
|
|
|
let instance = Instance::resolve(self.tcx,
|
|
|
|
param_env,
|
|
|
|
callee_def_id,
|
|
|
|
substs)?;
|
|
|
|
|
|
|
|
if let InstanceDef::Virtual(..) = instance.def {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(CallSite {
|
|
|
|
callee: instance.def_id(),
|
|
|
|
substs: instance.substs,
|
|
|
|
bb,
|
|
|
|
location: terminator.source_info
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-10-21 10:58:39 -05:00
|
|
|
fn consider_optimizing(&self,
|
|
|
|
callsite: CallSite<'tcx>,
|
|
|
|
callee_mir: &Mir<'tcx>)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
debug!("consider_optimizing({:?})", callsite);
|
|
|
|
self.should_inline(callsite, callee_mir)
|
|
|
|
&& self.tcx.consider_optimizing(|| format!("Inline {:?} into {:?}",
|
|
|
|
callee_mir.span,
|
|
|
|
callsite))
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:29:16 -05:00
|
|
|
fn should_inline(&self,
|
|
|
|
callsite: CallSite<'tcx>,
|
|
|
|
callee_mir: &Mir<'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;
|
|
|
|
|
|
|
|
// Don't inline closures that have captures
|
|
|
|
// FIXME: Handle closures better
|
|
|
|
if callee_mir.upvar_decls.len() > 0 {
|
2017-11-09 15:03:03 -06:00
|
|
|
debug!(" upvar decls present - not inlining");
|
2017-02-08 03:24:49 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-26 07:34:03 -06:00
|
|
|
// Cannot inline generators which haven't been transformed yet
|
2017-07-10 14:11:31 -05:00
|
|
|
if callee_mir.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
|
|
|
// Do not inline {u,i}128 lang items, codegen const eval depends
|
2018-01-16 02:24:38 -06:00
|
|
|
// on detecting calls to these lang items and intercepting them
|
|
|
|
if tcx.is_binop_lang_item(callsite.callee).is_some() {
|
|
|
|
debug!(" not inlining 128bit integer lang item");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 => {
|
|
|
|
debug!("#[inline(never)] present - not inlining");
|
|
|
|
return false
|
|
|
|
}
|
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() {
|
|
|
|
if callsite.substs.types().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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut threshold = if hinted {
|
|
|
|
HINT_THRESHOLD
|
|
|
|
} else {
|
|
|
|
DEFAULT_THRESHOLD
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if callee_mir.basic_blocks().len() <= 3 {
|
|
|
|
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
|
|
|
|
|
2017-11-10 11:20:35 -06:00
|
|
|
let param_env = tcx.param_env(self.source.def_id);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
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];
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 00:07:25 -05:00
|
|
|
let mut visited = BitSet::new_empty(callee_mir.basic_blocks().len());
|
2017-02-08 03:24:49 -06:00
|
|
|
while let Some(bb) = work_list.pop() {
|
|
|
|
if !visited.insert(bb.index()) { continue; }
|
|
|
|
let blk = &callee_mir.basic_blocks()[bb];
|
|
|
|
|
|
|
|
for stmt in &blk.statements {
|
|
|
|
// Don't count StorageLive/StorageDead in the inlining cost.
|
|
|
|
match stmt.kind {
|
|
|
|
StatementKind::StorageLive(_) |
|
|
|
|
StatementKind::StorageDead(_) |
|
|
|
|
StatementKind::Nop => {}
|
|
|
|
_ => cost += INSTR_COST
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let term = blk.terminator();
|
|
|
|
let mut is_drop = false;
|
|
|
|
match term.kind {
|
|
|
|
TerminatorKind::Drop { ref location, target, unwind } |
|
|
|
|
TerminatorKind::DropAndReplace { ref location, target, unwind, .. } => {
|
|
|
|
is_drop = true;
|
|
|
|
work_list.push(target);
|
|
|
|
// If the location doesn't actually need dropping, treat it like
|
|
|
|
// a regular goto.
|
2017-07-12 14:59:05 -05:00
|
|
|
let ty = location.ty(callee_mir, tcx).subst(tcx, callsite.substs);
|
2017-02-08 03:24:49 -06:00
|
|
|
let ty = ty.to_ty(tcx);
|
2017-05-10 09:28:06 -05:00
|
|
|
if ty.needs_drop(tcx, param_env) {
|
2017-02-08 03:24:49 -06:00
|
|
|
cost += CALL_PENALTY;
|
|
|
|
if let Some(unwind) = unwind {
|
|
|
|
work_list.push(unwind);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cost += INSTR_COST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminatorKind::Unreachable |
|
|
|
|
TerminatorKind::Call { destination: None, .. } if first_block => {
|
|
|
|
// If the function always diverges, don't inline
|
|
|
|
// unless the cost is zero
|
|
|
|
threshold = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminatorKind::Call {func: Operand::Constant(ref f), .. } => {
|
2018-08-21 19:35:02 -05:00
|
|
|
if let ty::FnDef(def_id, _) = f.ty.sty {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert { .. } => cost += CALL_PENALTY,
|
|
|
|
_ => cost += INSTR_COST
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
for v in callee_mir.vars_and_temps_iter() {
|
|
|
|
let v = &callee_mir.local_decls[v];
|
|
|
|
let ty = v.ty.subst(tcx, callsite.substs);
|
|
|
|
// Cost of the var is the size in machine-words, if we know
|
|
|
|
// it.
|
|
|
|
if let Some(size) = type_size_of(tcx, param_env.clone(), ty) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:29:16 -05:00
|
|
|
fn inline_call(&self,
|
|
|
|
callsite: CallSite<'tcx>,
|
|
|
|
caller_mir: &mut Mir<'tcx>,
|
|
|
|
mut callee_mir: Mir<'tcx>) -> bool {
|
2017-02-08 03:24:49 -06:00
|
|
|
let terminator = caller_mir[callsite.bb].terminator.take().unwrap();
|
|
|
|
match terminator.kind {
|
|
|
|
// FIXME: Handle inlining of diverging calls
|
|
|
|
TerminatorKind::Call { args, destination: Some(destination), cleanup, .. } => {
|
2017-05-01 15:03:05 -05:00
|
|
|
debug!("Inlined {:?} into {:?}", callsite.callee, self.source);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let mut local_map = IndexVec::with_capacity(callee_mir.local_decls.len());
|
2018-05-28 06:16:09 -05:00
|
|
|
let mut scope_map = IndexVec::with_capacity(callee_mir.source_scopes.len());
|
2017-02-08 03:24:49 -06:00
|
|
|
let mut promoted_map = IndexVec::with_capacity(callee_mir.promoted.len());
|
|
|
|
|
2018-05-28 06:16:09 -05:00
|
|
|
for mut scope in callee_mir.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);
|
|
|
|
scope.span = callee_mir.span;
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.span = callsite.location.span;
|
|
|
|
|
2018-05-28 06:16:09 -05:00
|
|
|
let idx = caller_mir.source_scopes.push(scope);
|
2017-02-08 03:24:49 -06:00
|
|
|
scope_map.push(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
for loc in callee_mir.vars_and_temps_iter() {
|
|
|
|
let mut local = callee_mir.local_decls[loc].clone();
|
|
|
|
|
2018-05-29 13:31:33 -05:00
|
|
|
local.source_info.scope =
|
|
|
|
scope_map[local.source_info.scope];
|
|
|
|
local.source_info.span = callsite.location.span;
|
2018-05-29 05:55:21 -05:00
|
|
|
local.visibility_scope = scope_map[local.visibility_scope];
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let idx = caller_mir.local_decls.push(local);
|
|
|
|
local_map.push(idx);
|
|
|
|
}
|
|
|
|
|
2018-07-26 10:11:10 -05:00
|
|
|
promoted_map.extend(
|
|
|
|
callee_mir.promoted.iter().cloned().map(|p| caller_mir.promoted.push(p))
|
|
|
|
);
|
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.
|
|
|
|
fn dest_needs_borrow(place: &Place) -> bool {
|
|
|
|
match *place {
|
2017-12-01 06:31:47 -06:00
|
|
|
Place::Projection(ref p) => {
|
2017-02-08 03:24:49 -06:00
|
|
|
match p.elem {
|
|
|
|
ProjectionElem::Deref |
|
|
|
|
ProjectionElem::Index(_) => true,
|
|
|
|
_ => dest_needs_borrow(&p.base)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Static variables need a borrow because the callee
|
|
|
|
// might modify the same static.
|
2017-12-01 06:31:47 -06:00
|
|
|
Place::Static(_) => true,
|
2017-02-08 03:24:49 -06:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dest = if dest_needs_borrow(&destination.0) {
|
|
|
|
debug!("Creating temp for return destination");
|
|
|
|
let dest = Rvalue::Ref(
|
2017-04-19 17:58:12 -05:00
|
|
|
self.tcx.types.re_erased,
|
2018-01-15 05:47:26 -06:00
|
|
|
BorrowKind::Mut { allow_two_phase_borrow: false },
|
2017-02-08 03:24:49 -06:00
|
|
|
destination.0);
|
|
|
|
|
2017-07-12 14:59:05 -05:00
|
|
|
let ty = dest.ty(caller_mir, self.tcx);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2017-04-11 15:52:51 -05:00
|
|
|
let temp = LocalDecl::new_temp(ty, callsite.location.span);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let tmp = caller_mir.local_decls.push(temp);
|
2017-12-01 06:31:47 -06:00
|
|
|
let tmp = Place::Local(tmp);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let stmt = Statement {
|
|
|
|
source_info: callsite.location,
|
2018-09-23 20:32:31 -05:00
|
|
|
kind: StatementKind::Assign(tmp.clone(), box dest)
|
2017-02-08 03:24:49 -06:00
|
|
|
};
|
|
|
|
caller_mir[callsite.bb]
|
|
|
|
.statements.push(stmt);
|
|
|
|
tmp.deref()
|
|
|
|
} else {
|
|
|
|
destination.0
|
|
|
|
};
|
|
|
|
|
|
|
|
let return_block = destination.1;
|
|
|
|
|
2018-04-18 17:47:07 -05:00
|
|
|
// Copy the arguments if needed.
|
|
|
|
let args: Vec<_> = self.make_call_args(args, &callsite, caller_mir);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
|
|
|
let bb_len = caller_mir.basic_blocks().len();
|
|
|
|
let mut integrator = Integrator {
|
|
|
|
block_idx: bb_len,
|
|
|
|
args: &args,
|
2017-08-07 00:54:09 -05:00
|
|
|
local_map,
|
|
|
|
scope_map,
|
|
|
|
promoted_map,
|
2017-02-08 03:24:49 -06:00
|
|
|
_callsite: callsite,
|
|
|
|
destination: dest,
|
2017-08-07 00:54:09 -05:00
|
|
|
return_block,
|
2017-02-08 03:24:49 -06:00
|
|
|
cleanup_block: cleanup,
|
|
|
|
in_cleanup_block: false
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
for (bb, mut block) in callee_mir.basic_blocks_mut().drain_enumerated(..) {
|
|
|
|
integrator.visit_basic_block_data(bb, &mut block);
|
|
|
|
caller_mir.basic_blocks_mut().push(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
let terminator = Terminator {
|
|
|
|
source_info: callsite.location,
|
|
|
|
kind: TerminatorKind::Goto { target: BasicBlock::new(bb_len) }
|
|
|
|
};
|
|
|
|
|
|
|
|
caller_mir[callsite.bb].terminator = Some(terminator);
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
kind => {
|
|
|
|
caller_mir[callsite.bb].terminator = Some(Terminator {
|
|
|
|
source_info: terminator.source_info,
|
2017-08-07 00:54:09 -05:00
|
|
|
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>,
|
|
|
|
caller_mir: &mut Mir<'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]`.
|
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();
|
2017-11-10 11:05:29 -06:00
|
|
|
let self_ = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_mir);
|
|
|
|
let tuple = self.create_temp_if_necessary(args.next().unwrap(), callsite, caller_mir);
|
2017-11-10 08:06:06 -06:00
|
|
|
assert!(args.next().is_none());
|
|
|
|
|
2017-12-01 06:31:47 -06:00
|
|
|
let tuple = Place::Local(tuple);
|
2018-08-21 19:35:02 -05:00
|
|
|
let tuple_tys = if let ty::Tuple(s) = tuple.ty(caller_mir, tcx).to_ty(tcx).sty {
|
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.
|
|
|
|
let tuple_tmp_args =
|
|
|
|
tuple_tys.iter().enumerate().map(|(i, ty)| {
|
|
|
|
// This is e.g. `tuple_tmp.0` in our example above.
|
2017-11-17 09:19:57 -06:00
|
|
|
let tuple_field = Operand::Move(tuple.clone().field(Field::new(i), ty));
|
2017-11-18 17:02:41 -06:00
|
|
|
|
|
|
|
// Spill to a local to make e.g. `tmp0`.
|
2017-11-27 13:01:30 -06:00
|
|
|
self.create_temp_if_necessary(tuple_field, callsite, caller_mir)
|
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()
|
2017-11-27 13:01:30 -06:00
|
|
|
.map(|a| self.create_temp_if_necessary(a, callsite, caller_mir))
|
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>,
|
|
|
|
caller_mir: &mut Mir<'tcx>,
|
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
|
|
|
|
2017-12-01 06:31:47 -06:00
|
|
|
if let Operand::Move(Place::Local(local)) = arg {
|
2017-11-10 11:05:29 -06:00
|
|
|
if caller_mir.local_kind(local) == LocalKind::Temp {
|
|
|
|
// Reuse the operand if it's a temporary already
|
2017-11-27 13:01:30 -06:00
|
|
|
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
|
|
|
|
2017-11-10 11:05:29 -06:00
|
|
|
debug!("Creating temp for argument {:?}", arg);
|
|
|
|
// Otherwise, create a temporary for the arg
|
|
|
|
let arg = Rvalue::Use(arg);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2017-11-10 11:05:29 -06:00
|
|
|
let ty = arg.ty(caller_mir, self.tcx);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2017-11-10 11:05:29 -06:00
|
|
|
let arg_tmp = LocalDecl::new_temp(ty, callsite.location.span);
|
|
|
|
let arg_tmp = caller_mir.local_decls.push(arg_tmp);
|
2017-02-08 03:24:49 -06:00
|
|
|
|
2017-11-10 11:05:29 -06:00
|
|
|
let stmt = Statement {
|
|
|
|
source_info: callsite.location,
|
2018-09-23 20:32:31 -05:00
|
|
|
kind: StatementKind::Assign(Place::Local(arg_tmp), box arg),
|
2017-11-10 11:05:29 -06:00
|
|
|
};
|
|
|
|
caller_mir[callsite.bb].statements.push(stmt);
|
|
|
|
arg_tmp
|
2017-02-08 03:24:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 07:01:04 -05:00
|
|
|
fn type_size_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-02-08 03:24:49 -06:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
struct Integrator<'a, 'tcx: 'a> {
|
|
|
|
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-02-08 03:24:49 -06:00
|
|
|
promoted_map: IndexVec<Promoted, Promoted>,
|
|
|
|
_callsite: CallSite<'tcx>,
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Integrator<'a, 'tcx> {
|
|
|
|
fn update_target(&self, tgt: BasicBlock) -> BasicBlock {
|
|
|
|
let new = BasicBlock::new(tgt.index() + self.block_idx);
|
|
|
|
debug!("Updating target `{:?}`, new: `{:?}`", tgt, new);
|
|
|
|
new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
|
2017-09-03 11:14:31 -05:00
|
|
|
fn visit_local(&mut self,
|
|
|
|
local: &mut Local,
|
2017-12-01 06:31:47 -06:00
|
|
|
_ctxt: PlaceContext<'tcx>,
|
2017-09-03 11:14:31 -05:00
|
|
|
_location: Location) {
|
2017-12-01 06:39:51 -06:00
|
|
|
if *local == RETURN_PLACE {
|
2017-09-03 11:14:31 -05:00
|
|
|
match self.destination {
|
2017-12-01 06:31:47 -06:00
|
|
|
Place::Local(l) => {
|
2017-09-06 05:25:46 -05:00
|
|
|
*local = l;
|
|
|
|
return;
|
|
|
|
},
|
2017-12-01 06:39:51 -06:00
|
|
|
ref place => bug!("Return place is {:?}, not local", place)
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let idx = local.index() - 1;
|
|
|
|
if idx < self.args.len() {
|
2017-11-27 13:01:30 -06:00
|
|
|
*local = self.args[idx];
|
|
|
|
return;
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
2017-09-06 05:25:46 -05:00
|
|
|
*local = self.local_map[Local::new(idx - self.args.len())];
|
2017-09-03 11:14:31 -05:00
|
|
|
}
|
|
|
|
|
2017-12-01 06:39:51 -06:00
|
|
|
fn visit_place(&mut self,
|
|
|
|
place: &mut Place<'tcx>,
|
2017-12-01 06:31:47 -06:00
|
|
|
_ctxt: PlaceContext<'tcx>,
|
2017-02-08 03:24:49 -06:00
|
|
|
_location: Location) {
|
2018-07-21 18:01:07 -05:00
|
|
|
|
|
|
|
match place {
|
|
|
|
Place::Local(RETURN_PLACE) => {
|
|
|
|
// Return pointer; update the place itself
|
|
|
|
*place = self.destination.clone();
|
|
|
|
},
|
|
|
|
Place::Promoted(ref mut promoted) => {
|
|
|
|
if let Some(p) = self.promoted_map.get(promoted.0).cloned() {
|
|
|
|
promoted.0 = p;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => self.super_place(place, _ctxt, _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;
|
|
|
|
}
|
|
|
|
|
2018-10-24 06:47:48 -05:00
|
|
|
fn visit_retag(&mut self, fn_entry: &mut bool, place: &mut Place<'tcx>, loc: Location) {
|
|
|
|
self.super_retag(fn_entry, place, loc);
|
|
|
|
|
|
|
|
// We have to patch all inlined retags to be aware that they are no longer
|
|
|
|
// happening on function entry.
|
|
|
|
*fn_entry = false;
|
|
|
|
}
|
|
|
|
|
2017-02-08 03:24:49 -06:00
|
|
|
fn visit_terminator_kind(&mut self, block: BasicBlock,
|
|
|
|
kind: &mut TerminatorKind<'tcx>, loc: Location) {
|
|
|
|
self.super_terminator_kind(block, kind, loc);
|
|
|
|
|
|
|
|
match *kind {
|
2016-12-26 07:34:03 -06:00
|
|
|
TerminatorKind::GeneratorDrop |
|
2017-07-10 14:11:31 -05:00
|
|
|
TerminatorKind::Yield { .. } => bug!(),
|
2017-02-08 03:24:49 -06:00
|
|
|
TerminatorKind::Goto { ref mut target} => {
|
|
|
|
*target = self.update_target(*target);
|
|
|
|
}
|
|
|
|
TerminatorKind::SwitchInt { ref mut targets, .. } => {
|
|
|
|
for tgt in targets {
|
|
|
|
*tgt = self.update_target(*tgt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Drop { ref mut target, ref mut unwind, .. } |
|
|
|
|
TerminatorKind::DropAndReplace { ref mut target, ref mut unwind, .. } => {
|
|
|
|
*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 => {
|
|
|
|
*kind = TerminatorKind::Goto { target: self.return_block };
|
|
|
|
}
|
|
|
|
TerminatorKind::Resume => {
|
|
|
|
if let Some(tgt) = self.cleanup_block {
|
|
|
|
*kind = TerminatorKind::Goto { target: tgt }
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 18:17:16 -06:00
|
|
|
TerminatorKind::Abort => { }
|
2017-02-08 03:24:49 -06:00
|
|
|
TerminatorKind::Unreachable => { }
|
2017-10-13 08:36:15 -05:00
|
|
|
TerminatorKind::FalseEdges { ref mut real_target, ref mut imaginary_targets } => {
|
|
|
|
*real_target = self.update_target(*real_target);
|
|
|
|
for target in imaginary_targets {
|
|
|
|
*target = self.update_target(*target);
|
|
|
|
}
|
|
|
|
}
|
2018-01-25 00:45:45 -06:00
|
|
|
TerminatorKind::FalseUnwind { real_target: _ , unwind: _ } =>
|
|
|
|
// see the ordering of passes in the optimized_mir query.
|
|
|
|
bug!("False unwinds should have been removed before inlining")
|
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];
|
|
|
|
}
|
|
|
|
}
|