Class Loops
- Namespace
- Graphity.ControlFlow
- Assembly
- Graphity.ControlFlow.dll
The loop nesting forest of a control-flow graph: its loops, how they nest and where each of them is entered, irreducible loops included.
public sealed class Loops
- Inheritance
-
Loops
- Inherited Members
Examples
Here, the loop of a method load around its block then, from its control flow
cfg and its entry block entry:
// The basic blocks of load are the nodes 0..5 of cfg: entry, header, body, then,
// next, exit.
// Find the loops of load, from its entry block.
Loops loops = Loops.Of(BiAdjacentGraph.Of(cfg), entry);
// Find the loop around the block then, with its header and its body.
int loop = loops.InnermostLoopOf(then); // 0 - the for loop, the only loop
int header = loops.HeaderOf(loop); // 1 - the block header
// [1, 2, 3, 4] - header, body, then and next
ReadOnlySpan<int> blocks = loops.BodyOf(loop);
Remarks
A loop has a header, through which it is normally entered, and a body: the header, the other nodes of the loop and those of the loops nested in it. Of(BiAdjacentGraph, int) finds the loops from the entry of the graph. The forest is defined on every graph, reducible or not, over the nodes that the entries reach:
- The outermost loops are the strongly connected components that contain at least one edge.
- The header of a loop is the node of its component that a depth-first search from the entries reaches first. The search takes the entries in list order and the out-nodes of a node in ascending order.
- Without the edges that lead into the header from inside the component, the rest of it breaks up into the nested loops, by the same rule.
Uses Havlak's algorithm, without recursion. On a reducible graph, the loops are the natural loops.
An irreducible loop is a cycle that can be entered at more than one node, as goto, bytecode
or generated state machines produce it. It is one loop with several
entries, and which of them is the header depends on the order of the
search. An entry of the graph that lies in a loop is an entry of that loop. A node with a self-loop
is the header of a loop of its own, unless it already is the header of a larger one. Nodes that no
entry reaches are in no loop.
Loops have IDs 0..LoopCount-1, in preorder of the forest: a loop has a smaller ID than the
loops nested in it, which follow it as one range of IDs. Going through the IDs downwards visits the
inner loops first.
Like Dominators, the analysis follows edges against their direction and so takes a BiAdjacentGraph; the same graph serves both. Instances are immutable and safe to share between threads.
Memory: 4 x NodeCount bytes, plus 4 bytes per node in a loop, 24 bytes per loop and 4 bytes per entry of a loop.
Properties
IsReducible
Gets whether the graph is reducible: whether every loop has its header as its only entry.
public bool IsReducible { get; }
Property Value
Remarks
The loops of a reducible graph are its natural loops. A node counts only if the entries reach it.
Time: O(1).
LoopCount
Gets the number of loops.
public int LoopCount { get; }
Property Value
Remarks
Time: O(1).
Methods
BodyOf(int)
Returns the body of loop: its nodes and those of the loops nested in it.
public ReadOnlySpan<int> BodyOf(int loop)
Parameters
loopintLoop ID in
0..LoopCount-1.
Returns
- ReadOnlySpan<int>
The node IDs of the body.
Remarks
The nodes come in this order: first the header, then, in ascending order, the other nodes whose
innermost loop is loop, then the nodes of the nested loops in the same form,
loop after loop in ID order.
The result is a read-only span over this forest's own array, not a copy; to keep it beyond a
method, copy it with ToArray(). All bodies share the array, so a node is stored once,
however deeply its loop is nested.
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
loopis out of range.
EntriesOf(int)
Returns the entries of loop, in ascending order.
public SortedIntView EntriesOf(int loop)
Parameters
loopintLoop ID in
0..LoopCount-1.
Returns
- SortedIntView
The entry node IDs.
Remarks
These are the nodes of the loop with an in-node outside the loop, and the entries of the graph that lie in the loop. An in-node counts only if the entries of the graph reach it. A loop that can only be entered through its header has the header as its only entry.
The result is a view of this forest's own array, not a copy.
Time: O(EntriesOf(loop).Count).
Exceptions
- ArgumentOutOfRangeException
loopis out of range.
HeaderOf(int)
Returns the header of loop.
public int HeaderOf(int loop)
Parameters
loopintLoop ID in
0..LoopCount-1.
Returns
- int
The header node ID.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
loopis out of range.
InnermostLoopOf(int)
Returns the innermost loop that contains node, or -1 if it lies in no loop.
public int InnermostLoopOf(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
- int
The loop ID, or -1.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
IsInLoop(int, int)
Returns whether node lies in loop, directly or in a loop
nested in it.
public bool IsInLoop(int node, int loop)
Parameters
Returns
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeorloopis out of range.
LoopDepthOf(int)
Returns the loop depth of node: the number of loops that contain it. A node in
no loop has loop depth 0.
public int LoopDepthOf(int node)
Parameters
nodeintNode ID in
0..NodeCount-1.
Returns
- int
The loop depth.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
nodeis out of range.
Of(BiAdjacentGraph, int)
Returns the loop nesting forest of graph with one entry. Equivalent to
Of(graph, [entry]).
public static Loops Of(BiAdjacentGraph graph, int entry)
Parameters
graphBiAdjacentGraphThe control-flow graph.
entryintNode ID in
0..NodeCount-1.
Returns
- Loops
The loop nesting forest.
Remarks
Time: as for Of(BiAdjacentGraph, ReadOnlySpan<int>).
Memory: as for Of(BiAdjacentGraph, ReadOnlySpan<int>).
Exceptions
- ArgumentOutOfRangeException
entryis out of range.- ArgumentException
graphhas more than 2^30 - 30 nodes.
Of(BiAdjacentGraph, ReadOnlySpan<int>)
Returns the loop nesting forest of graph with the given entries.
public static Loops Of(BiAdjacentGraph graph, ReadOnlySpan<int> entries)
Parameters
graphBiAdjacentGraphThe control-flow graph.
entriesReadOnlySpan<int>Node IDs in
0..NodeCount-1, in the order the search takes them.
Returns
- Loops
The loop nesting forest.
Remarks
The entries work as in Of(BiAdjacentGraph, ReadOnlySpan<int>): an entry given twice counts once, and with no entries, no node is reachable, so there is no loop. Their order matters here: it decides which node of an irreducible loop the search reaches first, and so which node is its header.
Time: O(entries.Length + (NodeCount + EdgeCount) log NodeCount) on a reducible graph. On an
irreducible graph, an edge costs one more step for every loop it enters other than through the
header.
Memory: the result, as given in the class documentation; while it runs, about
45 x NodeCount + 4 x EdgeCount bytes more, plus 28 bytes per loop and 12 bytes per entry of a loop.
Exceptions
- ArgumentOutOfRangeException
An entry is out of range.
- ArgumentException
graphhas more than 2^30 - 30 nodes.
ParentOf(int)
Returns the loop that immediately encloses loop, or -1 for an outermost loop.
public int ParentOf(int loop)
Parameters
loopintLoop ID in
0..LoopCount-1.
Returns
- int
The enclosing loop ID, smaller than
loop, or -1.
Remarks
Time: O(1).
Exceptions
- ArgumentOutOfRangeException
loopis out of range.