Skip to content

Commit d4add19

Browse files
Rollup merge of rust-lang#77296 - tmiasko:liveness-option, r=ecstatic-morse
liveness: Use Option::None to represent absent live nodes No functional changes intended.
2 parents 87387fd + 93e3db3 commit d4add19

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

compiler/rustc_passes/src/liveness.rs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
//! - `reader`: the `LiveNode` ID of some node which will read the value
6363
//! that `V` holds on entry to `N`. Formally: a node `M` such
6464
//! 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
6666
//! value will never be read (the variable is dead, essentially).
6767
//!
6868
//! - `writer`: the `LiveNode` ID of some node which will write the
6969
//! variable `V` and which is reachable from `N`. Formally: a node `M`
7070
//! 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
7272
//! of `V` that follows `N`.
7373
//!
7474
//! - `used`: a boolean value indicating whether `V` is *used*. We
@@ -114,7 +114,6 @@ rustc_index::newtype_index! {
114114
rustc_index::newtype_index! {
115115
pub struct LiveNode {
116116
DEBUG_FORMAT = "ln({})",
117-
const INVALID_NODE = LiveNode::MAX_AS_U32,
118117
}
119118
}
120119

@@ -168,12 +167,6 @@ pub fn provide(providers: &mut Providers) {
168167
// variable must not be assigned if there is some successor
169168
// assignment. And so forth.
170169

171-
impl LiveNode {
172-
fn is_valid(self) -> bool {
173-
self != INVALID_NODE
174-
}
175-
}
176-
177170
struct CaptureInfo {
178171
ln: LiveNode,
179172
var_hid: HirId,
@@ -467,8 +460,8 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
467460

468461
#[derive(Clone, Copy)]
469462
struct RWU {
470-
reader: LiveNode,
471-
writer: LiveNode,
463+
reader: Option<LiveNode>,
464+
writer: Option<LiveNode>,
472465
used: bool,
473466
}
474467

@@ -490,10 +483,10 @@ struct RWUTable {
490483
unpacked_rwus: Vec<RWU>,
491484
}
492485

493-
// A constant representing `RWU { reader: INVALID_NODE; writer: INVALID_NODE; used: false }`.
486+
// A constant representing `RWU { reader: None; writer: None; used: false }`.
494487
const INV_INV_FALSE: u32 = u32::MAX;
495488

496-
// A constant representing `RWU { reader: INVALID_NODE; writer: INVALID_NODE; used: true }`.
489+
// A constant representing `RWU { reader: None; writer: None; used: true }`.
497490
const INV_INV_TRUE: u32 = u32::MAX - 1;
498491

499492
impl RWUTable {
@@ -504,24 +497,24 @@ impl RWUTable {
504497
fn get(&self, idx: usize) -> RWU {
505498
let packed_rwu = self.packed_rwus[idx];
506499
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 },
509502
_ => self.unpacked_rwus[packed_rwu as usize],
510503
}
511504
}
512505

513-
fn get_reader(&self, idx: usize) -> LiveNode {
506+
fn get_reader(&self, idx: usize) -> Option<LiveNode> {
514507
let packed_rwu = self.packed_rwus[idx];
515508
match packed_rwu {
516-
INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE,
509+
INV_INV_FALSE | INV_INV_TRUE => None,
517510
_ => self.unpacked_rwus[packed_rwu as usize].reader,
518511
}
519512
}
520513

521-
fn get_writer(&self, idx: usize) -> LiveNode {
514+
fn get_writer(&self, idx: usize) -> Option<LiveNode> {
522515
let packed_rwu = self.packed_rwus[idx];
523516
match packed_rwu {
524-
INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE,
517+
INV_INV_FALSE | INV_INV_TRUE => None,
525518
_ => self.unpacked_rwus[packed_rwu as usize].writer,
526519
}
527520
}
@@ -541,7 +534,7 @@ impl RWUTable {
541534
}
542535

543536
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 {
545538
// When we overwrite an indexing entry in `self.packed_rwus` with
546539
// `INV_INV_{TRUE,FALSE}` we don't remove the corresponding entry
547540
// from `self.unpacked_rwus`; it's not worth the effort, and we
@@ -570,7 +563,7 @@ struct Liveness<'a, 'tcx> {
570563
typeck_results: &'a ty::TypeckResults<'tcx>,
571564
param_env: ty::ParamEnv<'tcx>,
572565
upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
573-
successors: IndexVec<LiveNode, LiveNode>,
566+
successors: IndexVec<LiveNode, Option<LiveNode>>,
574567
rwu_table: RWUTable,
575568

576569
/// A live node representing a point of execution before closure entry &
@@ -606,7 +599,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
606599
typeck_results,
607600
param_env,
608601
upvars,
609-
successors: IndexVec::from_elem_n(INVALID_NODE, num_live_nodes),
602+
successors: IndexVec::from_elem_n(None, num_live_nodes),
610603
rwu_table: RWUTable::new(num_live_nodes * num_vars),
611604
closure_ln,
612605
exit_ln,
@@ -651,30 +644,33 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
651644
}
652645

653646
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+
}
657652
}
658653

659654
// Is this variable live on entry to any of its successor nodes?
660655
fn live_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
661-
let successor = self.successors[ln];
656+
let successor = self.successors[ln].unwrap();
662657
self.live_on_entry(successor, var)
663658
}
664659

665660
fn used_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
666-
assert!(ln.is_valid());
667661
self.rwu_table.get_used(self.idx(ln, var))
668662
}
669663

670664
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+
}
674670
}
675671

