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