Skip to content

Commit d7bd3ea

Browse files
artagnonkbluck
authored andcommitted
ConstantRange: add query for isAllPositive (llvm#97420)
ConstantRange has queries for isAllNegative and isAllNonNegative, but misses a query for isAllPositive. Add this function.
1 parent 919fbda commit d7bd3ea

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

llvm/include/llvm/IR/ConstantRange.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ class [[nodiscard]] ConstantRange {
277277
/// Return true if all values in this range are non-negative.
278278
bool isAllNonNegative() const;
279279

280+
/// Return true if all values in this range are positive.
281+
bool isAllPositive() const;
282+
280283
/// Return the largest unsigned value contained in the ConstantRange.
281284
APInt getUnsignedMax() const;
282285

llvm/lib/IR/ConstantRange.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,16 @@ bool ConstantRange::isAllNonNegative() const {
440440
return !isSignWrappedSet() && Lower.isNonNegative();
441441
}
442442

443+
bool ConstantRange::isAllPositive() const {
444+
// Empty set is all positive, full set is not.
445+
if (isEmptySet())
446+
return true;
447+
if (isFullSet())
448+
return false;
449+
450+
return !isSignWrappedSet() && Lower.isStrictlyPositive();
451+
}
452+
443453
APInt ConstantRange::getUnsignedMax() const {
444454
if (isFullSet() || isUpperWrapped())
445455
return APInt::getMaxValue(getBitWidth());

llvm/unittests/IR/ConstantRangeTest.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,23 +2398,31 @@ TEST_F(ConstantRangeTest, Negative) {
23982398
// they are also covered by the exhaustive test below.
23992399
EXPECT_TRUE(Empty.isAllNegative());
24002400
EXPECT_TRUE(Empty.isAllNonNegative());
2401+
EXPECT_TRUE(Empty.isAllPositive());
24012402
EXPECT_FALSE(Full.isAllNegative());
24022403
EXPECT_FALSE(Full.isAllNonNegative());
2404+
EXPECT_FALSE(Full.isAllPositive());
24032405

24042406
EnumerateInterestingConstantRanges([](const ConstantRange &CR) {
24052407
bool AllNegative = true;
24062408
bool AllNonNegative = true;
2409+
bool AllPositive = true;
24072410
ForeachNumInConstantRange(CR, [&](const APInt &N) {
24082411
if (!N.isNegative())
24092412
AllNegative = false;
24102413
if (!N.isNonNegative())
24112414
AllNonNegative = false;
2415+
if (!N.isStrictlyPositive())
2416+
AllPositive = false;
24122417
});
2413-
assert((CR.isEmptySet() || !AllNegative || !AllNonNegative) &&
2414-
"Only empty set can be both all negative and all non-negative");
2418+
assert(
2419+
(CR.isEmptySet() || !AllNegative || !AllNonNegative || !AllPositive) &&
2420+
"Only empty set can be all negative, all non-negative, and all "
2421+
"positive");
24152422

24162423
EXPECT_EQ(AllNegative, CR.isAllNegative());
24172424
EXPECT_EQ(AllNonNegative, CR.isAllNonNegative());
2425+
EXPECT_EQ(AllPositive, CR.isAllPositive());
24182426
});
24192427
}
24202428

0 commit comments

Comments
 (0)