Looping-Patterns.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: Looping Patterns</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Looping Patterns">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Looping Patterns">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Looping-Patterns"></a>
  63. <div class="header">
  64. <p>
  65. 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>
  66. </div>
  67. <hr>
  68. <a name="Defining-Looping-Instruction-Patterns"></a>
  69. <h3 class="section">16.13 Defining Looping Instruction Patterns</h3>
  70. <a name="index-looping-instruction-patterns"></a>
  71. <a name="index-defining-looping-instruction-patterns"></a>
  72. <p>Some machines have special jump instructions that can be utilized to
  73. make loops more efficient. A common example is the 68000 &lsquo;<samp>dbra</samp>&rsquo;
  74. instruction which performs a decrement of a register and a branch if the
  75. result was greater than zero. Other machines, in particular digital
  76. signal processors (DSPs), have special block repeat instructions to
  77. provide low-overhead loop support. For example, the TI TMS320C3x/C4x
  78. DSPs have a block repeat instruction that loads special registers to
  79. mark the top and end of a loop and to count the number of loop
  80. iterations. This avoids the need for fetching and executing a
  81. &lsquo;<samp>dbra</samp>&rsquo;-like instruction and avoids pipeline stalls associated with
  82. the jump.
  83. </p>
  84. <p>GCC has three special named patterns to support low overhead looping.
  85. They are &lsquo;<samp>decrement_and_branch_until_zero</samp>&rsquo;, &lsquo;<samp>doloop_begin</samp>&rsquo;,
  86. and &lsquo;<samp>doloop_end</samp>&rsquo;. The first pattern,
  87. &lsquo;<samp>decrement_and_branch_until_zero</samp>&rsquo;, is not emitted during RTL
  88. generation but may be emitted during the instruction combination phase.
  89. This requires the assistance of the loop optimizer, using information
  90. collected during strength reduction, to reverse a loop to count down to
  91. zero. Some targets also require the loop optimizer to add a
  92. <code>REG_NONNEG</code> note to indicate that the iteration count is always
  93. positive. This is needed if the target performs a signed loop
  94. termination test. For example, the 68000 uses a pattern similar to the
  95. following for its <code>dbra</code> instruction:
  96. </p>
  97. <div class="smallexample">
  98. <pre class="smallexample">(define_insn &quot;decrement_and_branch_until_zero&quot;
  99. [(set (pc)
  100. (if_then_else
  101. (ge (plus:SI (match_operand:SI 0 &quot;general_operand&quot; &quot;+d*am&quot;)
  102. (const_int -1))
  103. (const_int 0))
  104. (label_ref (match_operand 1 &quot;&quot; &quot;&quot;))
  105. (pc)))
  106. (set (match_dup 0)
  107. (plus:SI (match_dup 0)
  108. (const_int -1)))]
  109. &quot;find_reg_note (insn, REG_NONNEG, 0)&quot;
  110. &quot;&hellip;&quot;)
  111. </pre></div>
  112. <p>Note that since the insn is both a jump insn and has an output, it must
  113. deal with its own reloads, hence the &lsquo;m&rsquo; constraints. Also note that
  114. since this insn is generated by the instruction combination phase
  115. combining two sequential insns together into an implicit parallel insn,
  116. the iteration counter needs to be biased by the same amount as the
  117. decrement operation, in this case -1. Note that the following similar
  118. pattern will not be matched by the combiner.
  119. </p>
  120. <div class="smallexample">
  121. <pre class="smallexample">(define_insn &quot;decrement_and_branch_until_zero&quot;
  122. [(set (pc)
  123. (if_then_else
  124. (ge (match_operand:SI 0 &quot;general_operand&quot; &quot;+d*am&quot;)
  125. (const_int 1))
  126. (label_ref (match_operand 1 &quot;&quot; &quot;&quot;))
  127. (pc)))
  128. (set (match_dup 0)
  129. (plus:SI (match_dup 0)
  130. (const_int -1)))]
  131. &quot;find_reg_note (insn, REG_NONNEG, 0)&quot;
  132. &quot;&hellip;&quot;)
  133. </pre></div>
  134. <p>The other two special looping patterns, &lsquo;<samp>doloop_begin</samp>&rsquo; and
  135. &lsquo;<samp>doloop_end</samp>&rsquo;, are emitted by the loop optimizer for certain
  136. well-behaved loops with a finite number of loop iterations using
  137. information collected during strength reduction.
  138. </p>
  139. <p>The &lsquo;<samp>doloop_end</samp>&rsquo; pattern describes the actual looping instruction
  140. (or the implicit looping operation) and the &lsquo;<samp>doloop_begin</samp>&rsquo; pattern
  141. is an optional companion pattern that can be used for initialization
  142. needed for some low-overhead looping instructions.
  143. </p>
  144. <p>Note that some machines require the actual looping instruction to be
  145. emitted at the top of the loop (e.g., the TMS320C3x/C4x DSPs). Emitting
  146. the true RTL for a looping instruction at the top of the loop can cause
  147. problems with flow analysis. So instead, a dummy <code>doloop</code> insn is
  148. emitted at the end of the loop. The machine dependent reorg pass checks
  149. for the presence of this <code>doloop</code> insn and then searches back to
  150. the top of the loop, where it inserts the true looping insn (provided
  151. there are no instructions in the loop which would cause problems). Any
  152. additional labels can be emitted at this point. In addition, if the
  153. desired special iteration counter register was not allocated, this
  154. machine dependent reorg pass could emit a traditional compare and jump
  155. instruction pair.
  156. </p>
  157. <p>The essential difference between the
  158. &lsquo;<samp>decrement_and_branch_until_zero</samp>&rsquo; and the &lsquo;<samp>doloop_end</samp>&rsquo;
  159. patterns is that the loop optimizer allocates an additional pseudo
  160. register for the latter as an iteration counter. This pseudo register
  161. cannot be used within the loop (i.e., general induction variables cannot
  162. be derived from it), however, in many cases the loop induction variable
  163. may become redundant and removed by the flow pass.
  164. </p>
  165. <hr>
  166. <div class="header">
  167. <p>
  168. 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>
  169. </div>
  170. </body>
  171. </html>