blob: 9eac6d71a6df523f9a75e7efab6edbb2db316e54 [file] [log] [blame]
Philip Reames58b47872019-07-24 23:24:13 +00001===========================================
2LLVM Loop Terminology (and Canonical Forms)
3===========================================
4
5.. contents::
6 :local:
7
8Introduction
9============
10
11Loops are a core concept in any optimizer. This page spells out some
12of the common terminology used within LLVM code to describe loop
13structures.
14
15First, let's start with the basics. In LLVM, a Loop is a cycle within
16the control flow graph (CFG) where there exists one block (the loop
17header block) which dominates all other blocks within the cycle.
18
19Note that there are some important implications of this definition:
20
21* Not all cycles are loops. There exist cycles that do not meet the
22 dominance requirement and such are not considered loops. LoopInfo
23 does not include such cycles.
24
25* Loops can contain non-loop cycles and non-loop cycles may contain
26 loops. Loops may also contain sub-loops.
27
28* Given the use of dominance in the definition, all loops are
29 statically reachable from the entry of the function. Loops which
30 become statically unreachable during optimization *must* be removed
31 from LoopInfo.
32
33* Every loop must have a header block, and some set of predecessors
34 outside the loop. A loop is allowed to be statically infinite, so
35 there need not be any exiting edges.
36
37* Any two loops are either fully disjoint (no intersecting blocks), or
38 one must be a sub-loop of the other.
39
40A loop may have an arbitrary number of exits, both explicit (via
41control flow) and implicit (via throwing calls which transfer control
42out of the containing function). There is no special requirement on
43the form or structure of exit blocks (the block outside the loop which
44is branched to). They may have multiple predecessors, phis, etc...
45
46Key Terminology
47===============
48
49Header Block - The basic block which dominates all other blocks
50contained within the loop. As such, it is the first one executed if
51the loop executes at all. Note that a block can be the header of
52two separate loops at the same time, but only if one is a sub-loop
53of the other.
54
55Exiting Block - A basic block contained within a given loop which has
56at least one successor outside of the loop and one successor inside the
57loop. (The latter is required for the block to be contained within the
58cycle which makes up the loop.) That is, it has a successor which is
59an Exit Block.
60
61Exit Block - A basic block outside of the associated loop which has a
62predecessor inside the loop. That is, it has a predecessor which is
63an Exiting Block.
64
65Latch Block - A basic block within the loop whose successors include
66the header block of the loop. Thus, a latch is a source of backedge.
67A loop may have multiple latch blocks. A latch block may be either
68conditional or unconditional.
69
70Backedge(s) - The edge(s) in the CFG from latch blocks to the header
71block. Note that there can be multiple such edges, and even multiple
72such edges leaving a single latch block.
73
74Loop Predecessor - The predecessor blocks of the loop header which
75are not contained by the loop itself. These are the only blocks
76through which execution can enter the loop. When used in the
77singular form implies that there is only one such unique block.
78
79Preheader Block - A preheader is a (singular) loop predecessor which
80ends in an unconditional transfer of control to the loop header. Note
81that not all loops have such blocks.
82
83Backedge Taken Count - The number of times the backedge will have
84executed before some interesting event happens. Commonly used without
85qualification of the event as a shorthand for when some exiting block
86branches to some exit block. May be zero, or not statically computable.
87
88Iteration Count - The number of times the header has executed before
89some interesting event happens. Commonly used w/o qualification to
90refer to the iteration count at which the loop exits. Will always be
91one greater than the backedge taken count. (Warning: Preceding
92statement is true in the *integer domain*; if you're dealing with fixed
93width integers (such as LLVM Values or SCEVs), you need to be cautious
94of overflow when converting one to the other.)
95
96Loop Simplify Form
97==================
98
99TBD
100
101
102Loop Closed SSA (LCSSA)
103=======================
104
105TBD
106
107"More Canonical" Loops
108======================
109
110TBD