1cef8759bSmrg@node poly_int 2cef8759bSmrg@chapter Sizes and offsets as runtime invariants 3cef8759bSmrg@cindex polynomial integers 4cef8759bSmrg@findex poly_int 5cef8759bSmrg 6cef8759bSmrgGCC allows the size of a hardware register to be a runtime invariant 7cef8759bSmrgrather than a compile-time constant. This in turn means that various 8cef8759bSmrgsizes and offsets must also be runtime invariants rather than 9cef8759bSmrgcompile-time constants, such as: 10cef8759bSmrg 11cef8759bSmrg@itemize @bullet 12cef8759bSmrg@item 13cef8759bSmrgthe size of a general @code{machine_mode} (@pxref{Machine Modes}); 14cef8759bSmrg 15cef8759bSmrg@item 16cef8759bSmrgthe size of a spill slot; 17cef8759bSmrg 18cef8759bSmrg@item 19cef8759bSmrgthe offset of something within a stack frame; 20cef8759bSmrg 21cef8759bSmrg@item 22cef8759bSmrgthe number of elements in a vector; 23cef8759bSmrg 24cef8759bSmrg@item 25cef8759bSmrgthe size and offset of a @code{mem} rtx (@pxref{Regs and Memory}); and 26cef8759bSmrg 27cef8759bSmrg@item 28cef8759bSmrgthe byte offset in a @code{subreg} rtx (@pxref{Regs and Memory}). 29cef8759bSmrg@end itemize 30cef8759bSmrg 31cef8759bSmrgThe motivating example is the Arm SVE ISA, whose vector registers can be 32cef8759bSmrgany multiple of 128 bits between 128 and 2048 inclusive. The compiler 33cef8759bSmrgnormally produces code that works for all SVE register sizes, with the 34cef8759bSmrgactual size only being known at runtime. 35cef8759bSmrg 36cef8759bSmrgGCC's main representation of such runtime invariants is the 37cef8759bSmrg@code{poly_int} class. This chapter describes what @code{poly_int} 38cef8759bSmrgdoes, lists the available operations, and gives some general 39cef8759bSmrgusage guidelines. 40cef8759bSmrg 41cef8759bSmrg@menu 42cef8759bSmrg* Overview of @code{poly_int}:: 43cef8759bSmrg* Consequences of using @code{poly_int}:: 44cef8759bSmrg* Comparisons involving @code{poly_int}:: 45cef8759bSmrg* Arithmetic on @code{poly_int}s:: 46cef8759bSmrg* Alignment of @code{poly_int}s:: 47cef8759bSmrg* Computing bounds on @code{poly_int}s:: 48cef8759bSmrg* Converting @code{poly_int}s:: 49cef8759bSmrg* Miscellaneous @code{poly_int} routines:: 50cef8759bSmrg* Guidelines for using @code{poly_int}:: 51cef8759bSmrg@end menu 52cef8759bSmrg 53cef8759bSmrg@node Overview of @code{poly_int} 54cef8759bSmrg@section Overview of @code{poly_int} 55cef8759bSmrg 56cef8759bSmrg@cindex @code{poly_int}, runtime value 57cef8759bSmrgWe define indeterminates @var{x1}, @dots{}, @var{xn} whose values are 58cef8759bSmrgonly known at runtime and use polynomials of the form: 59cef8759bSmrg 60cef8759bSmrg@smallexample 61cef8759bSmrg@var{c0} + @var{c1} * @var{x1} + @dots{} + @var{cn} * @var{xn} 62cef8759bSmrg@end smallexample 63cef8759bSmrg 64cef8759bSmrgto represent a size or offset whose value might depend on some 65cef8759bSmrgof these indeterminates. The coefficients @var{c0}, @dots{}, @var{cn} 66cef8759bSmrgare always known at compile time, with the @var{c0} term being the 67cef8759bSmrg``constant'' part that does not depend on any runtime value. 68cef8759bSmrg 69cef8759bSmrgGCC uses the @code{poly_int} class to represent these coefficients. 70cef8759bSmrgThe class has two template parameters: the first specifies the number of 71cef8759bSmrgcoefficients (@var{n} + 1) and the second specifies the type of the 72cef8759bSmrgcoefficients. For example, @samp{poly_int<2, unsigned short>} represents 73cef8759bSmrga polynomial with two coefficients (and thus one indeterminate), with each 74cef8759bSmrgcoefficient having type @code{unsigned short}. When @var{n} is 0, 75cef8759bSmrgthe class degenerates to a single compile-time constant @var{c0}. 76cef8759bSmrg 77cef8759bSmrg@cindex @code{poly_int}, template parameters 78cef8759bSmrg@findex NUM_POLY_INT_COEFFS 79cef8759bSmrgThe number of coefficients needed for compilation is a fixed 80cef8759bSmrgproperty of each target and is specified by the configuration macro 81cef8759bSmrg@code{NUM_POLY_INT_COEFFS}. The default value is 1, since most targets 82cef8759bSmrgdo not have such runtime invariants. Targets that need a different 83cef8759bSmrgvalue should @code{#define} the macro in their @file{@var{cpu}-modes.def} 84cef8759bSmrgfile. @xref{Back End}. 85cef8759bSmrg 86cef8759bSmrg@cindex @code{poly_int}, invariant range 87cef8759bSmrg@code{poly_int} makes the simplifying requirement that each indeterminate 88cef8759bSmrgmust be a nonnegative integer. An indeterminate value of 0 should usually 89cef8759bSmrgrepresent the minimum possible runtime value, with @var{c0} specifying 90cef8759bSmrgthe value in that case. 91cef8759bSmrg 92cef8759bSmrgFor example, when targetting the Arm SVE ISA, the single indeterminate 93cef8759bSmrgrepresents the number of 128-bit blocks in a vector @emph{beyond the minimum 94cef8759bSmrglength of 128 bits}. Thus the number of 64-bit doublewords in a vector 95cef8759bSmrgis 2 + 2 * @var{x1}. If an aggregate has a single SVE vector and 16 96cef8759bSmrgadditional bytes, its total size is 32 + 16 * @var{x1} bytes. 97cef8759bSmrg 98cef8759bSmrgThe header file @file{poly-int-types.h} provides typedefs for the 99cef8759bSmrgmost common forms of @code{poly_int}, all having 100cef8759bSmrg@code{NUM_POLY_INT_COEFFS} coefficients: 101cef8759bSmrg 102cef8759bSmrg@cindex @code{poly_int}, main typedefs 103cef8759bSmrg@table @code 104cef8759bSmrg@item poly_uint16 105cef8759bSmrga @samp{poly_int} with @code{unsigned short} coefficients. 106cef8759bSmrg 107cef8759bSmrg@item poly_int64 108cef8759bSmrga @samp{poly_int} with @code{HOST_WIDE_INT} coefficients. 109cef8759bSmrg 110cef8759bSmrg@item poly_uint64 111cef8759bSmrga @samp{poly_int} with @code{unsigned HOST_WIDE_INT} coefficients. 112cef8759bSmrg 113cef8759bSmrg@item poly_offset_int 114cef8759bSmrga @samp{poly_int} with @code{offset_int} coefficients. 115cef8759bSmrg 116cef8759bSmrg@item poly_wide_int 117cef8759bSmrga @samp{poly_int} with @code{wide_int} coefficients. 118cef8759bSmrg 119cef8759bSmrg@item poly_widest_int 120cef8759bSmrga @samp{poly_int} with @code{widest_int} coefficients. 121cef8759bSmrg@end table 122cef8759bSmrg 123cef8759bSmrgSince the main purpose of @code{poly_int} is to represent sizes and 124cef8759bSmrgoffsets, the last two typedefs are only rarely used. 125cef8759bSmrg 126cef8759bSmrg@node Consequences of using @code{poly_int} 127cef8759bSmrg@section Consequences of using @code{poly_int} 128cef8759bSmrg 129cef8759bSmrgThe two main consequences of using polynomial sizes and offsets are that: 130cef8759bSmrg 131cef8759bSmrg@itemize 132cef8759bSmrg@item 133cef8759bSmrgthere is no total ordering between the values at compile time, and 134cef8759bSmrg 135cef8759bSmrg@item 136cef8759bSmrgsome operations might yield results that cannot be expressed as a 137cef8759bSmrg@code{poly_int}. 138cef8759bSmrg@end itemize 139cef8759bSmrg 140cef8759bSmrgFor example, if @var{x} is a runtime invariant, we cannot tell at 141cef8759bSmrgcompile time whether: 142cef8759bSmrg 143cef8759bSmrg@smallexample 144cef8759bSmrg3 + 4@var{x} <= 1 + 5@var{x} 145cef8759bSmrg@end smallexample 146cef8759bSmrg 147cef8759bSmrgsince the condition is false when @var{x} <= 1 and true when @var{x} >= 2. 148cef8759bSmrg 149cef8759bSmrgSimilarly, @code{poly_int} cannot represent the result of: 150cef8759bSmrg 151cef8759bSmrg@smallexample 152cef8759bSmrg(3 + 4@var{x}) * (1 + 5@var{x}) 153cef8759bSmrg@end smallexample 154cef8759bSmrg 155cef8759bSmrgsince it cannot (and in practice does not need to) store powers greater 156cef8759bSmrgthan one. It also cannot represent the result of: 157cef8759bSmrg 158cef8759bSmrg@smallexample 159cef8759bSmrg(3 + 4@var{x}) / (1 + 5@var{x}) 160cef8759bSmrg@end smallexample 161cef8759bSmrg 162cef8759bSmrgThe following sections describe how we deal with these restrictions. 163cef8759bSmrg 164cef8759bSmrg@cindex @code{poly_int}, use in target-independent code 165cef8759bSmrgAs described earlier, a @code{poly_int<1, @var{T}>} has no indeterminates 166cef8759bSmrgand so degenerates to a compile-time constant of type @var{T}. It would 167cef8759bSmrgbe possible in that case to do all normal arithmetic on the @var{T}, 168cef8759bSmrgand to compare the @var{T} using the normal C++ operators. We deliberately 169cef8759bSmrgprevent target-independent code from doing this, since the compiler needs 170cef8759bSmrgto support other @code{poly_int<@var{n}, @var{T}>} as well, regardless of 171cef8759bSmrgthe current target's @code{NUM_POLY_INT_COEFFS}. 172cef8759bSmrg 173cef8759bSmrg@cindex @code{poly_int}, use in target-specific code 174cef8759bSmrgHowever, it would be very artificial to force target-specific code 175cef8759bSmrgto follow these restrictions if the target has no runtime indeterminates. 176cef8759bSmrgThere is therefore an implicit conversion from @code{poly_int<1, @var{T}>} 177cef8759bSmrgto @var{T} when compiling target-specific translation units. 178cef8759bSmrg 179cef8759bSmrg@node Comparisons involving @code{poly_int} 180cef8759bSmrg@section Comparisons involving @code{poly_int} 181cef8759bSmrg 182cef8759bSmrgIn general we need to compare sizes and offsets in two situations: 183cef8759bSmrgthose in which the values need to be ordered, and those in which 184cef8759bSmrgthe values can be unordered. More loosely, the distinction is often 185cef8759bSmrgbetween values that have a definite link (usually because they refer to the 186cef8759bSmrgsame underlying register or memory location) and values that have 187cef8759bSmrgno definite link. An example of the former is the relationship between 188cef8759bSmrgthe inner and outer sizes of a subreg, where we must know at compile time 189cef8759bSmrgwhether the subreg is paradoxical, partial, or complete. An example of 190cef8759bSmrgthe latter is alias analysis: we might want to check whether two 191cef8759bSmrgarbitrary memory references overlap. 192cef8759bSmrg 193cef8759bSmrgReferring back to the examples in the previous section, it makes sense 194cef8759bSmrgto ask whether a memory reference of size @samp{3 + 4@var{x}} overlaps 195cef8759bSmrgone of size @samp{1 + 5@var{x}}, but it does not make sense to have a 196cef8759bSmrgsubreg in which the outer mode has @samp{3 + 4@var{x}} bytes and the 197cef8759bSmrginner mode has @samp{1 + 5@var{x}} bytes (or vice versa). Such subregs 198cef8759bSmrgare always invalid and should trigger an internal compiler error 199cef8759bSmrgif formed. 200cef8759bSmrg 201cef8759bSmrgThe underlying operators are the same in both cases, but the distinction 202cef8759bSmrgaffects how they are used. 203cef8759bSmrg 204cef8759bSmrg@menu 205cef8759bSmrg* Comparison functions for @code{poly_int}:: 206cef8759bSmrg* Properties of the @code{poly_int} comparisons:: 207cef8759bSmrg* Comparing potentially-unordered @code{poly_int}s:: 208cef8759bSmrg* Comparing ordered @code{poly_int}s:: 209cef8759bSmrg* Checking for a @code{poly_int} marker value:: 210cef8759bSmrg* Range checks on @code{poly_int}s:: 211cef8759bSmrg* Sorting @code{poly_int}s:: 212cef8759bSmrg@end menu 213cef8759bSmrg 214cef8759bSmrg@node Comparison functions for @code{poly_int} 215cef8759bSmrg@subsection Comparison functions for @code{poly_int} 216cef8759bSmrg 217cef8759bSmrg@code{poly_int} provides the following routines for checking whether 218cef8759bSmrga particular condition ``may be'' (might be) true: 219cef8759bSmrg 220cef8759bSmrg@example 221cef8759bSmrgmaybe_lt maybe_le maybe_eq maybe_ge maybe_gt 222cef8759bSmrg maybe_ne 223cef8759bSmrg@end example 224cef8759bSmrg 225cef8759bSmrgThe functions have their natural meaning: 226cef8759bSmrg 227cef8759bSmrg@table @samp 228cef8759bSmrg@item maybe_lt(@var{a}, @var{b}) 229cef8759bSmrgReturn true if @var{a} might be less than @var{b}. 230cef8759bSmrg 231cef8759bSmrg@item maybe_le(@var{a}, @var{b}) 232cef8759bSmrgReturn true if @var{a} might be less than or equal to @var{b}. 233cef8759bSmrg 234cef8759bSmrg@item maybe_eq(@var{a}, @var{b}) 235cef8759bSmrgReturn true if @var{a} might be equal to @var{b}. 236cef8759bSmrg 237cef8759bSmrg@item maybe_ne(@var{a}, @var{b}) 238cef8759bSmrgReturn true if @var{a} might not be equal to @var{b}. 239cef8759bSmrg 240cef8759bSmrg@item maybe_ge(@var{a}, @var{b}) 241cef8759bSmrgReturn true if @var{a} might be greater than or equal to @var{b}. 242cef8759bSmrg 243cef8759bSmrg@item maybe_gt(@var{a}, @var{b}) 244cef8759bSmrgReturn true if @var{a} might be greater than @var{b}. 245cef8759bSmrg@end table 246cef8759bSmrg 247cef8759bSmrgFor readability, @code{poly_int} also provides ``known'' inverses of these 248cef8759bSmrgfunctions: 249cef8759bSmrg 250cef8759bSmrg@example 251cef8759bSmrgknown_lt (@var{a}, @var{b}) == !maybe_ge (@var{a}, @var{b}) 252cef8759bSmrgknown_le (@var{a}, @var{b}) == !maybe_gt (@var{a}, @var{b}) 253cef8759bSmrgknown_eq (@var{a}, @var{b}) == !maybe_ne (@var{a}, @var{b}) 254cef8759bSmrgknown_ge (@var{a}, @var{b}) == !maybe_lt (@var{a}, @var{b}) 255cef8759bSmrgknown_gt (@var{a}, @var{b}) == !maybe_le (@var{a}, @var{b}) 256cef8759bSmrgknown_ne (@var{a}, @var{b}) == !maybe_eq (@var{a}, @var{b}) 257cef8759bSmrg@end example 258cef8759bSmrg 259cef8759bSmrg@node Properties of the @code{poly_int} comparisons 260cef8759bSmrg@subsection Properties of the @code{poly_int} comparisons 261cef8759bSmrg 262cef8759bSmrgAll ``maybe'' relations except @code{maybe_ne} are transitive, so for example: 263cef8759bSmrg 264cef8759bSmrg@smallexample 265cef8759bSmrgmaybe_lt (@var{a}, @var{b}) && maybe_lt (@var{b}, @var{c}) implies maybe_lt (@var{a}, @var{c}) 266cef8759bSmrg@end smallexample 267cef8759bSmrg 268cef8759bSmrgfor all @var{a}, @var{b} and @var{c}. @code{maybe_lt}, @code{maybe_gt} 269cef8759bSmrgand @code{maybe_ne} are irreflexive, so for example: 270cef8759bSmrg 271cef8759bSmrg@smallexample 272cef8759bSmrg!maybe_lt (@var{a}, @var{a}) 273cef8759bSmrg@end smallexample 274cef8759bSmrg 275cef8759bSmrgis true for all @var{a}. @code{maybe_le}, @code{maybe_eq} and @code{maybe_ge} 276cef8759bSmrgare reflexive, so for example: 277cef8759bSmrg 278cef8759bSmrg@smallexample 279cef8759bSmrgmaybe_le (@var{a}, @var{a}) 280cef8759bSmrg@end smallexample 281cef8759bSmrg 282cef8759bSmrgis true for all @var{a}. @code{maybe_eq} and @code{maybe_ne} are symmetric, so: 283cef8759bSmrg 284cef8759bSmrg@smallexample 285cef8759bSmrgmaybe_eq (@var{a}, @var{b}) == maybe_eq (@var{b}, @var{a}) 286cef8759bSmrgmaybe_ne (@var{a}, @var{b}) == maybe_ne (@var{b}, @var{a}) 287cef8759bSmrg@end smallexample 288cef8759bSmrg 289cef8759bSmrgfor all @var{a} and @var{b}. In addition: 290cef8759bSmrg 291cef8759bSmrg@smallexample 292cef8759bSmrgmaybe_le (@var{a}, @var{b}) == maybe_lt (@var{a}, @var{b}) || maybe_eq (@var{a}, @var{b}) 293cef8759bSmrgmaybe_ge (@var{a}, @var{b}) == maybe_gt (@var{a}, @var{b}) || maybe_eq (@var{a}, @var{b}) 294cef8759bSmrgmaybe_lt (@var{a}, @var{b}) == maybe_gt (@var{b}, @var{a}) 295cef8759bSmrgmaybe_le (@var{a}, @var{b}) == maybe_ge (@var{b}, @var{a}) 296cef8759bSmrg@end smallexample 297cef8759bSmrg 298cef8759bSmrgHowever: 299cef8759bSmrg 300cef8759bSmrg@smallexample 301cef8759bSmrgmaybe_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})] 302cef8759bSmrgmaybe_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})] 303cef8759bSmrg@end smallexample 304cef8759bSmrg 305cef8759bSmrgOne example is again @samp{@var{a} == 3 + 4@var{x}} 306cef8759bSmrgand @samp{@var{b} == 1 + 5@var{x}}, where @samp{maybe_le (@var{a}, @var{b})}, 307cef8759bSmrg@samp{maybe_ge (@var{a}, @var{b})} and @samp{maybe_ne (@var{a}, @var{b})} 308cef8759bSmrgall hold. @code{maybe_le} and @code{maybe_ge} are therefore not antisymetric 309cef8759bSmrgand do not form a partial order. 310cef8759bSmrg 311cef8759bSmrgFrom the above, it follows that: 312cef8759bSmrg 313cef8759bSmrg@itemize @bullet 314cef8759bSmrg@item 315cef8759bSmrgAll ``known'' relations except @code{known_ne} are transitive. 316cef8759bSmrg 317cef8759bSmrg@item 318cef8759bSmrg@code{known_lt}, @code{known_ne} and @code{known_gt} are irreflexive. 319cef8759bSmrg 320cef8759bSmrg@item 321cef8759bSmrg@code{known_le}, @code{known_eq} and @code{known_ge} are reflexive. 322cef8759bSmrg@end itemize 323cef8759bSmrg 324cef8759bSmrgAlso: 325cef8759bSmrg 326cef8759bSmrg@smallexample 327cef8759bSmrgknown_lt (@var{a}, @var{b}) == known_gt (@var{b}, @var{a}) 328cef8759bSmrgknown_le (@var{a}, @var{b}) == known_ge (@var{b}, @var{a}) 329cef8759bSmrgknown_lt (@var{a}, @var{b}) implies !known_lt (@var{b}, @var{a}) [asymmetry] 330cef8759bSmrgknown_gt (@var{a}, @var{b}) implies !known_gt (@var{b}, @var{a}) 331cef8759bSmrgknown_le (@var{a}, @var{b}) && known_le (@var{b}, @var{a}) == known_eq (@var{a}, @var{b}) [== !maybe_ne (@var{a}, @var{b})] 332cef8759bSmrgknown_ge (@var{a}, @var{b}) && known_ge (@var{b}, @var{a}) == known_eq (@var{a}, @var{b}) [== !maybe_ne (@var{a}, @var{b})] 333cef8759bSmrg@end smallexample 334cef8759bSmrg 335cef8759bSmrg@code{known_le} and @code{known_ge} are therefore antisymmetric and are 336cef8759bSmrgpartial orders. However: 337cef8759bSmrg 338cef8759bSmrg@smallexample 339cef8759bSmrgknown_le (@var{a}, @var{b}) does not imply known_lt (@var{a}, @var{b}) || known_eq (@var{a}, @var{b}) 340cef8759bSmrgknown_ge (@var{a}, @var{b}) does not imply known_gt (@var{a}, @var{b}) || known_eq (@var{a}, @var{b}) 341cef8759bSmrg@end smallexample 342cef8759bSmrg 343cef8759bSmrgFor example, @samp{known_le (4, 4 + 4@var{x})} holds because the runtime 344cef8759bSmrgindeterminate @var{x} is a nonnegative integer, but neither 345cef8759bSmrg@code{known_lt (4, 4 + 4@var{x})} nor @code{known_eq (4, 4 + 4@var{x})} hold. 346cef8759bSmrg 347cef8759bSmrg@node Comparing potentially-unordered @code{poly_int}s 348cef8759bSmrg@subsection Comparing potentially-unordered @code{poly_int}s 349cef8759bSmrg 350cef8759bSmrgIn cases where there is no definite link between two @code{poly_int}s, 351cef8759bSmrgwe can usually make a conservatively-correct assumption. For example, 352cef8759bSmrgthe conservative assumption for alias analysis is that two references 353cef8759bSmrg@emph{might} alias. 354cef8759bSmrg 355cef8759bSmrgOne way of checking whether [@var{begin1}, @var{end1}) might overlap 356cef8759bSmrg[@var{begin2}, @var{end2}) using the @code{poly_int} comparisons is: 357cef8759bSmrg 358cef8759bSmrg@smallexample 359cef8759bSmrgmaybe_gt (@var{end1}, @var{begin2}) && maybe_gt (@var{end2}, @var{begin1}) 360cef8759bSmrg@end smallexample 361cef8759bSmrg 362cef8759bSmrgand another (equivalent) way is: 363cef8759bSmrg 364cef8759bSmrg@smallexample 365cef8759bSmrg!(known_le (@var{end1}, @var{begin2}) || known_le (@var{end2}, @var{begin1})) 366cef8759bSmrg@end smallexample 367cef8759bSmrg 368cef8759bSmrgHowever, in this particular example, it is better to use the range helper 369cef8759bSmrgfunctions instead. @xref{Range checks on @code{poly_int}s}. 370cef8759bSmrg 371cef8759bSmrg@node Comparing ordered @code{poly_int}s 372cef8759bSmrg@subsection Comparing ordered @code{poly_int}s 373cef8759bSmrg 374cef8759bSmrgIn cases where there is a definite link between two @code{poly_int}s, 375cef8759bSmrgsuch as the outer and inner sizes of subregs, we usually require the sizes 376cef8759bSmrgto be ordered by the @code{known_le} partial order. @code{poly_int} provides 377cef8759bSmrgthe following utility functions for ordered values: 378cef8759bSmrg 379cef8759bSmrg@table @samp 380cef8759bSmrg@item ordered_p (@var{a}, @var{b}) 381cef8759bSmrgReturn true if @var{a} and @var{b} are ordered by the @code{known_le} 382cef8759bSmrgpartial order. 383cef8759bSmrg 384cef8759bSmrg@item ordered_min (@var{a}, @var{b}) 385cef8759bSmrgAssert that @var{a} and @var{b} are ordered by @code{known_le} and return the 386cef8759bSmrgminimum of the two. When using this function, please add a comment explaining 387cef8759bSmrgwhy the values are known to be ordered. 388cef8759bSmrg 389cef8759bSmrg@item ordered_max (@var{a}, @var{b}) 390cef8759bSmrgAssert that @var{a} and @var{b} are ordered by @code{known_le} and return the 391cef8759bSmrgmaximum of the two. When using this function, please add a comment explaining 392cef8759bSmrgwhy the values are known to be ordered. 393cef8759bSmrg@end table 394cef8759bSmrg 395cef8759bSmrgFor example, if a subreg has an outer mode of size @var{outer} and an 396cef8759bSmrginner mode of size @var{inner}: 397cef8759bSmrg 398cef8759bSmrg@itemize @bullet 399cef8759bSmrg@item 400cef8759bSmrgthe subreg is complete if known_eq (@var{inner}, @var{outer}) 401cef8759bSmrg 402cef8759bSmrg@item 403cef8759bSmrgotherwise, the subreg is paradoxical if known_le (@var{inner}, @var{outer}) 404cef8759bSmrg 405cef8759bSmrg@item 406cef8759bSmrgotherwise, the subreg is partial if known_le (@var{outer}, @var{inner}) 407cef8759bSmrg 408cef8759bSmrg@item 409cef8759bSmrgotherwise, the subreg is ill-formed 410cef8759bSmrg@end itemize 411cef8759bSmrg 412cef8759bSmrgThus the subreg is only valid if 413cef8759bSmrg@samp{ordered_p (@var{outer}, @var{inner})} is true. If this condition 414cef8759bSmrgis already known to be true then: 415cef8759bSmrg 416cef8759bSmrg@itemize @bullet 417cef8759bSmrg@item 418cef8759bSmrgthe subreg is complete if known_eq (@var{inner}, @var{outer}) 419cef8759bSmrg 420cef8759bSmrg@item 421cef8759bSmrgthe subreg is paradoxical if maybe_lt (@var{inner}, @var{outer}) 422cef8759bSmrg 423cef8759bSmrg@item 424cef8759bSmrgthe subreg is partial if maybe_lt (@var{outer}, @var{inner}) 425cef8759bSmrg@end itemize 426cef8759bSmrg 427cef8759bSmrgwith the three conditions being mutually exclusive. 428cef8759bSmrg 429cef8759bSmrgCode that checks whether a subreg is valid would therefore generally 430cef8759bSmrgcheck whether @code{ordered_p} holds (in addition to whatever other 431cef8759bSmrgchecks are required for subreg validity). Code that is dealing 432cef8759bSmrgwith existing subregs can assert that @code{ordered_p} holds 433cef8759bSmrgand use either of the classifications above. 434cef8759bSmrg 435cef8759bSmrg@node Checking for a @code{poly_int} marker value 436cef8759bSmrg@subsection Checking for a @code{poly_int} marker value 437cef8759bSmrg 438cef8759bSmrgIt is sometimes useful to have a special ``marker value'' that is not 439cef8759bSmrgmeant to be taken literally. For example, some code uses a size 440cef8759bSmrgof -1 to represent an unknown size, rather than having to carry around 441cef8759bSmrga separate boolean to say whether the size is known. 442cef8759bSmrg 443cef8759bSmrgThe best way of checking whether something is a marker value is 444cef8759bSmrg@code{known_eq}. Conversely the best way of checking whether something 445cef8759bSmrgis @emph{not} a marker value is @code{maybe_ne}. 446cef8759bSmrg 447cef8759bSmrgThus in the size example just mentioned, @samp{known_eq (size, -1)} would 448cef8759bSmrgcheck for an unknown size and @samp{maybe_ne (size, -1)} would check for a 449cef8759bSmrgknown size. 450cef8759bSmrg 451cef8759bSmrg@node Range checks on @code{poly_int}s 452cef8759bSmrg@subsection Range checks on @code{poly_int}s 453cef8759bSmrg 454cef8759bSmrgAs well as the core comparisons 455cef8759bSmrg(@pxref{Comparison functions for @code{poly_int}}), @code{poly_int} provides 456cef8759bSmrgutilities for various kinds of range check. In each case the range 457cef8759bSmrgis represented by a start position and a size rather than a start 458cef8759bSmrgposition and an end position; this is because the former is used 459cef8759bSmrgmuch more often than the latter in GCC@. Also, the sizes can be 460cef8759bSmrg-1 (or all ones for unsigned sizes) to indicate a range with a known 461cef8759bSmrgstart position but an unknown size. All other sizes must be nonnegative. 462cef8759bSmrgA range of size 0 does not contain anything or overlap anything. 463cef8759bSmrg 464cef8759bSmrg@table @samp 465cef8759bSmrg@item known_size_p (@var{size}) 466cef8759bSmrgReturn true if @var{size} represents a known range size, false if it 467cef8759bSmrgis -1 or all ones (for signed and unsigned types respectively). 468cef8759bSmrg 469cef8759bSmrg@item ranges_maybe_overlap_p (@var{pos1}, @var{size1}, @var{pos2}, @var{size2}) 470cef8759bSmrgReturn true if the range described by @var{pos1} and @var{size1} @emph{might} 471cef8759bSmrgoverlap the range described by @var{pos2} and @var{size2} (in other words, 472cef8759bSmrgreturn true if we cannot prove that the ranges are disjoint). 473cef8759bSmrg 474cef8759bSmrg@item ranges_known_overlap_p (@var{pos1}, @var{size1}, @var{pos2}, @var{size2}) 475cef8759bSmrgReturn true if the range described by @var{pos1} and @var{size1} is known to 476cef8759bSmrgoverlap the range described by @var{pos2} and @var{size2}. 477cef8759bSmrg 478cef8759bSmrg@item known_subrange_p (@var{pos1}, @var{size1}, @var{pos2}, @var{size2}) 479cef8759bSmrgReturn true if the range described by @var{pos1} and @var{size1} is known to 480cef8759bSmrgbe contained in the range described by @var{pos2} and @var{size2}. 481cef8759bSmrg 482cef8759bSmrg@item maybe_in_range_p (@var{value}, @var{pos}, @var{size}) 483cef8759bSmrgReturn true if @var{value} @emph{might} be in the range described by 484cef8759bSmrg@var{pos} and @var{size} (in other words, return true if we cannot 485cef8759bSmrgprove that @var{value} is outside that range). 486cef8759bSmrg 487cef8759bSmrg@item known_in_range_p (@var{value}, @var{pos}, @var{size}) 488cef8759bSmrgReturn true if @var{value} is known to be in the range described 489cef8759bSmrgby @var{pos} and @var{size}. 490cef8759bSmrg 491cef8759bSmrg@item endpoint_representable_p (@var{pos}, @var{size}) 492cef8759bSmrgReturn true if the range described by @var{pos} and @var{size} is 493cef8759bSmrgopen-ended or if the endpoint (@var{pos} + @var{size}) is representable 494cef8759bSmrgin the same type as @var{pos} and @var{size}. The function returns false 495cef8759bSmrgif adding @var{size} to @var{pos} makes conceptual sense but could overflow. 496cef8759bSmrg@end table 497cef8759bSmrg 498cef8759bSmrgThere is also a @code{poly_int} version of the @code{IN_RANGE_P} macro: 499cef8759bSmrg 500cef8759bSmrg@table @samp 501cef8759bSmrg@item coeffs_in_range_p (@var{x}, @var{lower}, @var{upper}) 502cef8759bSmrgReturn true if every coefficient of @var{x} is in the inclusive range 503cef8759bSmrg[@var{lower}, @var{upper}]. This function can be useful when testing 504cef8759bSmrgwhether an operation would cause the values of coefficients to 505cef8759bSmrgoverflow. 506cef8759bSmrg 507cef8759bSmrgNote that the function does not indicate whether @var{x} itself is in the 508cef8759bSmrggiven range. @var{x} can be either a constant or a @code{poly_int}. 509cef8759bSmrg@end table 510cef8759bSmrg 511cef8759bSmrg@node Sorting @code{poly_int}s 512cef8759bSmrg@subsection Sorting @code{poly_int}s 513cef8759bSmrg 514cef8759bSmrg@code{poly_int} provides the following routine for sorting: 515cef8759bSmrg 516cef8759bSmrg@table @samp 517cef8759bSmrg@item compare_sizes_for_sort (@var{a}, @var{b}) 518cef8759bSmrgCompare @var{a} and @var{b} in reverse lexicographical order (that is, 519cef8759bSmrgcompare the highest-indexed coefficients first). This can be useful when 520cef8759bSmrgsorting data structures, since it has the effect of separating constant 521cef8759bSmrgand non-constant values. If all values are nonnegative, the constant 522cef8759bSmrgvalues come first. 523cef8759bSmrg 524cef8759bSmrgNote that the values do not necessarily end up in numerical order. 525cef8759bSmrgFor example, @samp{1 + 1@var{x}} would come after @samp{100} in the sort order, 526cef8759bSmrgbut may well be less than @samp{100} at run time. 527cef8759bSmrg@end table 528cef8759bSmrg 529cef8759bSmrg@node Arithmetic on @code{poly_int}s 530cef8759bSmrg@section Arithmetic on @code{poly_int}s 531cef8759bSmrg 532cef8759bSmrgAddition, subtraction, negation and bit inversion all work normally for 533cef8759bSmrg@code{poly_int}s. Multiplication by a constant multiplier and left 534cef8759bSmrgshifting by a constant shift amount also work normally. General 535cef8759bSmrgmultiplication of two @code{poly_int}s is not supported and is not 536cef8759bSmrguseful in practice. 537cef8759bSmrg 538cef8759bSmrgOther operations are only conditionally supported: the operation 539cef8759bSmrgmight succeed or might fail, depending on the inputs. 540cef8759bSmrg 541cef8759bSmrgThis section describes both types of operation. 542cef8759bSmrg 543cef8759bSmrg@menu 544cef8759bSmrg* Using @code{poly_int} with C++ arithmetic operators:: 545cef8759bSmrg* @code{wi} arithmetic on @code{poly_int}s:: 546cef8759bSmrg* Division of @code{poly_int}s:: 547cef8759bSmrg* Other @code{poly_int} arithmetic:: 548cef8759bSmrg@end menu 549cef8759bSmrg 550cef8759bSmrg@node Using @code{poly_int} with C++ arithmetic operators 551cef8759bSmrg@subsection Using @code{poly_int} with C++ arithmetic operators 552cef8759bSmrg 553cef8759bSmrgThe following C++ expressions are supported, where @var{p1} and @var{p2} 554cef8759bSmrgare @code{poly_int}s and where @var{c1} and @var{c2} are scalars: 555cef8759bSmrg 556cef8759bSmrg@smallexample 557cef8759bSmrg-@var{p1} 558cef8759bSmrg~@var{p1} 559cef8759bSmrg 560cef8759bSmrg@var{p1} + @var{p2} 561cef8759bSmrg@var{p1} + @var{c2} 562cef8759bSmrg@var{c1} + @var{p2} 563cef8759bSmrg 564cef8759bSmrg@var{p1} - @var{p2} 565cef8759bSmrg@var{p1} - @var{c2} 566cef8759bSmrg@var{c1} - @var{p2} 567cef8759bSmrg 568cef8759bSmrg@var{c1} * @var{p2} 569cef8759bSmrg@var{p1} * @var{c2} 570cef8759bSmrg 571cef8759bSmrg@var{p1} << @var{c2} 572cef8759bSmrg 573cef8759bSmrg@var{p1} += @var{p2} 574cef8759bSmrg@var{p1} += @var{c2} 575cef8759bSmrg 576cef8759bSmrg@var{p1} -= @var{p2} 577cef8759bSmrg@var{p1} -= @var{c2} 578cef8759bSmrg 579cef8759bSmrg@var{p1} *= @var{c2} 580cef8759bSmrg@var{p1} <<= @var{c2} 581cef8759bSmrg@end smallexample 582cef8759bSmrg 583cef8759bSmrgThese arithmetic operations handle integer ranks in a similar way 584cef8759bSmrgto C++. The main difference is that every coefficient narrower than 585cef8759bSmrg@code{HOST_WIDE_INT} promotes to @code{HOST_WIDE_INT}, whereas in 586cef8759bSmrgC++ everything narrower than @code{int} promotes to @code{int}. 587cef8759bSmrgFor example: 588cef8759bSmrg 589cef8759bSmrg@smallexample 590cef8759bSmrgpoly_uint16 + int -> poly_int64 591cef8759bSmrgunsigned int + poly_uint16 -> poly_int64 592cef8759bSmrgpoly_int64 + int -> poly_int64 593cef8759bSmrgpoly_int32 + poly_uint64 -> poly_uint64 594cef8759bSmrguint64 + poly_int64 -> poly_uint64 595cef8759bSmrgpoly_offset_int + int32 -> poly_offset_int 596cef8759bSmrgoffset_int + poly_uint16 -> poly_offset_int 597cef8759bSmrg@end smallexample 598cef8759bSmrg 599cef8759bSmrgIn the first two examples, both coefficients are narrower than 600cef8759bSmrg@code{HOST_WIDE_INT}, so the result has coefficients of type 601cef8759bSmrg@code{HOST_WIDE_INT}. In the other examples, the coefficient 602cef8759bSmrgwith the highest rank ``wins''. 603cef8759bSmrg 604cef8759bSmrgIf one of the operands is @code{wide_int} or @code{poly_wide_int}, 605cef8759bSmrgthe rules are the same as for @code{wide_int} arithmetic. 606cef8759bSmrg 607cef8759bSmrg@node @code{wi} arithmetic on @code{poly_int}s 608cef8759bSmrg@subsection @code{wi} arithmetic on @code{poly_int}s 609cef8759bSmrg 610cef8759bSmrgAs well as the C++ operators, @code{poly_int} supports the following 611cef8759bSmrg@code{wi} routines: 612cef8759bSmrg 613cef8759bSmrg@smallexample 614cef8759bSmrgwi::neg (@var{p1}, &@var{overflow}) 615cef8759bSmrg 616cef8759bSmrgwi::add (@var{p1}, @var{p2}) 617cef8759bSmrgwi::add (@var{p1}, @var{c2}) 618cef8759bSmrgwi::add (@var{c1}, @var{p1}) 619cef8759bSmrgwi::add (@var{p1}, @var{p2}, @var{sign}, &@var{overflow}) 620cef8759bSmrg 621cef8759bSmrgwi::sub (@var{p1}, @var{p2}) 622cef8759bSmrgwi::sub (@var{p1}, @var{c2}) 623cef8759bSmrgwi::sub (@var{c1}, @var{p1}) 624cef8759bSmrgwi::sub (@var{p1}, @var{p2}, @var{sign}, &@var{overflow}) 625cef8759bSmrg 626cef8759bSmrgwi::mul (@var{p1}, @var{c2}) 627cef8759bSmrgwi::mul (@var{c1}, @var{p1}) 628cef8759bSmrgwi::mul (@var{p1}, @var{c2}, @var{sign}, &@var{overflow}) 629cef8759bSmrg 630cef8759bSmrgwi::lshift (@var{p1}, @var{c2}) 631cef8759bSmrg@end smallexample 632cef8759bSmrg 633cef8759bSmrgThese routines just check whether overflow occurs on any individual 634cef8759bSmrgcoefficient; it is not possible to know at compile time whether the 635cef8759bSmrgfinal runtime value would overflow. 636cef8759bSmrg 637cef8759bSmrg@node Division of @code{poly_int}s 638cef8759bSmrg@subsection Division of @code{poly_int}s 639cef8759bSmrg 640cef8759bSmrgDivision of @code{poly_int}s is possible for certain inputs. The functions 641cef8759bSmrgfor division return true if the operation is possible and in most cases 642cef8759bSmrgreturn the results by pointer. The routines are: 643cef8759bSmrg 644cef8759bSmrg@table @samp 645cef8759bSmrg@item multiple_p (@var{a}, @var{b}) 646cef8759bSmrg@itemx multiple_p (@var{a}, @var{b}, &@var{quotient}) 647cef8759bSmrgReturn true if @var{a} is an exact multiple of @var{b}, storing the result 648cef8759bSmrgin @var{quotient} if so. There are overloads for various combinations 649cef8759bSmrgof polynomial and constant @var{a}, @var{b} and @var{quotient}. 650cef8759bSmrg 651cef8759bSmrg@item constant_multiple_p (@var{a}, @var{b}) 652cef8759bSmrg@itemx constant_multiple_p (@var{a}, @var{b}, &@var{quotient}) 653cef8759bSmrgLike @code{multiple_p}, but also test whether the multiple is a 654cef8759bSmrgcompile-time constant. 655cef8759bSmrg 656cef8759bSmrg@item can_div_trunc_p (@var{a}, @var{b}, &@var{quotient}) 657cef8759bSmrg@itemx can_div_trunc_p (@var{a}, @var{b}, &@var{quotient}, &@var{remainder}) 658cef8759bSmrgReturn true if we can calculate @samp{trunc (@var{a} / @var{b})} at compile 659cef8759bSmrgtime, storing the result in @var{quotient} and @var{remainder} if so. 660cef8759bSmrg 661cef8759bSmrg@item can_div_away_from_zero_p (@var{a}, @var{b}, &@var{quotient}) 662cef8759bSmrgReturn true if we can calculate @samp{@var{a} / @var{b}} at compile time, 663cef8759bSmrgrounding away from zero. Store the result in @var{quotient} if so. 664cef8759bSmrg 665cef8759bSmrgNote that this is true if and only if @code{can_div_trunc_p} is true. 666cef8759bSmrgThe only difference is in the rounding of the result. 667cef8759bSmrg@end table 668cef8759bSmrg 669cef8759bSmrgThere is also an asserting form of division: 670cef8759bSmrg 671cef8759bSmrg@table @samp 672cef8759bSmrg@item exact_div (@var{a}, @var{b}) 673cef8759bSmrgAssert that @var{a} is a multiple of @var{b} and return 674cef8759bSmrg@samp{@var{a} / @var{b}}. The result is a @code{poly_int} if @var{a} 675cef8759bSmrgis a @code{poly_int}. 676cef8759bSmrg@end table 677cef8759bSmrg 678cef8759bSmrg@node Other @code{poly_int} arithmetic 679cef8759bSmrg@subsection Other @code{poly_int} arithmetic 680cef8759bSmrg 681cef8759bSmrgThere are tentative routines for other operations besides division: 682cef8759bSmrg 683cef8759bSmrg@table @samp 684cef8759bSmrg@item can_ior_p (@var{a}, @var{b}, &@var{result}) 685cef8759bSmrgReturn true if we can calculate @samp{@var{a} | @var{b}} at compile time, 686cef8759bSmrgstoring the result in @var{result} if so. 687cef8759bSmrg@end table 688cef8759bSmrg 689cef8759bSmrgAlso, ANDs with a value @samp{(1 << @var{y}) - 1} or its inverse can be 690cef8759bSmrgtreated as alignment operations. @xref{Alignment of @code{poly_int}s}. 691cef8759bSmrg 692cef8759bSmrgIn addition, the following miscellaneous routines are available: 693cef8759bSmrg 694cef8759bSmrg@table @samp 695cef8759bSmrg@item coeff_gcd (@var{a}) 696cef8759bSmrgReturn the greatest common divisor of all nonzero coefficients in 697cef8759bSmrg@var{a}, or zero if @var{a} is known to be zero. 698cef8759bSmrg 699cef8759bSmrg@item common_multiple (@var{a}, @var{b}) 700cef8759bSmrgReturn a value that is a multiple of both @var{a} and @var{b}, where 701cef8759bSmrgone value is a @code{poly_int} and the other is a scalar. The result 702cef8759bSmrgwill be the least common multiple for some indeterminate values but 703cef8759bSmrgnot necessarily for all. 704cef8759bSmrg 705cef8759bSmrg@item force_common_multiple (@var{a}, @var{b}) 706cef8759bSmrgReturn a value that is a multiple of both @code{poly_int} @var{a} and 707cef8759bSmrg@code{poly_int} @var{b}, asserting that such a value exists. The 708cef8759bSmrgresult will be the least common multiple for some indeterminate values 709cef8759bSmrgbut not necessarily for all. 710cef8759bSmrg 711cef8759bSmrgWhen using this routine, please add a comment explaining why the 712cef8759bSmrgassertion is known to hold. 713cef8759bSmrg@end table 714cef8759bSmrg 715cef8759bSmrgPlease add any other operations that you find to be useful. 716cef8759bSmrg 717cef8759bSmrg@node Alignment of @code{poly_int}s 718cef8759bSmrg@section Alignment of @code{poly_int}s 719cef8759bSmrg 720cef8759bSmrg@code{poly_int} provides various routines for aligning values and for querying 721cef8759bSmrgmisalignments. In each case the alignment must be a power of 2. 722cef8759bSmrg 723cef8759bSmrg@table @samp 724cef8759bSmrg@item can_align_p (@var{value}, @var{align}) 725cef8759bSmrgReturn true if we can align @var{value} up or down to the nearest multiple 726cef8759bSmrgof @var{align} at compile time. The answer is the same for both directions. 727cef8759bSmrg 728cef8759bSmrg@item can_align_down (@var{value}, @var{align}, &@var{aligned}) 729cef8759bSmrgReturn true if @code{can_align_p}; if so, set @var{aligned} to the greatest 730cef8759bSmrgaligned value that is less than or equal to @var{value}. 731cef8759bSmrg 732cef8759bSmrg@item can_align_up (@var{value}, @var{align}, &@var{aligned}) 733cef8759bSmrgReturn true if @code{can_align_p}; if so, set @var{aligned} to the lowest 734cef8759bSmrgaligned value that is greater than or equal to @var{value}. 735cef8759bSmrg 736cef8759bSmrg@item known_equal_after_align_down (@var{a}, @var{b}, @var{align}) 737cef8759bSmrgReturn true if we can align @var{a} and @var{b} down to the nearest 738cef8759bSmrg@var{align} boundary at compile time and if the two results are equal. 739cef8759bSmrg 740cef8759bSmrg@item known_equal_after_align_up (@var{a}, @var{b}, @var{align}) 741cef8759bSmrgReturn true if we can align @var{a} and @var{b} up to the nearest 742cef8759bSmrg@var{align} boundary at compile time and if the two results are equal. 743cef8759bSmrg 744cef8759bSmrg@item aligned_lower_bound (@var{value}, @var{align}) 745cef8759bSmrgReturn a result that is no greater than @var{value} and that is aligned 746cef8759bSmrgto @var{align}. The result will the closest aligned value for some 747cef8759bSmrgindeterminate values but not necessarily for all. 748cef8759bSmrg 749cef8759bSmrgFor example, suppose we are allocating an object of @var{size} bytes 750cef8759bSmrgin a downward-growing stack whose current limit is given by @var{limit}. 751cef8759bSmrgIf the object requires @var{align} bytes of alignment, the new stack 752cef8759bSmrglimit is given by: 753cef8759bSmrg 754cef8759bSmrg@smallexample 755cef8759bSmrgaligned_lower_bound (@var{limit} - @var{size}, @var{align}) 756cef8759bSmrg@end smallexample 757cef8759bSmrg 758cef8759bSmrg@item aligned_upper_bound (@var{value}, @var{align}) 759cef8759bSmrgLikewise return a result that is no less than @var{value} and that is 760cef8759bSmrgaligned to @var{align}. This is the routine that would be used for 761cef8759bSmrgupward-growing stacks in the scenario just described. 762cef8759bSmrg 763cef8759bSmrg@item known_misalignment (@var{value}, @var{align}, &@var{misalign}) 764cef8759bSmrgReturn true if we can calculate the misalignment of @var{value} 765cef8759bSmrgwith respect to @var{align} at compile time, storing the result in 766cef8759bSmrg@var{misalign} if so. 767cef8759bSmrg 768cef8759bSmrg@item known_alignment (@var{value}) 769cef8759bSmrgReturn the minimum alignment that @var{value} is known to have 770cef8759bSmrg(in other words, the largest alignment that can be guaranteed 771cef8759bSmrgwhatever the values of the indeterminates turn out to be). 772cef8759bSmrgReturn 0 if @var{value} is known to be 0. 773cef8759bSmrg 774cef8759bSmrg@item force_align_down (@var{value}, @var{align}) 775cef8759bSmrgAssert that @var{value} can be aligned down to @var{align} at compile 776cef8759bSmrgtime and return the result. When using this routine, please add a 777cef8759bSmrgcomment explaining why the assertion is known to hold. 778cef8759bSmrg 779cef8759bSmrg@item force_align_up (@var{value}, @var{align}) 780cef8759bSmrgLikewise, but aligning up. 781cef8759bSmrg 782cef8759bSmrg@item force_align_down_and_div (@var{value}, @var{align}) 783cef8759bSmrgDivide the result of @code{force_align_down} by @var{align}. Again, 784cef8759bSmrgplease add a comment explaining why the assertion in @code{force_align_down} 785cef8759bSmrgis known to hold. 786cef8759bSmrg 787cef8759bSmrg@item force_align_up_and_div (@var{value}, @var{align}) 788cef8759bSmrgLikewise for @code{force_align_up}. 789cef8759bSmrg 790cef8759bSmrg@item force_get_misalignment (@var{value}, @var{align}) 791cef8759bSmrgAssert that we can calculate the misalignment of @var{value} with 792cef8759bSmrgrespect to @var{align} at compile time and return the misalignment. 793cef8759bSmrgWhen using this function, please add a comment explaining why 794cef8759bSmrgthe assertion is known to hold. 795cef8759bSmrg@end table 796cef8759bSmrg 797cef8759bSmrg@node Computing bounds on @code{poly_int}s 798cef8759bSmrg@section Computing bounds on @code{poly_int}s 799cef8759bSmrg 800cef8759bSmrg@code{poly_int} also provides routines for calculating lower and upper bounds: 801cef8759bSmrg 802cef8759bSmrg@table @samp 803cef8759bSmrg@item constant_lower_bound (@var{a}) 804cef8759bSmrgAssert that @var{a} is nonnegative and return the smallest value it can have. 805cef8759bSmrg 806*4c3eb207Smrg@item constant_lower_bound_with_limit (@var{a}, @var{b}) 807*4c3eb207SmrgReturn the least value @var{a} can have, given that the context in 808*4c3eb207Smrgwhich @var{a} appears guarantees that the answer is no less than @var{b}. 809*4c3eb207SmrgIn other words, the caller is asserting that @var{a} is greater than or 810*4c3eb207Smrgequal to @var{b} even if @samp{known_ge (@var{a}, @var{b})} doesn't hold. 811*4c3eb207Smrg 812*4c3eb207Smrg@item constant_upper_bound_with_limit (@var{a}, @var{b}) 813*4c3eb207SmrgReturn the greatest value @var{a} can have, given that the context in 814*4c3eb207Smrgwhich @var{a} appears guarantees that the answer is no greater than @var{b}. 815*4c3eb207SmrgIn other words, the caller is asserting that @var{a} is less than or equal 816*4c3eb207Smrgto @var{b} even if @samp{known_le (@var{a}, @var{b})} doesn't hold. 817*4c3eb207Smrg 818cef8759bSmrg@item lower_bound (@var{a}, @var{b}) 819cef8759bSmrgReturn a value that is always less than or equal to both @var{a} and @var{b}. 820cef8759bSmrgIt will be the greatest such value for some indeterminate values 821cef8759bSmrgbut necessarily for all. 822cef8759bSmrg 823cef8759bSmrg@item upper_bound (@var{a}, @var{b}) 824cef8759bSmrgReturn a value that is always greater than or equal to both @var{a} and 825cef8759bSmrg@var{b}. It will be the least such value for some indeterminate values 826cef8759bSmrgbut necessarily for all. 827cef8759bSmrg@end table 828cef8759bSmrg 829cef8759bSmrg@node Converting @code{poly_int}s 830cef8759bSmrg@section Converting @code{poly_int}s 831cef8759bSmrg 832cef8759bSmrgA @code{poly_int<@var{n}, @var{T}>} can be constructed from up to 833cef8759bSmrg@var{n} individual @var{T} coefficients, with the remaining coefficients 834cef8759bSmrgbeing implicitly zero. In particular, this means that every 835cef8759bSmrg@code{poly_int<@var{n}, @var{T}>} can be constructed from a single 836cef8759bSmrgscalar @var{T}, or something compatible with @var{T}. 837cef8759bSmrg 838cef8759bSmrgAlso, a @code{poly_int<@var{n}, @var{T}>} can be constructed from 839cef8759bSmrga @code{poly_int<@var{n}, @var{U}>} if @var{T} can be constructed 840cef8759bSmrgfrom @var{U}. 841cef8759bSmrg 842cef8759bSmrgThe following functions provide other forms of conversion, 843cef8759bSmrgor test whether such a conversion would succeed. 844cef8759bSmrg 845cef8759bSmrg@table @samp 846cef8759bSmrg@item @var{value}.is_constant () 847cef8759bSmrgReturn true if @code{poly_int} @var{value} is a compile-time constant. 848cef8759bSmrg 849cef8759bSmrg@item @var{value}.is_constant (&@var{c1}) 850cef8759bSmrgReturn true if @code{poly_int} @var{value} is a compile-time constant, 851cef8759bSmrgstoring it in @var{c1} if so. @var{c1} must be able to hold all 852cef8759bSmrgconstant values of @var{value} without loss of precision. 853cef8759bSmrg 854cef8759bSmrg@item @var{value}.to_constant () 855cef8759bSmrgAssert that @var{value} is a compile-time constant and return its value. 856cef8759bSmrgWhen using this function, please add a comment explaining why the 857cef8759bSmrgcondition is known to hold (for example, because an earlier phase 858cef8759bSmrgof analysis rejected non-constants). 859cef8759bSmrg 860cef8759bSmrg@item @var{value}.to_shwi (&@var{p2}) 861cef8759bSmrgReturn true if @samp{poly_int<@var{N}, @var{T}>} @var{value} can be 862cef8759bSmrgrepresented without loss of precision as a 863cef8759bSmrg@samp{poly_int<@var{N}, @code{HOST_WIDE_INT}>}, storing it in that 864cef8759bSmrgform in @var{p2} if so. 865cef8759bSmrg 866cef8759bSmrg@item @var{value}.to_uhwi (&@var{p2}) 867cef8759bSmrgReturn true if @samp{poly_int<@var{N}, @var{T}>} @var{value} can be 868cef8759bSmrgrepresented without loss of precision as a 869cef8759bSmrg@samp{poly_int<@var{N}, @code{unsigned HOST_WIDE_INT}>}, storing it in that 870cef8759bSmrgform in @var{p2} if so. 871cef8759bSmrg 872cef8759bSmrg@item @var{value}.force_shwi () 873cef8759bSmrgForcibly convert each coefficient of @samp{poly_int<@var{N}, @var{T}>} 874cef8759bSmrg@var{value} to @code{HOST_WIDE_INT}, truncating any that are out of range. 875cef8759bSmrgReturn the result as a @samp{poly_int<@var{N}, @code{HOST_WIDE_INT}>}. 876cef8759bSmrg 877cef8759bSmrg@item @var{value}.force_uhwi () 878cef8759bSmrgForcibly convert each coefficient of @samp{poly_int<@var{N}, @var{T}>} 879cef8759bSmrg@var{value} to @code{unsigned HOST_WIDE_INT}, truncating any that are 880cef8759bSmrgout of range. Return the result as a 881cef8759bSmrg@samp{poly_int<@var{N}, @code{unsigned HOST_WIDE_INT}>}. 882cef8759bSmrg 883cef8759bSmrg@item wi::shwi (@var{value}, @var{precision}) 884cef8759bSmrgReturn a @code{poly_int} with the same value as @var{value}, but with 885cef8759bSmrgthe coefficients converted from @code{HOST_WIDE_INT} to @code{wide_int}. 886cef8759bSmrg@var{precision} specifies the precision of the @code{wide_int} cofficients; 887cef8759bSmrgif this is wider than a @code{HOST_WIDE_INT}, the coefficients of 888cef8759bSmrg@var{value} will be sign-extended to fit. 889cef8759bSmrg 890cef8759bSmrg@item wi::uhwi (@var{value}, @var{precision}) 891cef8759bSmrgLike @code{wi::shwi}, except that @var{value} has coefficients of 892cef8759bSmrgtype @code{unsigned HOST_WIDE_INT}. If @var{precision} is wider than 893cef8759bSmrga @code{HOST_WIDE_INT}, the coefficients of @var{value} will be 894cef8759bSmrgzero-extended to fit. 895cef8759bSmrg 896cef8759bSmrg@item wi::sext (@var{value}, @var{precision}) 897cef8759bSmrgReturn a @code{poly_int} of the same type as @var{value}, sign-extending 898cef8759bSmrgevery coefficient from the low @var{precision} bits. This in effect 899cef8759bSmrgapplies @code{wi::sext} to each coefficient individually. 900cef8759bSmrg 901cef8759bSmrg@item wi::zext (@var{value}, @var{precision}) 902cef8759bSmrgLike @code{wi::sext}, but for zero extension. 903cef8759bSmrg 904cef8759bSmrg@item poly_wide_int::from (@var{value}, @var{precision}, @var{sign}) 905cef8759bSmrgConvert @var{value} to a @code{poly_wide_int} in which each coefficient 906cef8759bSmrghas @var{precision} bits. Extend the coefficients according to 907cef8759bSmrg@var{sign} if the coefficients have fewer bits. 908cef8759bSmrg 909cef8759bSmrg@item poly_offset_int::from (@var{value}, @var{sign}) 910cef8759bSmrgConvert @var{value} to a @code{poly_offset_int}, extending its coefficients 911cef8759bSmrgaccording to @var{sign} if they have fewer bits than @code{offset_int}. 912cef8759bSmrg 913cef8759bSmrg@item poly_widest_int::from (@var{value}, @var{sign}) 914cef8759bSmrgConvert @var{value} to a @code{poly_widest_int}, extending its coefficients 915cef8759bSmrgaccording to @var{sign} if they have fewer bits than @code{widest_int}. 916cef8759bSmrg@end table 917cef8759bSmrg 918cef8759bSmrg@node Miscellaneous @code{poly_int} routines 919cef8759bSmrg@section Miscellaneous @code{poly_int} routines 920cef8759bSmrg 921cef8759bSmrg@table @samp 922cef8759bSmrg@item print_dec (@var{value}, @var{file}, @var{sign}) 923cef8759bSmrg@itemx print_dec (@var{value}, @var{file}) 924cef8759bSmrgPrint @var{value} to @var{file} as a decimal value, interpreting 925cef8759bSmrgthe coefficients according to @var{sign}. The final argument is 926cef8759bSmrgoptional if @var{value} has an inherent sign; for example, 927cef8759bSmrg@code{poly_int64} values print as signed by default and 928cef8759bSmrg@code{poly_uint64} values print as unsigned by default. 929cef8759bSmrg 930cef8759bSmrgThis is a simply a @code{poly_int} version of a wide-int routine. 931cef8759bSmrg@end table 932cef8759bSmrg 933cef8759bSmrg@node Guidelines for using @code{poly_int} 934cef8759bSmrg@section Guidelines for using @code{poly_int} 935cef8759bSmrg 936cef8759bSmrgOne of the main design goals of @code{poly_int} was to make it easy 937cef8759bSmrgto write target-independent code that handles variable-sized registers 938cef8759bSmrgeven when the current target has fixed-sized registers. There are two 939cef8759bSmrgaspects to this: 940cef8759bSmrg 941cef8759bSmrg@itemize 942cef8759bSmrg@item 943cef8759bSmrgThe set of @code{poly_int} operations should be complete enough that 944cef8759bSmrgthe question in most cases becomes ``Can we do this operation on these 945cef8759bSmrgparticular @code{poly_int} values? If not, bail out'' rather than 946cef8759bSmrg``Are these @code{poly_int} values constant? If so, do the operation, 947cef8759bSmrgotherwise bail out''. 948cef8759bSmrg 949cef8759bSmrg@item 950cef8759bSmrgIf target-independent code compiles and runs correctly on a target 951cef8759bSmrgwith one value of @code{NUM_POLY_INT_COEFFS}, and if the code does not 952cef8759bSmrguse asserting functions like @code{to_constant}, it is reasonable to 953cef8759bSmrgassume that the code also works on targets with other values of 954cef8759bSmrg@code{NUM_POLY_INT_COEFFS}. There is no need to check this during 955cef8759bSmrgeveryday development. 956cef8759bSmrg@end itemize 957cef8759bSmrg 958cef8759bSmrgSo the general principle is: if target-independent code is dealing 959cef8759bSmrgwith a @code{poly_int} value, it is better to operate on it as a 960cef8759bSmrg@code{poly_int} if at all possible, choosing conservatively-correct 961cef8759bSmrgbehavior if a particular operation fails. For example, the following 962cef8759bSmrgcode handles an index @code{pos} into a sequence of vectors that each 963cef8759bSmrghave @code{nunits} elements: 964cef8759bSmrg 965cef8759bSmrg@smallexample 966cef8759bSmrg/* Calculate which vector contains the result, and which lane of 967cef8759bSmrg that vector we need. */ 968cef8759bSmrgif (!can_div_trunc_p (pos, nunits, &vec_entry, &vec_index)) 969cef8759bSmrg @{ 970cef8759bSmrg if (dump_enabled_p ()) 971cef8759bSmrg dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, 972cef8759bSmrg "Cannot determine which vector holds the" 973cef8759bSmrg " final result.\n"); 974cef8759bSmrg return false; 975cef8759bSmrg @} 976cef8759bSmrg@end smallexample 977cef8759bSmrg 978cef8759bSmrgHowever, there are some contexts in which operating on a 979cef8759bSmrg@code{poly_int} is not possible or does not make sense. One example 980cef8759bSmrgis when handling static initializers, since no current target supports 981cef8759bSmrgthe concept of a variable-length static initializer. In these 982cef8759bSmrgsituations, a reasonable fallback is: 983cef8759bSmrg 984cef8759bSmrg@smallexample 985cef8759bSmrgif (@var{poly_value}.is_constant (&@var{const_value})) 986cef8759bSmrg @{ 987cef8759bSmrg @dots{} 988cef8759bSmrg /* Operate on @var{const_value}. */ 989cef8759bSmrg @dots{} 990cef8759bSmrg @} 991cef8759bSmrgelse 992cef8759bSmrg @{ 993cef8759bSmrg @dots{} 994cef8759bSmrg /* Conservatively correct fallback. */ 995cef8759bSmrg @dots{} 996cef8759bSmrg @} 997cef8759bSmrg@end smallexample 998cef8759bSmrg 999cef8759bSmrg@code{poly_int} also provides some asserting functions like 1000cef8759bSmrg@code{to_constant}. Please only use these functions if there is a 1001cef8759bSmrggood theoretical reason to believe that the assertion cannot fire. 1002cef8759bSmrgFor example, if some work is divided into an analysis phase and an 1003cef8759bSmrgimplementation phase, the analysis phase might reject inputs that are 1004cef8759bSmrgnot @code{is_constant}, in which case the implementation phase can 1005cef8759bSmrgreasonably use @code{to_constant} on the remaining inputs. The assertions 1006cef8759bSmrgshould not be used to discover whether a condition ever occurs ``in the 1007cef8759bSmrgfield''; in other words, they should not be used to restrict code to 1008cef8759bSmrgconstants at first, with the intention of only implementing a 1009cef8759bSmrg@code{poly_int} version if a user hits the assertion. 1010cef8759bSmrg 1011cef8759bSmrgIf a particular asserting function like @code{to_constant} is needed 1012cef8759bSmrgmore than once for the same reason, it is probably worth adding a 1013cef8759bSmrghelper function or macro for that situation, so that the justification 1014cef8759bSmrgonly needs to be given once. For example: 1015cef8759bSmrg 1016cef8759bSmrg@smallexample 1017cef8759bSmrg/* Return the size of an element in a vector of size SIZE, given that 1018cef8759bSmrg the vector has NELTS elements. The return value is in the same units 1019cef8759bSmrg as SIZE (either bits or bytes). 1020cef8759bSmrg 1021cef8759bSmrg to_constant () is safe in this situation because vector elements are 1022cef8759bSmrg always constant-sized scalars. */ 1023cef8759bSmrg#define vector_element_size(SIZE, NELTS) \ 1024cef8759bSmrg (exact_div (SIZE, NELTS).to_constant ()) 1025cef8759bSmrg@end smallexample 1026cef8759bSmrg 1027cef8759bSmrgTarget-specific code in @file{config/@var{cpu}} only needs to handle 1028cef8759bSmrgnon-constant @code{poly_int}s if @code{NUM_POLY_INT_COEFFS} is greater 1029cef8759bSmrgthan one. For other targets, @code{poly_int} degenerates to a compile-time 1030cef8759bSmrgconstant and is often interchangable with a normal scalar integer. 1031cef8759bSmrgThere are two main exceptions: 1032cef8759bSmrg 1033cef8759bSmrg@itemize 1034cef8759bSmrg@item 1035cef8759bSmrgSometimes an explicit cast to an integer type might be needed, such as to 1036cef8759bSmrgresolve ambiguities in a @code{?:} expression, or when passing values 1037cef8759bSmrgthrough @code{...} to things like print functions. 1038cef8759bSmrg 1039cef8759bSmrg@item 1040cef8759bSmrgTarget macros are included in target-independent code and so do not 1041cef8759bSmrghave access to the implicit conversion to a scalar integer. 1042cef8759bSmrgIf this becomes a problem for a particular target macro, the 1043cef8759bSmrgpossible solutions, in order of preference, are: 1044cef8759bSmrg 1045cef8759bSmrg@itemize 1046cef8759bSmrg@item 1047cef8759bSmrgConvert the target macro to a target hook (for all targets). 1048cef8759bSmrg 1049cef8759bSmrg@item 1050cef8759bSmrgPut the target's implementation of the target macro in its 1051cef8759bSmrg@file{@var{cpu}.c} file and call it from the target macro in the 1052cef8759bSmrg@file{@var{cpu}.h} file. 1053cef8759bSmrg 1054cef8759bSmrg@item 1055cef8759bSmrgAdd @code{to_constant ()} calls where necessary. The previous option 1056cef8759bSmrgis preferable because it will help with any future conversion of the 1057cef8759bSmrgmacro to a hook. 1058cef8759bSmrg@end itemize 1059cef8759bSmrg@end itemize 1060cef8759bSmrg 1061