//! - `reader`: the `LiveNode` ID of some node which will read the value
//! that `V` holds on entry to `N`. Formally: a node `M` such
//! that there exists a path `P` from `N` to `M` where `P` does not
-//! write `V`. If the `reader` is `invalid_node()`, then the current
+//! write `V`. If the `reader` is `INVALID_NODE`, then the current
//! value will never be read (the variable is dead, essentially).
//!
//! - `writer`: the `LiveNode` ID of some node which will write the
//! variable `V` and which is reachable from `N`. Formally: a node `M`
//! such that there exists a path `P` from `N` to `M` and `M` writes
-//! `V`. If the `writer` is `invalid_node()`, then there is no writer
+//! `V`. If the `writer` is `INVALID_NODE`, then there is no writer
//! of `V` that follows `N`.
//!
//! - `used`: a boolean value indicating whether `V` is *used*. We
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::{Expr, HirId, HirIdMap, HirIdSet, Node};
+use rustc_index::vec::IndexVec;
use rustc_middle::hir::map::Map;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Span;
use std::collections::VecDeque;
-use std::fmt;
use std::io;
use std::io::prelude::*;
use std::rc::Rc;
-#[derive(Copy, Clone, PartialEq)]
-struct Variable(u32);
-
-#[derive(Copy, Clone, PartialEq)]
-struct LiveNode(u32);
-
-impl Variable {
- fn get(&self) -> usize {
- self.0 as usize
+rustc_index::newtype_index! {
+ pub struct Variable {
+ DEBUG_FORMAT = "v({})",
}
}
-impl LiveNode {
- fn get(&self) -> usize {
- self.0 as usize
+rustc_index::newtype_index! {
+ pub struct LiveNode {
+ DEBUG_FORMAT = "ln({})",
+ const INVALID_NODE = LiveNode::MAX_AS_U32,
}
}
*providers = Providers { check_mod_liveness, ..*providers };
}
-impl fmt::Debug for LiveNode {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "ln({})", self.get())
- }
-}
-
-impl fmt::Debug for Variable {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "v({})", self.get())
- }
-}
-
// ______________________________________________________________________
// Creating ir_maps
//
// assignment. And so forth.
impl LiveNode {
- fn is_valid(&self) -> bool {
- self.0 != u32::MAX
+ fn is_valid(self) -> bool {
+ self != INVALID_NODE
}
}
-fn invalid_node() -> LiveNode {
- LiveNode(u32::MAX)
-}
-
struct CaptureInfo {
ln: LiveNode,
var_hid: HirId,
live_node_map: HirIdMap<LiveNode>,
variable_map: HirIdMap<Variable>,
capture_info_map: HirIdMap<Rc<Vec<CaptureInfo>>>,
- var_kinds: Vec<VarKind>,
- lnks: Vec<LiveNodeKind>,
+ var_kinds: IndexVec<Variable, VarKind>,
+ lnks: IndexVec<LiveNode, LiveNodeKind>,
}
impl IrMaps<'tcx> {
live_node_map: HirIdMap::default(),
variable_map: HirIdMap::default(),
capture_info_map: Default::default(),
- var_kinds: Vec::new(),
- lnks: Vec::new(),
+ var_kinds: IndexVec::new(),
+ lnks: IndexVec::new(),
}
}
fn add_live_node(&mut self, lnk: LiveNodeKind) -> LiveNode {
- let ln = LiveNode(self.lnks.len() as u32);
- self.lnks.push(lnk);
+ let ln = self.lnks.push(lnk);
debug!("{:?} is of kind {}", ln, live_node_kind_to_string(lnk, self.tcx));
}
fn add_variable(&mut self, vk: VarKind) -> Variable {
- let v = Variable(self.var_kinds.len() as u32);
- self.var_kinds.push(vk);
+ let v = self.var_kinds.push(vk);
match vk {
Local(LocalInfo { id: node_id, .. }) | Param(node_id, _) | Upvar(node_id, _) => {
}
fn variable_name(&self, var: Variable) -> Symbol {
- match self.var_kinds[var.get()] {
+ match self.var_kinds[var] {
Local(LocalInfo { name, .. }) | Param(_, name) | Upvar(_, name) => name,
}
}
fn variable_is_shorthand(&self, var: Variable) -> bool {
- match self.var_kinds[var.get()] {
+ match self.var_kinds[var] {
Local(LocalInfo { is_shorthand, .. }) => is_shorthand,
Param(..) | Upvar(..) => false,
}
}
fn lnk(&self, ln: LiveNode) -> LiveNodeKind {
- self.lnks[ln.get()]
+ self.lnks[ln]
}
}
unpacked_rwus: Vec<RWU>,
}
-// A constant representing `RWU { reader: invalid_node(); writer: invalid_node(); used: false }`.
+// A constant representing `RWU { reader: INVALID_NODE; writer: INVALID_NODE; used: false }`.
const INV_INV_FALSE: u32 = u32::MAX;
-// A constant representing `RWU { reader: invalid_node(); writer: invalid_node(); used: true }`.
+// A constant representing `RWU { reader: INVALID_NODE; writer: INVALID_NODE; used: true }`.
const INV_INV_TRUE: u32 = u32::MAX - 1;
impl RWUTable {
fn get(&self, idx: usize) -> RWU {
let packed_rwu = self.packed_rwus[idx];
match packed_rwu {
- INV_INV_FALSE => RWU { reader: invalid_node(), writer: invalid_node(), used: false },
- INV_INV_TRUE => RWU { reader: invalid_node(), writer: invalid_node(), used: true },
+ INV_INV_FALSE => RWU { reader: INVALID_NODE, writer: INVALID_NODE, used: false },
+ INV_INV_TRUE => RWU { reader: INVALID_NODE, writer: INVALID_NODE, used: true },
_ => self.unpacked_rwus[packed_rwu as usize],
}
}
fn get_reader(&self, idx: usize) -> LiveNode {
let packed_rwu = self.packed_rwus[idx];
match packed_rwu {
- INV_INV_FALSE | INV_INV_TRUE => invalid_node(),
+ INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE,
_ => self.unpacked_rwus[packed_rwu as usize].reader,
}
}
fn get_writer(&self, idx: usize) -> LiveNode {
let packed_rwu = self.packed_rwus[idx];
match packed_rwu {
- INV_INV_FALSE | INV_INV_TRUE => invalid_node(),
+ INV_INV_FALSE | INV_INV_TRUE => INVALID_NODE,
_ => self.unpacked_rwus[packed_rwu as usize].writer,
}
}
}
fn assign_unpacked(&mut self, idx: usize, rwu: RWU) {
- if rwu.reader == invalid_node() && rwu.writer == invalid_node() {
+ if rwu.reader == INVALID_NODE && rwu.writer == INVALID_NODE {
// When we overwrite an indexing entry in `self.packed_rwus` with
// `INV_INV_{TRUE,FALSE}` we don't remove the corresponding entry
// from `self.unpacked_rwus`; it's not worth the effort, and we
ir: &'a mut IrMaps<'tcx>,
typeck_results: &'a ty::TypeckResults<'tcx>,
param_env: ty::ParamEnv<'tcx>,
- successors: Vec<LiveNode>,
+ successors: IndexVec<LiveNode, LiveNode>,
rwu_table: RWUTable,
/// A live node representing a point of execution before closure entry &
ir,
typeck_results,
param_env,
- successors: vec![invalid_node(); num_live_nodes],
+ successors: IndexVec::from_elem_n(INVALID_NODE, num_live_nodes),
rwu_table: RWUTable::new(num_live_nodes * num_vars),
closure_ln,
exit_ln,
}
fn idx(&self, ln: LiveNode, var: Variable) -> usize {
- ln.get() * self.ir.var_kinds.len() + var.get()
+ ln.index() * self.ir.var_kinds.len() + var.index()
}
fn live_on_entry(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
// Is this variable live on entry to any of its successor nodes?
fn live_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
- let successor = self.successors[ln.get()];
+ let successor = self.successors[ln];
self.live_on_entry(successor, var)
}
}
fn assigned_on_exit(&self, ln: LiveNode, var: Variable) -> Option<LiveNodeKind> {
- let successor = self.successors[ln.get()];
+ let successor = self.successors[ln];
self.assigned_on_entry(successor, var)
}
where
F: FnMut(&mut Liveness<'a, 'tcx>, usize, usize),
{
- let node_base_idx = self.idx(ln, Variable(0));
- let succ_base_idx = self.idx(succ_ln, Variable(0));
+ let node_base_idx = self.idx(ln, Variable::from(0u32));
+ let succ_base_idx = self.idx(succ_ln, Variable::from(0u32));
for var_idx in 0..self.ir.var_kinds.len() {
op(self, node_base_idx + var_idx, succ_base_idx + var_idx);
}
where
F: FnMut(usize) -> bool,
{
- let node_base_idx = self.idx(ln, Variable(0));
+ let node_base_idx = self.idx(ln, Variable::from(0u32));
for var_idx in 0..self.ir.var_kinds.len() {
let idx = node_base_idx + var_idx;
if test(idx) {
- write!(wr, " {:?}", Variable(var_idx as u32))?;
+ write!(wr, " {:?}", Variable::from(var_idx))?;
}
}
Ok(())
let mut wr = Vec::new();
{
let wr = &mut wr as &mut dyn Write;
- write!(wr, "[ln({:?}) of kind {:?} reads", ln.get(), self.ir.lnk(ln));
+ write!(wr, "[{:?} of kind {:?} reads", ln, self.ir.lnk(ln));
self.write_vars(wr, ln, |idx| self.rwu_table.get_reader(idx).is_valid());
write!(wr, " writes");
self.write_vars(wr, ln, |idx| self.rwu_table.get_writer(idx).is_valid());
write!(wr, " uses");
self.write_vars(wr, ln, |idx| self.rwu_table.get_used(idx));
- write!(wr, " precedes {:?}]", self.successors[ln.get()]);
+ write!(wr, " precedes {:?}]", self.successors[ln]);
}
String::from_utf8(wr).unwrap()
}
"^^ liveness computation results for body {} (entry={:?})",
{
for ln_idx in 0..self.ir.lnks.len() {
- debug!("{:?}", self.ln_str(LiveNode(ln_idx as u32)));
+ debug!("{:?}", self.ln_str(LiveNode::from(ln_idx)));
}
hir_id
},
}
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
- self.successors[ln.get()] = succ_ln;
+ self.successors[ln] = succ_ln;
// It is not necessary to initialize the RWUs here because they are all
// set to INV_INV_FALSE when they are created, and the sets only grow
fn init_from_succ(&mut self, ln: LiveNode, succ_ln: LiveNode) {
// more efficient version of init_empty() / merge_from_succ()
- self.successors[ln.get()] = succ_ln;
+ self.successors[ln] = succ_ln;
self.indices2(ln, succ_ln, |this, idx, succ_idx| {
this.rwu_table.copy_packed(idx, succ_idx);
let mut rwu = self.rwu_table.get(idx);
if (acc & ACC_WRITE) != 0 {
- rwu.reader = invalid_node();
+ rwu.reader = INVALID_NODE;
rwu.writer = ln;
}