[PATCH] D57983: [ConstantRange] Shl of ConstantRanges considers full-set shifting to last bit position

Marcello Maggioni via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 8 15:32:52 PST 2019


kariddi created this revision.
kariddi added reviewers: craig.topper, nlopes, reames.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Hello, I'm not sure here what I did is correct , but I noticed that if we do SHL of two 16-bit ranges like [0, 30000) with [1,2) we get "full-set" instead of what I would have expected [0, 60000) which is still in the 16-bit unsigned range.

In the ConstantRange SHL code I noticed that we compare if we are shifting out of the range with "uge" which considers "0x7FFFU << 1" out of range even if the result "0xFFFE" would still be in range.

Now, maybe this is done for being conservative with respect to signed ranges? Is that the reason? With this question I posted this patch with a possible fix if that is not the case.


Repository:
  rL LLVM

https://reviews.llvm.org/D57983

Files:
  lib/IR/ConstantRange.cpp
  unittests/IR/ConstantRangeTest.cpp


Index: unittests/IR/ConstantRangeTest.cpp
===================================================================
--- unittests/IR/ConstantRangeTest.cpp
+++ unittests/IR/ConstantRangeTest.cpp
@@ -21,6 +21,7 @@
   static ConstantRange Empty;
   static ConstantRange One;
   static ConstantRange Some;
+  static ConstantRange Some2;
   static ConstantRange Wrap;
 };
 
@@ -28,6 +29,7 @@
 ConstantRange ConstantRangeTest::Empty(16, false);
 ConstantRange ConstantRangeTest::One(APInt(16, 0xa));
 ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa));
+ConstantRange ConstantRangeTest::Some2(APInt(16, 0xfff), APInt(16, 0x8000));
 ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa));
 
 TEST_F(ConstantRangeTest, Basics) {
@@ -582,6 +584,8 @@
   EXPECT_EQ(Some.shl(Some), Full);   // TODO: [0xa << 0xa, 0xfc01)
   EXPECT_EQ(Some.shl(Wrap), Full);   // TODO: [0xa, 0x7ff << 0x5 + 1)
   EXPECT_EQ(Wrap.shl(Wrap), Full);
+  EXPECT_EQ(Some2.shl(ConstantRange(APInt(16, 0x1))),
+    ConstantRange(APInt(16, 0xfff << 0x1), APInt(16, 0x7fff << 0x1) + 1));
 }
 
 TEST_F(ConstantRangeTest, Lshr) {
Index: lib/IR/ConstantRange.cpp
===================================================================
--- lib/IR/ConstantRange.cpp
+++ lib/IR/ConstantRange.cpp
@@ -979,7 +979,7 @@
   APInt Other_umax = Other.getUnsignedMax();
 
   // there's overflow!
-  if (Other_umax.uge(max.countLeadingZeros()))
+  if (Other_umax.ugt(max.countLeadingZeros()))
     return ConstantRange(getBitWidth(), /*isFullSet=*/true);
 
   // FIXME: implement the other tricky cases


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57983.186058.patch
Type: text/x-patch
Size: 1575 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190208/9e86c7db/attachment.bin>


More information about the llvm-commits mailing list