[llvm] 16df25d - [APInt][NFC] Add branch hints to APInt single-word (#219437)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 00:04:21 PDT 2026


Author: Steffen Larsen
Date: 2026-09-08T09:04:15+02:00
New Revision: 16df25d03f5a5becf9dbe994ef6b6d4546b357d9

URL: https://github.com/llvm/llvm-project/commit/16df25d03f5a5becf9dbe994ef6b6d4546b357d9
DIFF: https://github.com/llvm/llvm-project/commit/16df25d03f5a5becf9dbe994ef6b6d4546b357d9.diff

LOG: [APInt][NFC] Add branch hints to APInt single-word (#219437)

The fast-path for APInt is generally done with for "single word"
integers, i.e. ones that can be represented in a single 64-bit word.
This commit adds branch hints to the single-word branching to help
indicate to the compilers prioritize branch-prediction towards the
fast-path. The trade-off of this is that the >64-bit APInt case will be
slower.

---------

Signed-off-by: Steffen Holst Larsen <sholstla at amd.com>

Added: 
    

Modified: 
    llvm/include/llvm/ADT/APInt.h
    llvm/lib/Support/APInt.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 454025b024ae6..c13677aaba0f8 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -129,7 +129,7 @@ class [[nodiscard]] APInt {
         }
       }
     }
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       U.VAL = val;
       if (implicitTrunc || isSigned)
         clearUnusedBits();
