LoopTerminology.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. .. _loop-terminology:
  2. ===========================================
  3. LLVM Loop Terminology (and Canonical Forms)
  4. ===========================================
  5. .. contents::
  6. :local:
  7. Introduction
  8. ============
  9. Loops are a core concept in any optimizer. This page spells out some
  10. of the common terminology used within LLVM code to describe loop
  11. structures.
  12. First, let's start with the basics. In LLVM, a Loop is a maximal set of basic
  13. blocks that form a strongly connected component (SCC) in the Control
  14. Flow Graph (CFG) where there exists a dedicated entry/header block that
  15. dominates all other blocks within the loop. Thus, without leaving the
  16. loop, one can reach every block in the loop from the header block and
  17. the header block from every block in the loop.
  18. Note that there are some important implications of this definition:
  19. * Not all SCCs are loops. There exist SCCs that do not meet the
  20. dominance requirement and such are not considered loops.
  21. * Loops can contain non-loop SCCs and non-loop SCCs may contain
  22. loops. Loops may also contain sub-loops.
  23. * A header block is uniquely associated with one loop. There can be
  24. multiple SCC within that loop, but the strongly connected component
  25. (SCC) formed from their union must always be unique.
  26. * Given the use of dominance in the definition, all loops are
  27. statically reachable from the entry of the function.
  28. * Every loop must have a header block, and some set of predecessors
  29. outside the loop. A loop is allowed to be statically infinite, so
  30. there need not be any exiting edges.
  31. * Any two loops are either fully disjoint (no intersecting blocks), or
  32. one must be a sub-loop of the other.
  33. A loop may have an arbitrary number of exits, both explicit (via
  34. control flow) and implicit (via throwing calls which transfer control
  35. out of the containing function). There is no special requirement on
  36. the form or structure of exit blocks (the block outside the loop which
  37. is branched to). They may have multiple predecessors, phis, etc...
  38. Key Terminology
  39. ===============
  40. Header Block - The basic block which dominates all other blocks
  41. contained within the loop. As such, it is the first one executed if
  42. the loop executes at all. Note that a block can be the header of
  43. two separate loops at the same time, but only if one is a sub-loop
  44. of the other.
  45. Exiting Block - A basic block contained within a given loop which has
  46. at least one successor outside of the loop and one successor inside the
  47. loop. (The latter is a consequence of the block being contained within
  48. an SCC which is part of the loop.) That is, it has a successor which
  49. is an Exit Block.
  50. Exit Block - A basic block outside of the associated loop which has a
  51. predecessor inside the loop. That is, it has a predecessor which is
  52. an Exiting Block.
  53. Latch Block - A basic block within the loop whose successors include
  54. the header block of the loop. Thus, a latch is a source of backedge.
  55. A loop may have multiple latch blocks. A latch block may be either
  56. conditional or unconditional.
  57. Backedge(s) - The edge(s) in the CFG from latch blocks to the header
  58. block. Note that there can be multiple such edges, and even multiple
  59. such edges leaving a single latch block.
  60. Loop Predecessor - The predecessor blocks of the loop header which
  61. are not contained by the loop itself. These are the only blocks
  62. through which execution can enter the loop. When used in the
  63. singular form implies that there is only one such unique block.
  64. Preheader Block - A preheader is a (singular) loop predecessor which
  65. ends in an unconditional transfer of control to the loop header. Note
  66. that not all loops have such blocks.
  67. Backedge Taken Count - The number of times the backedge will execute
  68. before some interesting event happens. Commonly used without
  69. qualification of the event as a shorthand for when some exiting block
  70. branches to some exit block. May be zero, or not statically computable.
  71. Iteration Count - The number of times the header will execute before
  72. some interesting event happens. Commonly used without qualification to
  73. refer to the iteration count at which the loop exits. Will always be
  74. one greater than the backedge taken count. *Warning*: Preceding
  75. statement is true in the *integer domain*; if you're dealing with fixed
  76. width integers (such as LLVM Values or SCEVs), you need to be cautious
  77. of overflow when converting one to the other.
  78. It's important to note that the same basic block can play multiple
  79. roles in the same loop, or in different loops at once. For example, a
  80. single block can be the header for two nested loops at once, while
  81. also being an exiting block for the inner one only, and an exit block
  82. for a sibling loop. Example:
  83. .. code-block:: C
  84. while (..) {
  85. for (..) {}
  86. do {
  87. do {
  88. // <-- block of interest
  89. if (exit) break;
  90. } while (..);
  91. } while (..)
  92. }
  93. LoopInfo
  94. ========
  95. LoopInfo is the core analysis for obtaining information about loops.
  96. There are few key implications of the definitions given above which
  97. are important for working successfully with this interface.
  98. * LoopInfo does not contain information about non-loop cycles. As a
  99. result, it is not suitable for any algorithm which requires complete
  100. cycle detection for correctness.
  101. * LoopInfo provides an interface for enumerating all top level loops
  102. (e.g. those not contained in any other loop). From there, you may
  103. walk the tree of sub-loops rooted in that top level loop.
  104. * Loops which become statically unreachable during optimization *must*
  105. be removed from LoopInfo. If this can not be done for some reason,
  106. then the optimization is *required* to preserve the static
  107. reachability of the loop.
  108. Loop Simplify Form
  109. ==================
  110. TBD
  111. Loop Closed SSA (LCSSA)
  112. =======================
  113. TBD
  114. "More Canonical" Loops
  115. ======================
  116. TBD