diff --git a/llvm/include/llvm/Analysis/ValueLattice.h b/llvm/include/llvm/Analysis/ValueLattice.h --- a/llvm/include/llvm/Analysis/ValueLattice.h +++ b/llvm/include/llvm/Analysis/ValueLattice.h @@ -220,14 +220,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(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 --- a/llvm/unittests/Analysis/ValueLatticeTest.cpp +++ b/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);