Skip to main content

Dominators

Struct Dominators 

Source
pub struct Dominators { /* private fields */ }
Expand description

The dominators of a graph with one or more entries, such as the control flow of a method, given by the immediate dominator of every node.

A node d dominates a node n if every path from an entry to n passes through d; every node dominates itself. The immediate dominator of n is the dominator closest to n other than n itself. The immediate dominators form the dominator tree, which graphs::dominator_tree returns as a graph. Several entries act like one virtual root above all of them. A node that only this virtual root dominates, such as an entry, has no immediate dominator and is a root of the dominator tree, which is then a forest. A node that no entry reaches has no immediate dominator either; is_reachable tells the two apart.

Whether one node dominates another is an ancestor query on the dominator tree. Ancestry::of_dominators answers it in O(log node_count) and PreorderAncestry in O(1). The analysis follows the edges against their direction, so it takes a BiAdjacentGraph. Here, the dominators of the basic blocks of the method load from its entry, and whether body and then dominate next:

let dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);
let idom = dominators.immediate_dominator_of(next); // Some(2) - the block body
let dominance = Ancestry::of_dominators(&dominators);
let body_dominates_next = dominance.is_ancestor_of(body, next); // true
let then_dominates_next = dominance.is_ancestor_of(then, next); // false

The post-dominators are the dominators of the transposed graph, with the exits as entries. A node p post-dominates n if every path from n to an exit passes through p. Here, next is the first block that every path from body to the exit of load passes through:

let post_dominators = Dominators::of(&BiAdjacentGraph::of(&cfg).transposed(), exit);
let join = post_dominators.immediate_dominator_of(body); // Some(4) - the block next

A node from which no exit can be reached, such as one in an endless loop, has no post-dominator, unless an extra exit with an edge from that loop is added to the graph first.

Uses the algorithm of Lengauer and Tarjan, without recursion. Dominance frontiers and control dependence are in the crate graphity-controlflow.

Instances are immutable and safe to share between threads (Send and Sync).

Memory: 4 x node_count bytes and 1 bit per node, plus the graph, which stays reachable as long as the dominators are.

Implementations§

Source§

impl Dominators

Source

pub fn of(graph: &BiAdjacentGraph, entry: u32) -> Dominators

Returns the dominators of graph with the single entry entry. Equivalent to of_entries(graph, &[entry]) - Java’s overload of(BiAdjacentGraph, int).

Time: O((node_count + edge_count) log node_count). Memory: 4 x node_count bytes and 1 bit per node for the result; while it runs, up to 32 x node_count bytes more.

§Panics

With an index out of bounds message if entry is not in 0..node_count; with an invalid argument message if graph has more than 2^30 - 6 nodes.

Source

pub fn of_entries(graph: &BiAdjacentGraph, entries: &[u32]) -> Dominators

Returns the dominators of graph with the given entries - Java’s overload of(BiAdjacentGraph, IntList).

Several entries act like one virtual root above all of them, as the type documentation describes. Their order does not matter. An entry given twice counts once, and with no entries, no node is reachable.

Time: O(entries.len() + (node_count + edge_count) log node_count). Memory: 4 x node_count bytes and 1 bit per node for the result; while it runs, up to 32 x node_count bytes more.

§Panics

With an index out of bounds message if an entry is not in 0..node_count; with an invalid argument message if graph has more than 2^30 - 6 nodes.

Source

pub fn graph(&self) -> &BiAdjacentGraph

Returns the graph these dominators were computed on, for analyses that need both, such as dominance frontiers. It shares its arrays with the graph passed in.

Time: O(1).

Source

pub fn immediate_dominator_of(&self, node: u32) -> Option<u32>

Returns the immediate dominator of node, or None if it has none - Java’s -1.

A node has none if it is an entry, if only the virtual root above several entries dominates it, or if no entry reaches it.

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Source

pub fn is_reachable(&self, node: u32) -> bool

Returns whether node is reachable from an entry.

Time: O(1).

§Panics

With an index out of bounds message if node is not in 0..node_count.

Trait Implementations§

Source§

impl Clone for Dominators

Source§

fn clone(&self) -> Dominators

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Dominators

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.