[llvm] [SimplifyLibCalls] fdim constant fold (PR #109235)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 25 06:23:39 PDT 2024


https://github.com/braw-lee updated https://github.com/llvm/llvm-project/pull/109235

>From 8f502a76e06d9f3d8cf524be88e15d2d3f4545b5 Mon Sep 17 00:00:00 2001
From: Kushal Pal <kushalpal109 at gmail.com>
Date: Sun, 15 Sep 2024 17:19:38 +0530
Subject: [PATCH] [SimplifyLibCalls] Constant fold `fdim`

Signed-off-by: Kushal Pal <kushalpal109 at gmail.com>
---
 .../llvm/Transforms/Utils/SimplifyLibCalls.h  |  1 +
 .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 31 +++++++
 llvm/test/Transforms/InstCombine/fdim.ll      | 89 +++++++++++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/fdim.ll

diff --git a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
index 2d3d2ada6183a7..128502b99d9a37 100644
--- a/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
+++ b/llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
@@ -212,6 +212,7 @@ class LibCallSimplifier {
   Value *optimizeTrigInversionPairs(CallInst *CI, IRBuilderBase &B);
   Value *optimizeSymmetric(CallInst *CI, LibFunc Func, IRBuilderBase &B);
   Value *optimizeRemquo(CallInst *CI, IRBuilderBase &B);
+  Value *optimizeFdim(CallInst *CI, IRBuilderBase &B);
   // Wrapper for all floating point library call optimizations
   Value *optimizeFloatingPointLibCall(CallInst *CI, LibFunc Func,
                                       IRBuilderBase &B);
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 6799d333fb2844..8b77eebc4907ca 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -3109,6 +3109,33 @@ Value *LibCallSimplifier::optimizeRemquo(CallInst *CI, IRBuilderBase &B) {
   return ConstantFP::get(CI->getType(), Rem);
 }
 
+/// Constant folds fdim
+Value *LibCallSimplifier::optimizeFdim(CallInst *CI, IRBuilderBase &B) {
+  const APFloat *X, *Y;
+  // Check if both values are constants
+  if (!match(CI->getArgOperand(0), m_APFloat(X)) ||
+      !match(CI->getArgOperand(1), m_APFloat(Y)))
+    return nullptr;
+  // If either argument is NaN, NaN is returned
+  if (X->isNaN() || Y->isNaN())
+    return ConstantFP::getQNaN(CI->getType());
+
+  IRBuilderBase::FastMathFlagGuard Guard(B);
+  FastMathFlags FMF = CI->getFastMathFlags();
+  // set no-NaN fast-math-flag as we already checked for NaN for both operands
+  FMF.setNoNaNs();
+  // set no-signed-zeroes as fdim will never return -0.0
+  FMF.setNoSignedZeros();
+  B.setFastMathFlags(FMF);
+  // fdim is equivalent to fmax(x - y, 0), except for the NaN handling
+  // requirements.
+  Intrinsic::ID IID = Intrinsic::maxnum;
+  return copyFlags(
+      *CI, B.CreateBinaryIntrinsic(
+               IID, B.CreateFSub(CI->getArgOperand(0), CI->getArgOperand(1)),
+               ConstantFP::get(CI->getType(), 0)));
+}
+
 //===----------------------------------------------------------------------===//
 // Integer Library Call Optimizations
 //===----------------------------------------------------------------------===//
@@ -4042,6 +4069,10 @@ Value *LibCallSimplifier::optimizeFloatingPointLibCall(CallInst *CI,
     if (hasFloatVersion(M, CI->getCalledFunction()->getName()))
       return optimizeBinaryDoubleFP(CI, Builder, TLI);
     return nullptr;
+  case LibFunc_fdim:
+  case LibFunc_fdimf:
+  case LibFunc_fdiml:
+    return optimizeFdim(CI, Builder);
   case LibFunc_fminf:
   case LibFunc_fmin:
   case LibFunc_fminl:
diff --git a/llvm/test/Transforms/InstCombine/fdim.ll b/llvm/test/Transforms/InstCombine/fdim.ll
new file mode 100644
index 00000000000000..ec289a05f97193
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fdim.ll
@@ -0,0 +1,89 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define double @fdim_double() {
+; CHECK-LABEL: define double @fdim_double() {
+; CHECK-NEXT:    ret double 2.500000e+00
+;
+  %dim = call double @fdim(double 10.5, double 8.0)
+  ret double %dim
+}
+
+define double @fdim_double1() {
+; CHECK-LABEL: define double @fdim_double1() {
+; CHECK-NEXT:    ret double 0.000000e+00
+;
+  %dim = call double @fdim(double 7.0, double 8.0)
+  ret double %dim
+}
+
+define float @fdim_float() {
+; CHECK-LABEL: define float @fdim_float() {
+; CHECK-NEXT:    ret float 0.000000e+00
+;
+  %dim = call float @fdimf(float 1.500000e+00, float 8.0)
+  ret float %dim
+}
+
+define float @fdim_float1() {
+; CHECK-LABEL: define float @fdim_float1() {
+; CHECK-NEXT:    ret float 2.000000e+00
+;
+  %dim = call float @fdimf(float 1.000000e+01, float 8.0)
+  ret float %dim
+}
+
+define double @fdim_poison1() {
+; CHECK-LABEL: define double @fdim_poison1() {
+; CHECK-NEXT:    [[DIM:%.*]] = call double @fdim(double poison, double 1.000000e+00)
+; CHECK-NEXT:    ret double [[DIM]]
+;
+  %dim = call double @fdim(double poison, double 1.0)
+  ret double %dim
+}
+
+define double @fdim_poison2() {
+; CHECK-LABEL: define double @fdim_poison2() {
+; CHECK-NEXT:    [[DIM:%.*]] = call double @fdim(double 1.000000e+00, double poison)
+; CHECK-NEXT:    ret double [[DIM]]
+;
+  %dim = call double @fdim(double 1.0, double poison)
+  ret double %dim
+}
+
+define double @fdim_undef1() {
+; CHECK-LABEL: define double @fdim_undef1() {
+; CHECK-NEXT:    [[DIM:%.*]] = call double @fdim(double undef, double 1.000000e+00)
+; CHECK-NEXT:    ret double [[DIM]]
+;
+  %dim = call double @fdim(double undef, double 1.0)
+  ret double %dim
+}
+
+define double @fdim_undef2() {
+; CHECK-LABEL: define double @fdim_undef2() {
+; CHECK-NEXT:    [[DIM:%.*]] = call double @fdim(double 1.000000e+00, double undef)
+; CHECK-NEXT:    ret double [[DIM]]
+;
+  %dim = call double @fdim(double 1.0, double undef)
+  ret double %dim
+}
+
+define double @fdim_inf(){
+; CHECK-LABEL: define double @fdim_inf() {
+; CHECK-NEXT:    ret double 0x7FF0000000000000
+;
+  %dim = call double @fdim(double 0x7FF0000000000000, double 0x8000000000000000 )
+  ret double %dim
+}
+
+define double @fdim_nzero(){
+; CHECK-LABEL: define double @fdim_nzero() {
+; CHECK-NEXT:    ret double 0.000000e+00
+;
+  %dim = call double @fdim(double -0.0, double +0.0)
+  ret double %dim
+}
+
+declare double @fdim(double, double)
+declare float @fdimf(float, float)



More information about the llvm-commits mailing list