[llvm] 18fa83e - ADT: Move some FPClassTest utility functions out of instcombine

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 3 14:52:06 PST 2023


Author: Matt Arsenault
Date: 2023-03-03T18:20:47-04:00
New Revision: 18fa83ed366930bc91a8b0f4f96e87536667c848

URL: https://github.com/llvm/llvm-project/commit/18fa83ed366930bc91a8b0f4f96e87536667c848
DIFF: https://github.com/llvm/llvm-project/commit/18fa83ed366930bc91a8b0f4f96e87536667c848.diff

LOG: ADT: Move some FPClassTest utility functions out of instcombine

Added: 
    

Modified: 
    llvm/include/llvm/ADT/FloatingPointMode.h
    llvm/lib/Support/FloatingPointMode.cpp
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/FloatingPointMode.h b/llvm/include/llvm/ADT/FloatingPointMode.h
index e6cc3023a88cf..dd7719b42c50d 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 72131d3348d78..9543884ff46ed 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 8b85141948109..916476a2d912e 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<ConstantInt>(Src1);
-  uint32_t Mask = CMask->getZExtValue();
+  FPClassTest Mask = static_cast<FPClassTest>(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);
   }
 


        


More information about the llvm-commits mailing list