62
62
//! - `reader`: the `LiveNode` ID of some node which will read the value
63
63
//! that `V` holds on entry to `N`. Formally: a node `M` such
64
64
//! that there exists a path `P` from `N` to `M` where `P` does not
65
- //! write `V`. If the `reader` is `INVALID_NODE `, then the current
65
+ //! write `V`. If the `reader` is `None `, then the current
66
66
//! value will never be read (the variable is dead, essentially).
67
67
//!
68
68
//! - `writer`: the `LiveNode` ID of some node which will write the
69
69
//! variable `V` and which is reachable from `N`. Formally: a node `M`
70
70
//! such that there exists a path `P` from `N` to `M` and `M` writes
71
- //! `V`. If the `writer` is `INVALID_NODE `, then there is no writer
71
+ //! `V`. If the `writer` is `None `, then there is no writer
72
72
//! of `V` that follows `N`.
73
73
//!
74
74
//! - `used`: a boolean value indicating whether `V` is *used*. We
@@ -114,7 +114,6 @@ rustc_index::newtype_index! {
114
114
rustc_index:: newtype_index! {
115
115
pub struct LiveNode {
116
116
DEBUG_FORMAT = "ln({})" ,
117
- const INVALID_NODE = LiveNode :: MAX_AS_U32 ,
118
117
}
119
118
}
120
119
@@ -168,12 +167,6 @@ pub fn provide(providers: &mut Providers) {
168
167
// variable must not be assigned if there is some successor
169
168
// assignment. And so forth.
170
169
171
- impl LiveNode {
172
- fn is_valid ( self ) -> bool {
173
- self != INVALID_NODE
174
- }
175
- }
176
-
177
170
struct CaptureInfo {
178
171
ln : LiveNode ,
179
172
var_hid : HirId ,
@@ -478,8 +471,8 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
478
471
479
472
#[ derive( Clone , Copy ) ]
480
473
struct RWU {
481
- reader : LiveNode ,
482
- writer : LiveNode ,
474
+ reader : Option < LiveNode > ,
475
+ writer : Option < LiveNode > ,
483
476
used : bool ,
484
477
}
485
478
@@ -501,10 +494,10 @@ struct RWUTable {
501
494
unpacked_rwus : Vec < RWU > ,
502
495
}
503
496
504
- // A constant representing `RWU { reader: INVALID_NODE ; writer: INVALID_NODE ; used: false }`.
497
+ // A constant representing `RWU { reader: None ; writer: None ; used: false }`.
505
498
const INV_INV_FALSE : u32 = u32:: MAX ;
506
499
507
- // A constant representing `RWU { reader: INVALID_NODE ; writer: INVALID_NODE ; used: true }`.
500
+ // A constant representing `RWU { reader: None ; writer: None ; used: true }`.
508
501
const INV_INV_TRUE : u32 = u32:: MAX - 1 ;
509
502
510
503
impl RWUTable {
@@ -515,24 +508,24 @@ impl RWUTable {
515
508
fn get ( & self , idx : usize ) -> RWU {
516
509
let packed_rwu = self . packed_rwus [ idx] ;
517
510
match packed_rwu {
518
- INV_INV_FALSE => RWU { reader : INVALID_NODE , writer : INVALID_NODE , used : false } ,
519
- INV_INV_TRUE => RWU { reader : INVALID_NODE , writer : INVALID_NODE , used : true } ,
511
+ INV_INV_FALSE => RWU { reader : None , writer : None , used : false } ,
512
+ INV_INV_TRUE => RWU { reader : None , writer : None , used : true } ,
520
513
_ => self . unpacked_rwus [ packed_rwu as usize ] ,
521
514
}
522
515
}
523
516
524
- fn get_reader ( & self , idx : usize ) -> LiveNode {
517
+ fn get_reader ( & self , idx : usize ) -> Option < LiveNode > {
525
518
let packed_rwu = self . packed_rwus [ idx] ;
526
519
match packed_rwu {
527
- INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE ,
520
+ INV_INV_FALSE | INV_INV_TRUE => None ,
528
521
_ => self . unpacked_rwus [ packed_rwu as usize ] . reader ,
529
522
}
530
523
}
531
524
532
- fn get_writer ( & self , idx : usize ) -> LiveNode {
525
+ fn get_writer ( & self , idx : usize ) -> Option < LiveNode > {
533
526
let packed_rwu = self . packed_rwus [ idx] ;
534
527
match packed_rwu {
535
- INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE ,
528
+ INV_INV_FALSE | INV_INV_TRUE => None ,
536
529
_ => self . unpacked_rwus [ packed_rwu as usize ] . writer ,
537
530
}
538
531
}
@@ -552,7 +545,7 @@ impl RWUTable {
552
545
}
553
546
554
547
fn assign_unpacked ( & mut self , idx : usize , rwu : RWU ) {
555
- if rwu. reader == INVALID_NODE && rwu. writer == INVALID_NODE {
548
+ if rwu. reader == None && rwu. writer == None {
556
549
// When we overwrite an indexing entry in `self.packed_rwus` with
557
550
// `INV_INV_{TRUE,FALSE}` we don't remove the corresponding entry
558
551
// from `self.unpacked_rwus`; it's not worth the effort, and we
@@ -581,7 +574,7 @@ struct Liveness<'a, 'tcx> {
581
574
typeck_results : & ' a ty:: TypeckResults < ' tcx > ,
582
575
param_env : ty:: ParamEnv < ' tcx > ,
583
576
upvars : Option < & ' tcx FxIndexMap < hir:: HirId , hir:: Upvar > > ,
584
- successors : IndexVec < LiveNode , LiveNode > ,
577
+ successors : IndexVec < LiveNode , Option < LiveNode > > ,
585
578
rwu_table : RWUTable ,
586
579
587
580
/// A live node representing a point of execution before closure entry &
@@ -617,7 +610,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
617
610
typeck_results,
618
611
param_env,
619
612
upvars,
620
- successors : IndexVec :: from_elem_n ( INVALID_NODE , num_live_nodes) ,
613
+ successors : IndexVec :: from_elem_n ( None , num_live_nodes) ,
621
614
rwu_table : RWUTable :: new ( num_live_nodes * num_vars) ,
622
615
closure_ln,
623
616
exit_ln,
@@ -662,30 +655,33 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
662
655
}
663
656
664
657
fn live_on_entry ( & self , ln : LiveNode , var : Variable ) -> Option < LiveNodeKind > {
665
- assert ! ( ln. is_valid( ) ) ;
666
- let reader = self . rwu_table . get_reader ( self . idx ( ln, var) ) ;
667
- if reader. is_valid ( ) { Some ( self . ir . lnks [ reader] ) } else { None }
658
+ if let Some ( reader) = self . rwu_table . get_reader ( self . idx ( ln, var) ) {
659
+ Some ( self . ir . lnks [ reader] )
660
+ } else {
661
+ None
662
+ }
668
663
}
669
664
670
665
// Is this variable live on entry to any of its successor nodes?
671
666
fn live_on_exit ( & self , ln : LiveNode , var : Variable ) -> Option < LiveNodeKind > {
672
- let successor = self . successors [ ln] ;
667
+ let successor = self . successors [ ln] . unwrap ( ) ;
673
668
self . live_on_entry ( successor, var)
674
669
}
675
670
676
671
fn used_on_entry ( & self , ln : LiveNode , var : Variable ) -> bool {
677
- assert ! ( ln. is_valid( ) ) ;
678
672
self . rwu_table . get_used ( self . idx ( ln, var) )
679
673
}
680
674
681
675
fn assigned_on_entry ( & self , ln : LiveNode , var : Variable ) -> Option < LiveNodeKind > {
682
- assert ! ( ln. is_valid( ) ) ;
683
- let writer = self . rwu_table . get_writer ( self . idx ( ln, var) ) ;
684
- if writer. is_valid ( ) { Some ( self . ir . lnks [ writer] ) } else { None }
676
+ if let Some ( writer) = self . rwu_table . get_writer ( self . idx ( ln, var) ) {
677
+ Some ( self . ir . lnks [ writer] )
678
+ } else {
679
+ None
680
+ }
685
681
}
686
682
687
683
fn assigned_on_exit ( & self , ln : LiveNode , var : Variable ) -> Option < LiveNodeKind > {
688
- let successor = self . successors [ ln] ;
684
+ let successor = self . successors [ ln] . unwrap ( ) ;
689
685
self . assigned_on_entry ( successor, var)
690
686
}
691
687
@@ -720,9 +716,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
720
716
{
721
717
let wr = & mut wr as & mut dyn Write ;
722
718
write ! ( wr, "[{:?} of kind {:?} reads" , ln, self . ir. lnks[ ln] ) ;
723
- self . write_vars ( wr, ln, |idx| self . rwu_table . get_reader ( idx) . is_valid ( ) ) ;
719
+ self . write_vars ( wr, ln, |idx| self . rwu_table . get_reader ( idx) . is_some ( ) ) ;
724
720
write ! ( wr, " writes" ) ;
725
- self . write_vars ( wr, ln, |idx| self . rwu_table . get_writer ( idx) . is_valid ( ) ) ;
721
+ self . write_vars ( wr, ln, |idx| self . rwu_table . get_writer ( idx) . is_some ( ) ) ;
726
722
write ! ( wr, " uses" ) ;
727
723
self . write_vars ( wr, ln, |idx| self . rwu_table . get_used ( idx) ) ;
728
724
@@ -746,7 +742,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
746
742
}
747
743
748
744
fn init_empty ( & mut self , ln : LiveNode , succ_ln : LiveNode ) {
749
- self . successors [ ln] = succ_ln;
745
+ self . successors [ ln] = Some ( succ_ln) ;
750
746
751
747
// It is not necessary to initialize the RWUs here because they are all
752
748
// set to INV_INV_FALSE when they are created, and the sets only grow
@@ -755,7 +751,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
755
751
756
752
fn init_from_succ ( & mut self , ln : LiveNode , succ_ln : LiveNode ) {
757
753
// more efficient version of init_empty() / merge_from_succ()
758
- self . successors [ ln] = succ_ln;
754
+ self . successors [ ln] = Some ( succ_ln) ;
759
755
760
756
self . indices2 ( ln, succ_ln, |this, idx, succ_idx| {
761
757
this. rwu_table . copy_packed ( idx, succ_idx) ;
@@ -779,12 +775,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
779
775
let mut changed = false ;
780
776
let mut rwu = this. rwu_table . get ( idx) ;
781
777
let succ_rwu = this. rwu_table . get ( succ_idx) ;
782
- if succ_rwu. reader . is_valid ( ) && ! rwu. reader . is_valid ( ) {
778
+ if succ_rwu. reader . is_some ( ) && rwu. reader . is_none ( ) {
783
779
rwu. reader = succ_rwu. reader ;
784
780
changed = true
785
781
}
786
782
787
- if succ_rwu. writer . is_valid ( ) && ! rwu. writer . is_valid ( ) {
783
+ if succ_rwu. writer . is_some ( ) && rwu. writer . is_none ( ) {
788
784
rwu. writer = succ_rwu. writer ;
789
785
changed = true
790
786
}
@@ -828,14 +824,14 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
828
824
let mut rwu = self . rwu_table . get ( idx) ;
829
825
830
826
if ( acc & ACC_WRITE ) != 0 {
831
- rwu. reader = INVALID_NODE ;
832
- rwu. writer = ln ;
827
+ rwu. reader = None ;
828
+ rwu. writer = Some ( ln ) ;
833
829
}
834
830
835
831
// Important: if we both read/write, must do read second
836
832
// or else the write will override.
837
833
if ( acc & ACC_READ ) != 0 {
838
- rwu. reader = ln ;
834
+ rwu. reader = Some ( ln ) ;
839
835
}
840
836
841
837
if ( acc & ACC_USE ) != 0 {
0 commit comments