Skip to content

Commit c3f12dd

Browse files
committed
[docs] Revise loop terminology reference.
Motivated by D88183, this seeks to clarify the current loop nomenclature with added illustrations, examples for possibly unexpected situations (infinite loops not part of the "parent" loop, logical loops sharing the same header, ...), and clarification on what other sources may consider a loop. The current document also has multiple errors that are fixed here. Some selected errors: * Loops a defined as strongly-connected components. A component a partition of all nodes, i.e. a subloop can never be a component. That is, the document as it currently is only covers top-level loops, even it also uses the term SCC for subloops. * "a block can be the header of two separate loops at the same time" (it is considered a single loop by LoopInfo) * "execute before some interesting event happens" (some interesting event is not well-defined) Reviewed By: baziotis, Whitney Differential Revision: https://reviews.llvm.org/D88408
1 parent 89e8a8b commit c3f12dd

10 files changed

+8024
-96
lines changed

llvm/docs/LangRef.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20066,6 +20066,8 @@ not overflow at link time under the medium code model if ``x`` is an
2006620066
a constant initializer folded into a function body. This intrinsic can be
2006720067
used to avoid the possibility of overflows when loading from such a constant.
2006820068

20069+
.. _llvm_sideeffect:
20070+
2006920071
'``llvm.sideeffect``' Intrinsic
2007020072
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2007120073

llvm/docs/LoopTerminology.rst

Lines changed: 218 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,119 +7,241 @@ LLVM Loop Terminology (and Canonical Forms)
77
.. contents::
88
:local:
99

10-
Introduction
11-
============
10+
Loop Definition
11+
===============
1212

13-
Loops are a core concept in any optimizer. This page spells out some
14-
of the common terminology used within LLVM code to describe loop
15-
structures.
13+
Loops are an important concept for a code optimizer. In LLVM, detection
14+
of loops in a control-flow graph is done by :ref:`loopinfo`. It is based
15+
on the following definition.
1616

17-
First, let's start with the basics. In LLVM, a Loop is a maximal set of basic
18-
blocks that form a strongly connected component (SCC) in the Control
19-
Flow Graph (CFG) where there exists a dedicated entry/header block that
20-
dominates all other blocks within the loop. Thus, without leaving the
21-
loop, one can reach every block in the loop from the header block and
22-
the header block from every block in the loop.
17+
A loop is a subset of nodes from the control-flow graph (CFG; where
18+
nodes represent basic blocks) with the following properties:
2319

24-
Note that there are some important implications of this definition:
20+
1. The induced subgraph (which is the subgraph that contains all the
21+
edges from the CFG within the loop) is strongly connected
22+
(every node is reachable from all others).
2523

26-
* Not all SCCs are loops. There exist SCCs that do not meet the
27-
dominance requirement and such are not considered loops.
24+
2. All edges from outside the subset into the subset point to the same
25+
node, called the **header**. As a consequence, the header dominates
26+
all nodes in the loop (i.e. every execution path to any of the loop's
27+
node will have to pass through the header).
2828

29-
* Loops can contain non-loop SCCs and non-loop SCCs may contain
30-
loops. Loops may also contain sub-loops.
29+
3. The loop is the maximum subset with these properties. That is, no
30+
additional nodes from the CFG can be added such that the induced
31+
subgraph would still be strongly connected and the header would
32+
remain the same.
3133

32-
* A header block is uniquely associated with one loop. There can be
33-
multiple SCC within that loop, but the strongly connected component
34-
(SCC) formed from their union must always be unique.
34+
In computer science literature, this is often called a *natural loop*.
35+
In LLVM, this is the only definition of a loop.
3536

36-
* Given the use of dominance in the definition, all loops are
37-
statically reachable from the entry of the function.
3837

39-
* Every loop must have a header block, and some set of predecessors
40-
outside the loop. A loop is allowed to be statically infinite, so
41-
there need not be any exiting edges.
38+
Terminology
39+
-----------
4240

43-
* Any two loops are either fully disjoint (no intersecting blocks), or
44-
one must be a sub-loop of the other.
41+
The definition of a loop comes with some additional terminology:
4542

46-
* Loops in a function form a forest. One implication of this fact
47-
is that a loop either has no parent or a single parent.
43+
* An **entering block** (or **loop predecessor**) is a non-loop node
44+
that has an edge into the loop (necessarily the header). If there is
45+
only one entering block entering block, and its only edge is to the
46+
header, it is also called the loop's **preheader**. The preheader
47+
dominates the loop without itself being part of the loop.
4848

49-
A loop may have an arbitrary number of exits, both explicit (via
50-
control flow) and implicit (via throwing calls which transfer control
51-
out of the containing function). There is no special requirement on
52-
the form or structure of exit blocks (the block outside the loop which
53-
is branched to). They may have multiple predecessors, phis, etc...
49+
* A **latch** is a loop node that has an edge to the header.
5450

