Skip to main content

Loops

Struct Loops 

Source
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 next

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

Source

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.

Source

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.

Source

pub fn loop_count(&self) -> u32

Returns the number of loops.

Time: O(1).

Source

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

Source

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.

Source

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.

Source

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

Source

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

Source

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

Source

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

Source

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 written

Time: O(1).

§Panics

With an index out of bounds message if loop_id is not in 0..loop_count().

Trait Implementations§

Source§

impl Clone for Loops

Source§

fn clone(&self) -> Loops

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Loops

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Loops

§

impl RefUnwindSafe for Loops

§

impl Send for Loops

§

impl Sync for Loops

§

impl Unpin for Loops

§

impl UnsafeUnpin for Loops

§

impl UnwindSafe for Loops

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.