Class Loops
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 - 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
IntList blocks = loops.bodyOf(loop);
- 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.
-
Method Summary
Modifier and TypeMethodDescriptionbodyOf(int loop) Returns the body ofloop: its nodes and those of the loops nested in it.entriesOf(int loop) Returns the entries ofloop, in ascending order.intheaderOf(int loop) Returns the header ofloop.intinnermostLoopOf(int node) Returns the innermost loop that containsnode, or-1if it lies in no loop.booleanisInLoop(int node, int loop) Returns whethernodelies inloop, directly or in a loop nested in it.booleanReturns whether the graph is reducible: whether every loop has its header as its only entry.intReturns the number of loops.intloopDepthOf(int node) Returns the loop depth ofnode: the number of loops that contain it.static Loopsof(BiAdjacentGraph graph, int entry) Returns the loop nesting forest ofgraphwith one entry.static Loopsof(BiAdjacentGraph graph, IntList entries) Returns the loop nesting forest ofgraphwith the given entries.intparentOf(int loop) Returns the loop that immediately enclosesloop, or-1for an outermost loop.
-
Method Details
-
of
Returns the loop nesting forest ofgraphwith one entry. Equivalent toof(graph, IntList.of(entry)).Time: as for
of(BiAdjacentGraph, IntList).
Memory: as forof(BiAdjacentGraph, IntList).- Parameters:
graph- the control-flow graphentry- node ID in0..nodeCount-1- Returns:
- the loop nesting forest
- Throws:
IndexOutOfBoundsException- ifentryis out of rangeIllegalArgumentException- ifgraphhas more than 230 - 6 nodes- In the guide:
-
of
Returns the loop nesting forest ofgraphwith the given entries.The entries work as in
Dominators.of(BiAdjacentGraph, IntList): 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.size() + (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.- Parameters:
graph- the control-flow graphentries- node IDs in0..nodeCount-1, in the order the search takes them- Returns:
- the loop nesting forest
- Throws:
IndexOutOfBoundsException- if an entry is out of rangeIllegalArgumentException- ifgraphhas more than 230 - 6 nodes- In the guide:
-
loopCount
Returns the number of loops.Time: O(1).
- Returns:
- loop count
- In the guide:
-
isReducible
Returns whether the graph is reducible: whether every loop has its header as its only entry.The loops of a reducible graph are its natural loops. A node counts only if the entries reach it.
Time: O(1).
- Returns:
trueif no loop can be entered other than through its header- In the guide:
-
innermostLoopOf
Returns the innermost loop that containsnode, or-1if it lies in no loop.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1- Returns:
- loop ID, or
-1 - Throws:
IndexOutOfBoundsException- ifnodeis out of range- In the guide:
-
loopDepthOf
Returns the loop depth ofnode: the number of loops that contain it. A node in no loop has loop depth0.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1- Returns:
- loop depth
- Throws:
IndexOutOfBoundsException- ifnodeis out of range- In the guide:
-
isInLoop
Returns whethernodelies inloop, directly or in a loop nested in it.Time: O(1).
- Parameters:
node- node ID in0..nodeCount-1loop- loop ID in0..loopCount()-1- Returns:
trueif the node is in the loop- Throws:
IndexOutOfBoundsException- ifnodeorloopis out of range- In the guide:
-
headerOf
Returns the header ofloop.Time: O(1).
- Parameters:
loop- loop ID in0..loopCount()-1- Returns:
- header node ID
- Throws:
IndexOutOfBoundsException- ifloopis out of range- In the guide:
-
entriesOf
Returns the entries ofloop, in ascending order.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).size()).
- Parameters:
loop- loop ID in0..loopCount()-1- Returns:
- entry node IDs
- Throws:
IndexOutOfBoundsException- ifloopis out of range- In the guide:
-
parentOf
Returns the loop that immediately enclosesloop, or-1for an outermost loop.Time: O(1).
- Parameters:
loop- loop ID in0..loopCount()-1- Returns:
- enclosing loop ID, smaller than
loop, or-1 - Throws:
IndexOutOfBoundsException- ifloopis out of range- In the guide:
-
bodyOf
Returns the body ofloop: its nodes and those of the loops nested in it.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 an unmodifiable view of this forest's own array, not a copy. All bodies share the array, so a node is stored once, however deeply its loop is nested.
Time: O(1).
- Parameters:
loop- loop ID in0..loopCount()-1- Returns:
- node IDs of the body
- Throws:
IndexOutOfBoundsException- ifloopis out of range- In the guide:
-