From 18fa83ed366930bc91a8b0f4f96e87536667c848 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Thu, 2 Mar 2023 05:47:52 -0400 Subject: [PATCH] ADT: Move some FPClassTest utility functions out of instcombine --- llvm/include/llvm/ADT/FloatingPointMode.h | 9 ++++++ llvm/lib/Support/FloatingPointMode.cpp | 34 ++++++++++++++++++++ .../Transforms/InstCombine/InstCombineCalls.cpp | 36 +++------------------- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/llvm/include/llvm/ADT/FloatingPointMode.h b/llvm/include/llvm/ADT/FloatingPointMode.h index e6cc302..dd7719b 100644 --- a/llvm/include/llvm/ADT/FloatingPointMode.h +++ b/llvm/include/llvm/ADT/FloatingPointMode.h @@ -217,11 +217,20 @@ enum FPClassTest : unsigned { fcPosFinite = fcPosNormal | fcPosSubnormal | fcPosZero, fcNegFinite = fcNegNormal | fcNegSubnormal | fcNegZero, fcFinite = fcPosFinite | fcNegFinite, + fcPositive = fcPosFinite | fcPosInf, + fcNegative = fcNegFinite | fcNegInf, + fcAllFlags = fcNan | fcInf | fcFinite, }; 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); + /// 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 72131d3..9543884 100644 --- a/llvm/lib/Support/FloatingPointMode.cpp +++ b/llvm/lib/Support/FloatingPointMode.cpp @@ -11,6 +11,40 @@ using namespace llvm; +FPClassTest llvm::fneg(FPClassTest Mask) { + FPClassTest NewMask = Mask & fcNan; + if (Mask & fcNegInf) + NewMask |= fcPosInf; + if (Mask & fcNegNormal) + NewMask |= fcPosNormal; + if (Mask & fcNegSubnormal) + NewMask |= fcPosSubnormal; + if (Mask & fcNegZero) + NewMask |= fcPosZero; + if (Mask & fcPosZero) + NewMask |= fcNegZero; + if (Mask & fcPosSubnormal) + NewMask |= fcNegSubnormal; + if (Mask & fcPosNormal) + NewMask |= fcNegNormal; + if (Mask & fcPosInf) + NewMask |= fcNegInf; + return NewMask; +} + +FPClassTest llvm::fabs(FPClassTest Mask) { + FPClassTest NewMask = Mask & fcNan; + if (Mask & fcPosZero) + NewMask |= fcZero; + if (Mask & fcPosSubnormal) + NewMask |= fcSubnormal; + if (Mask & fcPosNormal) + NewMask |= fcNormal; + if (Mask & fcPosInf) + 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 8b85141..916476a 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -834,47 +834,21 @@ Instruction *InstCombinerImpl::foldIntrinsicIsFPClass(IntrinsicInst &II) { Value *Src0 = II.getArgOperand(0); Value *Src1 = II.getArgOperand(1); const ConstantInt *CMask = cast(Src1); - uint32_t Mask = CMask->getZExtValue(); + FPClassTest Mask = static_cast(CMask->getZExtValue()); + const bool IsStrict = II.isStrictFP(); Value *FNegSrc; if (match(Src0, m_FNeg(m_Value(FNegSrc)))) { // is.fpclass (fneg x), mask -> is.fpclass x, (fneg mask) - unsigned NewMask = Mask & fcNan; - if (Mask & fcNegInf) - NewMask |= fcPosInf; - if (Mask & fcNegNormal) - NewMask |= fcPosNormal; - if (Mask & fcNegSubnormal) - NewMask |= fcPosSubnormal; - if (Mask & fcNegZero) - NewMask |= fcPosZero; - if (Mask & fcPosZero) - NewMask |= fcNegZero; - if (Mask & fcPosSubnormal) - NewMask |= fcNegSubnormal; - if (Mask & fcPosNormal) - NewMask |= fcNegNormal; - if (Mask & fcPosInf) - NewMask |= fcNegInf; - - II.setArgOperand(1, ConstantInt::get(Src1->getType(), NewMask)); + + II.setArgOperand(1, ConstantInt::get(Src1->getType(), fneg(Mask))); return replaceOperand(II, 0, FNegSrc); } Value *FAbsSrc; if (match(Src0, m_FAbs(m_Value(FAbsSrc)))) { - unsigned NewMask = Mask & fcNan; - if (Mask & fcPosZero) - NewMask |= fcZero; - if (Mask & fcPosSubnormal) - NewMask |= fcSubnormal; - if (Mask & fcPosNormal) - NewMask |= fcNormal; - if (Mask & fcPosInf) - NewMask |= fcInf; - - II.setArgOperand(1, ConstantInt::get(Src1->getType(), NewMask)); + II.setArgOperand(1, ConstantInt::get(Src1->getType(), fabs(Mask))); return replaceOperand(II, 0, FAbsSrc); } -- 2.7.4