Index: llvm/include/llvm/IR/ConstantRange.h =================================================================== --- llvm/include/llvm/IR/ConstantRange.h +++ llvm/include/llvm/IR/ConstantRange.h @@ -219,9 +219,15 @@ /// Compare set size of this range with the range CR. bool isSizeStrictlySmallerThan(const ConstantRange &CR) const; - // Compare set size of this range with Value. + /// Compare set size of this range with Value. bool isSizeLargerThan(uint64_t MaxSize) const; + /// Return true if all values in this range are negative. + bool isNegative() const; + + /// Return true if all values in this range are non-negative. + bool isNonNegative() const; + /// Return the largest unsigned value contained in the ConstantRange. APInt getUnsignedMax() const; Index: llvm/lib/IR/ConstantRange.cpp =================================================================== --- llvm/lib/IR/ConstantRange.cpp +++ llvm/lib/IR/ConstantRange.cpp @@ -389,6 +389,19 @@ return (Upper - Lower).ugt(MaxSize); } +bool ConstantRange::isNegative() const { + // Empty set is all negative, full set is not. + if (Lower == Upper) + return Lower.isNullValue(); + + return !isUpperSignWrapped() && !Upper.isStrictlyPositive(); +} + +bool ConstantRange::isNonNegative() const { + // Empty and full set are automatically treated correctly. + return !isSignWrappedSet() && Lower.isNonNegative(); +} + APInt ConstantRange::getUnsignedMax() const { if (isFullSet() || isUpperWrapped()) return APInt::getMaxValue(getBitWidth()); Index: llvm/unittests/IR/ConstantRangeTest.cpp =================================================================== --- llvm/unittests/IR/ConstantRangeTest.cpp +++ llvm/unittests/IR/ConstantRangeTest.cpp @@ -26,29 +26,39 @@ }; template -static void EnumerateTwoConstantRanges(unsigned Bits, Fn TestFn) { +static void EnumerateConstantRanges(unsigned Bits, Fn TestFn) { unsigned Max = 1 << Bits; - for (unsigned Lo1 = 0; Lo1 < Max; Lo1++) { - for (unsigned Hi1 = 0; Hi1 < Max; Hi1++) { + for (unsigned Lo = 0; Lo < Max; Lo++) { + for (unsigned Hi = 0; Hi < Max; Hi++) { // Enforce ConstantRange invariant. - if (Lo1 == Hi1 && Lo1 != 0 && Lo1 != Max - 1) + if (Lo == Hi && Lo != 0 && Lo != Max - 1) continue; - ConstantRange CR1(APInt(Bits, Lo1), APInt(Bits, Hi1)); - for (unsigned Lo2 = 0; Lo2 < Max; Lo2++) { - for (unsigned Hi2 = 0; Hi2 < Max; Hi2++) { - // Enforce ConstantRange invariant. - if (Lo2 == Hi2 && Lo2 != 0 && Lo2 != Max - 1) - continue; - - ConstantRange CR2(APInt(Bits, Lo2), APInt(Bits, Hi2)); - TestFn(CR1, CR2); - } - } + ConstantRange CR(APInt(Bits, Lo), APInt(Bits, Hi)); + TestFn(CR); } } } +template +static void EnumerateTwoConstantRanges(unsigned Bits, Fn TestFn) { + EnumerateConstantRanges(Bits, [&](const ConstantRange &CR1) { + EnumerateConstantRanges(Bits, [&](const ConstantRange &CR2) { + TestFn(CR1, CR2); + }); + }); +} + +template +static void ForeachNumInConstantRange(const ConstantRange &CR, Fn TestFn) { + // The loop is based on the set size to correctly handle empty/full ranges. + unsigned Size = CR.getSetSize().getLimitedValue(); + APInt N = CR.getLower(); + for (unsigned I = 0; I < Size; ++I, ++N) { + TestFn(N); + } +} + ConstantRange ConstantRangeTest::Full(16, true); ConstantRange ConstantRangeTest::Empty(16, false); ConstantRange ConstantRangeTest::One(APInt(16, 0xa)); @@ -1630,4 +1640,25 @@ } } +TEST_F(ConstantRangeTest, Negative) { + // All elements in an empty set (of which there are none) are both negative + // and non-negative. Checked explicitly for clarity, but also covered below. + EXPECT_TRUE(Empty.isNegative()); + EXPECT_TRUE(Empty.isNonNegative()); + + unsigned Bits = 4; + EnumerateConstantRanges(Bits, [](const ConstantRange &CR) { + bool AllNegative = true; + bool AllNonNegative = true; + ForeachNumInConstantRange(CR, [&](const APInt &N) { + if (!N.isNegative()) + AllNegative = false; + if (!N.isNonNegative()) + AllNonNegative = false; + }); + EXPECT_EQ(AllNegative, CR.isNegative()); + EXPECT_EQ(AllNonNegative, CR.isNonNegative()); + }); +} + } // anonymous namespace