Class Dominators
- Namespace
- Graphity
- Assembly
- Graphity.Core.dll
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.
public sealed class Dominators
- Inheritance
-
Dominators
- Inherited Members
Examples
Here, the dominators of the basic blocks of the method load from its entry, and
whether body and then dominate next:
Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);
// 2 - the block body
int idom = dominators.ImmediateDominatorOf(next);
Ancestry dominance = Ancestry.Of(dominators);
bool bodyDominatesNext = dominance.IsAncestorOf(body, next); // true
bool thenDominatesNext = dominance.IsAncestorOf(then, next); // false
Here, next is the first block that every path from body to the exit of
load passes through:
Dominators postDominators =
Dominators.Of(BiAdjacentGraph.Of(cfg).Transposed(), exit);
int join = postDominators.ImmediateDominatorOf(body); // 4 - the block next
Remarks
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 DominatorTree(Dominators) 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; IsReachable(int) tells the two apart.
Whether one node dominates another is an ancestor query on the dominator tree. Of(Dominators) answers it in O(log NodeCount) and PreorderAncestry in O(1). The analysis follows the edges against their direction, so it takes a BiAdjacentGraph.
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. 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 package Graphity.ControlFlow.
Instances are immutable and safe to share between threads.
Memory: 4 x NodeCount bytes and 1 bit per node, plus the graph, which stays reachable as long as the dominators are.
Properties
Graph
Gets the graph these dominators were computed on, for analyses that need both, such as dominance frontiers.
public BiAdjacentGraph Graph { get; }
Property Value
Remarks
Time: O(1).
Methods
ImmediateDominatorOf(int)
Returns the immediate dominator of node, or -1 if it has none.
public int ImmediateDominatorOf(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
- int
The immediate dominator, or -1.
Remarks
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).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
IsReachable(int)
Returns whether node is reachable from an entry.
public bool IsReachable(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
Of(BiAdjacentGraph, int)
Returns the dominators of graph with the single entry entry.
Equivalent to Of(graph, [entry]).
public static Dominators Of(BiAdjacentGraph graph, int entry)
Parameters
graphBiAdjacentGraphThe graph.
entryintNode ID in
0..NodeCount-1.
Returns
- Dominators
The dominators.
Remarks
Time: O((NodeCount + EdgeCount) log NodeCount).
Memory: 4 x NodeCount bytes and 1 bit per node for the result; while it runs, up to
32 x NodeCount bytes more.
Exceptions
- ArgumentOutOfRangeException
entryis out of range.- ArgumentException
graphhas more than 2^30 - 30 nodes.
Of(BiAdjacentGraph, ReadOnlySpan<int>)
Returns the dominators of graph with the given entries.
public static Dominators Of(BiAdjacentGraph graph, ReadOnlySpan<int> entries)
Parameters
graphBiAdjacentGraphThe graph.
entriesReadOnlySpan<int>Node IDs, each in
0..NodeCount-1.
Returns
- Dominators
The dominators.
Remarks
Several entries act like one virtual root above all of them, as the class documentation describes. Their order does not matter. An entry given twice counts once, and with no entries, no node is reachable.
Time: O(entries.Length + (NodeCount + EdgeCount) log NodeCount).
Memory: 4 x NodeCount bytes and 1 bit per node for the result; while it runs, up to
32 x NodeCount bytes more.
Exceptions
- ArgumentOutOfRangeException
An entry is out of range.
- ArgumentException
graphhas more than 2^30 - 30 nodes.