676672
fn assigned_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
677-
let successor = self.successors[ln];
673+
let successor = self.successors[ln].unwrap();
678674
self.assigned_on_entry(successor, var)
679675
}
680676

@@ -709,9 +705,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
709705
{
710706
let wr = &mut wr as &mut dyn Write;
711707
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());
713709
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());
715711
write!(wr, " uses");
716712
self.write_vars(wr, ln, |idx| self.rwu_table.get_used(idx));
717713

@@ -735,7 +731,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
735731
}
736732

737733
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
738-
self.successors[ln] = succ_ln;
734+
self.successors[ln] = Some(succ_ln);
739735

740736
// It is not necessary to initialize the RWUs here because they are all
741737
// set to INV_INV_FALSE when they are created, and the sets only grow
@@ -744,7 +740,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
744740

745741
fn init_from_succ(&mut self, ln: LiveNode, succ_ln: LiveNode) {
746742
// more efficient version of init_empty() / merge_from_succ()
747-
self.successors[ln] = succ_ln;
743+
self.successors[ln] = Some(succ_ln);
748744

749745
self.indices2(ln, succ_ln, |this, idx, succ_idx| {
750746
this.rwu_table.copy_packed(idx, succ_idx);
@@ -768,12 +764,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
768764
let mut changed = false;
769765
let mut rwu = this.rwu_table.get(idx);
770766
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() {
772768
rwu.reader = succ_rwu.reader;
773769
changed = true
774770
}
775771

776-
if succ_rwu.writer.is_valid() && !rwu.writer.is_valid() {
772+
if succ_rwu.writer.is_some() && rwu.writer.is_none() {
777773
rwu.writer = succ_rwu.writer;
778774
changed = true
779775
}
@@ -817,14 +813,14 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
817813
let mut rwu = self.rwu_table.get(idx);
818814

819815
if (acc & ACC_WRITE) != 0 {
820-
rwu.reader = INVALID_NODE;
821-
rwu.writer = ln;
816+
rwu.reader = None;
817+
rwu.writer = Some(ln);
822818
}
823819

824820
// Important: if we both read/write, must do read second
825821
// or else the write will override.
826822
if (acc & ACC_READ) != 0 {
827-
rwu.reader = ln;
823+
rwu.reader = Some(ln);
828824
}
829825

830826
if (acc & ACC_USE) != 0 {

0 commit comments

Comments
 (0)