Skip to content

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.

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.

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?

The control flow and its entry block
Its loop, with two entries
1: option2: value0: entry3: exit
1: option2: value0: entry3: exitloop 0
Input
  • Entry block
Context
  • Edge on a cycle
Output
  • 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.

Loops loops = Loops.of(BiAdjacentGraph.of(cfg), entry);
boolean reducible = loops.isReducible(); // false
// 1 - option and value, entered at both
int loopCount = loops.loopCount();
SortedIntView entries = loops.entriesOf(0); // [1, 2] - option and value
// 1 - option, reached first by the search
int header = loops.headerOf(0);
Full example
let loops = Loops::of(&BiAdjacentGraph::of(&cfg), entry);
let reducible = loops.is_reducible(); // false
let loop_count = loops.loop_count(); // 1 - option and value, entered at both
let entries = loops.entries_of(0); // [1, 2] - option and value
let header = loops.header_of(0); // 1 - option, reached first by the search
Full example
const loops = Loops.of(BiAdjacentGraph.of(cfg), entry);
const reducible = loops.isReducible; // false
const loopCount = loops.loopCount; // 1 - option and value, entered at both
const entries = loops.entriesOf(0); // [1, 2] - option and value
const header = loops.headerOf(0); // 1 - option, reached first by the search
Full example
Loops loops = Loops.Of(BiAdjacentGraph.Of(cfg), entry);
bool reducible = loops.IsReducible; // false
// 1 - option and value, entered at both
int loopCount = loops.LoopCount;
SortedIntView entries = loops.EntriesOf(0); // [1, 2] - option and value
// 1 - option, reached first by the search
int header = loops.HeaderOf(0);
Full example

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.

  • 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.Of searches from them in the order of the list. For an irreducible loop, rely on Loops.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 for or while is 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.