@@ -10,7 +10,6 @@ use rustc_middle::mir::visit::*;
1010use rustc_middle:: mir:: * ;
1111use rustc_middle:: ty:: TypeVisitableExt ;
1212use rustc_middle:: ty:: { self , Instance , InstanceDef , ParamEnv , Ty , TyCtxt } ;
13- use rustc_session:: config:: OptLevel ;
1413use rustc_target:: abi:: FieldIdx ;
1514use rustc_target:: spec:: abi:: Abi ;
1615
@@ -47,12 +46,8 @@ impl<'tcx> MirPass<'tcx> for Inline {
4746 }
4847
4948 match sess. mir_opt_level ( ) {
50- 0 | 1 => false ,
51- 2 => {
52- ( sess. opts . optimize == OptLevel :: Default
53- || sess. opts . optimize == OptLevel :: Aggressive )
54- && sess. opts . incremental == None
55- }
49+ 0 => false ,
50+ 1 | 2 => sess. opts . incremental . is_none ( ) ,
5651 _ => true ,
5752 }
5853 }
@@ -471,7 +466,15 @@ impl<'tcx> Inliner<'tcx> {
471466 if callee_body. basic_blocks . len ( ) <= 3 {
472467 threshold += threshold / 4 ;
473468 }
474- debug ! ( " final inline threshold = {}" , threshold) ;
469+
470+ if tcx. sess . mir_opt_level ( ) == 1 {
471+ threshold = 2 * INSTR_COST ;
472+
473+ if callee_body. basic_blocks . len ( ) > 1 {
474+ return Err ( "cost above threshold" ) ;
475+ }
476+ }
477+ debug ! ( "final inline threshold = {}" , threshold) ;
475478
476479 // FIXME: Give a bonus to functions with only a single caller
477480
@@ -481,6 +484,7 @@ impl<'tcx> Inliner<'tcx> {
481484 instance : callsite. callee ,
482485 callee_body,
483486 cost : 0 ,
487+ threshold,
484488 } ;
485489
486490 // Traverse the MIR manually so we can account for the effects of inlining on the CFG.
@@ -493,6 +497,10 @@ impl<'tcx> Inliner<'tcx> {
493497
494498 let blk = & callee_body. basic_blocks [ bb] ;
495499 checker. visit_basic_block_data ( bb, blk) ;
500+ if callee_attrs. inline != InlineAttr :: Always && checker. cost > threshold {
501+ debug ! ( "checker cost exceeded threshold partway through" ) ;
502+ return Err ( "cost above threshold" ) ;
503+ }
496504
497505 let term = blk. terminator ( ) ;
498506 if let TerminatorKind :: Drop { ref place, target, unwind, replace : _ } = term. kind {
@@ -805,12 +813,17 @@ struct CostChecker<'b, 'tcx> {
805813 tcx : TyCtxt < ' tcx > ,
806814 param_env : ParamEnv < ' tcx > ,
807815 cost : usize ,
816+ threshold : usize ,
808817 callee_body : & ' b Body < ' tcx > ,
809818 instance : ty:: Instance < ' tcx > ,
810819}
811820
812821impl < ' tcx > Visitor < ' tcx > for CostChecker < ' _ , ' tcx > {
813822 fn visit_statement ( & mut self , statement : & Statement < ' tcx > , _: Location ) {
823+ if self . cost > self . threshold {
824+ return ;
825+ }
826+
814827 // Don't count StorageLive/StorageDead in the inlining cost.
815828 match statement. kind {
816829 StatementKind :: StorageLive ( _)
0 commit comments