[llvm] r306154 - [APInt] Use trailing bit counting methods instead of population count method in isAllOnesValue, isMaxSigendValue, and isMinSignedValue. NFCI

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 23 13:28:50 PDT 2017


Author: ctopper
Date: Fri Jun 23 15:28:49 2017
New Revision: 306154

URL: http://llvm.org/viewvc/llvm-project?rev=306154&view=rev
Log:
[APInt] Use trailing bit counting methods instead of population count method in isAllOnesValue, isMaxSigendValue, and isMinSignedValue. NFCI

The trailing bit methods will early out if they find a bit of the opposite while popcount must always look at all bits. I also assume that more CPUs implement trailing bit counting with native instructions than population count.

Modified:
    llvm/trunk/include/llvm/ADT/APInt.h

Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=306154&r1=306153&r2=306154&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Fri Jun 23 15:28:49 2017
@@ -389,7 +389,7 @@ public:
   bool isAllOnesValue() const {
     if (isSingleWord())
       return U.VAL == WORD_MAX >> (APINT_BITS_PER_WORD - BitWidth);
-    return countPopulationSlowCase() == BitWidth;
+    return countTrailingOnesSlowCase() == BitWidth;
   }
 
   /// \brief Determine if all bits are clear
@@ -414,7 +414,7 @@ public:
   /// This checks to see if the value of this APInt is the maximum signed
   /// value for the APInt's bit width.
   bool isMaxSignedValue() const {
-    return !isNegative() && countPopulation() == BitWidth - 1;
+    return !isNegative() && countTrailingOnes() == BitWidth - 1;
   }
 
   /// \brief Determine if this is the smallest unsigned value.
@@ -428,7 +428,7 @@ public:
   /// This checks to see if the value of this APInt is the minimum signed
   /// value for the APInt's bit width.
   bool isMinSignedValue() const {
-    return isNegative() && isPowerOf2();
+    return isNegative() && countTrailingZeros() == BitWidth - 1;
   }
 
   /// \brief Check if this APInt has an N-bits unsigned integer value.




More information about the llvm-commits mailing list