You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
9.1KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Guidelines for using poly_int (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Guidelines for using poly_int (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Guidelines for using poly_int (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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="poly_005fint.html#poly_005fint" rel="up" title="poly_int">
  30. <link href="GENERIC.html#GENERIC" rel="next" title="GENERIC">
  31. <link href="Miscellaneous-poly_005fint-routines.html#Miscellaneous-poly_005fint-routines" rel="prev" title="Miscellaneous poly_int routines">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Guidelines-for-using-poly_005fint"></a>
  62. <div class="header">
  63. <p>
  64. Previous: <a href="Miscellaneous-poly_005fint-routines.html#Miscellaneous-poly_005fint-routines" accesskey="p" rel="prev">Miscellaneous <code>poly_int</code> routines</a>, Up: <a href="poly_005fint.html#poly_005fint" accesskey="u" rel="up">poly_int</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>
  65. </div>
  66. <hr>
  67. <a name="Guidelines-for-using-poly_005fint-1"></a>
  68. <h3 class="section">10.9 Guidelines for using <code>poly_int</code></h3>
  69. <p>One of the main design goals of <code>poly_int</code> was to make it easy
  70. to write target-independent code that handles variable-sized registers
  71. even when the current target has fixed-sized registers. There are two
  72. aspects to this:
  73. </p>
  74. <ul>
  75. <li> The set of <code>poly_int</code> operations should be complete enough that
  76. the question in most cases becomes &ldquo;Can we do this operation on these
  77. particular <code>poly_int</code> values? If not, bail out&rdquo; rather than
  78. &ldquo;Are these <code>poly_int</code> values constant? If so, do the operation,
  79. otherwise bail out&rdquo;.
  80. </li><li> If target-independent code compiles and runs correctly on a target
  81. with one value of <code>NUM_POLY_INT_COEFFS</code>, and if the code does not
  82. use asserting functions like <code>to_constant</code>, it is reasonable to
  83. assume that the code also works on targets with other values of
  84. <code>NUM_POLY_INT_COEFFS</code>. There is no need to check this during
  85. everyday development.
  86. </li></ul>
  87. <p>So the general principle is: if target-independent code is dealing
  88. with a <code>poly_int</code> value, it is better to operate on it as a
  89. <code>poly_int</code> if at all possible, choosing conservatively-correct
  90. behavior if a particular operation fails. For example, the following
  91. code handles an index <code>pos</code> into a sequence of vectors that each
  92. have <code>nunits</code> elements:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">/* Calculate which vector contains the result, and which lane of
  96. that vector we need. */
  97. if (!can_div_trunc_p (pos, nunits, &amp;vec_entry, &amp;vec_index))
  98. {
  99. if (dump_enabled_p ())
  100. dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
  101. &quot;Cannot determine which vector holds the&quot;
  102. &quot; final result.\n&quot;);
  103. return false;
  104. }
  105. </pre></div>
  106. <p>However, there are some contexts in which operating on a
  107. <code>poly_int</code> is not possible or does not make sense. One example
  108. is when handling static initializers, since no current target supports
  109. the concept of a variable-length static initializer. In these
  110. situations, a reasonable fallback is:
  111. </p>
  112. <div class="smallexample">
  113. <pre class="smallexample">if (<var>poly_value</var>.is_constant (&amp;<var>const_value</var>))
  114. {
  115. &hellip;
  116. /* Operate on <var>const_value</var>. */
  117. &hellip;
  118. }
  119. else
  120. {
  121. &hellip;
  122. /* Conservatively correct fallback. */
  123. &hellip;
  124. }
  125. </pre></div>
  126. <p><code>poly_int</code> also provides some asserting functions like
  127. <code>to_constant</code>. Please only use these functions if there is a
  128. good theoretical reason to believe that the assertion cannot fire.
  129. For example, if some work is divided into an analysis phase and an
  130. implementation phase, the analysis phase might reject inputs that are
  131. not <code>is_constant</code>, in which case the implementation phase can
  132. reasonably use <code>to_constant</code> on the remaining inputs. The assertions
  133. should not be used to discover whether a condition ever occurs &ldquo;in the
  134. field&rdquo;; in other words, they should not be used to restrict code to
  135. constants at first, with the intention of only implementing a
  136. <code>poly_int</code> version if a user hits the assertion.
  137. </p>
  138. <p>If a particular asserting function like <code>to_constant</code> is needed
  139. more than once for the same reason, it is probably worth adding a
  140. helper function or macro for that situation, so that the justification
  141. only needs to be given once. For example:
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample">/* Return the size of an element in a vector of size SIZE, given that
  145. the vector has NELTS elements. The return value is in the same units
  146. as SIZE (either bits or bytes).
  147. to_constant () is safe in this situation because vector elements are
  148. always constant-sized scalars. */
  149. #define vector_element_size(SIZE, NELTS) \
  150. (exact_div (SIZE, NELTS).to_constant ())
  151. </pre></div>
  152. <p>Target-specific code in <samp>config/<var>cpu</var></samp> only needs to handle
  153. non-constant <code>poly_int</code>s if <code>NUM_POLY_INT_COEFFS</code> is greater
  154. than one. For other targets, <code>poly_int</code> degenerates to a compile-time
  155. constant and is often interchangable with a normal scalar integer.
  156. There are two main exceptions:
  157. </p>
  158. <ul>
  159. <li> Sometimes an explicit cast to an integer type might be needed, such as to
  160. resolve ambiguities in a <code>?:</code> expression, or when passing values
  161. through <code>...</code> to things like print functions.
  162. </li><li> Target macros are included in target-independent code and so do not
  163. have access to the implicit conversion to a scalar integer.
  164. If this becomes a problem for a particular target macro, the
  165. possible solutions, in order of preference, are:
  166. <ul>
  167. <li> Convert the target macro to a target hook (for all targets).
  168. </li><li> Put the target&rsquo;s implementation of the target macro in its
  169. <samp><var>cpu</var>.c</samp> file and call it from the target macro in the
  170. <samp><var>cpu</var>.h</samp> file.
  171. </li><li> Add <code>to_constant ()</code> calls where necessary. The previous option
  172. is preferable because it will help with any future conversion of the
  173. macro to a hook.
  174. </li></ul>
  175. </li></ul>
  176. <hr>
  177. <div class="header">
  178. <p>
  179. Previous: <a href="Miscellaneous-poly_005fint-routines.html#Miscellaneous-poly_005fint-routines" accesskey="p" rel="prev">Miscellaneous <code>poly_int</code> routines</a>, Up: <a href="poly_005fint.html#poly_005fint" accesskey="u" rel="up">poly_int</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>
  180. </div>
  181. </body>
  182. </html>