@@ -16,9 +16,6 @@ use crate::transform::MirPass;
16
16
use std:: iter;
17
17
use std:: ops:: { Range , RangeFrom } ;
18
18
19
- const DEFAULT_THRESHOLD : usize = 50 ;
20
- const HINT_THRESHOLD : usize = 100 ;
21
-
22
19
const INSTR_COST : usize = 5 ;
23
20
const CALL_PENALTY : usize = 25 ;
24
21
const LANDINGPAD_PENALTY : usize = 50 ;
@@ -31,7 +28,8 @@ pub struct Inline;
31
28
#[ derive( Copy , Clone , Debug ) ]
32
29
struct CallSite < ' tcx > {
33
30
callee : Instance < ' tcx > ,
34
- bb : BasicBlock ,
31
+ block : BasicBlock ,
32
+ target : Option < BasicBlock > ,
35
33
source_info : SourceInfo ,
36
34
}
37
35
@@ -175,8 +173,7 @@ impl Inliner<'tcx> {
175
173
176
174
// Only consider direct calls to functions
177
175
let terminator = bb_data. terminator ( ) ;
178
- // FIXME: Handle inlining of diverging calls
179
- if let TerminatorKind :: Call { func : ref op, destination : Some ( _) , .. } = terminator. kind {
176
+ if let TerminatorKind :: Call { func : ref op, ref destination, .. } = terminator. kind {
180
177
if let ty:: FnDef ( callee_def_id, substs) = * op. ty ( caller_body, self . tcx ) . kind ( ) {
181
178
// To resolve an instance its substs have to be fully normalized, so
182
179
// we do this here.
@@ -190,7 +187,12 @@ impl Inliner<'tcx> {
190
187
return None ;
191
188
}
192
189
193
- return Some ( CallSite { callee, bb, source_info : terminator. source_info } ) ;
190
+ return Some ( CallSite {
191
+ callee,
192
+ block : bb,
193
+ target : destination. map ( |( _, target) | target) ,
194
+ source_info : terminator. source_info ,
195
+ } ) ;
194
196
}
195
197
}
196
198
@@ -248,7 +250,11 @@ impl Inliner<'tcx> {
248
250
}
249
251
}
250
252
251
- let mut threshold = if hinted { HINT_THRESHOLD } else { DEFAULT_THRESHOLD } ;
253
+ let mut threshold = if hinted {
254
+ self . tcx . sess . opts . debugging_opts . inline_mir_hint_threshold
255
+ } else {
256
+ self . tcx . sess . opts . debugging_opts . inline_mir_threshold
257
+ } ;
252
258
253
259
// Significantly lower the threshold for inlining cold functions
254
260
if codegen_fn_attrs. flags . contains ( CodegenFnAttrFlags :: COLD ) {
@@ -398,9 +404,9 @@ impl Inliner<'tcx> {
398
404
caller_body : & mut Body < ' tcx > ,
399
405
mut callee_body : Body < ' tcx > ,
400
406
) {
401
- let terminator = caller_body[ callsite. bb ] . terminator . take ( ) . unwrap ( ) ;
407
+ let terminator = caller_body[ callsite. block ] . terminator . take ( ) . unwrap ( ) ;
402
408
match terminator. kind {
403
- TerminatorKind :: Call { args, destination : Some ( destination ) , cleanup, .. } => {
409
+ TerminatorKind :: Call { args, destination, cleanup, .. } => {
404
410
// If the call is something like `a[*i] = f(i)`, where
405
411
// `i : &mut usize`, then just duplicating the `a[*i]`
406
412
// Place could result in two different locations if `f`
@@ -417,43 +423,39 @@ impl Inliner<'tcx> {
417
423
false
418
424
}
419
425
420
- let dest = if dest_needs_borrow ( destination. 0 ) {
421
- trace ! ( "creating temp for return destination" ) ;
422
- let dest = Rvalue :: Ref (
423
- self . tcx . lifetimes . re_erased ,
424
- BorrowKind :: Mut { allow_two_phase_borrow : false } ,
425
- destination. 0 ,
426
- ) ;
427
-
428
- let ty = dest. ty ( caller_body, self . tcx ) ;
429
-
430
- let temp = LocalDecl :: new ( ty, callsite. source_info . span ) ;
431
-
432
- let tmp = caller_body. local_decls . push ( temp) ;
433
- let tmp = Place :: from ( tmp) ;
434
-
435
- let stmt = Statement {
436
- source_info : callsite. source_info ,
437
- kind : StatementKind :: Assign ( box ( tmp, dest) ) ,
438
- } ;
439
- caller_body[ callsite. bb ] . statements . push ( stmt) ;
440
- self . tcx . mk_place_deref ( tmp)
426
+ let dest = if let Some ( ( destination_place, _) ) = destination {
427
+ if dest_needs_borrow ( destination_place) {
428
+ trace ! ( "creating temp for return destination" ) ;
429
+ let dest = Rvalue :: Ref (
430
+ self . tcx . lifetimes . re_erased ,
431
+ BorrowKind :: Mut { allow_two_phase_borrow : false } ,
432
+ destination_place,
433
+ ) ;
434
+ let dest_ty = dest. ty ( caller_body, self . tcx ) ;
435
+ let temp = Place :: from ( self . new_call_temp ( caller_body, & callsite, dest_ty) ) ;
436
+ caller_body[ callsite. block ] . statements . push ( Statement {
437
+ source_info : callsite. source_info ,
438
+ kind : StatementKind :: Assign ( box ( temp, dest) ) ,
439
+ } ) ;
440
+ self . tcx . mk_place_deref ( temp)
441
+ } else {
442
+ destination_place
443
+ }
441
444
} else {
442
- destination. 0
445
+ trace ! ( "creating temp for return place" ) ;
446
+ Place :: from ( self . new_call_temp ( caller_body, & callsite, callee_body. return_ty ( ) ) )
443
447
} ;
444
448
445
- let return_block = destination. 1 ;
446
-
447
449
// Copy the arguments if needed.
448
- let args: Vec < _ > = self . make_call_args ( args, & callsite, caller_body, return_block ) ;
450
+ let args: Vec < _ > = self . make_call_args ( args, & callsite, caller_body) ;
449
451
450
452
let mut integrator = Integrator {
451
453
args : & args,
452
454
new_locals : Local :: new ( caller_body. local_decls . len ( ) ) ..,
453
455
new_scopes : SourceScope :: new ( caller_body. source_scopes . len ( ) ) ..,
454
456
new_blocks : BasicBlock :: new ( caller_body. basic_blocks ( ) . len ( ) ) ..,
455
457
destination : dest,
456
- return_block,
458
+ return_block : callsite . target ,
457
459
cleanup_block : cleanup,
458
460
in_cleanup_block : false ,
459
461
tcx : self . tcx ,
@@ -502,7 +504,7 @@ impl Inliner<'tcx> {
502
504
caller_body. var_debug_info . extend ( callee_body. var_debug_info . drain ( ..) ) ;
503
505
caller_body. basic_blocks_mut ( ) . extend ( callee_body. basic_blocks_mut ( ) . drain ( ..) ) ;
504
506
505
- caller_body[ callsite. bb ] . terminator = Some ( Terminator {
507
+ caller_body[ callsite. block ] . terminator = Some ( Terminator {
506
508
source_info : callsite. source_info ,
507
509
kind : TerminatorKind :: Goto { target : integrator. map_block ( START_BLOCK ) } ,
508
510
} ) ;
@@ -526,7 +528,6 @@ impl Inliner<'tcx> {
526
528
args : Vec < Operand < ' tcx > > ,
527
529
callsite : & CallSite < ' tcx > ,
528
530
caller_body : & mut Body < ' tcx > ,
529
- return_block : BasicBlock ,
530
531
) -> Vec < Local > {
531
532
let tcx = self . tcx ;
532
533
@@ -557,18 +558,8 @@ impl Inliner<'tcx> {
557
558
// `callee_body.spread_arg == None`, instead of special-casing closures.
558
559
if tcx. is_closure ( callsite. callee . def_id ( ) ) {
559
560
let mut args = args. into_iter ( ) ;
560
- let self_ = self . create_temp_if_necessary (
561
- args. next ( ) . unwrap ( ) ,
562
- callsite,
563
- caller_body,
564
- return_block,
565
- ) ;
566
- let tuple = self . create_temp_if_necessary (
567
- args. next ( ) . unwrap ( ) ,
568
- callsite,
569
- caller_body,
570
- return_block,
571
- ) ;
561
+ let self_ = self . create_temp_if_necessary ( args. next ( ) . unwrap ( ) , callsite, caller_body) ;
562
+ let tuple = self . create_temp_if_necessary ( args. next ( ) . unwrap ( ) , callsite, caller_body) ;
572
563
assert ! ( args. next( ) . is_none( ) ) ;
573
564
574
565
let tuple = Place :: from ( tuple) ;
@@ -588,13 +579,13 @@ impl Inliner<'tcx> {
588
579
Operand :: Move ( tcx. mk_place_field ( tuple, Field :: new ( i) , ty. expect_ty ( ) ) ) ;
589
580
590
581
// Spill to a local to make e.g., `tmp0`.
591
- self . create_temp_if_necessary ( tuple_field, callsite, caller_body, return_block )
582
+ self . create_temp_if_necessary ( tuple_field, callsite, caller_body)
592
583
} ) ;
593
584
594
585
closure_ref_arg. chain ( tuple_tmp_args) . collect ( )
595
586
} else {
596
587
args. into_iter ( )
597
- . map ( |a| self . create_temp_if_necessary ( a, callsite, caller_body, return_block ) )
588
+ . map ( |a| self . create_temp_if_necessary ( a, callsite, caller_body) )
598
589
. collect ( )
599
590
}
600
591
}
@@ -606,46 +597,52 @@ impl Inliner<'tcx> {
606
597
arg : Operand < ' tcx > ,
607
598
callsite : & CallSite < ' tcx > ,
608
599
caller_body : & mut Body < ' tcx > ,
609
- return_block : BasicBlock ,
610
600
) -> Local {
611
- // FIXME: Analysis of the usage of the arguments to avoid
612
- // unnecessary temporaries.
613
-
601
+ // Reuse the operand if it is a moved temporary.
614
602
if let Operand :: Move ( place) = & arg {
615
603
if let Some ( local) = place. as_local ( ) {
616
604
if caller_body. local_kind ( local) == LocalKind :: Temp {
617
- // Reuse the operand if it's a temporary already
618
605
return local;
619
606
}
620
607
}
621
608
}
622
609
610
+ // Otherwise, create a temporary for the argument.
623
611
trace ! ( "creating temp for argument {:?}" , arg) ;
624
- // Otherwise, create a temporary for the arg
625
- let arg = Rvalue :: Use ( arg) ;
626
-
627
- let ty = arg. ty ( caller_body, self . tcx ) ;
628
-
629
- let arg_tmp = LocalDecl :: new ( ty, callsite. source_info . span ) ;
630
- let arg_tmp = caller_body. local_decls . push ( arg_tmp) ;
631
-
632
- caller_body[ callsite. bb ] . statements . push ( Statement {
612
+ let arg_ty = arg. ty ( caller_body, self . tcx ) ;
613
+ let local = self . new_call_temp ( caller_body, callsite, arg_ty) ;
614
+ caller_body[ callsite. block ] . statements . push ( Statement {
633
615
source_info : callsite. source_info ,
634
- kind : StatementKind :: StorageLive ( arg_tmp ) ,
616
+ kind : StatementKind :: Assign ( box ( Place :: from ( local ) , Rvalue :: Use ( arg ) ) ) ,
635
617
} ) ;
636
- caller_body[ callsite. bb ] . statements . push ( Statement {
618
+ local
619
+ }
620
+
621
+ /// Introduces a new temporary into the caller body that is live for the duration of the call.
622
+ fn new_call_temp (
623
+ & self ,
624
+ caller_body : & mut Body < ' tcx > ,
625
+ callsite : & CallSite < ' tcx > ,
626
+ ty : Ty < ' tcx > ,
627
+ ) -> Local {
628
+ let local = caller_body. local_decls . push ( LocalDecl :: new ( ty, callsite. source_info . span ) ) ;
629
+
630
+ caller_body[ callsite. block ] . statements . push ( Statement {
637
631
source_info : callsite. source_info ,
638
- kind : StatementKind :: Assign ( box ( Place :: from ( arg_tmp ) , arg ) ) ,
632
+ kind : StatementKind :: StorageLive ( local ) ,
639
633
} ) ;
640
- caller_body[ return_block] . statements . insert (
641
- 0 ,
642
- Statement {
643
- source_info : callsite. source_info ,
644
- kind : StatementKind :: StorageDead ( arg_tmp) ,
645
- } ,
646
- ) ;
647
-
648
- arg_tmp
634
+
635
+ if let Some ( block) = callsite. target {
636
+ caller_body[ block] . statements . insert (
637
+ 0 ,
638
+ Statement {
639
+ source_info : callsite. source_info ,
640
+ kind : StatementKind :: StorageDead ( local) ,
641
+ } ,
642
+ ) ;
643
+ }
644
+
645
+ local
649
646
}
650
647
}
651
648
@@ -670,7 +667,7 @@ struct Integrator<'a, 'tcx> {
670
667
new_scopes : RangeFrom < SourceScope > ,
671
668
new_blocks : RangeFrom < BasicBlock > ,
672
669
destination : Place < ' tcx > ,
673
- return_block : BasicBlock ,
670
+ return_block : Option < BasicBlock > ,
674
671
cleanup_block : Option < BasicBlock > ,
675
672
in_cleanup_block : bool ,
676
673
tcx : TyCtxt < ' tcx > ,
@@ -816,7 +813,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
816
813
}
817
814
}
818
815
TerminatorKind :: Return => {
819
- terminator. kind = TerminatorKind :: Goto { target : self . return_block } ;
816
+ terminator. kind = if let Some ( tgt) = self . return_block {
817
+ TerminatorKind :: Goto { target : tgt }
818
+ } else {
819
+ TerminatorKind :: Unreachable
820
+ }
820
821
}
821
822
TerminatorKind :: Resume => {
822
823
if let Some ( tgt) = self . cleanup_block {
0 commit comments