Insn-Canonicalizations.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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: Insn Canonicalizations</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Insn Canonicalizations">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Insn Canonicalizations">
  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="Expander-Definitions.html#Expander-Definitions" rel="next" title="Expander Definitions">
  31. <link href="Looping-Patterns.html#Looping-Patterns" rel="prev" title="Looping 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="Insn-Canonicalizations"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Expander-Definitions.html#Expander-Definitions" accesskey="n" rel="next">Expander Definitions</a>, Previous: <a href="Looping-Patterns.html#Looping-Patterns" accesskey="p" rel="prev">Looping 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="Canonicalization-of-Instructions"></a>
  69. <h3 class="section">16.14 Canonicalization of Instructions</h3>
  70. <a name="index-canonicalization-of-instructions"></a>
  71. <a name="index-insn-canonicalization"></a>
  72. <p>There are often cases where multiple RTL expressions could represent an
  73. operation performed by a single machine instruction. This situation is
  74. most commonly encountered with logical, branch, and multiply-accumulate
  75. instructions. In such cases, the compiler attempts to convert these
  76. multiple RTL expressions into a single canonical form to reduce the
  77. number of insn patterns required.
  78. </p>
  79. <p>In addition to algebraic simplifications, following canonicalizations
  80. are performed:
  81. </p>
  82. <ul>
  83. <li> For commutative and comparison operators, a constant is always made the
  84. second operand. If a machine only supports a constant as the second
  85. operand, only patterns that match a constant in the second operand need
  86. be supplied.
  87. </li><li> For associative operators, a sequence of operators will always chain
  88. to the left; for instance, only the left operand of an integer <code>plus</code>
  89. can itself be a <code>plus</code>. <code>and</code>, <code>ior</code>, <code>xor</code>,
  90. <code>plus</code>, <code>mult</code>, <code>smin</code>, <code>smax</code>, <code>umin</code>, and
  91. <code>umax</code> are associative when applied to integers, and sometimes to
  92. floating-point.
  93. </li><li> <a name="index-neg_002c-canonicalization-of"></a>
  94. <a name="index-not_002c-canonicalization-of"></a>
  95. <a name="index-mult_002c-canonicalization-of"></a>
  96. <a name="index-plus_002c-canonicalization-of"></a>
  97. <a name="index-minus_002c-canonicalization-of"></a>
  98. For these operators, if only one operand is a <code>neg</code>, <code>not</code>,
  99. <code>mult</code>, <code>plus</code>, or <code>minus</code> expression, it will be the
  100. first operand.
  101. </li><li> In combinations of <code>neg</code>, <code>mult</code>, <code>plus</code>, and
  102. <code>minus</code>, the <code>neg</code> operations (if any) will be moved inside
  103. the operations as far as possible. For instance,
  104. <code>(neg (mult A B))</code> is canonicalized as <code>(mult (neg A) B)</code>, but
  105. <code>(plus (mult (neg B) C) A)</code> is canonicalized as
  106. <code>(minus A (mult B C))</code>.
  107. </li><li> <a name="index-compare_002c-canonicalization-of"></a>
  108. For the <code>compare</code> operator, a constant is always the second operand
  109. if the first argument is a condition code register or <code>(cc0)</code>.
  110. </li><li> An operand of <code>neg</code>, <code>not</code>, <code>mult</code>, <code>plus</code>, or
  111. <code>minus</code> is made the first operand under the same conditions as
  112. above.
  113. </li><li> <code>(ltu (plus <var>a</var> <var>b</var>) <var>b</var>)</code> is converted to
  114. <code>(ltu (plus <var>a</var> <var>b</var>) <var>a</var>)</code>. Likewise with <code>geu</code> instead
  115. of <code>ltu</code>.
  116. </li><li> <code>(minus <var>x</var> (const_int <var>n</var>))</code> is converted to
  117. <code>(plus <var>x</var> (const_int <var>-n</var>))</code>.
  118. </li><li> Within address computations (i.e., inside <code>mem</code>), a left shift is
  119. converted into the appropriate multiplication by a power of two.
  120. </li><li> <a name="index-ior_002c-canonicalization-of"></a>
  121. <a name="index-and_002c-canonicalization-of"></a>
  122. <a name="index-De-Morgan_0027s-law"></a>
  123. De Morgan&rsquo;s Law is used to move bitwise negation inside a bitwise
  124. logical-and or logical-or operation. If this results in only one
  125. operand being a <code>not</code> expression, it will be the first one.
  126. <p>A machine that has an instruction that performs a bitwise logical-and of one
  127. operand with the bitwise negation of the other should specify the pattern
  128. for that instruction as
  129. </p>
  130. <div class="smallexample">
  131. <pre class="smallexample">(define_insn &quot;&quot;
  132. [(set (match_operand:<var>m</var> 0 &hellip;)
  133. (and:<var>m</var> (not:<var>m</var> (match_operand:<var>m</var> 1 &hellip;))
  134. (match_operand:<var>m</var> 2 &hellip;)))]
  135. &quot;&hellip;&quot;
  136. &quot;&hellip;&quot;)
  137. </pre></div>
  138. <p>Similarly, a pattern for a &ldquo;NAND&rdquo; instruction should be written
  139. </p>
  140. <div class="smallexample">
  141. <pre class="smallexample">(define_insn &quot;&quot;
  142. [(set (match_operand:<var>m</var> 0 &hellip;)
  143. (ior:<var>m</var> (not:<var>m</var> (match_operand:<var>m</var> 1 &hellip;))
  144. (not:<var>m</var> (match_operand:<var>m</var> 2 &hellip;))))]
  145. &quot;&hellip;&quot;
  146. &quot;&hellip;&quot;)
  147. </pre></div>
  148. <p>In both cases, it is not necessary to include patterns for the many
  149. logically equivalent RTL expressions.
  150. </p>
  151. </li><li> <a name="index-xor_002c-canonicalization-of"></a>
  152. The only possible RTL expressions involving both bitwise exclusive-or
  153. and bitwise negation are <code>(xor:<var>m</var> <var>x</var> <var>y</var>)</code>
  154. and <code>(not:<var>m</var> (xor:<var>m</var> <var>x</var> <var>y</var>))</code>.
  155. </li><li> The sum of three items, one of which is a constant, will only appear in
  156. the form
  157. <div class="smallexample">
  158. <pre class="smallexample">(plus:<var>m</var> (plus:<var>m</var> <var>x</var> <var>y</var>) <var>constant</var>)
  159. </pre></div>
  160. </li><li> <a name="index-zero_005fextract_002c-canonicalization-of"></a>
  161. <a name="index-sign_005fextract_002c-canonicalization-of"></a>
  162. Equality comparisons of a group of bits (usually a single bit) with zero
  163. will be written using <code>zero_extract</code> rather than the equivalent
  164. <code>and</code> or <code>sign_extract</code> operations.
  165. </li><li> <a name="index-mult_002c-canonicalization-of-1"></a>
  166. <code>(sign_extend:<var>m1</var> (mult:<var>m2</var> (sign_extend:<var>m2</var> <var>x</var>)
  167. (sign_extend:<var>m2</var> <var>y</var>)))</code> is converted to <code>(mult:<var>m1</var>
  168. (sign_extend:<var>m1</var> <var>x</var>) (sign_extend:<var>m1</var> <var>y</var>))</code>, and likewise
  169. for <code>zero_extend</code>.
  170. </li><li> <code>(sign_extend:<var>m1</var> (mult:<var>m2</var> (ashiftrt:<var>m2</var>
  171. <var>x</var> <var>s</var>) (sign_extend:<var>m2</var> <var>y</var>)))</code> is converted
  172. to <code>(mult:<var>m1</var> (sign_extend:<var>m1</var> (ashiftrt:<var>m2</var>
  173. <var>x</var> <var>s</var>)) (sign_extend:<var>m1</var> <var>y</var>))</code>, and likewise for
  174. patterns using <code>zero_extend</code> and <code>lshiftrt</code>. If the second
  175. operand of <code>mult</code> is also a shift, then that is extended also.
  176. This transformation is only applied when it can be proven that the
  177. original operation had sufficient precision to prevent overflow.
  178. </li></ul>
  179. <p>Further canonicalization rules are defined in the function
  180. <code>commutative_operand_precedence</code> in <samp>gcc/rtlanal.c</samp>.
  181. </p>
  182. <hr>
  183. <div class="header">
  184. <p>
  185. Next: <a href="Expander-Definitions.html#Expander-Definitions" accesskey="n" rel="next">Expander Definitions</a>, Previous: <a href="Looping-Patterns.html#Looping-Patterns" accesskey="p" rel="prev">Looping 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>
  186. </div>
  187. </body>
  188. </html>