@@ -31,6 +31,7 @@ class raw_ostream;
31
31
32
32
template <typename T> class SmallVectorImpl ;
33
33
template <typename T> class ArrayRef ;
34
+ template <typename T> class Optional ;
34
35
35
36
class APInt ;
36
37
@@ -84,7 +85,7 @@ class LLVM_NODISCARD APInt {
84
85
UP,
85
86
};
86
87
87
- static const WordType WORD_MAX = ~WordType (0 );
88
+ static const WordType WORDTYPE_MAX = ~WordType (0 );
88
89
89
90
private:
90
91
// / This union is used to store the integer value. When the
@@ -149,7 +150,7 @@ class LLVM_NODISCARD APInt {
149
150
unsigned WordBits = ((BitWidth-1 ) % APINT_BITS_PER_WORD) + 1 ;
150
151
151
152
// Mask out the high bits.
152
- uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - WordBits);
153
+ uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits);
153
154
if (isSingleWord ())
154
155
U.VAL &= mask;
155
156
else
@@ -394,7 +395,7 @@ class LLVM_NODISCARD APInt {
394
395
// / This checks to see if the value has all bits of the APInt are set or not.
395
396
bool isAllOnesValue () const {
396
397
if (isSingleWord ())
397
- return U.VAL == WORD_MAX >> (APINT_BITS_PER_WORD - BitWidth);
398
+ return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth);
398
399
return countTrailingOnesSlowCase () == BitWidth;
399
400
}
400
401
@@ -495,7 +496,7 @@ class LLVM_NODISCARD APInt {
495
496
assert (numBits != 0 && " numBits must be non-zero" );
496
497
assert (numBits <= BitWidth && " numBits out of range" );
497
498
if (isSingleWord ())
498
- return U.VAL == (WORD_MAX >> (APINT_BITS_PER_WORD - numBits));
499
+ return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits));
499
500
unsigned Ones = countTrailingOnesSlowCase ();
500
501
return (numBits == Ones) &&
501
502
((Ones + countLeadingZerosSlowCase ()) == BitWidth);
@@ -559,7 +560,7 @@ class LLVM_NODISCARD APInt {
559
560
// /
560
561
// / \returns the all-ones value for an APInt of the specified bit-width.
561
562
static APInt getAllOnesValue (unsigned numBits) {
562
- return APInt (numBits, WORD_MAX , true );
563
+ return APInt (numBits, WORDTYPE_MAX , true );
563
564
}
564
565
565
566
// / Get the '0' value.
@@ -1104,6 +1105,12 @@ class LLVM_NODISCARD APInt {
1104
1105
APInt sshl_ov (const APInt &Amt, bool &Overflow) const ;
1105
1106
APInt ushl_ov (const APInt &Amt, bool &Overflow) const ;
1106
1107
1108
+ // Operations that saturate
1109
+ APInt sadd_sat (const APInt &RHS) const ;
1110
+ APInt uadd_sat (const APInt &RHS) const ;
1111
+ APInt ssub_sat (const APInt &RHS) const ;
1112
+ APInt usub_sat (const APInt &RHS) const ;
1113
+
1107
1114
// / Array-indexing support.
1108
1115
// /
1109
1116
// / \returns the bit value at bitPosition
@@ -1382,7 +1389,7 @@ class LLVM_NODISCARD APInt {
1382
1389
// / Set every bit to 1.
1383
1390
void setAllBits () {
1384
1391
if (isSingleWord ())
1385
- U.VAL = WORD_MAX ;
1392
+ U.VAL = WORDTYPE_MAX ;
1386
1393
else
1387
1394
// Set all the bits in all the words.
1388
1395
memset (U.pVal , -1 , getNumWords () * APINT_WORD_SIZE);
@@ -1394,7 +1401,7 @@ class LLVM_NODISCARD APInt {
1394
1401
// /
1395
1402
// / Set the given bit to 1 whose position is given as "bitPosition".
1396
1403
void setBit (unsigned BitPosition) {
1397
- assert (BitPosition <= BitWidth && " BitPosition out of range" );
1404
+ assert (BitPosition < BitWidth && " BitPosition out of range" );
1398
1405
WordType Mask = maskBit (BitPosition);
1399
1406
if (isSingleWord ())
1400
1407
U.VAL |= Mask;
@@ -1415,7 +1422,7 @@ class LLVM_NODISCARD APInt {
1415
1422
if (loBit == hiBit)
1416
1423
return ;
1417
1424
if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
1418
- uint64_t mask = WORD_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
1425
+ uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
1419
1426
mask <<= loBit;
1420
1427
if (isSingleWord ())
1421
1428
U.VAL |= mask;
@@ -1453,7 +1460,7 @@ class LLVM_NODISCARD APInt {
1453
1460
// /
1454
1461
// / Set the given bit to 0 whose position is given as "bitPosition".
1455
1462
void clearBit (unsigned BitPosition) {
1456
- assert (BitPosition <= BitWidth && " BitPosition out of range" );
1463
+ assert (BitPosition < BitWidth && " BitPosition out of range" );
1457
1464
WordType Mask = ~maskBit (BitPosition);
1458
1465
if (isSingleWord ())
1459
1466
U.VAL &= Mask;
@@ -1469,7 +1476,7 @@ class LLVM_NODISCARD APInt {
1469
1476
// / Toggle every bit to its opposite value.
1470
1477
void flipAllBits () {
1471
1478
if (isSingleWord ()) {
1472
- U.VAL ^= WORD_MAX ;
1479
+ U.VAL ^= WORDTYPE_MAX ;
1473
1480
clearUnusedBits ();
1474
1481
} else {
1475
1482
flipAllBitsSlowCase ();
@@ -1758,7 +1765,7 @@ class LLVM_NODISCARD APInt {
1758
1765
// / referencing 2 in a space where 2 does no exist.
1759
1766
unsigned nearestLogBase2 () const {
1760
1767
// Special case when we have a bitwidth of 1. If VAL is 1, then we
1761
- // get 0. If VAL is 0, we get WORD_MAX which gets truncated to
1768
+ // get 0. If VAL is 0, we get WORDTYPE_MAX which gets truncated to
1762
1769
// UINT32_MAX.
1763
1770
if (BitWidth == 1 )
1764
1771
return U.VAL - 1 ;
@@ -2166,6 +2173,41 @@ APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2166
2173
// / Return A sign-divided by B, rounded by the given rounding mode.
2167
2174
APInt RoundingSDiv (const APInt &A, const APInt &B, APInt::Rounding RM);
2168
2175
2176
+ // / Let q(n) = An^2 + Bn + C, and BW = bit width of the value range
2177
+ // / (e.g. 32 for i32).
2178
+ // / This function finds the smallest number n, such that
2179
+ // / (a) n >= 0 and q(n) = 0, or
2180
+ // / (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all
2181
+ // / integers, belong to two different intervals [Rk, Rk+R),
2182
+ // / where R = 2^BW, and k is an integer.
2183
+ // / The idea here is to find when q(n) "overflows" 2^BW, while at the
2184
+ // / same time "allowing" subtraction. In unsigned modulo arithmetic a
2185
+ // / subtraction (treated as addition of negated numbers) would always
2186
+ // / count as an overflow, but here we want to allow values to decrease
2187
+ // / and increase as long as they are within the same interval.
2188
+ // / Specifically, adding of two negative numbers should not cause an
2189
+ // / overflow (as long as the magnitude does not exceed the bith width).
2190
+ // / On the other hand, given a positive number, adding a negative
2191
+ // / number to it can give a negative result, which would cause the
2192
+ // / value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is
2193
+ // / treated as a special case of an overflow.
2194
+ // /
2195
+ // / This function returns None if after finding k that minimizes the
2196
+ // / positive solution to q(n) = kR, both solutions are contained between
2197
+ // / two consecutive integers.
2198
+ // /
2199
+ // / There are cases where q(n) > T, and q(n+1) < T (assuming evaluation
2200
+ // / in arithmetic modulo 2^BW, and treating the values as signed) by the
2201
+ // / virtue of *signed* overflow. This function will *not* find such an n,
2202
+ // / however it may find a value of n satisfying the inequalities due to
2203
+ // / an *unsigned* overflow (if the values are treated as unsigned).
2204
+ // / To find a solution for a signed overflow, treat it as a problem of
2205
+ // / finding an unsigned overflow with a range with of BW-1.
2206
+ // /
2207
+ // / The returned value may have a different bit width from the input
2208
+ // / coefficients.
2209
+ Optional<APInt> SolveQuadraticEquationWrap (APInt A, APInt B, APInt C,
2210
+ unsigned RangeWidth);
2169
2211
} // End of APIntOps namespace
2170
2212
2171
2213
// See friend declaration above. This additional declaration is required in
0 commit comments