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