]> granicus.if.org Git - llvm/commitdiff
[APInt] Implement APInt::intersects without creating a temporary APInt in the multiwo...
authorCraig Topper <craig.topper@gmail.com>
Thu, 20 Apr 2017 02:11:27 +0000 (02:11 +0000)
committerCraig Topper <craig.topper@gmail.com>
Thu, 20 Apr 2017 02:11:27 +0000 (02:11 +0000)
Summary: This is a simple question we should be able to answer without creating a temporary to hold the AND result. We can also get an early out as soon as we find a word that intersects.

Reviewers: RKSimon, hans, spatel, davide

Reviewed By: hans, davide

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D32253

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300812 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 2fafeafb6adf444ee684fa7bc1cc45c9a7cd7f02..5f87765a52d23d8a6b58e2db9920e40f60317f0b 100644 (file)
@@ -209,6 +209,9 @@ private:
   /// out-of-line slow case for countPopulation
   unsigned countPopulationSlowCase() const LLVM_READONLY;
 
+  /// out-of-line slow case for intersects.
+  bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY;
+
   /// out-of-line slow case for setBits.
   void setBitsSlowCase(unsigned loBit, unsigned hiBit);
 
@@ -1206,9 +1209,10 @@ public:
   /// This operation tests if there are any pairs of corresponding bits
   /// between this APInt and RHS that are both set.
   bool intersects(const APInt &RHS) const {
-    APInt temp(*this);
-    temp &= RHS;
-    return temp != 0;
+    assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
+    if (isSingleWord())
+      return (VAL & RHS.VAL) != 0;
+    return intersectsSlowCase(RHS);
   }
 
   /// @}
index 805a912501d08ecea72cb0f4a1d2acffecead9dc..54ffc3c0274adbbfd9265281d2df27422af8283f 100644 (file)
@@ -722,6 +722,14 @@ unsigned APInt::countPopulationSlowCase() const {
   return Count;
 }
 
+bool APInt::intersectsSlowCase(const APInt &RHS) const {
+  for (unsigned i = 0, e = getNumWords(); i != e; ++i)
+    if ((pVal[i] & RHS.pVal[i]) != 0)
+      return true;
+
+  return false;
+}
+
 APInt APInt::byteSwap() const {
   assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
   if (BitWidth == 16)