[llvm] cf71a5e - [ConstantRange] Support zero size in isSizeLargerThan()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 7 12:22:52 PST 2021


Author: Nikita Popov
Date: 2021-11-07T21:22:45+01:00
New Revision: cf71a5ea8f95be423ebd381d21d0f9a05edc5018

URL: https://github.com/llvm/llvm-project/commit/cf71a5ea8f95be423ebd381d21d0f9a05edc5018
DIFF: https://github.com/llvm/llvm-project/commit/cf71a5ea8f95be423ebd381d21d0f9a05edc5018.diff

LOG: [ConstantRange] Support zero size in isSizeLargerThan()

>From an API perspective, it does not make a lot of sense that 0
is not a valid argument to this function. Add the exact check needed
to support it.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantRange.cpp
    llvm/unittests/IR/ConstantRangeTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index b01f5cf60c3c6..bbb07bfe3172d 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -381,11 +381,10 @@ ConstantRange::isSizeStrictlySmallerThan(const ConstantRange &Other) const {
 
 bool
 ConstantRange::isSizeLargerThan(uint64_t MaxSize) const {
-  assert(MaxSize && "MaxSize can't be 0.");
   // If this a full set, we need special handling to avoid needing an extra bit
   // to represent the size.
   if (isFullSet())
-    return APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
+    return MaxSize == 0 || APInt::getMaxValue(getBitWidth()).ugt(MaxSize - 1);
 
   return (Upper - Lower).ugt(MaxSize);
 }

diff  --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index ac5c4455b526d..2de1ae73d9dff 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -2594,4 +2594,15 @@ TEST_F(ConstantRangeTest, getEquivalentPredWithFlippedSignedness) {
   }
 }
 
+TEST_F(ConstantRangeTest, isSizeLargerThan) {
+  EXPECT_FALSE(Empty.isSizeLargerThan(0));
+
+  EXPECT_TRUE(Full.isSizeLargerThan(0));
+  EXPECT_TRUE(Full.isSizeLargerThan(65535));
+  EXPECT_FALSE(Full.isSizeLargerThan(65536));
+
+  EXPECT_TRUE(One.isSizeLargerThan(0));
+  EXPECT_FALSE(One.isSizeLargerThan(1));
+}
+
 } // anonymous namespace


        


More information about the llvm-commits mailing list