55-
Key Terminology
56-
===============
51+
* A **backedge** is an edge from a latch to the header.
52+
53+
* An **exiting edge** is an edge from inside the loop to a node outside
54+
of the loop. The source of such an edge is called an **exiting block**, its
55+
target is an **exit block**.
56+
57+
.. image:: ./loop-terminology.svg
58+
:width: 400 px
59+
60+
61+
Important Notes
62+
---------------
63+
64+
This loop definition has some noteworthy consequences:
65+
66+
* A node can be the header of at most one loop. As such, a loop can be
67+
identified by its header. Due to the header being the only entry into
68+
a loop, it can be called a Single-Entry-Multiple-Exits (SEME) region.
69+
70+
71+
* For basic blocks that are not reachable from the function's entry, the
72+
concept of loops is undefined. This follows from the concept of
73+
dominance being undefined as well.
74+
75+
76+
* The smallest loop consists of a single basic block that branches to
77+
itself. In this case that block is the header, latch (and exiting
78+
block if it has another edge to a different block) at the same time.
79+
A single block that has no branch to itself is not considered a loop,
80+
even though it is trivially strongly connected.
81+
82+
.. image:: ./loop-single.svg
83+
:width: 300 px
84+
85+
In this case, the role of header, exiting block and latch fall to the
86+
same node. :ref:`loopinfo` reports this as:
87+
88+
.. code-block:: console
89+
90+
$ opt input.ll -loops -analyze
91+
Loop at depth 1 containing: %for.body<header><latch><exiting>
5792
58-
**Header Block** - The basic block which dominates all other blocks
59-
contained within the loop. As such, it is the first one executed if
60-
the loop executes at all. Note that a block can be the header of
61-
two separate loops at the same time, but only if one is a sub-loop
62-
of the other.
63-
64-
**Exiting Block** - A basic block contained within a given loop which has
65-
at least one successor outside of the loop and one successor inside the
66-
loop. (The latter is a consequence of the block being contained within
67-
an SCC which is part of the loop.) That is, it has a successor which
68-
is an Exit Block.
69-
70-
**Exit Block** - A basic block outside of the associated loop which has a
71-
predecessor inside the loop. That is, it has a predecessor which is
72-
an Exiting Block.
73-
74-
**Latch Block** - A basic block within the loop whose successors include
75-
the header block of the loop. Thus, a latch is a source of backedge.
76-
A loop may have multiple latch blocks. A latch block may be either
77-
conditional or unconditional.
78-
79-
**Backedge(s)** - The edge(s) in the CFG from latch blocks to the header
80-
block. Note that there can be multiple such edges, and even multiple
81-
such edges leaving a single latch block.
82-
83-
**Loop Predecessor** - The predecessor blocks of the loop header which
84-
are not contained by the loop itself. These are the only blocks
85-
through which execution can enter the loop. When used in the
86-
singular form implies that there is only one such unique block.
87-
88-
**Preheader Block** - A preheader is a (singular) loop predecessor which
89-
ends in an unconditional transfer of control to the loop header. Note
90-
that not all loops have such blocks.
91-
92-
**Backedge Taken Count** - The number of times the backedge will execute
93-
before some interesting event happens. Commonly used without
94-
qualification of the event as a shorthand for when some exiting block
95-
branches to some exit block. May be zero, or not statically computable.
96-
97-
**Iteration Count** - The number of times the header will execute before
98-
some interesting event happens. Commonly used without qualification to
99-
refer to the iteration count at which the loop exits. Will always be
100-
one greater than the backedge taken count. *Warning*: Preceding
101-
statement is true in the *integer domain*; if you're dealing with fixed
102-
width integers (such as LLVM Values or SCEVs), you need to be cautious
103-
of overflow when converting one to the other.
104-
105-
It's important to note that the same basic block can play multiple
106-
roles in the same loop, or in different loops at once. For example, a
107-
single block can be the header for two nested loops at once, while
108-
also being an exiting block for the inner one only, and an exit block
109-
for a sibling loop. Example:
93+
94+
* Loops can be nested inside each other. That is, a loop's node set can
95+
be a subset of another loop with a different loop header. The loop
96+
hierarchy in a function forms a forest: Each top-level loop is the
97+
root of the tree of the loops nested inside it.
98+
99+
.. image:: ./loop-nested.svg
100+
:width: 350 px
101+
102+
103+
* It is not possible that two loops share only a few of their nodes.
104+
Two loops are either disjoint or one is nested inside the other. In
105+
the example below the left and right subsets both violate the
106+
maximality condition. Only the merge of both sets is considered a loop.
107+
108+
.. image:: ./loop-nonmaximal.svg
109+
:width: 250 px
110+
111+
112+
* It is also possible that two logical loops share a header, but are
113+
considered a single loop by LLVM:
110114

