Make instance an option in CostChecker.
This commit is contained in:
parent
8252ad02c4
commit
3cb0c2e385
@ -1,6 +1,6 @@
|
|||||||
use rustc_middle::mir::visit::*;
|
use rustc_middle::mir::visit::*;
|
||||||
use rustc_middle::mir::*;
|
use rustc_middle::mir::*;
|
||||||
use rustc_middle::ty::{self, ParamEnv, TyCtxt};
|
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
|
||||||
|
|
||||||
const INSTR_COST: usize = 5;
|
const INSTR_COST: usize = 5;
|
||||||
const CALL_PENALTY: usize = 25;
|
const CALL_PENALTY: usize = 25;
|
||||||
@ -14,14 +14,14 @@ pub(crate) struct CostChecker<'b, 'tcx> {
|
|||||||
param_env: ParamEnv<'tcx>,
|
param_env: ParamEnv<'tcx>,
|
||||||
cost: usize,
|
cost: usize,
|
||||||
callee_body: &'b Body<'tcx>,
|
callee_body: &'b Body<'tcx>,
|
||||||
instance: ty::Instance<'tcx>,
|
instance: Option<ty::Instance<'tcx>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'b, 'tcx> CostChecker<'b, 'tcx> {
|
impl<'b, 'tcx> CostChecker<'b, 'tcx> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
param_env: ParamEnv<'tcx>,
|
param_env: ParamEnv<'tcx>,
|
||||||
instance: ty::Instance<'tcx>,
|
instance: Option<ty::Instance<'tcx>>,
|
||||||
callee_body: &'b Body<'tcx>,
|
callee_body: &'b Body<'tcx>,
|
||||||
) -> CostChecker<'b, 'tcx> {
|
) -> CostChecker<'b, 'tcx> {
|
||||||
CostChecker { tcx, param_env, callee_body, instance, cost: 0 }
|
CostChecker { tcx, param_env, callee_body, instance, cost: 0 }
|
||||||
@ -30,6 +30,14 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
|
|||||||
pub fn cost(&self) -> usize {
|
pub fn cost(&self) -> usize {
|
||||||
self.cost
|
self.cost
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn instantiate_ty(&self, v: Ty<'tcx>) -> Ty<'tcx> {
|
||||||
|
if let Some(instance) = self.instance {
|
||||||
|
instance.instantiate_mir(self.tcx, ty::EarlyBinder::bind(&v))
|
||||||
|
} else {
|
||||||
|
v
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
|
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
|
||||||
@ -49,10 +57,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
|
|||||||
match terminator.kind {
|
match terminator.kind {
|
||||||
TerminatorKind::Drop { ref place, unwind, .. } => {
|
TerminatorKind::Drop { ref place, unwind, .. } => {
|
||||||
// If the place doesn't actually need dropping, treat it like a regular goto.
|
// If the place doesn't actually need dropping, treat it like a regular goto.
|
||||||
let ty = self.instance.instantiate_mir(
|
let ty = self.instantiate_ty(place.ty(self.callee_body, tcx).ty);
|
||||||
tcx,
|
|
||||||
ty::EarlyBinder::bind(&place.ty(self.callee_body, tcx).ty),
|
|
||||||
);
|
|
||||||
if ty.needs_drop(tcx, self.param_env) {
|
if ty.needs_drop(tcx, self.param_env) {
|
||||||
self.cost += CALL_PENALTY;
|
self.cost += CALL_PENALTY;
|
||||||
if let UnwindAction::Cleanup(_) = unwind {
|
if let UnwindAction::Cleanup(_) = unwind {
|
||||||
@ -63,8 +68,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TerminatorKind::Call { func: Operand::Constant(ref f), unwind, .. } => {
|
TerminatorKind::Call { func: Operand::Constant(ref f), unwind, .. } => {
|
||||||
let fn_ty =
|
let fn_ty = self.instantiate_ty(f.const_.ty());
|
||||||
self.instance.instantiate_mir(tcx, ty::EarlyBinder::bind(&f.const_.ty()));
|
|
||||||
self.cost += if let ty::FnDef(def_id, _) = *fn_ty.kind() && tcx.is_intrinsic(def_id) {
|
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
|
// Don't give intrinsics the extra penalty for calls
|
||||||
INSTR_COST
|
INSTR_COST
|
||||||
|
@ -475,7 +475,8 @@ impl<'tcx> Inliner<'tcx> {
|
|||||||
|
|
||||||
// FIXME: Give a bonus to functions with only a single caller
|
// FIXME: Give a bonus to functions with only a single caller
|
||||||
|
|
||||||
let mut checker = CostChecker::new(self.tcx, self.param_env, callsite.callee, callee_body);
|
let mut checker =
|
||||||
|
CostChecker::new(self.tcx, self.param_env, Some(callsite.callee), callee_body);
|
||||||
|
|
||||||
// Traverse the MIR manually so we can account for the effects of inlining on the CFG.
|
// Traverse the MIR manually so we can account for the effects of inlining on the CFG.
|
||||||
let mut work_list = vec![START_BLOCK];
|
let mut work_list = vec![START_BLOCK];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user