Expander-Definitions.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: Expander Definitions</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Expander Definitions">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Expander Definitions">
  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-Splitting.html#Insn-Splitting" rel="next" title="Insn Splitting">
  31. <link href="Insn-Canonicalizations.html#Insn-Canonicalizations" rel="prev" title="Insn Canonicalizations">
  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="Expander-Definitions"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Insn-Splitting.html#Insn-Splitting" accesskey="n" rel="next">Insn Splitting</a>, Previous: <a href="Insn-Canonicalizations.html#Insn-Canonicalizations" accesskey="p" rel="prev">Insn Canonicalizations</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-RTL-Sequences-for-Code-Generation"></a>
  69. <h3 class="section">16.15 Defining RTL Sequences for Code Generation</h3>
  70. <a name="index-expander-definitions"></a>
  71. <a name="index-code-generation-RTL-sequences"></a>
  72. <a name="index-defining-RTL-sequences-for-code-generation"></a>
  73. <p>On some target machines, some standard pattern names for RTL generation
  74. cannot be handled with single insn, but a sequence of RTL insns can
  75. represent them. For these target machines, you can write a
  76. <code>define_expand</code> to specify how to generate the sequence of RTL.
  77. </p>
  78. <a name="index-define_005fexpand"></a>
  79. <p>A <code>define_expand</code> is an RTL expression that looks almost like a
  80. <code>define_insn</code>; but, unlike the latter, a <code>define_expand</code> is used
  81. only for RTL generation and it can produce more than one RTL insn.
  82. </p>
  83. <p>A <code>define_expand</code> RTX has four operands:
  84. </p>
  85. <ul>
  86. <li> The name. Each <code>define_expand</code> must have a name, since the only
  87. use for it is to refer to it by name.
  88. </li><li> The RTL template. This is a vector of RTL expressions representing
  89. a sequence of separate instructions. Unlike <code>define_insn</code>, there
  90. is no implicit surrounding <code>PARALLEL</code>.
  91. </li><li> The condition, a string containing a C expression. This expression is
  92. used to express how the availability of this pattern depends on
  93. subclasses of target machine, selected by command-line options when GCC
  94. is run. This is just like the condition of a <code>define_insn</code> that
  95. has a standard name. Therefore, the condition (if present) may not
  96. depend on the data in the insn being matched, but only the
  97. target-machine-type flags. The compiler needs to test these conditions
  98. during initialization in order to learn exactly which named instructions
  99. are available in a particular run.
  100. </li><li> The preparation statements, a string containing zero or more C
  101. statements which are to be executed before RTL code is generated from
  102. the RTL template.
  103. <p>Usually these statements prepare temporary registers for use as
  104. internal operands in the RTL template, but they can also generate RTL
  105. insns directly by calling routines such as <code>emit_insn</code>, etc.
  106. Any such insns precede the ones that come from the RTL template.
  107. </p>
  108. </li><li> Optionally, a vector containing the values of attributes. See <a href="Insn-Attributes.html#Insn-Attributes">Insn Attributes</a>.
  109. </li></ul>
  110. <p>Every RTL insn emitted by a <code>define_expand</code> must match some
  111. <code>define_insn</code> in the machine description. Otherwise, the compiler
  112. will crash when trying to generate code for the insn or trying to optimize
  113. it.
  114. </p>
  115. <p>The RTL template, in addition to controlling generation of RTL insns,
  116. also describes the operands that need to be specified when this pattern
  117. is used. In particular, it gives a predicate for each operand.
  118. </p>
  119. <p>A true operand, which needs to be specified in order to generate RTL from
  120. the pattern, should be described with a <code>match_operand</code> in its first
  121. occurrence in the RTL template. This enters information on the operand&rsquo;s
  122. predicate into the tables that record such things. GCC uses the
  123. information to preload the operand into a register if that is required for
  124. valid RTL code. If the operand is referred to more than once, subsequent
  125. references should use <code>match_dup</code>.
  126. </p>
  127. <p>The RTL template may also refer to internal &ldquo;operands&rdquo; which are
  128. temporary registers or labels used only within the sequence made by the
  129. <code>define_expand</code>. Internal operands are substituted into the RTL
  130. template with <code>match_dup</code>, never with <code>match_operand</code>. The
  131. values of the internal operands are not passed in as arguments by the
  132. compiler when it requests use of this pattern. Instead, they are computed
  133. within the pattern, in the preparation statements. These statements
  134. compute the values and store them into the appropriate elements of
  135. <code>operands</code> so that <code>match_dup</code> can find them.
  136. </p>
  137. <p>There are two special macros defined for use in the preparation statements:
  138. <code>DONE</code> and <code>FAIL</code>. Use them with a following semicolon,
  139. as a statement.
  140. </p>
  141. <dl compact="compact">
  142. <dd>
  143. <a name="index-DONE"></a>
  144. </dd>
  145. <dt><code>DONE</code></dt>
  146. <dd><p>Use the <code>DONE</code> macro to end RTL generation for the pattern. The
  147. only RTL insns resulting from the pattern on this occasion will be
  148. those already emitted by explicit calls to <code>emit_insn</code> within the
  149. preparation statements; the RTL template will not be generated.
  150. </p>
  151. <a name="index-FAIL"></a>
  152. </dd>
  153. <dt><code>FAIL</code></dt>
  154. <dd><p>Make the pattern fail on this occasion. When a pattern fails, it means
  155. that the pattern was not truly available. The calling routines in the
  156. compiler will try other strategies for code generation using other patterns.
  157. </p>
  158. <p>Failure is currently supported only for binary (addition, multiplication,
  159. shifting, etc.) and bit-field (<code>extv</code>, <code>extzv</code>, and <code>insv</code>)
  160. operations.
  161. </p></dd>
  162. </dl>
  163. <p>If the preparation falls through (invokes neither <code>DONE</code> nor
  164. <code>FAIL</code>), then the <code>define_expand</code> acts like a
  165. <code>define_insn</code> in that the RTL template is used to generate the
  166. insn.
  167. </p>
  168. <p>The RTL template is not used for matching, only for generating the
  169. initial insn list. If the preparation statement always invokes
  170. <code>DONE</code> or <code>FAIL</code>, the RTL template may be reduced to a simple
  171. list of operands, such as this example:
  172. </p>
  173. <div class="smallexample">
  174. <pre class="smallexample">(define_expand &quot;addsi3&quot;
  175. [(match_operand:SI 0 &quot;register_operand&quot; &quot;&quot;)
  176. (match_operand:SI 1 &quot;register_operand&quot; &quot;&quot;)
  177. (match_operand:SI 2 &quot;register_operand&quot; &quot;&quot;)]
  178. </pre><pre class="smallexample"> &quot;&quot;
  179. &quot;
  180. {
  181. handle_add (operands[0], operands[1], operands[2]);
  182. DONE;
  183. }&quot;)
  184. </pre></div>
  185. <p>Here is an example, the definition of left-shift for the SPUR chip:
  186. </p>
  187. <div class="smallexample">
  188. <pre class="smallexample">(define_expand &quot;ashlsi3&quot;
  189. [(set (match_operand:SI 0 &quot;register_operand&quot; &quot;&quot;)
  190. (ashift:SI
  191. </pre><pre class="smallexample"> (match_operand:SI 1 &quot;register_operand&quot; &quot;&quot;)
  192. (match_operand:SI 2 &quot;nonmemory_operand&quot; &quot;&quot;)))]
  193. &quot;&quot;
  194. &quot;
  195. </pre></div>
  196. <div class="smallexample">
  197. <pre class="smallexample">{
  198. if (GET_CODE (operands[2]) != CONST_INT
  199. || (unsigned) INTVAL (operands[2]) &gt; 3)
  200. FAIL;
  201. }&quot;)
  202. </pre></div>
  203. <p>This example uses <code>define_expand</code> so that it can generate an RTL insn
  204. for shifting when the shift-count is in the supported range of 0 to 3 but
  205. fail in other cases where machine insns aren&rsquo;t available. When it fails,
  206. the compiler tries another strategy using different patterns (such as, a
  207. library call).
  208. </p>
  209. <p>If the compiler were able to handle nontrivial condition-strings in
  210. patterns with names, then it would be possible to use a
  211. <code>define_insn</code> in that case. Here is another case (zero-extension
  212. on the 68000) which makes more use of the power of <code>define_expand</code>:
  213. </p>
  214. <div class="smallexample">
  215. <pre class="smallexample">(define_expand &quot;zero_extendhisi2&quot;
  216. [(set (match_operand:SI 0 &quot;general_operand&quot; &quot;&quot;)
  217. (const_int 0))
  218. (set (strict_low_part
  219. (subreg:HI
  220. (match_dup 0)
  221. 0))
  222. (match_operand:HI 1 &quot;general_operand&quot; &quot;&quot;))]
  223. &quot;&quot;
  224. &quot;operands[1] = make_safe_from (operands[1], operands[0]);&quot;)
  225. </pre></div>
  226. <a name="index-make_005fsafe_005ffrom"></a>
  227. <p>Here two RTL insns are generated, one to clear the entire output operand
  228. and the other to copy the input operand into its low half. This sequence
  229. is incorrect if the input operand refers to [the old value of] the output
  230. operand, so the preparation statement makes sure this isn&rsquo;t so. The
  231. function <code>make_safe_from</code> copies the <code>operands[1]</code> into a
  232. temporary register if it refers to <code>operands[0]</code>. It does this
  233. by emitting another RTL insn.
  234. </p>
  235. <p>Finally, a third example shows the use of an internal operand.
  236. Zero-extension on the SPUR chip is done by <code>and</code>-ing the result
  237. against a halfword mask. But this mask cannot be represented by a
  238. <code>const_int</code> because the constant value is too large to be legitimate
  239. on this machine. So it must be copied into a register with
  240. <code>force_reg</code> and then the register used in the <code>and</code>.
  241. </p>
  242. <div class="smallexample">
  243. <pre class="smallexample">(define_expand &quot;zero_extendhisi2&quot;
  244. [(set (match_operand:SI 0 &quot;register_operand&quot; &quot;&quot;)
  245. (and:SI (subreg:SI
  246. (match_operand:HI 1 &quot;register_operand&quot; &quot;&quot;)
  247. 0)
  248. (match_dup 2)))]
  249. &quot;&quot;
  250. &quot;operands[2]
  251. = force_reg (SImode, GEN_INT (65535)); &quot;)
  252. </pre></div>
  253. <p><em>Note:</em> If the <code>define_expand</code> is used to serve a
  254. standard binary or unary arithmetic operation or a bit-field operation,
  255. then the last insn it generates must not be a <code>code_label</code>,
  256. <code>barrier</code> or <code>note</code>. It must be an <code>insn</code>,
  257. <code>jump_insn</code> or <code>call_insn</code>. If you don&rsquo;t need a real insn
  258. at the end, emit an insn to copy the result of the operation into
  259. itself. Such an insn will generate no code, but it can avoid problems
  260. in the compiler.
  261. </p>
  262. <hr>
  263. <div class="header">
  264. <p>
  265. Next: <a href="Insn-Splitting.html#Insn-Splitting" accesskey="n" rel="next">Insn Splitting</a>, Previous: <a href="Insn-Canonicalizations.html#Insn-Canonicalizations" accesskey="p" rel="prev">Insn Canonicalizations</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>
  266. </div>
  267. </body>
  268. </html>