rustc_mir_transform/
cost_checker.rs
1use rustc_middle::bug;
2use rustc_middle::mir::visit::*;
3use rustc_middle::mir::*;
4use rustc_middle::ty::{self, Ty, TyCtxt};
5
6const INSTR_COST: usize = 5;
7const CALL_PENALTY: usize = 25;
8const LANDINGPAD_PENALTY: usize = 50;
9const RESUME_PENALTY: usize = 45;
10const LARGE_SWITCH_PENALTY: usize = 20;
11const CONST_SWITCH_BONUS: usize = 10;
12
13#[derive(Clone)]
15pub(super) struct CostChecker<'b, 'tcx> {
16 tcx: TyCtxt<'tcx>,
17 typing_env: ty::TypingEnv<'tcx>,
18 penalty: usize,
19 bonus: usize,
20 callee_body: &'b Body<'tcx>,
21 instance: Option<ty::Instance<'tcx>>,
22}
23
24impl<'b, 'tcx> CostChecker<'b, 'tcx> {
25 pub(super) fn new(
26 tcx: TyCtxt<'tcx>,
27 typing_env: ty::TypingEnv<'tcx>,
28 instance: Option<ty::Instance<'tcx>>,
29 callee_body: &'b Body<'tcx>,
30 ) -> CostChecker<'b, 'tcx> {
31 CostChecker { tcx, typing_env, callee_body, instance, penalty: 0, bonus: 0 }
32 }
33
34 pub(super) fn add_function_level_costs(&mut self) {
40 if self.callee_body.basic_blocks.iter().filter(|bbd| is_call_like(bbd.terminator())).count()
43 == 1
44 {
45 self.bonus += CALL_PENALTY;
46 }
47 }
48
49 pub(super) fn cost(&self) -> usize {
50 usize::saturating_sub(self.penalty, self.bonus)
51 }
52
53 fn instantiate_ty(&self, v: Ty<'tcx>) -> Ty<'tcx> {
54 if let Some(instance) = self.instance {
55 instance.instantiate_mir(self.tcx, ty::EarlyBinder::bind(&v))
56 } else {
57 v
58 }
59 }
60}
61
62impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
63 fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
64 match statement.kind {
66 StatementKind::Intrinsic(ref ndi) => {
67 self.penalty += match **ndi {
68 NonDivergingIntrinsic::Assume(..) => INSTR_COST,
69 NonDivergingIntrinsic::CopyNonOverlapping(..) => CALL_PENALTY,
70 };
71 }
72 _ => self.super_statement(statement, location),
73 }
74 }
75
76 fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, _location: Location) {
77 match rvalue {
78 Rvalue::NullaryOp(NullOp::UbChecks, ..)
79 if !self
80 .tcx
81 .sess
82 .opts
83 .unstable_opts
84 .inline_mir_preserve_debug
85 .unwrap_or(self.tcx.sess.ub_checks()) =>
86 {
87 self.bonus += CALL_PENALTY;
91 }
92 Rvalue::NullaryOp(..) => {}
95 _ => self.penalty += INSTR_COST,
96 }
97 }
98
99 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) {
100 match &terminator.kind {
101 TerminatorKind::Drop { place, unwind, .. } => {
102 let ty = self.instantiate_ty(place.ty(self.callee_body, self.tcx).ty);
104 if ty.needs_drop(self.tcx, self.typing_env) {
105 self.penalty += CALL_PENALTY;
106 if let UnwindAction::Cleanup(_) = unwind {
107 self.penalty += LANDINGPAD_PENALTY;
108 }
109 }
110 }
111 TerminatorKind::Call { func, unwind, .. } => {
112 self.penalty += if let Some((def_id, ..)) = func.const_fn_def()
113 && self.tcx.intrinsic(def_id).is_some()
114 {
115 INSTR_COST
117 } else {
118 CALL_PENALTY
119 };
120 if let UnwindAction::Cleanup(_) = unwind {
121 self.penalty += LANDINGPAD_PENALTY;
122 }
123 }
124 TerminatorKind::TailCall { .. } => {
125 self.penalty += CALL_PENALTY;
126 }
127 TerminatorKind::SwitchInt { discr, targets } => {
128 if discr.constant().is_some() {
129 self.bonus += CONST_SWITCH_BONUS;
132 } else if targets.all_targets().len() > 3 {
133 self.penalty += LARGE_SWITCH_PENALTY;
135 } else {
136 self.penalty += INSTR_COST;
137 }
138 }
139 TerminatorKind::Assert { unwind, msg, .. } => {
140 self.penalty += if msg.is_optional_overflow_check()
141 && !self
142 .tcx
143 .sess
144 .opts
145 .unstable_opts
146 .inline_mir_preserve_debug
147 .unwrap_or(self.tcx.sess.overflow_checks())
148 {
149 INSTR_COST
150 } else {
151 CALL_PENALTY
152 };
153 if let UnwindAction::Cleanup(_) = unwind {
154 self.penalty += LANDINGPAD_PENALTY;
155 }
156 }
157 TerminatorKind::UnwindResume => self.penalty += RESUME_PENALTY,
158 TerminatorKind::InlineAsm { unwind, .. } => {
159 self.penalty += INSTR_COST;
160 if let UnwindAction::Cleanup(_) = unwind {
161 self.penalty += LANDINGPAD_PENALTY;
162 }
163 }
164 TerminatorKind::Unreachable => {
165 self.bonus += INSTR_COST;
166 }
167 TerminatorKind::Goto { .. } | TerminatorKind::Return => {}
168 TerminatorKind::UnwindTerminate(..) => {}
169 kind @ (TerminatorKind::FalseUnwind { .. }
170 | TerminatorKind::FalseEdge { .. }
171 | TerminatorKind::Yield { .. }
172 | TerminatorKind::CoroutineDrop) => {
173 bug!("{kind:?} should not be in runtime MIR");
174 }
175 }
176 }
177}
178
179pub(super) fn is_call_like(terminator: &Terminator<'_>) -> bool {
185 use TerminatorKind::*;
186 match terminator.kind {
187 Call { .. } | TailCall { .. } | Drop { .. } | Assert { .. } | InlineAsm { .. } => true,
188
189 Goto { .. }
190 | SwitchInt { .. }
191 | UnwindResume
192 | UnwindTerminate(_)
193 | Return
194 | Unreachable => false,
195
196 Yield { .. } | CoroutineDrop | FalseEdge { .. } | FalseUnwind { .. } => {
197 unreachable!()
198 }
199 }
200}