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,
@@ -467,8 +460,8 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
467
460
468
461
#[derive(Clone, Copy)]
469
462
struct RWU {
470
- reader: LiveNode,
471
- writer: LiveNode,
463
+ reader: Option< LiveNode> ,
464
+ writer: Option< LiveNode> ,
472
465
used: bool,
473
466
}
474
467
@@ -490,10 +483,10 @@ struct RWUTable {
490
483
unpacked_rwus: Vec<RWU>,
491
484
}
492
485
493
- // A constant representing `RWU { reader: INVALID_NODE ; writer: INVALID_NODE ; used: false }`.
486
+ // A constant representing `RWU { reader: None ; writer: None ; used: false }`.
494
487
const INV_INV_FALSE: u32 = u32::MAX;
495
488
496
- // A constant representing `RWU { reader: INVALID_NODE ; writer: INVALID_NODE ; used: true }`.
489
+ // A constant representing `RWU { reader: None ; writer: None ; used: true }`.
497
490
const INV_INV_TRUE: u32 = u32::MAX - 1;
498
491
499
492
impl RWUTable {
@@ -504,24 +497,24 @@ impl RWUTable {
504
497
fn get(&self, idx: usize) -> RWU {
505
498
let packed_rwu = self.packed_rwus[idx];
506
499
match packed_rwu {
507
- INV_INV_FALSE => RWU { reader: INVALID_NODE , writer: INVALID_NODE , used: false },
508
- INV_INV_TRUE => RWU { reader: INVALID_NODE , writer: INVALID_NODE , used: true },
500
+ INV_INV_FALSE => RWU { reader: None , writer: None , used: false },
501
+ INV_INV_TRUE => RWU { reader: None , writer: None , used: true },
509
502
_ => self.unpacked_rwus[packed_rwu as usize],
510
503
}
511
504
}
512
505
513
- fn get_reader(&self, idx: usize) -> LiveNode {
506
+ fn get_reader(&self, idx: usize) -> Option< LiveNode> {
514
507
let packed_rwu = self.packed_rwus[idx];
515
508
match packed_rwu {
516
- INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE ,
509
+ INV_INV_FALSE | INV_INV_TRUE => None ,
517
510
_ => self.unpacked_rwus[packed_rwu as usize].reader,
518
511
}
519
512
}
520
513
521
- fn get_writer(&self, idx: usize) -> LiveNode {
514
+ fn get_writer(&self, idx: usize) -> Option< LiveNode> {
522
515
let packed_rwu = self.packed_rwus[idx];
523
516
match packed_rwu {
524
- INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE ,
517
+ INV_INV_FALSE | INV_INV_TRUE => None ,
525
518
_ => self.unpacked_rwus[packed_rwu as usize].writer,
526
519
}
527
520
}
@@ -541,7 +534,7 @@ impl RWUTable {
541
534
}
542
535
543
536
fn assign_unpacked(&mut self, idx: usize, rwu: RWU) {
544
- if rwu.reader == INVALID_NODE && rwu.writer == INVALID_NODE {
537
+ if rwu.reader == None && rwu.writer == None {
545
538
// When we overwrite an indexing entry in `self.packed_rwus` with
546
539
// `INV_INV_{TRUE,FALSE}` we don't remove the corresponding entry
547
540
// from `self.unpacked_rwus`; it's not worth the effort, and we
@@ -570,7 +563,7 @@ struct Liveness<'a, 'tcx> {
570
563
typeck_results: &'a ty::TypeckResults<'tcx>,
571
564
param_env: ty::ParamEnv<'tcx>,
572
565
upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
573
- successors: IndexVec<LiveNode, LiveNode>,
566
+ successors: IndexVec<LiveNode, Option< LiveNode> >,
574
567
rwu_table: RWUTable,
575
568
576
569
/// A live node representing a point of execution before closure entry &
@@ -606,7 +599,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
606
599
typeck_results,
607
600
param_env,
608
601
upvars,
609
- successors: IndexVec::from_elem_n(INVALID_NODE , num_live_nodes),
602
+ successors: IndexVec::from_elem_n(None , num_live_nodes),
610
603
rwu_table: RWUTable::new(num_live_nodes * num_vars),
611
604
closure_ln,
612
605
exit_ln,
@@ -651,30 +644,33 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
651
644
}
652
645
653
646
fn live_on_entry(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
654
- assert!(ln.is_valid());
655
- let reader = self.rwu_table.get_reader(self.idx(ln, var));
656
- if reader.is_valid() { Some(self.ir.lnks[reader]) } else { None }
647
+ if let Some(reader) = self.rwu_table.get_reader(self.idx(ln, var)) {
648
+ Some(self.ir.lnks[reader])
649
+ } else {
650
+ None
651
+ }
657
652
}
658
653
659
654
// Is this variable live on entry to any of its successor nodes?
660
655
fn live_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
661
- let successor = self.successors[ln];
656
+ let successor = self.successors[ln].unwrap() ;
662
657
self.live_on_entry(successor, var)
663
658
}
664
659
665
660
fn used_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
666
- assert!(ln.is_valid());
667
661
self.rwu_table.get_used(self.idx(ln, var))
668
662
}
669
663
670
664
fn assigned_on_entry(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
671
- assert!(ln.is_valid());
672
- let writer = self.rwu_table.get_writer(self.idx(ln, var));
673
- if writer.is_valid() { Some(self.ir.lnks[writer]) } else { None }
665
+ if let Some(writer) = self.rwu_table.get_writer(self.idx(ln, var)) {
666
+ Some(self.ir.lnks[writer])
667
+ } else {
668
+ None
669
+ }
674
670
}
675
671
676
672
fn assigned_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
677
- let successor = self.successors[ln];
673
+ let successor = self.successors[ln].unwrap() ;
678
674
self.assigned_on_entry(successor, var)
679
675
}
680
676
@@ -709,9 +705,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
709
705
{
710
706
let wr = &mut wr as &mut dyn Write;
711
707
write!(wr, "[{:?} of kind {:?} reads", ln, self.ir.lnks[ln]);
712
- self.write_vars(wr, ln, |idx| self.rwu_table.get_reader(idx).is_valid ());
708
+ self.write_vars(wr, ln, |idx| self.rwu_table.get_reader(idx).is_some ());
713
709
write!(wr, " writes");
714
- self.write_vars(wr, ln, |idx| self.rwu_table.get_writer(idx).is_valid ());
710
+ self.write_vars(wr, ln, |idx| self.rwu_table.get_writer(idx).is_some ());
715
711
write!(wr, " uses");
716
712
self.write_vars(wr, ln, |idx| self.rwu_table.get_used(idx));
717
713
@@ -735,7 +731,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
735
731
}
736
732
737
733
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
738
- self.successors[ln] = succ_ln;
734
+ self.successors[ln] = Some( succ_ln) ;
739
735
740
736
// It is not necessary to initialize the RWUs here because they are all
741
737
// set to INV_INV_FALSE when they are created, and the sets only grow
@@ -744,7 +740,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
744
740
745
741
fn init_from_succ(&mut self, ln: LiveNode, succ_ln: LiveNode) {
746
742
// more efficient version of init_empty() / merge_from_succ()
747
- self.successors[ln] = succ_ln;
743
+ self.successors[ln] = Some( succ_ln) ;
748
744
749
745
self.indices2(ln, succ_ln, |this, idx, succ_idx| {
750
746
this.rwu_table.copy_packed(idx, succ_idx);
@@ -768,12 +764,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
768
764
let mut changed = false;
769
765
let mut rwu = this.rwu_table.get(idx);
770
766
let succ_rwu = this.rwu_table.get(succ_idx);
771
- if succ_rwu.reader.is_valid () && ! rwu.reader.is_valid () {
767
+ if succ_rwu.reader.is_some () && rwu.reader.is_none () {
772
768
rwu.reader = succ_rwu.reader;
773
769
changed = true
774
770
}
775
771
776
- if succ_rwu.writer.is_valid () && ! rwu.writer.is_valid () {
772
+ if succ_rwu.writer.is_some () && rwu.writer.is_none () {
777
773
rwu.writer = succ_rwu.writer;
778
774
changed = true
779
775
}
@@ -817,14 +813,14 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
817
813
let mut rwu = self.rwu_table.get(idx);
818
814
819
815
if (acc & ACC_WRITE) != 0 {
820
- rwu.reader = INVALID_NODE ;
821
- rwu.writer = ln ;
816
+ rwu.reader = None ;
817
+ rwu.writer = Some(ln) ;
822
818
}
823
819
824
820
// Important: if we both read/write, must do read second
825
821
// or else the write will override.
826
822
if (acc & ACC_READ) != 0 {
827
- rwu.reader = ln ;
823
+ rwu.reader = Some(ln) ;
828
824
}
829
825
830
826
if (acc & ACC_USE) != 0 {
0 commit comments