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.

199 lines
8.6KB

  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>define_peephole2 (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="define_peephole2 (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="define_peephole2 (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="Peephole-Definitions.html#Peephole-Definitions" rel="up" title="Peephole Definitions">
  30. <link href="Insn-Attributes.html#Insn-Attributes" rel="next" title="Insn Attributes">
  31. <link href="define_005fpeephole.html#define_005fpeephole" rel="prev" title="define_peephole">
  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="define_005fpeephole2"></a>
  62. <div class="header">
  63. <p>
  64. Previous: <a href="define_005fpeephole.html#define_005fpeephole" accesskey="p" rel="prev">define_peephole</a>, Up: <a href="Peephole-Definitions.html#Peephole-Definitions" accesskey="u" rel="up">Peephole Definitions</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="RTL-to-RTL-Peephole-Optimizers"></a>
  68. <h4 class="subsection">17.18.2 RTL to RTL Peephole Optimizers</h4>
  69. <a name="index-define_005fpeephole2"></a>
  70. <p>The <code>define_peephole2</code> definition tells the compiler how to
  71. substitute one sequence of instructions for another sequence,
  72. what additional scratch registers may be needed and what their
  73. lifetimes must be.
  74. </p>
  75. <div class="smallexample">
  76. <pre class="smallexample">(define_peephole2
  77. [<var>insn-pattern-1</var>
  78. <var>insn-pattern-2</var>
  79. &hellip;]
  80. &quot;<var>condition</var>&quot;
  81. [<var>new-insn-pattern-1</var>
  82. <var>new-insn-pattern-2</var>
  83. &hellip;]
  84. &quot;<var>preparation-statements</var>&quot;)
  85. </pre></div>
  86. <p>The definition is almost identical to <code>define_split</code>
  87. (see <a href="Insn-Splitting.html#Insn-Splitting">Insn Splitting</a>) except that the pattern to match is not a
  88. single instruction, but a sequence of instructions.
  89. </p>
  90. <p>It is possible to request additional scratch registers for use in the
  91. output template. If appropriate registers are not free, the pattern
  92. will simply not match.
  93. </p>
  94. <a name="index-match_005fscratch-1"></a>
  95. <a name="index-match_005fdup-1"></a>
  96. <p>Scratch registers are requested with a <code>match_scratch</code> pattern at
  97. the top level of the input pattern. The allocated register (initially) will
  98. be dead at the point requested within the original sequence. If the scratch
  99. is used at more than a single point, a <code>match_dup</code> pattern at the
  100. top level of the input pattern marks the last position in the input sequence
  101. at which the register must be available.
  102. </p>
  103. <p>Here is an example from the IA-32 machine description:
  104. </p>
  105. <div class="smallexample">
  106. <pre class="smallexample">(define_peephole2
  107. [(match_scratch:SI 2 &quot;r&quot;)
  108. (parallel [(set (match_operand:SI 0 &quot;register_operand&quot; &quot;&quot;)
  109. (match_operator:SI 3 &quot;arith_or_logical_operator&quot;
  110. [(match_dup 0)
  111. (match_operand:SI 1 &quot;memory_operand&quot; &quot;&quot;)]))
  112. (clobber (reg:CC 17))])]
  113. &quot;! optimize_size &amp;&amp; ! TARGET_READ_MODIFY&quot;
  114. [(set (match_dup 2) (match_dup 1))
  115. (parallel [(set (match_dup 0)
  116. (match_op_dup 3 [(match_dup 0) (match_dup 2)]))
  117. (clobber (reg:CC 17))])]
  118. &quot;&quot;)
  119. </pre></div>
  120. <p>This pattern tries to split a load from its use in the hopes that we&rsquo;ll be
  121. able to schedule around the memory load latency. It allocates a single
  122. <code>SImode</code> register of class <code>GENERAL_REGS</code> (<code>&quot;r&quot;</code>) that needs
  123. to be live only at the point just before the arithmetic.
  124. </p>
  125. <p>A real example requiring extended scratch lifetimes is harder to come by,
  126. so here&rsquo;s a silly made-up example:
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">(define_peephole2
  130. [(match_scratch:SI 4 &quot;r&quot;)
  131. (set (match_operand:SI 0 &quot;&quot; &quot;&quot;) (match_operand:SI 1 &quot;&quot; &quot;&quot;))
  132. (set (match_operand:SI 2 &quot;&quot; &quot;&quot;) (match_dup 1))
  133. (match_dup 4)
  134. (set (match_operand:SI 3 &quot;&quot; &quot;&quot;) (match_dup 1))]
  135. &quot;/* <span class="roman">determine 1 does not overlap 0 and 2</span> */&quot;
  136. [(set (match_dup 4) (match_dup 1))
  137. (set (match_dup 0) (match_dup 4))
  138. (set (match_dup 2) (match_dup 4))
  139. (set (match_dup 3) (match_dup 4))]
  140. &quot;&quot;)
  141. </pre></div>
  142. <p>There are two special macros defined for use in the preparation statements:
  143. <code>DONE</code> and <code>FAIL</code>. Use them with a following semicolon,
  144. as a statement.
  145. </p>
  146. <dl compact="compact">
  147. <dd>
  148. <a name="index-DONE-2"></a>
  149. </dd>
  150. <dt><code>DONE</code></dt>
  151. <dd><p>Use the <code>DONE</code> macro to end RTL generation for the peephole. The
  152. only RTL insns generated as replacement for the matched input insn will
  153. be those already emitted by explicit calls to <code>emit_insn</code> within
  154. the preparation statements; the replacement pattern is not used.
  155. </p>
  156. <a name="index-FAIL-2"></a>
  157. </dd>
  158. <dt><code>FAIL</code></dt>
  159. <dd><p>Make the <code>define_peephole2</code> fail on this occasion. When a <code>define_peephole2</code>
  160. fails, it means that the replacement was not truly available for the
  161. particular inputs it was given. In that case, GCC may still apply a
  162. later <code>define_peephole2</code> that also matches the given insn pattern.
  163. (Note that this is different from <code>define_split</code>, where <code>FAIL</code>
  164. prevents the input insn from being split at all.)
  165. </p></dd>
  166. </dl>
  167. <p>If the preparation falls through (invokes neither <code>DONE</code> nor
  168. <code>FAIL</code>), then the <code>define_peephole2</code> uses the replacement
  169. template.
  170. </p>
  171. <p>If we had not added the <code>(match_dup 4)</code> in the middle of the input
  172. sequence, it might have been the case that the register we chose at the
  173. beginning of the sequence is killed by the first or second <code>set</code>.
  174. </p>
  175. <hr>
  176. <div class="header">
  177. <p>
  178. Previous: <a href="define_005fpeephole.html#define_005fpeephole" accesskey="p" rel="prev">define_peephole</a>, Up: <a href="Peephole-Definitions.html#Peephole-Definitions" accesskey="u" rel="up">Peephole Definitions</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>
  179. </div>
  180. </body>
  181. </html>