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
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/ConstantFPRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ 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;

/// Calculate absolute value range.
LLVM_ABI ConstantFPRange abs() const;

/// Calculate range of negated values.
LLVM_ABI ConstantFPRange negate() const;
};

inline raw_ostream &operator<<(raw_ostream &OS, const ConstantFPRange &CR) {
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/IR/ConstantFPRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,23 @@ 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::abs() const {
if (isNaNOnly())
return *this;
// Check if the range is all non-negative or all non-positive.
if (Lower.isNegative() == Upper.isNegative()) {
if (Lower.isNegative())
return negate();
return *this;
}
Comment on lines +399 to +403
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler:

Suggested change
if (Lower.isNegative() == Upper.isNegative()) {
if (Lower.isNegative())
return negate();
return *this;
}
if (!Lower.isNegative())
return *this;
if (Upper.isNegative())
return negate();

// The range contains both positive and negative values.
APFloat NewLower = APFloat::getZero(getSemantics());
APFloat NewUpper = maxnum(-Lower, Upper);
return ConstantFPRange(std::move(NewLower), std::move(NewUpper), MayBeQNaN,
MayBeSNaN);
}

ConstantFPRange ConstantFPRange::negate() const {
return ConstantFPRange(-Upper, -Lower, MayBeQNaN, MayBeSNaN);
}
35 changes: 35 additions & 0 deletions llvm/unittests/IR/ConstantFPRangeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,39 @@ TEST_F(ConstantFPRangeTest, makeExactFCmpRegion) {
}
}

TEST_F(ConstantFPRangeTest, abs) {
EXPECT_EQ(Full.abs(),
ConstantFPRange(APFloat::getZero(Sem, /*Negative=*/false),
APFloat::getInf(Sem, /*Negative=*/false),
/*MayBeQNaN=*/true,
/*MayBeSNaN=*/true));
EXPECT_EQ(Empty.abs(), Empty);
EXPECT_EQ(Zero.abs(), PosZero);
EXPECT_EQ(PosInf.abs(), PosInf);
EXPECT_EQ(NegInf.abs(), PosInf);
EXPECT_EQ(Some.abs(), SomePos);
EXPECT_EQ(SomeNeg.abs(), SomePos);
EXPECT_EQ(NaN.abs(), NaN);
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(-2.0), APFloat(3.0)).abs(),
ConstantFPRange::getNonNaN(APFloat(0.0), APFloat(3.0)));
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(-3.0), APFloat(2.0)).abs(),
ConstantFPRange::getNonNaN(APFloat(0.0), APFloat(3.0)));
}

TEST_F(ConstantFPRangeTest, negate) {
EXPECT_EQ(Full.negate(), Full);
EXPECT_EQ(Empty.negate(), Empty);
EXPECT_EQ(Zero.negate(), Zero);
EXPECT_EQ(PosInf.negate(), NegInf);
EXPECT_EQ(NegInf.negate(), PosInf);
EXPECT_EQ(Some.negate(), Some);
EXPECT_EQ(SomePos.negate(), SomeNeg);
EXPECT_EQ(SomeNeg.negate(), SomePos);
EXPECT_EQ(NaN.negate(), NaN);
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(-2.0), APFloat(3.0)).negate(),
ConstantFPRange::getNonNaN(APFloat(-3.0), APFloat(2.0)));
EXPECT_EQ(ConstantFPRange::getNonNaN(APFloat(-3.0), APFloat(2.0)).negate(),
ConstantFPRange::getNonNaN(APFloat(-2.0), APFloat(3.0)));
}

} // anonymous namespace