[llvm] [InstCombine] Fold fmod to frem if we know it does not set errno. (PR #107912)

David Green via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 12 13:19:23 PDT 2024


================
@@ -2796,6 +2796,25 @@ Value *LibCallSimplifier::optimizeSqrt(CallInst *CI, IRBuilderBase &B) {
   return copyFlags(*CI, FabsCall);
 }
 
+Value *LibCallSimplifier::optimizeFMod(CallInst *CI, IRBuilderBase &B) {
+  SimplifyQuery SQ(DL, TLI, DT, AC, CI, true, true, DC);
+
+  // fmod(x,y) can set errno if y == 0 or x == +/-inf.
+  KnownFPClass Known0 = computeKnownFPClass(CI->getOperand(0), fcInf,
+                                            /*Depth=*/0, SQ);
+  if (Known0.isKnownNeverInfinity()) {
+    KnownFPClass Known1 = computeKnownFPClass(CI->getOperand(1), fcZero,
+                                              /*Depth=*/0, SQ);
+    if (Known1.isKnownNeverZero()) {
----------------
davemgreen wrote:

Thanks - my testing (https://gist.github.com/davemgreen/f6b084618c24db2b0996cc618cd8ca05) was on a normal linux system. There is a test in the end of ad3ad15229cc65874eae173137e47b5c817d77ea, and I've changed it to isKnownNeverLogicalZero.

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


More information about the llvm-commits mailing list