[PATCH] D81325: [APInt] set all bits for getBitsSetWithWrap if loBit == hiBit

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 5 20:04:21 PDT 2020


shchenz created this revision.
shchenz added reviewers: craig.topper, lebedev.ri, RKSimon, nikic, steven.zhang.
Herald added subscribers: llvm-commits, wuzish, dexonsmith, kbarton, hiraditya, nemanjai.
Herald added a project: LLVM.

For now, 
`getBitsSetWithWrap(unsigned numBits, unsigned loBit, unsigned hiBit)` & `getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)` both do nothing if `loBit == hiBit`.

We need to differentiate them. 
if `loBit == hiBit`,  `getBitsSetWithWrap` sets all bits. and `getBitsSet` does nothing.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81325

Files:
  llvm/include/llvm/ADT/APInt.h
  llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
  llvm/unittests/ADT/APIntTest.cpp


Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -2099,6 +2099,14 @@
   EXPECT_EQ(0u, i127hi1lo1wrap.countTrailingZeros());
   EXPECT_EQ(1u, i127hi1lo1wrap.countTrailingOnes());
   EXPECT_EQ(2u, i127hi1lo1wrap.countPopulation());
+  
+  APInt i32hiequallowrap = APInt::getBitsSetWithWrap(32, 10, 10);
+  EXPECT_EQ(32u, i32hiequallowrap.countLeadingOnes());
+  EXPECT_EQ(0u, i32hiequallowrap.countLeadingZeros());
+  EXPECT_EQ(32u, i32hiequallowrap.getActiveBits());
+  EXPECT_EQ(0u, i32hiequallowrap.countTrailingZeros());
+  EXPECT_EQ(32u, i32hiequallowrap.countTrailingOnes());
+  EXPECT_EQ(32u, i32hiequallowrap.countPopulation());
 }
 
 TEST(APIntTest, getHighBitsSet) {
Index: llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
===================================================================
--- llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
+++ llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
@@ -906,11 +906,6 @@
         // while in PowerPC ISA, lowerest bit is at index 63.
         APInt MaskSrc =
             APInt::getBitsSetWithWrap(32, 32 - MESrc - 1, 32 - MBSrc);
-        // Current APInt::getBitsSetWithWrap sets all bits to 0 if loBit is
-        // equal to highBit.
-        // If MBSrc - MESrc == 1, we expect a full set mask instead of Null.
-        if (SrcMaskFull && (MBSrc - MESrc == 1))
-          MaskSrc.setAllBits();
 
         APInt RotatedSrcMask = MaskSrc.rotl(SHMI);
         APInt FinalMask = RotatedSrcMask & MaskMI;
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -616,9 +616,9 @@
   }
 
   /// Wrap version of getBitsSet.
-  /// If \p hiBit is no less than \p loBit, this is same with getBitsSet.
-  /// If \p hiBit is less than \p loBit, the set bits "wrap". For example, with
-  /// parameters (32, 28, 4), you would get 0xF000000F.
+  /// If \p hiBit is bigger than \p loBit, this is same with getBitsSet.
+  /// If \p hiBit is not bigger than \p loBit, the set bits "wrap". For example,
+  /// with parameters (32, 28, 4), you would get 0xF000000F.
   static APInt getBitsSetWithWrap(unsigned numBits, unsigned loBit,
                                   unsigned hiBit) {
     APInt Res(numBits, 0);
@@ -1448,14 +1448,18 @@
   }
 
   /// Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
-  /// This function handles "wrap" case when \p loBit > \p hiBit, and calls
-  /// setBits when \p loBit <= \p hiBit.
+  /// This function handles "wrap" case when \p loBit >= \p hiBit, and calls
+  /// setBits when \p loBit < \p hiBit.
   void setBitsWithWrap(unsigned loBit, unsigned hiBit) {
     assert(hiBit <= BitWidth && "hiBit out of range");
     assert(loBit <= BitWidth && "loBit out of range");
-    if (loBit <= hiBit) {
+    if (loBit < hiBit) {
       setBits(loBit, hiBit);
       return;
+    } else if (loBit == hiBit) {
+      // Get all bits set if loBit == hiBit for wrap case.
+      setAllBits();
+      return;
     }
     setLowBits(hiBit);
     setHighBits(BitWidth - loBit);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81325.268987.patch
Type: text/x-patch
Size: 3195 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200606/b65eb9a8/attachment.bin>


More information about the llvm-commits mailing list