[NDS32] new attribute no_prologue and new option -mret-in-naked-func.
[official-gcc.git] / gcc / doc / poly-int.texi
blob1023e823cb3aaf7713f242cae4751f1929bbef25
1 @node poly_int
2 @chapter Sizes and offsets as runtime invariants
3 @cindex polynomial integers
4 @findex poly_int
6 GCC allows the size of a hardware register to be a runtime invariant
7 rather than a compile-time constant.  This in turn means that various
8 sizes and offsets must also be runtime invariants rather than
9 compile-time constants, such as:
11 @itemize @bullet
12 @item
13 the size of a general @code{machine_mode} (@pxref{Machine Modes});
15 @item
16 the size of a spill slot;
18 @item
19 the offset of something within a stack frame;
21 @item
22 the number of elements in a vector;
24 @item
25 the size and offset of a @code{mem} rtx (@pxref{Regs and Memory}); and
27 @item
28 the byte offset in a @code{subreg} rtx (@pxref{Regs and Memory}).
29 @end itemize
31 The motivating example is the Arm SVE ISA, whose vector registers can be
32 any multiple of 128 bits between 128 and 2048 inclusive.  The compiler
33 normally produces code that works for all SVE register sizes, with the
34 actual size only being known at runtime.
36 GCC's main representation of such runtime invariants is the
37 @code{poly_int} class.  This chapter describes what @code{poly_int}
38 does, lists the available operations, and gives some general
39 usage guidelines.
41 @menu
42 * Overview of @code{poly_int}::
43 * Consequences of using @code{poly_int}::
44 * Comparisons involving @code{poly_int}::
45 * Arithmetic on @code{poly_int}s::
46 * Alignment of @code{poly_int}s::
47 * Computing bounds on @code{poly_int}s::
48 * Converting @code{poly_int}s::
49 * Miscellaneous @code{poly_int} routines::
50 * Guidelines for using @code{poly_int}::
51 @end menu
53 @node Overview of @code{poly_int}
54 @section Overview of @code{poly_int}
56 @cindex @code{poly_int}, runtime value
57 We define indeterminates @var{x1}, @dots{}, @var{xn} whose values are
58 only known at runtime and use polynomials of the form:
60 @smallexample
61 @var{c0} + @var{c1} * @var{x1} + @dots{} + @var{cn} * @var{xn}
62 @end smallexample
64 to represent a size or offset whose value might depend on some
65 of these indeterminates.  The coefficients @var{c0}, @dots{}, @var{cn}
66 are always known at compile time, with the @var{c0} term being the
67 ``constant'' part that does not depend on any runtime value.
69 GCC uses the @code{poly_int} class to represent these coefficients.
70 The class has two template parameters: the first specifies the number of
71 coefficients (@var{n} + 1) and the second specifies the type of the
72 coefficients.  For example, @samp{poly_int<2, unsigned short>} represents
73 a polynomial with two coefficients (and thus one indeterminate), with each
74 coefficient having type @code{unsigned short}.  When @var{n} is 0,
75 the class degenerates to a single compile-time constant @var{c0}.
77 @cindex @code{poly_int}, template parameters
78 @findex NUM_POLY_INT_COEFFS
79 The number of coefficients needed for compilation is a fixed
80 property of each target and is specified by the configuration macro
81 @code{NUM_POLY_INT_COEFFS}.  The default value is 1, since most targets
82 do not have such runtime invariants.  Targets that need a different
83 value should @code{#define} the macro in their @file{@var{cpu}-modes.def}
84 file.  @xref{Back End}.
86 @cindex @code{poly_int}, invariant range
87 @code{poly_int} makes the simplifying requirement that each indeterminate
88 must be a nonnegative integer.  An indeterminate value of 0 should usually
89 represent the minimum possible runtime value, with @var{c0} specifying
90 the value in that case.
92 For example, when targetting the Arm SVE ISA, the single indeterminate
93 represents the number of 128-bit blocks in a vector @emph{beyond the minimum
94 length of 128 bits}.  Thus the number of 64-bit doublewords in a vector
95 is 2 + 2 * @var{x1}.  If an aggregate has a single SVE vector and 16
96 additional bytes, its total size is 32 + 16 * @var{x1} bytes.
98 The header file @file{poly-int-types.h} provides typedefs for the
99 most common forms of @code{poly_int}, all having
100 @code{NUM_POLY_INT_COEFFS} coefficients:
102 @cindex @code{poly_int}, main typedefs
103 @table @code
104 @item poly_uint16
105 a @samp{poly_int} with @code{unsigned short} coefficients.
107 @item poly_int64
108 a @samp{poly_int} with @code{HOST_WIDE_INT} coefficients.
110 @item poly_uint64
111 a @samp{poly_int} with @code{unsigned HOST_WIDE_INT} coefficients.
113 @item poly_offset_int
114 a @samp{poly_int} with @code{offset_int} coefficients.
116 @item poly_wide_int
117 a @samp{poly_int} with @code{wide_int} coefficients.
119 @item poly_widest_int
120 a @samp{poly_int} with @code{widest_int} coefficients.
121 @end table
123 Since the main purpose of @code{poly_int} is to represent sizes and
124 offsets, the last two typedefs are only rarely used.
126 @node Consequences of using @code{poly_int}
127 @section Consequences of using @code{poly_int}
129 The two main consequences of using polynomial sizes and offsets are that:
131 @itemize
132 @item
133 there is no total ordering between the values at compile time, and
135 @item
136 some operations might yield results that cannot be expressed as a
137 @code{poly_int}.
138 @end itemize
140 For example, if @var{x} is a runtime invariant, we cannot tell at
141 compile time whether:
143 @smallexample
144 3 + 4@var{x} <= 1 + 5@var{x}
145 @end smallexample
147 since the condition is false when @var{x} <= 1 and true when @var{x} >= 2.
149 Similarly, @code{poly_int} cannot represent the result of:
151 @smallexample
152 (3 + 4@var{x}) * (1 + 5@var{x})
153 @end smallexample
155 since it cannot (and in practice does not need to) store powers greater
156 than one.  It also cannot represent the result of:
158 @smallexample
159 (3 + 4@var{x}) / (1 + 5@var{x})
160 @end smallexample
162 The following sections describe how we deal with these restrictions.
164 @cindex @code{poly_int}, use in target-independent code
165 As described earlier, a @code{poly_int<1, @var{T}>} has no indeterminates
166 and so degenerates to a compile-time constant of type @var{T}.  It would
167 be possible in that case to do all normal arithmetic on the @var{T},
168 and to compare the @var{T} using the normal C++ operators.  We deliberately
169 prevent target-independent code from doing this, since the compiler needs
170 to support other @code{poly_int<@var{n}, @var{T}>} as well, regardless of
171 the current target's @code{NUM_POLY_INT_COEFFS}.
173 @cindex @code{poly_int}, use in target-specific code
174 However, it would be very artificial to force target-specific code
175 to follow these restrictions if the target has no runtime indeterminates.
176 There is therefore an implicit conversion from @code{poly_int<1, @var{T}>}
177 to @var{T} when compiling target-specific translation units.
179 @node Comparisons involving @code{poly_int}
180 @section Comparisons involving @code{poly_int}
182 In general we need to compare sizes and offsets in two situations:
183 those in which the values need to be ordered, and those in which
184 the values can be unordered.  More loosely, the distinction is often
185 between values that have a definite link (usually because they refer to the
186 same underlying register or memory location) and values that have
187 no definite link.  An example of the former is the relationship between
188 the inner and outer sizes of a subreg, where we must know at compile time
189 whether the subreg is paradoxical, partial, or complete.  An example of
190 the latter is alias analysis: we might want to check whether two
191 arbitrary memory references overlap.
193 Referring back to the examples in the previous section, it makes sense
194 to ask whether a memory reference of size @samp{3 + 4@var{x}} overlaps
195 one of size @samp{1 + 5@var{x}}, but it does not make sense to have a
196 subreg in which the outer mode has @samp{3 + 4@var{x}} bytes and the
197 inner mode has @samp{1 + 5@var{x}} bytes (or vice versa).  Such subregs
198 are always invalid and should trigger an internal compiler error
199 if formed.
201 The underlying operators are the same in both cases, but the distinction
202 affects how they are used.
204 @menu
205 * Comparison functions for @code{poly_int}::
206 * Properties of the @code{poly_int} comparisons::
207 * Comparing potentially-unordered @code{poly_int}s::
208 * Comparing ordered @code{poly_int}s::
209 * Checking for a @code{poly_int} marker value::
210 * Range checks on @code{poly_int}s::
211 * Sorting @code{poly_int}s::
212 @end menu
214 @node Comparison functions for @code{poly_int}
215 @subsection Comparison functions for @code{poly_int}
217 @code{poly_int} provides the following routines for checking whether
218 a particular condition ``may be'' (might be) true:
220 @example
221 maybe_lt maybe_le maybe_eq maybe_ge maybe_gt
222                   maybe_ne
223 @end example
225 The functions have their natural meaning:
227 @table @samp
228 @item maybe_lt(@var{a}, @var{b})
229 Return true if @var{a} might be less than @var{b}.
231 @item maybe_le(@var{a}, @var{b})
232 Return true if @var{a} might be less than or equal to @var{b}.
234 @item maybe_eq(@var{a}, @var{b})
235 Return true if @var{a} might be equal to @var{b}.
237 @item maybe_ne(@var{a}, @var{b})
238 Return true if @var{a} might not be equal to @var{b}.
240 @item maybe_ge(@var{a}, @var{b})
241 Return true if @var{a} might be greater than or equal to @var{b}.
243 @item maybe_gt(@var{a}, @var{b})
244 Return true if @var{a} might be greater than @var{b}.
245 @end table
247 For readability, @code{poly_int} also provides ``known'' inverses of these
248 functions:
250 @example
251 known_lt (@var{a}, @var{b}) == !maybe_ge (@var{a}, @var{b})
252 known_le (@var{a}, @var{b}) == !maybe_gt (@var{a}, @var{b})
253 known_eq (@var{a}, @var{b}) == !maybe_ne (@var{a}, @var{b})
254 known_ge (@var{a}, @var{b}) == !maybe_lt (@var{a}, @var{b})
255 known_gt (@var{a}, @var{b}) == !maybe_le (@var{a}, @var{b})
256 known_ne (@var{a}, @var{b}) == !maybe_eq (@var{a}, @var{b})
257 @end example
259 @node Properties of the @code{poly_int} comparisons
260 @subsection Properties of the @code{poly_int} comparisons
262 All ``maybe'' relations except @code{maybe_ne} are transitive, so for example:
264 @smallexample
265 maybe_lt (@var{a}, @var{b}) && maybe_lt (@var{b}, @var{c}) implies maybe_lt (@var{a}, @var{c})
266 @end smallexample
268 for all @var{a}, @var{b} and @var{c}.  @code{maybe_lt}, @code{maybe_gt}
269 and @code{maybe_ne} are irreflexive, so for example:
271 @smallexample
272 !maybe_lt (@var{a}, @var{a})
273 @end smallexample
275 is true for all @var{a}.  @code{maybe_le}, @code{maybe_eq} and @code{maybe_ge}
276 are reflexive, so for example:
278 @smallexample
279 maybe_le (@var{a}, @var{a})
280 @end smallexample
282 is true for all @var{a}.  @code{maybe_eq} and @code{maybe_ne} are symmetric, so:
284 @smallexample
285 maybe_eq (@var{a}, @var{b}) == maybe_eq (@var{b}, @var{a})
286 maybe_ne (@var{a}, @var{b}) == maybe_ne (@var{b}, @var{a})
287 @end smallexample
289 for all @var{a} and @var{b}.  In addition:
291 @smallexample
292 maybe_le (@var{a}, @var{b}) == maybe_lt (@var{a}, @var{b}) || maybe_eq (@var{a}, @var{b})
293 maybe_ge (@var{a}, @var{b}) == maybe_gt (@var{a}, @var{b}) || maybe_eq (@var{a}, @var{b})
294 maybe_lt (@var{a}, @var{b}) == maybe_gt (@var{b}, @var{a})
295 maybe_le (@var{a}, @var{b}) == maybe_ge (@var{b}, @var{a})
296 @end smallexample
298 However:
300 @smallexample
301 maybe_le (@var{a}, @var{b}) && maybe_le (@var{b}, @var{a}) does not imply !maybe_ne (@var{a}, @var{b}) [== known_eq (@var{a}, @var{b})]
302 maybe_ge (@var{a}, @var{b}) && maybe_ge (@var{b}, @var{a}) does not imply !maybe_ne (@var{a}, @var{b}) [== known_eq (@var{a}, @var{b})]
303 @end smallexample
305 One example is again @samp{@var{a} == 3 + 4@var{x}}
306 and @samp{@var{b} == 1 + 5@var{x}}, where @samp{maybe_le (@var{a}, @var{b})},
307 @samp{maybe_ge (@var{a}, @var{b})} and @samp{maybe_ne (@var{a}, @var{b})}
308 all hold.  @code{maybe_le} and @code{maybe_ge} are therefore not antisymetric
309 and do not form a partial order.
311 From the above, it follows that:
313 @itemize @bullet
314 @item
315 All ``known'' relations except @code{known_ne} are transitive.
317 @item
318 @code{known_lt}, @code{known_ne} and @code{known_gt} are irreflexive.
320 @item
321 @code{known_le}, @code{known_eq} and @code{known_ge} are reflexive.
322 @end itemize
324 Also:
326 @smallexample
327 known_lt (@var{a}, @var{b}) == known_gt (@var{b}, @var{a})
328 known_le (@var{a}, @var{b}) == known_ge (@var{b}, @var{a})
329 known_lt (@var{a}, @var{b}) implies !known_lt (@var{b}, @var{a})  [asymmetry]
330 known_gt (@var{a}, @var{b}) implies !known_gt (@var{b}, @var{a})
331 known_le (@var{a}, @var{b}) && known_le (@var{b}, @var{a}) == known_eq (@var{a}, @var{b}) [== !maybe_ne (@var{a}, @var{b})]
332 known_ge (@var{a}, @var{b}) && known_ge (@var{b}, @var{a}) == known_eq (@var{a}, @var{b}) [== !maybe_ne (@var{a}, @var{b})]
333 @end smallexample
335 @code{known_le} and @code{known_ge} are therefore antisymmetric and are
336 partial orders.  However:
338 @smallexample
339 known_le (@var{a}, @var{b}) does not imply known_lt (@var{a}, @var{b}) || known_eq (@var{a}, @var{b})
340 known_ge (@var{a}, @var{b}) does not imply known_gt (@var{a}, @var{b}) || known_eq (@var{a}, @var{b})
341 @end smallexample
343 For example, @samp{known_le (4, 4 + 4@var{x})} holds because the runtime
344 indeterminate @var{x} is a nonnegative integer, but neither
345 @code{known_lt (4, 4 + 4@var{x})} nor @code{known_eq (4, 4 + 4@var{x})} hold.
347 @node Comparing potentially-unordered @code{poly_int}s
348 @subsection Comparing potentially-unordered @code{poly_int}s
350 In cases where there is no definite link between two @code{poly_int}s,
351 we can usually make a conservatively-correct assumption.  For example,
352 the conservative assumption for alias analysis is that two references
353 @emph{might} alias.
355 One way of checking whether [@var{begin1}, @var{end1}) might overlap
356 [@var{begin2}, @var{end2}) using the @code{poly_int} comparisons is:
358 @smallexample
359 maybe_gt (@var{end1}, @var{begin2}) && maybe_gt (@var{end2}, @var{begin1})
360 @end smallexample
362 and another (equivalent) way is:
364 @smallexample
365 !(known_le (@var{end1}, @var{begin2}) || known_le (@var{end2}, @var{begin1}))
366 @end smallexample
368 However, in this particular example, it is better to use the range helper
369 functions instead.  @xref{Range checks on @code{poly_int}s}.
371 @node Comparing ordered @code{poly_int}s
372 @subsection Comparing ordered @code{poly_int}s
374 In cases where there is a definite link between two @code{poly_int}s,
375 such as the outer and inner sizes of subregs, we usually require the sizes
376 to be ordered by the @code{known_le} partial order.  @code{poly_int} provides
377 the following utility functions for ordered values:
379 @table @samp
380 @item ordered_p (@var{a}, @var{b})
381 Return true if @var{a} and @var{b} are ordered by the @code{known_le}
382 partial order.
384 @item ordered_min (@var{a}, @var{b})
385 Assert that @var{a} and @var{b} are ordered by @code{known_le} and return the
386 minimum of the two.  When using this function, please add a comment explaining
387 why the values are known to be ordered.
389 @item ordered_max (@var{a}, @var{b})
390 Assert that @var{a} and @var{b} are ordered by @code{known_le} and return the
391 maximum of the two.  When using this function, please add a comment explaining
392 why the values are known to be ordered.
393 @end table
395 For example, if a subreg has an outer mode of size @var{outer} and an
396 inner mode of size @var{inner}:
398 @itemize @bullet
399 @item
400 the subreg is complete if known_eq (@var{inner}, @var{outer})
402 @item
403 otherwise, the subreg is paradoxical if known_le (@var{inner}, @var{outer})
405 @item
406 otherwise, the subreg is partial if known_le (@var{outer}, @var{inner})
408 @item
409 otherwise, the subreg is ill-formed
410 @end itemize
412 Thus the subreg is only valid if
413 @samp{ordered_p (@var{outer}, @var{inner})} is true.  If this condition
414 is already known to be true then:
416 @itemize @bullet
417 @item
418 the subreg is complete if known_eq (@var{inner}, @var{outer})
420 @item
421 the subreg is paradoxical if maybe_lt (@var{inner}, @var{outer})
423 @item
424 the subreg is partial if maybe_lt (@var{outer}, @var{inner})
425 @end itemize
427 with the three conditions being mutually exclusive.
429 Code that checks whether a subreg is valid would therefore generally
430 check whether @code{ordered_p} holds (in addition to whatever other
431 checks are required for subreg validity).  Code that is dealing
432 with existing subregs can assert that @code{ordered_p} holds
433 and use either of the classifications above.
435 @node Checking for a @code{poly_int} marker value
436 @subsection Checking for a @code{poly_int} marker value
438 It is sometimes useful to have a special ``marker value'' that is not
439 meant to be taken literally.  For example, some code uses a size
440 of -1 to represent an unknown size, rather than having to carry around
441 a separate boolean to say whether the size is known.
443 The best way of checking whether something is a marker value is
444 @code{known_eq}.  Conversely the best way of checking whether something
445 is @emph{not} a marker value is @code{maybe_ne}.
447 Thus in the size example just mentioned, @samp{known_eq (size, -1)} would
448 check for an unknown size and @samp{maybe_ne (size, -1)} would check for a
449 known size.
451 @node Range checks on @code{poly_int}s
452 @subsection Range checks on @code{poly_int}s
454 As well as the core comparisons
455 (@pxref{Comparison functions for @code{poly_int}}), @code{poly_int} provides
456 utilities for various kinds of range check.  In each case the range
457 is represented by a start position and a size rather than a start
458 position and an end position; this is because the former is used
459 much more often than the latter in GCC@.  Also, the sizes can be
460 -1 (or all ones for unsigned sizes) to indicate a range with a known
461 start position but an unknown size.  All other sizes must be nonnegative.
462 A range of size 0 does not contain anything or overlap anything.
464 @table @samp
465 @item known_size_p (@var{size})
466 Return true if @var{size} represents a known range size, false if it
467 is -1 or all ones (for signed and unsigned types respectively).
469 @item ranges_maybe_overlap_p (@var{pos1}, @var{size1}, @var{pos2}, @var{size2})
470 Return true if the range described by @var{pos1} and @var{size1} @emph{might}
471 overlap the range described by @var{pos2} and @var{size2} (in other words,
472 return true if we cannot prove that the ranges are disjoint).
474 @item ranges_known_overlap_p (@var{pos1}, @var{size1}, @var{pos2}, @var{size2})
475 Return true if the range described by @var{pos1} and @var{size1} is known to
476 overlap the range described by @var{pos2} and @var{size2}.
478 @item known_subrange_p (@var{pos1}, @var{size1}, @var{pos2}, @var{size2})
479 Return true if the range described by @var{pos1} and @var{size1} is known to
480 be contained in the range described by @var{pos2} and @var{size2}.
482 @item maybe_in_range_p (@var{value}, @var{pos}, @var{size})
483 Return true if @var{value} @emph{might} be in the range described by
484 @var{pos} and @var{size} (in other words, return true if we cannot
485 prove that @var{value} is outside that range).
487 @item known_in_range_p (@var{value}, @var{pos}, @var{size})
488 Return true if @var{value} is known to be in the range described
489 by @var{pos} and @var{size}.
491 @item endpoint_representable_p (@var{pos}, @var{size})
492 Return true if the range described by @var{pos} and @var{size} is
493 open-ended or if the endpoint (@var{pos} + @var{size}) is representable
494 in the same type as @var{pos} and @var{size}.  The function returns false
495 if adding @var{size} to @var{pos} makes conceptual sense but could overflow.
496 @end table
498 There is also a @code{poly_int} version of the @code{IN_RANGE_P} macro:
500 @table @samp
501 @item coeffs_in_range_p (@var{x}, @var{lower}, @var{upper})
502 Return true if every coefficient of @var{x} is in the inclusive range
503 [@var{lower}, @var{upper}].  This function can be useful when testing
504 whether an operation would cause the values of coefficients to
505 overflow.
507 Note that the function does not indicate whether @var{x} itself is in the
508 given range.  @var{x} can be either a constant or a @code{poly_int}.
509 @end table
511 @node Sorting @code{poly_int}s
512 @subsection Sorting @code{poly_int}s
514 @code{poly_int} provides the following routine for sorting:
516 @table @samp
517 @item compare_sizes_for_sort (@var{a}, @var{b})
518 Compare @var{a} and @var{b} in reverse lexicographical order (that is,
519 compare the highest-indexed coefficients first).  This can be useful when
520 sorting data structures, since it has the effect of separating constant
521 and non-constant values.  If all values are nonnegative, the constant
522 values come first.
524 Note that the values do not necessarily end up in numerical order.
525 For example, @samp{1 + 1@var{x}} would come after @samp{100} in the sort order,
526 but may well be less than @samp{100} at run time.
527 @end table
529 @node Arithmetic on @code{poly_int}s
530 @section Arithmetic on @code{poly_int}s
532 Addition, subtraction, negation and bit inversion all work normally for
533 @code{poly_int}s.  Multiplication by a constant multiplier and left
534 shifting by a constant shift amount also work normally.  General
535 multiplication of two @code{poly_int}s is not supported and is not
536 useful in practice.
538 Other operations are only conditionally supported: the operation
539 might succeed or might fail, depending on the inputs.
541 This section describes both types of operation.
543 @menu
544 * Using @code{poly_int} with C++ arithmetic operators::
545 * @code{wi} arithmetic on @code{poly_int}s::
546 * Division of @code{poly_int}s::
547 * Other @code{poly_int} arithmetic::
548 @end menu
550 @node Using @code{poly_int} with C++ arithmetic operators
551 @subsection Using @code{poly_int} with C++ arithmetic operators
553 The following C++ expressions are supported, where @var{p1} and @var{p2}
554 are @code{poly_int}s and where @var{c1} and @var{c2} are scalars:
556 @smallexample
557 -@var{p1}
558 ~@var{p1}
560 @var{p1} + @var{p2}
561 @var{p1} + @var{c2}
562 @var{c1} + @var{p2}
564 @var{p1} - @var{p2}
565 @var{p1} - @var{c2}
566 @var{c1} - @var{p2}
568 @var{c1} * @var{p2}
569 @var{p1} * @var{c2}
571 @var{p1} << @var{c2}
573 @var{p1} += @var{p2}
574 @var{p1} += @var{c2}
576 @var{p1} -= @var{p2}
577 @var{p1} -= @var{c2}
579 @var{p1} *= @var{c2}
580 @var{p1} <<= @var{c2}
581 @end smallexample
583 These arithmetic operations handle integer ranks in a similar way
584 to C++.  The main difference is that every coefficient narrower than
585 @code{HOST_WIDE_INT} promotes to @code{HOST_WIDE_INT}, whereas in
586 C++ everything narrower than @code{int} promotes to @code{int}.
587 For example:
589 @smallexample
590 poly_uint16     + int          -> poly_int64
591 unsigned int    + poly_uint16  -> poly_int64
592 poly_int64      + int          -> poly_int64
593 poly_int32      + poly_uint64  -> poly_uint64
594 uint64          + poly_int64   -> poly_uint64
595 poly_offset_int + int32        -> poly_offset_int
596 offset_int      + poly_uint16  -> poly_offset_int
597 @end smallexample
599 In the first two examples, both coefficients are narrower than
600 @code{HOST_WIDE_INT}, so the result has coefficients of type
601 @code{HOST_WIDE_INT}.  In the other examples, the coefficient
602 with the highest rank ``wins''.
604 If one of the operands is @code{wide_int} or @code{poly_wide_int},
605 the rules are the same as for @code{wide_int} arithmetic.
607 @node @code{wi} arithmetic on @code{poly_int}s
608 @subsection @code{wi} arithmetic on @code{poly_int}s
610 As well as the C++ operators, @code{poly_int} supports the following
611 @code{wi} routines:
613 @smallexample
614 wi::neg (@var{p1}, &@var{overflow})
616 wi::add (@var{p1}, @var{p2})
617 wi::add (@var{p1}, @var{c2})
618 wi::add (@var{c1}, @var{p1})
619 wi::add (@var{p1}, @var{p2}, @var{sign}, &@var{overflow})
621 wi::sub (@var{p1}, @var{p2})
622 wi::sub (@var{p1}, @var{c2})
623 wi::sub (@var{c1}, @var{p1})
624 wi::sub (@var{p1}, @var{p2}, @var{sign}, &@var{overflow})
626 wi::mul (@var{p1}, @var{c2})
627 wi::mul (@var{c1}, @var{p1})
628 wi::mul (@var{p1}, @var{c2}, @var{sign}, &@var{overflow})
630 wi::lshift (@var{p1}, @var{c2})
631 @end smallexample
633 These routines just check whether overflow occurs on any individual
634 coefficient; it is not possible to know at compile time whether the
635 final runtime value would overflow.
637 @node Division of @code{poly_int}s
638 @subsection Division of @code{poly_int}s
640 Division of @code{poly_int}s is possible for certain inputs.  The functions
641 for division return true if the operation is possible and in most cases
642 return the results by pointer.  The routines are:
644 @table @samp
645 @item multiple_p (@var{a}, @var{b})
646 @itemx multiple_p (@var{a}, @var{b}, &@var{quotient})
647 Return true if @var{a} is an exact multiple of @var{b}, storing the result
648 in @var{quotient} if so.  There are overloads for various combinations
649 of polynomial and constant @var{a}, @var{b} and @var{quotient}.
651 @item constant_multiple_p (@var{a}, @var{b})
652 @itemx constant_multiple_p (@var{a}, @var{b}, &@var{quotient})
653 Like @code{multiple_p}, but also test whether the multiple is a
654 compile-time constant.
656 @item can_div_trunc_p (@var{a}, @var{b}, &@var{quotient})
657 @itemx can_div_trunc_p (@var{a}, @var{b}, &@var{quotient}, &@var{remainder})
658 Return true if we can calculate @samp{trunc (@var{a} / @var{b})} at compile
659 time, storing the result in @var{quotient} and @var{remainder} if so.
661 @item can_div_away_from_zero_p (@var{a}, @var{b}, &@var{quotient})
662 Return true if we can calculate @samp{@var{a} / @var{b}} at compile time,
663 rounding away from zero.  Store the result in @var{quotient} if so.
665 Note that this is true if and only if @code{can_div_trunc_p} is true.
666 The only difference is in the rounding of the result.
667 @end table
669 There is also an asserting form of division:
671 @table @samp
672 @item exact_div (@var{a}, @var{b})
673 Assert that @var{a} is a multiple of @var{b} and return
674 @samp{@var{a} / @var{b}}.  The result is a @code{poly_int} if @var{a}
675 is a @code{poly_int}.
676 @end table
678 @node Other @code{poly_int} arithmetic
679 @subsection Other @code{poly_int} arithmetic
681 There are tentative routines for other operations besides division:
683 @table @samp
684 @item can_ior_p (@var{a}, @var{b}, &@var{result})
685 Return true if we can calculate @samp{@var{a} | @var{b}} at compile time,
686 storing the result in @var{result} if so.
687 @end table
689 Also, ANDs with a value @samp{(1 << @var{y}) - 1} or its inverse can be
690 treated as alignment operations.  @xref{Alignment of @code{poly_int}s}.
692 In addition, the following miscellaneous routines are available:
694 @table @samp
695 @item coeff_gcd (@var{a})
696 Return the greatest common divisor of all nonzero coefficients in
697 @var{a}, or zero if @var{a} is known to be zero.
699 @item common_multiple (@var{a}, @var{b})
700 Return a value that is a multiple of both @var{a} and @var{b}, where
701 one value is a @code{poly_int} and the other is a scalar.  The result
702 will be the least common multiple for some indeterminate values but
703 not necessarily for all.
705 @item force_common_multiple (@var{a}, @var{b})
706 Return a value that is a multiple of both @code{poly_int} @var{a} and
707 @code{poly_int} @var{b}, asserting that such a value exists.  The
708 result will be the least common multiple for some indeterminate values
709 but not necessarily for all.
711 When using this routine, please add a comment explaining why the
712 assertion is known to hold.
713 @end table
715 Please add any other operations that you find to be useful.
717 @node Alignment of @code{poly_int}s
718 @section Alignment of @code{poly_int}s
720 @code{poly_int} provides various routines for aligning values and for querying
721 misalignments.  In each case the alignment must be a power of 2.
723 @table @samp
724 @item can_align_p (@var{value}, @var{align})
725 Return true if we can align @var{value} up or down to the nearest multiple
726 of @var{align} at compile time.  The answer is the same for both directions.
728 @item can_align_down (@var{value}, @var{align}, &@var{aligned})
729 Return true if @code{can_align_p}; if so, set @var{aligned} to the greatest
730 aligned value that is less than or equal to @var{value}.
732 @item can_align_up (@var{value}, @var{align}, &@var{aligned})
733 Return true if @code{can_align_p}; if so, set @var{aligned} to the lowest
734 aligned value that is greater than or equal to @var{value}.
736 @item known_equal_after_align_down (@var{a}, @var{b}, @var{align})
737 Return true if we can align @var{a} and @var{b} down to the nearest
738 @var{align} boundary at compile time and if the two results are equal.
740 @item known_equal_after_align_up (@var{a}, @var{b}, @var{align})
741 Return true if we can align @var{a} and @var{b} up to the nearest
742 @var{align} boundary at compile time and if the two results are equal.
744 @item aligned_lower_bound (@var{value}, @var{align})
745 Return a result that is no greater than @var{value} and that is aligned
746 to @var{align}.  The result will the closest aligned value for some
747 indeterminate values but not necessarily for all.
749 For example, suppose we are allocating an object of @var{size} bytes
750 in a downward-growing stack whose current limit is given by @var{limit}.
751 If the object requires @var{align} bytes of alignment, the new stack
752 limit is given by:
754 @smallexample
755 aligned_lower_bound (@var{limit} - @var{size}, @var{align})
756 @end smallexample
758 @item aligned_upper_bound (@var{value}, @var{align})
759 Likewise return a result that is no less than @var{value} and that is
760 aligned to @var{align}.  This is the routine that would be used for
761 upward-growing stacks in the scenario just described.
763 @item known_misalignment (@var{value}, @var{align}, &@var{misalign})
764 Return true if we can calculate the misalignment of @var{value}
765 with respect to @var{align} at compile time, storing the result in
766 @var{misalign} if so.
768 @item known_alignment (@var{value})
769 Return the minimum alignment that @var{value} is known to have
770 (in other words, the largest alignment that can be guaranteed
771 whatever the values of the indeterminates turn out to be).
772 Return 0 if @var{value} is known to be 0.
774 @item force_align_down (@var{value}, @var{align})
775 Assert that @var{value} can be aligned down to @var{align} at compile
776 time and return the result.  When using this routine, please add a
777 comment explaining why the assertion is known to hold.
779 @item force_align_up (@var{value}, @var{align})
780 Likewise, but aligning up.
782 @item force_align_down_and_div (@var{value}, @var{align})
783 Divide the result of @code{force_align_down} by @var{align}.  Again,
784 please add a comment explaining why the assertion in @code{force_align_down}
785 is known to hold.
787 @item force_align_up_and_div (@var{value}, @var{align})
788 Likewise for @code{force_align_up}.
790 @item force_get_misalignment (@var{value}, @var{align})
791 Assert that we can calculate the misalignment of @var{value} with
792 respect to @var{align} at compile time and return the misalignment.
793 When using this function, please add a comment explaining why
794 the assertion is known to hold.
795 @end table
797 @node Computing bounds on @code{poly_int}s
798 @section Computing bounds on @code{poly_int}s
800 @code{poly_int} also provides routines for calculating lower and upper bounds:
802 @table @samp
803 @item constant_lower_bound (@var{a})
804 Assert that @var{a} is nonnegative and return the smallest value it can have.
806 @item lower_bound (@var{a}, @var{b})
807 Return a value that is always less than or equal to both @var{a} and @var{b}.
808 It will be the greatest such value for some indeterminate values
809 but necessarily for all.
811 @item upper_bound (@var{a}, @var{b})
812 Return a value that is always greater than or equal to both @var{a} and
813 @var{b}.  It will be the least such value for some indeterminate values
814 but necessarily for all.
815 @end table
817 @node Converting @code{poly_int}s
818 @section Converting @code{poly_int}s
820 A @code{poly_int<@var{n}, @var{T}>} can be constructed from up to
821 @var{n} individual @var{T} coefficients, with the remaining coefficients
822 being implicitly zero.  In particular, this means that every
823 @code{poly_int<@var{n}, @var{T}>} can be constructed from a single
824 scalar @var{T}, or something compatible with @var{T}.
826 Also, a @code{poly_int<@var{n}, @var{T}>} can be constructed from
827 a @code{poly_int<@var{n}, @var{U}>} if @var{T} can be constructed
828 from @var{U}.
830 The following functions provide other forms of conversion,
831 or test whether such a conversion would succeed.
833 @table @samp
834 @item @var{value}.is_constant ()
835 Return true if @code{poly_int} @var{value} is a compile-time constant.
837 @item @var{value}.is_constant (&@var{c1})
838 Return true if @code{poly_int} @var{value} is a compile-time constant,
839 storing it in @var{c1} if so.  @var{c1} must be able to hold all
840 constant values of @var{value} without loss of precision.
842 @item @var{value}.to_constant ()
843 Assert that @var{value} is a compile-time constant and return its value.
844 When using this function, please add a comment explaining why the
845 condition is known to hold (for example, because an earlier phase
846 of analysis rejected non-constants).
848 @item @var{value}.to_shwi (&@var{p2})
849 Return true if @samp{poly_int<@var{N}, @var{T}>} @var{value} can be
850 represented without loss of precision as a
851 @samp{poly_int<@var{N}, @code{HOST_WIDE_INT}>}, storing it in that
852 form in @var{p2} if so.
854 @item @var{value}.to_uhwi (&@var{p2})
855 Return true if @samp{poly_int<@var{N}, @var{T}>} @var{value} can be
856 represented without loss of precision as a
857 @samp{poly_int<@var{N}, @code{unsigned HOST_WIDE_INT}>}, storing it in that
858 form in @var{p2} if so.
860 @item @var{value}.force_shwi ()
861 Forcibly convert each coefficient of @samp{poly_int<@var{N}, @var{T}>}
862 @var{value} to @code{HOST_WIDE_INT}, truncating any that are out of range.
863 Return the result as a @samp{poly_int<@var{N}, @code{HOST_WIDE_INT}>}.
865 @item @var{value}.force_uhwi ()
866 Forcibly convert each coefficient of @samp{poly_int<@var{N}, @var{T}>}
867 @var{value} to @code{unsigned HOST_WIDE_INT}, truncating any that are
868 out of range.  Return the result as a
869 @samp{poly_int<@var{N}, @code{unsigned HOST_WIDE_INT}>}.
871 @item wi::shwi (@var{value}, @var{precision})
872 Return a @code{poly_int} with the same value as @var{value}, but with
873 the coefficients converted from @code{HOST_WIDE_INT} to @code{wide_int}.
874 @var{precision} specifies the precision of the @code{wide_int} cofficients;
875 if this is wider than a @code{HOST_WIDE_INT}, the coefficients of
876 @var{value} will be sign-extended to fit.
878 @item wi::uhwi (@var{value}, @var{precision})
879 Like @code{wi::shwi}, except that @var{value} has coefficients of
880 type @code{unsigned HOST_WIDE_INT}.  If @var{precision} is wider than
881 a @code{HOST_WIDE_INT}, the coefficients of @var{value} will be
882 zero-extended to fit.
884 @item wi::sext (@var{value}, @var{precision})
885 Return a @code{poly_int} of the same type as @var{value}, sign-extending
886 every coefficient from the low @var{precision} bits.  This in effect
887 applies @code{wi::sext} to each coefficient individually.
889 @item wi::zext (@var{value}, @var{precision})
890 Like @code{wi::sext}, but for zero extension.
892 @item poly_wide_int::from (@var{value}, @var{precision}, @var{sign})
893 Convert @var{value} to a @code{poly_wide_int} in which each coefficient
894 has @var{precision} bits.  Extend the coefficients according to
895 @var{sign} if the coefficients have fewer bits.
897 @item poly_offset_int::from (@var{value}, @var{sign})
898 Convert @var{value} to a @code{poly_offset_int}, extending its coefficients
899 according to @var{sign} if they have fewer bits than @code{offset_int}.
901 @item poly_widest_int::from (@var{value}, @var{sign})
902 Convert @var{value} to a @code{poly_widest_int}, extending its coefficients
903 according to @var{sign} if they have fewer bits than @code{widest_int}.
904 @end table
906 @node Miscellaneous @code{poly_int} routines
907 @section Miscellaneous @code{poly_int} routines
909 @table @samp
910 @item print_dec (@var{value}, @var{file}, @var{sign})
911 @itemx print_dec (@var{value}, @var{file})
912 Print @var{value} to @var{file} as a decimal value, interpreting
913 the coefficients according to @var{sign}.  The final argument is
914 optional if @var{value} has an inherent sign; for example,
915 @code{poly_int64} values print as signed by default and
916 @code{poly_uint64} values print as unsigned by default.
918 This is a simply a @code{poly_int} version of a wide-int routine.
919 @end table
921 @node Guidelines for using @code{poly_int}
922 @section Guidelines for using @code{poly_int}
924 One of the main design goals of @code{poly_int} was to make it easy
925 to write target-independent code that handles variable-sized registers
926 even when the current target has fixed-sized registers.  There are two
927 aspects to this:
929 @itemize
930 @item
931 The set of @code{poly_int} operations should be complete enough that
932 the question in most cases becomes ``Can we do this operation on these
933 particular @code{poly_int} values?  If not, bail out'' rather than
934 ``Are these @code{poly_int} values constant?  If so, do the operation,
935 otherwise bail out''.
937 @item
938 If target-independent code compiles and runs correctly on a target
939 with one value of @code{NUM_POLY_INT_COEFFS}, and if the code does not
940 use asserting functions like @code{to_constant}, it is reasonable to
941 assume that the code also works on targets with other values of
942 @code{NUM_POLY_INT_COEFFS}.  There is no need to check this during
943 everyday development.
944 @end itemize
946 So the general principle is: if target-independent code is dealing
947 with a @code{poly_int} value, it is better to operate on it as a
948 @code{poly_int} if at all possible, choosing conservatively-correct
949 behavior if a particular operation fails.  For example, the following
950 code handles an index @code{pos} into a sequence of vectors that each
951 have @code{nunits} elements:
953 @smallexample
954 /* Calculate which vector contains the result, and which lane of
955    that vector we need.  */
956 if (!can_div_trunc_p (pos, nunits, &vec_entry, &vec_index))
957   @{
958     if (dump_enabled_p ())
959       dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
960                        "Cannot determine which vector holds the"
961                        " final result.\n");
962     return false;
963   @}
964 @end smallexample
966 However, there are some contexts in which operating on a
967 @code{poly_int} is not possible or does not make sense.  One example
968 is when handling static initializers, since no current target supports
969 the concept of a variable-length static initializer.  In these
970 situations, a reasonable fallback is:
972 @smallexample
973 if (@var{poly_value}.is_constant (&@var{const_value}))
974   @{
975     @dots{}
976     /* Operate on @var{const_value}.  */
977     @dots{}
978   @}
979 else
980   @{
981     @dots{}
982     /* Conservatively correct fallback.  */
983     @dots{}
984   @}
985 @end smallexample
987 @code{poly_int} also provides some asserting functions like
988 @code{to_constant}.  Please only use these functions if there is a
989 good theoretical reason to believe that the assertion cannot fire.
990 For example, if some work is divided into an analysis phase and an
991 implementation phase, the analysis phase might reject inputs that are
992 not @code{is_constant}, in which case the implementation phase can
993 reasonably use @code{to_constant} on the remaining inputs.  The assertions
994 should not be used to discover whether a condition ever occurs ``in the
995 field''; in other words, they should not be used to restrict code to
996 constants at first, with the intention of only implementing a
997 @code{poly_int} version if a user hits the assertion.
999 If a particular asserting function like @code{to_constant} is needed
1000 more than once for the same reason, it is probably worth adding a
1001 helper function or macro for that situation, so that the justification
1002 only needs to be given once.  For example:
1004 @smallexample
1005 /* Return the size of an element in a vector of size SIZE, given that
1006    the vector has NELTS elements.  The return value is in the same units
1007    as SIZE (either bits or bytes).
1009    to_constant () is safe in this situation because vector elements are
1010    always constant-sized scalars.  */
1011 #define vector_element_size(SIZE, NELTS) \
1012   (exact_div (SIZE, NELTS).to_constant ())
1013 @end smallexample
1015 Target-specific code in @file{config/@var{cpu}} only needs to handle
1016 non-constant @code{poly_int}s if @code{NUM_POLY_INT_COEFFS} is greater
1017 than one.  For other targets, @code{poly_int} degenerates to a compile-time
1018 constant and is often interchangable with a normal scalar integer.
1019 There are two main exceptions:
1021 @itemize
1022 @item
1023 Sometimes an explicit cast to an integer type might be needed, such as to
1024 resolve ambiguities in a @code{?:} expression, or when passing values
1025 through @code{...} to things like print functions.
1027 @item
1028 Target macros are included in target-independent code and so do not
1029 have access to the implicit conversion to a scalar integer.
1030 If this becomes a problem for a particular target macro, the
1031 possible solutions, in order of preference, are:
1033 @itemize
1034 @item
1035 Convert the target macro to a target hook (for all targets).
1037 @item
1038 Put the target's implementation of the target macro in its
1039 @file{@var{cpu}.c} file and call it from the target macro in the
1040 @file{@var{cpu}.h} file.
1042 @item
1043 Add @code{to_constant ()} calls where necessary.  The previous option
1044 is preferable because it will help with any future conversion of the
1045 macro to a hook.
1046 @end itemize
1047 @end itemize