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