Skip to content

Commit 93e3db3

Browse files
committed
liveness: Use Option::None to represent absent live nodes
No functional changes intended.
1 parent 381b445 commit 93e3db3

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

compiler/rustc_passes/src/liveness.rs

+36-40
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,
@@ -478,8 +471,8 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
478471

479472
#[derive(Clone, Copy)]
480473
struct RWU {
481-
reader: LiveNode,
482-
writer: LiveNode,
474+
reader: Option<LiveNode>,
475+
writer: Option<LiveNode>,
483476
used: bool,
484477
}
485478

@@ -501,10 +494,10 @@ struct RWUTable {
501494
unpacked_rwus: Vec<RWU>,
502495
}
503496

504-
// A constant representing `RWU { reader: INVALID_NODE; writer: INVALID_NODE; used: false }`.
497+
// A constant representing `RWU { reader: None; writer: None; used: false }`.
505498
const INV_INV_FALSE: u32 = u32::MAX;
506499

507-
// A constant representing `RWU { reader: INVALID_NODE; writer: INVALID_NODE; used: true }`.
500+
// A constant representing `RWU { reader: None; writer: None; used: true }`.
508501
const INV_INV_TRUE: u32 = u32::MAX - 1;
509502

510503
impl RWUTable {
@@ -515,24 +508,24 @@ impl RWUTable {
515508
fn get(&self, idx: usize) -> RWU {
516509
let packed_rwu = self.packed_rwus[idx];
517510
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 },
520513
_ => self.unpacked_rwus[packed_rwu as usize],
521514
}
522515
}
523516

524-
fn get_reader(&self, idx: usize) -> LiveNode {
517+
fn get_reader(&self, idx: usize) -> Option<LiveNode> {
525518
let packed_rwu = self.packed_rwus[idx];
526519
match packed_rwu {
527-
INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE,
520+
INV_INV_FALSE | INV_INV_TRUE => None,
528521
_ => self.unpacked_rwus[packed_rwu as usize].reader,
529522
}
530523
}
531524

532-
fn get_writer(&self, idx: usize) -> LiveNode {
525+
fn get_writer(&self, idx: usize) -> Option<LiveNode> {
533526
let packed_rwu = self.packed_rwus[idx];
534527
match packed_rwu {
535-
INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE,
528+
INV_INV_FALSE | INV_INV_TRUE => None,
536529
_ => self.unpacked_rwus[packed_rwu as usize].writer,
537530
}
538531
}
@@ -552,7 +545,7 @@ impl RWUTable {
552545
}
553546

554547
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 {
556549
// When we overwrite an indexing entry in `self.packed_rwus` with
557550
// `INV_INV_{TRUE,FALSE}` we don't remove the corresponding entry
558551
// from `self.unpacked_rwus`; it's not worth the effort, and we
@@ -581,7 +574,7 @@ struct Liveness<'a, 'tcx> {
581574
typeck_results: &'a ty::TypeckResults<'tcx>,
582575
param_env: ty::ParamEnv<'tcx>,
583576
upvars: Option<&'tcx FxIndexMap<hir::HirId, hir::Upvar>>,
584-
successors: IndexVec<LiveNode, LiveNode>,
577+
successors: IndexVec<LiveNode, Option<LiveNode>>,
585578
rwu_table: RWUTable,
586579

587580
/// A live node representing a point of execution before closure entry &
@@ -617,7 +610,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
617610
typeck_results,
618611
param_env,
619612
upvars,
620-
successors: IndexVec::from_elem_n(INVALID_NODE, num_live_nodes),
613+
successors: IndexVec::from_elem_n(None, num_live_nodes),
621614
rwu_table: RWUTable::new(num_live_nodes * num_vars),
622615
closure_ln,
623616
exit_ln,
@@ -662,30 +655,33 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
662655
}
663656

664657
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+
}
668663
}
669664

670665
// Is this variable live on entry to any of its successor nodes?
671666
fn live_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
672-
let successor = self.successors[ln];
667+
let successor = self.successors[ln].unwrap();
673668
self.live_on_entry(successor, var)
674669
}
675670

676671
fn used_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
677-
assert!(ln.is_valid());
678672
self.rwu_table.get_used(self.idx(ln, var))
679673
}
680674

681675
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+
}
685681
}
686682

687683
fn assigned_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
688-
let successor = self.successors[ln];
684+
let successor = self.successors[ln].unwrap();
689685
self.assigned_on_entry(successor, var)
690686
}
691687

@@ -720,9 +716,9 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
720716
{
721717
let wr = &mut wr as &mut dyn Write;
722718
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());
724720
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());
726722
write!(wr, " uses");
727723
self.write_vars(wr, ln, |idx| self.rwu_table.get_used(idx));
728724

@@ -746,7 +742,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
746742
}
747743

748744
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
749-
self.successors[ln] = succ_ln;
745+
self.successors[ln] = Some(succ_ln);
750746

751747
// It is not necessary to initialize the RWUs here because they are all
752748
// set to INV_INV_FALSE when they are created, and the sets only grow
@@ -755,7 +751,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
755751

756752
fn init_from_succ(&mut self, ln: LiveNode, succ_ln: LiveNode) {
757753
// more efficient version of init_empty() / merge_from_succ()
758-
self.successors[ln] = succ_ln;
754+
self.successors[ln] = Some(succ_ln);
759755

760756
self.indices2(ln, succ_ln, |this, idx, succ_idx| {
761757
this.rwu_table.copy_packed(idx, succ_idx);
@@ -779,12 +775,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
779775
let mut changed = false;
780776
let mut rwu = this.rwu_table.get(idx);
781777
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() {
783779
rwu.reader = succ_rwu.reader;
784780
changed = true
785781
}
786782

787-
if succ_rwu.writer.is_valid() && !rwu.writer.is_valid() {
783+
if succ_rwu.writer.is_some() && rwu.writer.is_none() {
788784
rwu.writer = succ_rwu.writer;
789785
changed = true
790786
}
@@ -828,14 +824,14 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
828824
let mut rwu = self.rwu_table.get(idx);
829825

830826
if (acc & ACC_WRITE) != 0 {
831-
rwu.reader = INVALID_NODE;
832-
rwu.writer = ln;
827+
rwu.reader = None;
828+
rwu.writer = Some(ln);
833829
}
834830

835831
// Important: if we both read/write, must do read second
836832
// or else the write will override.
837833
if (acc & ACC_READ) != 0 {
838-
rwu.reader = ln;
834+
rwu.reader = Some(ln);
839835
}
840836

841837
if (acc & ACC_USE) != 0 {

0 commit comments

Comments
 (0)