[PATCH] D16620: APInt: Simplify and Optimize EqualSlowCase

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 26 20:07:12 PST 2016

MatzeB created this revision.
MatzeB added reviewers: bkramer, zhousheng00, foad, echristo.
MatzeB added a subscriber: llvm-commits.
MatzeB set the repository for this revision to rL LLVM.
Herald added a subscriber: mcrosier.

Previously the code used getActiveBits() to determine the highest set bit
of each APInt first. However doing so requires the same amount of
memory accesses as simply comparing both numbers right away.

Removing all the active bit checks leads to simpler code and is faster
in my benchmark.

(This function is worth optimizing because unfortunately it sometimes shows up in profiles because SCEV often uses 65Bit APInts)




Index: lib/Support/APInt.cpp
--- lib/Support/APInt.cpp
+++ lib/Support/APInt.cpp
@@ -490,21 +490,8 @@
 bool APInt::EqualSlowCase(const APInt& RHS) const {
-  // Get some facts about the number of bits used in the two operands.
-  unsigned n1 = getActiveBits();
-  unsigned n2 = RHS.getActiveBits();
-  // If the number of bits isn't the same, they aren't equal
-  if (n1 != n2)
-    return false;
-  // If the number of bits fits in a word, we only need to compare the low word.
-  if (n1 <= APINT_BITS_PER_WORD)
-    return pVal[0] == RHS.pVal[0];
-  // Otherwise, compare everything
-  for (int i = whichWord(n1 - 1); i >= 0; --i)
-    if (pVal[i] != RHS.pVal[i])
+  for (unsigned I = 0, NumWords = getNumWords(); I < NumWords; ++I)
+    if (pVal[I] != RHS.pVal[I])
       return false;
   return true;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16620.46094.patch
Type: text/x-patch
Size: 887 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160127/94940c4c/attachment.bin>

More information about the llvm-commits mailing list