[llvm] [ConstantFPRange] Add `getWithout[NaN|Inf]` (PR #162696)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 9 09:44:44 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-ir
Author: Yingwei Zheng (dtcxzyw)
<details>
<summary>Changes</summary>
This patch adds getWithoutNaN/getWithoutInf. We will apply nnan/ninf flags to the range of operands/results for a more precise range.
---
Full diff: https://github.com/llvm/llvm-project/pull/162696.diff
3 Files Affected:
- (modified) llvm/include/llvm/IR/ConstantFPRange.h (+10)
- (modified) llvm/lib/IR/ConstantFPRange.cpp (+14)
- (modified) llvm/unittests/IR/ConstantFPRangeTest.cpp (+16)
``````````diff
diff --git a/llvm/include/llvm/IR/ConstantFPRange.h b/llvm/include/llvm/IR/ConstantFPRange.h
index 930c6f98c033f..8d4fe349c20eb 100644
--- a/llvm/include/llvm/IR/ConstantFPRange.h
+++ b/llvm/include/llvm/IR/ConstantFPRange.h
@@ -200,6 +200,16 @@ class [[nodiscard]] ConstantFPRange {
/// with another range. The resultant range is guaranteed to include the
/// elements of both sets, but may contain more.
LLVM_ABI ConstantFPRange unionWith(const ConstantFPRange &CR) const;
+
+ /// Get the range without NaNs. It is useful when we apply nnan flag to range
+ /// of operands/results.
+ ConstantFPRange getWithoutNaN() const {
+ return ConstantFPRange(Lower, Upper, false, false);
+ }
+
+ /// Get the range without infinities. It is useful when we apply ninf flag to
+ /// range of operands/results.
+ LLVM_ABI ConstantFPRange getWithoutInf() const;
};
inline raw_ostream &operator<<(raw_ostream &OS, const ConstantFPRange &CR) {
diff --git a/llvm/lib/IR/ConstantFPRange.cpp b/llvm/lib/IR/ConstantFPRange.cpp
index 7509188128524..6a8260802ca12 100644
--- a/llvm/lib/IR/ConstantFPRange.cpp
+++ b/llvm/lib/IR/ConstantFPRange.cpp
@@ -391,3 +391,17 @@ ConstantFPRange ConstantFPRange::unionWith(const ConstantFPRange &CR) const {
return ConstantFPRange(minnum(Lower, CR.Lower), maxnum(Upper, CR.Upper),
MayBeQNaN | CR.MayBeQNaN, MayBeSNaN | CR.MayBeSNaN);
}
+
+ConstantFPRange ConstantFPRange::getWithoutInf() const {
+ if (isNaNOnly())
+ return *this;
+ APFloat NewLower = Lower;
+ APFloat NewUpper = Upper;
+ if (Lower.isNegInfinity())
+ NewLower = APFloat::getLargest(getSemantics(), /*Negative=*/true);
+ if (Upper.isPosInfinity())
+ NewUpper = APFloat::getLargest(getSemantics(), /*Negative=*/false);
+ canonicalizeRange(NewLower, NewUpper);
+ return ConstantFPRange(std::move(NewLower), std::move(NewUpper), MayBeQNaN,
+ MayBeSNaN);
+}
diff --git a/llvm/unittests/IR/ConstantFPRangeTest.cpp b/llvm/unittests/IR/ConstantFPRangeTest.cpp
index 255f62d77b748..620d7ae8b35de 100644
--- a/llvm/unittests/IR/ConstantFPRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantFPRangeTest.cpp
@@ -767,4 +767,20 @@ TEST_F(ConstantFPRangeTest, makeExactFCmpRegion) {
}
}
+TEST_F(ConstantFPRangeTest, getWithout) {
+ EXPECT_EQ(Full.getWithoutNaN(), ConstantFPRange::getNonNaN(Sem));
+ EXPECT_EQ(NaN.getWithoutNaN(), Empty);
+
+ EXPECT_EQ(NaN.getWithoutInf(), NaN);
+ EXPECT_EQ(PosInf.getWithoutInf(), Empty);
+ EXPECT_EQ(NegInf.getWithoutInf(), Empty);
+ EXPECT_EQ(ConstantFPRange::getNonNaN(Sem).getWithoutInf(), Finite);
+ EXPECT_EQ(Zero.getWithoutInf(), Zero);
+ EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat::getInf(Sem, /*Negative=*/true),
+ APFloat(3.0))
+ .getWithoutInf(),
+ ConstantFPRange::getNonNaN(
+ APFloat::getLargest(Sem, /*Negative=*/true), APFloat(3.0)));
+}
+
} // anonymous namespace
``````````
</details>
https://github.com/llvm/llvm-project/pull/162696
More information about the llvm-commits
mailing list