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