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