|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
-
- Permission is granted to copy, distribute and/or modify this document
- under the terms of the GNU Free Documentation License, Version 1.3 or
- any later version published by the Free Software Foundation; with the
- Invariant Sections being "Funding Free Software", the Front-Cover
- Texts being (a) (see below), and with the Back-Cover Texts being (b)
- (see below). A copy of the license is included in the section entitled
- "GNU Free Documentation License".
-
- (a) The FSF's Front-Cover Text is:
-
- A GNU Manual
-
- (b) The FSF's Back-Cover Text is:
-
- You have freedom to copy and modify this GNU Manual, like GNU
- software. Copies published by the Free Software Foundation raise
- funds for GNU development. -->
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Guidelines for using poly_int (GNU Compiler Collection (GCC) Internals)</title>
-
- <meta name="description" content="Guidelines for using poly_int (GNU Compiler Collection (GCC) Internals)">
- <meta name="keywords" content="Guidelines for using poly_int (GNU Compiler Collection (GCC) Internals)">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
- <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="poly_005fint.html#poly_005fint" rel="up" title="poly_int">
- <link href="GENERIC.html#GENERIC" rel="next" title="GENERIC">
- <link href="Miscellaneous-poly_005fint-routines.html#Miscellaneous-poly_005fint-routines" rel="prev" title="Miscellaneous poly_int routines">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="Guidelines-for-using-poly_005fint"></a>
- <div class="header">
- <p>
- 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> [<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>
- </div>
- <hr>
- <a name="Guidelines-for-using-poly_005fint-1"></a>
- <h3 class="section">10.9 Guidelines for using <code>poly_int</code></h3>
-
- <p>One of the main design goals of <code>poly_int</code> was to make it easy
- to write target-independent code that handles variable-sized registers
- even when the current target has fixed-sized registers. There are two
- aspects to this:
- </p>
- <ul>
- <li> The set of <code>poly_int</code> operations should be complete enough that
- the question in most cases becomes “Can we do this operation on these
- particular <code>poly_int</code> values? If not, bail out” rather than
- “Are these <code>poly_int</code> values constant? If so, do the operation,
- otherwise bail out”.
-
- </li><li> If target-independent code compiles and runs correctly on a target
- with one value of <code>NUM_POLY_INT_COEFFS</code>, and if the code does not
- use asserting functions like <code>to_constant</code>, it is reasonable to
- assume that the code also works on targets with other values of
- <code>NUM_POLY_INT_COEFFS</code>. There is no need to check this during
- everyday development.
- </li></ul>
-
- <p>So the general principle is: if target-independent code is dealing
- with a <code>poly_int</code> value, it is better to operate on it as a
- <code>poly_int</code> if at all possible, choosing conservatively-correct
- behavior if a particular operation fails. For example, the following
- code handles an index <code>pos</code> into a sequence of vectors that each
- have <code>nunits</code> elements:
- </p>
- <div class="smallexample">
- <pre class="smallexample">/* Calculate which vector contains the result, and which lane of
- that vector we need. */
- if (!can_div_trunc_p (pos, nunits, &vec_entry, &vec_index))
- {
- if (dump_enabled_p ())
- dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
- "Cannot determine which vector holds the"
- " final result.\n");
- return false;
- }
- </pre></div>
-
- <p>However, there are some contexts in which operating on a
- <code>poly_int</code> is not possible or does not make sense. One example
- is when handling static initializers, since no current target supports
- the concept of a variable-length static initializer. In these
- situations, a reasonable fallback is:
- </p>
- <div class="smallexample">
- <pre class="smallexample">if (<var>poly_value</var>.is_constant (&<var>const_value</var>))
- {
- …
- /* Operate on <var>const_value</var>. */
- …
- }
- else
- {
- …
- /* Conservatively correct fallback. */
- …
- }
- </pre></div>
-
- <p><code>poly_int</code> also provides some asserting functions like
- <code>to_constant</code>. Please only use these functions if there is a
- good theoretical reason to believe that the assertion cannot fire.
- For example, if some work is divided into an analysis phase and an
- implementation phase, the analysis phase might reject inputs that are
- not <code>is_constant</code>, in which case the implementation phase can
- reasonably use <code>to_constant</code> on the remaining inputs. The assertions
- should not be used to discover whether a condition ever occurs “in the
- field”; in other words, they should not be used to restrict code to
- constants at first, with the intention of only implementing a
- <code>poly_int</code> version if a user hits the assertion.
- </p>
- <p>If a particular asserting function like <code>to_constant</code> is needed
- more than once for the same reason, it is probably worth adding a
- helper function or macro for that situation, so that the justification
- only needs to be given once. For example:
- </p>
- <div class="smallexample">
- <pre class="smallexample">/* Return the size of an element in a vector of size SIZE, given that
- the vector has NELTS elements. The return value is in the same units
- as SIZE (either bits or bytes).
-
- to_constant () is safe in this situation because vector elements are
- always constant-sized scalars. */
- #define vector_element_size(SIZE, NELTS) \
- (exact_div (SIZE, NELTS).to_constant ())
- </pre></div>
-
- <p>Target-specific code in <samp>config/<var>cpu</var></samp> only needs to handle
- non-constant <code>poly_int</code>s if <code>NUM_POLY_INT_COEFFS</code> is greater
- than one. For other targets, <code>poly_int</code> degenerates to a compile-time
- constant and is often interchangable with a normal scalar integer.
- There are two main exceptions:
- </p>
- <ul>
- <li> Sometimes an explicit cast to an integer type might be needed, such as to
- resolve ambiguities in a <code>?:</code> expression, or when passing values
- through <code>...</code> to things like print functions.
-
- </li><li> Target macros are included in target-independent code and so do not
- have access to the implicit conversion to a scalar integer.
- If this becomes a problem for a particular target macro, the
- possible solutions, in order of preference, are:
-
- <ul>
- <li> Convert the target macro to a target hook (for all targets).
-
- </li><li> Put the target’s implementation of the target macro in its
- <samp><var>cpu</var>.c</samp> file and call it from the target macro in the
- <samp><var>cpu</var>.h</samp> file.
-
- </li><li> Add <code>to_constant ()</code> calls where necessary. The previous option
- is preferable because it will help with any future conversion of the
- macro to a hook.
- </li></ul>
- </li></ul>
-
-
-
- <hr>
- <div class="header">
- <p>
- 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> [<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>
- </div>
-
-
-
- </body>
- </html>
|