You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 line
11KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Loop representation (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Loop representation (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Loop representation (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Loop-Analysis-and-Representation.html#Loop-Analysis-and-Representation" rel="up" title="Loop Analysis and Representation">
  30. <link href="Loop-querying.html#Loop-querying" rel="next" title="Loop querying">
  31. <link href="Loop-Analysis-and-Representation.html#Loop-Analysis-and-Representation" rel="prev" title="Loop Analysis and Representation">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Loop-representation"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Loop-querying.html#Loop-querying" accesskey="n" rel="next">Loop querying</a>, Up: <a href="Loop-Analysis-and-Representation.html#Loop-Analysis-and-Representation" accesskey="u" rel="up">Loop Analysis and Representation</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Loop-representation-1"></a>
  68. <h3 class="section">16.1 Loop representation</h3>
  69. <a name="index-Loop-representation"></a>
  70. <a name="index-Loop-analysis"></a>
  71. <p>This chapter describes the representation of loops in GCC, and functions
  72. that can be used to build, modify and analyze this representation. Most
  73. of the interfaces and data structures are declared in <samp>cfgloop.h</samp>.
  74. Loop structures are analyzed and this information disposed or updated
  75. at the discretion of individual passes. Still most of the generic
  76. CFG manipulation routines are aware of loop structures and try to
  77. keep them up-to-date. By this means an increasing part of the
  78. compilation pipeline is setup to maintain loop structure across
  79. passes to allow attaching meta information to individual loops
  80. for consumption by later passes.
  81. </p>
  82. <p>In general, a natural loop has one entry block (header) and possibly
  83. several back edges (latches) leading to the header from the inside of
  84. the loop. Loops with several latches may appear if several loops share
  85. a single header, or if there is a branching in the middle of the loop.
  86. The representation of loops in GCC however allows only loops with a
  87. single latch. During loop analysis, headers of such loops are split and
  88. forwarder blocks are created in order to disambiguate their structures.
  89. Heuristic based on profile information and structure of the induction
  90. variables in the loops is used to determine whether the latches
  91. correspond to sub-loops or to control flow in a single loop. This means
  92. that the analysis sometimes changes the CFG, and if you run it in the
  93. middle of an optimization pass, you must be able to deal with the new
  94. blocks. You may avoid CFG changes by passing
  95. <code>LOOPS_MAY_HAVE_MULTIPLE_LATCHES</code> flag to the loop discovery,
  96. note however that most other loop manipulation functions will not work
  97. correctly for loops with multiple latch edges (the functions that only
  98. query membership of blocks to loops and subloop relationships, or
  99. enumerate and test loop exits, can be expected to work).
  100. </p>
  101. <p>Body of the loop is the set of blocks that are dominated by its header,
  102. and reachable from its latch against the direction of edges in CFG. The
  103. loops are organized in a containment hierarchy (tree) such that all the
  104. loops immediately contained inside loop L are the children of L in the
  105. tree. This tree is represented by the <code>struct loops</code> structure.
  106. The root of this tree is a fake loop that contains all blocks in the
  107. function. Each of the loops is represented in a <code>struct loop</code>
  108. structure. Each loop is assigned an index (<code>num</code> field of the
  109. <code>struct loop</code> structure), and the pointer to the loop is stored in
  110. the corresponding field of the <code>larray</code> vector in the loops
  111. structure. The indices do not have to be continuous, there may be
  112. empty (<code>NULL</code>) entries in the <code>larray</code> created by deleting
  113. loops. Also, there is no guarantee on the relative order of a loop
  114. and its subloops in the numbering. The index of a loop never changes.
  115. </p>
  116. <p>The entries of the <code>larray</code> field should not be accessed directly.
  117. The function <code>get_loop</code> returns the loop description for a loop with
  118. the given index. <code>number_of_loops</code> function returns number of
  119. loops in the function. To traverse all loops, use <code>FOR_EACH_LOOP</code>
  120. macro. The <code>flags</code> argument of the macro is used to determine
  121. the direction of traversal and the set of loops visited. Each loop is
  122. guaranteed to be visited exactly once, regardless of the changes to the
  123. loop tree, and the loops may be removed during the traversal. The newly
  124. created loops are never traversed, if they need to be visited, this
  125. must be done separately after their creation.
  126. </p>
  127. <p>Each basic block contains the reference to the innermost loop it belongs
  128. to (<code>loop_father</code>). For this reason, it is only possible to have
  129. one <code>struct loops</code> structure initialized at the same time for each
  130. CFG. The global variable <code>current_loops</code> contains the
  131. <code>struct loops</code> structure. Many of the loop manipulation functions
  132. assume that dominance information is up-to-date.
  133. </p>
  134. <p>The loops are analyzed through <code>loop_optimizer_init</code> function. The
  135. argument of this function is a set of flags represented in an integer
  136. bitmask. These flags specify what other properties of the loop
  137. structures should be calculated/enforced and preserved later:
  138. </p>
  139. <ul>
  140. <li> <code>LOOPS_MAY_HAVE_MULTIPLE_LATCHES</code>: If this flag is set, no
  141. changes to CFG will be performed in the loop analysis, in particular,
  142. loops with multiple latch edges will not be disambiguated. If a loop
  143. has multiple latches, its latch block is set to NULL. Most of
  144. the loop manipulation functions will not work for loops in this shape.
  145. No other flags that require CFG changes can be passed to
  146. loop_optimizer_init.
  147. </li><li> <code>LOOPS_HAVE_PREHEADERS</code>: Forwarder blocks are created in such
  148. a way that each loop has only one entry edge, and additionally, the
  149. source block of this entry edge has only one successor. This creates a
  150. natural place where the code can be moved out of the loop, and ensures
  151. that the entry edge of the loop leads from its immediate super-loop.
  152. </li><li> <code>LOOPS_HAVE_SIMPLE_LATCHES</code>: Forwarder blocks are created to
  153. force the latch block of each loop to have only one successor. This
  154. ensures that the latch of the loop does not belong to any of its
  155. sub-loops, and makes manipulation with the loops significantly easier.
  156. Most of the loop manipulation functions assume that the loops are in
  157. this shape. Note that with this flag, the &ldquo;normal&rdquo; loop without any
  158. control flow inside and with one exit consists of two basic blocks.
  159. </li><li> <code>LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS</code>: Basic blocks and
  160. edges in the strongly connected components that are not natural loops
  161. (have more than one entry block) are marked with
  162. <code>BB_IRREDUCIBLE_LOOP</code> and <code>EDGE_IRREDUCIBLE_LOOP</code> flags. The
  163. flag is not set for blocks and edges that belong to natural loops that
  164. are in such an irreducible region (but it is set for the entry and exit
  165. edges of such a loop, if they lead to/from this region).
  166. </li><li> <code>LOOPS_HAVE_RECORDED_EXITS</code>: The lists of exits are recorded
  167. and updated for each loop. This makes some functions (e.g.,
  168. <code>get_loop_exit_edges</code>) more efficient. Some functions (e.g.,
  169. <code>single_exit</code>) can be used only if the lists of exits are
  170. recorded.
  171. </li></ul>
  172. <p>These properties may also be computed/enforced later, using functions
  173. <code>create_preheaders</code>, <code>force_single_succ_latches</code>,
  174. <code>mark_irreducible_loops</code> and <code>record_loop_exits</code>.
  175. The properties can be queried using <code>loops_state_satisfies_p</code>.
  176. </p>
  177. <p>The memory occupied by the loops structures should be freed with
  178. <code>loop_optimizer_finalize</code> function. When loop structures are
  179. setup to be preserved across passes this function reduces the
  180. information to be kept up-to-date to a minimum (only
  181. <code>LOOPS_MAY_HAVE_MULTIPLE_LATCHES</code> set).
  182. </p>
  183. <p>The CFG manipulation functions in general do not update loop structures.
  184. Specialized versions that additionally do so are provided for the most
  185. common tasks. On GIMPLE, <code>cleanup_tree_cfg_loop</code> function can be
  186. used to cleanup CFG while updating the loops structures if
  187. <code>current_loops</code> is set.
  188. </p>
  189. <p>At the moment loop structure is preserved from the start of GIMPLE
  190. loop optimizations until the end of RTL loop optimizations. During
  191. this time a loop can be tracked by its <code>struct loop</code> and number.
  192. </p>
  193. <hr>
  194. <div class="header">
  195. <p>
  196. Next: <a href="Loop-querying.html#Loop-querying" accesskey="n" rel="next">Loop querying</a>, Up: <a href="Loop-Analysis-and-Representation.html#Loop-Analysis-and-Representation" accesskey="u" rel="up">Loop Analysis and Representation</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  197. </div>
  198. </body>
  199. </html>