Skip to content

Commit

Permalink
Avoid using Option where values are always Some
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Dec 6, 2021
1 parent 2b63059 commit 3187480
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions compiler/rustc_data_structures/src/graph/dominators/mod.rs
Expand Up @@ -23,8 +23,11 @@ rustc_index::newtype_index! {
pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
// compute the post order index (rank) for each node
let mut post_order_rank = IndexVec::from_elem_n(0, graph.num_nodes());
let mut parent: IndexVec<PreorderIndex, Option<PreorderIndex>> =
IndexVec::from_elem_n(None, graph.num_nodes());

// We allocate capacity for the full set of nodes, because most of the time
// most of the nodes *are* reachable.
let mut parent: IndexVec<PreorderIndex, PreorderIndex> =
IndexVec::with_capacity(graph.num_nodes());

let mut stack = vec![PreOrderFrame {
pre_order_idx: PreorderIndex::new(0),
Expand All @@ -35,6 +38,7 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
let mut real_to_pre_order: IndexVec<G::Node, Option<PreorderIndex>> =
IndexVec::from_elem_n(None, graph.num_nodes());
pre_order_to_real.push(graph.start_node());
parent.push(PreorderIndex::new(0)); // the parent of the root node is the root for now.
real_to_pre_order[graph.start_node()] = Some(PreorderIndex::new(0));
let mut post_order_idx = 0;

Expand All @@ -43,7 +47,7 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
if real_to_pre_order[successor].is_none() {
let pre_order_idx = pre_order_to_real.push(successor);
real_to_pre_order[successor] = Some(pre_order_idx);
parent[pre_order_idx] = Some(frame.pre_order_idx);
parent.push(frame.pre_order_idx);
stack.push(PreOrderFrame { pre_order_idx, iter: graph.successors(successor) });

continue 'recurse;
Expand All @@ -67,7 +71,7 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {
// Optimization: process buckets just once, at the start of the
// iteration. Do not explicitly empty the bucket (even though it will
// not be used again), to save some instructions.
let z = parent[w].unwrap();
let z = parent[w];
for &v in bucket[z].iter() {
let y = eval(&mut parent, lastlinked, &semi, &mut label, v);
idom[v] = if semi[y] < z { y } else { z };
Expand All @@ -83,10 +87,10 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {

// Optimization: Do not insert into buckets if parent[w] = semi[w], as
// we then immediately know the idom.
if parent[w].unwrap() != semi[w] {
if parent[w] != semi[w] {
bucket[semi[w]].push(w);
} else {
idom[w] = parent[w].unwrap();
idom[w] = parent[w];
}

// Optimization: We share the parent array between processed and not
Expand All @@ -109,7 +113,7 @@ pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {

#[inline]
fn eval(
ancestor: &mut IndexVec<PreorderIndex, Option<PreorderIndex>>,
ancestor: &mut IndexVec<PreorderIndex, PreorderIndex>,
lastlinked: Option<PreorderIndex>,
semi: &IndexVec<PreorderIndex, PreorderIndex>,
label: &mut IndexVec<PreorderIndex, PreorderIndex>,
Expand All @@ -130,14 +134,14 @@ fn is_processed(v: PreorderIndex, lastlinked: Option<PreorderIndex>) -> bool {

#[inline]
fn compress(
ancestor: &mut IndexVec<PreorderIndex, Option<PreorderIndex>>,
ancestor: &mut IndexVec<PreorderIndex, PreorderIndex>,
lastlinked: Option<PreorderIndex>,
semi: &IndexVec<PreorderIndex, PreorderIndex>,
label: &mut IndexVec<PreorderIndex, PreorderIndex>,
v: PreorderIndex,
) {
assert!(is_processed(v, lastlinked));
let u = ancestor[v].unwrap();
let u = ancestor[v];
if is_processed(u, lastlinked) {
compress(ancestor, lastlinked, semi, label, u);
if semi[label[u]] < semi[label[v]] {
Expand Down

0 comments on commit 3187480

Please sign in to comment.