]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ADT/APInt.h
MFC r309362:
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / ADT / APInt.h
1 //===-- llvm/ADT/APInt.h - For Arbitrary Precision Integer -----*- C++ -*--===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file implements a class to represent arbitrary precision
12 /// integral constant values and operations on them.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ADT_APINT_H
17 #define LLVM_ADT_APINT_H
18
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/MathExtras.h"
21 #include <cassert>
22 #include <climits>
23 #include <cstring>
24 #include <string>
25
26 namespace llvm {
27 class FoldingSetNodeID;
28 class StringRef;
29 class hash_code;
30 class raw_ostream;
31
32 template <typename T> class SmallVectorImpl;
33 template <typename T> class ArrayRef;
34
35 // An unsigned host type used as a single part of a multi-part
36 // bignum.
37 typedef uint64_t integerPart;
38
39 const unsigned int host_char_bit = 8;
40 const unsigned int integerPartWidth =
41     host_char_bit * static_cast<unsigned int>(sizeof(integerPart));
42
43 //===----------------------------------------------------------------------===//
44 //                              APInt Class
45 //===----------------------------------------------------------------------===//
46
47 /// \brief 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 ///
73 class APInt {
74   unsigned BitWidth; ///< The number of bits in this APInt.
75
76   /// This union is used to store the integer value. When the
77   /// integer bit-width <= 64, it uses VAL, otherwise it uses pVal.
78   union {
79     uint64_t VAL;   ///< Used to store the <= 64 bits integer value.
80     uint64_t *pVal; ///< Used to store the >64 bits integer value.
81   };
82
83   /// This enum is used to hold the constants we needed for APInt.
84   enum {
85     /// Bits in a word
86     APINT_BITS_PER_WORD =
87         static_cast<unsigned int>(sizeof(uint64_t)) * CHAR_BIT,
88     /// Byte size of a word
89     APINT_WORD_SIZE = static_cast<unsigned int>(sizeof(uint64_t))
90   };
91
92   friend struct DenseMapAPIntKeyInfo;
93
94   /// \brief Fast internal constructor
95   ///
96   /// This constructor is used only internally for speed of construction of
97   /// temporaries. It is unsafe for general use so it is not public.
98   APInt(uint64_t *val, unsigned bits) : BitWidth(bits), pVal(val) {}
99
100   /// \brief Determine if this APInt just has one word to store value.
101   ///
102   /// \returns true if the number of bits <= 64, false otherwise.
103   bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
104
105   /// \brief Determine which word a bit is in.
106   ///
107   /// \returns the word position for the specified bit position.
108   static unsigned whichWord(unsigned bitPosition) {
109     return bitPosition / APINT_BITS_PER_WORD;
110   }
111
112   /// \brief Determine which bit in a word a bit is in.
113   ///
114   /// \returns the bit position in a word for the specified bit position
115   /// in the APInt.
116   static unsigned whichBit(unsigned bitPosition) {
117     return bitPosition % APINT_BITS_PER_WORD;
118   }
119
120   /// \brief Get a single bit mask.
121   ///
122   /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
123   /// This method generates and returns a uint64_t (word) mask for a single
124   /// bit at a specific bit position. This is used to mask the bit in the
125   /// corresponding word.
126   static uint64_t maskBit(unsigned bitPosition) {
127     return 1ULL << whichBit(bitPosition);
128   }
129
130   /// \brief Clear unused high order bits
131   ///
132   /// This method is used internally to clear the top "N" bits in the high order
133   /// word that are not used by the APInt. This is needed after the most
134   /// significant word is assigned a value to ensure that those bits are
135   /// zero'd out.
136   APInt &clearUnusedBits() {
137     // Compute how many bits are used in the final word
138     unsigned wordBits = BitWidth % APINT_BITS_PER_WORD;
139     if (wordBits == 0)
140       // If all bits are used, we want to leave the value alone. This also
141       // avoids the undefined behavior of >> when the shift is the same size as
142       // the word size (64).
143       return *this;
144
145     // Mask out the high bits.
146     uint64_t mask = ~uint64_t(0ULL) >> (APINT_BITS_PER_WORD - wordBits);
147     if (isSingleWord())
148       VAL &= mask;
149     else
150       pVal[getNumWords() - 1] &= mask;
151     return *this;
152   }
153
154   /// \brief Get the word corresponding to a bit position
155   /// \returns the corresponding word for the specified bit position.
156   uint64_t getWord(unsigned bitPosition) const {
157     return isSingleWord() ? VAL : pVal[whichWord(bitPosition)];
158   }
159
160   /// \brief Convert a char array into an APInt
161   ///
162   /// \param radix 2, 8, 10, 16, or 36
163   /// Converts a string into a number.  The string must be non-empty
164   /// and well-formed as a number of the given base. The bit-width
165   /// must be sufficient to hold the result.
166   ///
167   /// This is used by the constructors that take string arguments.
168   ///
169   /// StringRef::getAsInteger is superficially similar but (1) does
170   /// not assume that the string is well-formed and (2) grows the
171   /// result to hold the input.
172   void fromString(unsigned numBits, StringRef str, uint8_t radix);
173
174   /// \brief An internal division function for dividing APInts.
175   ///
176   /// This is used by the toString method to divide by the radix. It simply
177   /// provides a more convenient form of divide for internal use since KnuthDiv
178   /// has specific constraints on its inputs. If those constraints are not met
179   /// then it provides a simpler form of divide.
180   static void divide(const APInt &LHS, unsigned lhsWords, const APInt &RHS,
181                      unsigned rhsWords, APInt *Quotient, APInt *Remainder);
182
183   /// out-of-line slow case for inline constructor
184   void initSlowCase(uint64_t val, bool isSigned);
185
186   /// shared code between two array constructors
187   void initFromArray(ArrayRef<uint64_t> array);
188
189   /// out-of-line slow case for inline copy constructor
190   void initSlowCase(const APInt &that);
191
192   /// out-of-line slow case for shl
193   APInt shlSlowCase(unsigned shiftAmt) const;
194
195   /// out-of-line slow case for operator&
196   APInt AndSlowCase(const APInt &RHS) const;
197
198   /// out-of-line slow case for operator|
199   APInt OrSlowCase(const APInt &RHS) const;
200
201   /// out-of-line slow case for operator^
202   APInt XorSlowCase(const APInt &RHS) const;
203
204   /// out-of-line slow case for operator=
205   APInt &AssignSlowCase(const APInt &RHS);
206
207   /// out-of-line slow case for operator==
208   bool EqualSlowCase(const APInt &RHS) const;
209
210   /// out-of-line slow case for operator==
211   bool EqualSlowCase(uint64_t Val) const;
212
213   /// out-of-line slow case for countLeadingZeros
214   unsigned countLeadingZerosSlowCase() const;
215
216   /// out-of-line slow case for countTrailingOnes
217   unsigned countTrailingOnesSlowCase() const;
218
219   /// out-of-line slow case for countPopulation
220   unsigned countPopulationSlowCase() const;
221
222 public:
223   /// \name Constructors
224   /// @{
225
226   /// \brief Create a new APInt of numBits width, initialized as val.
227   ///
228   /// If isSigned is true then val is treated as if it were a signed value
229   /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
230   /// will be done. Otherwise, no sign extension occurs (high order bits beyond
231   /// the range of val are zero filled).
232   ///
233   /// \param numBits the bit width of the constructed APInt
234   /// \param val the initial value of the APInt
235   /// \param isSigned how to treat signedness of val
236   APInt(unsigned numBits, uint64_t val, bool isSigned = false)
237       : BitWidth(numBits), VAL(0) {
238     assert(BitWidth && "bitwidth too small");
239     if (isSingleWord())
240       VAL = val;
241     else
242       initSlowCase(val, isSigned);
243     clearUnusedBits();
244   }
245
246   /// \brief Construct an APInt of numBits width, initialized as bigVal[].
247   ///
248   /// Note that bigVal.size() can be smaller or larger than the corresponding
249   /// bit width but any extraneous bits will be dropped.
250   ///
251   /// \param numBits the bit width of the constructed APInt
252   /// \param bigVal a sequence of words to form the initial value of the APInt
253   APInt(unsigned numBits, ArrayRef<uint64_t> bigVal);
254
255   /// Equivalent to APInt(numBits, ArrayRef<uint64_t>(bigVal, numWords)), but
256   /// deprecated because this constructor is prone to ambiguity with the
257   /// APInt(unsigned, uint64_t, bool) constructor.
258   ///
259   /// If this overload is ever deleted, care should be taken to prevent calls
260   /// from being incorrectly captured by the APInt(unsigned, uint64_t, bool)
261   /// constructor.
262   APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
263
264   /// \brief Construct an APInt from a string representation.
265   ///
266   /// This constructor interprets the string \p str in the given radix. The
267   /// interpretation stops when the first character that is not suitable for the
268   /// radix is encountered, or the end of the string. Acceptable radix values
269   /// are 2, 8, 10, 16, and 36. It is an error for the value implied by the
270   /// string to require more bits than numBits.
271   ///
272   /// \param numBits the bit width of the constructed APInt
273   /// \param str the string to be interpreted
274   /// \param radix the radix to use for the conversion
275   APInt(unsigned numBits, StringRef str, uint8_t radix);
276
277   /// Simply makes *this a copy of that.
278   /// @brief Copy Constructor.
279   APInt(const APInt &that) : BitWidth(that.BitWidth), VAL(0) {
280     if (isSingleWord())
281       VAL = that.VAL;
282     else
283       initSlowCase(that);
284   }
285
286   /// \brief Move Constructor.
287   APInt(APInt &&that) : BitWidth(that.BitWidth), VAL(that.VAL) {
288     that.BitWidth = 0;
289   }
290
291   /// \brief Destructor.
292   ~APInt() {
293     if (needsCleanup())
294       delete[] pVal;
295   }
296
297   /// \brief Default constructor that creates an uninteresting APInt
298   /// representing a 1-bit zero value.
299   ///
300   /// This is useful for object deserialization (pair this with the static
301   ///  method Read).
302   explicit APInt() : BitWidth(1), VAL(0) {}
303
304   /// \brief Returns whether this instance allocated memory.
305   bool needsCleanup() const { return !isSingleWord(); }
306
307   /// Used to insert APInt objects, or objects that contain APInt objects, into
308   ///  FoldingSets.
309   void Profile(FoldingSetNodeID &id) const;
310
311   /// @}
312   /// \name Value Tests
313   /// @{
314
315   /// \brief Determine sign of this APInt.
316   ///
317   /// This tests the high bit of this APInt to determine if it is set.
318   ///
319   /// \returns true if this APInt is negative, false otherwise
320   bool isNegative() const { return (*this)[BitWidth - 1]; }
321
322   /// \brief Determine if this APInt Value is non-negative (>= 0)
323   ///
324   /// This tests the high bit of the APInt to determine if it is unset.
325   bool isNonNegative() const { return !isNegative(); }
326
327   /// \brief Determine if this APInt Value is positive.
328   ///
329   /// This tests if the value of this APInt is positive (> 0). Note
330   /// that 0 is not a positive value.
331   ///
332   /// \returns true if this APInt is positive.
333   bool isStrictlyPositive() const { return isNonNegative() && !!*this; }
334
335   /// \brief Determine if all bits are set
336   ///
337   /// This checks to see if the value has all bits of the APInt are set or not.
338   bool isAllOnesValue() const {
339     if (isSingleWord())
340       return VAL == ~integerPart(0) >> (APINT_BITS_PER_WORD - BitWidth);
341     return countPopulationSlowCase() == BitWidth;
342   }
343
344   /// \brief Determine if this is the largest unsigned value.
345   ///
346   /// This checks to see if the value of this APInt is the maximum unsigned
347   /// value for the APInt's bit width.
348   bool isMaxValue() const { return isAllOnesValue(); }
349
350   /// \brief Determine if this is the largest signed value.
351   ///
352   /// This checks to see if the value of this APInt is the maximum signed
353   /// value for the APInt's bit width.
354   bool isMaxSignedValue() const {
355     return !isNegative() && countPopulation() == BitWidth - 1;
356   }
357
358   /// \brief Determine if this is the smallest unsigned value.
359   ///
360   /// This checks to see if the value of this APInt is the minimum unsigned
361   /// value for the APInt's bit width.
362   bool isMinValue() const { return !*this; }
363
364   /// \brief Determine if this is the smallest signed value.
365   ///
366   /// This checks to see if the value of this APInt is the minimum signed
367   /// value for the APInt's bit width.
368   bool isMinSignedValue() const {
369     return isNegative() && isPowerOf2();
370   }
371
372   /// \brief Check if this APInt has an N-bits unsigned integer value.
373   bool isIntN(unsigned N) const {
374     assert(N && "N == 0 ???");
375     return getActiveBits() <= N;
376   }
377
378   /// \brief Check if this APInt has an N-bits signed integer value.
379   bool isSignedIntN(unsigned N) const {
380     assert(N && "N == 0 ???");
381     return getMinSignedBits() <= N;
382   }
383
384   /// \brief Check if this APInt's value is a power of two greater than zero.
385   ///
386   /// \returns true if the argument APInt value is a power of two > 0.
387   bool isPowerOf2() const {
388     if (isSingleWord())
389       return isPowerOf2_64(VAL);
390     return countPopulationSlowCase() == 1;
391   }
392
393   /// \brief Check if the APInt's value is returned by getSignBit.
394   ///
395   /// \returns true if this is the value returned by getSignBit.
396   bool isSignBit() const { return isMinSignedValue(); }
397
398   /// \brief Convert APInt to a boolean value.
399   ///
400   /// This converts the APInt to a boolean value as a test against zero.
401   bool getBoolValue() const { return !!*this; }
402
403   /// If this value is smaller than the specified limit, return it, otherwise
404   /// return the limit value.  This causes the value to saturate to the limit.
405   uint64_t getLimitedValue(uint64_t Limit = ~0ULL) const {
406     return (getActiveBits() > 64 || getZExtValue() > Limit) ? Limit
407                                                             : getZExtValue();
408   }
409
410   /// \brief Check if the APInt consists of a repeated bit pattern.
411   ///
412   /// e.g. 0x01010101 satisfies isSplat(8).
413   /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit
414   /// width without remainder.
415   bool isSplat(unsigned SplatSizeInBits) const;
416
417   /// @}
418   /// \name Value Generators
419   /// @{
420
421   /// \brief Gets maximum unsigned value of APInt for specific bit width.
422   static APInt getMaxValue(unsigned numBits) {
423     return getAllOnesValue(numBits);
424   }
425
426   /// \brief Gets maximum signed value of APInt for a specific bit width.
427   static APInt getSignedMaxValue(unsigned numBits) {
428     APInt API = getAllOnesValue(numBits);
429     API.clearBit(numBits - 1);
430     return API;
431   }
432
433   /// \brief Gets minimum unsigned value of APInt for a specific bit width.
434   static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
435
436   /// \brief Gets minimum signed value of APInt for a specific bit width.
437   static APInt getSignedMinValue(unsigned numBits) {
438     APInt API(numBits, 0);
439     API.setBit(numBits - 1);
440     return API;
441   }
442
443   /// \brief Get the SignBit for a specific bit width.
444   ///
445   /// This is just a wrapper function of getSignedMinValue(), and it helps code
446   /// readability when we want to get a SignBit.
447   static APInt getSignBit(unsigned BitWidth) {
448     return getSignedMinValue(BitWidth);
449   }
450
451   /// \brief Get the all-ones value.
452   ///
453   /// \returns the all-ones value for an APInt of the specified bit-width.
454   static APInt getAllOnesValue(unsigned numBits) {
455     return APInt(numBits, UINT64_MAX, true);
456   }
457
458   /// \brief Get the '0' value.
459   ///
460   /// \returns the '0' value for an APInt of the specified bit-width.
461   static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
462
463   /// \brief Compute an APInt containing numBits highbits from this APInt.
464   ///
465   /// Get an APInt with the same BitWidth as this APInt, just zero mask
466   /// the low bits and right shift to the least significant bit.
467   ///
468   /// \returns the high "numBits" bits of this APInt.
469   APInt getHiBits(unsigned numBits) const;
470
471   /// \brief Compute an APInt containing numBits lowbits from this APInt.
472   ///
473   /// Get an APInt with the same BitWidth as this APInt, just zero mask
474   /// the high bits.
475   ///
476   /// \returns the low "numBits" bits of this APInt.
477   APInt getLoBits(unsigned numBits) const;
478
479   /// \brief Return an APInt with exactly one bit set in the result.
480   static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
481     APInt Res(numBits, 0);
482     Res.setBit(BitNo);
483     return Res;
484   }
485
486   /// \brief Get a value with a block of bits set.
487   ///
488   /// Constructs an APInt value that has a contiguous range of bits set. The
489   /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
490   /// bits will be zero. For example, with parameters(32, 0, 16) you would get
491   /// 0x0000FFFF. If hiBit is less than loBit then the set bits "wrap". For
492   /// example, with parameters (32, 28, 4), you would get 0xF000000F.
493   ///
494   /// \param numBits the intended bit width of the result
495   /// \param loBit the index of the lowest bit set.
496   /// \param hiBit the index of the highest bit set.
497   ///
498   /// \returns An APInt value with the requested bits set.
499   static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit) {
500     assert(hiBit <= numBits && "hiBit out of range");
501     assert(loBit < numBits && "loBit out of range");
502     if (hiBit < loBit)
503       return getLowBitsSet(numBits, hiBit) |
504              getHighBitsSet(numBits, numBits - loBit);
505     return getLowBitsSet(numBits, hiBit - loBit).shl(loBit);
506   }
507
508   /// \brief Get a value with high bits set
509   ///
510   /// Constructs an APInt value that has the top hiBitsSet bits set.
511   ///
512   /// \param numBits the bitwidth of the result
513   /// \param hiBitsSet the number of high-order bits set in the result.
514   static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet) {
515     assert(hiBitsSet <= numBits && "Too many bits to set!");
516     // Handle a degenerate case, to avoid shifting by word size
517     if (hiBitsSet == 0)
518       return APInt(numBits, 0);
519     unsigned shiftAmt = numBits - hiBitsSet;
520     // For small values, return quickly
521     if (numBits <= APINT_BITS_PER_WORD)
522       return APInt(numBits, ~0ULL << shiftAmt);
523     return getAllOnesValue(numBits).shl(shiftAmt);
524   }
525
526   /// \brief Get a value with low bits set
527   ///
528   /// Constructs an APInt value that has the bottom loBitsSet bits set.
529   ///
530   /// \param numBits the bitwidth of the result
531   /// \param loBitsSet the number of low-order bits set in the result.
532   static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet) {
533     assert(loBitsSet <= numBits && "Too many bits to set!");
534     // Handle a degenerate case, to avoid shifting by word size
535     if (loBitsSet == 0)
536       return APInt(numBits, 0);
537     if (loBitsSet == APINT_BITS_PER_WORD)
538       return APInt(numBits, UINT64_MAX);
539     // For small values, return quickly.
540     if (loBitsSet <= APINT_BITS_PER_WORD)
541       return APInt(numBits, UINT64_MAX >> (APINT_BITS_PER_WORD - loBitsSet));
542     return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
543   }
544
545   /// \brief Return a value containing V broadcasted over NewLen bits.
546   static APInt getSplat(unsigned NewLen, const APInt &V) {
547     assert(NewLen >= V.getBitWidth() && "Can't splat to smaller bit width!");
548
549     APInt Val = V.zextOrSelf(NewLen);
550     for (unsigned I = V.getBitWidth(); I < NewLen; I <<= 1)
551       Val |= Val << I;
552
553     return Val;
554   }
555
556   /// \brief Determine if two APInts have the same value, after zero-extending
557   /// one of them (if needed!) to ensure that the bit-widths match.
558   static bool isSameValue(const APInt &I1, const APInt &I2) {
559     if (I1.getBitWidth() == I2.getBitWidth())
560       return I1 == I2;
561
562     if (I1.getBitWidth() > I2.getBitWidth())
563       return I1 == I2.zext(I1.getBitWidth());
564
565     return I1.zext(I2.getBitWidth()) == I2;
566   }
567
568   /// \brief Overload to compute a hash_code for an APInt value.
569   friend hash_code hash_value(const APInt &Arg);
570
571   /// This function returns a pointer to the internal storage of the APInt.
572   /// This is useful for writing out the APInt in binary form without any
573   /// conversions.
574   const uint64_t *getRawData() const {
575     if (isSingleWord())
576       return &VAL;
577     return &pVal[0];
578   }
579
580   /// @}
581   /// \name Unary Operators
582   /// @{
583
584   /// \brief Postfix increment operator.
585   ///
586   /// \returns a new APInt value representing *this incremented by one
587   const APInt operator++(int) {
588     APInt API(*this);
589     ++(*this);
590     return API;
591   }
592
593   /// \brief Prefix increment operator.
594   ///
595   /// \returns *this incremented by one
596   APInt &operator++();
597
598   /// \brief Postfix decrement operator.
599   ///
600   /// \returns a new APInt representing *this decremented by one.
601   const APInt operator--(int) {
602     APInt API(*this);
603     --(*this);
604     return API;
605   }
606
607   /// \brief Prefix decrement operator.
608   ///
609   /// \returns *this decremented by one.
610   APInt &operator--();
611
612   /// \brief Unary bitwise complement operator.
613   ///
614   /// Performs a bitwise complement operation on this APInt.
615   ///
616   /// \returns an APInt that is the bitwise complement of *this
617   APInt operator~() const {
618     APInt Result(*this);
619     Result.flipAllBits();
620     return Result;
621   }
622
623   /// \brief Unary negation operator
624   ///
625   /// Negates *this using two's complement logic.
626   ///
627   /// \returns An APInt value representing the negation of *this.
628   APInt operator-() const {
629     APInt Result(*this);
630     Result.flipAllBits();
631     ++Result;
632     return Result;
633   }
634
635   /// \brief Logical negation operator.
636   ///
637   /// Performs logical negation operation on this APInt.
638   ///
639   /// \returns true if *this is zero, false otherwise.
640   bool operator!() const {
641     if (isSingleWord())
642       return !VAL;
643
644     for (unsigned i = 0; i != getNumWords(); ++i)
645       if (pVal[i])
646         return false;
647     return true;
648   }
649
650   /// @}
651   /// \name Assignment Operators
652   /// @{
653
654   /// \brief Copy assignment operator.
655   ///
656   /// \returns *this after assignment of RHS.
657   APInt &operator=(const APInt &RHS) {
658     // If the bitwidths are the same, we can avoid mucking with memory
659     if (isSingleWord() && RHS.isSingleWord()) {
660       VAL = RHS.VAL;
661       BitWidth = RHS.BitWidth;
662       return clearUnusedBits();
663     }
664
665     return AssignSlowCase(RHS);
666   }
667
668   /// @brief Move assignment operator.
669   APInt &operator=(APInt &&that) {
670     if (!isSingleWord()) {
671       // The MSVC STL shipped in 2013 requires that self move assignment be a
672       // no-op.  Otherwise algorithms like stable_sort will produce answers
673       // where half of the output is left in a moved-from state.
674       if (this == &that)
675         return *this;
676       delete[] pVal;
677     }
678
679     // Use memcpy so that type based alias analysis sees both VAL and pVal
680     // as modified.
681     memcpy(&VAL, &that.VAL, sizeof(uint64_t));
682
683     // If 'this == &that', avoid zeroing our own bitwidth by storing to 'that'
684     // first.
685     unsigned ThatBitWidth = that.BitWidth;
686     that.BitWidth = 0;
687     BitWidth = ThatBitWidth;
688
689     return *this;
690   }
691
692   /// \brief Assignment operator.
693   ///
694   /// The RHS value is assigned to *this. If the significant bits in RHS exceed
695   /// the bit width, the excess bits are truncated. If the bit width is larger
696   /// than 64, the value is zero filled in the unspecified high order bits.
697   ///
698   /// \returns *this after assignment of RHS value.
699   APInt &operator=(uint64_t RHS);
700
701   /// \brief Bitwise AND assignment operator.
702   ///
703   /// Performs a bitwise AND operation on this APInt and RHS. The result is
704   /// assigned to *this.
705   ///
706   /// \returns *this after ANDing with RHS.
707   APInt &operator&=(const APInt &RHS);
708
709   /// \brief Bitwise OR assignment operator.
710   ///
711   /// Performs a bitwise OR operation on this APInt and RHS. The result is
712   /// assigned *this;
713   ///
714   /// \returns *this after ORing with RHS.
715   APInt &operator|=(const APInt &RHS);
716
717   /// \brief Bitwise OR assignment operator.
718   ///
719   /// Performs a bitwise OR operation on this APInt and RHS. RHS is
720   /// logically zero-extended or truncated to match the bit-width of
721   /// the LHS.
722   APInt &operator|=(uint64_t RHS) {
723     if (isSingleWord()) {
724       VAL |= RHS;
725       clearUnusedBits();
726     } else {
727       pVal[0] |= RHS;
728     }
729     return *this;
730   }
731
732   /// \brief Bitwise XOR assignment operator.
733   ///
734   /// Performs a bitwise XOR operation on this APInt and RHS. The result is
735   /// assigned to *this.
736   ///
737   /// \returns *this after XORing with RHS.
738   APInt &operator^=(const APInt &RHS);
739
740   /// \brief Multiplication assignment operator.
741   ///
742   /// Multiplies this APInt by RHS and assigns the result to *this.
743   ///
744   /// \returns *this
745   APInt &operator*=(const APInt &RHS);
746
747   /// \brief Addition assignment operator.
748   ///
749   /// Adds RHS to *this and assigns the result to *this.
750   ///
751   /// \returns *this
752   APInt &operator+=(const APInt &RHS);
753
754   /// \brief Subtraction assignment operator.
755   ///
756   /// Subtracts RHS from *this and assigns the result to *this.
757   ///
758   /// \returns *this
759   APInt &operator-=(const APInt &RHS);
760
761   /// \brief Left-shift assignment function.
762   ///
763   /// Shifts *this left by shiftAmt and assigns the result to *this.
764   ///
765   /// \returns *this after shifting left by shiftAmt
766   APInt &operator<<=(unsigned shiftAmt) {
767     *this = shl(shiftAmt);
768     return *this;
769   }
770
771   /// @}
772   /// \name Binary Operators
773   /// @{
774
775   /// \brief Bitwise AND operator.
776   ///
777   /// Performs a bitwise AND operation on *this and RHS.
778   ///
779   /// \returns An APInt value representing the bitwise AND of *this and RHS.
780   APInt operator&(const APInt &RHS) const {
781     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
782     if (isSingleWord())
783       return APInt(getBitWidth(), VAL & RHS.VAL);
784     return AndSlowCase(RHS);
785   }
786   APInt LLVM_ATTRIBUTE_UNUSED_RESULT And(const APInt &RHS) const {
787     return this->operator&(RHS);
788   }
789
790   /// \brief Bitwise OR operator.
791   ///
792   /// Performs a bitwise OR operation on *this and RHS.
793   ///
794   /// \returns An APInt value representing the bitwise OR of *this and RHS.
795   APInt operator|(const APInt &RHS) const {
796     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
797     if (isSingleWord())
798       return APInt(getBitWidth(), VAL | RHS.VAL);
799     return OrSlowCase(RHS);
800   }
801
802   /// \brief Bitwise OR function.
803   ///
804   /// Performs a bitwise or on *this and RHS. This is implemented by simply
805   /// calling operator|.
806   ///
807   /// \returns An APInt value representing the bitwise OR of *this and RHS.
808   APInt LLVM_ATTRIBUTE_UNUSED_RESULT Or(const APInt &RHS) const {
809     return this->operator|(RHS);
810   }
811
812   /// \brief Bitwise XOR operator.
813   ///
814   /// Performs a bitwise XOR operation on *this and RHS.
815   ///
816   /// \returns An APInt value representing the bitwise XOR of *this and RHS.
817   APInt operator^(const APInt &RHS) const {
818     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
819     if (isSingleWord())
820       return APInt(BitWidth, VAL ^ RHS.VAL);
821     return XorSlowCase(RHS);
822   }
823
824   /// \brief Bitwise XOR function.
825   ///
826   /// Performs a bitwise XOR operation on *this and RHS. This is implemented
827   /// through the usage of operator^.
828   ///
829   /// \returns An APInt value representing the bitwise XOR of *this and RHS.
830   APInt LLVM_ATTRIBUTE_UNUSED_RESULT Xor(const APInt &RHS) const {
831     return this->operator^(RHS);
832   }
833
834   /// \brief Multiplication operator.
835   ///
836   /// Multiplies this APInt by RHS and returns the result.
837   APInt operator*(const APInt &RHS) const;
838
839   /// \brief Addition operator.
840   ///
841   /// Adds RHS to this APInt and returns the result.
842   APInt operator+(const APInt &RHS) const;
843   APInt operator+(uint64_t RHS) const;
844
845   /// \brief Subtraction operator.
846   ///
847   /// Subtracts RHS from this APInt and returns the result.
848   APInt operator-(const APInt &RHS) const;
849   APInt operator-(uint64_t RHS) const;
850
851   /// \brief Left logical shift operator.
852   ///
853   /// Shifts this APInt left by \p Bits and returns the result.
854   APInt operator<<(unsigned Bits) const { return shl(Bits); }
855
856   /// \brief Left logical shift operator.
857   ///
858   /// Shifts this APInt left by \p Bits and returns the result.
859   APInt operator<<(const APInt &Bits) const { return shl(Bits); }
860
861   /// \brief Arithmetic right-shift function.
862   ///
863   /// Arithmetic right-shift this APInt by shiftAmt.
864   APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(unsigned shiftAmt) const;
865
866   /// \brief Logical right-shift function.
867   ///
868   /// Logical right-shift this APInt by shiftAmt.
869   APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(unsigned shiftAmt) const;
870
871   /// \brief Left-shift function.
872   ///
873   /// Left-shift this APInt by shiftAmt.
874   APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(unsigned shiftAmt) const {
875     assert(shiftAmt <= BitWidth && "Invalid shift amount");
876     if (isSingleWord()) {
877       if (shiftAmt >= BitWidth)
878         return APInt(BitWidth, 0); // avoid undefined shift results
879       return APInt(BitWidth, VAL << shiftAmt);
880     }
881     return shlSlowCase(shiftAmt);
882   }
883
884   /// \brief Rotate left by rotateAmt.
885   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotl(unsigned rotateAmt) const;
886
887   /// \brief Rotate right by rotateAmt.
888   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(unsigned rotateAmt) const;
889
890   /// \brief Arithmetic right-shift function.
891   ///
892   /// Arithmetic right-shift this APInt by shiftAmt.
893   APInt LLVM_ATTRIBUTE_UNUSED_RESULT ashr(const APInt &shiftAmt) const;
894
895   /// \brief Logical right-shift function.
896   ///
897   /// Logical right-shift this APInt by shiftAmt.
898   APInt LLVM_ATTRIBUTE_UNUSED_RESULT lshr(const APInt &shiftAmt) const;
899
900   /// \brief Left-shift function.
901   ///
902   /// Left-shift this APInt by shiftAmt.
903   APInt LLVM_ATTRIBUTE_UNUSED_RESULT shl(const APInt &shiftAmt) const;
904
905   /// \brief Rotate left by rotateAmt.
906   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotl(const APInt &rotateAmt) const;
907
908   /// \brief Rotate right by rotateAmt.
909   APInt LLVM_ATTRIBUTE_UNUSED_RESULT rotr(const APInt &rotateAmt) const;
910
911   /// \brief Unsigned division operation.
912   ///
913   /// Perform an unsigned divide operation on this APInt by RHS. Both this and
914   /// RHS are treated as unsigned quantities for purposes of this division.
915   ///
916   /// \returns a new APInt value containing the division result
917   APInt LLVM_ATTRIBUTE_UNUSED_RESULT udiv(const APInt &RHS) const;
918
919   /// \brief Signed division function for APInt.
920   ///
921   /// Signed divide this APInt by APInt RHS.
922   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sdiv(const APInt &RHS) const;
923
924   /// \brief Unsigned remainder operation.
925   ///
926   /// Perform an unsigned remainder operation on this APInt with RHS being the
927   /// divisor. Both this and RHS are treated as unsigned quantities for purposes
928   /// of this operation. Note that this is a true remainder operation and not a
929   /// modulo operation because the sign follows the sign of the dividend which
930   /// is *this.
931   ///
932   /// \returns a new APInt value containing the remainder result
933   APInt LLVM_ATTRIBUTE_UNUSED_RESULT urem(const APInt &RHS) const;
934
935   /// \brief Function for signed remainder operation.
936   ///
937   /// Signed remainder operation on APInt.
938   APInt LLVM_ATTRIBUTE_UNUSED_RESULT srem(const APInt &RHS) const;
939
940   /// \brief Dual division/remainder interface.
941   ///
942   /// Sometimes it is convenient to divide two APInt values and obtain both the
943   /// quotient and remainder. This function does both operations in the same
944   /// computation making it a little more efficient. The pair of input arguments
945   /// may overlap with the pair of output arguments. It is safe to call
946   /// udivrem(X, Y, X, Y), for example.
947   static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
948                       APInt &Remainder);
949
950   static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient,
951                       APInt &Remainder);
952
953   // Operations that return overflow indicators.
954   APInt sadd_ov(const APInt &RHS, bool &Overflow) const;
955   APInt uadd_ov(const APInt &RHS, bool &Overflow) const;
956   APInt ssub_ov(const APInt &RHS, bool &Overflow) const;
957   APInt usub_ov(const APInt &RHS, bool &Overflow) const;
958   APInt sdiv_ov(const APInt &RHS, bool &Overflow) const;
959   APInt smul_ov(const APInt &RHS, bool &Overflow) const;
960   APInt umul_ov(const APInt &RHS, bool &Overflow) const;
961   APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
962   APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
963
964   /// \brief Array-indexing support.
965   ///
966   /// \returns the bit value at bitPosition
967   bool operator[](unsigned bitPosition) const {
968     assert(bitPosition < getBitWidth() && "Bit position out of bounds!");
969     return (maskBit(bitPosition) &
970             (isSingleWord() ? VAL : pVal[whichWord(bitPosition)])) !=
971            0;
972   }
973
974   /// @}
975   /// \name Comparison Operators
976   /// @{
977
978   /// \brief Equality operator.
979   ///
980   /// Compares this APInt with RHS for the validity of the equality
981   /// relationship.
982   bool operator==(const APInt &RHS) const {
983     assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
984     if (isSingleWord())
985       return VAL == RHS.VAL;
986     return EqualSlowCase(RHS);
987   }
988
989   /// \brief Equality operator.
990   ///
991   /// Compares this APInt with a uint64_t for the validity of the equality
992   /// relationship.
993   ///
994   /// \returns true if *this == Val
995   bool operator==(uint64_t Val) const {
996     if (isSingleWord())
997       return VAL == Val;
998     return EqualSlowCase(Val);
999   }
1000
1001   /// \brief Equality comparison.
1002   ///
1003   /// Compares this APInt with RHS for the validity of the equality
1004   /// relationship.
1005   ///
1006   /// \returns true if *this == Val
1007   bool eq(const APInt &RHS) const { return (*this) == RHS; }
1008
1009   /// \brief Inequality operator.
1010   ///
1011   /// Compares this APInt with RHS for the validity of the inequality
1012   /// relationship.
1013   ///
1014   /// \returns true if *this != Val
1015   bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
1016
1017   /// \brief Inequality operator.
1018   ///
1019   /// Compares this APInt with a uint64_t for the validity of the inequality
1020   /// relationship.
1021   ///
1022   /// \returns true if *this != Val
1023   bool operator!=(uint64_t Val) const { return !((*this) == Val); }
1024
1025   /// \brief Inequality comparison
1026   ///
1027   /// Compares this APInt with RHS for the validity of the inequality
1028   /// relationship.
1029   ///
1030   /// \returns true if *this != Val
1031   bool ne(const APInt &RHS) const { return !((*this) == RHS); }
1032
1033   /// \brief Unsigned less than comparison
1034   ///
1035   /// Regards both *this and RHS as unsigned quantities and compares them for
1036   /// the validity of the less-than relationship.
1037   ///
1038   /// \returns true if *this < RHS when both are considered unsigned.
1039   bool ult(const APInt &RHS) const;
1040
1041   /// \brief Unsigned less than comparison
1042   ///
1043   /// Regards both *this as an unsigned quantity and compares it with RHS for
1044   /// the validity of the less-than relationship.
1045   ///
1046   /// \returns true if *this < RHS when considered unsigned.
1047   bool ult(uint64_t RHS) const {
1048     return getActiveBits() > 64 ? false : getZExtValue() < RHS;
1049   }
1050
1051   /// \brief Signed less than comparison
1052   ///
1053   /// Regards both *this and RHS as signed quantities and compares them for
1054   /// validity of the less-than relationship.
1055   ///
1056   /// \returns true if *this < RHS when both are considered signed.
1057   bool slt(const APInt &RHS) const;
1058
1059   /// \brief Signed less than comparison
1060   ///
1061   /// Regards both *this as a signed quantity and compares it with RHS for
1062   /// the validity of the less-than relationship.
1063   ///
1064   /// \returns true if *this < RHS when considered signed.
1065   bool slt(int64_t RHS) const {
1066     return getMinSignedBits() > 64 ? isNegative() : getSExtValue() < RHS;
1067   }
1068
1069   /// \brief Unsigned less or equal comparison
1070   ///
1071   /// Regards both *this and RHS as unsigned quantities and compares them for
1072   /// validity of the less-or-equal relationship.
1073   ///
1074   /// \returns true if *this <= RHS when both are considered unsigned.
1075   bool ule(const APInt &RHS) const { return ult(RHS) || eq(RHS); }
1076
1077   /// \brief Unsigned less or equal comparison
1078   ///
1079   /// Regards both *this as an unsigned quantity and compares it with RHS for
1080   /// the validity of the less-or-equal relationship.
1081   ///
1082   /// \returns true if *this <= RHS when considered unsigned.
1083   bool ule(uint64_t RHS) const { return !ugt(RHS); }
1084
1085   /// \brief Signed less or equal comparison
1086   ///
1087   /// Regards both *this and RHS as signed quantities and compares them for
1088   /// validity of the less-or-equal relationship.
1089   ///
1090   /// \returns true if *this <= RHS when both are considered signed.
1091   bool sle(const APInt &RHS) const { return slt(RHS) || eq(RHS); }
1092
1093   /// \brief Signed less or equal comparison
1094   ///
1095   /// Regards both *this as a signed quantity and compares it with RHS for the
1096   /// validity of the less-or-equal relationship.
1097   ///
1098   /// \returns true if *this <= RHS when considered signed.
1099   bool sle(uint64_t RHS) const { return !sgt(RHS); }
1100
1101   /// \brief Unsigned greather than comparison
1102   ///
1103   /// Regards both *this and RHS as unsigned quantities and compares them for
1104   /// the validity of the greater-than relationship.
1105   ///
1106   /// \returns true if *this > RHS when both are considered unsigned.
1107   bool ugt(const APInt &RHS) const { return !ult(RHS) && !eq(RHS); }
1108
1109   /// \brief Unsigned greater than comparison
1110   ///
1111   /// Regards both *this as an unsigned quantity and compares it with RHS for
1112   /// the validity of the greater-than relationship.
1113   ///
1114   /// \returns true if *this > RHS when considered unsigned.
1115   bool ugt(uint64_t RHS) const {
1116     return getActiveBits() > 64 ? true : getZExtValue() > RHS;
1117   }
1118
1119   /// \brief Signed greather than comparison
1120   ///
1121   /// Regards both *this and RHS as signed quantities and compares them for the
1122   /// validity of the greater-than relationship.
1123   ///
1124   /// \returns true if *this > RHS when both are considered signed.
1125   bool sgt(const APInt &RHS) const { return !slt(RHS) && !eq(RHS); }
1126
1127   /// \brief Signed greater than comparison
1128   ///
1129   /// Regards both *this as a signed quantity and compares it with RHS for
1130   /// the validity of the greater-than relationship.
1131   ///
1132   /// \returns true if *this > RHS when considered signed.
1133   bool sgt(int64_t RHS) const {
1134     return getMinSignedBits() > 64 ? !isNegative() : getSExtValue() > RHS;
1135   }
1136
1137   /// \brief Unsigned greater or equal comparison
1138   ///
1139   /// Regards both *this and RHS as unsigned quantities and compares them for
1140   /// validity of the greater-or-equal relationship.
1141   ///
1142   /// \returns true if *this >= RHS when both are considered unsigned.
1143   bool uge(const APInt &RHS) const { return !ult(RHS); }
1144
1145   /// \brief Unsigned greater or equal comparison
1146   ///
1147   /// Regards both *this as an unsigned quantity and compares it with RHS for
1148   /// the validity of the greater-or-equal relationship.
1149   ///
1150   /// \returns true if *this >= RHS when considered unsigned.
1151   bool uge(uint64_t RHS) const { return !ult(RHS); }
1152
1153   /// \brief Signed greather or equal comparison
1154   ///
1155   /// Regards both *this and RHS as signed quantities and compares them for
1156   /// validity of the greater-or-equal relationship.
1157   ///
1158   /// \returns true if *this >= RHS when both are considered signed.
1159   bool sge(const APInt &RHS) const { return !slt(RHS); }
1160
1161   /// \brief Signed greater or equal comparison
1162   ///
1163   /// Regards both *this as a signed quantity and compares it with RHS for
1164   /// the validity of the greater-or-equal relationship.
1165   ///
1166   /// \returns true if *this >= RHS when considered signed.
1167   bool sge(int64_t RHS) const { return !slt(RHS); }
1168
1169   /// This operation tests if there are any pairs of corresponding bits
1170   /// between this APInt and RHS that are both set.
1171   bool intersects(const APInt &RHS) const { return (*this & RHS) != 0; }
1172
1173   /// @}
1174   /// \name Resizing Operators
1175   /// @{
1176
1177   /// \brief Truncate to new width.
1178   ///
1179   /// Truncate the APInt to a specified width. It is an error to specify a width
1180   /// that is greater than or equal to the current width.
1181   APInt LLVM_ATTRIBUTE_UNUSED_RESULT trunc(unsigned width) const;
1182
1183   /// \brief Sign extend to a new width.
1184   ///
1185   /// This operation sign extends the APInt to a new width. If the high order
1186   /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
1187   /// It is an error to specify a width that is less than or equal to the
1188   /// current width.
1189   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sext(unsigned width) const;
1190
1191   /// \brief Zero extend to a new width.
1192   ///
1193   /// This operation zero extends the APInt to a new width. The high order bits
1194   /// are filled with 0 bits.  It is an error to specify a width that is less
1195   /// than or equal to the current width.
1196   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zext(unsigned width) const;
1197
1198   /// \brief Sign extend or truncate to width
1199   ///
1200   /// Make this APInt have the bit width given by \p width. The value is sign
1201   /// extended, truncated, or left alone to make it that width.
1202   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrTrunc(unsigned width) const;
1203
1204   /// \brief Zero extend or truncate to width
1205   ///
1206   /// Make this APInt have the bit width given by \p width. The value is zero
1207   /// extended, truncated, or left alone to make it that width.
1208   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrTrunc(unsigned width) const;
1209
1210   /// \brief Sign extend or truncate to width
1211   ///
1212   /// Make this APInt have the bit width given by \p width. The value is sign
1213   /// extended, or left alone to make it that width.
1214   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sextOrSelf(unsigned width) const;
1215
1216   /// \brief Zero extend or truncate to width
1217   ///
1218   /// Make this APInt have the bit width given by \p width. The value is zero
1219   /// extended, or left alone to make it that width.
1220   APInt LLVM_ATTRIBUTE_UNUSED_RESULT zextOrSelf(unsigned width) const;
1221
1222   /// @}
1223   /// \name Bit Manipulation Operators
1224   /// @{
1225
1226   /// \brief Set every bit to 1.
1227   void setAllBits() {
1228     if (isSingleWord())
1229       VAL = UINT64_MAX;
1230     else {
1231       // Set all the bits in all the words.
1232       for (unsigned i = 0; i < getNumWords(); ++i)
1233         pVal[i] = UINT64_MAX;
1234     }
1235     // Clear the unused ones
1236     clearUnusedBits();
1237   }
1238
1239   /// \brief Set a given bit to 1.
1240   ///
1241   /// Set the given bit to 1 whose position is given as "bitPosition".
1242   void setBit(unsigned bitPosition);
1243
1244   /// \brief Set every bit to 0.
1245   void clearAllBits() {
1246     if (isSingleWord())
1247       VAL = 0;
1248     else
1249       memset(pVal, 0, getNumWords() * APINT_WORD_SIZE);
1250   }
1251
1252   /// \brief Set a given bit to 0.
1253   ///
1254   /// Set the given bit to 0 whose position is given as "bitPosition".
1255   void clearBit(unsigned bitPosition);
1256
1257   /// \brief Toggle every bit to its opposite value.
1258   void flipAllBits() {
1259     if (isSingleWord())
1260       VAL ^= UINT64_MAX;
1261     else {
1262       for (unsigned i = 0; i < getNumWords(); ++i)
1263         pVal[i] ^= UINT64_MAX;
1264     }
1265     clearUnusedBits();
1266   }
1267
1268   /// \brief Toggles a given bit to its opposite value.
1269   ///
1270   /// Toggle a given bit to its opposite value whose position is given
1271   /// as "bitPosition".
1272   void flipBit(unsigned bitPosition);
1273
1274   /// @}
1275   /// \name Value Characterization Functions
1276   /// @{
1277
1278   /// \brief Return the number of bits in the APInt.
1279   unsigned getBitWidth() const { return BitWidth; }
1280
1281   /// \brief Get the number of words.
1282   ///
1283   /// Here one word's bitwidth equals to that of uint64_t.
1284   ///
1285   /// \returns the number of words to hold the integer value of this APInt.
1286   unsigned getNumWords() const { return getNumWords(BitWidth); }
1287
1288   /// \brief Get the number of words.
1289   ///
1290   /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
1291   ///
1292   /// \returns the number of words to hold the integer value with a given bit
1293   /// width.
1294   static unsigned getNumWords(unsigned BitWidth) {
1295     return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
1296   }
1297
1298   /// \brief Compute the number of active bits in the value
1299   ///
1300   /// This function returns the number of active bits which is defined as the
1301   /// bit width minus the number of leading zeros. This is used in several
1302   /// computations to see how "wide" the value is.
1303   unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
1304
1305   /// \brief Compute the number of active words in the value of this APInt.
1306   ///
1307   /// This is used in conjunction with getActiveData to extract the raw value of
1308   /// the APInt.
1309   unsigned getActiveWords() const {
1310     unsigned numActiveBits = getActiveBits();
1311     return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
1312   }
1313
1314   /// \brief Get the minimum bit size for this signed APInt
1315   ///
1316   /// Computes the minimum bit width for this APInt while considering it to be a
1317   /// signed (and probably negative) value. If the value is not negative, this
1318   /// function returns the same value as getActiveBits()+1. Otherwise, it
1319   /// returns the smallest bit width that will retain the negative value. For
1320   /// example, -1 can be written as 0b1 or 0xFFFFFFFFFF. 0b1 is shorter and so
1321   /// for -1, this function will always return 1.
1322   unsigned getMinSignedBits() const {
1323     if (isNegative())
1324       return BitWidth - countLeadingOnes() + 1;
1325     return getActiveBits() + 1;
1326   }
1327
1328   /// \brief Get zero extended value
1329   ///
1330   /// This method attempts to return the value of this APInt as a zero extended
1331   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
1332   /// uint64_t. Otherwise an assertion will result.
1333   uint64_t getZExtValue() const {
1334     if (isSingleWord())
1335       return VAL;
1336     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
1337     return pVal[0];
1338   }
1339
1340   /// \brief Get sign extended value
1341   ///
1342   /// This method attempts to return the value of this APInt as a sign extended
1343   /// int64_t. The bit width must be <= 64 or the value must fit within an
1344   /// int64_t. Otherwise an assertion will result.
1345   int64_t getSExtValue() const {
1346     if (isSingleWord())
1347       return int64_t(VAL << (APINT_BITS_PER_WORD - BitWidth)) >>
1348              (APINT_BITS_PER_WORD - BitWidth);
1349     assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
1350     return int64_t(pVal[0]);
1351   }
1352
1353   /// \brief Get bits required for string value.
1354   ///
1355   /// This method determines how many bits are required to hold the APInt
1356   /// equivalent of the string given by \p str.
1357   static unsigned getBitsNeeded(StringRef str, uint8_t radix);
1358
1359   /// \brief The APInt version of the countLeadingZeros functions in
1360   ///   MathExtras.h.
1361   ///
1362   /// It counts the number of zeros from the most significant bit to the first
1363   /// one bit.
1364   ///
1365   /// \returns BitWidth if the value is zero, otherwise returns the number of
1366   ///   zeros from the most significant bit to the first one bits.
1367   unsigned countLeadingZeros() const {
1368     if (isSingleWord()) {
1369       unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
1370       return llvm::countLeadingZeros(VAL) - unusedBits;
1371     }
1372     return countLeadingZerosSlowCase();
1373   }
1374
1375   /// \brief Count the number of leading one bits.
1376   ///
1377   /// This function is an APInt version of the countLeadingOnes
1378   /// functions in MathExtras.h. It counts the number of ones from the most
1379   /// significant bit to the first zero bit.
1380   ///
1381   /// \returns 0 if the high order bit is not set, otherwise returns the number
1382   /// of 1 bits from the most significant to the least
1383   unsigned countLeadingOnes() const;
1384
1385   /// Computes the number of leading bits of this APInt that are equal to its
1386   /// sign bit.
1387   unsigned getNumSignBits() const {
1388     return isNegative() ? countLeadingOnes() : countLeadingZeros();
1389   }
1390
1391   /// \brief Count the number of trailing zero bits.
1392   ///
1393   /// This function is an APInt version of the countTrailingZeros
1394   /// functions in MathExtras.h. It counts the number of zeros from the least
1395   /// significant bit to the first set bit.
1396   ///
1397   /// \returns BitWidth if the value is zero, otherwise returns the number of
1398   /// zeros from the least significant bit to the first one bit.
1399   unsigned countTrailingZeros() const;
1400
1401   /// \brief Count the number of trailing one bits.
1402   ///
1403   /// This function is an APInt version of the countTrailingOnes
1404   /// functions in MathExtras.h. It counts the number of ones from the least
1405   /// significant bit to the first zero bit.
1406   ///
1407   /// \returns BitWidth if the value is all ones, otherwise returns the number
1408   /// of ones from the least significant bit to the first zero bit.
1409   unsigned countTrailingOnes() const {
1410     if (isSingleWord())
1411       return llvm::countTrailingOnes(VAL);
1412     return countTrailingOnesSlowCase();
1413   }
1414
1415   /// \brief Count the number of bits set.
1416   ///
1417   /// This function is an APInt version of the countPopulation functions
1418   /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
1419   ///
1420   /// \returns 0 if the value is zero, otherwise returns the number of set bits.
1421   unsigned countPopulation() const {
1422     if (isSingleWord())
1423       return llvm::countPopulation(VAL);
1424     return countPopulationSlowCase();
1425   }
1426
1427   /// @}
1428   /// \name Conversion Functions
1429   /// @{
1430   void print(raw_ostream &OS, bool isSigned) const;
1431
1432   /// Converts an APInt to a string and append it to Str.  Str is commonly a
1433   /// SmallString.
1434   void toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
1435                 bool formatAsCLiteral = false) const;
1436
1437   /// Considers the APInt to be unsigned and converts it into a string in the
1438   /// radix given. The radix can be 2, 8, 10 16, or 36.
1439   void toStringUnsigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1440     toString(Str, Radix, false, false);
1441   }
1442
1443   /// Considers the APInt to be signed and converts it into a string in the
1444   /// radix given. The radix can be 2, 8, 10, 16, or 36.
1445   void toStringSigned(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
1446     toString(Str, Radix, true, false);
1447   }
1448
1449   /// \brief Return the APInt as a std::string.
1450   ///
1451   /// Note that this is an inefficient method.  It is better to pass in a
1452   /// SmallVector/SmallString to the methods above to avoid thrashing the heap
1453   /// for the string.
1454   std::string toString(unsigned Radix, bool Signed) const;
1455
1456   /// \returns a byte-swapped representation of this APInt Value.
1457   APInt LLVM_ATTRIBUTE_UNUSED_RESULT byteSwap() const;
1458
1459   /// \returns the value with the bit representation reversed of this APInt
1460   /// Value.
1461   APInt LLVM_ATTRIBUTE_UNUSED_RESULT reverseBits() const;
1462
1463   /// \brief Converts this APInt to a double value.
1464   double roundToDouble(bool isSigned) const;
1465
1466   /// \brief Converts this unsigned APInt to a double value.
1467   double roundToDouble() const { return roundToDouble(false); }
1468
1469   /// \brief Converts this signed APInt to a double value.
1470   double signedRoundToDouble() const { return roundToDouble(true); }
1471
1472   /// \brief Converts APInt bits to a double
1473   ///
1474   /// The conversion does not do a translation from integer to double, it just
1475   /// re-interprets the bits as a double. Note that it is valid to do this on
1476   /// any bit width. Exactly 64 bits will be translated.
1477   double bitsToDouble() const {
1478     union {
1479       uint64_t I;
1480       double D;
1481     } T;
1482     T.I = (isSingleWord() ? VAL : pVal[0]);
1483     return T.D;
1484   }
1485
1486   /// \brief Converts APInt bits to a double
1487   ///
1488   /// The conversion does not do a translation from integer to float, it just
1489   /// re-interprets the bits as a float. Note that it is valid to do this on
1490   /// any bit width. Exactly 32 bits will be translated.
1491   float bitsToFloat() const {
1492     union {
1493       unsigned I;
1494       float F;
1495     } T;
1496     T.I = unsigned((isSingleWord() ? VAL : pVal[0]));
1497     return T.F;
1498   }
1499
1500   /// \brief Converts a double to APInt bits.
1501   ///
1502   /// The conversion does not do a translation from double to integer, it just
1503   /// re-interprets the bits of the double.
1504   static APInt LLVM_ATTRIBUTE_UNUSED_RESULT doubleToBits(double V) {
1505     union {
1506       uint64_t I;
1507       double D;
1508     } T;
1509     T.D = V;
1510     return APInt(sizeof T * CHAR_BIT, T.I);
1511   }
1512
1513   /// \brief Converts a float to APInt bits.
1514   ///
1515   /// The conversion does not do a translation from float to integer, it just
1516   /// re-interprets the bits of the float.
1517   static APInt LLVM_ATTRIBUTE_UNUSED_RESULT floatToBits(float V) {
1518     union {
1519       unsigned I;
1520       float F;
1521     } T;
1522     T.F = V;
1523     return APInt(sizeof T * CHAR_BIT, T.I);
1524   }
1525
1526   /// @}
1527   /// \name Mathematics Operations
1528   /// @{
1529
1530   /// \returns the floor log base 2 of this APInt.
1531   unsigned logBase2() const { return BitWidth - 1 - countLeadingZeros(); }
1532
1533   /// \returns the ceil log base 2 of this APInt.
1534   unsigned ceilLogBase2() const {
1535     return BitWidth - (*this - 1).countLeadingZeros();
1536   }
1537
1538   /// \returns the nearest log base 2 of this APInt. Ties round up.
1539   ///
1540   /// NOTE: When we have a BitWidth of 1, we define:
1541   ///
1542   ///   log2(0) = UINT32_MAX
1543   ///   log2(1) = 0
1544   ///
1545   /// to get around any mathematical concerns resulting from
1546   /// referencing 2 in a space where 2 does no exist.
1547   unsigned nearestLogBase2() const {
1548     // Special case when we have a bitwidth of 1. If VAL is 1, then we
1549     // get 0. If VAL is 0, we get UINT64_MAX which gets truncated to
1550     // UINT32_MAX.
1551     if (BitWidth == 1)
1552       return VAL - 1;
1553
1554     // Handle the zero case.
1555     if (!getBoolValue())
1556       return UINT32_MAX;
1557
1558     // The non-zero case is handled by computing:
1559     //
1560     //   nearestLogBase2(x) = logBase2(x) + x[logBase2(x)-1].
1561     //
1562     // where x[i] is referring to the value of the ith bit of x.
1563     unsigned lg = logBase2();
1564     return lg + unsigned((*this)[lg - 1]);
1565   }
1566
1567   /// \returns the log base 2 of this APInt if its an exact power of two, -1
1568   /// otherwise
1569   int32_t exactLogBase2() const {
1570     if (!isPowerOf2())
1571       return -1;
1572     return logBase2();
1573   }
1574
1575   /// \brief Compute the square root
1576   APInt LLVM_ATTRIBUTE_UNUSED_RESULT sqrt() const;
1577
1578   /// \brief Get the absolute value;
1579   ///
1580   /// If *this is < 0 then return -(*this), otherwise *this;
1581   APInt LLVM_ATTRIBUTE_UNUSED_RESULT abs() const {
1582     if (isNegative())
1583       return -(*this);
1584     return *this;
1585   }
1586
1587   /// \returns the multiplicative inverse for a given modulo.
1588   APInt multiplicativeInverse(const APInt &modulo) const;
1589
1590   /// @}
1591   /// \name Support for division by constant
1592   /// @{
1593
1594   /// Calculate the magic number for signed division by a constant.
1595   struct ms;
1596   ms magic() const;
1597
1598   /// Calculate the magic number for unsigned division by a constant.
1599   struct mu;
1600   mu magicu(unsigned LeadingZeros = 0) const;
1601
1602   /// @}
1603   /// \name Building-block Operations for APInt and APFloat
1604   /// @{
1605
1606   // These building block operations operate on a representation of arbitrary
1607   // precision, two's-complement, bignum integer values. They should be
1608   // sufficient to implement APInt and APFloat bignum requirements. Inputs are
1609   // generally a pointer to the base of an array of integer parts, representing
1610   // an unsigned bignum, and a count of how many parts there are.
1611
1612   /// Sets the least significant part of a bignum to the input value, and zeroes
1613   /// out higher parts.
1614   static void tcSet(integerPart *, integerPart, unsigned int);
1615
1616   /// Assign one bignum to another.
1617   static void tcAssign(integerPart *, const integerPart *, unsigned int);
1618
1619   /// Returns true if a bignum is zero, false otherwise.
1620   static bool tcIsZero(const integerPart *, unsigned int);
1621
1622   /// Extract the given bit of a bignum; returns 0 or 1.  Zero-based.
1623   static int tcExtractBit(const integerPart *, unsigned int bit);
1624
1625   /// Copy the bit vector of width srcBITS from SRC, starting at bit srcLSB, to
1626   /// DST, of dstCOUNT parts, such that the bit srcLSB becomes the least
1627   /// significant bit of DST.  All high bits above srcBITS in DST are
1628   /// zero-filled.
1629   static void tcExtract(integerPart *, unsigned int dstCount,
1630                         const integerPart *, unsigned int srcBits,
1631                         unsigned int srcLSB);
1632
1633   /// Set the given bit of a bignum.  Zero-based.
1634   static void tcSetBit(integerPart *, unsigned int bit);
1635
1636   /// Clear the given bit of a bignum.  Zero-based.
1637   static void tcClearBit(integerPart *, unsigned int bit);
1638
1639   /// Returns the bit number of the least or most significant set bit of a
1640   /// number.  If the input number has no bits set -1U is returned.
1641   static unsigned int tcLSB(const integerPart *, unsigned int);
1642   static unsigned int tcMSB(const integerPart *parts, unsigned int n);
1643
1644   /// Negate a bignum in-place.
1645   static void tcNegate(integerPart *, unsigned int);
1646
1647   /// DST += RHS + CARRY where CARRY is zero or one.  Returns the carry flag.
1648   static integerPart tcAdd(integerPart *, const integerPart *,
1649                            integerPart carry, unsigned);
1650
1651   /// DST -= RHS + CARRY where CARRY is zero or one. Returns the carry flag.
1652   static integerPart tcSubtract(integerPart *, const integerPart *,
1653                                 integerPart carry, unsigned);
1654
1655   /// DST += SRC * MULTIPLIER + PART   if add is true
1656   /// DST  = SRC * MULTIPLIER + PART   if add is false
1657   ///
1658   /// Requires 0 <= DSTPARTS <= SRCPARTS + 1.  If DST overlaps SRC they must
1659   /// start at the same point, i.e. DST == SRC.
1660   ///
1661   /// If DSTPARTS == SRC_PARTS + 1 no overflow occurs and zero is returned.
1662   /// Otherwise DST is filled with the least significant DSTPARTS parts of the
1663   /// result, and if all of the omitted higher parts were zero return zero,
1664   /// otherwise overflow occurred and return one.
1665   static int tcMultiplyPart(integerPart *dst, const integerPart *src,
1666                             integerPart multiplier, integerPart carry,
1667                             unsigned int srcParts, unsigned int dstParts,
1668                             bool add);
1669
1670   /// DST = LHS * RHS, where DST has the same width as the operands and is
1671   /// filled with the least significant parts of the result.  Returns one if
1672   /// overflow occurred, otherwise zero.  DST must be disjoint from both
1673   /// operands.
1674   static int tcMultiply(integerPart *, const integerPart *, const integerPart *,
1675                         unsigned);
1676
1677   /// DST = LHS * RHS, where DST has width the sum of the widths of the
1678   /// operands.  No overflow occurs.  DST must be disjoint from both
1679   /// operands. Returns the number of parts required to hold the result.
1680   static unsigned int tcFullMultiply(integerPart *, const integerPart *,
1681                                      const integerPart *, unsigned, unsigned);
1682
1683   /// If RHS is zero LHS and REMAINDER are left unchanged, return one.
1684   /// Otherwise set LHS to LHS / RHS with the fractional part discarded, set
1685   /// REMAINDER to the remainder, return zero.  i.e.
1686   ///
1687   ///  OLD_LHS = RHS * LHS + REMAINDER
1688   ///
1689   /// SCRATCH is a bignum of the same size as the operands and result for use by
1690   /// the routine; its contents need not be initialized and are destroyed.  LHS,
1691   /// REMAINDER and SCRATCH must be distinct.
1692   static int tcDivide(integerPart *lhs, const integerPart *rhs,
1693                       integerPart *remainder, integerPart *scratch,
1694                       unsigned int parts);
1695
1696   /// Shift a bignum left COUNT bits.  Shifted in bits are zero.  There are no
1697   /// restrictions on COUNT.
1698   static void tcShiftLeft(integerPart *, unsigned int parts,
1699                           unsigned int count);
1700
1701   /// Shift a bignum right COUNT bits.  Shifted in bits are zero.  There are no
1702   /// restrictions on COUNT.
1703   static void tcShiftRight(integerPart *, unsigned int parts,
1704                            unsigned int count);
1705
1706   /// The obvious AND, OR and XOR and complement operations.
1707   static void tcAnd(integerPart *, const integerPart *, unsigned int);
1708   static void tcOr(integerPart *, const integerPart *, unsigned int);
1709   static void tcXor(integerPart *, const integerPart *, unsigned int);
1710   static void tcComplement(integerPart *, unsigned int);
1711
1712   /// Comparison (unsigned) of two bignums.
1713   static int tcCompare(const integerPart *, const integerPart *, unsigned int);
1714
1715   /// Increment a bignum in-place.  Return the carry flag.
1716   static integerPart tcIncrement(integerPart *, unsigned int);
1717
1718   /// Decrement a bignum in-place.  Return the borrow flag.
1719   static integerPart tcDecrement(integerPart *, unsigned int);
1720
1721   /// Set the least significant BITS and clear the rest.
1722   static void tcSetLeastSignificantBits(integerPart *, unsigned int,
1723                                         unsigned int bits);
1724
1725   /// \brief debug method
1726   void dump() const;
1727
1728   /// @}
1729 };
1730
1731 /// Magic data for optimising signed division by a constant.
1732 struct APInt::ms {
1733   APInt m;    ///< magic number
1734   unsigned s; ///< shift amount
1735 };
1736
1737 /// Magic data for optimising unsigned division by a constant.
1738 struct APInt::mu {
1739   APInt m;    ///< magic number
1740   bool a;     ///< add indicator
1741   unsigned s; ///< shift amount
1742 };
1743
1744 inline bool operator==(uint64_t V1, const APInt &V2) { return V2 == V1; }
1745
1746 inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
1747
1748 inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) {
1749   I.print(OS, true);
1750   return OS;
1751 }
1752
1753 namespace APIntOps {
1754
1755 /// \brief Determine the smaller of two APInts considered to be signed.
1756 inline const APInt &smin(const APInt &A, const APInt &B) {
1757   return A.slt(B) ? A : B;
1758 }
1759
1760 /// \brief Determine the larger of two APInts considered to be signed.
1761 inline const APInt &smax(const APInt &A, const APInt &B) {
1762   return A.sgt(B) ? A : B;
1763 }
1764
1765 /// \brief Determine the smaller of two APInts considered to be signed.
1766 inline const APInt &umin(const APInt &A, const APInt &B) {
1767   return A.ult(B) ? A : B;
1768 }
1769
1770 /// \brief Determine the larger of two APInts considered to be unsigned.
1771 inline const APInt &umax(const APInt &A, const APInt &B) {
1772   return A.ugt(B) ? A : B;
1773 }
1774
1775 /// \brief Check if the specified APInt has a N-bits unsigned integer value.
1776 inline bool isIntN(unsigned N, const APInt &APIVal) { return APIVal.isIntN(N); }
1777
1778 /// \brief Check if the specified APInt has a N-bits signed integer value.
1779 inline bool isSignedIntN(unsigned N, const APInt &APIVal) {
1780   return APIVal.isSignedIntN(N);
1781 }
1782
1783 /// \returns true if the argument APInt value is a sequence of ones starting at
1784 /// the least significant bit with the remainder zero.
1785 inline bool isMask(unsigned numBits, const APInt &APIVal) {
1786   return numBits <= APIVal.getBitWidth() &&
1787          APIVal == APInt::getLowBitsSet(APIVal.getBitWidth(), numBits);
1788 }
1789
1790 /// \returns true if the argument is a non-empty sequence of ones starting at
1791 /// the least significant bit with the remainder zero (32 bit version).
1792 /// Ex. isMask(0x0000FFFFU) == true.
1793 inline bool isMask(const APInt &Value) {
1794   return (Value != 0) && ((Value + 1) & Value) == 0;
1795 }
1796
1797 /// \brief Return true if the argument APInt value contains a sequence of ones
1798 /// with the remainder zero.
1799 inline bool isShiftedMask(unsigned numBits, const APInt &APIVal) {
1800   return isMask(numBits, (APIVal - APInt(numBits, 1)) | APIVal);
1801 }
1802
1803 /// \brief Returns a byte-swapped representation of the specified APInt Value.
1804 inline APInt byteSwap(const APInt &APIVal) { return APIVal.byteSwap(); }
1805
1806 /// \brief Returns the floor log base 2 of the specified APInt value.
1807 inline unsigned logBase2(const APInt &APIVal) { return APIVal.logBase2(); }
1808
1809 /// \brief Compute GCD of two APInt values.
1810 ///
1811 /// This function returns the greatest common divisor of the two APInt values
1812 /// using Euclid's algorithm.
1813 ///
1814 /// \returns the greatest common divisor of Val1 and Val2
1815 APInt GreatestCommonDivisor(const APInt &Val1, const APInt &Val2);
1816
1817 /// \brief Converts the given APInt to a double value.
1818 ///
1819 /// Treats the APInt as an unsigned value for conversion purposes.
1820 inline double RoundAPIntToDouble(const APInt &APIVal) {
1821   return APIVal.roundToDouble();
1822 }
1823
1824 /// \brief Converts the given APInt to a double value.
1825 ///
1826 /// Treats the APInt as a signed value for conversion purposes.
1827 inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
1828   return APIVal.signedRoundToDouble();
1829 }
1830
1831 /// \brief Converts the given APInt to a float vlalue.
1832 inline float RoundAPIntToFloat(const APInt &APIVal) {
1833   return float(RoundAPIntToDouble(APIVal));
1834 }
1835
1836 /// \brief Converts the given APInt to a float value.
1837 ///
1838 /// Treast the APInt as a signed value for conversion purposes.
1839 inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
1840   return float(APIVal.signedRoundToDouble());
1841 }
1842
1843 /// \brief Converts the given double value into a APInt.
1844 ///
1845 /// This function convert a double value to an APInt value.
1846 APInt RoundDoubleToAPInt(double Double, unsigned width);
1847
1848 /// \brief Converts a float value into a APInt.
1849 ///
1850 /// Converts a float value into an APInt value.
1851 inline APInt RoundFloatToAPInt(float Float, unsigned width) {
1852   return RoundDoubleToAPInt(double(Float), width);
1853 }
1854
1855 /// \brief Arithmetic right-shift function.
1856 ///
1857 /// Arithmetic right-shift the APInt by shiftAmt.
1858 inline APInt ashr(const APInt &LHS, unsigned shiftAmt) {
1859   return LHS.ashr(shiftAmt);
1860 }
1861
1862 /// \brief Logical right-shift function.
1863 ///
1864 /// Logical right-shift the APInt by shiftAmt.
1865 inline APInt lshr(const APInt &LHS, unsigned shiftAmt) {
1866   return LHS.lshr(shiftAmt);
1867 }
1868
1869 /// \brief Left-shift function.
1870 ///
1871 /// Left-shift the APInt by shiftAmt.
1872 inline APInt shl(const APInt &LHS, unsigned shiftAmt) {
1873   return LHS.shl(shiftAmt);
1874 }
1875
1876 /// \brief Signed division function for APInt.
1877 ///
1878 /// Signed divide APInt LHS by APInt RHS.
1879 inline APInt sdiv(const APInt &LHS, const APInt &RHS) { return LHS.sdiv(RHS); }
1880
1881 /// \brief Unsigned division function for APInt.
1882 ///
1883 /// Unsigned divide APInt LHS by APInt RHS.
1884 inline APInt udiv(const APInt &LHS, const APInt &RHS) { return LHS.udiv(RHS); }
1885
1886 /// \brief Function for signed remainder operation.
1887 ///
1888 /// Signed remainder operation on APInt.
1889 inline APInt srem(const APInt &LHS, const APInt &RHS) { return LHS.srem(RHS); }
1890
1891 /// \brief Function for unsigned remainder operation.
1892 ///
1893 /// Unsigned remainder operation on APInt.
1894 inline APInt urem(const APInt &LHS, const APInt &RHS) { return LHS.urem(RHS); }
1895
1896 /// \brief Function for multiplication operation.
1897 ///
1898 /// Performs multiplication on APInt values.
1899 inline APInt mul(const APInt &LHS, const APInt &RHS) { return LHS * RHS; }
1900
1901 /// \brief Function for addition operation.
1902 ///
1903 /// Performs addition on APInt values.
1904 inline APInt add(const APInt &LHS, const APInt &RHS) { return LHS + RHS; }
1905
1906 /// \brief Function for subtraction operation.
1907 ///
1908 /// Performs subtraction on APInt values.
1909 inline APInt sub(const APInt &LHS, const APInt &RHS) { return LHS - RHS; }
1910
1911 /// \brief Bitwise AND function for APInt.
1912 ///
1913 /// Performs bitwise AND operation on APInt LHS and
1914 /// APInt RHS.
1915 inline APInt And(const APInt &LHS, const APInt &RHS) { return LHS & RHS; }
1916
1917 /// \brief Bitwise OR function for APInt.
1918 ///
1919 /// Performs bitwise OR operation on APInt LHS and APInt RHS.
1920 inline APInt Or(const APInt &LHS, const APInt &RHS) { return LHS | RHS; }
1921
1922 /// \brief Bitwise XOR function for APInt.
1923 ///
1924 /// Performs bitwise XOR operation on APInt.
1925 inline APInt Xor(const APInt &LHS, const APInt &RHS) { return LHS ^ RHS; }
1926
1927 /// \brief Bitwise complement function.
1928 ///
1929 /// Performs a bitwise complement operation on APInt.
1930 inline APInt Not(const APInt &APIVal) { return ~APIVal; }
1931
1932 } // End of APIntOps namespace
1933
1934 // See friend declaration above. This additional declaration is required in
1935 // order to compile LLVM with IBM xlC compiler.
1936 hash_code hash_value(const APInt &Arg);
1937 } // End of llvm namespace
1938
1939 #endif