@@ -171,7 +171,7 @@ class [[nodiscard]] APInt {
 
   /// Copy Constructor.
   APInt(const APInt &that) : BitWidth(that.BitWidth) {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL = that.U.VAL;
     else
       initSlowCase(that);
@@ -368,14 +368,14 @@ class [[nodiscard]] APInt {
   bool isAllOnes() const {
     if (BitWidth == 0)
       return true;
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return U.VAL == WORDTYPE_MAX >> (APINT_BITS_PER_WORD - BitWidth);
     return countTrailingOnesSlowCase() == BitWidth;
   }
 
   /// Determine if this value is zero, i.e. all bits are clear.
   bool isZero() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return U.VAL == 0;
     return countLeadingZerosSlowCase() == BitWidth;
   }
@@ -384,7 +384,7 @@ class [[nodiscard]] APInt {
   ///
   /// This checks to see if the value of this APInt is one.
   bool isOne() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return U.VAL == 1;
     return countLeadingZerosSlowCase() == BitWidth - 1;
   }
@@ -400,7 +400,7 @@ class [[nodiscard]] APInt {
   /// This checks to see if the value of this APInt is the maximum signed
   /// value for the APInt's bit width.
   bool isMaxSignedValue() const {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       assert(BitWidth && "zero width values not allowed");
       return U.VAL == ((WordType(1) << (BitWidth - 1)) - 1);
     }
@@ -418,7 +418,7 @@ class [[nodiscard]] APInt {
   /// This checks to see if the value of this APInt is the minimum signed
   /// value for the APInt's bit width.
   bool isMinSignedValue() const {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       assert(BitWidth && "zero width values not allowed");
       return U.VAL == (WordType(1) << (BitWidth - 1));
     }
@@ -435,7 +435,7 @@ class [[nodiscard]] APInt {
   ///
   /// \returns true if the argument APInt value is a power of two > 0.
   bool isPowerOf2() const {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       assert(BitWidth && "zero width values not allowed");
       return isPowerOf2_64(U.VAL);
     }
@@ -485,7 +485,7 @@ class [[nodiscard]] APInt {
   bool isMask(unsigned numBits) const {
     assert(numBits != 0 && "numBits must be non-zero");
     assert(numBits <= BitWidth && "numBits out of range");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return U.VAL == (WORDTYPE_MAX >> (APINT_BITS_PER_WORD - numBits));
     unsigned Ones = countTrailingOnesSlowCase();
     return (numBits == Ones) &&
@@ -496,7 +496,7 @@ class [[nodiscard]] APInt {
   /// the least significant bit with the remainder zero.
   /// Ex. isMask(0x0000FFFFU) == true.
   bool isMask() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return isMask_64(U.VAL);
     unsigned Ones = countTrailingOnesSlowCase();
     return (Ones > 0) && ((Ones + countLeadingZerosSlowCase()) == BitWidth);
@@ -505,7 +505,7 @@ class [[nodiscard]] APInt {
   /// Return true if this APInt value contains a non-empty sequence of ones with
   /// the remainder zero.
   bool isShiftedMask() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return isShiftedMask_64(U.VAL);
     unsigned Ones = countPopulationSlowCase();
     unsigned LeadZ = countLeadingZerosSlowCase();
@@ -517,7 +517,7 @@ class [[nodiscard]] APInt {
   /// lowest set bit and \p MaskLen is updated to specify the length of the
   /// mask, else neither are updated.
   bool isShiftedMask(unsigned &MaskIdx, unsigned &MaskLen) const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return isShiftedMask_64(U.VAL, MaskIdx, MaskLen);
     unsigned Ones = countPopulationSlowCase();
     unsigned LeadZ = countLeadingZerosSlowCase();
@@ -570,7 +570,7 @@ class [[nodiscard]] APInt {
   /// This is useful for writing out the APInt in binary form without any
   /// conversions.
   const uint64_t *getRawData() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return &U.VAL;
     return &U.pVal[0];
   }
@@ -621,7 +621,7 @@ class [[nodiscard]] APInt {
   APInt &operator=(const APInt &RHS) {
     // The common case (both source or dest being inline) doesn't require
     // allocation or deallocation.
-    if (isSingleWord() && RHS.isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord() && RHS.isSingleWord())) {
       U.VAL = RHS.U.VAL;
       BitWidth = RHS.BitWidth;
       return *this;
@@ -639,7 +639,7 @@ class [[nodiscard]] APInt {
       return *this;
 #endif
     assert(this != &that && "Self-move not supported");
-    if (!isSingleWord())
+    if (LLVM_UNLIKELY(!isSingleWord()))
       delete[] U.pVal;
 
     // Use memcpy so that type based alias analysis sees both VAL and pVal
@@ -659,7 +659,7 @@ class [[nodiscard]] APInt {
   ///
   /// \returns *this after assignment of RHS value.
   APInt &operator=(uint64_t RHS) {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       U.VAL = RHS;
       return clearUnusedBits();
     }
@@ -676,7 +676,7 @@ class [[nodiscard]] APInt {
   /// \returns *this after ANDing with RHS.
   APInt &operator&=(const APInt &RHS) {
     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL &= RHS.U.VAL;
     else
       andAssignSlowCase(RHS);
@@ -689,7 +689,7 @@ class [[nodiscard]] APInt {
   /// logically zero-extended or truncated to match the bit-width of
   /// the LHS.
   APInt &operator&=(uint64_t RHS) {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       U.VAL &= RHS;
       return *this;
     }
@@ -706,7 +706,7 @@ class [[nodiscard]] APInt {
   /// \returns *this after ORing with RHS.
   APInt &operator|=(const APInt &RHS) {
     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL |= RHS.U.VAL;
     else
       orAssignSlowCase(RHS);
@@ -719,7 +719,7 @@ class [[nodiscard]] APInt {
   /// logically zero-extended or truncated to match the bit-width of
   /// the LHS.
   APInt &operator|=(uint64_t RHS) {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       U.VAL |= RHS;
       return clearUnusedBits();
     }
@@ -735,7 +735,7 @@ class [[nodiscard]] APInt {
   /// \returns *this after XORing with RHS.
   APInt &operator^=(const APInt &RHS) {
     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL ^= RHS.U.VAL;
     else
       xorAssignSlowCase(RHS);
@@ -748,7 +748,7 @@ class [[nodiscard]] APInt {
   /// logically zero-extended or truncated to match the bit-width of
   /// the LHS.
   APInt &operator^=(uint64_t RHS) {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       U.VAL ^= RHS;
       return clearUnusedBits();
     }
@@ -787,7 +787,7 @@ class [[nodiscard]] APInt {
   /// \returns *this after shifting left by ShiftAmt
   APInt &operator<<=(unsigned ShiftAmt) {
     assert(ShiftAmt <= BitWidth && "Invalid shift amount");
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       if (ShiftAmt == BitWidth)
         U.VAL = 0;
       else
@@ -836,7 +836,7 @@ class [[nodiscard]] APInt {
   /// Arithmetic right-shift this APInt by ShiftAmt in place.
   void ashrInPlace(unsigned ShiftAmt) {
     assert(ShiftAmt <= BitWidth && "Invalid shift amount");
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       int64_t SExtVAL = SignExtend64(U.VAL, BitWidth);
       if (ShiftAmt == BitWidth)
         U.VAL = SExtVAL >> (APINT_BITS_PER_WORD - 1); // Fill with sign bit.
@@ -860,7 +860,7 @@ class [[nodiscard]] APInt {
   /// Logical right-shift this APInt by ShiftAmt in place.
   void lshrInPlace(unsigned ShiftAmt) {
     assert(ShiftAmt <= BitWidth && "Invalid shift amount");
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       if (ShiftAmt == BitWidth)
         U.VAL = 0;
       else
@@ -1060,7 +1060,7 @@ class [[nodiscard]] APInt {
   /// relationship.
   bool operator==(const APInt &RHS) const {
     assert(BitWidth == RHS.BitWidth && "Comparison requires equal bit widths");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return U.VAL == RHS.U.VAL;
     return equalSlowCase(RHS);
   }
@@ -1141,7 +1141,7 @@ class [[nodiscard]] APInt {
   ///
   /// \returns true if *this < RHS when considered signed.
   bool slt(int64_t RHS) const {
-    return (!isSingleWord() && getSignificantBits() > 64)
+    return LLVM_UNLIKELY(!isSingleWord() && getSignificantBits() > 64)
                ? isNegative()
                : getSExtValue() < RHS;
   }
@@ -1212,7 +1212,7 @@ class [[nodiscard]] APInt {
   ///
   /// \returns true if *this > RHS when considered signed.
   bool sgt(int64_t RHS) const {
-    return (!isSingleWord() && getSignificantBits() > 64)
+    return LLVM_UNLIKELY(!isSingleWord() && getSignificantBits() > 64)
                ? !isNegative()
                : getSExtValue() > RHS;
   }
@@ -1253,7 +1253,7 @@ class [[nodiscard]] APInt {
   /// between this APInt and RHS that are both set.
   bool intersects(const APInt &RHS) const {
     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return (U.VAL & RHS.U.VAL) != 0;
     return intersectsSlowCase(RHS);
   }
@@ -1261,7 +1261,7 @@ class [[nodiscard]] APInt {
   /// This operation checks that all bits set in this APInt are also set in RHS.
   bool isSubsetOf(const APInt &RHS) const {
     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return (U.VAL & ~RHS.U.VAL) == 0;
     return isSubsetOfSlowCase(RHS);
   }
@@ -1269,7 +1269,7 @@ class [[nodiscard]] APInt {
   /// This operation checks if all bits are set in either this or RHS.
   bool isInverseOf(const APInt &RHS) const {
     assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return (U.VAL ^ RHS.U.VAL) == llvm::maskTrailingOnes<WordType>(BitWidth);
     return isInverseOfSlowCase(RHS);
   }
@@ -1338,7 +1338,7 @@ class [[nodiscard]] APInt {
 
   /// Set every bit to 1.
   void setAllBits() {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL = WORDTYPE_MAX;
     else
       // Set all the bits in all the words.
@@ -1351,7 +1351,7 @@ class [[nodiscard]] APInt {
   void setBit(unsigned BitPosition) {
     assert(BitPosition < BitWidth && "BitPosition out of range");
     WordType Mask = maskBit(BitPosition);
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL |= Mask;
     else
       U.pVal[whichWord(BitPosition)] |= Mask;
@@ -1393,7 +1393,7 @@ class [[nodiscard]] APInt {
     if (hiBit <= APINT_BITS_PER_WORD) {
       uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
       mask <<= loBit;
-      if (isSingleWord())
+      if (LLVM_LIKELY(isSingleWord()))
         U.VAL |= mask;
       else
         U.pVal[0] |= mask;
@@ -1415,7 +1415,7 @@ class [[nodiscard]] APInt {
 
   /// Set every bit to 0.
   void clearAllBits() {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL = 0;
     else
       memset(U.pVal, 0, getNumWords() * APINT_WORD_SIZE);
@@ -1427,7 +1427,7 @@ class [[nodiscard]] APInt {
   void clearBit(unsigned BitPosition) {
     assert(BitPosition < BitWidth && "BitPosition out of range");
     WordType Mask = ~maskBit(BitPosition);
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL &= Mask;
     else
       U.pVal[whichWord(BitPosition)] &= Mask;
@@ -1443,7 +1443,7 @@ class [[nodiscard]] APInt {
     if (HiBit <= APINT_BITS_PER_WORD) {
       uint64_t Mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (HiBit - LoBit));
       Mask = ~(Mask << LoBit);
-      if (isSingleWord())
+      if (LLVM_LIKELY(isSingleWord()))
         U.VAL &= Mask;
       else
         U.pVal[0] &= Mask;
@@ -1471,7 +1471,7 @@ class [[nodiscard]] APInt {
 
   /// Toggle every bit to its opposite value.
   void flipAllBits() {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       U.VAL ^= WORDTYPE_MAX;
       clearUnusedBits();
     } else {
@@ -1559,7 +1559,7 @@ class [[nodiscard]] APInt {
   /// uint64_t. The bitwidth must be <= 64 or the value must fit within a
   /// uint64_t. Otherwise an assertion will result.
   uint64_t getZExtValue() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return U.VAL;
     assert(getActiveBits() <= 64 && "Too many bits for uint64_t");
     return U.pVal[0];
@@ -1581,7 +1581,7 @@ class [[nodiscard]] APInt {
   /// int64_t. The bit width must be <= 64 or the value must fit within an
   /// int64_t. Otherwise an assertion will result.
   int64_t getSExtValue() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return SignExtend64(U.VAL, BitWidth);
     assert(getSignificantBits() <= 64 && "Too many bits for int64_t");
     return int64_t(U.pVal[0]);
@@ -1617,7 +1617,7 @@ class [[nodiscard]] APInt {
   /// \returns BitWidth if the value is zero, otherwise returns the number of
   ///   zeros from the most significant bit to the first one bits.
   unsigned countl_zero() const {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       unsigned unusedBits = APINT_BITS_PER_WORD - BitWidth;
       return llvm::countl_zero(U.VAL) - unusedBits;
     }
@@ -1634,7 +1634,7 @@ class [[nodiscard]] APInt {
   /// \returns 0 if the high order bit is not set, otherwise returns the number
   /// of 1 bits from the most significant to the least
   unsigned countl_one() const {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       if (LLVM_UNLIKELY(BitWidth == 0))
         return 0;
       return llvm::countl_one(U.VAL << (APINT_BITS_PER_WORD - BitWidth));
@@ -1658,7 +1658,7 @@ class [[nodiscard]] APInt {
   /// \returns BitWidth if the value is zero, otherwise returns the number of
   /// zeros from the least significant bit to the first one bit.
   unsigned countr_zero() const {
-    if (isSingleWord()) {
+    if (LLVM_LIKELY(isSingleWord())) {
       unsigned TrailingZeros = llvm::countr_zero(U.VAL);
       return (TrailingZeros > BitWidth ? BitWidth : TrailingZeros);
     }
@@ -1675,7 +1675,7 @@ class [[nodiscard]] APInt {
   /// \returns BitWidth if the value is all ones, otherwise returns the number
   /// of ones from the least significant bit to the first zero bit.
   unsigned countr_one() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return llvm::countr_one(U.VAL);
     return countTrailingOnesSlowCase();
   }
@@ -1689,7 +1689,7 @@ class [[nodiscard]] APInt {
   ///
   /// \returns 0 if the value is zero, otherwise returns the number of set bits.
   unsigned popcount() const {
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       return llvm::popcount(U.VAL);
     return countPopulationSlowCase();
   }
@@ -2010,7 +2010,7 @@ class [[nodiscard]] APInt {
     if (LLVM_UNLIKELY(BitWidth == 0))
       mask = 0;
 
-    if (isSingleWord())
+    if (LLVM_LIKELY(isSingleWord()))
       U.VAL &= mask;
     else
       U.pVal[getNumWords() - 1] &= mask;
@@ -2020,7 +2020,7 @@ class [[nodiscard]] APInt {
   /// Get the word corresponding to a bit position
   /// \returns the corresponding word for the specified bit position.
   uint64_t getWord(unsigned bitPosition) const {
-    return isSingleWord() ? U.VAL : U.pVal[whichWord(bitPosition)];
+    return LLVM_LIKELY(isSingleWord()) ? U.VAL : U.pVal[whichWord(bitPosition)];
   }
 
   /// Utility method to change the bit width of this APInt to new bit width,

diff  --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index d9413415b348b..7d23722d7eb33 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -91,7 +91,7 @@ void APInt::initSlowCase(const APInt& that) {
 
 void APInt::initFromArray(ArrayRef<uint64_t> bigVal) {
   assert(bigVal.data() && "Null pointer detected!");
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL = bigVal[0];
   else {
     // Get memory, cleared to 0
@@ -122,14 +122,14 @@ void APInt::reallocate(unsigned NewBitWidth) {
   }
 
   // If we have an allocation, delete it.
-  if (!isSingleWord())
+  if (LLVM_UNLIKELY(!isSingleWord()))
     delete [] U.pVal;
 
   // Update BitWidth.
   BitWidth = NewBitWidth;
 
   // If we are supposed to have an allocation, create it.
-  if (!isSingleWord())
+  if (LLVM_UNLIKELY(!isSingleWord()))
     U.pVal = getMemory(getNumWords());
 }
 
@@ -142,7 +142,7 @@ void APInt::assignSlowCase(const APInt &RHS) {
   reallocate(RHS.getBitWidth());
 
   // Copy the data.
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL = RHS.U.VAL;
   else
     memcpy(U.pVal, RHS.U.pVal, getNumWords() * APINT_WORD_SIZE);
@@ -152,7 +152,7 @@ void APInt::assignSlowCase(const APInt &RHS) {
 void APInt::Profile(FoldingSetNodeID& ID) const {
   ID.AddInteger(BitWidth);
 
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     ID.AddInteger(U.VAL);
     return;
   }
@@ -172,7 +172,7 @@ bool APInt::isAligned(Align A) const {
 
 /// Prefix increment operator. Increments the APInt by one.
 APInt& APInt::operator++() {
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     ++U.VAL;
   else
     tcIncrement(U.pVal, getNumWords());
@@ -181,7 +181,7 @@ APInt& APInt::operator++() {
 
 /// Prefix decrement operator. Decrements the APInt by one.
 APInt& APInt::operator--() {
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     --U.VAL;
   else
     tcDecrement(U.pVal, getNumWords());
@@ -193,7 +193,7 @@ APInt& APInt::operator--() {
 /// Addition assignment operator.
 APInt& APInt::operator+=(const APInt& RHS) {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL += RHS.U.VAL;
   else
     tcAdd(U.pVal, RHS.U.pVal, 0, getNumWords());
@@ -201,7 +201,7 @@ APInt& APInt::operator+=(const APInt& RHS) {
 }
 
 APInt& APInt::operator+=(uint64_t RHS) {
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL += RHS;
   else
     tcAddPart(U.pVal, RHS, getNumWords());
@@ -213,7 +213,7 @@ APInt& APInt::operator+=(uint64_t RHS) {
 /// Subtraction assignment operator.
 APInt& APInt::operator-=(const APInt& RHS) {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL -= RHS.U.VAL;
   else
     tcSubtract(U.pVal, RHS.U.pVal, 0, getNumWords());
@@ -221,7 +221,7 @@ APInt& APInt::operator-=(const APInt& RHS) {
 }
 
 APInt& APInt::operator-=(uint64_t RHS) {
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL -= RHS;
   else
     tcSubtractPart(U.pVal, RHS, getNumWords());
@@ -230,7 +230,7 @@ APInt& APInt::operator-=(uint64_t RHS) {
 
 APInt APInt::operator*(const APInt& RHS) const {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     return APInt(BitWidth, U.VAL * RHS.U.VAL, /*isSigned=*/false,
                  /*implicitTrunc=*/true);
 
@@ -264,7 +264,7 @@ APInt &APInt::operator*=(const APInt &RHS) {
 }
 
 APInt& APInt::operator*=(uint64_t RHS) {
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     U.VAL *= RHS;
   } else {
     unsigned NumWords = getNumWords();
@@ -279,7 +279,7 @@ bool APInt::equalSlowCase(const APInt &RHS) const {
 
 int APInt::compare(const APInt& RHS) const {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     return U.VAL < RHS.U.VAL ? -1 : U.VAL > RHS.U.VAL;
 
   return tcCompare(U.pVal, RHS.U.pVal, getNumWords());
@@ -287,7 +287,7 @@ int APInt::compare(const APInt& RHS) const {
 
 int APInt::compareSigned(const APInt& RHS) const {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be same for comparison");
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     int64_t lhsSext = SignExtend64(U.VAL, BitWidth);
     int64_t rhsSext = SignExtend64(RHS.U.VAL, BitWidth);
     return lhsSext < rhsSext ? -1 : lhsSext > rhsSext;
@@ -405,7 +405,7 @@ void APInt::insertBits(const APInt &subBits, unsigned bitPosition) {
   }
 
   // Single word result can be done as a direct bitmask.
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - subBitWidth);
     U.VAL &= ~(mask << bitPosition);
     U.VAL |= (subBits.U.VAL << bitPosition);
@@ -451,7 +451,7 @@ void APInt::insertBits(const APInt &subBits, unsigned bitPosition) {
 void APInt::insertBits(uint64_t subBits, unsigned bitPosition, unsigned numBits) {
   uint64_t maskBits = maskTrailingOnes<uint64_t>(numBits);
   subBits &= maskBits;
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     U.VAL &= ~(maskBits << bitPosition);
     U.VAL |= subBits << bitPosition;
     return;
@@ -479,7 +479,7 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const {
   assert(bitPosition < BitWidth && (numBits + bitPosition) <= BitWidth &&
          "Illegal bit extraction");
 
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     return APInt(numBits, U.VAL >> bitPosition, /*isSigned=*/false,
                  /*implicitTrunc=*/true);
 
@@ -502,7 +502,8 @@ APInt APInt::extractBits(unsigned numBits, unsigned bitPosition) const {
   unsigned NumSrcWords = getNumWords();
   unsigned NumDstWords = Result.getNumWords();
 
-  uint64_t *DestPtr = Result.isSingleWord() ? &Result.U.VAL : Result.U.pVal;
+  uint64_t *DestPtr =
+      LLVM_LIKELY(Result.isSingleWord()) ? &Result.U.VAL : Result.U.pVal;
   for (unsigned word = 0; word < NumDstWords; ++word) {
     uint64_t w0 = U.pVal[loWord + word];
     uint64_t w1 =
@@ -520,7 +521,7 @@ uint64_t APInt::extractBitsAsZExtValue(unsigned numBits,
   assert(numBits <= 64 && "Illegal bit extraction");
 
   uint64_t maskBits = maskTrailingOnes<uint64_t>(numBits);
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     return (U.VAL >> bitPosition) & maskBits;
 
   static_assert(APINT_BITS_PER_WORD >= 64,
@@ -611,7 +612,7 @@ unsigned APInt::getBitsNeeded(StringRef str, uint8_t radix) {
 }
 
 hash_code llvm::hash_value(const APInt &Arg) {
-  if (Arg.isSingleWord())
+  if (LLVM_LIKELY(Arg.isSingleWord()))
     return hash_combine(Arg.BitWidth, Arg.U.VAL);
 
   return hash_combine(
@@ -783,7 +784,7 @@ APInt APInt::byteSwap() const {
 }
 
 APInt APInt::reverseBits() const {
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     switch (BitWidth) {
     case 64:
       return APInt(BitWidth, llvm::reverseBits<uint64_t>(U.VAL));
@@ -913,7 +914,7 @@ APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) {
 double APInt::roundToDouble(bool isSigned) const {
   // Handle the simple case where the value is contained in one uint64_t.
   // It is wrong to optimize getWord(0) to VAL; there might be more than one word.
-  if (isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD) {
+  if (LLVM_LIKELY(isSingleWord() || getActiveBits() <= APINT_BITS_PER_WORD)) {
     if (isSigned) {
       int64_t sext = SignExtend64(getWord(0), BitWidth);
       return double(sext);
@@ -1256,7 +1257,8 @@ APInt APInt::sqrtFloor() const {
         /* 16-24 */ 4, 4, 4, 4, 4, 4, 4, 4, 4,
         /* 25-31 */ 5, 5, 5, 5, 5, 5, 5,
     };
-    return APInt(BitWidth, results[ (isSingleWord() ? U.VAL : U.pVal[0]) ]);
+    return APInt(BitWidth,
+                 results[(LLVM_LIKELY(isSingleWord()) ? U.VAL : U.pVal[0])]);
   }
 
   // If the magnitude of the value fits in less than 52 bits (the precision of
@@ -1264,9 +1266,9 @@ APInt APInt::sqrtFloor() const {
   // libc sqrt function which will probably use a hardware sqrt computation.
   // This should be faster than the algorithm below.
   if (magnitude < 52) {
-    return APInt(
-        BitWidth,
-        uint64_t(::floor(::sqrt(double(isSingleWord() ? U.VAL : U.pVal[0])))));
+    return APInt(BitWidth,
+                 uint64_t(::floor(::sqrt(double(
+                     LLVM_LIKELY(isSingleWord()) ? U.VAL : U.pVal[0])))));
   }
 
   // Okay, all the short cuts are exhausted. We must compute it. The following
@@ -1601,7 +1603,7 @@ APInt APInt::udiv(const APInt &RHS) const {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
 
   // First, deal with the easy case
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     assert(RHS.U.VAL != 0 && "Divide by zero?");
     return APInt(BitWidth, U.VAL / RHS.U.VAL);
   }
@@ -1639,7 +1641,7 @@ APInt APInt::udiv(uint64_t RHS) const {
   assert(RHS != 0 && "Divide by zero?");
 
   // First, deal with the easy case
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     return APInt(BitWidth, U.VAL / RHS);
 
   // Get some facts about the LHS words.
@@ -1692,7 +1694,7 @@ APInt APInt::sdiv(int64_t RHS) const {
 
 APInt APInt::urem(const APInt &RHS) const {
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     assert(RHS.U.VAL != 0 && "Remainder by zero?");
     return APInt(BitWidth, U.VAL % RHS.U.VAL);
   }
@@ -1737,7 +1739,7 @@ APInt APInt::urem(const APInt &RHS) const {
 uint64_t APInt::urem(uint64_t RHS) const {
   assert(RHS != 0 && "Remainder by zero?");
 
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     return U.VAL % RHS;
 
   // Get some facts about the LHS
@@ -1797,7 +1799,7 @@ void APInt::udivrem(const APInt &LHS, const APInt &RHS,
   unsigned BitWidth = LHS.BitWidth;
 
   // First, deal with the easy case
-  if (LHS.isSingleWord()) {
+  if (LLVM_LIKELY(LHS.isSingleWord())) {
     assert(RHS.U.VAL != 0 && "Divide by zero?");
     uint64_t QuotVal = LHS.U.VAL / RHS.U.VAL;
     uint64_t RemVal = LHS.U.VAL % RHS.U.VAL;
@@ -1868,7 +1870,7 @@ void APInt::udivrem(const APInt &LHS, uint64_t RHS, APInt &Quotient,
   unsigned BitWidth = LHS.BitWidth;
 
   // First, deal with the easy case
-  if (LHS.isSingleWord()) {
+  if (LLVM_LIKELY(LHS.isSingleWord())) {
     uint64_t QuotVal = LHS.U.VAL / RHS;
     Remainder = LHS.U.VAL % RHS;
     Quotient = APInt(BitWidth, QuotVal);
@@ -2168,7 +2170,7 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
          "Insufficient bit width");
 
   // Allocate memory if needed
-  if (isSingleWord())
+  if (LLVM_LIKELY(isSingleWord()))
     U.VAL = 0;
   else
     U.pVal = getClearedMemory(getNumWords());
@@ -2242,7 +2244,7 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix, bool Signed,
                                    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   const char *Digits = BothDigits + (UpperCase ? 36 : 0);
 
-  if (isSingleWord()) {
+  if (LLVM_LIKELY(isSingleWord())) {
     char Buffer[65];
     char *BufPtr = std::end(Buffer);
 


        


More information about the llvm-commits mailing list