Skip to content

ConstantRange: add query for isAllPositive #97420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/IR/ConstantRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ class [[nodiscard]] ConstantRange {
/// Return true if all values in this range are non-negative.
bool isAllNonNegative() const;

/// Return true if all values in this range are positive.
bool isAllPositive() const;

/// Return the largest unsigned value contained in the ConstantRange.
APInt getUnsignedMax() const;

Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/IR/ConstantRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ bool ConstantRange::isAllNonNegative() const {
return !isSignWrappedSet() && Lower.isNonNegative();
}

bool ConstantRange::isAllPositive() const {
// Empty set is all positive, full set is not.
if (isEmptySet())
return true;
if (isFullSet())
return false;

return !isSignWrappedSet() && Lower.isStrictlyPositive();
}

APInt ConstantRange::getUnsignedMax() const {
if (isFullSet() || isUpperWrapped())
return APInt::getMaxValue(getBitWidth());
Expand Down
12 changes: 10 additions & 2 deletions llvm/unittests/IR/ConstantRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2398,23 +2398,31 @@ TEST_F(ConstantRangeTest, Negative) {
// they are also covered by the exhaustive test below.
EXPECT_TRUE(Empty.isAllNegative());
EXPECT_TRUE(Empty.isAllNonNegative());
EXPECT_TRUE(Empty.isAllPositive());
EXPECT_FALSE(Full.isAllNegative());
EXPECT_FALSE(Full.isAllNonNegative());
EXPECT_FALSE(Full.isAllPositive());

EnumerateInterestingConstantRanges([](const ConstantRange &CR) {
bool AllNegative = true;
bool AllNonNegative = true;
bool AllPositive = true;
ForeachNumInConstantRange(CR, [&](const APInt &N) {
if (!N.isNegative())
AllNegative = false;
if (!N.isNonNegative())
AllNonNegative = false;
if (!N.isStrictlyPositive())
AllPositive = false;
});
assert((CR.isEmptySet() || !AllNegative || !AllNonNegative) &&
"Only empty set can be both all negative and all non-negative");
assert(
(CR.isEmptySet() || !AllNegative || !AllNonNegative || !AllPositive) &&
"Only empty set can be all negative, all non-negative, and all "
"positive");

EXPECT_EQ(AllNegative, CR.isAllNegative());
EXPECT_EQ(AllNonNegative, CR.isAllNonNegative());
EXPECT_EQ(AllPositive, CR.isAllPositive());
});
}

Expand Down
Loading