[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 15:40:19 PST 2020


fhahn created this revision.
fhahn added reviewers: efriedma, davide, nikic.
Herald added a project: LLVM.

Currently we always return true, when markConstantRange is used on an
object already containing a constant range. If NewR is a subset of the
existing constant range however, nothing changes and we should return
false.

I also went ahead and added a clarifying comment and improved the
assertion.


Repository:
  rG LLVM Github Monorepo

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,27 @@
   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 subset of existing range [10, 2)).
+  EXPECT_FALSE(
+      LV1.markConstantRange({APInt(32, 10, true), APInt(32, 20, true)}));
+  EXPECT_FALSE(
+      LV1.markConstantRange({APInt(32, 14, true), APInt(32, 20, true)}));
+  EXPECT_FALSE(
+      LV1.markConstantRange({APInt(32, 14, true), APInt(32, 19, 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 already contains \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().contains(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.239723.patch
Type: text/x-patch
Size: 2522 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200122/f2b8c3c4/attachment.bin>


More information about the llvm-commits mailing list