graphity_controlflow/lib.rs
1//! Analyses of control flow: loops, dominance frontiers, control dependence and the placement of
2//! phi-nodes.
3//!
4//! This crate is the counterpart of the Java module `graphity-controlflow`, package
5//! `com.kobayami.graphity.controlflow`, released with `graphity-core` at the same version. Its
6//! analyses work on a control-flow graph, with the basic blocks of a method as nodes and an edge
7//! wherever control can pass from one block to another:
8//!
9//! - [`Loops`] - the loop nesting forest: every loop with its header, its body and its entries, and
10//! how the loops nest, irreducible loops included.
11//! - [`IteratedDominanceFrontier`] - the blocks where SSA (static single assignment) form places the
12//! phi-nodes of a variable.
13//! - [`control_flow`] - the dominance frontiers as a graph, and from them control dependence: which
14//! block decides whether another one runs.
15//!
16//! The analyses follow edges against their direction, so they take a
17//! [`BiAdjacentGraph`](graphity_core::BiAdjacentGraph), or the
18//! [`Dominators`](graphity_core::Dominators) computed on one; build it once and pass it to all of
19//! them. Here, the loop of a method `load` around its block `then`, from its control flow `cfg` and
20//! its entry block `entry`:
21//!
22//! <!-- example: analyzing_control_flow.rs#loops -->
23//! ```
24//! # use graphity_core::BiAdjacentGraph;
25//! # use graphity_controlflow::Loops;
26//! # let cfg = graphity_core::Graph::of(&[0, 1, 3, 5, 6, 7, 7], &[1, 2, 5, 3, 4, 4, 1]).unwrap();
27//! # let (entry, then) = (0, 3);
28//! // The basic blocks of load are the nodes 0 to 5 of cfg: entry, header, body, then,
29//! // next, exit.
30//!
31//! // Find the loops of load, from its entry block.
32//! let loops = Loops::of(&BiAdjacentGraph::of(&cfg), entry);
33//!
34//! // Find the loop around the block then, with its header and its body.
35//!
36//! // 0 - the for loop, the only loop
37//! let loop_id = loops.innermost_loop_of(then).unwrap();
38//! let header = loops.header_of(loop_id); // 1 - the block header
39//! let blocks = loops.body_of(loop_id); // [1, 2, 3, 4] - header, body, then and next
40//! # assert_eq!(loops.loop_count(), 1);
41//! # assert_eq!((loop_id, header), (0, 1));
42//! # assert_eq!(blocks, [1, 2, 3, 4]);
43//! ```
44//!
45//! The crate reads the graph through [`graphity_core::csr`], like the algorithms of the core, and
46//! uses nothing but the public API of `graphity-core`, as the Java module uses nothing but the
47//! exported package of the core.
48//!
49//! # Errors and panics
50//!
51//! The conventions of the core apply (see [`graphity_core::error`]). Where Java returns `-1` for
52//! "none", a single lookup returns `Option<u32>`. A node, loop or entry out of its range panics with a
53//! message that starts with `index out of bounds: `. Any other broken contract, such as a graph above
54//! the node limit or a result above the largest array, panics with a message that starts with
55//! `invalid argument: `.
56
57pub mod control_flow;
58mod dfs_preorder;
59mod error;
60mod growth;
61mod int_hash_set;
62mod iterated_dominance_frontier;
63mod loops;
64
65pub use iterated_dominance_frontier::IteratedDominanceFrontier;
66pub use loops::Loops;