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.

256 lines
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>define_peephole (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="define_peephole (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="define_peephole (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="define_005fpeephole2.html#define_005fpeephole2" rel="next" title="define_peephole2">
  31. <link href="Peephole-Definitions.html#Peephole-Definitions" rel="prev" title="Peephole Definitions">
  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_005fpeephole"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="define_005fpeephole2.html#define_005fpeephole2" accesskey="n" rel="next">define_peephole2</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-Text-Peephole-Optimizers"></a>
  68. <h4 class="subsection">17.18.1 RTL to Text Peephole Optimizers</h4>
  69. <a name="index-define_005fpeephole"></a>
  70. <p>A definition looks like this:
  71. </p>
  72. <div class="smallexample">
  73. <pre class="smallexample">(define_peephole
  74. [<var>insn-pattern-1</var>
  75. <var>insn-pattern-2</var>
  76. &hellip;]
  77. &quot;<var>condition</var>&quot;
  78. &quot;<var>template</var>&quot;
  79. &quot;<var>optional-insn-attributes</var>&quot;)
  80. </pre></div>
  81. <p>The last string operand may be omitted if you are not using any
  82. machine-specific information in this machine description. If present,
  83. it must obey the same rules as in a <code>define_insn</code>.
  84. </p>
  85. <p>In this skeleton, <var>insn-pattern-1</var> and so on are patterns to match
  86. consecutive insns. The optimization applies to a sequence of insns when
  87. <var>insn-pattern-1</var> matches the first one, <var>insn-pattern-2</var> matches
  88. the next, and so on.
  89. </p>
  90. <p>Each of the insns matched by a peephole must also match a
  91. <code>define_insn</code>. Peepholes are checked only at the last stage just
  92. before code generation, and only optionally. Therefore, any insn which
  93. would match a peephole but no <code>define_insn</code> will cause a crash in code
  94. generation in an unoptimized compilation, or at various optimization
  95. stages.
  96. </p>
  97. <p>The operands of the insns are matched with <code>match_operands</code>,
  98. <code>match_operator</code>, and <code>match_dup</code>, as usual. What is not
  99. usual is that the operand numbers apply to all the insn patterns in the
  100. definition. So, you can check for identical operands in two insns by
  101. using <code>match_operand</code> in one insn and <code>match_dup</code> in the
  102. other.
  103. </p>
  104. <p>The operand constraints used in <code>match_operand</code> patterns do not have
  105. any direct effect on the applicability of the peephole, but they will
  106. be validated afterward, so make sure your constraints are general enough
  107. to apply whenever the peephole matches. If the peephole matches
  108. but the constraints are not satisfied, the compiler will crash.
  109. </p>
  110. <p>It is safe to omit constraints in all the operands of the peephole; or
  111. you can write constraints which serve as a double-check on the criteria
  112. previously tested.
  113. </p>
  114. <p>Once a sequence of insns matches the patterns, the <var>condition</var> is
  115. checked. This is a C expression which makes the final decision whether to
  116. perform the optimization (we do so if the expression is nonzero). If
  117. <var>condition</var> is omitted (in other words, the string is empty) then the
  118. optimization is applied to every sequence of insns that matches the
  119. patterns.
  120. </p>
  121. <p>The defined peephole optimizations are applied after register allocation
  122. is complete. Therefore, the peephole definition can check which
  123. operands have ended up in which kinds of registers, just by looking at
  124. the operands.
  125. </p>
  126. <a name="index-prev_005factive_005finsn"></a>
  127. <p>The way to refer to the operands in <var>condition</var> is to write
  128. <code>operands[<var>i</var>]</code> for operand number <var>i</var> (as matched by
  129. <code>(match_operand <var>i</var> &hellip;)</code>). Use the variable <code>insn</code>
  130. to refer to the last of the insns being matched; use
  131. <code>prev_active_insn</code> to find the preceding insns.
  132. </p>
  133. <a name="index-dead_005for_005fset_005fp"></a>
  134. <p>When optimizing computations with intermediate results, you can use
  135. <var>condition</var> to match only when the intermediate results are not used
  136. elsewhere. Use the C expression <code>dead_or_set_p (<var>insn</var>,
  137. <var>op</var>)</code>, where <var>insn</var> is the insn in which you expect the value
  138. to be used for the last time (from the value of <code>insn</code>, together
  139. with use of <code>prev_nonnote_insn</code>), and <var>op</var> is the intermediate
  140. value (from <code>operands[<var>i</var>]</code>).
  141. </p>
  142. <p>Applying the optimization means replacing the sequence of insns with one
  143. new insn. The <var>template</var> controls ultimate output of assembler code
  144. for this combined insn. It works exactly like the template of a
  145. <code>define_insn</code>. Operand numbers in this template are the same ones
  146. used in matching the original sequence of insns.
  147. </p>
  148. <p>The result of a defined peephole optimizer does not need to match any of
  149. the insn patterns in the machine description; it does not even have an
  150. opportunity to match them. The peephole optimizer definition itself serves
  151. as the insn pattern to control how the insn is output.
  152. </p>
  153. <p>Defined peephole optimizers are run as assembler code is being output,
  154. so the insns they produce are never combined or rearranged in any way.
  155. </p>
  156. <p>Here is an example, taken from the 68000 machine description:
  157. </p>
  158. <div class="smallexample">
  159. <pre class="smallexample">(define_peephole
  160. [(set (reg:SI 15) (plus:SI (reg:SI 15) (const_int 4)))
  161. (set (match_operand:DF 0 &quot;register_operand&quot; &quot;=f&quot;)
  162. (match_operand:DF 1 &quot;register_operand&quot; &quot;ad&quot;))]
  163. &quot;FP_REG_P (operands[0]) &amp;&amp; ! FP_REG_P (operands[1])&quot;
  164. {
  165. rtx xoperands[2];
  166. xoperands[1] = gen_rtx_REG (SImode, REGNO (operands[1]) + 1);
  167. #ifdef MOTOROLA
  168. output_asm_insn (&quot;move.l %1,(sp)&quot;, xoperands);
  169. output_asm_insn (&quot;move.l %1,-(sp)&quot;, operands);
  170. return &quot;fmove.d (sp)+,%0&quot;;
  171. #else
  172. output_asm_insn (&quot;movel %1,sp@&quot;, xoperands);
  173. output_asm_insn (&quot;movel %1,sp@-&quot;, operands);
  174. return &quot;fmoved sp@+,%0&quot;;
  175. #endif
  176. })
  177. </pre></div>
  178. <p>The effect of this optimization is to change
  179. </p>
  180. <div class="smallexample">
  181. <pre class="smallexample">jbsr _foobar
  182. addql #4,sp
  183. movel d1,sp@-
  184. movel d0,sp@-
  185. fmoved sp@+,fp0
  186. </pre></div>
  187. <p>into
  188. </p>
  189. <div class="smallexample">
  190. <pre class="smallexample">jbsr _foobar
  191. movel d1,sp@
  192. movel d0,sp@-
  193. fmoved sp@+,fp0
  194. </pre></div>
  195. <p><var>insn-pattern-1</var> and so on look <em>almost</em> like the second
  196. operand of <code>define_insn</code>. There is one important difference: the
  197. second operand of <code>define_insn</code> consists of one or more RTX&rsquo;s
  198. enclosed in square brackets. Usually, there is only one: then the same
  199. action can be written as an element of a <code>define_peephole</code>. But
  200. when there are multiple actions in a <code>define_insn</code>, they are
  201. implicitly enclosed in a <code>parallel</code>. Then you must explicitly
  202. write the <code>parallel</code>, and the square brackets within it, in the
  203. <code>define_peephole</code>. Thus, if an insn pattern looks like this,
  204. </p>
  205. <div class="smallexample">
  206. <pre class="smallexample">(define_insn &quot;divmodsi4&quot;
  207. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=d&quot;)
  208. (div:SI (match_operand:SI 1 &quot;general_operand&quot; &quot;0&quot;)
  209. (match_operand:SI 2 &quot;general_operand&quot; &quot;dmsK&quot;)))
  210. (set (match_operand:SI 3 &quot;general_operand&quot; &quot;=d&quot;)
  211. (mod:SI (match_dup 1) (match_dup 2)))]
  212. &quot;TARGET_68020&quot;
  213. &quot;divsl%.l %2,%3:%0&quot;)
  214. </pre></div>
  215. <p>then the way to mention this insn in a peephole is as follows:
  216. </p>
  217. <div class="smallexample">
  218. <pre class="smallexample">(define_peephole
  219. [&hellip;
  220. (parallel
  221. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;=d&quot;)
  222. (div:SI (match_operand:SI 1 &quot;general_operand&quot; &quot;0&quot;)
  223. (match_operand:SI 2 &quot;general_operand&quot; &quot;dmsK&quot;)))
  224. (set (match_operand:SI 3 &quot;general_operand&quot; &quot;=d&quot;)
  225. (mod:SI (match_dup 1) (match_dup 2)))])
  226. &hellip;]
  227. &hellip;)
  228. </pre></div>
  229. <hr>
  230. <div class="header">
  231. <p>
  232. Next: <a href="define_005fpeephole2.html#define_005fpeephole2" accesskey="n" rel="next">define_peephole2</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>
  233. </div>
  234. </body>
  235. </html>