@@ -38,7 +38,7 @@ struct CallSite<'tcx> {
3838
3939impl < ' tcx > MirPass < ' tcx > for Inline {
4040 fn run_pass ( & self , tcx : TyCtxt < ' tcx > , source : MirSource < ' tcx > , body : & mut Body < ' tcx > ) {
41- if tcx. sess . opts . debugging_opts . mir_opt_level >= 2 {
41+ if tcx. sess . opts . debugging_opts . mir_opt_level >= 1 {
4242 if tcx. sess . opts . debugging_opts . instrument_coverage {
4343 // The current implementation of source code coverage injects code region counters
4444 // into the MIR, and assumes a 1-to-1 correspondence between MIR and source-code-
@@ -100,7 +100,12 @@ impl Inliner<'tcx> {
100100 continue ;
101101 }
102102
103- let callee_body = if let Some ( callee_def_id) = callsite. callee . as_local ( ) {
103+ let callee_body = if self . tcx . is_trivial_mir ( callsite. callee ) {
104+ self . tcx . optimized_mir ( callsite. callee )
105+ } else if self . tcx . sess . opts . debugging_opts . mir_opt_level < 2 {
106+ // Only inline trivial functions by default.
107+ continue ;
108+ } else if let Some ( callee_def_id) = callsite. callee . as_local ( ) {
104109 let callee_hir_id = self . tcx . hir ( ) . as_local_hir_id ( callee_def_id) ;
105110 let self_hir_id =
106111 self . tcx . hir ( ) . as_local_hir_id ( self . source . def_id ( ) . expect_local ( ) ) ;
@@ -802,3 +807,44 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
802807 * scope = self . scope_map [ * scope] ;
803808 }
804809}
810+
811+ struct FunctionCallFinder {
812+ found : bool ,
813+ }
814+
815+ impl FunctionCallFinder {
816+ fn new ( ) -> Self {
817+ FunctionCallFinder { found : false }
818+ }
819+ }
820+
821+ impl < ' tcx > Visitor < ' tcx > for FunctionCallFinder {
822+ fn visit_terminator ( & mut self , terminator : & Terminator < ' tcx > , _location : Location ) {
823+ if let TerminatorKind :: Call { .. } = terminator. kind {
824+ self . found = true ;
825+ }
826+ }
827+ }
828+
829+ pub fn is_trivial_mir ( tcx : TyCtxt < ' tcx > , did : DefId ) -> bool {
830+ debug ! ( "is_trivial_mir({:?})" , did) ;
831+ if tcx. is_constructor ( did) {
832+ debug ! ( "is_trivial_mir = true (constructor)" ) ;
833+ return true ;
834+ }
835+
836+ if let Some ( did) = did. as_local ( ) {
837+ let body = tcx
838+ . mir_drops_elaborated_and_const_checked ( ty:: WithOptConstParam :: unknown ( did) )
839+ . borrow ( ) ;
840+ let mut finder = FunctionCallFinder :: new ( ) ;
841+ finder. visit_body ( & body) ;
842+ debug ! ( "is_trivial_mir = {}" , !finder. found) ;
843+ !finder. found
844+ } else {
845+ // This branch is only taken if no `optimized_mir` is available for
846+ // an extern crate, as `is_trivial_mir` has otherwise been encoded.
847+ debug ! ( "is_trivial_mir = false (no MIR available)" ) ;
848+ false
849+ }
850+ }
0 commit comments