LLVM Loop Terminology (and Canonical Forms)
Introduction
Loops are a core concept in any optimizer. This page spells out someof the common terminology used within LLVM code to describe loopstructures.
First, let’s start with the basics. In LLVM, a Loop is a maximal set of basicblocks that form a strongly connected component (SCC) in the ControlFlow Graph (CFG) where there exists a dedicated entry/header block thatdominates all other blocks within the loop. Thus, without leaving theloop, one can reach every block in the loop from the header block andthe header block from every block in the loop.
Note that there are some important implications of this definition:
- Not all SCCs are loops. There exist SCCs that do not meet thedominance requirement and such are not considered loops.
- Loops can contain non-loop SCCs and non-loop SCCs may containloops. Loops may also contain sub-loops.
- A header block is uniquely associated with one loop. There can bemultiple SCC within that loop, but the strongly connected component(SCC) formed from their union must always be unique.
- Given the use of dominance in the definition, all loops arestatically reachable from the entry of the function.
- Every loop must have a header block, and some set of predecessorsoutside the loop. A loop is allowed to be statically infinite, sothere need not be any exiting edges.
- Any two loops are either fully disjoint (no intersecting blocks), orone must be a sub-loop of the other.
- Loops in a function form a forest. One implication of this factis that a loop either has no parent or a single parent.
A loop may have an arbitrary number of exits, both explicit (viacontrol flow) and implicit (via throwing calls which transfer controlout of the containing function). There is no special requirement onthe form or structure of exit blocks (the block outside the loop whichis branched to). They may have multiple predecessors, phis, etc…
Key Terminology
Header Block - The basic block which dominates all other blockscontained within the loop. As such, it is the first one executed ifthe loop executes at all. Note that a block can be the header oftwo separate loops at the same time, but only if one is a sub-loopof the other.
Exiting Block - A basic block contained within a given loop which hasat least one successor outside of the loop and one successor inside theloop. (The latter is a consequence of the block being contained withinan SCC which is part of the loop.) That is, it has a successor whichis an Exit Block.
Exit Block - A basic block outside of the associated loop which has apredecessor inside the loop. That is, it has a predecessor which isan Exiting Block.
Latch Block - A basic block within the loop whose successors includethe header block of the loop. Thus, a latch is a source of backedge.A loop may have multiple latch blocks. A latch block may be eitherconditional or unconditional.
Backedge(s) - The edge(s) in the CFG from latch blocks to the headerblock. Note that there can be multiple such edges, and even multiplesuch edges leaving a single latch block.
Loop Predecessor - The predecessor blocks of the loop header whichare not contained by the loop itself. These are the only blocksthrough which execution can enter the loop. When used in thesingular form implies that there is only one such unique block.
Preheader Block - A preheader is a (singular) loop predecessor whichends in an unconditional transfer of control to the loop header. Notethat not all loops have such blocks.
Backedge Taken Count - The number of times the backedge will executebefore some interesting event happens. Commonly used withoutqualification of the event as a shorthand for when some exiting blockbranches to some exit block. May be zero, or not statically computable.
Iteration Count - The number of times the header will execute beforesome interesting event happens. Commonly used without qualification torefer to the iteration count at which the loop exits. Will always beone greater than the backedge taken count. Warning: Precedingstatement is true in the integer domain; if you’re dealing with fixedwidth integers (such as LLVM Values or SCEVs), you need to be cautiousof overflow when converting one to the other.
It’s important to note that the same basic block can play multipleroles in the same loop, or in different loops at once. For example, asingle block can be the header for two nested loops at once, whilealso being an exiting block for the inner one only, and an exit blockfor a sibling loop. Example:
- while (..) {
- for (..) {}
- do {
- do {
- // <-- block of interest
- if (exit) break;
- } while (..);
- } while (..)
- }
LoopInfo
LoopInfo is the core analysis for obtaining information about loops.There are few key implications of the definitions given above whichare important for working successfully with this interface.
- LoopInfo does not contain information about non-loop cycles. As aresult, it is not suitable for any algorithm which requires completecycle detection for correctness.
- LoopInfo provides an interface for enumerating all top level loops(e.g. those not contained in any other loop). From there, you maywalk the tree of sub-loops rooted in that top level loop.
- Loops which become statically unreachable during optimization must_be removed from LoopInfo. If this can not be done for some reason,then the optimization is _required to preserve the staticreachability of the loop.
Loop Simplify Form
The Loop Simplify Form is a canonical form that makesseveral analyses and transformations simpler and more effective.It is ensured by the LoopSimplify(-loop-simplify) pass and is automaticallyadded by the pass managers when scheduling a LoopPass.This pass is implemented inLoopSimplify.h.When it is successful, the loop has:
- A preheader.
- A single backedge (which implies that there is a single latch).
- Dedicated exits. That is, no exit block for the loophas a predecessor that is outside the loop. This impliesthat all exit blocks are dominated by the loop header.
Loop Closed SSA (LCSSA)
TBD
“More Canonical” Loops
Rotated Loops
Loops are rotated by the LoopRotate (loop-rotate)pass, which converts loops into do/while style loops and isimplemented inLoopRotation.h. Example:
- void test(int n) {
- for (int i = 0; i < n; i += 1)
- // Loop body
- }
is transformed to:
- void test(int n) {
- int i = 0;
- do {
- // Loop body
- i += 1;
- } while (i < n);
- }
Warning: This transformation is valid only if the compilercan prove that the loop body will be executed at least once. Otherwise,it has to insert a guard which will test it at runtime. In the exampleabove, that would be:
- void test(int n) {
- int i = 0;
- if (n > 0) {
- do {
- // Loop body
- i += 1;
- } while (i < n);
- }
- }
It’s important to understand the effect of loop rotationat the LLVM IR level. We follow with the previous examplesin LLVM IR while also providing a graphical representationof the control-flow graphs (CFG). You can get the same graphicalresults by utilizing the view-cfg pass.
The initial for loop could be translated to:
- define void @test(i32 %n) {
- entry:
- br label %for.header
- for.header:
- %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
- %cond = icmp slt i32 %i, %n
- br i1 %cond, label %body, label %exit
- body:
- ; Loop body
- br label %latch
- latch:
- %i.next = add nsw i32 %i, 1
- br label %for.header
- exit:
- ret void
- }
Before we explain how LoopRotate will actuallytransform this loop, here’s how we could convertit (by hand) to a do-while style loop.
- define void @test(i32 %n) {
- entry:
- br label %body
- body:
- %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
- ; Loop body
- br label %latch
- latch:
- %i.next = add nsw i32 %i, 1
- %cond = icmp slt i32 %i.next, %n
- br i1 %cond, label %body, label %exit
- exit:
- ret void
- }
- The condition check was moved to the “bottom” of the loop, i.e.the latch. This is something that LoopRotate does by copying the headerof the loop to the latch.
- The compiler in this case can’t deduce that the loop willdefinitely execute at least once so the above transformationis not valid. As mentioned above, a guard has to be inserted,which is something that LoopRotate will do.
This is how LoopRotate transforms this loop:
- define void @test(i32 %n) {
- entry:
- %guard_cond = icmp slt i32 0, %n
- br i1 %guard_cond, label %loop.preheader, label %exit
- loop.preheader:
- br label %body
- body:
- %i2 = phi i32 [ 0, %loop.preheader ], [ %i.next, %latch ]
- br label %latch
- latch:
- %i.next = add nsw i32 %i2, 1
- %cond = icmp slt i32 %i.next, %n
- br i1 %cond, label %body, label %loop.exit
- loop.exit:
- br label %exit
- exit:
- ret void
- }
The result is a little bit more complicated than we may expectbecause LoopRotate ensures that the loop is inLoop Simplify Formafter rotation.In this case, it inserted the %loop.preheader basic block sothat the loop has a preheader and it introduced the %loop.exitbasic block so that the loop has dedicated exits(otherwise, %exit would be jumped from both %latch and %entry,but %entry is not contained in the loop).Note that a loop has to be in Loop Simplify Form beforehandtoo for LoopRotate to be applied successfully.
The main advantage of this form is that it allows hoistinginvariant instructions, especially loads, into the preheader.That could be done in non-rotated loops as well but withsome disadvantages. Let’s illustrate them with an example:
- for (int i = 0; i < n; ++i) {
- auto v = *p;
- use(v);
- }
We assume that loading from p is invariant and use(v) is somestatement that uses v.If we wanted to execute the load only once we could move it“out” of the loop body, resulting in this:
- auto v = *p;
- for (int i = 0; i < n; ++i) {
- use(v);
- }
However, now, in the case that n <= 0, in the initial form,the loop body would never execute, and so, the load wouldnever execute. This is a problem mainly for semantic reasons.Consider the case in which n <= 0 and loading from p is invalid.In the initial program there would be no error. However, with thistransformation we would introduce one, effectively breakingthe initial semantics.
To avoid both of these problems, we can insert a guard:
- if (n > 0) { // loop guard
- auto v = *p;
- for (int i = 0; i < n; ++i) {
- use(v);
- }
- }
This is certainly better but it could be improved slightly. Noticethat the check for whether n is bigger than 0 is executed twice (andn does not change in between). Once when we check the guard conditionand once in the first execution of the loop. To avoid that, we coulddo an unconditional first execution and insert the loop conditionin the end. This effectively means transforming the loop into a do-while loop:
- if (0 < n) {
- auto v = *p;
- do {
- use(v);
- ++i;
- } while (i < n);
- }
Note that LoopRotate does not generally do suchhoisting. Rather, it is an enabling transformation for otherpasses like Loop-Invariant Code Motion (-licm).