[PATCH] D69032: [APInt] add wrapping support for APInt::setBits

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 16 05:49:35 PDT 2019


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

This was supportted before https://reviews.llvm.org/rL301769.

In rL301769 <https://reviews.llvm.org/rL301769>, It removed this support for no uses for this feature. 
But `getBitsSet` which calls `setBits` still comment `If hiBit is less than loBit then the set bits "wrap". `. This is a little confusing.

I add this feature back because in PowerPC, I need it to combine two `PPC::RLWINM` which will be posted later. In ` PPC::RLWINM` wrapping set bits is supportted.

It was suggested to add another function `setBitsWithWrap` in rL301769 <https://reviews.llvm.org/rL301769> for this new feature. but I am thinking using a unified interface like previous may be better. So I use old implementation. Same thing happens for `ConstantRange::add` and `ConstantRange::addWithNoWrap`, there is a plan to merge this two interfaces into one.


https://reviews.llvm.org/D69032

Files:
  llvm/include/llvm/ADT/APInt.h
  llvm/unittests/ADT/APIntTest.cpp


Index: llvm/unittests/ADT/APIntTest.cpp
===================================================================
--- llvm/unittests/ADT/APIntTest.cpp
+++ llvm/unittests/ADT/APIntTest.cpp
@@ -1907,6 +1907,22 @@
   EXPECT_EQ(1u, i127hi1lo1.countTrailingZeros());
   EXPECT_EQ(0u, i127hi1lo1.countTrailingOnes());
   EXPECT_EQ(125u, i127hi1lo1.countPopulation());
+
+  APInt i64hi1lo1wrap = APInt::getBitsSet(64, 63, 1);
+  EXPECT_EQ(1u, i64hi1lo1wrap.countLeadingOnes());
+  EXPECT_EQ(0u, i64hi1lo1wrap.countLeadingZeros());
+  EXPECT_EQ(64u, i64hi1lo1wrap.getActiveBits());
+  EXPECT_EQ(0u, i64hi1lo1wrap.countTrailingZeros());
+  EXPECT_EQ(1u, i64hi1lo1wrap.countTrailingOnes());
+  EXPECT_EQ(2u, i64hi1lo1wrap.countPopulation());
+
+  APInt i127hi1lo1wrap = APInt::getBitsSet(127, 126, 1);
+  EXPECT_EQ(1u, i127hi1lo1wrap.countLeadingOnes());
+  EXPECT_EQ(0u, i127hi1lo1wrap.countLeadingZeros());
+  EXPECT_EQ(127u, i127hi1lo1wrap.getActiveBits());
+  EXPECT_EQ(0u, i127hi1lo1wrap.countTrailingZeros());
+  EXPECT_EQ(1u, i127hi1lo1wrap.countTrailingOnes());
+  EXPECT_EQ(2u, i127hi1lo1wrap.countPopulation());
 }
 
 TEST(APIntTest, getHighBitsSet) {
Index: llvm/include/llvm/ADT/APInt.h
===================================================================
--- llvm/include/llvm/ADT/APInt.h
+++ llvm/include/llvm/ADT/APInt.h
@@ -1417,9 +1417,13 @@
   void setBits(unsigned loBit, unsigned hiBit) {
     assert(hiBit <= BitWidth && "hiBit out of range");
     assert(loBit <= BitWidth && "loBit out of range");
-    assert(loBit <= hiBit && "loBit greater than hiBit");
     if (loBit == hiBit)
       return;
+    if (loBit > hiBit) {
+      setLowBits(hiBit);
+      setHighBits(BitWidth - loBit);
+      return;
+    }
     if (loBit < APINT_BITS_PER_WORD && hiBit <= APINT_BITS_PER_WORD) {
       uint64_t mask = WORDTYPE_MAX >> (APINT_BITS_PER_WORD - (hiBit - loBit));
       mask <<= loBit;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69032.225202.patch
Type: text/x-patch
Size: 1890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191016/a544d590/attachment.bin>


More information about the llvm-commits mailing list