llvm.org GIT mirror llvm / 26b584c
Remove \brief commands from doxygen comments. We've been running doxygen with the autobrief option for a couple of years now. This makes the \brief markers into our comments redundant. Since they are a visual distraction and we don't want to encourage more \brief markers in new code either, this patch removes them all. Patch produced by for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done Differential Revision: https://reviews.llvm.org/D46290 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@331272 91177308-0d34-0410-b5e6-96231b3b80d8 Adrian Prantl 1 year, 3 months ago
781 changed file(s) with 5014 addition(s) and 5014 deletion(s). Raw diff Collapse all Expand all
12141214 return X;
12151215 }
12161216
1217 /// \brief Returns the negated value of the argument.
1217 /// Returns the negated value of the argument.
12181218 inline APFloat neg(APFloat X) {
12191219 X.changeSign();
12201220 return X;
77 //===----------------------------------------------------------------------===//
88 ///
99 /// \file
10 /// \brief This file implements a class to represent arbitrary precision
10 /// This file implements a class to represent arbitrary precision
1111 /// integral constant values and operations on them.
1212 ///
1313 //===----------------------------------------------------------------------===//
3939 // APInt Class
4040 //===----------------------------------------------------------------------===//
4141
42 /// \brief Class for arbitrary precision integers.
42 /// Class for arbitrary precision integers.
4343 ///
4444 /// APInt is a functional replacement for common case unsigned integer type like
4545 /// "unsigned", "unsigned long" or "uint64_t", but also allows non-byte-width
9393
9494 friend class APSInt;
9595
96 /// \brief Fast internal constructor
96 /// Fast internal constructor
9797 ///
9898 /// This constructor is used only internally for speed of construction of
9999 /// temporaries. It is unsafe for general use so it is not public.
101101 U.pVal = val;
102102 }
103103
104 /// \brief Determine if this APInt just has one word to store value.
104 /// Determine if this APInt just has one word to store value.
105105 ///
106106 /// \returns true if the number of bits <= 64, false otherwise.
107107 bool isSingleWord() const { return BitWidth <= APINT_BITS_PER_WORD; }
108108
109 /// \brief Determine which word a bit is in.
109 /// Determine which word a bit is in.
110110 ///
111111 /// \returns the word position for the specified bit position.
112112 static unsigned whichWord(unsigned bitPosition) {
113113 return bitPosition / APINT_BITS_PER_WORD;
114114 }
115115
116 /// \brief Determine which bit in a word a bit is in.
116 /// Determine which bit in a word a bit is in.
117117 ///
118118 /// \returns the bit position in a word for the specified bit position
119119 /// in the APInt.
121121 return bitPosition % APINT_BITS_PER_WORD;
122122 }
123123
124 /// \brief Get a single bit mask.
124 /// Get a single bit mask.
125125 ///
126126 /// \returns a uint64_t with only bit at "whichBit(bitPosition)" set
127127 /// This method generates and returns a uint64_t (word) mask for a single
131131 return 1ULL << whichBit(bitPosition);
132132 }
133133
134 /// \brief Clear unused high order bits
134 /// Clear unused high order bits
135135 ///
136136 /// This method is used internally to clear the top "N" bits in the high order
137137 /// word that are not used by the APInt. This is needed after the most
150150 return *this;
151151 }
152152
153 /// \brief Get the word corresponding to a bit position
153 /// Get the word corresponding to a bit position
154154 /// \returns the corresponding word for the specified bit position.
155155 uint64_t getWord(unsigned bitPosition) const {
156156 return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
161161 /// value of any bits upon return. Caller should populate the bits after.
162162 void reallocate(unsigned NewBitWidth);
163163
164 /// \brief Convert a char array into an APInt
164 /// Convert a char array into an APInt
165165 ///
166166 /// \param radix 2, 8, 10, 16, or 36
167167 /// Converts a string into a number. The string must be non-empty
175175 /// result to hold the input.
176176 void fromString(unsigned numBits, StringRef str, uint8_t radix);
177177
178 /// \brief An internal division function for dividing APInts.
178 /// An internal division function for dividing APInts.
179179 ///
180180 /// This is used by the toString method to divide by the radix. It simply
181181 /// provides a more convenient form of divide for internal use since KnuthDiv
257257 /// \name Constructors
258258 /// @{
259259
260 /// \brief Create a new APInt of numBits width, initialized as val.
260 /// Create a new APInt of numBits width, initialized as val.
261261 ///
262262 /// If isSigned is true then val is treated as if it were a signed value
263263 /// (i.e. as an int64_t) and the appropriate sign extension to the bit width
278278 }
279279 }
280280
281 /// \brief Construct an APInt of numBits width, initialized as bigVal[].
281 /// Construct an APInt of numBits width, initialized as bigVal[].
282282 ///
283283 /// Note that bigVal.size() can be smaller or larger than the corresponding
284284 /// bit width but any extraneous bits will be dropped.
296296 /// constructor.
297297 APInt(unsigned numBits, unsigned numWords, const uint64_t bigVal[]);
298298
299 /// \brief Construct an APInt from a string representation.
299 /// Construct an APInt from a string representation.
300300 ///
301301 /// This constructor interprets the string \p str in the given radix. The
302302 /// interpretation stops when the first character that is not suitable for the
318318 initSlowCase(that);
319319 }
320320
321 /// \brief Move Constructor.
321 /// Move Constructor.
322322 APInt(APInt &&that) : BitWidth(that.BitWidth) {
323323 memcpy(&U, &that.U, sizeof(U));
324324 that.BitWidth = 0;
325325 }
326326
327 /// \brief Destructor.
327 /// Destructor.
328328 ~APInt() {
329329 if (needsCleanup())
330330 delete[] U.pVal;
331331 }
332332
333 /// \brief Default constructor that creates an uninteresting APInt
333 /// Default constructor that creates an uninteresting APInt
334334 /// representing a 1-bit zero value.
335335 ///
336336 /// This is useful for object deserialization (pair this with the static
337337 /// method Read).
338338 explicit APInt() : BitWidth(1) { U.VAL = 0; }
339339
340 /// \brief Returns whether this instance allocated memory.
340 /// Returns whether this instance allocated memory.
341341 bool needsCleanup() const { return !isSingleWord(); }
342342
343343 /// Used to insert APInt objects, or objects that contain APInt objects, into
348348 /// \name Value Tests
349349 /// @{
350350
351 /// \brief Determine sign of this APInt.
351 /// Determine sign of this APInt.
352352 ///
353353 /// This tests the high bit of this APInt to determine if it is set.
354354 ///
355355 /// \returns true if this APInt is negative, false otherwise
356356 bool isNegative() const { return (*this)[BitWidth - 1]; }
357357
358 /// \brief Determine if this APInt Value is non-negative (>= 0)
358 /// Determine if this APInt Value is non-negative (>= 0)
359359 ///
360360 /// This tests the high bit of the APInt to determine if it is unset.
361361 bool isNonNegative() const { return !isNegative(); }
362362
363 /// \brief Determine if sign bit of this APInt is set.
363 /// Determine if sign bit of this APInt is set.
364364 ///
365365 /// This tests the high bit of this APInt to determine if it is set.
366366 ///
367367 /// \returns true if this APInt has its sign bit set, false otherwise.
368368 bool isSignBitSet() const { return (*this)[BitWidth-1]; }
369369
370 /// \brief Determine if sign bit of this APInt is clear.
370 /// Determine if sign bit of this APInt is clear.
371371 ///
372372 /// This tests the high bit of this APInt to determine if it is clear.
373373 ///
374374 /// \returns true if this APInt has its sign bit clear, false otherwise.
375375 bool isSignBitClear() const { return !isSignBitSet(); }
376376
377 /// \brief Determine if this APInt Value is positive.
377 /// Determine if this APInt Value is positive.
378378 ///
379379 /// This tests if the value of this APInt is positive (> 0). Note
380380 /// that 0 is not a positive value.
382382 /// \returns true if this APInt is positive.
383383 bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); }
384384
385 /// \brief Determine if all bits are set
385 /// Determine if all bits are set
386386 ///
387387 /// This checks to see if the value has all bits of the APInt are set or not.
388388 bool isAllOnesValue() const {
391391 return countTrailingOnesSlowCase() == BitWidth;
392392 }
393393
394 /// \brief Determine if all bits are clear
394 /// Determine if all bits are clear
395395 ///
396396 /// This checks to see if the value has all bits of the APInt are clear or
397397 /// not.
398398 bool isNullValue() const { return !*this; }
399399
400 /// \brief Determine if this is a value of 1.
400 /// Determine if this is a value of 1.
401401 ///
402402 /// This checks to see if the value of this APInt is one.
403403 bool isOneValue() const {
406406 return countLeadingZerosSlowCase() == BitWidth - 1;
407407 }
408408
409 /// \brief Determine if this is the largest unsigned value.
409 /// Determine if this is the largest unsigned value.
410410 ///
411411 /// This checks to see if the value of this APInt is the maximum unsigned
412412 /// value for the APInt's bit width.
413413 bool isMaxValue() const { return isAllOnesValue(); }
414414
415 /// \brief Determine if this is the largest signed value.
415 /// Determine if this is the largest signed value.
416416 ///
417417 /// This checks to see if the value of this APInt is the maximum signed
418418 /// value for the APInt's bit width.
422422 return !isNegative() && countTrailingOnesSlowCase() == BitWidth - 1;
423423 }
424424
425 /// \brief Determine if this is the smallest unsigned value.
425 /// Determine if this is the smallest unsigned value.
426426 ///
427427 /// This checks to see if the value of this APInt is the minimum unsigned
428428 /// value for the APInt's bit width.
429429 bool isMinValue() const { return isNullValue(); }
430430
431 /// \brief Determine if this is the smallest signed value.
431 /// Determine if this is the smallest signed value.
432432 ///
433433 /// This checks to see if the value of this APInt is the minimum signed
434434 /// value for the APInt's bit width.
438438 return isNegative() && countTrailingZerosSlowCase() == BitWidth - 1;
439439 }
440440
441 /// \brief Check if this APInt has an N-bits unsigned integer value.
441 /// Check if this APInt has an N-bits unsigned integer value.
442442 bool isIntN(unsigned N) const {
443443 assert(N && "N == 0 ???");
444444 return getActiveBits() <= N;
445445 }
446446
447 /// \brief Check if this APInt has an N-bits signed integer value.
447 /// Check if this APInt has an N-bits signed integer value.
448448 bool isSignedIntN(unsigned N) const {
449449 assert(N && "N == 0 ???");
450450 return getMinSignedBits() <= N;
451451 }
452452
453 /// \brief Check if this APInt's value is a power of two greater than zero.
453 /// Check if this APInt's value is a power of two greater than zero.
454454 ///
455455 /// \returns true if the argument APInt value is a power of two > 0.
456456 bool isPowerOf2() const {
459459 return countPopulationSlowCase() == 1;
460460 }
461461
462 /// \brief Check if the APInt's value is returned by getSignMask.
462 /// Check if the APInt's value is returned by getSignMask.
463463 ///
464464 /// \returns true if this is the value returned by getSignMask.
465465 bool isSignMask() const { return isMinSignedValue(); }
466466
467 /// \brief Convert APInt to a boolean value.
467 /// Convert APInt to a boolean value.
468468 ///
469469 /// This converts the APInt to a boolean value as a test against zero.
470470 bool getBoolValue() const { return !!*this; }
475475 return ugt(Limit) ? Limit : getZExtValue();
476476 }
477477
478 /// \brief Check if the APInt consists of a repeated bit pattern.
478 /// Check if the APInt consists of a repeated bit pattern.
479479 ///
480480 /// e.g. 0x01010101 satisfies isSplat(8).
481481 /// \param SplatSizeInBits The size of the pattern in bits. Must divide bit
504504 return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth);
505505 }
506506
507 /// \brief Return true if this APInt value contains a sequence of ones with
507 /// Return true if this APInt value contains a sequence of ones with
508508 /// the remainder zero.
509509 bool isShiftedMask() const {
510510 if (isSingleWord())
518518 /// \name Value Generators
519519 /// @{
520520
521 /// \brief Gets maximum unsigned value of APInt for specific bit width.
521 /// Gets maximum unsigned value of APInt for specific bit width.
522522 static APInt getMaxValue(unsigned numBits) {
523523 return getAllOnesValue(numBits);
524524 }
525525
526 /// \brief Gets maximum signed value of APInt for a specific bit width.
526 /// Gets maximum signed value of APInt for a specific bit width.
527527 static APInt getSignedMaxValue(unsigned numBits) {
528528 APInt API = getAllOnesValue(numBits);
529529 API.clearBit(numBits - 1);
530530 return API;
531531 }
532532
533 /// \brief Gets minimum unsigned value of APInt for a specific bit width.
533 /// Gets minimum unsigned value of APInt for a specific bit width.
534534 static APInt getMinValue(unsigned numBits) { return APInt(numBits, 0); }
535535
536 /// \brief Gets minimum signed value of APInt for a specific bit width.
536 /// Gets minimum signed value of APInt for a specific bit width.
537537 static APInt getSignedMinValue(unsigned numBits) {
538538 APInt API(numBits, 0);
539539 API.setBit(numBits - 1);
540540 return API;
541541 }
542542
543 /// \brief Get the SignMask for a specific bit width.
543 /// Get the SignMask for a specific bit width.
544544 ///
545545 /// This is just a wrapper function of getSignedMinValue(), and it helps code
546546 /// readability when we want to get a SignMask.
548548 return getSignedMinValue(BitWidth);
549549 }
550550
551 /// \brief Get the all-ones value.
551 /// Get the all-ones value.
552552 ///
553553 /// \returns the all-ones value for an APInt of the specified bit-width.
554554 static APInt getAllOnesValue(unsigned numBits) {
555555 return APInt(numBits, WORD_MAX, true);
556556 }
557557
558 /// \brief Get the '0' value.
558 /// Get the '0' value.
559559 ///
560560 /// \returns the '0' value for an APInt of the specified bit-width.
561561 static APInt getNullValue(unsigned numBits) { return APInt(numBits, 0); }
562562
563 /// \brief Compute an APInt containing numBits highbits from this APInt.
563 /// Compute an APInt containing numBits highbits from this APInt.
564564 ///
565565 /// Get an APInt with the same BitWidth as this APInt, just zero mask
566566 /// the low bits and right shift to the least significant bit.
568568 /// \returns the high "numBits" bits of this APInt.
569569 APInt getHiBits(unsigned numBits) const;
570570
571 /// \brief Compute an APInt containing numBits lowbits from this APInt.
571 /// Compute an APInt containing numBits lowbits from this APInt.
572572 ///
573573 /// Get an APInt with the same BitWidth as this APInt, just zero mask
574574 /// the high bits.
576576 /// \returns the low "numBits" bits of this APInt.
577577 APInt getLoBits(unsigned numBits) const;
578578
579 /// \brief Return an APInt with exactly one bit set in the result.
579 /// Return an APInt with exactly one bit set in the result.
580580 static APInt getOneBitSet(unsigned numBits, unsigned BitNo) {
581581 APInt Res(numBits, 0);
582582 Res.setBit(BitNo);
583583 return Res;
584584 }
585585
586 /// \brief Get a value with a block of bits set.
586 /// Get a value with a block of bits set.
587587 ///
588588 /// Constructs an APInt value that has a contiguous range of bits set. The
589589 /// bits from loBit (inclusive) to hiBit (exclusive) will be set. All other
602602 return Res;
603603 }
604604
605 /// \brief Get a value with upper bits starting at loBit set.
605 /// Get a value with upper bits starting at loBit set.
606606 ///
607607 /// Constructs an APInt value that has a contiguous range of bits set. The
608608 /// bits from loBit (inclusive) to numBits (exclusive) will be set. All other
619619 return Res;
620620 }
621621
622 /// \brief Get a value with high bits set
622 /// Get a value with high bits set
623623 ///
624624 /// Constructs an APInt value that has the top hiBitsSet bits set.
625625 ///
631631 return Res;
632632 }
633633
634 /// \brief Get a value with low bits set
634 /// Get a value with low bits set
635635 ///
636636 /// Constructs an APInt value that has the bottom loBitsSet bits set.
637637 ///
643643 return Res;
644644 }
645645
646 /// \brief Return a value containing V broadcasted over NewLen bits.
646 /// Return a value containing V broadcasted over NewLen bits.
647647 static APInt getSplat(unsigned NewLen, const APInt &V);
648648
649 /// \brief Determine if two APInts have the same value, after zero-extending
649 /// Determine if two APInts have the same value, after zero-extending
650650 /// one of them (if needed!) to ensure that the bit-widths match.
651651 static bool isSameValue(const APInt &I1, const APInt &I2) {
652652 if (I1.getBitWidth() == I2.getBitWidth())
658658 return I1.zext(I2.getBitWidth()) == I2;
659659 }
660660
661 /// \brief Overload to compute a hash_code for an APInt value.
661 /// Overload to compute a hash_code for an APInt value.
662662 friend hash_code hash_value(const APInt &Arg);
663663
664664 /// This function returns a pointer to the internal storage of the APInt.
674674 /// \name Unary Operators
675675 /// @{
676676
677 /// \brief Postfix increment operator.
677 /// Postfix increment operator.
678678 ///
679679 /// Increments *this by 1.
680680 ///
685685 return API;
686686 }
687687
688 /// \brief Prefix increment operator.
688 /// Prefix increment operator.
689689 ///
690690 /// \returns *this incremented by one
691691 APInt &operator++();
692692
693 /// \brief Postfix decrement operator.
693 /// Postfix decrement operator.
694694 ///
695695 /// Decrements *this by 1.
696696 ///
701701 return API;
702702 }
703703
704 /// \brief Prefix decrement operator.
704 /// Prefix decrement operator.
705705 ///
706706 /// \returns *this decremented by one.
707707 APInt &operator--();
708708
709 /// \brief Logical negation operator.
709 /// Logical negation operator.
710710 ///
711711 /// Performs logical negation operation on this APInt.
712712 ///
721721 /// \name Assignment Operators
722722 /// @{
723723
724 /// \brief Copy assignment operator.
724 /// Copy assignment operator.
725725 ///
726726 /// \returns *this after assignment of RHS.
727727 APInt &operator=(const APInt &RHS) {
757757 return *this;
758758 }
759759
760 /// \brief Assignment operator.
760 /// Assignment operator.
761761 ///
762762 /// The RHS value is assigned to *this. If the significant bits in RHS exceed
763763 /// the bit width, the excess bits are truncated. If the bit width is larger
775775 return *this;
776776 }
777777
778 /// \brief Bitwise AND assignment operator.
778 /// Bitwise AND assignment operator.
779779 ///
780780 /// Performs a bitwise AND operation on this APInt and RHS. The result is
781781 /// assigned to *this.
790790 return *this;
791791 }
792792
793 /// \brief Bitwise AND assignment operator.
793 /// Bitwise AND assignment operator.
794794 ///
795795 /// Performs a bitwise AND operation on this APInt and RHS. RHS is
796796 /// logically zero-extended or truncated to match the bit-width of
805805 return *this;
806806 }
807807
808 /// \brief Bitwise OR assignment operator.
808 /// Bitwise OR assignment operator.
809809 ///
810810 /// Performs a bitwise OR operation on this APInt and RHS. The result is
811811 /// assigned *this;
820820 return *this;
821821 }
822822
823 /// \brief Bitwise OR assignment operator.
823 /// Bitwise OR assignment operator.
824824 ///
825825 /// Performs a bitwise OR operation on this APInt and RHS. RHS is
826826 /// logically zero-extended or truncated to match the bit-width of
835835 return *this;
836836 }
837837
838 /// \brief Bitwise XOR assignment operator.
838 /// Bitwise XOR assignment operator.
839839 ///
840840 /// Performs a bitwise XOR operation on this APInt and RHS. The result is
841841 /// assigned to *this.
850850 return *this;
851851 }
852852
853 /// \brief Bitwise XOR assignment operator.
853 /// Bitwise XOR assignment operator.
854854 ///
855855 /// Performs a bitwise XOR operation on this APInt and RHS. RHS is
856856 /// logically zero-extended or truncated to match the bit-width of
865865 return *this;
866866 }
867867
868 /// \brief Multiplication assignment operator.
868 /// Multiplication assignment operator.
869869 ///
870870 /// Multiplies this APInt by RHS and assigns the result to *this.
871871 ///
873873 APInt &operator*=(const APInt &RHS);
874874 APInt &operator*=(uint64_t RHS);
875875
876 /// \brief Addition assignment operator.
876 /// Addition assignment operator.
877877 ///
878878 /// Adds RHS to *this and assigns the result to *this.
879879 ///
881881 APInt &operator+=(const APInt &RHS);
882882 APInt &operator+=(uint64_t RHS);
883883
884 /// \brief Subtraction assignment operator.
884 /// Subtraction assignment operator.
885885 ///
886886 /// Subtracts RHS from *this and assigns the result to *this.
887887 ///
889889 APInt &operator-=(const APInt &RHS);
890890 APInt &operator-=(uint64_t RHS);
891891
892 /// \brief Left-shift assignment function.
892 /// Left-shift assignment function.
893893 ///
894894 /// Shifts *this left by shiftAmt and assigns the result to *this.
895895 ///
907907 return *this;
908908 }
909909
910 /// \brief Left-shift assignment function.
910 /// Left-shift assignment function.
911911 ///
912912 /// Shifts *this left by shiftAmt and assigns the result to *this.
913913 ///
918918 /// \name Binary Operators
919919 /// @{
920920
921 /// \brief Multiplication operator.
921 /// Multiplication operator.
922922 ///
923923 /// Multiplies this APInt by RHS and returns the result.
924924 APInt operator*(const APInt &RHS) const;
925925
926 /// \brief Left logical shift operator.
926 /// Left logical shift operator.
927927 ///
928928 /// Shifts this APInt left by \p Bits and returns the result.
929929 APInt operator<<(unsigned Bits) const { return shl(Bits); }
930930
931 /// \brief Left logical shift operator.
931 /// Left logical shift operator.
932932 ///
933933 /// Shifts this APInt left by \p Bits and returns the result.
934934 APInt operator<<(const APInt &Bits) const { return shl(Bits); }
935935
936 /// \brief Arithmetic right-shift function.
936 /// Arithmetic right-shift function.
937937 ///
938938 /// Arithmetic right-shift this APInt by shiftAmt.
939939 APInt ashr(unsigned ShiftAmt) const {
957957 ashrSlowCase(ShiftAmt);
958958 }
959959
960 /// \brief Logical right-shift function.
960 /// Logical right-shift function.
961961 ///
962962 /// Logical right-shift this APInt by shiftAmt.
963963 APInt lshr(unsigned shiftAmt) const {
979979 lshrSlowCase(ShiftAmt);
980980 }
981981
982 /// \brief Left-shift function.
982 /// Left-shift function.
983983 ///
984984 /// Left-shift this APInt by shiftAmt.
985985 APInt shl(unsigned shiftAmt) const {
988988 return R;
989989 }
990990
991 /// \brief Rotate left by rotateAmt.
991 /// Rotate left by rotateAmt.
992992 APInt rotl(unsigned rotateAmt) const;
993993
994 /// \brief Rotate right by rotateAmt.
994 /// Rotate right by rotateAmt.
995995 APInt rotr(unsigned rotateAmt) const;
996996
997 /// \brief Arithmetic right-shift function.
997 /// Arithmetic right-shift function.
998998 ///
999999 /// Arithmetic right-shift this APInt by shiftAmt.
10001000 APInt ashr(const APInt &ShiftAmt) const {
10061006 /// Arithmetic right-shift this APInt by shiftAmt in place.
10071007 void ashrInPlace(const APInt &shiftAmt);
10081008
1009 /// \brief Logical right-shift function.
1009 /// Logical right-shift function.
10101010 ///
10111011 /// Logical right-shift this APInt by shiftAmt.
10121012 APInt lshr(const APInt &ShiftAmt) const {
10181018 /// Logical right-shift this APInt by ShiftAmt in place.
10191019 void lshrInPlace(const APInt &ShiftAmt);
10201020
1021 /// \brief Left-shift function.
1021 /// Left-shift function.
10221022 ///
10231023 /// Left-shift this APInt by shiftAmt.
10241024 APInt shl(const APInt &ShiftAmt) const {
10271027 return R;
10281028 }
10291029
1030 /// \brief Rotate left by rotateAmt.
1030 /// Rotate left by rotateAmt.
10311031 APInt rotl(const APInt &rotateAmt) const;
10321032
1033 /// \brief Rotate right by rotateAmt.
1033 /// Rotate right by rotateAmt.
10341034 APInt rotr(const APInt &rotateAmt) const;
10351035
1036 /// \brief Unsigned division operation.
1036 /// Unsigned division operation.
10371037 ///
10381038 /// Perform an unsigned divide operation on this APInt by RHS. Both this and
10391039 /// RHS are treated as unsigned quantities for purposes of this division.
10421042 APInt udiv(const APInt &RHS) const;
10431043 APInt udiv(uint64_t RHS) const;
10441044
1045 /// \brief Signed division function for APInt.
1045 /// Signed division function for APInt.
10461046 ///
10471047 /// Signed divide this APInt by APInt RHS.
10481048 APInt sdiv(const APInt &RHS) const;
10491049 APInt sdiv(int64_t RHS) const;
10501050
1051 /// \brief Unsigned remainder operation.
1051 /// Unsigned remainder operation.
10521052 ///
10531053 /// Perform an unsigned remainder operation on this APInt with RHS being the
10541054 /// divisor. Both this and RHS are treated as unsigned quantities for purposes
10601060 APInt urem(const APInt &RHS) const;
10611061 uint64_t urem(uint64_t RHS) const;
10621062
1063 /// \brief Function for signed remainder operation.
1063 /// Function for signed remainder operation.
10641064 ///
10651065 /// Signed remainder operation on APInt.
10661066 APInt srem(const APInt &RHS) const;
10671067 int64_t srem(int64_t RHS) const;
10681068
1069 /// \brief Dual division/remainder interface.
1069 /// Dual division/remainder interface.
10701070 ///
10711071 /// Sometimes it is convenient to divide two APInt values and obtain both the
10721072 /// quotient and remainder. This function does both operations in the same
10941094 APInt sshl_ov(const APInt &Amt, bool &Overflow) const;
10951095 APInt ushl_ov(const APInt &Amt, bool &Overflow) const;
10961096
1097 /// \brief Array-indexing support.
1097 /// Array-indexing support.
10981098 ///
10991099 /// \returns the bit value at bitPosition
11001100 bool operator[](unsigned bitPosition) const {
11061106 /// \name Comparison Operators
11071107 /// @{
11081108
1109 /// \brief Equality operator.
1109 /// Equality operator.
11101110 ///
11111111 /// Compares this APInt with RHS for the validity of the equality
11121112 /// relationship.
11171117 return EqualSlowCase(RHS);
11181118 }
11191119
1120 /// \brief Equality operator.
1120 /// Equality operator.
11211121 ///
11221122 /// Compares this APInt with a uint64_t for the validity of the equality
11231123 /// relationship.
11271127 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() == Val;
11281128 }
11291129
1130 /// \brief Equality comparison.
1130 /// Equality comparison.
11311131 ///
11321132 /// Compares this APInt with RHS for the validity of the equality
11331133 /// relationship.
11351135 /// \returns true if *this == Val
11361136 bool eq(const APInt &RHS) const { return (*this) == RHS; }
11371137
1138 /// \brief Inequality operator.
1138 /// Inequality operator.
11391139 ///
11401140 /// Compares this APInt with RHS for the validity of the inequality
11411141 /// relationship.
11431143 /// \returns true if *this != Val
11441144 bool operator!=(const APInt &RHS) const { return !((*this) == RHS); }
11451145
1146 /// \brief Inequality operator.
1146 /// Inequality operator.
11471147 ///
11481148 /// Compares this APInt with a uint64_t for the validity of the inequality
11491149 /// relationship.
11511151 /// \returns true if *this != Val
11521152 bool operator!=(uint64_t Val) const { return !((*this) == Val); }
11531153
1154 /// \brief Inequality comparison
1154 /// Inequality comparison
11551155 ///
11561156 /// Compares this APInt with RHS for the validity of the inequality
11571157 /// relationship.
11591159 /// \returns true if *this != Val
11601160 bool ne(const APInt &RHS) const { return !((*this) == RHS); }
11611161
1162 /// \brief Unsigned less than comparison
1162 /// Unsigned less than comparison
11631163 ///
11641164 /// Regards both *this and RHS as unsigned quantities and compares them for
11651165 /// the validity of the less-than relationship.
11671167 /// \returns true if *this < RHS when both are considered unsigned.
11681168 bool ult(const APInt &RHS) const { return compare(RHS) < 0; }
11691169
1170 /// \brief Unsigned less than comparison
1170 /// Unsigned less than comparison
11711171 ///
11721172 /// Regards both *this as an unsigned quantity and compares it with RHS for
11731173 /// the validity of the less-than relationship.
11781178 return (isSingleWord() || getActiveBits() <= 64) && getZExtValue() < RHS;
11791179 }
11801180
1181 /// \brief Signed less than comparison
1181 /// Signed less than comparison
11821182 ///
11831183 /// Regards both *this and RHS as signed quantities and compares them for
11841184 /// validity of the less-than relationship.
11861186 /// \returns true if *this < RHS when both are considered signed.
11871187 bool slt(const APInt &RHS) const { return compareSigned(RHS) < 0; }
11881188
1189 /// \brief Signed less than comparison
1189 /// Signed less than comparison
11901190 ///
11911191 /// Regards both *this as a signed quantity and compares it with RHS for
11921192 /// the validity of the less-than relationship.
11971197 : getSExtValue() < RHS;
11981198 }
11991199
1200 /// \brief Unsigned less or equal comparison
1200 /// Unsigned less or equal comparison
12011201 ///
12021202 /// Regards both *this and RHS as unsigned quantities and compares them for
12031203 /// validity of the less-or-equal relationship.
12051205 /// \returns true if *this <= RHS when both are considered unsigned.
12061206 bool ule(const APInt &RHS) const { return compare(RHS) <= 0; }
12071207
1208 /// \brief Unsigned less or equal comparison
1208 /// Unsigned less or equal comparison
12091209 ///
12101210 /// Regards both *this as an unsigned quantity and compares it with RHS for
12111211 /// the validity of the less-or-equal relationship.
12131213 /// \returns true if *this <= RHS when considered unsigned.
12141214 bool ule(uint64_t RHS) const { return !ugt(RHS); }
12151215
1216 /// \brief Signed less or equal comparison
1216 /// Signed less or equal comparison
12171217 ///
12181218 /// Regards both *this and RHS as signed quantities and compares them for
12191219 /// validity of the less-or-equal relationship.
12211221 /// \returns true if *this <= RHS when both are considered signed.
12221222 bool sle(const APInt &RHS) const { return compareSigned(RHS) <= 0; }
12231223
1224 /// \brief Signed less or equal comparison
1224 /// Signed less or equal comparison
12251225 ///
12261226 /// Regards both *this as a signed quantity and compares it with RHS for the
12271227 /// validity of the less-or-equal relationship.
12291229 /// \returns true if *this <= RHS when considered signed.
12301230 bool sle(uint64_t RHS) const { return !sgt(RHS); }
12311231
1232 /// \brief Unsigned greather than comparison
1232 /// Unsigned greather than comparison
12331233 ///
12341234 /// Regards both *this and RHS as unsigned quantities and compares them for
12351235 /// the validity of the greater-than relationship.
12371237 /// \returns true if *this > RHS when both are considered unsigned.
12381238 bool ugt(const APInt &RHS) const { return !ule(RHS); }
12391239
1240 /// \brief Unsigned greater than comparison
1240 /// Unsigned greater than comparison
12411241 ///
12421242 /// Regards both *this as an unsigned quantity and compares it with RHS for
12431243 /// the validity of the greater-than relationship.
12481248 return (!isSingleWord() && getActiveBits() > 64) || getZExtValue() > RHS;
12491249 }
12501250
1251 /// \brief Signed greather than comparison
1251 /// Signed greather than comparison
12521252 ///
12531253 /// Regards both *this and RHS as signed quantities and compares them for the
12541254 /// validity of the greater-than relationship.
12561256 /// \returns true if *this > RHS when both are considered signed.
12571257 bool sgt(const APInt &RHS) const { return !sle(RHS); }
12581258
1259 /// \brief Signed greater than comparison
1259 /// Signed greater than comparison
12601260 ///
12611261 /// Regards both *this as a signed quantity and compares it with RHS for
12621262 /// the validity of the greater-than relationship.
12671267 : getSExtValue() > RHS;
12681268 }
12691269
1270 /// \brief Unsigned greater or equal comparison
1270 /// Unsigned greater or equal comparison
12711271 ///
12721272 /// Regards both *this and RHS as unsigned quantities and compares them for
12731273 /// validity of the greater-or-equal relationship.
12751275 /// \returns true if *this >= RHS when both are considered unsigned.
12761276 bool uge(const APInt &RHS) const { return !ult(RHS); }
12771277
1278 /// \brief Unsigned greater or equal comparison
1278 /// Unsigned greater or equal comparison
12791279 ///
12801280 /// Regards both *this as an unsigned quantity and compares it with RHS for
12811281 /// the validity of the greater-or-equal relationship.
12831283 /// \returns true if *this >= RHS when considered unsigned.
12841284 bool uge(uint64_t RHS) const { return !ult(RHS); }
12851285
1286 /// \brief Signed greater or equal comparison
1286 /// Signed greater or equal comparison
12871287 ///
12881288 /// Regards both *this and RHS as signed quantities and compares them for
12891289 /// validity of the greater-or-equal relationship.
12911291 /// \returns true if *this >= RHS when both are considered signed.
12921292 bool sge(const APInt &RHS) const { return !slt(RHS); }
12931293
1294 /// \brief Signed greater or equal comparison
1294 /// Signed greater or equal comparison
12951295 ///
12961296 /// Regards both *this as a signed quantity and compares it with RHS for
12971297 /// the validity of the greater-or-equal relationship.
13201320 /// \name Resizing Operators
13211321 /// @{
13221322
1323 /// \brief Truncate to new width.
1323 /// Truncate to new width.
13241324 ///
13251325 /// Truncate the APInt to a specified width. It is an error to specify a width
13261326 /// that is greater than or equal to the current width.
13271327 APInt trunc(unsigned width) const;
13281328
1329 /// \brief Sign extend to a new width.
1329 /// Sign extend to a new width.
13301330 ///
13311331 /// This operation sign extends the APInt to a new width. If the high order
13321332 /// bit is set, the fill on the left will be done with 1 bits, otherwise zero.
13341334 /// current width.
13351335 APInt sext(unsigned width) const;
13361336
1337 /// \brief Zero extend to a new width.
1337 /// Zero extend to a new width.
13381338 ///
13391339 /// This operation zero extends the APInt to a new width. The high order bits
13401340 /// are filled with 0 bits. It is an error to specify a width that is less
13411341 /// than or equal to the current width.
13421342 APInt zext(unsigned width) const;
13431343
1344 /// \brief Sign extend or truncate to width
1344 /// Sign extend or truncate to width
13451345 ///
13461346 /// Make this APInt have the bit width given by \p width. The value is sign
13471347 /// extended, truncated, or left alone to make it that width.
13481348 APInt sextOrTrunc(unsigned width) const;
13491349
1350 /// \brief Zero extend or truncate to width
1350 /// Zero extend or truncate to width
13511351 ///
13521352 /// Make this APInt have the bit width given by \p width. The value is zero
13531353 /// extended, truncated, or left alone to make it that width.
13541354 APInt zextOrTrunc(unsigned width) const;
13551355
1356 /// \brief Sign extend or truncate to width
1356 /// Sign extend or truncate to width
13571357 ///
13581358 /// Make this APInt have the bit width given by \p width. The value is sign
13591359 /// extended, or left alone to make it that width.
13601360 APInt sextOrSelf(unsigned width) const;
13611361
1362 /// \brief Zero extend or truncate to width
1362 /// Zero extend or truncate to width
13631363 ///
13641364 /// Make this APInt have the bit width given by \p width. The value is zero
13651365 /// extended, or left alone to make it that width.
13691369 /// \name Bit Manipulation Operators
13701370 /// @{
13711371
1372 /// \brief Set every bit to 1.
1372 /// Set every bit to 1.
13731373 void setAllBits() {
13741374 if (isSingleWord())
13751375 U.VAL = WORD_MAX;
13801380 clearUnusedBits();
13811381 }
13821382
1383 /// \brief Set a given bit to 1.
1383 /// Set a given bit to 1.
13841384 ///
13851385 /// Set the given bit to 1 whose position is given as "bitPosition".
13861386 void setBit(unsigned BitPosition) {
14311431 return setBits(BitWidth - hiBits, BitWidth);
14321432 }
14331433
1434 /// \brief Set every bit to 0.
1434 /// Set every bit to 0.
14351435 void clearAllBits() {
14361436 if (isSingleWord())
14371437 U.VAL = 0;
14391439 memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE);
14401440 }
14411441
1442 /// \brief Set a given bit to 0.
1442 /// Set a given bit to 0.
14431443 ///
14441444 /// Set the given bit to 0 whose position is given as "bitPosition".
14451445 void clearBit(unsigned BitPosition) {
14561456 clearBit(BitWidth - 1);
14571457 }
14581458
1459 /// \brief Toggle every bit to its opposite value.
1459 /// Toggle every bit to its opposite value.
14601460 void flipAllBits() {
14611461 if (isSingleWord()) {
14621462 U.VAL ^= WORD_MAX;
14661466 }
14671467 }
14681468
1469 /// \brief Toggles a given bit to its opposite value.
1469 /// Toggles a given bit to its opposite value.
14701470 ///
14711471 /// Toggle a given bit to its opposite value whose position is given
14721472 /// as "bitPosition".
14881488 /// \name Value Characterization Functions
14891489 /// @{
14901490
1491 /// \brief Return the number of bits in the APInt.
1491 /// Return the number of bits in the APInt.
14921492 unsigned getBitWidth() const { return BitWidth; }
14931493
1494 /// \brief Get the number of words.
1494 /// Get the number of words.
14951495 ///
14961496 /// Here one word's bitwidth equals to that of uint64_t.
14971497 ///
14981498 /// \returns the number of words to hold the integer value of this APInt.
14991499 unsigned getNumWords() const { return getNumWords(BitWidth); }
15001500
1501 /// \brief Get the number of words.
1501 /// Get the number of words.
15021502 ///
15031503 /// *NOTE* Here one word's bitwidth equals to that of uint64_t.
15041504 ///
15081508 return ((uint64_t)BitWidth + APINT_BITS_PER_WORD - 1) / APINT_BITS_PER_WORD;
15091509 }
15101510
1511 /// \brief Compute the number of active bits in the value
1511 /// Compute the number of active bits in the value
15121512 ///
15131513 /// This function returns the number of active bits which is defined as the
15141514 /// bit width minus the number of leading zeros. This is used in several
15151515 /// computations to see how "wide" the value is.
15161516 unsigned getActiveBits() const { return BitWidth - countLeadingZeros(); }
15171517
1518 /// \brief Compute the number of active words in the value of this APInt.
1518 /// Compute the number of active words in the value of this APInt.
15191519 ///
15201520 /// This is used in conjunction with getActiveData to extract the raw value of
15211521 /// the APInt.
15241524 return numActiveBits ? whichWord(numActiveBits - 1) + 1 : 1;
15251525 }
15261526
1527 /// \brief Get the minimum bit size for this signed APInt
1527 /// Get the minimum bit size for this signed APInt
15281528 ///
15291529 /// Computes the minimum bit width for this APInt while considering it to be a
15301530 /// signed (and probably negative) value. If the value is not negative, this
15381538 return getActiveBits() + 1;
15391539 }
15401540
1541 /// \brief Get zero extended value
1541 /// Get zero extended value
15421542 ///
15431543 /// This method attempts to return the value of this APInt as a zero extended
15441544 /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
15501550 return U.pVal[0];
15511551 }
15521552
1553 /// \brief Get sign extended value
1553 /// Get sign extended value
15541554 ///
15551555 /// This method attempts to return the value of this APInt as a sign extended
15561556 /// int64_t. The bit width must be <= 64 or the value must fit within an
15621562 return int64_t(U.pVal[0]);
15631563 }
15641564
1565 /// \brief Get bits required for string value.
1565 /// Get bits required for string value.
15661566 ///
15671567 /// This method determines how many bits are required to hold the APInt
15681568 /// equivalent of the string given by \p str.
15691569 static unsigned getBitsNeeded(StringRef str, uint8_t radix);
15701570
1571 /// \brief The APInt version of the countLeadingZeros functions in
1571 /// The APInt version of the countLeadingZeros functions in
15721572 /// MathExtras.h.
15731573 ///
15741574 /// It counts the number of zeros from the most significant bit to the first
15841584 return countLeadingZerosSlowCase();
15851585 }
15861586
1587 /// \brief Count the number of leading one bits.
1587 /// Count the number of leading one bits.
15881588 ///
15891589 /// This function is an APInt version of the countLeadingOnes
15901590 /// functions in MathExtras.h. It counts the number of ones from the most
16041604 return isNegative() ? countLeadingOnes() : countLeadingZeros();
16051605 }
16061606
1607 /// \brief Count the number of trailing zero bits.
1607 /// Count the number of trailing zero bits.
16081608 ///
16091609 /// This function is an APInt version of the countTrailingZeros
16101610 /// functions in MathExtras.h. It counts the number of zeros from the least
16181618 return countTrailingZerosSlowCase();
16191619 }
16201620
1621 /// \brief Count the number of trailing one bits.
1621 /// Count the number of trailing one bits.
16221622 ///
16231623 /// This function is an APInt version of the countTrailingOnes
16241624 /// functions in MathExtras.h. It counts the number of ones from the least
16321632 return countTrailingOnesSlowCase();
16331633 }
16341634
1635 /// \brief Count the number of bits set.
1635 /// Count the number of bits set.
16361636 ///
16371637 /// This function is an APInt version of the countPopulation functions
16381638 /// in MathExtras.h. It counts the number of 1 bits in the APInt value.
16661666 toString(Str, Radix, true, false);
16671667 }
16681668
1669 /// \brief Return the APInt as a std::string.
1669 /// Return the APInt as a std::string.
16701670 ///
16711671 /// Note that this is an inefficient method. It is better to pass in a
16721672 /// SmallVector/SmallString to the methods above to avoid thrashing the heap
16801680 /// Value.
16811681 APInt reverseBits() const;
16821682
1683 /// \brief Converts this APInt to a double value.
1683 /// Converts this APInt to a double value.
16841684 double roundToDouble(bool isSigned) const;
16851685
1686 /// \brief Converts this unsigned APInt to a double value.
1686 /// Converts this unsigned APInt to a double value.
16871687 double roundToDouble() const { return roundToDouble(false); }
16881688
1689 /// \brief Converts this signed APInt to a double value.
1689 /// Converts this signed APInt to a double value.
16901690 double signedRoundToDouble() const { return roundToDouble(true); }
16911691
1692 /// \brief Converts APInt bits to a double
1692 /// Converts APInt bits to a double
16931693 ///
16941694 /// The conversion does not do a translation from integer to double, it just
16951695 /// re-interprets the bits as a double. Note that it is valid to do this on
16981698 return BitsToDouble(getWord(0));
16991699 }
17001700
1701 /// \brief Converts APInt bits to a double
1701 /// Converts APInt bits to a double
17021702 ///
17031703 /// The conversion does not do a translation from integer to float, it just
17041704 /// re-interprets the bits as a float. Note that it is valid to do this on
17071707 return BitsToFloat(getWord(0));
17081708 }
17091709
1710 /// \brief Converts a double to APInt bits.
1710 /// Converts a double to APInt bits.
17111711 ///
17121712 /// The conversion does not do a translation from double to integer, it just
17131713 /// re-interprets the bits of the double.
17151715 return APInt(sizeof(double) * CHAR_BIT, DoubleToBits(V));
17161716 }
17171717
1718 /// \brief Converts a float to APInt bits.
1718 /// Converts a float to APInt bits.
17191719 ///
17201720 /// The conversion does not do a translation from float to integer, it just
17211721 /// re-interprets the bits of the float.
17741774 return logBase2();
17751775 }
17761776
1777 /// \brief Compute the square root
1777 /// Compute the square root
17781778 APInt sqrt() const;
17791779
1780 /// \brief Get the absolute value;
1780 /// Get the absolute value;
17811781 ///
17821782 /// If *this is < 0 then return -(*this), otherwise *this;
17831783 APInt abs() const {
19281928 /// Set the least significant BITS and clear the rest.
19291929 static void tcSetLeastSignificantBits(WordType *, unsigned, unsigned bits);
19301930
1931 /// \brief debug method
1931 /// debug method
19321932 void dump() const;
19331933
19341934 /// @}
19511951
19521952 inline bool operator!=(uint64_t V1, const APInt &V2) { return V2 != V1; }
19531953
1954 /// \brief Unary bitwise complement operator.
1954 /// Unary bitwise complement operator.
19551955 ///
19561956 /// \returns an APInt that is the bitwise complement of \p v.
19571957 inline APInt operator~(APInt v) {
20842084
20852085 namespace APIntOps {
20862086
2087 /// \brief Determine the smaller of two APInts considered to be signed.
2087 /// Determine the smaller of two APInts considered to be signed.
20882088 inline const APInt &smin(const APInt &A, const APInt &B) {
20892089 return A.slt(B) ? A : B;
20902090 }
20912091
2092 /// \brief Determine the larger of two APInts considered to be signed.
2092 /// Determine the larger of two APInts considered to be signed.
20932093 inline const APInt &smax(const APInt &A, const APInt &B) {
20942094 return A.sgt(B) ? A : B;
20952095 }
20962096
2097 /// \brief Determine the smaller of two APInts considered to be signed.
2097 /// Determine the smaller of two APInts considered to be signed.
20982098 inline const APInt &umin(const APInt &A, const APInt &B) {
20992099 return A.ult(B) ? A : B;
21002100 }
21012101
2102 /// \brief Determine the larger of two APInts considered to be unsigned.
2102 /// Determine the larger of two APInts considered to be unsigned.
21032103 inline const APInt &umax(const APInt &A, const APInt &B) {
21042104 return A.ugt(B) ? A : B;
21052105 }
21062106
2107 /// \brief Compute GCD of two unsigned APInt values.
2107 /// Compute GCD of two unsigned APInt values.
21082108 ///
21092109 /// This function returns the greatest common divisor of the two APInt values
21102110 /// using Stein's algorithm.
21122112 /// \returns the greatest common divisor of A and B.
21132113 APInt GreatestCommonDivisor(APInt A, APInt B);
21142114
2115 /// \brief Converts the given APInt to a double value.
2115 /// Converts the given APInt to a double value.
21162116 ///
21172117 /// Treats the APInt as an unsigned value for conversion purposes.
21182118 inline double RoundAPIntToDouble(const APInt &APIVal) {
21192119 return APIVal.roundToDouble();
21202120 }
21212121
2122 /// \brief Converts the given APInt to a double value.
2122 /// Converts the given APInt to a double value.
21232123 ///
21242124 /// Treats the APInt as a signed value for conversion purposes.
21252125 inline double RoundSignedAPIntToDouble(const APInt &APIVal) {
21262126 return APIVal.signedRoundToDouble();
21272127 }
21282128
2129 /// \brief Converts the given APInt to a float vlalue.
2129 /// Converts the given APInt to a float vlalue.
21302130 inline float RoundAPIntToFloat(const APInt &APIVal) {
21312131 return float(RoundAPIntToDouble(APIVal));
21322132 }
21332133
2134 /// \brief Converts the given APInt to a float value.
2134 /// Converts the given APInt to a float value.
21352135 ///
21362136 /// Treast the APInt as a signed value for conversion purposes.
21372137 inline float RoundSignedAPIntToFloat(const APInt &APIVal) {
21382138 return float(APIVal.signedRoundToDouble());
21392139 }
21402140
2141 /// \brief Converts the given double value into a APInt.
2141 /// Converts the given double value into a APInt.
21422142 ///
21432143 /// This function convert a double value to an APInt value.
21442144 APInt RoundDoubleToAPInt(double Double, unsigned width);
21452145
2146 /// \brief Converts a float value into a APInt.
2146 /// Converts a float value into a APInt.
21472147 ///
21482148 /// Converts a float value into an APInt value.
21492149 inline APInt RoundFloatToAPInt(float Float, unsigned width) {
7171 }
7272 using APInt::toString;
7373
74 /// \brief Get the correctly-extended \c int64_t value.
74 /// Get the correctly-extended \c int64_t value.
7575 int64_t getExtValue() const {
7676 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
7777 return isSigned() ? getSExtValue() : getZExtValue();
278278 : APInt::getSignedMinValue(numBits), Unsigned);
279279 }
280280
281 /// \brief Determine if two APSInts have the same value, zero- or
281 /// Determine if two APSInts have the same value, zero- or
282282 /// sign-extending as needed.
283283 static bool isSameValue(const APSInt &I1, const APSInt &I2) {
284284 return !compareValues(I1, I2);
285285 }
286286
287 /// \brief Compare underlying values of two numbers.
287 /// Compare underlying values of two numbers.
288288 static int compareValues(const APSInt &I1, const APSInt &I2) {
289289 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
290290 return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
183183 /// slice(n) - Chop off the first N elements of the array.
184184 ArrayRef slice(size_t N) const { return slice(N, size() - N); }
185185
186 /// \brief Drop the first \p N elements of the array.
186 /// Drop the first \p N elements of the array.
187187 ArrayRef drop_front(size_t N = 1) const {
188188 assert(size() >= N && "Dropping more elements than exist");
189189 return slice(N, size() - N);
190190 }
191191
192 /// \brief Drop the last \p N elements of the array.
192 /// Drop the last \p N elements of the array.
193193 ArrayRef drop_back(size_t N = 1) const {
194194 assert(size() >= N && "Dropping more elements than exist");
195195 return slice(0, size() - N);
196196 }
197197
198 /// \brief Return a copy of *this with the first N elements satisfying the
198 /// Return a copy of *this with the first N elements satisfying the
199199 /// given predicate removed.
200200 template ArrayRef drop_while(PredicateT Pred) const {
201201 return ArrayRef(find_if_not(*this, Pred), end());
202202 }
203203
204 /// \brief Return a copy of *this with the first N elements not satisfying
204 /// Return a copy of *this with the first N elements not satisfying
205205 /// the given predicate removed.
206206 template ArrayRef drop_until(PredicateT Pred) const {
207207 return ArrayRef(find_if(*this, Pred), end());
208208 }
209209
210 /// \brief Return a copy of *this with only the first \p N elements.
210 /// Return a copy of *this with only the first \p N elements.
211211 ArrayRef take_front(size_t N = 1) const {
212212 if (N >= size())
213213 return *this;
214214 return drop_back(size() - N);
215215 }
216216
217 /// \brief Return a copy of *this with only the last \p N elements.
217 /// Return a copy of *this with only the last \p N elements.
218218 ArrayRef take_back(size_t N = 1) const {
219219 if (N >= size())
220220 return *this;
221221 return drop_front(size() - N);
222222 }
223223
224 /// \brief Return the first N elements of this Array that satisfy the given
224 /// Return the first N elements of this Array that satisfy the given
225225 /// predicate.
226226 template ArrayRef take_while(PredicateT Pred) const {
227227 return ArrayRef(begin(), find_if_not(*this, Pred));
228228 }
229229
230 /// \brief Return the first N elements of this Array that don't satisfy the
230 /// Return the first N elements of this Array that don't satisfy the
231231 /// given predicate.
232232 template ArrayRef take_until(PredicateT Pred) const {
233233 return ArrayRef(begin(), find_if(*this, Pred));
357357 return slice(N, this->size() - N);
358358 }
359359
360 /// \brief Drop the first \p N elements of the array.
360 /// Drop the first \p N elements of the array.
361361 MutableArrayRef drop_front(size_t N = 1) const {
362362 assert(this->size() >= N && "Dropping more elements than exist");
363363 return slice(N, this->size() - N);
368368 return slice(0, this->size() - N);
369369 }
370370
371 /// \brief Return a copy of *this with the first N elements satisfying the
371 /// Return a copy of *this with the first N elements satisfying the
372372 /// given predicate removed.
373373 template
374374 MutableArrayRef drop_while(PredicateT Pred) const {
375375 return MutableArrayRef(find_if_not(*this, Pred), end());
376376 }
377377
378 /// \brief Return a copy of *this with the first N elements not satisfying
378 /// Return a copy of *this with the first N elements not satisfying
379379 /// the given predicate removed.
380380 template
381381 MutableArrayRef drop_until(PredicateT Pred) const {
382382 return MutableArrayRef(find_if(*this, Pred), end());
383383 }
384384
385 /// \brief Return a copy of *this with only the first \p N elements.
385 /// Return a copy of *this with only the first \p N elements.
386386 MutableArrayRef take_front(size_t N = 1) const {
387387 if (N >= this->size())
388388 return *this;
389389 return drop_back(this->size() - N);
390390 }
391391
392 /// \brief Return a copy of *this with only the last \p N elements.
392 /// Return a copy of *this with only the last \p N elements.
393393 MutableArrayRef take_back(size_t N = 1) const {
394394 if (N >= this->size())
395395 return *this;
396396 return drop_front(this->size() - N);
397397 }
398398
399 /// \brief Return the first N elements of this Array that satisfy the given
399 /// Return the first N elements of this Array that satisfy the given
400400 /// predicate.
401401 template
402402 MutableArrayRef take_while(PredicateT Pred) const {
403403 return MutableArrayRef(begin(), find_if_not(*this, Pred));
404404 }
405405
406 /// \brief Return the first N elements of this Array that don't satisfy the
406 /// Return the first N elements of this Array that don't satisfy the
407407 /// given predicate.
408408 template
409409 MutableArrayRef take_until(PredicateT Pred) const {
778778 }
779779
780780 private:
781 /// \brief Perform a logical left shift of \p Count words by moving everything
781 /// Perform a logical left shift of \p Count words by moving everything
782782 /// \p Count words to the right in memory.
783783 ///
784784 /// While confusing, words are stored from least significant at Bits[0] to
809809 clear_unused_bits();
810810 }
811811
812 /// \brief Perform a logical right shift of \p Count words by moving those
812 /// Perform a logical right shift of \p Count words by moving those
813813 /// words to the left in memory. See wordShl for more information.
814814 ///
815815 void wordShr(uint32_t Count) {
176176 return *this;
177177 }
178178
179 /// \brief Skips all children of the current node and traverses to next node
179 /// Skips all children of the current node and traverses to next node
180180 ///
181181 /// Note: This function takes care of incrementing the iterator. If you
182182 /// always increment and call this function, you risk walking off the end.
2323
2424 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
2525
26 /// \brief A base class for data structure classes wishing to make iterators
26 /// A base class for data structure classes wishing to make iterators
2727 /// ("handles") pointing into themselves fail-fast. When building without
2828 /// asserts, this class is empty and does nothing.
2929 ///
3838 public:
3939 DebugEpochBase() : Epoch(0) {}
4040
41 /// \brief Calling incrementEpoch invalidates all handles pointing into the
41 /// Calling incrementEpoch invalidates all handles pointing into the
4242 /// calling instance.
4343 void incrementEpoch() { ++Epoch; }
4444
45 /// \brief The destructor calls incrementEpoch to make use-after-free bugs
45 /// The destructor calls incrementEpoch to make use-after-free bugs
4646 /// more likely to crash deterministically.
4747 ~DebugEpochBase() { incrementEpoch(); }
4848
49 /// \brief A base class for iterator classes ("handles") that wish to poll for
49 /// A base class for iterator classes ("handles") that wish to poll for
5050 /// iterator invalidating modifications in the underlying data structure.
5151 /// When LLVM is built without asserts, this class is empty and does nothing.
5252 ///
6464 explicit HandleBase(const DebugEpochBase *Parent)
6565 : EpochAddress(&Parent->Epoch), EpochAtCreation(Parent->Epoch) {}
6666
67 /// \brief Returns true if the DebugEpochBase this Handle is linked to has
67 /// Returns true if the DebugEpochBase this Handle is linked to has
6868 /// not called incrementEpoch on itself since the creation of this
6969 /// HandleBase instance.
7070 bool isHandleInSync() const { return *EpochAddress == EpochAtCreation; }
7171
72 /// \brief Returns a pointer to the epoch word stored in the data structure
72 /// Returns a pointer to the epoch word stored in the data structure
7373 /// this handle points into. Can be used to check if two iterators point
7474 /// into the same data structure.
7575 const void *getEpochAddress() const { return EpochAddress; }
5656
5757 namespace llvm {
5858
59 /// \brief An opaque object representing a hash code.
59 /// An opaque object representing a hash code.
6060 ///
6161 /// This object represents the result of hashing some entity. It is intended to
6262 /// be used to implement hashtables or other hashing-based data structures.
7272 size_t value;
7373
7474 public:
75 /// \brief Default construct a hash_code.
75 /// Default construct a hash_code.
7676 /// Note that this leaves the value uninitialized.
7777 hash_code() = default;
7878
79 /// \brief Form a hash code directly from a numerical value.
79 /// Form a hash code directly from a numerical value.
8080 hash_code(size_t value) : value(value) {}
8181
82 /// \brief Convert the hash code to its numerical value for use.
82 /// Convert the hash code to its numerical value for use.
8383 /*explicit*/ operator size_t() const { return value; }
8484
8585 friend bool operator==(const hash_code &lhs, const hash_code &rhs) {
8989 return lhs.value != rhs.value;
9090 }
9191
92 /// \brief Allow a hash_code to be directly run through hash_value.
92 /// Allow a hash_code to be directly run through hash_value.
9393 friend size_t hash_value(const hash_code &code) { return code.value; }
9494 };
9595
96 /// \brief Compute a hash_code for any integer value.
96 /// Compute a hash_code for any integer value.
9797 ///
9898 /// Note that this function is intended to compute the same hash_code for
9999 /// a particular value without regard to the pre-promotion type. This is in
104104 typename std::enable_if::value, hash_code>::type
105105 hash_value(T value);
106106
107 /// \brief Compute a hash_code for a pointer's address.
107 /// Compute a hash_code for a pointer's address.
108108 ///
109109 /// N.B.: This hashes the *address*. Not the value and not the type.
110110 template hash_code hash_value(const T *ptr);
111111
112 /// \brief Compute a hash_code for a pair of objects.
112 /// Compute a hash_code for a pair of objects.
113113 template
114114 hash_code hash_value(const std::pair &arg);
115115
116 /// \brief Compute a hash_code for a standard string.
116 /// Compute a hash_code for a standard string.
117117 template
118118 hash_code hash_value(const std::basic_string &arg);
119119
120120
121 /// \brief Override the execution seed with a fixed value.
121 /// Override the execution seed with a fixed value.
122122 ///
123123 /// This hashing library uses a per-execution seed designed to change on each
124124 /// run with high probability in order to ensure that the hash codes are not
163163 static const uint64_t k2 = 0x9ae16a3b2f90404fULL;
164164 static const uint64_t k3 = 0xc949d7c7509e6557ULL;
165165
166 /// \brief Bitwise right rotate.
166 /// Bitwise right rotate.
167167 /// Normally this will compile to a single instruction, especially if the
168168 /// shift is a manifest constant.
169169 inline uint64_t rotate(uint64_t val, size_t shift) {
253253 return k2 ^ seed;
254254 }
255255
256 /// \brief The intermediate state used during hashing.
256 /// The intermediate state used during hashing.
257257 /// Currently, the algorithm for computing hash codes is based on CityHash and
258258 /// keeps 56 bytes of arbitrary state.
259259 struct hash_state {
260260 uint64_t h0, h1, h2, h3, h4, h5, h6;
261261
262 /// \brief Create a new hash_state structure and initialize it based on the
262 /// Create a new hash_state structure and initialize it based on the
263263 /// seed and the first 64-byte chunk.
264264 /// This effectively performs the initial mix.
265265 static hash_state create(const char *s, uint64_t seed) {
271271 return state;
272272 }
273273
274 /// \brief Mix 32-bytes from the input sequence into the 16-bytes of 'a'
274 /// Mix 32-bytes from the input sequence into the 16-bytes of 'a'
275275 /// and 'b', including whatever is already in 'a' and 'b'.
276276 static void mix_32_bytes(const char *s, uint64_t &a, uint64_t &b) {
277277 a += fetch64(s);
283283 a += c;
284284 }
285285
286 /// \brief Mix in a 64-byte buffer of data.
286 /// Mix in a 64-byte buffer of data.
287287 /// We mix all 64 bytes even when the chunk length is smaller, but we
288288 /// record the actual length.
289289 void mix(const char *s) {
301301 std::swap(h2, h0);
302302 }
303303
304 /// \brief Compute the final 64-bit hash code value based on the current
304 /// Compute the final 64-bit hash code value based on the current
305305 /// state and the length of bytes hashed.
306306 uint64_t finalize(size_t length) {
307307 return hash_16_bytes(hash_16_bytes(h3, h5) + shift_mix(h1) * k1 + h2,
310310 };
311311
312312
313 /// \brief A global, fixed seed-override variable.
313 /// A global, fixed seed-override variable.
314314 ///
315315 /// This variable can be set using the \see llvm::set_fixed_execution_seed
316316 /// function. See that function for details. Do not, under any circumstances,
331331 }
332332
333333
334 /// \brief Trait to indicate whether a type's bits can be hashed directly.
334 /// Trait to indicate whether a type's bits can be hashed directly.
335335 ///
336336 /// A type trait which is true if we want to combine values for hashing by
337337 /// reading the underlying data. It is false if values of this type must
358358 (sizeof(T) + sizeof(U)) ==
359359 sizeof(std::pair))> {};
360360
361 /// \brief Helper to get the hashable data representation for a type.
361 /// Helper to get the hashable data representation for a type.
362362 /// This variant is enabled when the type itself can be used.
363363 template
364364 typename std::enable_if::value, T>::type
365365 get_hashable_data(const T &value) {
366366 return value;
367367 }
368 /// \brief Helper to get the hashable data representation for a type.
368 /// Helper to get the hashable data representation for a type.
369369 /// This variant is enabled when we must first call hash_value and use the
370370 /// result as our data.
371371 template
375375 return hash_value(value);
376376 }
377377
378 /// \brief Helper to store data from a value into a buffer and advance the
378 /// Helper to store data from a value into a buffer and advance the
379379 /// pointer into that buffer.
380380 ///
381381 /// This routine first checks whether there is enough space in the provided
394394 return true;
395395 }
396396
397 /// \brief Implement the combining of integral values into a hash_code.
397 /// Implement the combining of integral values into a hash_code.
398398 ///
399399 /// This overload is selected when the value type of the iterator is
400400 /// integral. Rather than computing a hash_code for each object and then
434434 return state.finalize(length);
435435 }
436436
437 /// \brief Implement the combining of integral values into a hash_code.
437 /// Implement the combining of integral values into a hash_code.
438438 ///
439439 /// This overload is selected when the value type of the iterator is integral
440440 /// and when the input iterator is actually a pointer. Rather than computing
469469 } // namespace hashing
470470
471471
472 /// \brief Compute a hash_code for a sequence of values.
472 /// Compute a hash_code for a sequence of values.
473473 ///
474474 /// This hashes a sequence of values. It produces the same hash_code as
475475 /// 'hash_combine(a, b, c, ...)', but can run over arbitrary sized sequences
485485 namespace hashing {
486486 namespace detail {
487487
488 /// \brief Helper class to manage the recursive combining of hash_combine
488 /// Helper class to manage the recursive combining of hash_combine
489489 /// arguments.
490490 ///
491491 /// This class exists to manage the state and various calls involved in the
498498 const size_t seed;
499499
500500 public:
501 /// \brief Construct a recursive hash combining helper.
501 /// Construct a recursive hash combining helper.
502502 ///
503503 /// This sets up the state for a recursive hash combine, including getting
504504 /// the seed and buffer setup.
505505 hash_combine_recursive_helper()
506506 : seed(get_execution_seed()) {}
507507
508 /// \brief Combine one chunk of data into the current in-flight hash.
508 /// Combine one chunk of data into the current in-flight hash.
509509 ///
510510 /// This merges one chunk of data into the hash. First it tries to buffer
511511 /// the data. If the buffer is full, it hashes the buffer into its
546546 return buffer_ptr;
547547 }
548548
549 /// \brief Recursive, variadic combining method.
549 /// Recursive, variadic combining method.
550550 ///
551551 /// This function recurses through each argument, combining that argument
552552 /// into a single hash.
559559 return combine(length, buffer_ptr, buffer_end, args...);
560560 }
561561
562 /// \brief Base case for recursive, variadic combining.
562 /// Base case for recursive, variadic combining.
563563 ///
564564 /// The base case when combining arguments recursively is reached when all
565565 /// arguments have been handled. It flushes the remaining buffer and
587587 } // namespace detail
588588 } // namespace hashing
589589
590 /// \brief Combine values into a single hash_code.
590 /// Combine values into a single hash_code.
591591 ///
592592 /// This routine accepts a varying number of arguments of any type. It will
593593 /// attempt to combine them into a single hash_code. For user-defined types it
609609 namespace hashing {
610610 namespace detail {
611611
612 /// \brief Helper to hash the value of a single integer.
612 /// Helper to hash the value of a single integer.
613613 ///
614614 /// Overloads for smaller integer types are not provided to ensure consistent
615615 /// behavior in the presence of integral promotions. Essentially,
156156 (Vector.begin() + Pos->second);
157157 }
158158
159 /// \brief Remove the last element from the vector.
159 /// Remove the last element from the vector.
160160 void pop_back() {
161161 typename MapType::iterator Pos = Map.find(Vector.back().first);
162162 Map.erase(Pos);
163163 Vector.pop_back();
164164 }
165165
166 /// \brief Remove the element given by Iterator.
166 /// Remove the element given by Iterator.
167167 ///
168168 /// Returns an iterator to the element following the one which was removed,
169169 /// which may be end().
186186 return Next;
187187 }
188188
189 /// \brief Remove all elements with the key value Key.
189 /// Remove all elements with the key value Key.
190190 ///
191191 /// Returns the number of elements removed.
192192 size_type erase(const KeyT &Key) {
197197 return 1;
198198 }
199199
200 /// \brief Remove the elements that match the predicate.
200 /// Remove the elements that match the predicate.
201201 ///
202202 /// Erase all elements that match \c Pred in a single pass. Takes linear
203203 /// time.
226226 Vector.erase(O, Vector.end());
227227 }
228228
229 /// \brief A MapVector that performs no allocations if smaller than a certain
229 /// A MapVector that performs no allocations if smaller than a certain
230230 /// size.
231231 template
232232 struct SmallMapVector
1616 #define LLVM_ADT_NONE_H
1717
1818 namespace llvm {
19 /// \brief A simple null object to allow implicit construction of Optional
19 /// A simple null object to allow implicit construction of Optional
2020 /// and similar types without having to spell out the specialization's name.
2121 // (constant value 1 in an attempt to workaround MSVC build issue... )
2222 enum class NoneType { None = 1 };
6464 }
6565 };
6666
67 /// \brief Store a vector of values using a specific number of bits for each
67 /// Store a vector of values using a specific number of bits for each
6868 /// value. Both signed and unsigned types can be used, e.g
6969 /// @code
7070 /// PackedVector vec;
3232
3333 namespace llvm {
3434
35 /// \brief Enumerate the SCCs of a directed graph in reverse topological order
35 /// Enumerate the SCCs of a directed graph in reverse topological order
3636 /// of the SCC DAG.
3737 ///
3838 /// This is implemented using Tarjan's DFS algorithm using an internal stack to
103103 }
104104 static scc_iterator end(const GraphT &) { return scc_iterator(); }
105105
106 /// \brief Direct loop termination test which is more efficient than
106 /// Direct loop termination test which is more efficient than
107107 /// comparison with \c end().
108108 bool isAtEnd() const {
109109 assert(!CurrentSCC.empty() || VisitStack.empty());
124124 return CurrentSCC;
125125 }
126126
127 /// \brief Test if the current SCC has a loop.
127 /// Test if the current SCC has a loop.
128128 ///
129129 /// If the SCC has more than one node, this is trivially true. If not, it may
130130 /// still contain a loop if the node has an edge back to itself.
221221 return false;
222222 }
223223
224 /// \brief Construct the begin iterator for a deduced graph type T.
224 /// Construct the begin iterator for a deduced graph type T.
225225 template scc_iterator scc_begin(const T &G) {
226226 return scc_iterator::begin(G);
227227 }
228228
229 /// \brief Construct the end iterator for a deduced graph type T.
229 /// Construct the end iterator for a deduced graph type T.
230230 template scc_iterator scc_end(const T &G) {
231231 return scc_iterator::end(G);
232232 }
710710 // Extra additions to
711711 //===----------------------------------------------------------------------===//
712712
713 /// \brief Function object to check whether the first component of a std::pair
713 /// Function object to check whether the first component of a std::pair
714714 /// compares less than the first component of another std::pair.
715715 struct less_first {
716716 template bool operator()(const T &lhs, const T &rhs) const {
718718 }
719719 };
720720
721 /// \brief Function object to check whether the second component of a std::pair
721 /// Function object to check whether the second component of a std::pair
722722 /// compares less than the second component of another std::pair.
723723 struct less_second {
724724 template bool operator()(const T &lhs, const T &rhs) const {
728728
729729 // A subset of N3658. More stuff can be added as-needed.
730730
731 /// \brief Represents a compile-time sequence of integers.
731 /// Represents a compile-time sequence of integers.
732732 template struct integer_sequence {
733733 using value_type = T;
734734
735735 static constexpr size_t size() { return sizeof...(I); }
736736 };
737737
738 /// \brief Alias for the common case of a sequence of size_ts.
738 /// Alias for the common case of a sequence of size_ts.
739739 template
740740 struct index_sequence : integer_sequence {};
741741
744744 template
745745 struct build_index_impl<0, I...> : index_sequence {};
746746
747 /// \brief Creates a compile-time integer sequence for a parameter pack.
747 /// Creates a compile-time integer sequence for a parameter pack.
748748 template
749749 struct index_sequence_for : build_index_impl {};
750750
753753 template struct rank : rank {};
754754 template <> struct rank<0> {};
755755
756 /// \brief traits class for checking whether type T is one of any of the given
756 /// traits class for checking whether type T is one of any of the given
757757 /// types in the variadic list.
758758 template struct is_one_of {
759759 static const bool value = false;
765765 std::is_same::value || is_one_of::value;
766766 };
767767
768 /// \brief traits class for checking whether type T is a base class for all
768 /// traits class for checking whether type T is a base class for all
769769 /// the given types in the variadic list.
770770 template struct are_base_of {
771771 static const bool value = true;
10041004 return std::lower_bound(adl_begin(Range), adl_end(Range), I);
10051005 }
10061006
1007 /// \brief Given a range of type R, iterate the entire range and return a
1007 /// Given a range of type R, iterate the entire range and return a
10081008 /// SmallVector with elements of the vector. This is useful, for example,
10091009 /// when you want to iterate a range and then sort the results.
10101010 template
10311031
10321032 // Implement make_unique according to N3656.
10331033
1034 /// \brief Constructs a `new T()` with the given args and returns a
1034 /// Constructs a `new T()` with the given args and returns a
10351035 /// `unique_ptr` which owns the object.
10361036 ///
10371037 /// Example:
10441044 return std::unique_ptr(new T(std::forward(args)...));
10451045 }
10461046
1047 /// \brief Constructs a `new T[n]` with the given args and returns a
1047 /// Constructs a `new T[n]` with the given args and returns a
10481048 /// `unique_ptr` which owns the object.
10491049 ///
10501050 /// \param n size of the new array.
3030
3131 namespace llvm {
3232
33 /// \brief A vector that has set insertion semantics.
33 /// A vector that has set insertion semantics.
3434 ///
3535 /// This adapter class provides a way to keep a set of things that also has the
3636 /// property of a deterministic iteration order. The order of iteration is the
5151 using const_reverse_iterator = typename vector_type::const_reverse_iterator;
5252 using size_type = typename vector_type::size_type;
5353
54 /// \brief Construct an empty SetVector
54 /// Construct an empty SetVector
5555 SetVector() = default;
5656
57 /// \brief Initialize a SetVector with a range of elements
57 /// Initialize a SetVector with a range of elements
5858 template
5959 SetVector(It Start, It End) {
6060 insert(Start, End);
6868 return std::move(vector_);
6969 }
7070
71 /// \brief Determine if the SetVector is empty or not.
71 /// Determine if the SetVector is empty or not.
7272 bool empty() const {
7373 return vector_.empty();
7474 }
7575
76 /// \brief Determine the number of elements in the SetVector.
76 /// Determine the number of elements in the SetVector.
7777 size_type size() const {
7878 return vector_.size();
7979 }
8080
81 /// \brief Get an iterator to the beginning of the SetVector.
81 /// Get an iterator to the beginning of the SetVector.
8282 iterator begin() {
8383 return vector_.begin();
8484 }
8585
86 /// \brief Get a const_iterator to the beginning of the SetVector.
86 /// Get a const_iterator to the beginning of the SetVector.
8787 const_iterator begin() const {
8888 return vector_.begin();
8989 }
9090
91 /// \brief Get an iterator to the end of the SetVector.
91 /// Get an iterator to the end of the SetVector.
9292 iterator end() {
9393 return vector_.end();
9494 }
9595
96 /// \brief Get a const_iterator to the end of the SetVector.
96 /// Get a const_iterator to the end of the SetVector.
9797 const_iterator end() const {
9898 return vector_.end();
9999 }
100100
101 /// \brief Get an reverse_iterator to the end of the SetVector.
101 /// Get an reverse_iterator to the end of the SetVector.
102102 reverse_iterator rbegin() {
103103 return vector_.rbegin();
104104 }
105105
106 /// \brief Get a const_reverse_iterator to the end of the SetVector.
106 /// Get a const_reverse_iterator to the end of the SetVector.
107107 const_reverse_iterator rbegin() const {
108108 return vector_.rbegin();
109109 }
110110
111 /// \brief Get a reverse_iterator to the beginning of the SetVector.
111 /// Get a reverse_iterator to the beginning of the SetVector.
112112 reverse_iterator rend() {
113113 return vector_.rend();
114114 }
115115
116 /// \brief Get a const_reverse_iterator to the beginning of the SetVector.
116 /// Get a const_reverse_iterator to the beginning of the SetVector.
117117 const_reverse_iterator rend() const {
118118 return vector_.rend();
119119 }
120120
121 /// \brief Return the first element of the SetVector.
121 /// Return the first element of the SetVector.
122122 const T &front() const {
123123 assert(!empty() && "Cannot call front() on empty SetVector!");
124124 return vector_.front();
125125 }
126126
127 /// \brief Return the last element of the SetVector.
127 /// Return the last element of the SetVector.
128128 const T &back() const {
129129 assert(!empty() && "Cannot call back() on empty SetVector!");
130130 return vector_.back();
131131 }
132132
133 /// \brief Index into the SetVector.
133 /// Index into the SetVector.
134134 const_reference operator[](size_type n) const {
135135 assert(n < vector_.size() && "SetVector access out of range!");
136136 return vector_[n];
137137 }
138138
139 /// \brief Insert a new element into the SetVector.
139 /// Insert a new element into the SetVector.
140140 /// \returns true if the element was inserted into the SetVector.
141141 bool insert(const value_type &X) {
142142 bool result = set_.insert(X).second;
145145 return result;
146146 }
147147
148 /// \brief Insert a range of elements into the SetVector.
148 /// Insert a range of elements into the SetVector.
149149 template
150150 void insert(It Start, It End) {
151151 for (; Start != End; ++Start)
153153 vector_.push_back(*Start);
154154 }
155155
156 /// \brief Remove an item from the set vector.
156 /// Remove an item from the set vector.
157157 bool remove(const value_type& X) {
158158 if (set_.erase(X)) {
159159 typename vector_type::iterator I = find(vector_, X);
182182 return vector_.erase(NI);
183183 }
184184
185 /// \brief Remove items from the set vector based on a predicate function.
185 /// Remove items from the set vector based on a predicate function.
186186 ///
187187 /// This is intended to be equivalent to the following code, if we could
188188 /// write it:
205205 return true;
206206 }
207207
208 /// \brief Count the number of elements of a given key in the SetVector.
208 /// Count the number of elements of a given key in the SetVector.
209209 /// \returns 0 if the element is not in the SetVector, 1 if it is.
210210 size_type count(const key_type &key) const {
211211 return set_.count(key);
212212 }
213213
214 /// \brief Completely clear the SetVector
214 /// Completely clear the SetVector
215215 void clear() {
216216 set_.clear();
217217 vector_.clear();
218218 }
219219
220 /// \brief Remove the last element of the SetVector.
220 /// Remove the last element of the SetVector.
221221 void pop_back() {
222222 assert(!empty() && "Cannot remove an element from an empty SetVector!");
223223 set_.erase(back());
238238 return vector_ != that.vector_;
239239 }
240240
241 /// \brief Compute This := This u S, return whether 'This' changed.
241 /// Compute This := This u S, return whether 'This' changed.
242242 /// TODO: We should be able to use set_union from SetOperations.h, but
243243 /// SetVector interface is inconsistent with DenseSet.
244244 template
253253 return Changed;
254254 }
255255
256 /// \brief Compute This := This - B
256 /// Compute This := This - B
257257 /// TODO: We should be able to use set_subtract from SetOperations.h, but
258258 /// SetVector interface is inconsistent with DenseSet.
259259 template
264264 }
265265
266266 private:
267 /// \brief A wrapper predicate designed for use with std::remove_if.
267 /// A wrapper predicate designed for use with std::remove_if.
268268 ///
269269 /// This predicate wraps a predicate suitable for use with std::remove_if to
270270 /// call set_.erase(x) on each element which is slated for removal.
291291 vector_type vector_; ///< The vector.
292292 };
293293
294 /// \brief A SetVector that performs no allocations if smaller than
294 /// A SetVector that performs no allocations if smaller than
295295 /// a certain size.
296296 template
297297 class SmallSetVector
299299 public:
300300 SmallSetVector() = default;
301301
302 /// \brief Initialize a SmallSetVector with a range of elements
302 /// Initialize a SmallSetVector with a range of elements
303303 template
304304 SmallSetVector(It Start, It End) {
305305 this->insert(Start, End);
334334 enum { Val = RoundUpToPowerOfTwoH::Val };
335335 };
336336
337 /// \brief A templated base class for \c SmallPtrSet which provides the
337 /// A templated base class for \c SmallPtrSet which provides the
338338 /// typesafe interface that is common across all small sizes.
339339 ///
340340 /// This is particularly useful for passing around between interface boundaries
168168 #define STATISTIC(VARNAME, DESC) \
169169 static llvm::Statistic VARNAME = {DEBUG_TYPE, #VARNAME, DESC, {0}, {false}}
170170
171 /// \brief Enable the collection and printing of statistics.
171 /// Enable the collection and printing of statistics.
172172 void EnableStatistics(bool PrintOnExit = true);
173173
174 /// \brief Check if statistics are enabled.
174 /// Check if statistics are enabled.
175175 bool AreStatisticsEnabled();
176176
177 /// \brief Return a file stream to print our output on.
177 /// Return a file stream to print our output on.
178178 std::unique_ptr CreateInfoOutputFile();
179179
180 /// \brief Print statistics to the file returned by CreateInfoOutputFile().
180 /// Print statistics to the file returned by CreateInfoOutputFile().
181181 void PrintStatistics();
182182
183 /// \brief Print statistics to the given output stream.
183 /// Print statistics to the given output stream.
184184 void PrintStatistics(raw_ostream &OS);
185185
186186 /// Print statistics in JSON format. This does include all global timers (\see
189189 /// PrintStatisticsJSON().
190190 void PrintStatisticsJSON(raw_ostream &OS);
191191
192 /// \brief Get the statistics. This can be used to look up the value of
192 /// Get the statistics. This can be used to look up the value of
193193 /// statistics without needing to parse JSON.
194194 ///
195195 /// This function does not prevent statistics being updated by other threads
198198 /// completes.
199199 const std::vector> GetStatistics();
200200
201 /// \brief Reset the statistics. This can be used to zero and de-register the
201 /// Reset the statistics. This can be used to zero and de-register the
202202 /// statistics in order to measure a compilation.
203203 ///
204204 /// When this function begins to call destructors prior to returning, all
156156 return Output;
157157 }
158158
159 /// \brief Convert the string \p S to an integer of the specified type using
159 /// Convert the string \p S to an integer of the specified type using
160160 /// the radix \p Base. If \p Base is 0, auto-detects the radix.
161161 /// Returns true if the number was successfully converted, false otherwise.
162162 template bool to_integer(StringRef S, N &Num, unsigned Base = 0) {
200200 LLVM_NODISCARD
201201 int compare_numeric(StringRef RHS) const;
202202
203 /// \brief Determine the edit distance between this string and another
203 /// Determine the edit distance between this string and another
204204 /// string.
205205 ///
206206 /// \param Other the string to compare this string against.
911911
912912 /// @}
913913
914 /// \brief Compute a hash_code for a StringRef.
914 /// Compute a hash_code for a StringRef.
915915 LLVM_NODISCARD
916916 hash_code hash_value(StringRef S);
917917
1919
2020 namespace llvm {
2121
22 /// \brief A switch()-like statement whose cases are string literals.
22 /// A switch()-like statement whose cases are string literals.
2323 ///
2424 /// The StringSwitch class is a simple form of a switch() statement that
2525 /// determines whether the given string matches one of the given string
4040 /// \endcode
4141 template
4242 class StringSwitch {
43 /// \brief The string we are matching.
43 /// The string we are matching.
4444 const StringRef Str;
4545
46 /// \brief The pointer to the result of this switch statement, once known,
46 /// The pointer to the result of this switch statement, once known,
4747 /// null before that.
4848 Optional Result;
4949
7171 return Vector[ID - 1];
7272 }
7373
74 /// \brief Return an iterator to the start of the vector.
74 /// Return an iterator to the start of the vector.
7575 iterator begin() { return Vector.begin(); }
7676
77 /// \brief Return an iterator to the start of the vector.
77 /// Return an iterator to the start of the vector.
7878 const_iterator begin() const { return Vector.begin(); }
7979
80 /// \brief Return an iterator to the end of the vector.
80 /// Return an iterator to the end of the vector.
8181 iterator end() { return Vector.end(); }
8282
83 /// \brief Return an iterator to the end of the vector.
83 /// Return an iterator to the end of the vector.
8484 const_iterator end() const { return Vector.end(); }
8585
8686 /// size - Returns the number of entries in the vector.
5252 #define LLVM_COMMA_JOIN31(x) LLVM_COMMA_JOIN30(x), x ## 30
5353 #define LLVM_COMMA_JOIN32(x) LLVM_COMMA_JOIN31(x), x ## 31
5454
55 /// \brief Class which can simulate a type-safe variadic function.
55 /// Class which can simulate a type-safe variadic function.
5656 ///
5757 /// The VariadicFunction class template makes it easy to define
5858 /// type-safe variadic functions where all arguments have the same
2121
2222 namespace llvm {
2323
24 /// \brief Determine the edit distance between two sequences.
24 /// Determine the edit distance between two sequences.
2525 ///
2626 /// \param FromArray the first sequence to compare.
2727 ///
355355
356356 using base_list_type::sort;
357357
358 /// \brief Get the previous node, or \c nullptr for the list head.
358 /// Get the previous node, or \c nullptr for the list head.
359359 pointer getPrevNode(reference N) const {
360360 auto I = N.getIterator();
361361 if (I == begin())
362362 return nullptr;
363363 return &*std::prev(I);
364364 }
365 /// \brief Get the previous node, or \c nullptr for the list head.
365 /// Get the previous node, or \c nullptr for the list head.
366366 const_pointer getPrevNode(const_reference N) const {
367367 return getPrevNode(const_cast(N));
368368 }
369369
370 /// \brief Get the next node, or \c nullptr for the list tail.
370 /// Get the next node, or \c nullptr for the list tail.
371371 pointer getNextNode(reference N) const {
372372 auto Next = std::next(N.getIterator());
373373 if (Next == end())
374374 return nullptr;
375375 return &*Next;
376376 }
377 /// \brief Get the next node, or \c nullptr for the list tail.
377 /// Get the next node, or \c nullptr for the list tail.
378378 const_pointer getNextNode(const_reference N) const {
379379 return getNextNode(const_cast(N));
380380 }
270270 public:
271271 /// @name Adjacent Node Accessors
272272 /// @{
273 /// \brief Get the previous node, or \c nullptr for the list head.
273 /// Get the previous node, or \c nullptr for the list head.
274274 NodeTy *getPrevNode() {
275275 // Should be separated to a reused function, but then we couldn't use auto
276276 // (and would need the type of the list).
279279 return List.getPrevNode(*static_cast(this));
280280 }
281281
282 /// \brief Get the previous node, or \c nullptr for the list head.
282 /// Get the previous node, or \c nullptr for the list head.
283283 const NodeTy *getPrevNode() const {
284284 return const_cast(this)->getPrevNode();
285285 }
286286
287 /// \brief Get the next node, or \c nullptr for the list tail.
287 /// Get the next node, or \c nullptr for the list tail.
288288 NodeTy *getNextNode() {
289289 // Should be separated to a reused function, but then we couldn't use auto
290290 // (and would need the type of the list).
293293 return List.getNextNode(*static_cast(this));
294294 }
295295
296 /// \brief Get the next node, or \c nullptr for the list tail.
296 /// Get the next node, or \c nullptr for the list tail.
297297 const NodeTy *getNextNode() const {
298298 return const_cast(this)->getNextNode();
299299 }
1818
1919 namespace llvm {
2020
21 /// \brief CRTP base class which implements the entire standard iterator facade
21 /// CRTP base class which implements the entire standard iterator facade
2222 /// in terms of a minimal subset of the interface.
2323 ///
2424 /// Use this when it is reasonable to implement most of the iterator
182182 }
183183 };
184184
185 /// \brief CRTP base class for adapting an iterator to a different type.
185 /// CRTP base class for adapting an iterator to a different type.
186186 ///
187187 /// This class can be used through CRTP to adapt one iterator into another.
188188 /// Typically this is done through providing in the derived class a custom \c
273273 ReferenceT operator*() const { return *I; }
274274 };
275275
276 /// \brief An iterator type that allows iterating over the pointees via some
276 /// An iterator type that allows iterating over the pointees via some
277277 /// other iterator.
278278 ///
279279 /// The typical usage of this is to expose a type that iterates over Ts, but
2323
2424 namespace llvm {
2525
26 /// \brief A range adaptor for a pair of iterators.
26 /// A range adaptor for a pair of iterators.
2727 ///
2828 /// This just wraps two iterators into a range-compatible interface. Nothing
2929 /// fancy at all.
4646 IteratorT end() const { return end_iterator; }
4747 };
4848
49 /// \brief Convenience function for iterating over sub-ranges.
49 /// Convenience function for iterating over sub-ranges.
5050 ///
5151 /// This provides a bit of syntactic sugar to make using sub-ranges
5252 /// in for loops a bit easier. Analogous to std::make_pair().
658658 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
659659 ModRefInfo getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2);
660660
661 /// \brief Return information about whether a particular call site modifies
661 /// Return information about whether a particular call site modifies
662662 /// or reads the specified memory location \p MemLoc before instruction \p I
663663 /// in a BasicBlock. An ordered basic block \p OBB can be used to speed up
664664 /// instruction ordering queries inside the BasicBlock containing \p I.
668668 const MemoryLocation &MemLoc, DominatorTree *DT,
669669 OrderedBasicBlock *OBB = nullptr);
670670
671 /// \brief A convenience wrapper to synthesize a memory location.
671 /// A convenience wrapper to synthesize a memory location.
672672 ModRefInfo callCapturesBefore(const Instruction *I, const Value *P,
673673 uint64_t Size, DominatorTree *DT,
674674 OrderedBasicBlock *OBB = nullptr) {
5555 }
5656 ~AAEvaluator();
5757
58 /// \brief Run the pass over the function.
58 /// Run the pass over the function.
5959 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
6060
6161 private:
3131 class raw_ostream;
3232 class Value;
3333
34 /// \brief A cache of \@llvm.assume calls within a function.
34 /// A cache of \@llvm.assume calls within a function.
3535 ///
3636 /// This cache provides fast lookup of assumptions within a function by caching
3737 /// them and amortizing the cost of scanning for them across all queries. Passes
3939 /// register any new \@llvm.assume calls that they create. Deletions of
4040 /// \@llvm.assume calls do not require special handling.
4141 class AssumptionCache {
42 /// \brief The function for which this cache is handling assumptions.
42 /// The function for which this cache is handling assumptions.
4343 ///
4444 /// We track this to lazily populate our assumptions.
4545 Function &F;
4646
47 /// \brief Vector of weak value handles to calls of the \@llvm.assume
47 /// Vector of weak value handles to calls of the \@llvm.assume
4848 /// intrinsic.
4949 SmallVector AssumeHandles;
5050
6363
6464 friend AffectedValueCallbackVH;
6565
66 /// \brief A map of values about which an assumption might be providing
66 /// A map of values about which an assumption might be providing
6767 /// information to the relevant set of assumptions.
6868 using AffectedValuesMap =
6969 DenseMap,
7676 /// Copy affected values in the cache for OV to be affected values for NV.
7777 void copyAffectedValuesInCache(Value *OV, Value *NV);
7878
79 /// \brief Flag tracking whether we have scanned the function yet.
79 /// Flag tracking whether we have scanned the function yet.
8080 ///
8181 /// We want to be as lazy about this as possible, and so we scan the function
8282 /// at the last moment.
8383 bool Scanned = false;
8484
85 /// \brief Scan the function for assumptions and add them to the cache.
85 /// Scan the function for assumptions and add them to the cache.
8686 void scanFunction();
8787
8888 public:
89 /// \brief Construct an AssumptionCache from a function by scanning all of
89 /// Construct an AssumptionCache from a function by scanning all of
9090 /// its instructions.
9191 AssumptionCache(Function &F) : F(F) {}
9292
9797 return false;
9898 }
9999
100 /// \brief Add an \@llvm.assume intrinsic to this function's cache.
100 /// Add an \@llvm.assume intrinsic to this function's cache.
101101 ///
102102 /// The call passed in must be an instruction within this function and must
103103 /// not already be in the cache.
104104 void registerAssumption(CallInst *CI);
105105
106 /// \brief Update the cache of values being affected by this assumption (i.e.
106 /// Update the cache of values being affected by this assumption (i.e.
107107 /// the values about which this assumption provides information).
108108 void updateAffectedValues(CallInst *CI);
109109
110 /// \brief Clear the cache of \@llvm.assume intrinsics for a function.
110 /// Clear the cache of \@llvm.assume intrinsics for a function.
111111 ///
112112 /// It will be re-scanned the next time it is requested.
113113 void clear() {
116116 Scanned = false;
117117 }
118118
119 /// \brief Access the list of assumption handles currently tracked for this
119 /// Access the list of assumption handles currently tracked for this
120120 /// function.
121121 ///
122122 /// Note that these produce weak handles that may be null. The caller must
130130 return AssumeHandles;
131131 }
132132
133 /// \brief Access the list of assumptions which affect this value.
133 /// Access the list of assumptions which affect this value.
134134 MutableArrayRef assumptionsFor(const Value *V) {
135135 if (!Scanned)
136136 scanFunction();
143143 }
144144 };
145145
146 /// \brief A function analysis which provides an \c AssumptionCache.
146 /// A function analysis which provides an \c AssumptionCache.
147147 ///
148148 /// This analysis is intended for use with the new pass manager and will vend
149149 /// assumption caches for a given function.
160160 }
161161 };
162162
163 /// \brief Printer pass for the \c AssumptionAnalysis results.
163 /// Printer pass for the \c AssumptionAnalysis results.
164164 class AssumptionPrinterPass : public PassInfoMixin {
165165 raw_ostream &OS;
166166
170170 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
171171 };
172172
173 /// \brief An immutable pass that tracks lazily created \c AssumptionCache
173 /// An immutable pass that tracks lazily created \c AssumptionCache
174174 /// objects.
175175 ///
176176 /// This is essentially a workaround for the legacy pass manager's weaknesses
202202 FunctionCallsMap AssumptionCaches;
203203
204204 public:
205 /// \brief Get the cached assumptions for a function.
205 /// Get the cached assumptions for a function.
206206 ///
207207 /// If no assumptions are cached, this will scan the function. Otherwise, the
208208 /// existing cache will be returned.
172172 const DecomposedGEP &DecompGEP, const DecomposedGEP &DecompObject,
173173 uint64_t ObjectAccessSize);
174174
175 /// \brief A Heuristic for aliasGEP that searches for a constant offset
175 /// A Heuristic for aliasGEP that searches for a constant offset
176176 /// between the variables.
177177 ///
178178 /// GetLinearExpression has some limitations, as generally zext(%x + 1)
6464 /// floating points.
6565 BlockFrequency getBlockFreq(const BasicBlock *BB) const;
6666
67 /// \brief Returns the estimated profile count of \p BB.
67 /// Returns the estimated profile count of \p BB.
6868 /// This computes the relative block frequency of \p BB and multiplies it by
6969 /// the enclosing function's count (if available) and returns the value.
7070 Optional getBlockProfileCount(const BasicBlock *BB) const;
7171
72 /// \brief Returns the estimated profile count of \p Freq.
72 /// Returns the estimated profile count of \p Freq.
7373 /// This uses the frequency \p Freq and multiplies it by
7474 /// the enclosing function's count (if available) and returns the value.
7575 Optional getProfileCountFromFreq(uint64_t Freq) const;
7676
77 /// \brief Returns true if \p BB is an irreducible loop header
77 /// Returns true if \p BB is an irreducible loop header
7878 /// block. Otherwise false.
7979 bool isIrrLoopHeader(const BasicBlock *BB);
8080
104104 void print(raw_ostream &OS) const;
105105 };
106106
107 /// \brief Analysis pass which computes \c BlockFrequencyInfo.
107 /// Analysis pass which computes \c BlockFrequencyInfo.
108108 class BlockFrequencyAnalysis
109109 : public AnalysisInfoMixin {
110110 friend AnalysisInfoMixin;
112112 static AnalysisKey Key;
113113
114114 public:
115 /// \brief Provide the result type for this analysis pass.
115 /// Provide the result type for this analysis pass.
116116 using Result = BlockFrequencyInfo;
117117
118 /// \brief Run the analysis pass over a function and produce BFI.
118 /// Run the analysis pass over a function and produce BFI.
119119 Result run(Function &F, FunctionAnalysisManager &AM);
120120 };
121121
122 /// \brief Printer pass for the \c BlockFrequencyInfo results.
122 /// Printer pass for the \c BlockFrequencyInfo results.
123123 class BlockFrequencyPrinterPass
124124 : public PassInfoMixin {
125125 raw_ostream &OS;
130130 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
131131 };
132132
133 /// \brief Legacy analysis pass which computes \c BlockFrequencyInfo.
133 /// Legacy analysis pass which computes \c BlockFrequencyInfo.
134134 class BlockFrequencyInfoWrapperPass : public FunctionPass {
135135 BlockFrequencyInfo BFI;
136136
6565 // This is part of a workaround for a GCC 4.7 crash on lambdas.
6666 template struct BlockEdgesAdder;
6767
68 /// \brief Mass of a block.
68 /// Mass of a block.
6969 ///
7070 /// This class implements a sort of fixed-point fraction always between 0.0 and
7171 /// 1.0. getMass() == std::numeric_limits::max() indicates a value of
9999
100100 bool operator!() const { return isEmpty(); }
101101
102 /// \brief Add another mass.
102 /// Add another mass.
103103 ///
104104 /// Adds another mass, saturating at \a isFull() rather than overflowing.
105105 BlockMass &operator+=(BlockMass X) {
108108 return *this;
109109 }
110110
111 /// \brief Subtract another mass.
111 /// Subtract another mass.
112112 ///
113113 /// Subtracts another mass, saturating at \a isEmpty() rather than
114114 /// undeflowing.
130130 bool operator<(BlockMass X) const { return Mass < X.Mass; }
131131 bool operator>(BlockMass X) const { return Mass > X.Mass; }
132132
133 /// \brief Convert to scaled number.
133 /// Convert to scaled number.
134134 ///
135135 /// Convert to \a ScaledNumber. \a isFull() gives 1.0, while \a isEmpty()
136136 /// gives slightly above 0.0.
163163 static const bool value = true;
164164 };
165165
166 /// \brief Base class for BlockFrequencyInfoImpl
166 /// Base class for BlockFrequencyInfoImpl
167167 ///
168168 /// BlockFrequencyInfoImplBase has supporting data structures and some
169169 /// algorithms for BlockFrequencyInfoImplBase. Only algorithms that depend on
176176 using Scaled64 = ScaledNumber;
177177 using BlockMass = bfi_detail::BlockMass;
178178
179 /// \brief Representative of a block.
179 /// Representative of a block.
180180 ///
181181 /// This is a simple wrapper around an index into the reverse-post-order
182182 /// traversal of the blocks.
205205 }
206206 };
207207
208 /// \brief Stats about a block itself.
208 /// Stats about a block itself.
209209 struct FrequencyData {
210210 Scaled64 Scaled;
211211 uint64_t Integer;
212212 };
213213
214 /// \brief Data about a loop.
214 /// Data about a loop.
215215 ///
216216 /// Contains the data necessary to represent a loop as a pseudo-node once it's
217217 /// packaged.
269269 }
270270 };
271271
272 /// \brief Index of loop information.
272 /// Index of loop information.
273273 struct WorkingData {
274274 BlockNode Node; ///< This node.
275275 LoopData *Loop = nullptr; ///< The loop this block is inside.
292292 return Loop->Parent->Parent;
293293 }
294294
295 /// \brief Resolve a node to its representative.
295 /// Resolve a node to its representative.
296296 ///
297297 /// Get the node currently representing Node, which could be a containing
298298 /// loop.
319319 return L;
320320 }
321321
322 /// \brief Get the appropriate mass for a node.
322 /// Get the appropriate mass for a node.
323323 ///
324324 /// Get appropriate mass for Node. If Node is a loop-header (whose loop
325325 /// has been packaged), returns the mass of its pseudo-node. If it's a
332332 return Loop->Parent->Mass;
333333 }
334334
335 /// \brief Has ContainingLoop been packaged up?
335 /// Has ContainingLoop been packaged up?
336336 bool isPackaged() const { return getResolvedNode() != Node; }
337337
338 /// \brief Has Loop been packaged up?
338 /// Has Loop been packaged up?
339339 bool isAPackage() const { return isLoopHeader() && Loop->IsPackaged; }
340340
341 /// \brief Has Loop been packaged up twice?
341 /// Has Loop been packaged up twice?
342342 bool isADoublePackage() const {
343343 return isDoubleLoopHeader() && Loop->Parent->IsPackaged;
344344 }
345345 };
346346
347 /// \brief Unscaled probability weight.
347 /// Unscaled probability weight.
348348 ///
349349 /// Probability weight for an edge in the graph (including the
350350 /// successor/target node).
368368 : Type(Type), TargetNode(TargetNode), Amount(Amount) {}
369369 };
370370
371 /// \brief Distribution of unscaled probability weight.
371 /// Distribution of unscaled probability weight.
372372 ///
373373 /// Distribution of unscaled probability weight to a set of successors.
374374 ///
397397 add(Node, Amount, Weight::Backedge);
398398 }
399399
400 /// \brief Normalize the distribution.
400 /// Normalize the distribution.
401401 ///
402402 /// Combines multiple edges to the same \a Weight::TargetNode and scales
403403 /// down so that \a Total fits into 32-bits.
412412 void add(const BlockNode &Node, uint64_t Amount, Weight::DistType Type);
413413 };
414414
415 /// \brief Data about each block. This is used downstream.
415 /// Data about each block. This is used downstream.
416416 std::vector Freqs;
417417
418 /// \brief Whether each block is an irreducible loop header.
418 /// Whether each block is an irreducible loop header.
419419 /// This is used downstream.
420420 SparseBitVector<> IsIrrLoopHeader;
421421
422 /// \brief Loop data: see initializeLoops().
422 /// Loop data: see initializeLoops().
423423 std::vector Working;
424424
425 /// \brief Indexed information about loops.
425 /// Indexed information about loops.
426426 std::list Loops;
427427
428 /// \brief Virtual destructor.
428 /// Virtual destructor.
429429 ///
430430 /// Need a virtual destructor to mask the compiler warning about
431431 /// getBlockName().
432432 virtual ~BlockFrequencyInfoImplBase() = default;
433433
434 /// \brief Add all edges out of a packaged loop to the distribution.
434 /// Add all edges out of a packaged loop to the distribution.
435435 ///
436436 /// Adds all edges from LocalLoopHead to Dist. Calls addToDist() to add each
437437 /// successor edge.
440440 bool addLoopSuccessorsToDist(const LoopData *OuterLoop, LoopData &Loop,
441441 Distribution &Dist);
442442
443 /// \brief Add an edge to the distribution.
443 /// Add an edge to the distribution.
444444 ///
445445 /// Adds an edge to Succ to Dist. If \c LoopHead.isValid(), then whether the
446446 /// edge is local/exit/backedge is in the context of LoopHead. Otherwise,
456456 return *Working[Head.Index].Loop;
457457 }
458458
459 /// \brief Analyze irreducible SCCs.
459 /// Analyze irreducible SCCs.
460460 ///
461461 /// Separate irreducible SCCs from \c G, which is an explict graph of \c
462462 /// OuterLoop (or the top-level function, if \c OuterLoop is \c nullptr).
467467 analyzeIrreducible(const bfi_detail::IrreducibleGraph &G, LoopData *OuterLoop,
468468 std::list::iterator Insert);
469469
470 /// \brief Update a loop after packaging irreducible SCCs inside of it.
470 /// Update a loop after packaging irreducible SCCs inside of it.
471471 ///
472472 /// Update \c OuterLoop. Before finding irreducible control flow, it was
473473 /// partway through \a computeMassInLoop(), so \a LoopData::Exits and \a
475475 /// up need to be removed from \a OuterLoop::Nodes.
476476 void updateLoopWithIrreducible(LoopData &OuterLoop);
477477
478 /// \brief Distribute mass according to a distribution.
478 /// Distribute mass according to a distribution.
479479 ///
480480 /// Distributes the mass in Source according to Dist. If LoopHead.isValid(),
481481 /// backedges and exits are stored in its entry in Loops.
484484 void distributeMass(const BlockNode &Source, LoopData *OuterLoop,
485485 Distribution &Dist);
486486
487 /// \brief Compute the loop scale for a loop.
487 /// Compute the loop scale for a loop.
488488 void computeLoopScale(LoopData &Loop);
489489
490490 /// Adjust the mass of all headers in an irreducible loop.
499499
500500 void distributeIrrLoopHeaderMass(Distribution &Dist);
501501
502 /// \brief Package up a loop.
502 /// Package up a loop.
503503 void packageLoop(LoopData &Loop);
504504
505 /// \brief Unwrap loops.
505 /// Unwrap loops.
506506 void unwrapLoops();
507507
508 /// \brief Finalize frequency metrics.
508 /// Finalize frequency metrics.
509509 ///
510510 /// Calculates final frequencies and cleans up no-longer-needed data
511511 /// structures.
512512 void finalizeMetrics();
513513
514 /// \brief Clear all memory.
514 /// Clear all memory.
515515 void clear();
516516
517517 virtual std::string getBlockName(const BlockNode &Node) const;
559559 using LoopInfoT = MachineLoopInfo;
560560 };
561561
562 /// \brief Get the name of a MachineBasicBlock.
562 /// Get the name of a MachineBasicBlock.
563563 ///
564564 /// Get the name of a MachineBasicBlock. It's templated so that including from
565565 /// CodeGen is unnecessary (that would be a layering issue).
573573 return (MachineName + "[" + BB->getName() + "]").str();
574574 return MachineName.str();
575575 }
576 /// \brief Get the name of a BasicBlock.
576 /// Get the name of a BasicBlock.
577577 template <> inline std::string getBlockName(const BasicBlock *BB) {
578578 assert(BB && "Unexpected nullptr");
579579 return BB->getName().str();
580580 }
581581
582 /// \brief Graph of irreducible control flow.
582 /// Graph of irreducible control flow.
583583 ///
584584 /// This graph is used for determining the SCCs in a loop (or top-level
585585 /// function) that has irreducible control flow.
618618 std::vector Nodes;
619619 SmallDenseMap Lookup;
620620
621 /// \brief Construct an explicit graph containing irreducible control flow.
621 /// Construct an explicit graph containing irreducible control flow.
622622 ///
623623 /// Construct an explicit graph of the control flow in \c OuterLoop (or the
624624 /// top-level function, if \c OuterLoop is \c nullptr). Uses \c
686686
687687 } // end namespace bfi_detail
688688
689 /// \brief Shared implementation for block frequency analysis.
689 /// Shared implementation for block frequency analysis.
690690 ///
691691 /// This is a shared implementation of BlockFrequencyInfo and
692692 /// MachineBlockFrequencyInfo, and calculates the relative frequencies of
877877 return RPOT[Node.Index];
878878 }
879879
880 /// \brief Run (and save) a post-order traversal.
880 /// Run (and save) a post-order traversal.
881881 ///
882882 /// Saves a reverse post-order traversal of all the nodes in \a F.
883883 void initializeRPOT();
884884
885 /// \brief Initialize loop data.
885 /// Initialize loop data.
886886 ///
887887 /// Build up \a Loops using \a LoopInfo. \a LoopInfo gives us a mapping from
888888 /// each block to the deepest loop it's in, but we need the inverse. For each
891891 /// the loop that are not in sub-loops.
892892 void initializeLoops();
893893
894 /// \brief Propagate to a block's successors.
894 /// Propagate to a block's successors.
895895 ///
896896 /// In the context of distributing mass through \c OuterLoop, divide the mass
897897 /// currently assigned to \c Node between its successors.
899899 /// \return \c true unless there's an irreducible backedge.
900900 bool propagateMassToSuccessors(LoopData *OuterLoop, const BlockNode &Node);
901901
902 /// \brief Compute mass in a particular loop.
902 /// Compute mass in a particular loop.
903903 ///
904904 /// Assign mass to \c Loop's header, and then for each block in \c Loop in
905905 /// reverse post-order, distribute mass to its successors. Only visits nodes
909909 /// \return \c true unless there's an irreducible backedge.
910910 bool computeMassInLoop(LoopData &Loop);
911911
912 /// \brief Try to compute mass in the top-level function.
912 /// Try to compute mass in the top-level function.
913913 ///
914914 /// Assign mass to the entry block, and then for each block in reverse
915915 /// post-order, distribute mass to its successors. Skips nodes that have
919919 /// \return \c true unless there's an irreducible backedge.
920920 bool tryToComputeMassInFunction();
921921
922 /// \brief Compute mass in (and package up) irreducible SCCs.
922 /// Compute mass in (and package up) irreducible SCCs.
923923 ///
924924 /// Find the irreducible SCCs in \c OuterLoop, add them to \a Loops (in front
925925 /// of \c Insert), and call \a computeMassInLoop() on each of them.
934934 void computeIrreducibleMass(LoopData *OuterLoop,
935935 std::list::iterator Insert);
936936
937 /// \brief Compute mass in all loops.
937 /// Compute mass in all loops.
938938 ///
939939 /// For each loop bottom-up, call \a computeMassInLoop().
940940 ///
945945 /// \post \a computeMassInLoop() has returned \c true for every loop.
946946 void computeMassInLoops();
947947
948 /// \brief Compute mass in the top-level function.
948 /// Compute mass in the top-level function.
949949 ///
950950 /// Uses \a tryToComputeMassInFunction() and \a computeIrreducibleMass() to
951951 /// compute mass in the top-level function.
993993
994994 const BranchProbabilityInfoT &getBPI() const { return *BPI; }
995995
996 /// \brief Print the frequencies for the current function.
996 /// Print the frequencies for the current function.
997997 ///
998998 /// Prints the frequencies for the blocks in the current function.
999999 ///
3737 class TargetLibraryInfo;
3838 class Value;
3939
40 /// \brief Analysis providing branch probability information.
40 /// Analysis providing branch probability information.
4141 ///
4242 /// This is a function analysis which provides information on the relative
4343 /// probabilities of each "edge" in the function's CFG where such an edge is
7878
7979 void print(raw_ostream &OS) const;
8080
81 /// \brief Get an edge's probability, relative to other out-edges of the Src.
81 /// Get an edge's probability, relative to other out-edges of the Src.
8282 ///
8383 /// This routine provides access to the fractional probability between zero
8484 /// (0%) and one (100%) of this edge executing, relative to other edges
8787 BranchProbability getEdgeProbability(const BasicBlock *Src,
8888 unsigned IndexInSuccessors) const;
8989
90 /// \brief Get the probability of going from Src to Dst.
90 /// Get the probability of going from Src to Dst.
9191 ///
9292 /// It returns the sum of all probabilities for edges from Src to Dst.
9393 BranchProbability getEdgeProbability(const BasicBlock *Src,
9696 BranchProbability getEdgeProbability(const BasicBlock *Src,
9797 succ_const_iterator Dst) const;
9898
99 /// \brief Test if an edge is hot relative to other out-edges of the Src.
99 /// Test if an edge is hot relative to other out-edges of the Src.
100100 ///
101101 /// Check whether this edge out of the source block is 'hot'. We define hot
102102 /// as having a relative probability >= 80%.
103103 bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const;
104104
105 /// \brief Retrieve the hot successor of a block if one exists.
105 /// Retrieve the hot successor of a block if one exists.
106106 ///
107107 /// Given a basic block, look through its successors and if one exists for
108108 /// which \see isEdgeHot would return true, return that successor block.
109109 const BasicBlock *getHotSucc(const BasicBlock *BB) const;
110110
111 /// \brief Print an edge's probability.
111 /// Print an edge's probability.
112112 ///
113113 /// Retrieves an edge's probability similarly to \see getEdgeProbability, but
114114 /// then prints that probability to the provided stream. That stream is then
116116 raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
117117 const BasicBlock *Dst) const;
118118
119 /// \brief Set the raw edge probability for the given edge.
119 /// Set the raw edge probability for the given edge.
120120 ///
121121 /// This allows a pass to explicitly set the edge probability for an edge. It
122122 /// can be used when updating the CFG to update and preserve the branch
178178
179179 DenseMap Probs;
180180
181 /// \brief Track the last function we run over for printing.
181 /// Track the last function we run over for printing.
182182 const Function *LastF;
183183
184 /// \brief Track the set of blocks directly succeeded by a returning block.
184 /// Track the set of blocks directly succeeded by a returning block.
185185 SmallPtrSet PostDominatedByUnreachable;
186186
187 /// \brief Track the set of blocks that always lead to a cold call.
187 /// Track the set of blocks that always lead to a cold call.
188188 SmallPtrSet PostDominatedByColdCall;
189189
190190 void updatePostDominatedByUnreachable(const BasicBlock *BB);
200200 bool calcInvokeHeuristics(const BasicBlock *BB);
201201 };
202202
203 /// \brief Analysis pass which computes \c BranchProbabilityInfo.
203 /// Analysis pass which computes \c BranchProbabilityInfo.
204204 class BranchProbabilityAnalysis
205205 : public AnalysisInfoMixin {
206206 friend AnalysisInfoMixin;
208208 static AnalysisKey Key;
209209
210210 public:
211 /// \brief Provide the result type for this analysis pass.
211 /// Provide the result type for this analysis pass.
212212 using Result = BranchProbabilityInfo;
213213
214 /// \brief Run the analysis pass over a function and produce BPI.
214 /// Run the analysis pass over a function and produce BPI.
215215 BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM);
216216 };
217217
218 /// \brief Printer pass for the \c BranchProbabilityAnalysis results.
218 /// Printer pass for the \c BranchProbabilityAnalysis results.
219219 class BranchProbabilityPrinterPass
220220 : public PassInfoMixin {
221221 raw_ostream &OS;
226226 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
227227 };
228228
229 /// \brief Legacy analysis pass which computes \c BranchProbabilityInfo.
229 /// Legacy analysis pass which computes \c BranchProbabilityInfo.
230230 class BranchProbabilityInfoWrapperPass : public FunctionPass {
231231 BranchProbabilityInfo BPI;
232232
4848 bool isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
4949 bool AllowIdenticalEdges = false);
5050
51 /// \brief Determine whether instruction 'To' is reachable from 'From',
51 /// Determine whether instruction 'To' is reachable from 'From',
5252 /// returning true if uncertain.
5353 ///
5454 /// Determine whether there is a path from From to To within a single function.
6767 const DominatorTree *DT = nullptr,
6868 const LoopInfo *LI = nullptr);
6969
70 /// \brief Determine whether block 'To' is reachable from 'From', returning
70 /// Determine whether block 'To' is reachable from 'From', returning
7171 /// true if uncertain.
7272 ///
7373 /// Determine whether there is a path from From to To within a single function.
7777 const DominatorTree *DT = nullptr,
7878 const LoopInfo *LI = nullptr);
7979
80 /// \brief Determine whether there is at least one path from a block in
80 /// Determine whether there is at least one path from a block in
8181 /// 'Worklist' to 'StopBB', returning true if uncertain.
8282 ///
8383 /// Determine whether there is a path from at least one block in Worklist to
8989 const DominatorTree *DT = nullptr,
9090 const LoopInfo *LI = nullptr);
9191
92 /// \brief Return true if the control flow in \p RPOTraversal is irreducible.
92 /// Return true if the control flow in \p RPOTraversal is irreducible.
9393 ///
9494 /// This is a generic implementation to detect CFG irreducibility based on loop
9595 /// info analysis. It can be used for any kind of CFG (Loop, MachineLoop,
5555 /// Evict the given function from cache
5656 void evict(const Function *Fn);
5757
58 /// \brief Get the alias summary for the given function
58 /// Get the alias summary for the given function
5959 /// Return nullptr if the summary is not found or not available
6060 const cflaa::AliasSummary *getAliasSummary(const Function &);
6161
6363 AliasResult alias(const MemoryLocation &, const MemoryLocation &);
6464
6565 private:
66 /// \brief Ensures that the given function is available in the cache.
66 /// Ensures that the given function is available in the cache.
6767 /// Returns the appropriate entry from the cache.
6868 const Optional &ensureCached(const Function &);
6969
70 /// \brief Inserts the given Function into the cache.
70 /// Inserts the given Function into the cache.
7171 void scan(const Function &);
7272
73 /// \brief Build summary for a given function
73 /// Build summary for a given function
7474 FunctionInfo buildInfoFrom(const Function &);
7575
7676 const TargetLibraryInfo &TLI;
7777
78 /// \brief Cached mapping of Functions to their StratifiedSets.
78 /// Cached mapping of Functions to their StratifiedSets.
7979 /// If a function's sets are currently being built, it is marked
8080 /// in the cache as an Optional without a value. This way, if we
8181 /// have any kind of recursion, it is discernable from a function
5454 return false;
5555 }
5656
57 /// \brief Inserts the given Function into the cache.
57 /// Inserts the given Function into the cache.
5858 void scan(Function *Fn);
5959
6060 void evict(Function *Fn);
6161
62 /// \brief Ensures that the given function is available in the cache.
62 /// Ensures that the given function is available in the cache.
6363 /// Returns the appropriate entry from the cache.
6464 const Optional &ensureCached(Function *Fn);
65