ADT: Move some FPClassTest utility functions out of instcombine
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Thu, 2 Mar 2023 09:47:52 +0000 (05:47 -0400)
committerMatt Arsenault <arsenm2@gmail.com>
Fri, 3 Mar 2023 22:20:47 +0000 (18:20 -0400)
llvm/include/llvm/ADT/FloatingPointMode.h
llvm/lib/Support/FloatingPointMode.cpp
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

index e6cc302..dd7719b 100644 (file)
@@ -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);
 
index 72131d3..9543884 100644 (file)
 
 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.
index 8b85141..916476a 100644 (file)
@@ -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);
   }