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