[llvm] [SimplfiyLibCalls] Constant fold `remquo` (PR #99647)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 19 07:42:24 PDT 2024


================
@@ -3018,6 +3018,35 @@ void LibCallSimplifier::classifyArgUse(
   }
 }
 
+/// Constant folds remquo
+Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
+  const APFloat *X, *Y;
+  if (!match(CI->getArgOperand(0), m_APFloat(X)) || !match(CI->getArgOperand(1), m_APFloat(Y)))
+    return nullptr;
+
+  if (X->isNaN() || Y->isNaN() || X->isInfinity() || Y->isZero())
+    return nullptr;
+
+  APFloat::opStatus Status;
+  APFloat Quot = *X;
+  Status = Quot.divide(*Y, APFloat::rmNearestTiesToEven);
+  if (Status != APFloat::opOK && Status != APFloat::opInexact)
+    return nullptr;
+  APFloat Rem = *X;
+  if (Rem.remainder(*Y) != APFloat::opOK)
+    return nullptr;
+
+  // TODO: We can only keep at least the three of the last bits of x/y
+  APSInt QuotInt(32, /*isUnsigned=*/false);
+  bool IsExact;
+  Status = Quot.convertToInteger(QuotInt, APFloat::rmNearestTiesToEven, &IsExact);
+  if (Status != APFloat::opOK || Status != APFloat::opInexact)
+    return nullptr;
----------------
dtcxzyw wrote:

See https://github.com/llvm/llvm-project/pull/99647#discussion_r1684373367. The current implementation doesn't match the standard behavior. I don't think it's appropriate to move these logic into APFloat.


https://github.com/llvm/llvm-project/pull/99647


More information about the llvm-commits mailing list