pub struct Loops { /* private fields */ }Expand description
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. Loops::of 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 to 5 of cfg: entry, header, body, then,
// next, exit.
// Find the loops of load, from its entry block.
let loops = Loops::of(&BiAdjacentGraph::of(&cfg), entry);
// Find the loop around the block then, with its header and its body.
// 0 - the for loop, the only loop
let loop_id = loops.innermost_loop_of(then).unwrap();
let header = loops.header_of(loop_id); // 1 - the block header
let blocks = loops.body_of(loop_id); // [1, 2, 3, 4] - header, body, then and nextThe 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..loop_count(), 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 (Send and Sync).
Memory: 4 x node_count bytes, plus 4 bytes per node in a loop, 24 bytes per loop and 4 bytes per entry of a loop.
Implementations§
Source§impl Loops
impl Loops
Sourcepub fn of(graph: &BiAdjacentGraph, entry: u32) -> Loops
pub fn of(graph: &BiAdjacentGraph, entry: u32) -> Loops
Returns the loop nesting forest of graph with one entry. Equivalent to
of_entries(graph, &[entry]) - Java’s overload of(BiAdjacentGraph, int).
Time: as for of_entries.
Memory: as for of_entries.
§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.
Sourcepub fn of_entries(graph: &BiAdjacentGraph, entries: &[u32]) -> Loops
pub fn of_entries(graph: &BiAdjacentGraph, entries: &[u32]) -> Loops
Returns the loop nesting forest of graph with the given entries - Java’s overload
of(BiAdjacentGraph, IntList).
The entries work as in Dominators::of_entries: 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.len() + (node_count + edge_count) log node_count) 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 type documentation; while it runs, about 45 x node_count + 4 x edge_count bytes more, plus 28 bytes per loop and 12 bytes per entry of a loop.
§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.
Sourcepub fn loop_count(&self) -> u32
pub fn loop_count(&self) -> u32
Returns the number of loops.
Time: O(1).
Sourcepub fn is_reducible(&self) -> bool
pub fn is_reducible(&self) -> bool
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).
Sourcepub fn innermost_loop_of(&self, node: u32) -> Option<u32>
pub fn innermost_loop_of(&self, node: u32) -> Option<u32>
Returns the innermost loop that contains node, or None if it lies in no loop - Java’s -1.
Time: O(1).
§Panics
With an index out of bounds message if node is not in 0..node_count.
Sourcepub fn loop_depth_of(&self, node: u32) -> u32
pub fn loop_depth_of(&self, node: u32) -> u32
Returns the loop depth of node: the number of loops that contain it. A node in no loop has loop
depth 0.
Time: O(1).
§Panics
With an index out of bounds message if node is not in 0..node_count.
Sourcepub fn is_in_loop(&self, node: u32, loop_id: u32) -> bool
pub fn is_in_loop(&self, node: u32, loop_id: u32) -> bool
Returns whether node lies in loop loop_id, directly or in a loop nested in it. The parameter is
Java’s loop, which is a keyword in Rust.
Time: O(1).
§Panics
With an index out of bounds message if node is not in 0..node_count or loop_id not in
0..loop_count().
Sourcepub fn header_of(&self, loop_id: u32) -> u32
pub fn header_of(&self, loop_id: u32) -> u32
Returns the header of loop loop_id.
Time: O(1).
§Panics
With an index out of bounds message if loop_id is not in 0..loop_count().
Sourcepub fn entries_of(&self, loop_id: u32) -> SortedIntView<'_>
pub fn entries_of(&self, loop_id: u32) -> SortedIntView<'_>
Returns the entries of loop loop_id, 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(entries_of(loop_id).len()).
§Panics
With an index out of bounds message if loop_id is not in 0..loop_count().
Sourcepub fn parent_of(&self, loop_id: u32) -> Option<u32>
pub fn parent_of(&self, loop_id: u32) -> Option<u32>
Returns the loop that immediately encloses loop loop_id, or None for an outermost loop - Java’s
-1.
Time: O(1).
§Panics
With an index out of bounds message if loop_id is not in 0..loop_count().
Sourcepub fn body_of(&self, loop_id: u32) -> &[u32]
pub fn body_of(&self, loop_id: u32) -> &[u32]
Returns the body of loop loop_id: 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_id, then the nodes of the nested loops in the same form, loop after loop
in ID order.
The result is a view of this forest’s own array, not a copy, and cannot be written - Java returns
an unmodifiable IntList view. All bodies share the array, so a node is stored once, however
deeply its loop is nested.
let loops = Loops::of(&BiAdjacentGraph::of(&b.build()), 0);
loops.body_of(0)[0] = 1; // a shared slice cannot be writtenTime: O(1).
§Panics
With an index out of bounds message if loop_id is not in 0..loop_count().