Skip to content
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
10 changes: 10 additions & 0 deletions llvm/include/llvm/IR/ConstantFPRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ class [[nodiscard]] ConstantFPRange {

/// Calculate range of negated values.
LLVM_ABI ConstantFPRange negate() 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) {
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/IR/ConstantFPRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,17 @@ ConstantFPRange ConstantFPRange::abs() const {
ConstantFPRange ConstantFPRange::negate() const {
return ConstantFPRange(-Upper, -Lower, MayBeQNaN, 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);
}
16 changes: 16 additions & 0 deletions llvm/unittests/IR/ConstantFPRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,4 +802,20 @@ TEST_F(ConstantFPRangeTest, negate) {
ConstantFPRange::getNonNaN(APFloat(-2.0), APFloat(3.0)));
}

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
Loading