8.2 Find loops that can be entered in more than one place
You find the loops of a control-flow graph that can be entered at more than one block, which are called irreducible, and see how Graphity reports them.
Problem
Section titled “Problem”You want to know which loops of a function can be entered in more than one place. A loop written
with for or while is entered through its header and through no other block. Code can also jump
into the middle of a loop. A block at which execution can enter a loop from outside it is an
entry of the loop. A loop with more than one entry is irreducible.
Many optimizations assume that a loop has a single entry, so an analysis has to know which loops
have several, and which blocks they are. Irreducible loops come from:
- code with
goto, written by hand or generated; - bytecode that a tool changed after compiling, for example to optimize or to obfuscate it;
- state machines generated as code, with a jump for every transition.
Example
Section titled “Example”The method parseArgsparseArgsparseArgsParseArgs of a small program is
generated from a state machine. Its states option and value jump to each other. The method
starts in either of them, depending on the first argument:
entry: if (isOption(args[0])) goto option; else goto value;option: read an option; if (done) goto exit; else goto value;value: read a value; if (done) goto exit; else goto option;exit: return the settings;The graph cfg holds its control flow. This control-flow graph has a node for each
basic block and an edge from each block to each block that can run
right after it. Which loops does it have, and through which blocks can they be entered?
- Entry block
- Edge on a cycle
- Loop
- Loop entry
The picture after shows the one loop that Graphity finds, with the blocks option and value. Both
are entries of the loop, and option is also its header. The orange edges lie on the
cycle of the loop.
Solution
Section titled “Solution”Loops loops = Loops.of(BiAdjacentGraph.of(cfg), entry);boolean reducible = loops.isReducible(); // false// 1 - option and value, entered at bothint loopCount = loops.loopCount();SortedIntView entries = loops.entriesOf(0); // [1, 2] - option and value// 1 - option, reached first by the searchint header = loops.headerOf(0);let loops = Loops::of(&BiAdjacentGraph::of(&cfg), entry);let reducible = loops.is_reducible(); // falselet loop_count = loops.loop_count(); // 1 - option and value, entered at bothlet entries = loops.entries_of(0); // [1, 2] - option and valuelet header = loops.header_of(0); // 1 - option, reached first by the searchconst loops = Loops.of(BiAdjacentGraph.of(cfg), entry);const reducible = loops.isReducible; // falseconst loopCount = loops.loopCount; // 1 - option and value, entered at bothconst entries = loops.entriesOf(0); // [1, 2] - option and valueconst header = loops.headerOf(0); // 1 - option, reached first by the searchLoops loops = Loops.Of(BiAdjacentGraph.Of(cfg), entry);bool reducible = loops.IsReducible; // false// 1 - option and value, entered at bothint loopCount = loops.LoopCount;SortedIntView entries = loops.EntriesOf(0); // [1, 2] - option and value// 1 - option, reached first by the searchint header = loops.HeaderOf(0);Graphity reports the irreducible loop as one loop with two entries. Its header is the loop entry
that a depth-first search from the
entry block of the graph reaches first.
The search takes the out-nodes of each block in ascending
order, so here the header is option.
Good to know
Section titled “Good to know”- Which entry is the header depends on the order of the search, not on the code. With several
entry blocks of the function,
Loops.ofLoops::of_entriesLoops.ofLoops.Ofsearches from them in the order of the list. For an irreducible loop, rely onLoops.entriesOfLoops::entries_ofLoops.entriesOfLoops.EntriesOf, not on the header alone. - Making it reducible. Compilers duplicate code to turn an irreducible loop into a reducible one, or treat it as a unit and give up some optimizations inside it. Either way they start from the entries.
- Natural loops. A loop that can be entered only through its header is a
natural loop. Every loop written with
fororwhileis one. If no loop is irreducible, the loops that Graphity finds are the natural loops. See Find the loops of a function and how they nest for such loops.
API used
Section titled “API used”Loops.of(BiAdjacentGraph, int)Loops::ofLoops.of(graph: BiAdjacentGraph, entry: number)Loops.Of(BiAdjacentGraph, int)Loops.isReducible()Loops::is_reducibleLoops.isReducibleLoops.IsReducibleLoops.loopCount()Loops::loop_countLoops.loopCountLoops.LoopCountLoops.entriesOf(int)Loops::entries_ofLoops.entriesOfLoops.EntriesOfLoops.headerOf(int)Loops::header_ofLoops.headerOfLoops.HeaderOfLoops.of(BiAdjacentGraph, IntList)Loops::of_entriesLoops.of(graph: BiAdjacentGraph, entries: IntList)Loops.Of(BiAdjacentGraph, ReadOnlySpan<int>)BiAdjacentGraph.of(Graph)BiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Of