define_005fpeephole2.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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: define_peephole2</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: define_peephole2">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: define_peephole2">
  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="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.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="define_005fpeephole2"></a>
  63. <div class="header">
  64. <p>
  65. 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>
  66. </div>
  67. <hr>
  68. <a name="RTL-to-RTL-Peephole-Optimizers"></a>
  69. <h4 class="subsection">16.18.2 RTL to RTL Peephole Optimizers</h4>
  70. <a name="index-define_005fpeephole2"></a>
  71. <p>The <code>define_peephole2</code> definition tells the compiler how to
  72. substitute one sequence of instructions for another sequence,
  73. what additional scratch registers may be needed and what their
  74. lifetimes must be.
  75. </p>
  76. <div class="smallexample">
  77. <pre class="smallexample">(define_peephole2
  78. [<var>insn-pattern-1</var>
  79. <var>insn-pattern-2</var>
  80. &hellip;]
  81. &quot;<var>condition</var>&quot;
  82. [<var>new-insn-pattern-1</var>
  83. <var>new-insn-pattern-2</var>
  84. &hellip;]
  85. &quot;<var>preparation-statements</var>&quot;)
  86. </pre></div>
  87. <p>The definition is almost identical to <code>define_split</code>
  88. (see <a href="Insn-Splitting.html#Insn-Splitting">Insn Splitting</a>) except that the pattern to match is not a
  89. single instruction, but a sequence of instructions.
  90. </p>
  91. <p>It is possible to request additional scratch registers for use in the
  92. output template. If appropriate registers are not free, the pattern
  93. will simply not match.
  94. </p>
  95. <a name="index-match_005fscratch-1"></a>
  96. <a name="index-match_005fdup-1"></a>
  97. <p>Scratch registers are requested with a <code>match_scratch</code> pattern at
  98. the top level of the input pattern. The allocated register (initially) will
  99. be dead at the point requested within the original sequence. If the scratch
  100. is used at more than a single point, a <code>match_dup</code> pattern at the
  101. top level of the input pattern marks the last position in the input sequence
  102. at which the register must be available.
  103. </p>
  104. <p>Here is an example from the IA-32 machine description:
  105. </p>
  106. <div class="smallexample">
  107. <pre class="smallexample">(define_peephole2
  108. [(match_scratch:SI 2 &quot;r&quot;)
  109. (parallel [(set (match_operand:SI 0 &quot;register_operand&quot; &quot;&quot;)
  110. (match_operator:SI 3 &quot;arith_or_logical_operator&quot;
  111. [(match_dup 0)
  112. (match_operand:SI 1 &quot;memory_operand&quot; &quot;&quot;)]))
  113. (clobber (reg:CC 17))])]
  114. &quot;! optimize_size &amp;&amp; ! TARGET_READ_MODIFY&quot;
  115. [(set (match_dup 2) (match_dup 1))
  116. (parallel [(set (match_dup 0)
  117. (match_op_dup 3 [(match_dup 0) (match_dup 2)]))
  118. (clobber (reg:CC 17))])]
  119. &quot;&quot;)
  120. </pre></div>
  121. <p>This pattern tries to split a load from its use in the hopes that we&rsquo;ll be
  122. able to schedule around the memory load latency. It allocates a single
  123. <code>SImode</code> register of class <code>GENERAL_REGS</code> (<code>&quot;r&quot;</code>) that needs
  124. to be live only at the point just before the arithmetic.
  125. </p>
  126. <p>A real example requiring extended scratch lifetimes is harder to come by,
  127. so here&rsquo;s a silly made-up example:
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">(define_peephole2
  131. [(match_scratch:SI 4 &quot;r&quot;)
  132. (set (match_operand:SI 0 &quot;&quot; &quot;&quot;) (match_operand:SI 1 &quot;&quot; &quot;&quot;))
  133. (set (match_operand:SI 2 &quot;&quot; &quot;&quot;) (match_dup 1))
  134. (match_dup 4)
  135. (set (match_operand:SI 3 &quot;&quot; &quot;&quot;) (match_dup 1))]
  136. &quot;/* <span class="roman">determine 1 does not overlap 0 and 2</span> */&quot;
  137. [(set (match_dup 4) (match_dup 1))
  138. (set (match_dup 0) (match_dup 4))
  139. (set (match_dup 2) (match_dup 4))
  140. (set (match_dup 3) (match_dup 4))]
  141. &quot;&quot;)
  142. </pre></div>
  143. <p>If we had not added the <code>(match_dup 4)</code> in the middle of the input
  144. sequence, it might have been the case that the register we chose at the
  145. beginning of the sequence is killed by the first or second <code>set</code>.
  146. </p>
  147. <hr>
  148. <div class="header">
  149. <p>
  150. 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>
  151. </div>
  152. </body>
  153. </html>