[llvm] c1943b4 - [ValueLattice] Update markConstantRange to return false equal ranges.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 15 13:07:55 PST 2020


Author: Florian Hahn
Date: 2020-02-15T22:06:55+01:00
New Revision: c1943b42c5b7feff5d0e0c1358d02889e2be165f

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

LOG: [ValueLattice] Update markConstantRange to return false equal ranges.

Currently we always return true, when markConstantRange is used on an
object already containing a constant range. If NewR is equal to 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.

Reviewers: efriedma, davide, nikic

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D73240

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/ValueLattice.h b/llvm/include/llvm/Analysis/ValueLattice.h
index b3bf6b571aaa..a43231ae2d3e 100644
--- a/llvm/include/llvm/Analysis/ValueLattice.h
+++ b/llvm/include/llvm/Analysis/ValueLattice.h
@@ -220,14 +220,21 @@ class ValueLatticeElement {
     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(NewR.contains(getConstantRange()) && "Existing range must be a subset of NewR");
-        Range = std::move(NewR);
-      }
+        return markOverdefined();
+
+      assert(NewR.contains(getConstantRange()) &&
+             "Existing range must be a subset of NewR");
+      Range = std::move(NewR);
       return true;
     }
 

diff  --git a/llvm/unittests/Analysis/ValueLatticeTest.cpp b/llvm/unittests/Analysis/ValueLatticeTest.cpp
index 844254ee32f2..10477ca0b574 100644
--- a/llvm/unittests/Analysis/ValueLatticeTest.cpp
+++ b/llvm/unittests/Analysis/ValueLatticeTest.cpp
@@ -43,6 +43,23 @@ TEST_F(ValueLatticeTest, ValueLatticeGetters) {
   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);


        


More information about the llvm-commits mailing list