[llvm] ConstantRange: add query for isAllPositive (PR #97420)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 2 07:03:56 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Ramkumar Ramachandra (artagnon)
<details>
<summary>Changes</summary>
ConstantRange has queries for isAllNegative and isAllNonNegative, but misses a query for isAllPositive. Add this function.
---
Full diff: https://github.com/llvm/llvm-project/pull/97420.diff
3 Files Affected:
- (modified) llvm/include/llvm/IR/ConstantRange.h (+3)
- (modified) llvm/lib/IR/ConstantRange.cpp (+10)
- (modified) llvm/unittests/IR/ConstantRangeTest.cpp (+10-2)
``````````diff
diff --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h
index 7b94b9c6c6d11..86d0a6b35d748 100644
--- a/llvm/include/llvm/IR/ConstantRange.h
+++ b/llvm/include/llvm/IR/ConstantRange.h
@@ -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;
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 19041704a40be..50de975d83c0a 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -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());
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index 392c41f74b431..0181e2ce6ac92 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -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());
});
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/97420
More information about the llvm-commits
mailing list