RTL-Template.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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: RTL Template</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: RTL Template">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: RTL Template">
  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="Output-Template.html#Output-Template" rel="next" title="Output Template">
  31. <link href="Example.html#Example" rel="prev" title="Example">
  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="RTL-Template"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Output-Template.html#Output-Template" accesskey="n" rel="next">Output Template</a>, Previous: <a href="Example.html#Example" accesskey="p" rel="prev">Example</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="RTL-Template-1"></a>
  69. <h3 class="section">16.4 RTL Template</h3>
  70. <a name="index-RTL-insn-template"></a>
  71. <a name="index-generating-insns"></a>
  72. <a name="index-insns_002c-generating"></a>
  73. <a name="index-recognizing-insns"></a>
  74. <a name="index-insns_002c-recognizing"></a>
  75. <p>The RTL template is used to define which insns match the particular pattern
  76. and how to find their operands. For named patterns, the RTL template also
  77. says how to construct an insn from specified operands.
  78. </p>
  79. <p>Construction involves substituting specified operands into a copy of the
  80. template. Matching involves determining the values that serve as the
  81. operands in the insn being matched. Both of these activities are
  82. controlled by special expression types that direct matching and
  83. substitution of the operands.
  84. </p>
  85. <dl compact="compact">
  86. <dd><a name="index-match_005foperand"></a>
  87. </dd>
  88. <dt><code>(match_operand:<var>m</var> <var>n</var> <var>predicate</var> <var>constraint</var>)</code></dt>
  89. <dd><p>This expression is a placeholder for operand number <var>n</var> of
  90. the insn. When constructing an insn, operand number <var>n</var>
  91. will be substituted at this point. When matching an insn, whatever
  92. appears at this position in the insn will be taken as operand
  93. number <var>n</var>; but it must satisfy <var>predicate</var> or this instruction
  94. pattern will not match at all.
  95. </p>
  96. <p>Operand numbers must be chosen consecutively counting from zero in
  97. each instruction pattern. There may be only one <code>match_operand</code>
  98. expression in the pattern for each operand number. Usually operands
  99. are numbered in the order of appearance in <code>match_operand</code>
  100. expressions. In the case of a <code>define_expand</code>, any operand numbers
  101. used only in <code>match_dup</code> expressions have higher values than all
  102. other operand numbers.
  103. </p>
  104. <p><var>predicate</var> is a string that is the name of a function that
  105. accepts two arguments, an expression and a machine mode.
  106. See <a href="Predicates.html#Predicates">Predicates</a>. During matching, the function will be called with
  107. the putative operand as the expression and <var>m</var> as the mode
  108. argument (if <var>m</var> is not specified, <code>VOIDmode</code> will be used,
  109. which normally causes <var>predicate</var> to accept any mode). If it
  110. returns zero, this instruction pattern fails to match.
  111. <var>predicate</var> may be an empty string; then it means no test is to be
  112. done on the operand, so anything which occurs in this position is
  113. valid.
  114. </p>
  115. <p>Most of the time, <var>predicate</var> will reject modes other than <var>m</var>&mdash;but
  116. not always. For example, the predicate <code>address_operand</code> uses
  117. <var>m</var> as the mode of memory ref that the address should be valid for.
  118. Many predicates accept <code>const_int</code> nodes even though their mode is
  119. <code>VOIDmode</code>.
  120. </p>
  121. <p><var>constraint</var> controls reloading and the choice of the best register
  122. class to use for a value, as explained later (see <a href="Constraints.html#Constraints">Constraints</a>).
  123. If the constraint would be an empty string, it can be omitted.
  124. </p>
  125. <p>People are often unclear on the difference between the constraint and the
  126. predicate. The predicate helps decide whether a given insn matches the
  127. pattern. The constraint plays no role in this decision; instead, it
  128. controls various decisions in the case of an insn which does match.
  129. </p>
  130. <a name="index-match_005fscratch"></a>
  131. </dd>
  132. <dt><code>(match_scratch:<var>m</var> <var>n</var> <var>constraint</var>)</code></dt>
  133. <dd><p>This expression is also a placeholder for operand number <var>n</var>
  134. and indicates that operand must be a <code>scratch</code> or <code>reg</code>
  135. expression.
  136. </p>
  137. <p>When matching patterns, this is equivalent to
  138. </p>
  139. <div class="smallexample">
  140. <pre class="smallexample">(match_operand:<var>m</var> <var>n</var> &quot;scratch_operand&quot; <var>constraint</var>)
  141. </pre></div>
  142. <p>but, when generating RTL, it produces a (<code>scratch</code>:<var>m</var>)
  143. expression.
  144. </p>
  145. <p>If the last few expressions in a <code>parallel</code> are <code>clobber</code>
  146. expressions whose operands are either a hard register or
  147. <code>match_scratch</code>, the combiner can add or delete them when
  148. necessary. See <a href="Side-Effects.html#Side-Effects">Side Effects</a>.
  149. </p>
  150. <a name="index-match_005fdup"></a>
  151. </dd>
  152. <dt><code>(match_dup <var>n</var>)</code></dt>
  153. <dd><p>This expression is also a placeholder for operand number <var>n</var>.
  154. It is used when the operand needs to appear more than once in the
  155. insn.
  156. </p>
  157. <p>In construction, <code>match_dup</code> acts just like <code>match_operand</code>:
  158. the operand is substituted into the insn being constructed. But in
  159. matching, <code>match_dup</code> behaves differently. It assumes that operand
  160. number <var>n</var> has already been determined by a <code>match_operand</code>
  161. appearing earlier in the recognition template, and it matches only an
  162. identical-looking expression.
  163. </p>
  164. <p>Note that <code>match_dup</code> should not be used to tell the compiler that
  165. a particular register is being used for two operands (example:
  166. <code>add</code> that adds one register to another; the second register is
  167. both an input operand and the output operand). Use a matching
  168. constraint (see <a href="Simple-Constraints.html#Simple-Constraints">Simple Constraints</a>) for those. <code>match_dup</code> is for the cases where one
  169. operand is used in two places in the template, such as an instruction
  170. that computes both a quotient and a remainder, where the opcode takes
  171. two input operands but the RTL template has to refer to each of those
  172. twice; once for the quotient pattern and once for the remainder pattern.
  173. </p>
  174. <a name="index-match_005foperator"></a>
  175. </dd>
  176. <dt><code>(match_operator:<var>m</var> <var>n</var> <var>predicate</var> [<var>operands</var>&hellip;])</code></dt>
  177. <dd><p>This pattern is a kind of placeholder for a variable RTL expression
  178. code.
  179. </p>
  180. <p>When constructing an insn, it stands for an RTL expression whose
  181. expression code is taken from that of operand <var>n</var>, and whose
  182. operands are constructed from the patterns <var>operands</var>.
  183. </p>
  184. <p>When matching an expression, it matches an expression if the function
  185. <var>predicate</var> returns nonzero on that expression <em>and</em> the
  186. patterns <var>operands</var> match the operands of the expression.
  187. </p>
  188. <p>Suppose that the function <code>commutative_operator</code> is defined as
  189. follows, to match any expression whose operator is one of the
  190. commutative arithmetic operators of RTL and whose mode is <var>mode</var>:
  191. </p>
  192. <div class="smallexample">
  193. <pre class="smallexample">int
  194. commutative_integer_operator (x, mode)
  195. rtx x;
  196. machine_mode mode;
  197. {
  198. enum rtx_code code = GET_CODE (x);
  199. if (GET_MODE (x) != mode)
  200. return 0;
  201. return (GET_RTX_CLASS (code) == RTX_COMM_ARITH
  202. || code == EQ || code == NE);
  203. }
  204. </pre></div>
  205. <p>Then the following pattern will match any RTL expression consisting
  206. of a commutative operator applied to two general operands:
  207. </p>
  208. <div class="smallexample">
  209. <pre class="smallexample">(match_operator:SI 3 &quot;commutative_operator&quot;
  210. [(match_operand:SI 1 &quot;general_operand&quot; &quot;g&quot;)
  211. (match_operand:SI 2 &quot;general_operand&quot; &quot;g&quot;)])
  212. </pre></div>
  213. <p>Here the vector <code>[<var>operands</var>&hellip;]</code> contains two patterns
  214. because the expressions to be matched all contain two operands.
  215. </p>
  216. <p>When this pattern does match, the two operands of the commutative
  217. operator are recorded as operands 1 and 2 of the insn. (This is done
  218. by the two instances of <code>match_operand</code>.) Operand 3 of the insn
  219. will be the entire commutative expression: use <code>GET_CODE
  220. (operands[3])</code> to see which commutative operator was used.
  221. </p>
  222. <p>The machine mode <var>m</var> of <code>match_operator</code> works like that of
  223. <code>match_operand</code>: it is passed as the second argument to the
  224. predicate function, and that function is solely responsible for
  225. deciding whether the expression to be matched &ldquo;has&rdquo; that mode.
  226. </p>
  227. <p>When constructing an insn, argument 3 of the gen-function will specify
  228. the operation (i.e. the expression code) for the expression to be
  229. made. It should be an RTL expression, whose expression code is copied
  230. into a new expression whose operands are arguments 1 and 2 of the
  231. gen-function. The subexpressions of argument 3 are not used;
  232. only its expression code matters.
  233. </p>
  234. <p>When <code>match_operator</code> is used in a pattern for matching an insn,
  235. it usually best if the operand number of the <code>match_operator</code>
  236. is higher than that of the actual operands of the insn. This improves
  237. register allocation because the register allocator often looks at
  238. operands 1 and 2 of insns to see if it can do register tying.
  239. </p>
  240. <p>There is no way to specify constraints in <code>match_operator</code>. The
  241. operand of the insn which corresponds to the <code>match_operator</code>
  242. never has any constraints because it is never reloaded as a whole.
  243. However, if parts of its <var>operands</var> are matched by
  244. <code>match_operand</code> patterns, those parts may have constraints of
  245. their own.
  246. </p>
  247. <a name="index-match_005fop_005fdup"></a>
  248. </dd>
  249. <dt><code>(match_op_dup:<var>m</var> <var>n</var>[<var>operands</var>&hellip;])</code></dt>
  250. <dd><p>Like <code>match_dup</code>, except that it applies to operators instead of
  251. operands. When constructing an insn, operand number <var>n</var> will be
  252. substituted at this point. But in matching, <code>match_op_dup</code> behaves
  253. differently. It assumes that operand number <var>n</var> has already been
  254. determined by a <code>match_operator</code> appearing earlier in the
  255. recognition template, and it matches only an identical-looking
  256. expression.
  257. </p>
  258. <a name="index-match_005fparallel"></a>
  259. </dd>
  260. <dt><code>(match_parallel <var>n</var> <var>predicate</var> [<var>subpat</var>&hellip;])</code></dt>
  261. <dd><p>This pattern is a placeholder for an insn that consists of a
  262. <code>parallel</code> expression with a variable number of elements. This
  263. expression should only appear at the top level of an insn pattern.
  264. </p>
  265. <p>When constructing an insn, operand number <var>n</var> will be substituted at
  266. this point. When matching an insn, it matches if the body of the insn
  267. is a <code>parallel</code> expression with at least as many elements as the
  268. vector of <var>subpat</var> expressions in the <code>match_parallel</code>, if each
  269. <var>subpat</var> matches the corresponding element of the <code>parallel</code>,
  270. <em>and</em> the function <var>predicate</var> returns nonzero on the
  271. <code>parallel</code> that is the body of the insn. It is the responsibility
  272. of the predicate to validate elements of the <code>parallel</code> beyond
  273. those listed in the <code>match_parallel</code>.
  274. </p>
  275. <p>A typical use of <code>match_parallel</code> is to match load and store
  276. multiple expressions, which can contain a variable number of elements
  277. in a <code>parallel</code>. For example,
  278. </p>
  279. <div class="smallexample">
  280. <pre class="smallexample">(define_insn &quot;&quot;
  281. [(match_parallel 0 &quot;load_multiple_operation&quot;
  282. [(set (match_operand:SI 1 &quot;gpc_reg_operand&quot; &quot;=r&quot;)
  283. (match_operand:SI 2 &quot;memory_operand&quot; &quot;m&quot;))
  284. (use (reg:SI 179))
  285. (clobber (reg:SI 179))])]
  286. &quot;&quot;
  287. &quot;loadm 0,0,%1,%2&quot;)
  288. </pre></div>
  289. <p>This example comes from <samp>a29k.md</samp>. The function
  290. <code>load_multiple_operation</code> is defined in <samp>a29k.c</samp> and checks
  291. that subsequent elements in the <code>parallel</code> are the same as the
  292. <code>set</code> in the pattern, except that they are referencing subsequent
  293. registers and memory locations.
  294. </p>
  295. <p>An insn that matches this pattern might look like:
  296. </p>
  297. <div class="smallexample">
  298. <pre class="smallexample">(parallel
  299. [(set (reg:SI 20) (mem:SI (reg:SI 100)))
  300. (use (reg:SI 179))
  301. (clobber (reg:SI 179))
  302. (set (reg:SI 21)
  303. (mem:SI (plus:SI (reg:SI 100)
  304. (const_int 4))))
  305. (set (reg:SI 22)
  306. (mem:SI (plus:SI (reg:SI 100)
  307. (const_int 8))))])
  308. </pre></div>
  309. <a name="index-match_005fpar_005fdup"></a>
  310. </dd>
  311. <dt><code>(match_par_dup <var>n</var> [<var>subpat</var>&hellip;])</code></dt>
  312. <dd><p>Like <code>match_op_dup</code>, but for <code>match_parallel</code> instead of
  313. <code>match_operator</code>.
  314. </p>
  315. </dd>
  316. </dl>
  317. <hr>
  318. <div class="header">
  319. <p>
  320. Next: <a href="Output-Template.html#Output-Template" accesskey="n" rel="next">Output Template</a>, Previous: <a href="Example.html#Example" accesskey="p" rel="prev">Example</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>
  321. </div>
  322. </body>
  323. </html>