Defining-Predicates.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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: Defining Predicates</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Defining Predicates">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Defining Predicates">
  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="Predicates.html#Predicates" rel="up" title="Predicates">
  30. <link href="Constraints.html#Constraints" rel="next" title="Constraints">
  31. <link href="Machine_002dIndependent-Predicates.html#Machine_002dIndependent-Predicates" rel="prev" title="Machine-Independent Predicates">
  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="Defining-Predicates"></a>
  63. <div class="header">
  64. <p>
  65. Previous: <a href="Machine_002dIndependent-Predicates.html#Machine_002dIndependent-Predicates" accesskey="p" rel="prev">Machine-Independent Predicates</a>, Up: <a href="Predicates.html#Predicates" accesskey="u" rel="up">Predicates</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-Machine_002dSpecific-Predicates"></a>
  69. <h4 class="subsection">16.7.2 Defining Machine-Specific Predicates</h4>
  70. <a name="index-defining-predicates"></a>
  71. <a name="index-define_005fpredicate"></a>
  72. <a name="index-define_005fspecial_005fpredicate"></a>
  73. <p>Many machines have requirements for their operands that cannot be
  74. expressed precisely using the generic predicates. You can define
  75. additional predicates using <code>define_predicate</code> and
  76. <code>define_special_predicate</code> expressions. These expressions have
  77. three operands:
  78. </p>
  79. <ul>
  80. <li> The name of the predicate, as it will be referred to in
  81. <code>match_operand</code> or <code>match_operator</code> expressions.
  82. </li><li> An RTL expression which evaluates to true if the predicate allows the
  83. operand <var>op</var>, false if it does not. This expression can only use
  84. the following RTL codes:
  85. <dl compact="compact">
  86. <dt><code>MATCH_OPERAND</code></dt>
  87. <dd><p>When written inside a predicate expression, a <code>MATCH_OPERAND</code>
  88. expression evaluates to true if the predicate it names would allow
  89. <var>op</var>. The operand number and constraint are ignored. Due to
  90. limitations in <code>genrecog</code>, you can only refer to generic
  91. predicates and predicates that have already been defined.
  92. </p>
  93. </dd>
  94. <dt><code>MATCH_CODE</code></dt>
  95. <dd><p>This expression evaluates to true if <var>op</var> or a specified
  96. subexpression of <var>op</var> has one of a given list of RTX codes.
  97. </p>
  98. <p>The first operand of this expression is a string constant containing a
  99. comma-separated list of RTX code names (in lower case). These are the
  100. codes for which the <code>MATCH_CODE</code> will be true.
  101. </p>
  102. <p>The second operand is a string constant which indicates what
  103. subexpression of <var>op</var> to examine. If it is absent or the empty
  104. string, <var>op</var> itself is examined. Otherwise, the string constant
  105. must be a sequence of digits and/or lowercase letters. Each character
  106. indicates a subexpression to extract from the current expression; for
  107. the first character this is <var>op</var>, for the second and subsequent
  108. characters it is the result of the previous character. A digit
  109. <var>n</var> extracts &lsquo;<samp>XEXP&nbsp;(<var>e</var>,&nbsp;<var>n</var>)<!-- /@w --></samp>&rsquo;; a letter <var>l</var>
  110. extracts &lsquo;<samp>XVECEXP&nbsp;(<var>e</var>,&nbsp;0,&nbsp;<var>n</var>)<!-- /@w --></samp>&rsquo; where <var>n</var> is the
  111. alphabetic ordinal of <var>l</var> (0 for &lsquo;a&rsquo;, 1 for &rsquo;b&rsquo;, and so on). The
  112. <code>MATCH_CODE</code> then examines the RTX code of the subexpression
  113. extracted by the complete string. It is not possible to extract
  114. components of an <code>rtvec</code> that is not at position 0 within its RTX
  115. object.
  116. </p>
  117. </dd>
  118. <dt><code>MATCH_TEST</code></dt>
  119. <dd><p>This expression has one operand, a string constant containing a C
  120. expression. The predicate&rsquo;s arguments, <var>op</var> and <var>mode</var>, are
  121. available with those names in the C expression. The <code>MATCH_TEST</code>
  122. evaluates to true if the C expression evaluates to a nonzero value.
  123. <code>MATCH_TEST</code> expressions must not have side effects.
  124. </p>
  125. </dd>
  126. <dt><code>AND</code></dt>
  127. <dt><code>IOR</code></dt>
  128. <dt><code>NOT</code></dt>
  129. <dt><code>IF_THEN_ELSE</code></dt>
  130. <dd><p>The basic &lsquo;<samp>MATCH_</samp>&rsquo; expressions can be combined using these
  131. logical operators, which have the semantics of the C operators
  132. &lsquo;<samp>&amp;&amp;</samp>&rsquo;, &lsquo;<samp>||</samp>&rsquo;, &lsquo;<samp>!</samp>&rsquo;, and &lsquo;<samp>?&nbsp;:<!-- /@w --></samp>&rsquo; respectively. As
  133. in Common Lisp, you may give an <code>AND</code> or <code>IOR</code> expression an
  134. arbitrary number of arguments; this has exactly the same effect as
  135. writing a chain of two-argument <code>AND</code> or <code>IOR</code> expressions.
  136. </p></dd>
  137. </dl>
  138. </li><li> An optional block of C code, which should execute
  139. &lsquo;<samp>return&nbsp;true<!-- /@w --></samp>&rsquo; if the predicate is found to match and
  140. &lsquo;<samp>return&nbsp;false<!-- /@w --></samp>&rsquo; if it does not. It must not have any side
  141. effects. The predicate arguments, <var>op</var> and <var>mode</var>, are
  142. available with those names.
  143. <p>If a code block is present in a predicate definition, then the RTL
  144. expression must evaluate to true <em>and</em> the code block must
  145. execute &lsquo;<samp>return&nbsp;true<!-- /@w --></samp>&rsquo; for the predicate to allow the operand.
  146. The RTL expression is evaluated first; do not re-check anything in the
  147. code block that was checked in the RTL expression.
  148. </p></li></ul>
  149. <p>The program <code>genrecog</code> scans <code>define_predicate</code> and
  150. <code>define_special_predicate</code> expressions to determine which RTX
  151. codes are possibly allowed. You should always make this explicit in
  152. the RTL predicate expression, using <code>MATCH_OPERAND</code> and
  153. <code>MATCH_CODE</code>.
  154. </p>
  155. <p>Here is an example of a simple predicate definition, from the IA64
  156. machine description:
  157. </p>
  158. <div class="smallexample">
  159. <pre class="smallexample">;; <span class="roman">True if <var>op</var> is a <code>SYMBOL_REF</code> which refers to the sdata section.</span>
  160. (define_predicate &quot;small_addr_symbolic_operand&quot;
  161. (and (match_code &quot;symbol_ref&quot;)
  162. (match_test &quot;SYMBOL_REF_SMALL_ADDR_P (op)&quot;)))
  163. </pre></div>
  164. <p>And here is another, showing the use of the C block.
  165. </p>
  166. <div class="smallexample">
  167. <pre class="smallexample">;; <span class="roman">True if <var>op</var> is a register operand that is (or could be) a GR reg.</span>
  168. (define_predicate &quot;gr_register_operand&quot;
  169. (match_operand 0 &quot;register_operand&quot;)
  170. {
  171. unsigned int regno;
  172. if (GET_CODE (op) == SUBREG)
  173. op = SUBREG_REG (op);
  174. regno = REGNO (op);
  175. return (regno &gt;= FIRST_PSEUDO_REGISTER || GENERAL_REGNO_P (regno));
  176. })
  177. </pre></div>
  178. <p>Predicates written with <code>define_predicate</code> automatically include
  179. a test that <var>mode</var> is <code>VOIDmode</code>, or <var>op</var> has the same
  180. mode as <var>mode</var>, or <var>op</var> is a <code>CONST_INT</code> or
  181. <code>CONST_DOUBLE</code>. They do <em>not</em> check specifically for
  182. integer <code>CONST_DOUBLE</code>, nor do they test that the value of either
  183. kind of constant fits in the requested mode. This is because
  184. target-specific predicates that take constants usually have to do more
  185. stringent value checks anyway. If you need the exact same treatment
  186. of <code>CONST_INT</code> or <code>CONST_DOUBLE</code> that the generic predicates
  187. provide, use a <code>MATCH_OPERAND</code> subexpression to call
  188. <code>const_int_operand</code>, <code>const_double_operand</code>, or
  189. <code>immediate_operand</code>.
  190. </p>
  191. <p>Predicates written with <code>define_special_predicate</code> do not get any
  192. automatic mode checks, and are treated as having special mode handling
  193. by <code>genrecog</code>.
  194. </p>
  195. <p>The program <code>genpreds</code> is responsible for generating code to
  196. test predicates. It also writes a header file containing function
  197. declarations for all machine-specific predicates. It is not necessary
  198. to declare these predicates in <samp><var>cpu</var>-protos.h</samp>.
  199. </p>
  200. <hr>
  201. <div class="header">
  202. <p>
  203. Previous: <a href="Machine_002dIndependent-Predicates.html#Machine_002dIndependent-Predicates" accesskey="p" rel="prev">Machine-Independent Predicates</a>, Up: <a href="Predicates.html#Predicates" accesskey="u" rel="up">Predicates</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>
  204. </div>
  205. </body>
  206. </html>