From 07acfe3a4d473fccbb2b027cc8c77aec25d1dbe3 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Thu, 14 Sep 2023 19:46:53 +0300 Subject: [PATCH] ADT: Replace FPClassTest fabs with inverse_fabs and unknown_sign (#66390) --- llvm/include/llvm/ADT/FloatingPointMode.h | 8 ++++++-- llvm/lib/Support/FloatingPointMode.cpp | 15 ++++++++++++++- .../Transforms/InstCombine/InstCombineCalls.cpp | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/ADT/FloatingPointMode.h b/llvm/include/llvm/ADT/FloatingPointMode.h index d80a811fd447..468de085d1be 100644 --- a/llvm/include/llvm/ADT/FloatingPointMode.h +++ b/llvm/include/llvm/ADT/FloatingPointMode.h @@ -269,8 +269,12 @@ LLVM_DECLARE_ENUM_AS_BITMASK(FPClassTest, /* LargestValue */ fcPosInf); /// Return the test mask which returns true if the value's sign bit is flipped. FPClassTest fneg(FPClassTest Mask); -/// Return the test mask which returns true if the value's sign bit is cleared. -FPClassTest fabs(FPClassTest Mask); +/// Return the test mask which returns true after fabs is applied to the value. +FPClassTest inverse_fabs(FPClassTest Mask); + +/// Return the test mask which returns true if the value could have the same set +/// of classes, but with a different sign. +FPClassTest unknown_sign(FPClassTest Mask); /// Write a human readable form of \p Mask to \p OS raw_ostream &operator<<(raw_ostream &OS, FPClassTest Mask); diff --git a/llvm/lib/Support/FloatingPointMode.cpp b/llvm/lib/Support/FloatingPointMode.cpp index 9543884ff46e..5a2836eb8243 100644 --- a/llvm/lib/Support/FloatingPointMode.cpp +++ b/llvm/lib/Support/FloatingPointMode.cpp @@ -32,7 +32,7 @@ FPClassTest llvm::fneg(FPClassTest Mask) { return NewMask; } -FPClassTest llvm::fabs(FPClassTest Mask) { +FPClassTest llvm::inverse_fabs(FPClassTest Mask) { FPClassTest NewMask = Mask & fcNan; if (Mask & fcPosZero) NewMask |= fcZero; @@ -45,6 +45,19 @@ FPClassTest llvm::fabs(FPClassTest Mask) { return NewMask; } +FPClassTest llvm::unknown_sign(FPClassTest Mask) { + FPClassTest NewMask = Mask & fcNan; + if (Mask & fcZero) + NewMask |= fcZero; + if (Mask & fcSubnormal) + NewMask |= fcSubnormal; + if (Mask & fcNormal) + NewMask |= fcNormal; + if (Mask & fcInf) + NewMask |= fcInf; + return NewMask; +} + // Every bitfield has a unique name and one or more aliasing names that cover // multiple bits. Names should be listed in order of preference, with higher // popcounts listed first. diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 17cfd90ac209..c6100f24b050 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -899,7 +899,7 @@ Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) { Value *FAbsSrc; if (match(Src0, m_FAbs(m_Value(FAbsSrc)))) { - II.setArgOperand(1, ConstantInt::get(Src1->getType(), fabs(Mask))); + II.setArgOperand(1, ConstantInt::get(Src1->getType(), inverse_fabs(Mask))); return replaceOperand(II, 0, FAbsSrc); }