Class Loops

java.lang.Object
com.kobayami.graphity.controlflow.Loops

public final class Loops extends Object
The loop nesting forest of a control-flow graph: its loops, how they nest and where each of them is entered, irreducible loops included.

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 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.

  • Method Summary

    Modifier and Type
    Method
    Description
    bodyOf(int loop)
    Returns the body of loop: its nodes and those of the loops nested in it.
    entriesOf(int loop)
    Returns the entries of loop, in ascending order.
    int
    headerOf(int loop)
    Returns the header of loop.
    int
    innermostLoopOf(int node)
    Returns the innermost loop that contains node, or -1 if it lies in no loop.
    boolean
    isInLoop(int node, int loop)
    Returns whether node lies in loop, directly or in a loop nested in it.
    boolean
    Returns whether the graph is reducible: whether every loop has its header as its only entry.
    int
    Returns the number of loops.
    int
    loopDepthOf(int node)
    Returns the loop depth of node: the number of loops that contain it.
    static Loops
    of(BiAdjacentGraph graph, int entry)
    Returns the loop nesting forest of graph with one entry.
    static Loops
    of(BiAdjacentGraph graph, IntList entries)
    Returns the loop nesting forest of graph with the given entries.
    int
    parentOf(int loop)
    Returns the loop that immediately encloses loop, or -1 for an outermost loop.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait