LLVM 23.0.0git
APInt.h
Go to the documentation of this file.
1//===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements a class to represent arbitrary precision
11/// integral constant values and operations on them.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_APINT_H
16#define LLVM_ADT_APINT_H
17
21#include <cassert>
22#include <climits>
23#include <cstring>
24#include <optional>
25#include <utility>
26
27namespace llvm {
29class StringRef;
30class hash_code;
31class raw_ostream;
32struct Align;
33class DynamicAPInt;
34
35template <typename T> class SmallVectorImpl;
36template <typename T> class ArrayRef;
37template <typename T, typename Enable> struct DenseMapInfo;
38
39class APInt;
40
41inline APInt operator-(APInt);
42
43//===----------------------------------------------------------------------===//
44// APInt Class
45//===----------------------------------------------------------------------===//
46
47/// Class for arbitrary precision integers.
48///
49/// APInt is a functional replacement for common case unsigned integer type like
50/// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
51/// integer sizes and large integer value types such as 3-bits, 15-bits, or more
52/// than 64-bits of precision. APInt provides a variety of arithmetic operators
53/// and methods to manipulate integer values of any bit-width. It supports both
54/// the typical integer arithmetic and comparison operations as well as bitwise
55/// manipulation.
56///
57/// The class has several invariants worth noting:
58/// * All bit, byte, and word positions are zero-based.
59/// * Once the bit width is set, it doesn't change except by the Truncate,
60/// SignExtend, or ZeroExtend operations.
61/// * All binary operators must be on APInt instances of the same bit width.
62/// Attempting to use these operators on instances with different bit
63/// widths will yield an assertion.
64/// * The value is stored canonically as an unsigned value. For operations
65/// where it makes a difference, there are both signed and unsigned variants
66/// of the operation. For example, sdiv and udiv. However, because the bit
67/// widths must be the same, operations such as Mul and Add produce the same
68/// results regardless of whether the values are interpreted as signed or
69/// not.
70/// * In general, the class tries to follow the style of computation that LLVM
71/// uses in its IR. This simplifies its use for LLVM.
72/// * APInt supports zero-bit-width values, but operations that require bits
73/// are not defined on it (e.g. you cannot ask for the sign of a zero-bit
74/// integer). This means that operations like zero extension and logical
75/// shifts are defined, but sign extension and ashr is not. Zero bit values
76/// compare and hash equal to themselves, and countLeadingZeros returns 0.
77///
78class [[nodiscard]] APInt {
79public:
81
82 /// Byte size of a word.
83 static constexpr unsigned APINT_WORD_SIZE = sizeof(WordType);
84
85 /// Bits in a word.
86 static constexpr unsigned APINT_BITS_PER_WORD = APINT_WORD_SIZE * CHAR_BIT;
87
88 enum class Rounding {
92 };
93
94 static constexpr WordType WORDTYPE_MAX = ~WordType(0);
95
96 /// \name Constructors
97 /// @{
98
99 /// Create a new APInt of numBits width, initialized as val.
100 ///
101 /// If isSigned is true then val is treated as if it were a signed value
102 /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
103 /// will be done. Otherwise, no sign extension occurs (high order bits beyond
104 /// the range of val are zero filled).
105 ///
106 /// \param numBits the bit width of the constructed APInt
107 /// \param val the initial value of the APInt
108 /// \param isSigned how to treat signedness of val
109 /// \param implicitTrunc allow implicit truncation of non-zero/sign bits of
110 /// val beyond the range of numBits
111 APInt(unsigned numBits, uint64_t val, bool isSigned = false,
112 bool implicitTrunc = false)
113 : BitWidth(numBits) {
114 if (!implicitTrunc) {
115 if (isSigned) {
116 if (BitWidth == 0) {
117 assert((val == 0 || val == uint64_t(-1)) &&
118 "Value must be 0 or -1 for signed 0-bit APInt");
119 } else {
120 assert(llvm::isIntN(BitWidth, val) &&
121 "Value is not an N-bit signed value");
122 }
123 } else {
124 if (BitWidth == 0) {
125 assert(val == 0 && "Value must be zero for unsigned 0-bit APInt");
126 } else {
127 assert(llvm::isUIntN(BitWidth, val) &&
128 "Value is not an N-bit unsigned value");
129 }
130 }
131 }
132 if (isSingleWord()) {
133 U.VAL = val;
134 if (implicitTrunc || isSigned)
136 } else {
137 initSlowCase(val, isSigned);
138 }
139 }
140
141 /// Construct an APInt of numBits width, initialized as bigVal[].
142 ///
143 /// Note that bigVal.size() can be smaller or larger than the corresponding
144 /// bit width but any extraneous bits will be dropped.
145 ///
146 /// \param numBits the bit width of the constructed APInt
147 /// \param bigVal a sequence of words to form the initial value of the APInt
148 LLVM_ABI APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
149
150 /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
151 /// deprecated because this constructor is prone to ambiguity with the
152 /// APInt(unsigned, uint64_t, bool) constructor.
153 ///
154 /// Once all uses of this constructor are migrated to other constructors,
155 /// consider marking this overload ""= delete" to prevent calls from being
156 /// incorrectly bound to the APInt(unsigned, uint64_t, bool) constructor.
157 [[deprecated("Use other constructors of APInt")]]
158 LLVM_ABI APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
159
160 /// Construct an APInt from a string representation.
161 ///
162 /// This constructor interprets the string \p str in the given radix. The
163 /// interpretation stops when the first character that is not suitable for the
164 /// radix is encountered, or the end of the string. Acceptable radix values
165 /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
166 /// string to require more bits than numBits.
167 ///
168 /// \param numBits the bit width of the constructed APInt
169 /// \param str the string to be interpreted
170 /// \param radix the radix to use for the conversion
171 LLVM_ABI APInt(unsigned numBits, StringRef str, uint8_t radix);
172
173 /// Default constructor that creates an APInt with a 1-bit zero value.
174 explicit APInt() { U.VAL = 0; }
175
176 /// Copy Constructor.
177 APInt(const APInt &that) : BitWidth(that.BitWidth) {
178 if (isSingleWord())
179 U.VAL = that.U.VAL;
180 else
181 initSlowCase(that);
182 }
183
184 /// Move Constructor.
185 APInt(APInt &&that) : BitWidth(that.BitWidth) {
186 memcpy(&U, &that.U, sizeof(U));
187 that.BitWidth = 0;
188 }
189
190 /// Destructor.
192 if (needsCleanup())
193 delete[] U.pVal;
194 }
195
196 /// @}
197 /// \name Value Generators
198 /// @{
199
200 /// Get the '0' value for the specified bit-width.
201 static APInt getZero(unsigned numBits) { return APInt(numBits, 0); }
202
203 /// Return an APInt zero bits wide.
204 static APInt getZeroWidth() { return getZero(0); }
205
206 /// Gets maximum unsigned value of APInt for specific bit width.
207 static APInt getMaxValue(unsigned numBits) { return getAllOnes(numBits); }
208
209 /// Gets maximum signed value of APInt for a specific bit width.
210 static APInt getSignedMaxValue(unsigned numBits) {
211 APInt API = getAllOnes(numBits);
212 API.clearBit(numBits - 1);
213 return API;
214 }
215
216 /// Gets minimum unsigned value of APInt for a specific bit width.
217 static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
218
219 /// Gets minimum signed value of APInt for a specific bit width.
220 static APInt getSignedMinValue(unsigned numBits) {
221 APInt API(numBits, 0);
222 API.setBit(numBits - 1);
223 return API;
224 }
225
226 /// Get the SignMask for a specific bit width.
227 ///
228 /// This is just a wrapper function of getSignedMinValue(), and it helps code
229 /// readability when we want to get a SignMask.
230 static APInt getSignMask(unsigned BitWidth) {
231 return getSignedMinValue(BitWidth);
232 }
233
234 /// Return an APInt of a specified width with all bits set.
235 static APInt getAllOnes(unsigned numBits) {
236 return APInt(numBits, WORDTYPE_MAX, true);
237 }
238
239 /// Return an APInt with exactly one bit set in the result.
240 static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
241 APInt Res(numBits, 0);
242 Res.setBit(BitNo);
243 return Res;
244 }
245
246 /// Get a value with a block of bits set.
247 ///
248 /// Constructs an APInt value that has a contiguous range of bits set. The
249 /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
250 /// bits will be zero. For example, with parameters(32, 0, 16) you would get
251 /// 0x0000FFFF. Please call getBitsSetWithWrap if \p loBit may be greater than
252 /// \p hiBit.
253 ///
254 /// \param numBits the intended bit width of the result
255 /// \param loBit the index of the lowest bit set.
256 /// \param hiBit the index of the highest bit set.
257 ///
258 /// \returns An APInt value with the requested bits set.
259 static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
260 APInt Res(numBits, 0);
261 Res.setBits(loBit, hiBit);
262 return Res;
263 }
264
265 /// Wrap version of getBitsSet.
266 /// If \p hiBit is bigger than \p loBit, this is same with getBitsSet.
267 /// If \p hiBit is not bigger than \p loBit, the set bits "wrap". For example,
268 /// with parameters (32, 28, 4), you would get 0xF000000F.
269 /// If \p hiBit is equal to \p loBit, you would get a result with all bits
270 /// set.
271 static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit,
272 unsigned hiBit) {
273 APInt Res(numBits, 0);
274 Res.setBitsWithWrap(loBit, hiBit);
275 return Res;
276 }
277
278 /// Constructs an APInt value that has a contiguous range of bits set. The
279 /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other
280 /// bits will be zero. For example, with parameters(32, 12) you would get
281 /// 0xFFFFF000.
282 ///
283 /// \param numBits the intended bit width of the result
284 /// \param loBit the index of the lowest bit to set.
285 ///
286 /// \returns An APInt value with the requested bits set.
287 static APInt getBitsSetFrom(unsigned numBits, unsigned loBit) {
288 APInt Res(numBits, 0);
289 Res.setBitsFrom(loBit);
290 return Res;
291 }
292
293 /// Constructs an APInt value that has the top hiBitsSet bits set.
294 ///
295 /// \param numBits the bitwidth of the result
296 /// \param hiBitsSet the number of high-order bits set in the result.
297 static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
298 APInt Res(numBits, 0);
299 Res.setHighBits(hiBitsSet);
300 return Res;
301 }
302
303 /// Constructs an APInt value that has the bottom loBitsSet bits set.
304 ///
305 /// \param numBits the bitwidth of the result
306 /// \param loBitsSet the number of low-order bits set in the result.
307 static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
308 APInt Res(numBits, 0);
309 Res.setLowBits(loBitsSet);
310 return Res;
311 }
312
313 /// Return a value containing V broadcasted over NewLen bits.
314 LLVM_ABI static APInt getSplat(unsigned NewLen, const APInt &V);
315
316 /// @}
317 /// \name Value Tests
318 /// @{
319
320 /// Determine if this APInt just has one word to store value.
321 ///
322 /// \returns true if the number of bits <= 64, false otherwise.
323 bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
324
325 /// Determine sign of this APInt.
326 ///
327 /// This tests the high bit of this APInt to determine if it is set.
328 ///
329 /// \returns true if this APInt is negative, false otherwise
330 bool isNegative() const { return (*this)[BitWidth - 1]; }
331
332 /// Determine if this APInt Value is non-negative (>= 0)
333 ///
334 /// This tests the high bit of the APInt to determine if it is unset.
335 bool isNonNegative() const { return !isNegative(); }
336
337 /// Determine if sign bit of this APInt is set.
338 ///
339 /// This tests the high bit of this APInt to determine if it is set.
340 ///
341 /// \returns true if this APInt has its sign bit set, false otherwise.
342 bool isSignBitSet() const { return (*this)[BitWidth - 1]; }
343
344 /// Determine if sign bit of this APInt is clear.
345 ///
346 /// This tests the high bit of this APInt to determine if it is clear.
347 ///
348 /// \returns true if this APInt has its sign bit clear, false otherwise.
349 bool isSignBitClear() const { return !isSignBitSet(); }
350
351 /// Determine if this APInt Value is positive.
352 ///
353 /// This tests if the value of this APInt is positive (> 0). Note
354 /// that 0 is not a positive value.
355 ///
356 /// \returns true if this APInt is positive.
357 bool isStrictlyPositive() const { return isNonNegative() && !isZero(); }
358
359 /// Determine if this APInt Value is non-positive (<= 0).
360 ///
361 /// \returns true if this APInt is non-positive.
362 bool isNonPositive() const { return !isStrictlyPositive(); }
363
364 /// Determine if this APInt Value only has the specified bit set.
365 ///
366 /// \returns true if this APInt only has the specified bit set.
367 bool isOneBitSet(unsigned BitNo) const {
368 return (*this)[BitNo] && popcount() == 1;
369 }
370
371 /// Determine if all bits are set. This is true for zero-width values.
372 bool isAllOnes() const {
373 if (BitWidth == 0)
374 return true;
375 if (isSingleWord())
376 return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth);
377 return countTrailingOnesSlowCase() == BitWidth;
378 }
379
380 /// Determine if this value is zero, i.e. all bits are clear.
381 bool isZero() const {
382 if (isSingleWord())
383 return U.VAL == 0;
384 return countLeadingZerosSlowCase() == BitWidth;
385 }
386
387 /// Determine if this is a value of 1.
388 ///
389 /// This checks to see if the value of this APInt is one.
390 bool isOne() const {
391 if (isSingleWord())
392 return U.VAL == 1;
393 return countLeadingZerosSlowCase() == BitWidth - 1;
394 }
395
396 /// Determine if this is the largest unsigned value.
397 ///
398 /// This checks to see if the value of this APInt is the maximum unsigned
399 /// value for the APInt's bit width.
400 bool isMaxValue() const { return isAllOnes(); }
401
402 /// Determine if this is the largest signed value.
403 ///
404 /// This checks to see if the value of this APInt is the maximum signed
405 /// value for the APInt's bit width.
406 bool isMaxSignedValue() const {
407 if (isSingleWord()) {
408 assert(BitWidth && "zero width values not allowed");
409 return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1);
410 }
411 return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1;
412 }
413
414 /// Determine if this is the smallest unsigned value.
415 ///
416 /// This checks to see if the value of this APInt is the minimum unsigned
417 /// value for the APInt's bit width.
418 bool isMinValue() const { return isZero(); }
419
420 /// Determine if this is the smallest signed value.
421 ///
422 /// This checks to see if the value of this APInt is the minimum signed
423 /// value for the APInt's bit width.
424 bool isMinSignedValue() const {
425 if (isSingleWord()) {
426 assert(BitWidth && "zero width values not allowed");
427 return U.VAL == (WordType(1) << (BitWidth - 1));
428 }
429 return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1;
430 }
431
432 /// Check if this APInt has an N-bits unsigned integer value.
433 bool isIntN(unsigned N) const { return getActiveBits() <= N; }
434
435 /// Check if this APInt has an N-bits signed integer value.
436 bool isSignedIntN(unsigned N) const { return getSignificantBits() <= N; }
437
438 /// Check if this APInt's value is a power of two greater than zero.
439 ///
440 /// \returns true if the argument APInt value is a power of two > 0.
441 bool isPowerOf2() const {
442 if (isSingleWord()) {
443 assert(BitWidth && "zero width values not allowed");
444 return isPowerOf2_64(U.VAL);
445 }
446 return isPowerOf2SlowCase();
447 }
448
449 /// Check if this APInt's negated value is a power of two greater than zero.
450 bool isNegatedPowerOf2() const {
451 assert(BitWidth && "zero width values not allowed");
452 if (isNonNegative())
453 return false;
454 // NegatedPowerOf2 - shifted mask in the top bits.
455 unsigned LO = countl_one();
456 unsigned TZ = countr_zero();
457 return (LO + TZ) == BitWidth;
458 }
459
460 /// Checks if this APInt -interpreted as an address- is aligned to the
461 /// provided value.
462 LLVM_ABI bool isAligned(Align A) const;
463
464 /// Check if the APInt's value is returned by getSignMask.
465 ///
466 /// \returns true if this is the value returned by getSignMask.
467 bool isSignMask() const { return isMinSignedValue(); }
468
469 /// Convert APInt to a boolean value.
470 ///
471 /// This converts the APInt to a boolean value as a test against zero.
472 bool getBoolValue() const { return !isZero(); }
473
474 /// If this value is smaller than the specified limit, return it, otherwise
475 /// return the limit value. This causes the value to saturate to the limit.
477 return ugt(Limit) ? Limit : getZExtValue();
478 }
479
480 /// Check if the APInt consists of a repeated bit pattern.
481 ///
482 /// e.g. 0x01010101 satisfies isSplat(8).
483 /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit
484 /// width without remainder.
485 LLVM_ABI bool isSplat(unsigned SplatSizeInBits) const;
486
487 /// \returns true if this APInt value is a sequence of \param numBits ones
488 /// starting at the least significant bit with the remainder zero.
489 bool isMask(unsigned numBits) const {
490 assert(numBits != 0 && "numBits must be non-zero");
491 assert(numBits <= BitWidth && "numBits out of range");
492 if (isSingleWord())
493 return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits));
494 unsigned Ones = countTrailingOnesSlowCase();
495 return (numBits == Ones) &&
496 ((Ones + countLeadingZerosSlowCase()) == BitWidth);
497 }
498
499 /// \returns true if this APInt is a non-empty sequence of ones starting at
500 /// the least significant bit with the remainder zero.
501 /// Ex. isMask(0x0000FFFFU) == true.
502 bool isMask() const {
503 if (isSingleWord())
504 return isMask_64(U.VAL);
505 unsigned Ones = countTrailingOnesSlowCase();
506 return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth);
507 }
508
509 /// Return true if this APInt value contains a non-empty sequence of ones with
510 /// the remainder zero.
511 bool isShiftedMask() const {
512 if (isSingleWord())
513 return isShiftedMask_64(U.VAL);
514 unsigned Ones = countPopulationSlowCase();
515 unsigned LeadZ = countLeadingZerosSlowCase();
516 return (Ones + LeadZ + countTrailingZerosSlowCase()) == BitWidth;
517 }
518
519 /// Return true if this APInt value contains a non-empty sequence of ones with
520 /// the remainder zero. If true, \p MaskIdx will specify the index of the
521 /// lowest set bit and \p MaskLen is updated to specify the length of the
522 /// mask, else neither are updated.
523 bool isShiftedMask(unsigned &MaskIdx, unsigned &MaskLen) const {
524 if (isSingleWord())
525 return isShiftedMask_64(U.VAL, MaskIdx, MaskLen);
526 unsigned Ones = countPopulationSlowCase();
527 unsigned LeadZ = countLeadingZerosSlowCase();
528 unsigned TrailZ = countTrailingZerosSlowCase();
529 if ((Ones + LeadZ + TrailZ) != BitWidth)
530 return false;
531 MaskLen = Ones;
532 MaskIdx = TrailZ;
533 return true;
534 }
535
536 /// Compute an APInt containing numBits highbits from this APInt.
537 ///
538 /// Get an APInt with the same BitWidth as this APInt, just zero mask the low
539 /// bits and right shift to the least significant bit.
540 ///
541 /// \returns the high "numBits" bits of this APInt.
542 LLVM_ABI APInt getHiBits(unsigned numBits) const;
543
544 /// Compute an APInt containing numBits lowbits from this APInt.
545 ///
546 /// Get an APInt with the same BitWidth as this APInt, just zero mask the high
547 /// bits.
548 ///
549 /// \returns the low "numBits" bits of this APInt.
550 LLVM_ABI APInt getLoBits(unsigned numBits) const;
551
552 /// Determine if two APInts have the same value, after zero-extending or
553 /// sign-extending (if \p SignedCompare) one of them (if needed!) to ensure
554 /// that the bit-widths match.
555 static bool isSameValue(const APInt &I1, const APInt &I2,
556 bool SignedCompare = false) {
557 if (I1.getBitWidth() == I2.getBitWidth())
558 return I1 == I2;
559
560 auto ZExtOrSExt = [SignedCompare](const APInt &I, unsigned BitWidth) {
561 return SignedCompare ? I.sext(BitWidth) : I.zext(BitWidth);
562 };
563
564 if (I1.getBitWidth() > I2.getBitWidth())
565 return I1 == ZExtOrSExt(I2, I1.getBitWidth());
566
567 return ZExtOrSExt(I1, I2.getBitWidth()) == I2;
568 }
569
570 /// Overload to compute a hash_code for an APInt value.
571 LLVM_ABI friend hash_code hash_value(const APInt &Arg);
572
573 /// This function returns a pointer to the internal storage of the APInt.
574 /// This is useful for writing out the APInt in binary form without any
575 /// conversions.
576 const uint64_t *getRawData() const {
577 if (isSingleWord())
578 return &U.VAL;
579 return &U.pVal[0];
580 }
581
582 /// @}
583 /// \name Unary Operators
584 /// @{
585
586 /// Postfix increment operator. Increment *this by 1.
587 ///
588 /// \returns a new APInt value representing the original value of *this.
590 APInt API(*this);
591 ++(*this);
592 return API;
593 }
594
595 /// Prefix increment operator.
596 ///
597 /// \returns *this incremented by one
598 LLVM_ABI APInt &operator++();
599
600 /// Postfix decrement operator. Decrement *this by 1.
601 ///
602 /// \returns a new APInt value representing the original value of *this.
604 APInt API(*this);
605 --(*this);
606 return API;
607 }
608
609 /// Prefix decrement operator.
610 ///
611 /// \returns *this decremented by one.
612 LLVM_ABI APInt &operator--();
613
614 /// Logical negation operation on this APInt returns true if zero, like normal
615 /// integers.
616 bool operator!() const { return isZero(); }
617
618 /// @}
619 /// \name Assignment Operators
620 /// @{
621
622 /// Copy assignment operator.
623 ///
624 /// \returns *this after assignment of RHS.
626 // The common case (both source or dest being inline) doesn't require
627 // allocation or deallocation.
628 if (isSingleWord() && RHS.isSingleWord()) {
629 U.VAL = RHS.U.VAL;
630 BitWidth = RHS.BitWidth;
631 return *this;
632 }
633
634 assignSlowCase(RHS);
635 return *this;
636 }
637
638 /// Move assignment operator.
640#ifdef EXPENSIVE_CHECKS
641 // Some std::shuffle implementations still do self-assignment.
642 if (this == &that)
643 return *this;
644#endif
645 assert(this != &that && "Self-move not supported");
646 if (!isSingleWord())
647 delete[] U.pVal;
648
649 // Use memcpy so that type based alias analysis sees both VAL and pVal
650 // as modified.
651 memcpy(&U, &that.U, sizeof(U));
652
653 BitWidth = that.BitWidth;
654 that.BitWidth = 0;
655 return *this;
656 }
657
658 /// Assignment operator.
659 ///
660 /// The RHS value is assigned to *this. If the significant bits in RHS exceed
661 /// the bit width, the excess bits are truncated. If the bit width is larger
662 /// than 64, the value is zero filled in the unspecified high order bits.
663 ///
664 /// \returns *this after assignment of RHS value.
666 if (isSingleWord()) {
667 U.VAL = RHS;
668 return clearUnusedBits();
669 }
670 U.pVal[0] = RHS;
671 memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
672 return *this;
673 }
674
675 /// Bitwise AND assignment operator.
676 ///
677 /// Performs a bitwise AND operation on this APInt and RHS. The result is
678 /// assigned to *this.
679 ///
680 /// \returns *this after ANDing with RHS.
682 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
683 if (isSingleWord())
684 U.VAL &= RHS.U.VAL;
685 else
686 andAssignSlowCase(RHS);
687 return *this;
688 }
689
690 /// Bitwise AND assignment operator.
691 ///
692 /// Performs a bitwise AND operation on this APInt and RHS. RHS is
693 /// logically zero-extended or truncated to match the bit-width of
694 /// the LHS.
696 if (isSingleWord()) {
697 U.VAL &= RHS;
698 return *this;
699 }
700 U.pVal[0] &= RHS;
701 memset(U.pVal + 1, 0, (getNumWords() - 1) * APINT_WORD_SIZE);
702 return *this;
703 }
704
705 /// Bitwise OR assignment operator.
706 ///
707 /// Performs a bitwise OR operation on this APInt and RHS. The result is
708 /// assigned *this;
709 ///
710 /// \returns *this after ORing with RHS.
712 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
713 if (isSingleWord())
714 U.VAL |= RHS.U.VAL;
715 else
716 orAssignSlowCase(RHS);
717 return *this;
718 }
719
720 /// Bitwise OR assignment operator.
721 ///
722 /// Performs a bitwise OR operation on this APInt and RHS. RHS is
723 /// logically zero-extended or truncated to match the bit-width of
724 /// the LHS.
726 if (isSingleWord()) {
727 U.VAL |= RHS;
728 return clearUnusedBits();
729 }
730 U.pVal[0] |= RHS;
731 return *this;
732 }
733
734 /// Bitwise XOR assignment operator.
735 ///
736 /// Performs a bitwise XOR operation on this APInt and RHS. The result is
737 /// assigned to *this.
738 ///
739 /// \returns *this after XORing with RHS.
741 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
742 if (isSingleWord())
743 U.VAL ^= RHS.U.VAL;
744 else
745 xorAssignSlowCase(RHS);
746 return *this;
747 }
748
749 /// Bitwise XOR assignment operator.
750 ///
751 /// Performs a bitwise XOR operation on this APInt and RHS. RHS is
752 /// logically zero-extended or truncated to match the bit-width of
753 /// the LHS.
755 if (isSingleWord()) {
756 U.VAL ^= RHS;
757 return clearUnusedBits();
758 }
759 U.pVal[0] ^= RHS;
760 return *this;
761 }
762
763 /// Multiplication assignment operator.
764 ///
765 /// Multiplies this APInt by RHS and assigns the result to *this.
766 ///
767 /// \returns *this
770
771 /// Addition assignment operator.
772 ///
773 /// Adds RHS to *this and assigns the result to *this.
774 ///
775 /// \returns *this
778
779 /// Subtraction assignment operator.
780 ///
781 /// Subtracts RHS from *this and assigns the result to *this.
782 ///
783 /// \returns *this
786
787 /// Left-shift assignment function.
788 ///
789 /// Shifts *this left by shiftAmt and assigns the result to *this.
790 ///
791 /// \returns *this after shifting left by ShiftAmt
792 APInt &operator<<=(unsigned ShiftAmt) {
793 assert(ShiftAmt <= BitWidth && "Invalid shift amount");
794 if (isSingleWord()) {
795 if (ShiftAmt == BitWidth)
796 U.VAL = 0;
797 else
798 U.VAL <<= ShiftAmt;
799 return clearUnusedBits();
800 }
801 shlSlowCase(ShiftAmt);
802 return *this;
803 }
804
805 /// Left-shift assignment function.
806 ///
807 /// Shifts *this left by shiftAmt and assigns the result to *this.
808 ///
809 /// \returns *this after shifting left by ShiftAmt
810 LLVM_ABI APInt &operator<<=(const APInt &ShiftAmt);
811
812 /// @}
813 /// \name Binary Operators
814 /// @{
815
816 /// Multiplication operator.
817 ///
818 /// Multiplies this APInt by RHS and returns the result.
819 LLVM_ABI APInt operator*(const APInt &RHS) const;
820
821 /// Left logical shift operator.
822 ///
823 /// Shifts this APInt left by \p Bits and returns the result.
824 APInt operator<<(unsigned Bits) const { return shl(Bits); }
825
826 /// Left logical shift operator.
827 ///
828 /// Shifts this APInt left by \p Bits and returns the result.
829 APInt operator<<(const APInt &Bits) const { return shl(Bits); }
830
831 /// Arithmetic right-shift function.
832 ///
833 /// Arithmetic right-shift this APInt by shiftAmt.
834 APInt ashr(unsigned ShiftAmt) const {
835 APInt R(*this);
836 R.ashrInPlace(ShiftAmt);
837 return R;
838 }
839
840 /// Arithmetic right-shift this APInt by ShiftAmt in place.
841 void ashrInPlace(unsigned ShiftAmt) {
842 assert(ShiftAmt <= BitWidth && "Invalid shift amount");
843 if (isSingleWord()) {
844 int64_t SExtVAL = SignExtend64(U.VAL, BitWidth);
845 if (ShiftAmt == BitWidth)
846 U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit.
847 else
848 U.VAL = SExtVAL >> ShiftAmt;
850 return;
851 }
852 ashrSlowCase(ShiftAmt);
853 }
854
855 /// Logical right-shift function.
856 ///
857 /// Logical right-shift this APInt by shiftAmt.
858 APInt lshr(unsigned shiftAmt) const {
859 APInt R(*this);
860 R.lshrInPlace(shiftAmt);
861 return R;
862 }
863
864 /// Logical right-shift this APInt by ShiftAmt in place.
865 void lshrInPlace(unsigned ShiftAmt) {
866 assert(ShiftAmt <= BitWidth && "Invalid shift amount");
867 if (isSingleWord()) {
868 if (ShiftAmt == BitWidth)
869 U.VAL = 0;
870 else
871 U.VAL >>= ShiftAmt;
872 return;
873 }
874 lshrSlowCase(ShiftAmt);
875 }
876
877 /// Left-shift function.
878 ///
879 /// Left-shift this APInt by shiftAmt.
880 APInt shl(unsigned shiftAmt) const {
881 APInt R(*this);
882 R <<= shiftAmt;
883 return R;
884 }
885
886 /// relative logical shift right
887 APInt relativeLShr(int RelativeShift) const {
888 return RelativeShift > 0 ? lshr(RelativeShift) : shl(-RelativeShift);
889 }
890
891 /// relative logical shift left
892 APInt relativeLShl(int RelativeShift) const {
893 return relativeLShr(-RelativeShift);
894 }
895
896 /// relative arithmetic shift right
897 APInt relativeAShr(int RelativeShift) const {
898 return RelativeShift > 0 ? ashr(RelativeShift) : shl(-RelativeShift);
899 }
900
901 /// relative arithmetic shift left
902 APInt relativeAShl(int RelativeShift) const {
903 return relativeAShr(-RelativeShift);
904 }
905
906 /// Rotate left by rotateAmt.
907 LLVM_ABI APInt rotl(unsigned rotateAmt) const;
908
909 /// Rotate right by rotateAmt.
910 LLVM_ABI APInt rotr(unsigned rotateAmt) const;
911
912 /// Arithmetic right-shift function.
913 ///
914 /// Arithmetic right-shift this APInt by shiftAmt.
915 APInt ashr(const APInt &ShiftAmt) const {
916 APInt R(*this);
917 R.ashrInPlace(ShiftAmt);
918 return R;
919 }
920
921 /// Arithmetic right-shift this APInt by shiftAmt in place.
922 LLVM_ABI void ashrInPlace(const APInt &shiftAmt);
923
924 /// Logical right-shift function.
925 ///
926 /// Logical right-shift this APInt by shiftAmt.
927 APInt lshr(const APInt &ShiftAmt) const {
928 APInt R(*this);
929 R.lshrInPlace(ShiftAmt);
930 return R;
931 }
932
933 /// Logical right-shift this APInt by ShiftAmt in place.
934 LLVM_ABI void lshrInPlace(const APInt &ShiftAmt);
935
936 /// Left-shift function.
937 ///
938 /// Left-shift this APInt by shiftAmt.
939 APInt shl(const APInt &ShiftAmt) const {
940 APInt R(*this);
941 R <<= ShiftAmt;
942 return R;
943 }
944
945 /// Rotate left by rotateAmt.
946 LLVM_ABI APInt rotl(const APInt &rotateAmt) const;
947
948 /// Rotate right by rotateAmt.
949 LLVM_ABI APInt rotr(const APInt &rotateAmt) const;
950
951 /// Concatenate the bits from "NewLSB" onto the bottom of *this. This is
952 /// equivalent to:
953 /// (this->zext(NewWidth) << NewLSB.getBitWidth()) | NewLSB.zext(NewWidth)
954 APInt concat(const APInt &NewLSB) const {
955 if (getBitWidth() == 0)
956 return NewLSB;
957 /// If the result will be small, then both the merged values are small.
958 unsigned NewWidth = getBitWidth() + NewLSB.getBitWidth();
959 if (NewWidth <= APINT_BITS_PER_WORD)
960 return APInt(NewWidth, (U.VAL << NewLSB.getBitWidth()) | NewLSB.U.VAL);
961 return concatSlowCase(NewLSB);
962 }
963
964 /// Unsigned division operation.
965 ///
966 /// Perform an unsigned divide operation on this APInt by RHS. Both this and
967 /// RHS are treated as unsigned quantities for purposes of this division.
968 ///
969 /// \returns a new APInt value containing the division result, rounded towards
970 /// zero.
971 LLVM_ABI APInt udiv(const APInt &RHS) const;
972 LLVM_ABI APInt udiv(uint64_t RHS) const;
973
974 /// Signed division function for APInt.
975 ///
976 /// Signed divide this APInt by APInt RHS.
977 ///
978 /// The result is rounded towards zero.
979 LLVM_ABI APInt sdiv(const APInt &RHS) const;
980 LLVM_ABI APInt sdiv(int64_t RHS) const;
981
982 /// Unsigned remainder operation.
983 ///
984 /// Perform an unsigned remainder operation on this APInt with RHS being the
985 /// divisor. Both this and RHS are treated as unsigned quantities for purposes
986 /// of this operation.
987 ///
988 /// \returns a new APInt value containing the remainder result
989 LLVM_ABI APInt urem(const APInt &RHS) const;
990 LLVM_ABI uint64_t urem(uint64_t RHS) const;
991
992 /// Function for signed remainder operation.
993 ///
994 /// Signed remainder operation on APInt.
995 ///
996 /// Note that this is a true remainder operation and not a modulo operation
997 /// because the sign follows the sign of the dividend which is *this.
998 LLVM_ABI APInt srem(const APInt &RHS) const;
999 LLVM_ABI int64_t srem(int64_t RHS) const;
1000
1001 /// Dual division/remainder interface.
1002 ///
1003 /// Sometimes it is convenient to divide two APInt values and obtain both the
1004 /// quotient and remainder. This function does both operations in the same
1005 /// computation making it a little more efficient. The pair of input arguments
1006 /// may overlap with the pair of output arguments. It is safe to call
1007 /// udivrem(X, Y, X, Y), for example.
1008 LLVM_ABI static void udivrem(const APInt &LHS, const APInt &RHS,
1009 APInt &Quotient, APInt &Remainder);
1010 LLVM_ABI static void udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient,
1011 uint64_t &Remainder);
1012
1013 LLVM_ABI static void sdivrem(const APInt &LHS, const APInt &RHS,
1014 APInt &Quotient, APInt &Remainder);
1015 LLVM_ABI static void sdivrem(const APInt &LHS, int64_t RHS, APInt &Quotient,
1016 int64_t &Remainder);
1017
1018 // Operations that return overflow indicators.
1019 LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
1020 LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
1021 LLVM_ABI APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
1022 LLVM_ABI APInt usub_ov(const APInt &RHS, bool &Overflow) const;
1023 LLVM_ABI APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
1024 LLVM_ABI APInt smul_ov(const APInt &RHS, bool &Overflow) const;
1025 LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const;
1026 LLVM_ABI APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
1027 LLVM_ABI APInt sshl_ov(unsigned Amt, bool &Overflow) const;
1028 LLVM_ABI APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
1029 LLVM_ABI APInt ushl_ov(unsigned Amt, bool &Overflow) const;
1030
1031 /// Signed integer floor division operation.
1032 ///
1033 /// Rounds towards negative infinity, i.e. 5 / -2 = -3. Iff minimum value
1034 /// divided by -1 set Overflow to true.
1035 LLVM_ABI APInt sfloordiv_ov(const APInt &RHS, bool &Overflow) const;
1036
1037 // Operations that saturate
1038 LLVM_ABI APInt sadd_sat(const APInt &RHS) const;
1039 LLVM_ABI APInt uadd_sat(const APInt &RHS) const;
1040 LLVM_ABI APInt ssub_sat(const APInt &RHS) const;
1041 LLVM_ABI APInt usub_sat(const APInt &RHS) const;
1042 LLVM_ABI APInt smul_sat(const APInt &RHS) const;
1043 LLVM_ABI APInt umul_sat(const APInt &RHS) const;
1044 LLVM_ABI APInt sshl_sat(const APInt &RHS) const;
1045 LLVM_ABI APInt sshl_sat(unsigned RHS) const;
1046 LLVM_ABI APInt ushl_sat(const APInt &RHS) const;
1047 LLVM_ABI APInt ushl_sat(unsigned RHS) const;
1048
1049 /// Array-indexing support.
1050 ///
1051 /// \returns the bit value at bitPosition
1052 bool operator[](unsigned bitPosition) const {
1053 assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
1054 return (maskBit(bitPosition) & getWord(bitPosition)) != 0;
1055 }
1056
1057 /// @}
1058 /// \name Comparison Operators
1059 /// @{
1060
1061 /// Equality operator.
1062 ///
1063 /// Compares this APInt with RHS for the validity of the equality
1064 /// relationship.
1065 bool operator==(const APInt &RHS) const {
1066 assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
1067 if (isSingleWord())
1068 return U.VAL == RHS.U.VAL;
1069 return equalSlowCase(RHS);
1070 }
1071
1072 /// Equality operator.
1073 ///
1074 /// Compares this APInt with a uint64_t for the validity of the equality
1075 /// relationship.
1076 ///
1077 /// \returns true if *this == Val
1078 bool operator==(uint64_t Val) const {
1079 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val;
1080 }
1081
1082 /// Equality comparison.
1083 ///
1084 /// Compares this APInt with RHS for the validity of the equality
1085 /// relationship.
1086 ///
1087 /// \returns true if *this == Val
1088 bool eq(const APInt &RHS) const { return (*this) == RHS; }
1089
1090 /// Inequality operator.
1091 ///
1092 /// Compares this APInt with RHS for the validity of the inequality
1093 /// relationship.
1094 ///
1095 /// \returns true if *this != Val
1096 bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
1097
1098 /// Inequality operator.
1099 ///
1100 /// Compares this APInt with a uint64_t for the validity of the inequality
1101 /// relationship.
1102 ///
1103 /// \returns true if *this != Val
1104 bool operator!=(uint64_t Val) const { return !((*this) == Val); }
1105
1106 /// Inequality comparison
1107 ///
1108 /// Compares this APInt with RHS for the validity of the inequality
1109 /// relationship.
1110 ///
1111 /// \returns true if *this != Val
1112 bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1113
1114 /// Unsigned less than comparison
1115 ///
1116 /// Regards both *this and RHS as unsigned quantities and compares them for
1117 /// the validity of the less-than relationship.
1118 ///
1119 /// \returns true if *this < RHS when both are considered unsigned.
1120 bool ult(const APInt &RHS) const { return compare(RHS) < 0; }
1121
1122 /// Unsigned less than comparison
1123 ///
1124 /// Regards both *this as an unsigned quantity and compares it with RHS for
1125 /// the validity of the less-than relationship.
1126 ///
1127 /// \returns true if *this < RHS when considered unsigned.
1128 bool ult(uint64_t RHS) const {
1129 // Only need to check active bits if not a single word.
1130 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS;
1131 }
1132
1133 /// Signed less than comparison
1134 ///
1135 /// Regards both *this and RHS as signed quantities and compares them for
1136 /// validity of the less-than relationship.
1137 ///
1138 /// \returns true if *this < RHS when both are considered signed.
1139 bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; }
1140
1141 /// Signed less than comparison
1142 ///
1143 /// Regards both *this as a signed quantity and compares it with RHS for
1144 /// the validity of the less-than relationship.
1145 ///
1146 /// \returns true if *this < RHS when considered signed.
1147 bool slt(int64_t RHS) const {
1148 return (!isSingleWord() && getSignificantBits() > 64)
1149 ? isNegative()
1150 : getSExtValue() < RHS;
1151 }
1152
1153 /// Unsigned less or equal comparison
1154 ///
1155 /// Regards both *this and RHS as unsigned quantities and compares them for
1156 /// validity of the less-or-equal relationship.
1157 ///
1158 /// \returns true if *this <= RHS when both are considered unsigned.
1159 bool ule(const APInt &RHS) const { return compare(RHS) <= 0; }
1160
1161 /// Unsigned less or equal comparison
1162 ///
1163 /// Regards both *this as an unsigned quantity and compares it with RHS for
1164 /// the validity of the less-or-equal relationship.
1165 ///
1166 /// \returns true if *this <= RHS when considered unsigned.
1167 bool ule(uint64_t RHS) const { return !ugt(RHS); }
1168
1169 /// Signed less or equal comparison
1170 ///
1171 /// Regards both *this and RHS as signed quantities and compares them for
1172 /// validity of the less-or-equal relationship.
1173 ///
1174 /// \returns true if *this <= RHS when both are considered signed.
1175 bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; }
1176
1177 /// Signed less or equal comparison
1178 ///
1179 /// Regards both *this as a signed quantity and compares it with RHS for the
1180 /// validity of the less-or-equal relationship.
1181 ///
1182 /// \returns true if *this <= RHS when considered signed.
1183 bool sle(uint64_t RHS) const { return !sgt(RHS); }
1184
1185 /// Unsigned greater than comparison
1186 ///
1187 /// Regards both *this and RHS as unsigned quantities and compares them for
1188 /// the validity of the greater-than relationship.
1189 ///
1190 /// \returns true if *this > RHS when both are considered unsigned.
1191 bool ugt(const APInt &RHS) const { return !ule(RHS); }
1192
1193 /// Unsigned greater than comparison
1194 ///
1195 /// Regards both *this as an unsigned quantity and compares it with RHS for
1196 /// the validity of the greater-than relationship.
1197 ///
1198 /// \returns true if *this > RHS when considered unsigned.
1199 bool ugt(uint64_t RHS) const {
1200 // Only need to check active bits if not a single word.
1201 return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS;
1202 }
1203
1204 /// Signed greater than comparison
1205 ///
1206 /// Regards both *this and RHS as signed quantities and compares them for the
1207 /// validity of the greater-than relationship.
1208 ///
1209 /// \returns true if *this > RHS when both are considered signed.
1210 bool sgt(const APInt &RHS) const { return !sle(RHS); }
1211
1212 /// Signed greater than comparison
1213 ///
1214 /// Regards both *this as a signed quantity and compares it with RHS for
1215 /// the validity of the greater-than relationship.
1216 ///
1217 /// \returns true if *this > RHS when considered signed.
1218 bool sgt(int64_t RHS) const {
1219 return (!isSingleWord() && getSignificantBits() > 64)
1220 ? !isNegative()
1221 : getSExtValue() > RHS;
1222 }
1223
1224 /// Unsigned greater or equal comparison
1225 ///
1226 /// Regards both *this and RHS as unsigned quantities and compares them for
1227 /// validity of the greater-or-equal relationship.
1228 ///
1229 /// \returns true if *this >= RHS when both are considered unsigned.
1230 bool uge(const APInt &RHS) const { return !ult(RHS); }
1231
1232 /// Unsigned greater or equal comparison
1233 ///
1234 /// Regards both *this as an unsigned quantity and compares it with RHS for
1235 /// the validity of the greater-or-equal relationship.
1236 ///
1237 /// \returns true if *this >= RHS when considered unsigned.
1238 bool uge(uint64_t RHS) const { return !ult(RHS); }
1239
1240 /// Signed greater or equal comparison
1241 ///
1242 /// Regards both *this and RHS as signed quantities and compares them for
1243 /// validity of the greater-or-equal relationship.
1244 ///
1245 /// \returns true if *this >= RHS when both are considered signed.
1246 bool sge(const APInt &RHS) const { return !slt(RHS); }
1247
1248 /// Signed greater or equal comparison
1249 ///
1250 /// Regards both *this as a signed quantity and compares it with RHS for
1251 /// the validity of the greater-or-equal relationship.
1252 ///
1253 /// \returns true if *this >= RHS when considered signed.
1254 bool sge(int64_t RHS) const { return !slt(RHS); }
1255
1256 /// This operation tests if there are any pairs of corresponding bits
1257 /// between this APInt and RHS that are both set.
1258 bool intersects(const APInt &RHS) const {
1259 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1260 if (isSingleWord())
1261 return (U.VAL & RHS.U.VAL) != 0;
1262 return intersectsSlowCase(RHS);
1263 }
1264
1265 /// This operation checks that all bits set in this APInt are also set in RHS.
1266 bool isSubsetOf(const APInt &RHS) const {
1267 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1268 if (isSingleWord())
1269 return (U.VAL & ~RHS.U.VAL) == 0;
1270 return isSubsetOfSlowCase(RHS);
1271 }
1272
1273 /// This operation checks if all bits are set in either this or RHS.
1274 bool isInverseOf(const APInt &RHS) const {
1275 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1276 if (isSingleWord())
1277 return (U.VAL ^ RHS.U.VAL) == llvm::maskTrailingOnes<WordType>(BitWidth);
1278 return isInverseOfSlowCase(RHS);
1279 }
1280
1281 /// @}
1282 /// \name Resizing Operators
1283 /// @{
1284
1285 /// Truncate to new width.
1286 ///
1287 /// Truncate the APInt to a specified width. It is an error to specify a width
1288 /// that is greater than the current width.
1289 LLVM_ABI APInt trunc(unsigned width) const;
1290
1291 /// Truncate to new width with unsigned saturation.
1292 ///
1293 /// If the APInt, treated as unsigned integer, can be losslessly truncated to
1294 /// the new bitwidth, then return truncated APInt. Else, return max value.
1295 LLVM_ABI APInt truncUSat(unsigned width) const;
1296
1297 /// Truncate to new width with signed saturation to signed result.
1298 ///
1299 /// If this APInt, treated as signed integer, can be losslessly truncated to
1300 /// the new bitwidth, then return truncated APInt. Else, return either
1301 /// signed min value if the APInt was negative, or signed max value.
1302 LLVM_ABI APInt truncSSat(unsigned width) const;
1303
1304 /// Truncate to new width with signed saturation to unsigned result.
1305 ///
1306 /// If this APInt, treated as signed integer, can be losslessly truncated to
1307 /// the new bitwidth, then return truncated APInt. Else, return either
1308 /// zero if the APInt was negative, or unsigned max value.
1309 /// If \p width matches the current bit width then no changes are made.
1310 LLVM_ABI APInt truncSSatU(unsigned width) const;
1311
1312 /// Sign extend to a new width.
1313 ///
1314 /// This operation sign extends the APInt to a new width. If the high order
1315 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1316 /// It is an error to specify a width that is less than the
1317 /// current width.
1318 LLVM_ABI APInt sext(unsigned width) const;
1319
1320 /// Zero extend to a new width.
1321 ///
1322 /// This operation zero extends the APInt to a new width. The high order bits
1323 /// are filled with 0 bits. It is an error to specify a width that is less
1324 /// than the current width.
1325 LLVM_ABI APInt zext(unsigned width) const;
1326
1327 /// Sign extend or truncate to width
1328 ///
1329 /// Make this APInt have the bit width given by \p width. The value is sign
1330 /// extended, truncated, or left alone to make it that width.
1331 LLVM_ABI APInt sextOrTrunc(unsigned width) const;
1332
1333 /// Zero extend or truncate to width
1334 ///
1335 /// Make this APInt have the bit width given by \p width. The value is zero
1336 /// extended, truncated, or left alone to make it that width.
1337 LLVM_ABI APInt zextOrTrunc(unsigned width) const;
1338
1339 /// @}
1340 /// \name Bit Manipulation Operators
1341 /// @{
1342
1343 /// Set every bit to 1.
1344 void setAllBits() {
1345 if (isSingleWord())
1346 U.VAL = WORDTYPE_MAX;
1347 else
1348 // Set all the bits in all the words.
1349 memset(U.pVal, -1, getNumWords() * APINT_WORD_SIZE);
1350 // Clear the unused ones
1352 }
1353
1354 /// Set the given bit to 1 whose position is given as "bitPosition".
1355 void setBit(unsigned BitPosition) {
1356 assert(BitPosition < BitWidth && "BitPosition out of range");
1357 WordType Mask = maskBit(BitPosition);
1358 if (isSingleWord())
1359 U.VAL |= Mask;
1360 else
1361 U.pVal[whichWord(BitPosition)] |= Mask;
1362 }
1363
1364 /// Set the sign bit to 1.
1365 void setSignBit() { setBit(BitWidth - 1); }
1366
1367 /// Set a given bit to a given value.
1368 void setBitVal(unsigned BitPosition, bool BitValue) {
1369 if (BitValue)
1370 setBit(BitPosition);
1371 else
1372 clearBit(BitPosition);
1373 }
1374
1375 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
1376 /// This function handles "wrap" case when \p loBit >= \p hiBit, and calls
1377 /// setBits when \p loBit < \p hiBit.
1378 /// For \p loBit == \p hiBit wrap case, set every bit to 1.
1379 void setBitsWithWrap(unsigned loBit, unsigned hiBit) {
1380 assert(hiBit <= BitWidth && "hiBit out of range");
1381 assert(loBit <= BitWidth && "loBit out of range");
1382 if (loBit < hiBit) {
1383 setBits(loBit, hiBit);
1384 return;
1385 }
1386 setLowBits(hiBit);
1387 setHighBits(BitWidth - loBit);
1388 }
1389
1390 /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
1391 /// This function handles case when \p loBit <= \p hiBit.
1392 void setBits(unsigned loBit, unsigned hiBit) {
1393 assert(hiBit <= BitWidth && "hiBit out of range");
1394 assert(loBit <= hiBit && "loBit greater than hiBit");
1395 if (loBit == hiBit)
1396 return;
1397 if (hiBit <= APINT_BITS_PER_WORD) {
1398 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
1399 mask <<= loBit;
1400 if (isSingleWord())
1401 U.VAL |= mask;
1402 else
1403 U.pVal[0] |= mask;
1404 } else {
1405 setBitsSlowCase(loBit, hiBit);
1406 }
1407 }
1408
1409 /// Set the top bits starting from loBit.
1410 void setBitsFrom(unsigned loBit) { return setBits(loBit, BitWidth); }
1411
1412 /// Set the bottom loBits bits.
1413 void setLowBits(unsigned loBits) { return setBits(0, loBits); }
1414
1415 /// Set the top hiBits bits.
1416 void setHighBits(unsigned hiBits) {
1417 return setBits(BitWidth - hiBits, BitWidth);
1418 }
1419
1420 /// Set every bit to 0.
1422 if (isSingleWord())
1423 U.VAL = 0;
1424 else
1425 memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE);
1426 }
1427
1428 /// Set a given bit to 0.
1429 ///
1430 /// Set the given bit to 0 whose position is given as "bitPosition".
1431 void clearBit(unsigned BitPosition) {
1432 assert(BitPosition < BitWidth && "BitPosition out of range");
1433 WordType Mask = ~maskBit(BitPosition);
1434 if (isSingleWord())
1435 U.VAL &= Mask;
1436 else
1437 U.pVal[whichWord(BitPosition)] &= Mask;
1438 }
1439
1440 /// Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
1441 /// This function handles case when \p LoBit <= \p HiBit.
1442 void clearBits(unsigned LoBit, unsigned HiBit) {
1443 assert(HiBit <= BitWidth && "HiBit out of range");
1444 assert(LoBit <= HiBit && "LoBit greater than HiBit");
1445 if (LoBit == HiBit)
1446 return;
1447 if (HiBit <= APINT_BITS_PER_WORD) {
1448 uint64_t Mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (HiBit - LoBit));
1449 Mask = ~(Mask << LoBit);
1450 if (isSingleWord())
1451 U.VAL &= Mask;
1452 else
1453 U.pVal[0] &= Mask;
1454 } else {
1455 clearBitsSlowCase(LoBit, HiBit);
1456 }
1457 }
1458
1459 /// Set bottom loBits bits to 0.
1460 void clearLowBits(unsigned loBits) {
1461 assert(loBits <= BitWidth && "More bits than bitwidth");
1462 APInt Keep = getHighBitsSet(BitWidth, BitWidth - loBits);
1463 *this &= Keep;
1464 }
1465
1466 /// Set top hiBits bits to 0.
1467 void clearHighBits(unsigned hiBits) {
1468 assert(hiBits <= BitWidth && "More bits than bitwidth");
1469 APInt Keep = getLowBitsSet(BitWidth, BitWidth - hiBits);
1470 *this &= Keep;
1471 }
1472
1473 /// Set the sign bit to 0.
1474 void clearSignBit() { clearBit(BitWidth - 1); }
1475
1476 /// Toggle every bit to its opposite value.
1478 if (isSingleWord()) {
1479 U.VAL ^= WORDTYPE_MAX;
1481 } else {
1482 flipAllBitsSlowCase();
1483 }
1484 }
1485
1486 /// Toggles a given bit to its opposite value.
1487 ///
1488 /// Toggle a given bit to its opposite value whose position is given
1489 /// as "bitPosition".
1490 LLVM_ABI void flipBit(unsigned bitPosition);
1491
1492 /// Negate this APInt in place.
1493 void negate() {
1494 flipAllBits();
1495 ++(*this);
1496 }
1497
1498 /// Insert the bits from a smaller APInt starting at bitPosition.
1499 LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition);
1500 LLVM_ABI void insertBits(uint64_t SubBits, unsigned bitPosition,
1501 unsigned numBits);
1502
1503 /// Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
1504 LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const;
1505 LLVM_ABI uint64_t extractBitsAsZExtValue(unsigned numBits,
1506 unsigned bitPosition) const;
1507
1508 /// @}
1509 /// \name Value Characterization Functions
1510 /// @{
1511
1512 /// Return the number of bits in the APInt.
1513 unsigned getBitWidth() const { return BitWidth; }
1514
1515 /// Get the number of words.
1516 ///
1517 /// Here one word's bitwidth equals to that of uint64_t.
1518 ///
1519 /// \returns the number of words to hold the integer value of this APInt.
1520 unsigned getNumWords() const { return getNumWords(BitWidth); }
1521
1522 /// Get the number of words.
1523 ///
1524 /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1525 ///
1526 /// \returns the number of words to hold the integer value with a given bit
1527 /// width.
1528 static unsigned getNumWords(unsigned BitWidth) {
1529 return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1530 }
1531
1532 /// Compute the number of active bits in the value
1533 ///
1534 /// This function returns the number of active bits which is defined as the
1535 /// bit width minus the number of leading zeros. This is used in several
1536 /// computations to see how "wide" the value is.
1537 unsigned getActiveBits() const { return BitWidth - countl_zero(); }
1538
1539 /// Compute the number of active words in the value of this APInt.
1540 ///
1541 /// This is used in conjunction with getActiveData to extract the raw value of
1542 /// the APInt.
1543 unsigned getActiveWords() const {
1544 unsigned numActiveBits = getActiveBits();
1545 return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1546 }
1547
1548 /// Get the minimum bit size for this signed APInt
1549 ///
1550 /// Computes the minimum bit width for this APInt while considering it to be a
1551 /// signed (and probably negative) value. If the value is not negative, this
1552 /// function returns the same value as getActiveBits()+1. Otherwise, it
1553 /// returns the smallest bit width that will retain the negative value. For
1554 /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1555 /// for -1, this function will always return 1.
1556 unsigned getSignificantBits() const {
1557 return BitWidth - getNumSignBits() + 1;
1558 }
1559
1560 /// Get zero extended value
1561 ///
1562 /// This method attempts to return the value of this APInt as a zero extended
1563 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1564 /// uint64_t. Otherwise an assertion will result.
1566 if (isSingleWord())
1567 return U.VAL;
1568 assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
1569 return U.pVal[0];
1570 }
1571
1572 /// Get zero extended value if possible
1573 ///
1574 /// This method attempts to return the value of this APInt as a zero extended
1575 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1576 /// uint64_t. Otherwise no value is returned.
1577 std::optional<uint64_t> tryZExtValue() const {
1578 return (getActiveBits() <= 64) ? std::optional<uint64_t>(getZExtValue())
1579 : std::nullopt;
1580 };
1581
1582 /// Get sign extended value
1583 ///
1584 /// This method attempts to return the value of this APInt as a sign extended
1585 /// int64_t. The bit width must be <= 64 or the value must fit within an
1586 /// int64_t. Otherwise an assertion will result.
1587 int64_t getSExtValue() const {
1588 if (isSingleWord())
1589 return SignExtend64(U.VAL, BitWidth);
1590 assert(getSignificantBits() <= 64 && "Too many bits for int64_t");
1591 return int64_t(U.pVal[0]);
1592 }
1593
1594 /// Get sign extended value if possible
1595 ///
1596 /// This method attempts to return the value of this APInt as a sign extended
1597 /// int64_t. The bitwidth must be <= 64 or the value must fit within an
1598 /// int64_t. Otherwise no value is returned.
1599 std::optional<int64_t> trySExtValue() const {
1600 return (getSignificantBits() <= 64) ? std::optional<int64_t>(getSExtValue())
1601 : std::nullopt;
1602 };
1603
1604 /// Get bits required for string value.
1605 ///
1606 /// This method determines how many bits are required to hold the APInt
1607 /// equivalent of the string given by \p str.
1608 LLVM_ABI static unsigned getBitsNeeded(StringRef str, uint8_t radix);
1609
1610 /// Get the bits that are sufficient to represent the string value. This may
1611 /// over estimate the amount of bits required, but it does not require
1612 /// parsing the value in the string.
1613 LLVM_ABI static unsigned getSufficientBitsNeeded(StringRef Str,
1614 uint8_t Radix);
1615
1616 /// The APInt version of std::countl_zero.
1617 ///
1618 /// It counts the number of zeros from the most significant bit to the first
1619 /// one bit.
1620 ///
1621 /// \returns BitWidth if the value is zero, otherwise returns the number of
1622 /// zeros from the most significant bit to the first one bits.
1623 unsigned countl_zero() const {
1624 if (isSingleWord()) {
1625 unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1626 return llvm::countl_zero(U.VAL) - unusedBits;
1627 }
1628 return countLeadingZerosSlowCase();
1629 }
1630
1631 unsigned countLeadingZeros() const { return countl_zero(); }
1632
1633 /// Count the number of leading one bits.
1634 ///
1635 /// This function is an APInt version of std::countl_one. It counts the number
1636 /// of ones from the most significant bit to the first zero bit.
1637 ///
1638 /// \returns 0 if the high order bit is not set, otherwise returns the number
1639 /// of 1 bits from the most significant to the least
1640 unsigned countl_one() const {
1641 if (isSingleWord()) {
1642 if (LLVM_UNLIKELY(BitWidth == 0))
1643 return 0;
1644 return llvm::countl_one(U.VAL << (APINT_BITS_PER_WORD - BitWidth));
1645 }
1646 return countLeadingOnesSlowCase();
1647 }
1648
1649 unsigned countLeadingOnes() const { return countl_one(); }
1650
1651 /// Computes the number of leading bits of this APInt that are equal to its
1652 /// sign bit.
1653 unsigned getNumSignBits() const {
1654 return isNegative() ? countl_one() : countl_zero();
1655 }
1656
1657 /// Count the number of trailing zero bits.
1658 ///
1659 /// This function is an APInt version of std::countr_zero. It counts the
1660 /// number of zeros from the least significant bit to the first set bit.
1661 ///
1662 /// \returns BitWidth if the value is zero, otherwise returns the number of
1663 /// zeros from the least significant bit to the first one bit.
1664 unsigned countr_zero() const {
1665 if (isSingleWord()) {
1666 unsigned TrailingZeros = llvm::countr_zero(U.VAL);
1667 return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros);
1668 }
1669 return countTrailingZerosSlowCase();
1670 }
1671
1672 unsigned countTrailingZeros() const { return countr_zero(); }
1673
1674 /// Count the number of trailing one bits.
1675 ///
1676 /// This function is an APInt version of std::countr_one. It counts the number
1677 /// of ones from the least significant bit to the first zero bit.
1678 ///
1679 /// \returns BitWidth if the value is all ones, otherwise returns the number
1680 /// of ones from the least significant bit to the first zero bit.
1681 unsigned countr_one() const {
1682 if (isSingleWord())
1683 return llvm::countr_one(U.VAL);
1684 return countTrailingOnesSlowCase();
1685 }
1686
1687 unsigned countTrailingOnes() const { return countr_one(); }
1688
1689 /// Count the number of bits set.
1690 ///
1691 /// This function is an APInt version of std::popcount. It counts the number
1692 /// of 1 bits in the APInt value.
1693 ///
1694 /// \returns 0 if the value is zero, otherwise returns the number of set bits.
1695 unsigned popcount() const {
1696 if (isSingleWord())
1697 return llvm::popcount(U.VAL);
1698 return countPopulationSlowCase();
1699 }
1700
1701 /// @}
1702 /// \name Conversion Functions
1703 /// @{
1704 LLVM_ABI void print(raw_ostream &OS, bool isSigned) const;
1705
1706 /// Converts an APInt to a string and append it to Str. Str is commonly a
1707 /// SmallString. If Radix > 10, UpperCase determine the case of letter
1708 /// digits.
1709 LLVM_ABI void toString(SmallVectorImpl<char> &Str, unsigned Radix,
1710 bool Signed, bool formatAsCLiteral = false,
1711 bool UpperCase = true,
1712 bool InsertSeparators = false) const;
1713
1714 /// Considers the APInt to be unsigned and converts it into a string in the
1715 /// radix given. The radix can be 2, 8, 10 16, or 36.
1716 void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1717 toString(Str, Radix, false, false);
1718 }
1719
1720 /// Considers the APInt to be signed and converts it into a string in the
1721 /// radix given. The radix can be 2, 8, 10, 16, or 36.
1722 void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1723 toString(Str, Radix, true, false);
1724 }
1725
1726 /// \returns a byte-swapped representation of this APInt Value.
1727 LLVM_ABI APInt byteSwap() const;
1728
1729 /// \returns the value with the bit representation reversed of this APInt
1730 /// Value.
1731 LLVM_ABI APInt reverseBits() const;
1732
1733 /// Converts this APInt to a double value.
1734 LLVM_ABI double roundToDouble(bool isSigned) const;
1735
1736 /// Converts this unsigned APInt to a double value.
1737 double roundToDouble() const { return roundToDouble(false); }
1738
1739 /// Converts this signed APInt to a double value.
1740 double signedRoundToDouble() const { return roundToDouble(true); }
1741
1742 /// Converts APInt bits to a double
1743 ///
1744 /// The conversion does not do a translation from integer to double, it just
1745 /// re-interprets the bits as a double. Note that it is valid to do this on
1746 /// any bit width. Exactly 64 bits will be translated.
1747 double bitsToDouble() const { return llvm::bit_cast<double>(getWord(0)); }
1748
1749#ifdef HAS_IEE754_FLOAT128
1750 float128 bitsToQuad() const {
1751 __uint128_t ul = ((__uint128_t)U.pVal[1] << 64) + U.pVal[0];
1752 return llvm::bit_cast<float128>(ul);
1753 }
1754#endif
1755
1756 /// Converts APInt bits to a float
1757 ///
1758 /// The conversion does not do a translation from integer to float, it just
1759 /// re-interprets the bits as a float. Note that it is valid to do this on
1760 /// any bit width. Exactly 32 bits will be translated.
1761 float bitsToFloat() const {
1762 return llvm::bit_cast<float>(static_cast<uint32_t>(getWord(0)));
1763 }
1764
1765 /// Converts a double to APInt bits.
1766 ///
1767 /// The conversion does not do a translation from double to integer, it just
1768 /// re-interprets the bits of the double.
1769 static APInt doubleToBits(double V) {
1770 return APInt(sizeof(double) * CHAR_BIT, llvm::bit_cast<uint64_t>(V));
1771 }
1772
1773 /// Converts a float to APInt bits.
1774 ///
1775 /// The conversion does not do a translation from float to integer, it just
1776 /// re-interprets the bits of the float.
1777 static APInt floatToBits(float V) {
1778 return APInt(sizeof(float) * CHAR_BIT, llvm::bit_cast<uint32_t>(V));
1779 }
1780
1781 /// @}
1782 /// \name Mathematics Operations
1783 /// @{
1784
1785 /// \returns the floor log base 2 of this APInt.
1786 unsigned logBase2() const { return getActiveBits() - 1; }
1787
1788 /// \returns the ceil log base 2 of this APInt.
1789 unsigned ceilLogBase2() const {
1790 APInt temp(*this);
1791 --temp;
1792 return temp.getActiveBits();
1793 }
1794
1795 /// \returns the nearest log base 2 of this APInt. Ties round up.
1796 ///
1797 /// NOTE: When we have a BitWidth of 1, we define:
1798 ///
1799 /// log2(0) = UINT32_MAX
1800 /// log2(1) = 0
1801 ///
1802 /// to get around any mathematical concerns resulting from
1803 /// referencing 2 in a space where 2 does no exist.
1804 LLVM_ABI unsigned nearestLogBase2() const;
1805
1806 /// \returns the log base 2 of this APInt if its an exact power of two, -1
1807 /// otherwise
1808 int32_t exactLogBase2() const {
1809 if (!isPowerOf2())
1810 return -1;
1811 return logBase2();
1812 }
1813
1814 /// Compute the floor of the square root of the unsigned value.
1815 LLVM_ABI APInt sqrtFloor() const;
1816
1817 /// Get the absolute value. If *this is < 0 then return -(*this), otherwise
1818 /// *this. Note that the "most negative" signed number (e.g. -128 for 8 bit
1819 /// wide APInt) is unchanged due to how negation works.
1820 APInt abs() const {
1821 if (isNegative())
1822 return -(*this);
1823 return *this;
1824 }
1825
1826 /// \returns the multiplicative inverse of an odd APInt modulo 2^BitWidth.
1827 LLVM_ABI APInt multiplicativeInverse() const;
1828
1829 /// @}
1830 /// \name Building-block Operations for APInt and APFloat
1831 /// @{
1832
1833 // These building block operations operate on a representation of arbitrary
1834 // precision, two's-complement, bignum integer values. They should be
1835 // sufficient to implement APInt and APFloat bignum requirements. Inputs are
1836 // generally a pointer to the base of an array of integer parts, representing
1837 // an unsigned bignum, and a count of how many parts there are.
1838
1839 /// Sets the least significant part of a bignum to the input value, and zeroes
1840 /// out higher parts.
1841 LLVM_ABI static void tcSet(WordType *, WordType, unsigned);
1842
1843 /// Assign one bignum to another.
1844 LLVM_ABI static void tcAssign(WordType *, const WordType *, unsigned);
1845
1846 /// Returns true if a bignum is zero, false otherwise.
1847 LLVM_ABI static bool tcIsZero(const WordType *, unsigned);
1848
1849 /// Extract the given bit of a bignum; returns 0 or 1. Zero-based.
1850 LLVM_ABI static int tcExtractBit(const WordType *, unsigned bit);
1851
1852 /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
1853 /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
1854 /// significant bit of DST. All high bits above srcBITS in DST are
1855 /// zero-filled.
1856 LLVM_ABI static void tcExtract(WordType *, unsigned dstCount,
1857 const WordType *, unsigned srcBits,
1858 unsigned srcLSB);
1859
1860 /// Set the given bit of a bignum. Zero-based.
1861 LLVM_ABI static void tcSetBit(WordType *, unsigned bit);
1862
1863 /// Clear the given bit of a bignum. Zero-based.
1864 LLVM_ABI static void tcClearBit(WordType *, unsigned bit);
1865
1866 /// Returns the bit number of the least or most significant set bit of a
1867 /// number. If the input number has no bits set -1U is returned.
1868 LLVM_ABI static unsigned tcLSB(const WordType *, unsigned n);
1869 LLVM_ABI static unsigned tcMSB(const WordType *parts, unsigned n);
1870
1871 /// Negate a bignum in-place.
1872 LLVM_ABI static void tcNegate(WordType *, unsigned);
1873
1874 /// DST += RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1875 LLVM_ABI static WordType tcAdd(WordType *, const WordType *, WordType carry,
1876 unsigned);
1877 /// DST += RHS. Returns the carry flag.
1878 LLVM_ABI static WordType tcAddPart(WordType *, WordType, unsigned);
1879
1880 /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1881 LLVM_ABI static WordType tcSubtract(WordType *, const WordType *,
1882 WordType carry, unsigned);
1883 /// DST -= RHS. Returns the carry flag.
1884 LLVM_ABI static WordType tcSubtractPart(WordType *, WordType, unsigned);
1885
1886 /// DST += SRC * MULTIPLIER + PART if add is true
1887 /// DST = SRC * MULTIPLIER + PART if add is false
1888 ///
1889 /// Requires 0 <= DSTPARTS <= SRCPARTS + 1. If DST overlaps SRC they must
1890 /// start at the same point, i.e. DST == SRC.
1891 ///
1892 /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1893 /// Otherwise DST is filled with the least significant DSTPARTS parts of the
1894 /// result, and if all of the omitted higher parts were zero return zero,
1895 /// otherwise overflow occurred and return one.
1896 LLVM_ABI static int tcMultiplyPart(WordType *dst, const WordType *src,
1897 WordType multiplier, WordType carry,
1898 unsigned srcParts, unsigned dstParts,
1899 bool add);
1900
1901 /// DST = LHS * RHS, where DST has the same width as the operands and is
1902 /// filled with the least significant parts of the result. Returns one if
1903 /// overflow occurred, otherwise zero. DST must be disjoint from both
1904 /// operands.
1905 LLVM_ABI static int tcMultiply(WordType *, const WordType *, const WordType *,
1906 unsigned);
1907
1908 /// DST = LHS * RHS, where DST has width the sum of the widths of the
1909 /// operands. No overflow occurs. DST must be disjoint from both operands.
1910 LLVM_ABI static void tcFullMultiply(WordType *, const WordType *,
1911 const WordType *, unsigned, unsigned);
1912
1913 /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1914 /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1915 /// REMAINDER to the remainder, return zero. i.e.
1916 ///
1917 /// OLD_LHS = RHS * LHS + REMAINDER
1918 ///
1919 /// SCRATCH is a bignum of the same size as the operands and result for use by
1920 /// the routine; its contents need not be initialized and are destroyed. LHS,
1921 /// REMAINDER and SCRATCH must be distinct.
1922 LLVM_ABI static int tcDivide(WordType *lhs, const WordType *rhs,
1923 WordType *remainder, WordType *scratch,
1924 unsigned parts);
1925
1926 /// Shift a bignum left Count bits. Shifted in bits are zero. There are no
1927 /// restrictions on Count.
1928 LLVM_ABI static void tcShiftLeft(WordType *, unsigned Words, unsigned Count);
1929
1930 /// Shift a bignum right Count bits. Shifted in bits are zero. There are no
1931 /// restrictions on Count.
1932 LLVM_ABI static void tcShiftRight(WordType *, unsigned Words, unsigned Count);
1933
1934 /// Comparison (unsigned) of two bignums.
1935 LLVM_ABI static int tcCompare(const WordType *, const WordType *, unsigned);
1936
1937 /// Increment a bignum in-place. Return the carry flag.
1938 static WordType tcIncrement(WordType *dst, unsigned parts) {
1939 return tcAddPart(dst, 1, parts);
1940 }
1941
1942 /// Decrement a bignum in-place. Return the borrow flag.
1943 static WordType tcDecrement(WordType *dst, unsigned parts) {
1944 return tcSubtractPart(dst, 1, parts);
1945 }
1946
1947 /// Used to insert APInt objects, or objects that contain APInt objects, into
1948 /// FoldingSets.
1949 LLVM_ABI void Profile(FoldingSetNodeID &id) const;
1950
1951#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1952 /// debug method
1953 LLVM_DUMP_METHOD void dump() const;
1954#endif
1955
1956 /// Returns whether this instance allocated memory.
1957 bool needsCleanup() const { return !isSingleWord(); }
1958
1959private:
1960 /// This union is used to store the integer value. When the
1961 /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
1962 union {
1963 uint64_t VAL; ///< Used to store the <= 64 bits integer value.
1964 uint64_t *pVal; ///< Used to store the >64 bits integer value.
1965 } U;
1966
1967 unsigned BitWidth = 1; ///< The number of bits in this APInt.
1968
1969 friend struct DenseMapInfo<APInt, void>;
1970 friend class APSInt;
1971
1972 // Make DynamicAPInt a friend so it can access BitWidth directly.
1973 friend DynamicAPInt;
1974
1975 /// This constructor is used only internally for speed of construction of
1976 /// temporaries. It is unsafe since it takes ownership of the pointer, so it
1977 /// is not public.
1978 APInt(uint64_t *val, unsigned bits) : BitWidth(bits) { U.pVal = val; }
1979
1980 /// Determine which word a bit is in.
1981 ///
1982 /// \returns the word position for the specified bit position.
1983 static unsigned whichWord(unsigned bitPosition) {
1984 return bitPosition / APINT_BITS_PER_WORD;
1985 }
1986
1987 /// Determine which bit in a word the specified bit position is in.
1988 static unsigned whichBit(unsigned bitPosition) {
1989 return bitPosition % APINT_BITS_PER_WORD;
1990 }
1991
1992 /// Get a single bit mask.
1993 ///
1994 /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
1995 /// This method generates and returns a uint64_t (word) mask for a single
1996 /// bit at a specific bit position. This is used to mask the bit in the
1997 /// corresponding word.
1998 static uint64_t maskBit(unsigned bitPosition) {
1999 return 1ULL << whichBit(bitPosition);
2000 }
2001
2002 /// Clear unused high order bits
2003 ///
2004 /// This method is used internally to clear the top "N" bits in the high order
2005 /// word that are not used by the APInt. This is needed after the most
2006 /// significant word is assigned a value to ensure that those bits are
2007 /// zero'd out.
2008 APInt &clearUnusedBits() {
2009 // Compute how many bits are used in the final word.
2010 unsigned WordBits = ((BitWidth - 1) % APINT_BITS_PER_WORD) + 1;
2011
2012 // Mask out the high bits.
2013 uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - WordBits);
2014 if (LLVM_UNLIKELY(BitWidth == 0))
2015 mask = 0;
2016
2017 if (isSingleWord())
2018 U.VAL &= mask;
2019 else
2020 U.pVal[getNumWords() - 1] &= mask;
2021 return *this;
2022 }
2023
2024 /// Get the word corresponding to a bit position
2025 /// \returns the corresponding word for the specified bit position.
2026 uint64_t getWord(unsigned bitPosition) const {
2027 return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
2028 }
2029
2030 /// Utility method to change the bit width of this APInt to new bit width,
2031 /// allocating and/or deallocating as necessary. There is no guarantee on the
2032 /// value of any bits upon return. Caller should populate the bits after.
2033 void reallocate(unsigned NewBitWidth);
2034
2035 /// Convert a char array into an APInt
2036 ///
2037 /// \param radix 2, 8, 10, 16, or 36
2038 /// Converts a string into a number. The string must be non-empty
2039 /// and well-formed as a number of the given base. The bit-width
2040 /// must be sufficient to hold the result.
2041 ///
2042 /// This is used by the constructors that take string arguments.
2043 ///
2044 /// StringRef::getAsInteger is superficially similar but (1) does
2045 /// not assume that the string is well-formed and (2) grows the
2046 /// result to hold the input.
2047 void fromString(unsigned numBits, StringRef str, uint8_t radix);
2048
2049 /// An internal division function for dividing APInts.
2050 ///
2051 /// This is used by the toString method to divide by the radix. It simply
2052 /// provides a more convenient form of divide for internal use since KnuthDiv
2053 /// has specific constraints on its inputs. If those constraints are not met
2054 /// then it provides a simpler form of divide.
2055 static void divide(const WordType *LHS, unsigned lhsWords,
2056 const WordType *RHS, unsigned rhsWords, WordType *Quotient,
2057 WordType *Remainder);
2058
2059 /// out-of-line slow case for inline constructor
2060 LLVM_ABI void initSlowCase(uint64_t val, bool isSigned);
2061
2062 /// shared code between two array constructors
2063 void initFromArray(ArrayRef<uint64_t> array);
2064
2065 /// out-of-line slow case for inline copy constructor
2066 LLVM_ABI void initSlowCase(const APInt &that);
2067
2068 /// out-of-line slow case for shl
2069 LLVM_ABI void shlSlowCase(unsigned ShiftAmt);
2070
2071 /// out-of-line slow case for lshr.
2072 LLVM_ABI void lshrSlowCase(unsigned ShiftAmt);
2073
2074 /// out-of-line slow case for ashr.
2075 LLVM_ABI void ashrSlowCase(unsigned ShiftAmt);
2076
2077 /// out-of-line slow case for operator=
2078 LLVM_ABI void assignSlowCase(const APInt &RHS);
2079
2080 /// out-of-line slow case for operator==
2081 LLVM_ABI bool equalSlowCase(const APInt &RHS) const LLVM_READONLY;
2082
2083 /// out-of-line slow case for countLeadingZeros
2084 LLVM_ABI unsigned countLeadingZerosSlowCase() const LLVM_READONLY;
2085
2086 /// out-of-line slow case for countLeadingOnes.
2087 LLVM_ABI unsigned countLeadingOnesSlowCase() const LLVM_READONLY;
2088
2089 /// out-of-line slow case for countTrailingZeros.
2090 LLVM_ABI unsigned countTrailingZerosSlowCase() const LLVM_READONLY;
2091
2092 /// out-of-line slow case for countTrailingOnes
2093 LLVM_ABI unsigned countTrailingOnesSlowCase() const LLVM_READONLY;
2094
2095 /// out-of-line slow case for countPopulation
2096 LLVM_ABI unsigned countPopulationSlowCase() const LLVM_READONLY;
2097
2098 /// out-of-line slow case for isPowerOf2
2099 LLVM_ABI bool isPowerOf2SlowCase() const LLVM_READONLY;
2100
2101 /// out-of-line slow case for intersects.
2102 LLVM_ABI bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY;
2103
2104 /// out-of-line slow case for isSubsetOf.
2105 LLVM_ABI bool isSubsetOfSlowCase(const APInt &RHS) const LLVM_READONLY;
2106
2107 /// out-of-line slow case for isInverseOf.
2108 LLVM_ABI bool isInverseOfSlowCase(const APInt &RHS) const LLVM_READONLY;
2109
2110 /// out-of-line slow case for setBits.
2111 LLVM_ABI void setBitsSlowCase(unsigned loBit, unsigned hiBit);
2112
2113 /// out-of-line slow case for clearBits.
2114 LLVM_ABI void clearBitsSlowCase(unsigned LoBit, unsigned HiBit);
2115
2116 /// out-of-line slow case for flipAllBits.
2117 LLVM_ABI void flipAllBitsSlowCase();
2118
2119 /// out-of-line slow case for concat.
2120 LLVM_ABI APInt concatSlowCase(const APInt &NewLSB) const;
2121
2122 /// out-of-line slow case for operator&=.
2123 LLVM_ABI void andAssignSlowCase(const APInt &RHS);
2124
2125 /// out-of-line slow case for operator|=.
2126 LLVM_ABI void orAssignSlowCase(const APInt &RHS);
2127
2128 /// out-of-line slow case for operator^=.
2129 LLVM_ABI void xorAssignSlowCase(const APInt &RHS);
2130
2131 /// Unsigned comparison. Returns -1, 0, or 1 if this APInt is less than, equal
2132 /// to, or greater than RHS.
2133 LLVM_ABI int compare(const APInt &RHS) const LLVM_READONLY;
2134
2135 /// Signed comparison. Returns -1, 0, or 1 if this APInt is less than, equal
2136 /// to, or greater than RHS.
2137 LLVM_ABI int compareSigned(const APInt &RHS) const LLVM_READONLY;
2138
2139 /// @}
2140};
2141
2142inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
2143
2144inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
2145
2146/// Unary bitwise complement operator.
2147///
2148/// \returns an APInt that is the bitwise complement of \p v.
2150 v.flipAllBits();
2151 return v;
2152}
2153
2154inline APInt operator&(APInt a, const APInt &b) {
2155 a &= b;
2156 return a;
2157}
2158
2159inline APInt operator&(const APInt &a, APInt &&b) {
2160 b &= a;
2161 return std::move(b);
2162}
2163
2165 a &= RHS;
2166 return a;
2167}
2168
2170 b &= LHS;
2171 return b;
2172}
2173
2174inline APInt operator|(APInt a, const APInt &b) {
2175 a |= b;
2176 return a;
2177}
2178
2179inline APInt operator|(const APInt &a, APInt &&b) {
2180 b |= a;
2181 return std::move(b);
2182}
2183
2185 a |= RHS;
2186 return a;
2187}
2188
2190 b |= LHS;
2191 return b;
2192}
2193
2194inline APInt operator^(APInt a, const APInt &b) {
2195 a ^= b;
2196 return a;
2197}
2198
2199inline APInt operator^(const APInt &a, APInt &&b) {
2200 b ^= a;
2201 return std::move(b);
2202}
2203
2205 a ^= RHS;
2206 return a;
2207}
2208
2210 b ^= LHS;
2211 return b;
2212}
2213
2215 I.print(OS, true);
2216 return OS;
2217}
2218
2220 v.negate();
2221 return v;
2222}
2223
2224inline APInt operator+(APInt a, const APInt &b) {
2225 a += b;
2226 return a;
2227}
2228
2229inline APInt operator+(const APInt &a, APInt &&b) {
2230 b += a;
2231 return std::move(b);
2232}
2233
2235 a += RHS;
2236 return a;
2237}
2238
2240 b += LHS;
2241 return b;
2242}
2243
2244inline APInt operator-(APInt a, const APInt &b) {
2245 a -= b;
2246 return a;
2247}
2248
2249inline APInt operator-(const APInt &a, APInt &&b) {
2250 b.negate();
2251 b += a;
2252 return std::move(b);
2253}
2254
2256 a -= RHS;
2257 return a;
2258}
2259
2261 b.negate();
2262 b += LHS;
2263 return b;
2264}
2265
2267 a *= RHS;
2268 return a;
2269}
2270
2272 b *= LHS;
2273 return b;
2274}
2275
2276namespace APIntOps {
2277
2278/// Determine the smaller of two APInts considered to be signed.
2279inline const APInt &smin(const APInt &A, const APInt &B) {
2280 return A.slt(B) ? A : B;
2281}
2282
2283/// Determine the larger of two APInts considered to be signed.
2284inline const APInt &smax(const APInt &A, const APInt &B) {
2285 return A.sgt(B) ? A : B;
2286}
2287
2288/// Determine the smaller of two APInts considered to be unsigned.
2289inline const APInt &umin(const APInt &A, const APInt &B) {
2290 return A.ult(B) ? A : B;
2291}
2292
2293/// Determine the larger of two APInts considered to be unsigned.
2294inline const APInt &umax(const APInt &A, const APInt &B) {
2295 return A.ugt(B) ? A : B;
2296}
2297
2298/// Determine the absolute difference of two APInts considered to be signed.
2299inline APInt abds(const APInt &A, const APInt &B) {
2300 return A.sge(B) ? (A - B) : (B - A);
2301}
2302
2303/// Determine the absolute difference of two APInts considered to be unsigned.
2304inline APInt abdu(const APInt &A, const APInt &B) {
2305 return A.uge(B) ? (A - B) : (B - A);
2306}
2307
2308/// Compute the floor of the signed average of C1 and C2
2309LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2);
2310
2311/// Compute the floor of the unsigned average of C1 and C2
2312LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2);
2313
2314/// Compute the ceil of the signed average of C1 and C2
2315LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2);
2316
2317/// Compute the ceil of the unsigned average of C1 and C2
2318LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2);
2319
2320/// Performs (2*N)-bit multiplication on sign-extended operands.
2321/// Returns the high N bits of the multiplication result.
2322LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2);
2323
2324/// Performs (2*N)-bit multiplication on zero-extended operands.
2325/// Returns the high N bits of the multiplication result.
2326LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2);
2327
2328/// Performs (2*N)-bit multiplication on sign-extended operands.
2329LLVM_ABI APInt mulsExtended(const APInt &C1, const APInt &C2);
2330
2331/// Performs (2*N)-bit multiplication on zero-extended operands.
2332LLVM_ABI APInt muluExtended(const APInt &C1, const APInt &C2);
2333
2334/// Compute X^N for N>=0.
2335/// 0^0 is supported and returns 1.
2336LLVM_ABI APInt pow(const APInt &X, int64_t N);
2337
2338/// Compute GCD of two unsigned APInt values.
2339///
2340/// This function returns the greatest common divisor of the two APInt values
2341/// using Stein's algorithm.
2342///
2343/// \returns the greatest common divisor of A and B.
2344LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B);
2345
2346/// Converts the given APInt to a double value.
2347///
2348/// Treats the APInt as an unsigned value for conversion purposes.
2349inline double RoundAPIntToDouble(const APInt &APIVal) {
2350 return APIVal.roundToDouble();
2351}
2352
2353/// Converts the given APInt to a double value.
2354///
2355/// Treats the APInt as a signed value for conversion purposes.
2356inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
2357 return APIVal.signedRoundToDouble();
2358}
2359
2360/// Converts the given APInt to a float value.
2361inline float RoundAPIntToFloat(const APInt &APIVal) {
2362 return float(RoundAPIntToDouble(APIVal));
2363}
2364
2365/// Converts the given APInt to a float value.
2366///
2367/// Treats the APInt as a signed value for conversion purposes.
2368inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
2369 return float(APIVal.signedRoundToDouble());
2370}
2371
2372/// Converts the given double value into a APInt.
2373///
2374/// This function convert a double value to an APInt value.
2375LLVM_ABI APInt RoundDoubleToAPInt(double Double, unsigned width);
2376
2377/// Converts a float value into a APInt.
2378///
2379/// Converts a float value into an APInt value.
2380inline APInt RoundFloatToAPInt(float Float, unsigned width) {
2381 return RoundDoubleToAPInt(double(Float), width);
2382}
2383
2384/// Return A unsign-divided by B, rounded by the given rounding mode.
2385LLVM_ABI APInt RoundingUDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2386
2387/// Return A sign-divided by B, rounded by the given rounding mode.
2388LLVM_ABI APInt RoundingSDiv(const APInt &A, const APInt &B, APInt::Rounding RM);
2389
2390/// Let q(n) = An^2 + Bn + C, and BW = bit width of the value range
2391/// (e.g. 32 for i32).
2392/// This function finds the smallest number n, such that
2393/// (a) n >= 0 and q(n) = 0, or
2394/// (b) n >= 1 and q(n-1) and q(n), when evaluated in the set of all
2395/// integers, belong to two different intervals [Rk, Rk+R),
2396/// where R = 2^BW, and k is an integer.
2397/// The idea here is to find when q(n) "overflows" 2^BW, while at the
2398/// same time "allowing" subtraction. In unsigned modulo arithmetic a
2399/// subtraction (treated as addition of negated numbers) would always
2400/// count as an overflow, but here we want to allow values to decrease
2401/// and increase as long as they are within the same interval.
2402/// Specifically, adding of two negative numbers should not cause an
2403/// overflow (as long as the magnitude does not exceed the bit width).
2404/// On the other hand, given a positive number, adding a negative
2405/// number to it can give a negative result, which would cause the
2406/// value to go from [-2^BW, 0) to [0, 2^BW). In that sense, zero is
2407/// treated as a special case of an overflow.
2408///
2409/// This function returns std::nullopt if after finding k that minimizes the
2410/// positive solution to q(n) = kR, both solutions are contained between
2411/// two consecutive integers.
2412///
2413/// There are cases where q(n) > T, and q(n+1) < T (assuming evaluation
2414/// in arithmetic modulo 2^BW, and treating the values as signed) by the
2415/// virtue of *signed* overflow. This function will *not* find such an n,
2416/// however it may find a value of n satisfying the inequalities due to
2417/// an *unsigned* overflow (if the values are treated as unsigned).
2418/// To find a solution for a signed overflow, treat it as a problem of
2419/// finding an unsigned overflow with a range with of BW-1.
2420///
2421/// The returned value may have a different bit width from the input
2422/// coefficients.
2423LLVM_ABI std::optional<APInt>
2424SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, unsigned RangeWidth);
2425
2426/// Compare two values, and if they are different, return the position of the
2427/// most significant bit that is different in the values.
2428LLVM_ABI std::optional<unsigned> GetMostSignificantDifferentBit(const APInt &A,
2429 const APInt &B);
2430
2431/// Splat/Merge neighboring bits to widen/narrow the bitmask represented
2432/// by \param A to \param NewBitWidth bits.
2433///
2434/// MatchAnyBits: (Default)
2435/// e.g. ScaleBitMask(0b0101, 8) -> 0b00110011
2436/// e.g. ScaleBitMask(0b00011011, 4) -> 0b0111
2437///
2438/// MatchAllBits:
2439/// e.g. ScaleBitMask(0b0101, 8) -> 0b00110011
2440/// e.g. ScaleBitMask(0b00011011, 4) -> 0b0001
2441/// A.getBitwidth() or NewBitWidth must be a whole multiples of the other.
2442LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth,
2443 bool MatchAllBits = false);
2444
2445/// Perform a funnel shift left.
2446///
2447/// Concatenate Hi and Lo (Hi is the most significant bits of the wide value),
2448/// the combined value is shifted left by Shift (modulo the bit width of the
2449/// original arguments), and the most significant bits are extracted to produce
2450/// a result that is the same size as the original arguments.
2451///
2452/// Examples:
2453/// (1) fshl(i8 255, i8 0, i8 15) = 128 (0b10000000)
2454/// (2) fshl(i8 15, i8 15, i8 11) = 120 (0b01111000)
2455/// (3) fshl(i8 0, i8 255, i8 8) = 0 (0b00000000)
2456/// (4) fshl(i8 255, i8 0, i8 15) = fshl(i8 255, i8 0, i8 7) // 15 % 8
2457LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift);
2458
2459/// Perform a funnel shift right.
2460///
2461/// Concatenate Hi and Lo (Hi is the most significant bits of the wide value),
2462/// the combined value is shifted right by Shift (modulo the bit width of the
2463/// original arguments), and the least significant bits are extracted to produce
2464/// a result that is the same size as the original arguments.
2465///
2466/// Examples:
2467/// (1) fshr(i8 255, i8 0, i8 15) = 254 (0b11111110)
2468/// (2) fshr(i8 15, i8 15, i8 11) = 225 (0b11100001)
2469/// (3) fshr(i8 0, i8 255, i8 8) = 255 (0b11111111)
2470/// (4) fshr(i8 255, i8 0, i8 9) = fshr(i8 255, i8 0, i8 1) // 9 % 8
2471LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift);
2472
2473/// Perform a carry-less multiply, also known as XOR multiplication, and return
2474/// low-bits. All arguments and result have the same bitwidth.
2475///
2476/// Examples:
2477/// (1) clmul(i4 1, i4 2) = 2
2478/// (2) clmul(i4 5, i4 6) = 14
2479/// (3) clmul(i4 -4, i4 2) = -8
2480/// (4) clmul(i4 -4, i4 -5) = 4
2481LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS);
2482
2483/// Perform a reversed carry-less multiply.
2484///
2485/// clmulr(a, b) = bitreverse(clmul(bitreverse(a), bitreverse(b)))
2486LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS);
2487
2488/// Perform a carry-less multiply, and return high-bits. All arguments and
2489/// result have the same bitwidth.
2490///
2491/// clmulh(a, b) = clmulr(a, b) >> 1
2492LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS);
2493
2494/// Perform a "compress" operation, also known as pext or bext.
2495///
2496/// Selects the bits from /p Val at the positions where /p Mask has a 1-bit,
2497/// and packs them contiguously into the least significant bits of the result.
2498///
2499/// Examples:
2500/// (1) pext(i8 0b1010'1010, i8 0b1100'1100) = 0b0000'1010
2501/// (2) pext(i8 0b1111'1111, i8 0b1010'1010) = 0b0000'1111
2502LLVM_ABI APInt pext(const APInt &Val, const APInt &Mask);
2503
2504/// Perform an "expand" operation, also known as pdep or bdep.
2505///
2506/// Places the least significant bits of /p Val at the positions where /p Mask
2507/// has a 1-bit, and zeros the remaining bits.
2508///
2509/// Examples:
2510/// (1) pdep(i8 0b0000'1010, i8 0b1100'1100) = 0b1000'1000
2511/// (2) pdep(i8 0b0000'1111, i8 0b1010'1010) = 0b1010'1010
2512LLVM_ABI APInt pdep(const APInt &Val, const APInt &Mask);
2513
2514} // namespace APIntOps
2515
2516// See friend declaration above. This additional declaration is required in
2517// order to compile LLVM with IBM xlC compiler.
2518LLVM_ABI hash_code hash_value(const APInt &Arg);
2519
2520/// Fills the StoreBytes bytes of memory starting from Dst with the integer held
2521/// in IntVal.
2522LLVM_ABI void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
2523 unsigned StoreBytes);
2524
2525/// Loads the integer stored in the LoadBytes bytes starting from Src into
2526/// IntVal, which is assumed to be wide enough and to hold zero.
2527LLVM_ABI void LoadIntFromMemory(APInt &IntVal, const uint8_t *Src,
2528 unsigned LoadBytes);
2529
2530/// Provide DenseMapInfo for APInt.
2531template <> struct DenseMapInfo<APInt, void> {
2532 LLVM_ABI static unsigned getHashValue(const APInt &Key);
2533
2534 static bool isEqual(const APInt &LHS, const APInt &RHS) {
2535 return LHS.getBitWidth() == RHS.getBitWidth() && LHS == RHS;
2536 }
2537};
2538
2539} // namespace llvm
2540
2541#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static const MCExpr * setBits(const MCExpr *Dst, const MCExpr *Value, uint32_t Mask, uint32_t Shift, MCContext &Ctx)
Set bits in a kernel descriptor MCExpr field: return ((Dst & ~Mask) | (Value << Shift))
always inline
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
static constexpr unsigned long long mask(BlockVerifier::State S)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:338
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:672
#define LLVM_READONLY
Definition Compiler.h:324
static bool isSigned(unsigned Opcode)
static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown, const KnownBits &OffsetKnown, const KnownBits &WidthKnown)
static raw_ostream & operator<<(raw_ostream &OS, const MatchPosition &Pos)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static bool isAligned(const Value *Base, Align Alignment, const DataLayout &DL)
Definition Loads.cpp:30
static bool isSplat(Value *V)
Return true if V is a splat of a value (which is used when multiplying a matrix with a scalar).
#define I(x, y, z)
Definition MD5.cpp:57
static const char * toString(MIToken::TokenKind TokenKind)
Definition MIParser.cpp:630
Load MIR Sample Profile
const uint64_t BitWidth
static uint64_t clearUnusedBits(uint64_t Val, unsigned Size)
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1577
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
bool slt(int64_t RHS) const
Signed less than comparison.
Definition APInt.h:1147
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1431
APInt relativeLShr(int RelativeShift) const
relative logical shift right
Definition APInt.h:887
bool isNegatedPowerOf2() const
Check if this APInt's negated value is a power of two greater than zero.
Definition APInt.h:450
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
APInt operator--(int)
Postfix decrement operator.
Definition APInt.h:603
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1565
uint64_t * pVal
Used to store the >64 bits integer value.
Definition APInt.h:1964
friend class APSInt
Definition APInt.h:1970
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1416
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1695
~APInt()
Destructor.
Definition APInt.h:191
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1410
APInt operator<<(const APInt &Bits) const
Left logical shift operator.
Definition APInt.h:829
bool isMask() const
Definition APInt.h:502
APInt operator<<(unsigned Bits) const
Left logical shift operator.
Definition APInt.h:824
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1537
bool sgt(int64_t RHS) const
Signed greater than comparison.
Definition APInt.h:1218
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1355
bool operator[](unsigned bitPosition) const
Array-indexing support.
Definition APInt.h:1052
bool operator!=(const APInt &RHS) const
Inequality operator.
Definition APInt.h:1096
void toStringUnsigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be unsigned and converts it into a string in the radix given.
Definition APInt.h:1716
APInt & operator&=(const APInt &RHS)
Bitwise AND assignment operator.
Definition APInt.h:681
APInt abs() const
Get the absolute value.
Definition APInt.h:1820
unsigned ceilLogBase2() const
Definition APInt.h:1789
unsigned countLeadingOnes() const
Definition APInt.h:1649
APInt relativeLShl(int RelativeShift) const
relative logical shift left
Definition APInt.h:892
APInt & operator=(const APInt &RHS)
Copy assignment operator.
Definition APInt.h:625
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1210
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool isInverseOf(const APInt &RHS) const
This operation checks if all bits are set in either this or RHS.
Definition APInt.h:1274
APInt(unsigned numBits, uint64_t val, bool isSigned=false, bool implicitTrunc=false)
Create a new APInt of numBits width, initialized as val.
Definition APInt.h:111
APInt & operator^=(uint64_t RHS)
Bitwise XOR assignment operator.
Definition APInt.h:754
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1191
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
APInt & operator|=(uint64_t RHS)
Bitwise OR assignment operator.
Definition APInt.h:725
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition APInt.h:467
static APInt floatToBits(float V)
Converts a float to APInt bits.
Definition APInt.h:1777
uint64_t WordType
Definition APInt.h:80
void setSignBit()
Set the sign bit to 1.
Definition APInt.h:1365
static constexpr unsigned APINT_WORD_SIZE
Byte size of a word.
Definition APInt.h:83
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1513
bool sle(uint64_t RHS) const
Signed less or equal comparison.
Definition APInt.h:1183
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1120
bool uge(uint64_t RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1238
bool operator!() const
Logical negation operation on this APInt returns true if zero, like normal integers.
Definition APInt.h:616
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
APInt & operator=(uint64_t RHS)
Assignment operator.
Definition APInt.h:665
APInt relativeAShr(int RelativeShift) const
relative arithmetic shift right
Definition APInt.h:897
APInt(const APInt &that)
Copy Constructor.
Definition APInt.h:177
APInt & operator|=(const APInt &RHS)
Bitwise OR assignment operator.
Definition APInt.h:711
bool isSingleWord() const
Determine if this APInt just has one word to store value.
Definition APInt.h:323
bool operator==(uint64_t Val) const
Equality operator.
Definition APInt.h:1078
APInt operator++(int)
Postfix increment operator.
Definition APInt.h:589
unsigned getNumWords() const
Get the number of words.
Definition APInt.h:1520
bool isMinValue() const
Determine if this is the smallest unsigned value.
Definition APInt.h:418
APInt ashr(const APInt &ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:915
APInt()
Default constructor that creates an APInt with a 1-bit zero value.
Definition APInt.h:174
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
APInt(APInt &&that)
Move Constructor.
Definition APInt.h:185
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
APInt concat(const APInt &NewLSB) const
Concatenate the bits from "NewLSB" onto the bottom of *this.
Definition APInt.h:954
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1258
bool eq(const APInt &RHS) const
Equality comparison.
Definition APInt.h:1088
int32_t exactLogBase2() const
Definition APInt.h:1808
APInt & operator<<=(unsigned ShiftAmt)
Left-shift assignment function.
Definition APInt.h:792
double roundToDouble() const
Converts this unsigned APInt to a double value.
Definition APInt.h:1737
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1421
APInt relativeAShl(int RelativeShift) const
relative arithmetic shift left
Definition APInt.h:902
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:841
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1175
void negate()
Negate this APInt in place.
Definition APInt.h:1493
static WordType tcDecrement(WordType *dst, unsigned parts)
Decrement a bignum in-place. Return the borrow flag.
Definition APInt.h:1943
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1664
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
Definition APInt.h:436
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1653
bool isOneBitSet(unsigned BitNo) const
Determine if this APInt Value only has the specified bit set.
Definition APInt.h:367
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1623
bool operator==(const APInt &RHS) const
Equality operator.
Definition APInt.h:1065
APInt shl(const APInt &ShiftAmt) const
Left-shift function.
Definition APInt.h:939
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI friend hash_code hash_value(const APInt &Arg)
Overload to compute a hash_code for an APInt value.
bool isShiftedMask(unsigned &MaskIdx, unsigned &MaskLen) const
Return true if this APInt value contains a non-empty sequence of ones with the remainder zero.
Definition APInt.h:523
static constexpr WordType WORDTYPE_MAX
Definition APInt.h:94
static LLVM_ABI WordType tcSubtractPart(WordType *, WordType, unsigned)
DST -= RHS. Returns the carry flag.
Definition APInt.cpp:2537
void setBitsWithWrap(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1379
APInt lshr(const APInt &ShiftAmt) const
Logical right-shift function.
Definition APInt.h:927
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
Definition APInt.h:362
unsigned countTrailingZeros() const
Definition APInt.h:1672
unsigned getSignificantBits() const
Get the minimum bit size for this signed APInt.
Definition APInt.h:1556
unsigned countLeadingZeros() const
Definition APInt.h:1631
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
void flipAllBits()
Toggle every bit to its opposite value.
Definition APInt.h:1477
static unsigned getNumWords(unsigned BitWidth)
Get the number of words.
Definition APInt.h:1528
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
bool needsCleanup() const
Returns whether this instance allocated memory.
Definition APInt.h:1957
unsigned countl_one() const
Count the number of leading one bits.
Definition APInt.h:1640
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1460
unsigned logBase2() const
Definition APInt.h:1786
static APInt getZeroWidth()
Return an APInt zero bits wide.
Definition APInt.h:204
double signedRoundToDouble() const
Converts this signed APInt to a double value.
Definition APInt.h:1740
bool isShiftedMask() const
Return true if this APInt value contains a non-empty sequence of ones with the remainder zero.
Definition APInt.h:511
float bitsToFloat() const
Converts APInt bits to a float.
Definition APInt.h:1761
static constexpr unsigned APINT_BITS_PER_WORD
Bits in a word.
Definition APInt.h:86
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:476
bool ule(uint64_t RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1167
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
void setAllBits()
Set every bit to 1.
Definition APInt.h:1344
uint64_t VAL
Used to store the <= 64 bits integer value.
Definition APInt.h:1963
bool ugt(uint64_t RHS) const
Unsigned greater than comparison.
Definition APInt.h:1199
bool sge(int64_t RHS) const
Signed greater or equal comparison.
Definition APInt.h:1254
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
static APInt doubleToBits(double V)
Converts a double to APInt bits.
Definition APInt.h:1769
bool isMask(unsigned numBits) const
Definition APInt.h:489
APInt & operator=(APInt &&that)
Move assignment operator.
Definition APInt.h:639
static WordType tcIncrement(WordType *dst, unsigned parts)
Increment a bignum in-place. Return the carry flag.
Definition APInt.h:1938
APInt & operator^=(const APInt &RHS)
Bitwise XOR assignment operator.
Definition APInt.h:740
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1159
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1392
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
double bitsToDouble() const
Converts APInt bits to a double.
Definition APInt.h:1747
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1266
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
unsigned getActiveWords() const
Compute the number of active words in the value of this APInt.
Definition APInt.h:1543
bool ne(const APInt &RHS) const
Inequality comparison.
Definition APInt.h:1112
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1442
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
Definition APInt.h:342
static LLVM_ABI WordType tcAddPart(WordType *, WordType, unsigned)
DST += RHS. Returns the carry flag.
Definition APInt.cpp:2499
const uint64_t * getRawData() const
This function returns a pointer to the internal storage of the APInt.
Definition APInt.h:576
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1139
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1413
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
Definition APInt.h:433
unsigned countTrailingOnes() const
Definition APInt.h:1687
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1246
std::optional< int64_t > trySExtValue() const
Get sign extended value if possible.
Definition APInt.h:1599
APInt & operator&=(uint64_t RHS)
Bitwise AND assignment operator.
Definition APInt.h:695
LLVM_ABI double roundToDouble(bool isSigned) const
Converts this APInt to a double value.
Definition APInt.cpp:912
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
void clearHighBits(unsigned hiBits)
Set top hiBits bits to 0.
Definition APInt.h:1467
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1587
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:865
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
unsigned countr_one() const
Count the number of trailing one bits.
Definition APInt.h:1681
static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit, unsigned hiBit)
Wrap version of getBitsSet.
Definition APInt.h:271
bool isSignBitClear() const
Determine if sign bit of this APInt is clear.
Definition APInt.h:349
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1230
void setBitVal(unsigned BitPosition, bool BitValue)
Set a given bit to a given value.
Definition APInt.h:1368
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1474
bool isMaxValue() const
Determine if this is the largest unsigned value.
Definition APInt.h:400
void toStringSigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be signed and converts it into a string in the radix given.
Definition APInt.h:1722
bool ult(uint64_t RHS) const
Unsigned less than comparison.
Definition APInt.h:1128
bool operator!=(uint64_t Val) const
Inequality operator.
Definition APInt.h:1104
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class provides support for dynamic arbitrary-precision arithmetic.
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:208
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An opaque object representing a hash code.
Definition Hashing.h:77
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define UINT64_MAX
Definition DataTypes.h:77
LLVM_ABI std::error_code fromString(StringRef String, Metadata &HSAMetadata)
Converts String to HSAMetadata.
float RoundAPIntToFloat(const APInt &APIVal)
Converts the given APInt to a float value.
Definition APInt.h:2361
double RoundAPIntToDouble(const APInt &APIVal)
Converts the given APInt to a double value.
Definition APInt.h:2349
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition APInt.h:2279
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition APInt.h:2284
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition APInt.h:2289
APInt RoundFloatToAPInt(float Float, unsigned width)
Converts a float value into a APInt.
Definition APInt.h:2380
LLVM_ABI APInt RoundDoubleToAPInt(double Double, unsigned width)
Converts the given double value into a APInt.
Definition APInt.cpp:873
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2299
double RoundSignedAPIntToDouble(const APInt &APIVal)
Converts the given APInt to a double value.
Definition APInt.h:2356
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2304
float RoundSignedAPIntToFloat(const APInt &APIVal)
Converts the given APInt to a float value.
Definition APInt.h:2368
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2294
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
constexpr T rotr(T V, int R)
Definition bit.h:399
APInt operator&(APInt a, const APInt &b)
Definition APInt.h:2154
APInt operator*(APInt a, uint64_t RHS)
Definition APInt.h:2266
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2144
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt & operator+=(DynamicAPInt &A, int64_t B)
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt & operator-=(DynamicAPInt &A, int64_t B)
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
APInt operator~(APInt v)
Unary bitwise complement operator.
Definition APInt.h:2149
constexpr int popcount(T Value) noexcept
Count the number of set bits in a value.
Definition bit.h:156
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:273
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt & operator*=(DynamicAPInt &A, int64_t B)
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
APInt operator^(APInt a, const APInt &b)
Definition APInt.h:2194
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
Definition MathExtras.h:261
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
Definition bit.h:302
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
To bit_cast(const From &from) noexcept
Definition bit.h:90
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
APInt operator-(APInt)
Definition APInt.h:2219
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:248
constexpr T reverseBits(T Val)
Reverse the bits in Val.
Definition MathExtras.h:118
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
APInt operator+(APInt a, const APInt &b)
Definition APInt.h:2224
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2174
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:77
constexpr T rotl(T V, int R)
Definition bit.h:386
@ Keep
No function return thunk.
Definition CodeGen.h:162
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static bool isEqual(const APInt &LHS, const APInt &RHS)
Definition APInt.h:2534
static LLVM_ABI unsigned getHashValue(const APInt &Key)
An information struct used to provide DenseMap with the various necessary components for a given valu...