[llvm] r292873 - [APInt] Remove calls to clearUnusedBits from XorSlowCase and operator^=

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 18:10:16 PST 2017


Author: ctopper
Date: Mon Jan 23 20:10:15 2017
New Revision: 292873

URL: http://llvm.org/viewvc/llvm-project?rev=292873&view=rev
Log:
[APInt] Remove calls to clearUnusedBits from XorSlowCase and operator^=

Summary:
There's a comment in XorSlowCase that says "0^0==1" which isn't true. 0 xored with 0 is still 0. So I don't think we need to clear any unused bits here.

Now there is no difference between XorSlowCase and AndSlowCase/OrSlowCase other than the operation being performed

Reviewers: majnemer, MatzeB, chandlerc, bkramer

Reviewed By: MatzeB

Subscribers: chfast, llvm-commits

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

Modified:
    llvm/trunk/lib/Support/APInt.cpp
    llvm/trunk/unittests/ADT/APIntTest.cpp

Modified: llvm/trunk/lib/Support/APInt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=292873&r1=292872&r2=292873&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APInt.cpp (original)
+++ llvm/trunk/lib/Support/APInt.cpp Mon Jan 23 20:10:15 2017
@@ -440,13 +440,12 @@ APInt& APInt::operator^=(const APInt& RH
   assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
   if (isSingleWord()) {
     VAL ^= RHS.VAL;
-    this->clearUnusedBits();
     return *this;
   }
   unsigned numWords = getNumWords();
   for (unsigned i = 0; i < numWords; ++i)
     pVal[i] ^= RHS.pVal[i];
-  return clearUnusedBits();
+  return *this;
 }
 
 APInt APInt::AndSlowCase(const APInt& RHS) const {
@@ -471,10 +470,7 @@ APInt APInt::XorSlowCase(const APInt& RH
   for (unsigned i = 0; i < numWords; ++i)
     val[i] = pVal[i] ^ RHS.pVal[i];
 
-  APInt Result(val, getBitWidth());
-  // 0^0==1 so clear the high bits in case they got set.
-  Result.clearUnusedBits();
-  return Result;
+  return APInt(val, getBitWidth());
 }
 
 APInt APInt::operator*(const APInt& RHS) const {

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=292873&r1=292872&r2=292873&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Mon Jan 23 20:10:15 2017
@@ -158,6 +158,36 @@ TEST(APIntTest, i1) {
   EXPECT_EQ(two, one - neg_one);
   EXPECT_EQ(zero, one - one);
 
+  // And
+  EXPECT_EQ(zero, zero & zero);
+  EXPECT_EQ(zero, one & zero);
+  EXPECT_EQ(zero, zero & one);
+  EXPECT_EQ(one, one & one);
+  EXPECT_EQ(zero, zero & zero);
+  EXPECT_EQ(zero, neg_one & zero);
+  EXPECT_EQ(zero, zero & neg_one);
+  EXPECT_EQ(neg_one, neg_one & neg_one);
+
+  // Or
+  EXPECT_EQ(zero, zero | zero);
+  EXPECT_EQ(one, one | zero);
+  EXPECT_EQ(one, zero | one);
+  EXPECT_EQ(one, one | one);
+  EXPECT_EQ(zero, zero | zero);
+  EXPECT_EQ(neg_one, neg_one | zero);
+  EXPECT_EQ(neg_one, zero | neg_one);
+  EXPECT_EQ(neg_one, neg_one | neg_one);
+
+  // Xor
+  EXPECT_EQ(zero, zero ^ zero);
+  EXPECT_EQ(one, one ^ zero);
+  EXPECT_EQ(one, zero ^ one);
+  EXPECT_EQ(zero, one ^ one);
+  EXPECT_EQ(zero, zero ^ zero);
+  EXPECT_EQ(neg_one, neg_one ^ zero);
+  EXPECT_EQ(neg_one, zero ^ neg_one);
+  EXPECT_EQ(zero, neg_one ^ neg_one);
+
   // Shifts.
   EXPECT_EQ(zero, one << one);
   EXPECT_EQ(one, one << zero);




More information about the llvm-commits mailing list