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.

213 lines
9.9KB

  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>Looping Patterns (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Looping Patterns (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Looping Patterns (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="Machine-Desc.html#Machine-Desc" rel="up" title="Machine Desc">
  30. <link href="Insn-Canonicalizations.html#Insn-Canonicalizations" rel="next" title="Insn Canonicalizations">
  31. <link href="Jump-Patterns.html#Jump-Patterns" rel="prev" title="Jump Patterns">
  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="Looping-Patterns"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Insn-Canonicalizations.html#Insn-Canonicalizations" accesskey="n" rel="next">Insn Canonicalizations</a>, Previous: <a href="Jump-Patterns.html#Jump-Patterns" accesskey="p" rel="prev">Jump Patterns</a>, Up: <a href="Machine-Desc.html#Machine-Desc" accesskey="u" rel="up">Machine Desc</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="Defining-Looping-Instruction-Patterns"></a>
  68. <h3 class="section">17.13 Defining Looping Instruction Patterns</h3>
  69. <a name="index-looping-instruction-patterns"></a>
  70. <a name="index-defining-looping-instruction-patterns"></a>
  71. <p>Some machines have special jump instructions that can be utilized to
  72. make loops more efficient. A common example is the 68000 &lsquo;<samp>dbra</samp>&rsquo;
  73. instruction which performs a decrement of a register and a branch if the
  74. result was greater than zero. Other machines, in particular digital
  75. signal processors (DSPs), have special block repeat instructions to
  76. provide low-overhead loop support. For example, the TI TMS320C3x/C4x
  77. DSPs have a block repeat instruction that loads special registers to
  78. mark the top and end of a loop and to count the number of loop
  79. iterations. This avoids the need for fetching and executing a
  80. &lsquo;<samp>dbra</samp>&rsquo;-like instruction and avoids pipeline stalls associated with
  81. the jump.
  82. </p>
  83. <p>GCC has two special named patterns to support low overhead looping.
  84. They are &lsquo;<samp>doloop_begin</samp>&rsquo; and &lsquo;<samp>doloop_end</samp>&rsquo;. These are emitted
  85. by the loop optimizer for certain well-behaved loops with a finite
  86. number of loop iterations using information collected during strength
  87. reduction.
  88. </p>
  89. <p>The &lsquo;<samp>doloop_end</samp>&rsquo; pattern describes the actual looping instruction
  90. (or the implicit looping operation) and the &lsquo;<samp>doloop_begin</samp>&rsquo; pattern
  91. is an optional companion pattern that can be used for initialization
  92. needed for some low-overhead looping instructions.
  93. </p>
  94. <p>Note that some machines require the actual looping instruction to be
  95. emitted at the top of the loop (e.g., the TMS320C3x/C4x DSPs). Emitting
  96. the true RTL for a looping instruction at the top of the loop can cause
  97. problems with flow analysis. So instead, a dummy <code>doloop</code> insn is
  98. emitted at the end of the loop. The machine dependent reorg pass checks
  99. for the presence of this <code>doloop</code> insn and then searches back to
  100. the top of the loop, where it inserts the true looping insn (provided
  101. there are no instructions in the loop which would cause problems). Any
  102. additional labels can be emitted at this point. In addition, if the
  103. desired special iteration counter register was not allocated, this
  104. machine dependent reorg pass could emit a traditional compare and jump
  105. instruction pair.
  106. </p>
  107. <p>For the &lsquo;<samp>doloop_end</samp>&rsquo; pattern, the loop optimizer allocates an
  108. additional pseudo register as an iteration counter. This pseudo
  109. register cannot be used within the loop (i.e., general induction
  110. variables cannot be derived from it), however, in many cases the loop
  111. induction variable may become redundant and removed by the flow pass.
  112. </p>
  113. <p>The &lsquo;<samp>doloop_end</samp>&rsquo; pattern must have a specific structure to be
  114. handled correctly by GCC. The example below is taken (slightly
  115. simplified) from the PDP-11 target:
  116. </p>
  117. <div class="smallexample">
  118. <pre class="smallexample">(define_expand &quot;doloop_end&quot;
  119. [(parallel [(set (pc)
  120. (if_then_else
  121. (ne (match_operand:HI 0 &quot;nonimmediate_operand&quot; &quot;+r,!m&quot;)
  122. (const_int 1))
  123. (label_ref (match_operand 1 &quot;&quot; &quot;&quot;))
  124. (pc)))
  125. (set (match_dup 0)
  126. (plus:HI (match_dup 0)
  127. (const_int -1)))])]
  128. &quot;&quot;
  129. &quot;{
  130. if (GET_MODE (operands[0]) != HImode)
  131. FAIL;
  132. }&quot;)
  133. (define_insn &quot;doloop_end_insn&quot;
  134. [(set (pc)
  135. (if_then_else
  136. (ne (match_operand:HI 0 &quot;nonimmediate_operand&quot; &quot;+r,!m&quot;)
  137. (const_int 1))
  138. (label_ref (match_operand 1 &quot;&quot; &quot;&quot;))
  139. (pc)))
  140. (set (match_dup 0)
  141. (plus:HI (match_dup 0)
  142. (const_int -1)))]
  143. &quot;&quot;
  144. {
  145. if (which_alternative == 0)
  146. return &quot;sob %0,%l1&quot;;
  147. /* emulate sob */
  148. output_asm_insn (&quot;dec %0&quot;, operands);
  149. return &quot;bne %l1&quot;;
  150. })
  151. </pre></div>
  152. <p>The first part of the pattern describes the branch condition. GCC
  153. supports three cases for the way the target machine handles the loop
  154. counter:
  155. </p><ul>
  156. <li> Loop terminates when the loop register decrements to zero. This
  157. is represented by a <code>ne</code> comparison of the register (its old value)
  158. with constant 1 (as in the example above).
  159. </li><li> Loop terminates when the loop register decrements to -1.
  160. This is represented by a <code>ne</code> comparison of the register with
  161. constant zero.
  162. </li><li> Loop terminates when the loop register decrements to a negative
  163. value. This is represented by a <code>ge</code> comparison of the register
  164. with constant zero. For this case, GCC will attach a <code>REG_NONNEG</code>
  165. note to the <code>doloop_end</code> insn if it can determine that the register
  166. will be non-negative.
  167. </li></ul>
  168. <p>Since the <code>doloop_end</code> insn is a jump insn that also has an output,
  169. the reload pass does not handle the output operand. Therefore, the
  170. constraint must allow for that operand to be in memory rather than a
  171. register. In the example shown above, that is handled (in the
  172. <code>doloop_end_insn</code> pattern) by using a loop instruction sequence
  173. that can handle memory operands when the memory alternative appears.
  174. </p>
  175. <p>GCC does not check the mode of the loop register operand when generating
  176. the <code>doloop_end</code> pattern. If the pattern is only valid for some
  177. modes but not others, the pattern should be a <code>define_expand</code>
  178. pattern that checks the operand mode in the preparation code, and issues
  179. <code>FAIL</code> if an unsupported mode is found. The example above does
  180. this, since the machine instruction to be used only exists for
  181. <code>HImode</code>.
  182. </p>
  183. <p>If the <code>doloop_end</code> pattern is a <code>define_expand</code>, there must
  184. also be a <code>define_insn</code> or <code>define_insn_and_split</code> matching
  185. the generated pattern. Otherwise, the compiler will fail during loop
  186. optimization.
  187. </p>
  188. <hr>
  189. <div class="header">
  190. <p>
  191. Next: <a href="Insn-Canonicalizations.html#Insn-Canonicalizations" accesskey="n" rel="next">Insn Canonicalizations</a>, Previous: <a href="Jump-Patterns.html#Jump-Patterns" accesskey="p" rel="prev">Jump Patterns</a>, Up: <a href="Machine-Desc.html#Machine-Desc" accesskey="u" rel="up">Machine Desc</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>
  192. </div>
  193. </body>
  194. </html>