[PATCH] D73240: [ValueLattice] Update markConstantRange to return false for subset ranges.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 22 19:03:48 PST 2020


fhahn updated this revision to Diff 239765.
fhahn added a comment.

Restrict input for markConstantRange to either matching the existing constant range or being a superset.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73240/new/

https://reviews.llvm.org/D73240

Files:
  llvm/include/llvm/Analysis/ValueLattice.h
  llvm/unittests/Analysis/ValueLatticeTest.cpp


Index: llvm/unittests/Analysis/ValueLatticeTest.cpp
===================================================================
--- llvm/unittests/Analysis/ValueLatticeTest.cpp
+++ llvm/unittests/Analysis/ValueLatticeTest.cpp
@@ -43,6 +43,23 @@
   EXPECT_TRUE(ValueLatticeElement::getNot(C2).isNotConstant());
 }
 
+TEST_F(ValueLatticeTest, MarkConstantRange) {
+  auto LV1 =
+      ValueLatticeElement::getRange({APInt(32, 10, true), APInt(32, 20, true)});
+
+  // Test markConstantRange() with an equal range.
+  EXPECT_FALSE(
+      LV1.markConstantRange({APInt(32, 10, true), APInt(32, 20, true)}));
+
+  // Test markConstantRange() with supersets of existing range.
+  EXPECT_TRUE(LV1.markConstantRange({APInt(32, 5, true), APInt(32, 20, true)}));
+  EXPECT_EQ(LV1.getConstantRange().getLower().getLimitedValue(), 5U);
+  EXPECT_EQ(LV1.getConstantRange().getUpper().getLimitedValue(), 20U);
+  EXPECT_TRUE(LV1.markConstantRange({APInt(32, 5, true), APInt(32, 23, true)}));
+  EXPECT_EQ(LV1.getConstantRange().getLower().getLimitedValue(), 5U);
+  EXPECT_EQ(LV1.getConstantRange().getUpper().getLimitedValue(), 23U);
+}
+
 TEST_F(ValueLatticeTest, MergeIn) {
   auto I32Ty = IntegerType::get(Context, 32);
   auto *C1 = ConstantInt::get(I32Ty, 1);
Index: llvm/include/llvm/Analysis/ValueLattice.h
===================================================================
--- llvm/include/llvm/Analysis/ValueLattice.h
+++ llvm/include/llvm/Analysis/ValueLattice.h
@@ -243,14 +243,21 @@
     return true;
   }
 
+  /// Mark the object as constant range with \p NewR. If the object is already a
+  /// constant range, nothing changes if the existing range is equal to \p
+  /// NewR. Otherwise \p NewR must be a superset of the existing range or the
+  /// object must be undefined.
   bool markConstantRange(ConstantRange NewR) {
     if (isConstantRange()) {
+      if (getConstantRange() == NewR)
+        return false;
+
       if (NewR.isEmptySet())
-        markOverdefined();
-      else {
-        assert(getConstantRange().difference(NewR).isEmptySet());
-        Range = std::move(NewR);
-      }
+        return markOverdefined();
+
+      assert(NewR.contains(getConstantRange()) &&
+             "NewR must be a superset of the existing range");
+      Range = std::move(NewR);
       return true;
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73240.239765.patch
Type: text/x-patch
Size: 2307 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200123/050e585f/attachment.bin>


More information about the llvm-commits mailing list