Skip to content

Commit 86506d1

Browse files
committed
Make instance an option in CostChecker.
1 parent 8b15741 commit 86506d1

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

compiler/rustc_mir_transform/src/cost_checker.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::mir::visit::*;
22
use rustc_middle::mir::*;
3-
use rustc_middle::ty::{self, ParamEnv, TyCtxt};
3+
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
44

55
const INSTR_COST: usize = 5;
66
const CALL_PENALTY: usize = 25;
@@ -14,14 +14,14 @@ pub(crate) struct CostChecker<'b, 'tcx> {
1414
param_env: ParamEnv<'tcx>,
1515
cost: usize,
1616
callee_body: &'b Body<'tcx>,
17-
instance: ty::Instance<'tcx>,
17+
instance: Option<ty::Instance<'tcx>>,
1818
}
1919

2020
impl<'b, 'tcx> CostChecker<'b, 'tcx> {
2121
pub fn new(
2222
tcx: TyCtxt<'tcx>,
2323
param_env: ParamEnv<'tcx>,
24-
instance: ty::Instance<'tcx>,
24+
instance: Option<ty::Instance<'tcx>>,
2525
callee_body: &'b Body<'tcx>,
2626
) -> CostChecker<'b, 'tcx> {
2727
CostChecker { tcx, param_env, callee_body, instance, cost: 0 }
@@ -30,6 +30,14 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
3030
pub fn cost(&self) -> usize {
3131
self.cost
3232
}
33+
34+
fn instantiate_ty(&self, v: Ty<'tcx>) -> Ty<'tcx> {
35+
if let Some(instance) = self.instance {
36+
instance.instantiate_mir(self.tcx, ty::EarlyBinder::bind(&v))
37+
} else {
38+
v
39+
}
40+
}
3341
}
3442

3543
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
@@ -49,10 +57,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
4957
match terminator.kind {
5058
TerminatorKind::Drop { ref place, unwind, .. } => {
5159
// If the place doesn't actually need dropping, treat it like a regular goto.
52-
let ty = self.instance.instantiate_mir(
53-
tcx,
54-
ty::EarlyBinder::bind(&place.ty(self.callee_body, tcx).ty),
55-
);
60+
let ty = self.instantiate_ty(place.ty(self.callee_body, tcx).ty);
5661
if ty.needs_drop(tcx, self.param_env) {
5762
self.cost += CALL_PENALTY;
5863
if let UnwindAction::Cleanup(_) = unwind {
@@ -63,8 +68,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
6368
}
6469
}
6570
TerminatorKind::Call { func: Operand::Constant(ref f), unwind, .. } => {
66-
let fn_ty =
67-
self.instance.instantiate_mir(tcx, ty::EarlyBinder::bind(&f.const_.ty()));
71+
let fn_ty = self.instantiate_ty(f.const_.ty());
6872
self.cost += if let ty::FnDef(def_id, _) = *fn_ty.kind() && tcx.is_intrinsic(def_id) {
6973
// Don't give intrinsics the extra penalty for calls
7074
INSTR_COST

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ impl<'tcx> Inliner<'tcx> {
475475

476476
// FIXME: Give a bonus to functions with only a single caller
477477

478-
let mut checker = CostChecker::new(self.tcx, self.param_env, callsite.callee, callee_body);
478+
let mut checker =
479+
CostChecker::new(self.tcx, self.param_env, Some(callsite.callee), callee_body);
479480

480481
// Traverse the MIR manually so we can account for the effects of inlining on the CFG.
481482
let mut work_list = vec![START_BLOCK];

0 commit comments

Comments
 (0)