@@ -12,7 +12,8 @@ const RESUME_PENALTY: usize = 45;
1212pub ( crate ) struct CostChecker < ' b , ' tcx > {
1313 tcx : TyCtxt < ' tcx > ,
1414 param_env : ParamEnv < ' tcx > ,
15- cost : usize ,
15+ penalty : usize ,
16+ bonus : usize ,
1617 callee_body : & ' b Body < ' tcx > ,
1718 instance : Option < ty:: Instance < ' tcx > > ,
1819}
@@ -24,11 +25,11 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
2425 instance : Option < ty:: Instance < ' tcx > > ,
2526 callee_body : & ' b Body < ' tcx > ,
2627 ) -> CostChecker < ' b , ' tcx > {
27- CostChecker { tcx, param_env, callee_body, instance, cost : 0 }
28+ CostChecker { tcx, param_env, callee_body, instance, penalty : 0 , bonus : 0 }
2829 }
2930
3031 pub fn cost ( & self ) -> usize {
31- self . cost
32+ usize :: saturating_sub ( self . penalty , self . bonus )
3233 }
3334
3435 fn instantiate_ty ( & self , v : Ty < ' tcx > ) -> Ty < ' tcx > {
@@ -48,7 +49,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
4849 | StatementKind :: StorageDead ( _)
4950 | StatementKind :: Deinit ( _)
5051 | StatementKind :: Nop => { }
51- _ => self . cost += INSTR_COST ,
52+ _ => self . penalty += INSTR_COST ,
5253 }
5354 }
5455
@@ -59,17 +60,17 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
5960 // If the place doesn't actually need dropping, treat it like a regular goto.
6061 let ty = self . instantiate_ty ( place. ty ( self . callee_body , tcx) . ty ) ;
6162 if ty. needs_drop ( tcx, self . param_env ) {
62- self . cost += CALL_PENALTY ;
63+ self . penalty += CALL_PENALTY ;
6364 if let UnwindAction :: Cleanup ( _) = unwind {
64- self . cost += LANDINGPAD_PENALTY ;
65+ self . penalty += LANDINGPAD_PENALTY ;
6566 }
6667 } else {
67- self . cost += INSTR_COST ;
68+ self . penalty += INSTR_COST ;
6869 }
6970 }
7071 TerminatorKind :: Call { func : Operand :: Constant ( ref f) , unwind, .. } => {
7172 let fn_ty = self . instantiate_ty ( f. const_ . ty ( ) ) ;
72- self . cost += if let ty:: FnDef ( def_id, _) = * fn_ty. kind ( )
73+ self . penalty += if let ty:: FnDef ( def_id, _) = * fn_ty. kind ( )
7374 && tcx. intrinsic ( def_id) . is_some ( )
7475 {
7576 // Don't give intrinsics the extra penalty for calls
@@ -78,23 +79,23 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
7879 CALL_PENALTY
7980 } ;
8081 if let UnwindAction :: Cleanup ( _) = unwind {
81- self . cost += LANDINGPAD_PENALTY ;
82+ self . penalty += LANDINGPAD_PENALTY ;
8283 }
8384 }
8485 TerminatorKind :: Assert { unwind, .. } => {
85- self . cost += CALL_PENALTY ;
86+ self . penalty += CALL_PENALTY ;
8687 if let UnwindAction :: Cleanup ( _) = unwind {
87- self . cost += LANDINGPAD_PENALTY ;
88+ self . penalty += LANDINGPAD_PENALTY ;
8889 }
8990 }
90- TerminatorKind :: UnwindResume => self . cost += RESUME_PENALTY ,
91+ TerminatorKind :: UnwindResume => self . penalty += RESUME_PENALTY ,
9192 TerminatorKind :: InlineAsm { unwind, .. } => {
92- self . cost += INSTR_COST ;
93+ self . penalty += INSTR_COST ;
9394 if let UnwindAction :: Cleanup ( _) = unwind {
94- self . cost += LANDINGPAD_PENALTY ;
95+ self . penalty += LANDINGPAD_PENALTY ;
9596 }
9697 }
97- _ => self . cost += INSTR_COST ,
98+ _ => self . penalty += INSTR_COST ,
9899 }
99100 }
100101}
0 commit comments