111115
.. code-block:: C
112116
113-
while (..) {
114-
for (..) {}
115-
do {
116-
do {
117-
// <-- block of interest
118-
if (exit) break;
119-
} while (..);
120-
} while (..)
117+
for (int i = 0; i < 128; ++i)
118+
for (int j = 0; j < 128; ++j)
119+
body(i,j);
120+
121+
which might be represented in LLVM-IR as follows. Note that there is
122+
only a single header and hence just a single loop.
123+
124+
.. image:: ./loop-merge.svg
125+
:width: 400 px
126+
127+
The :ref:`LoopSimplify <loop-terminology-loop-simplify>` pass will
128+
detect the loop and ensure separate headers for the outer and inner loop.
129+
130+
.. image:: ./loop-separate.svg
131+
:width: 400 px
132+
133+
* A cycle in the CFG does not imply there is a loop. The example below
134+
shows such a CFG, where there is no header node that dominates all
135+
other nodes in the cycle. This is called **irreducible control-flow**.
136+
137+
.. image:: ./loop-irreducible.svg
138+
:width: 150 px
139+
140+
The term reducible results from the ability to collapse the CFG into a
141+
single node by successively replacing one of three base structures with
142+
a single node: A sequential execution of basic blocks, a conditional
143+
branching (or switch) with re-joining, and a basic block looping on itself.
144+
`Uncyclopedia <https://en.wikipedia.org/wiki/Control-flow_graph#Reducibility>`_
145+
has a more formal definition, which basically says that every cycle has
146+
a dominating header.
147+
148+
149+
* Irreducible control-flow can occur at any level of the loop nesting.
150+
That is, a loop that itself does not contain any loops can still have
151+
cyclic control flow in its body; a loop that is not nested inside
152+
another loop can still be part of an outer cycle; and there can be
153+
additional cycles between any two loops where one is contained in the other.
154+
155+
156+
* Exiting edges are not the only way to break out of a loop. Other
157+
possibilities are unreachable terminators, [[noreturn]] functions,
158+
exceptions, signals, and your computer's power button.
159+
160+
161+
* A basic block "inside" the loop that does not have a path back to the
162+
loop (i.e. to a latch or header) is not considered part of the loop.
163+
This is illustrated by the following code.
164+
165+
.. code-block:: C
166+
167+
for (unsigned i = 0; i <= n; ++i) {
168+
if (c1) {
169+
// When reaching this block, we will have exited the loop.
170+
do_something();
171+
break;
172+
}
173+
if (c2) {
174+
// abort(), never returns, so we have exited the loop.
175+
abort();
176+
}
177+
if (c3) {
178+
// The unreachable allows the compiler to assume that this will not rejoin the loop.
179+
do_something();
180+
__builtin_unreachable();
181+
}
182+
if (c4) {
183+
// This statically infinite loop is not nested because control-flow will not continue with the for-loop.
184+
while(true) {
185+
do_something();
186+
}
187+
}
121188
}
122189
190+
191+
* There is no requirement for the control flow to eventually leave the
192+
loop, i.e. a loop can be infinite. A **statically infinite loop** is a
193+
loop that has no exiting edges. A **dynamically infinite loop** has
194+
exiting edges, but it is possible to be never taken. This may happen
195+
only under some circumstances, such as when n == UINT_MAX in the code
196+
below.
197+
198+
.. code-block:: C
199+
200+
for (unsigned i = 0; i <= n; ++i)
201+
body(i);
202+
203+
It is possible for the optimizer to turn a dynamically infinite loop
204+
into a statically infinite loop, for instance when it can prove that the
205+
exiting condition is always false. Because the exiting edge is never
206+
taken, the optimizer can change the conditional branch into an
207+
unconditional one.
208+
209+
Note that under some circumstances the compiler may assume that a loop will
210+
eventually terminate without proving it. For instance, it may remove a loop
211+
that does not do anything in its body. If the loop was infinite, this
212+
optimization resulted in an "infinite" performance speed-up. A call
213+
to the intrinsic :ref:`llvm.sideeffect<llvm_sideeffect>` can be added
214+
into the loop to ensure that the optimizer does not make this assumption
215+
without proof.
216+
217+
218+
* The number of executions of the loop header before leaving the loop is
219+
the **loop trip count** (or **iteration count**). If the loop should
220+
not be executed at all, a **loop guard** must skip the entire loop:
221+
222+
.. image:: ./loop-guard.svg
223+
:width: 500 px
224+
225+
Since the first thing a loop header might do is to check whether there
226+
is another execution and if not, immediately exit without doing any work
227+
(also see :ref:`loop-terminology-loop-rotate`), loop trip count is not
228+
the best measure of a loop's number of iterations. For instance, the
229+
number of header executions of the code below for a non-positive n
230+
(before loop rotation) is 1, even though the loop body is not executed
231+
at all.
232+
233+
.. code-block:: C
234+
235+
for (int i = 0; i < n; ++i)
236+
body(i);
237+
238+
A better measure is the **backedge-taken count**, which is the number of
239+
times any of the backedges is taken before the loop. It is one less than
240+
the trip count for executions that enter the header.
241+
242+
243+
.. _loopinfo:
244+
123245
LoopInfo
124246
========
125247

@@ -139,7 +261,7 @@ are important for working successfully with this interface.
139261
be removed from LoopInfo. If this can not be done for some reason,
140262
then the optimization is *required* to preserve the static
141263
reachability of the loop.
142-
264+
143265

144266
.. _loop-terminology-loop-simplify:
145267

0 commit comments

Comments
 (0)