[llvm] [ConstantFold] Fold `logb` and `logbf` when the input parameter is a constant value. (PR #111232)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 8 05:24:35 PDT 2024


https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/111232

>From beff7992c18dad10f5145da052c226ea429584cc Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sat, 5 Oct 2024 13:37:54 +0800
Subject: [PATCH 1/3] const fold logb{f}

---
 llvm/lib/Analysis/ConstantFolding.cpp    | 11 +++++--
 llvm/test/Transforms/InstCombine/logb.ll | 41 ++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Transforms/InstCombine/logb.ll

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index a7a6de3f3b97b0..b1e39d9417cb5b 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1676,9 +1676,9 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
            Name == "floor" || Name == "floorf" ||
            Name == "fmod" || Name == "fmodf";
   case 'l':
-    return Name == "log" || Name == "logf" || Name == "log2" ||
-           Name == "log2f" || Name == "log10" || Name == "log10f" ||
-           Name == "logl";
+    return Name == "log" || Name == "logf" || Name == "logl" ||
+           Name == "log2" || Name == "log2f" || Name == "log10" ||
+           Name == "log10f" || Name == "logb" || Name == "logbf";
   case 'n':
     return Name == "nearbyint" || Name == "nearbyintf";
   case 'p':
@@ -2388,6 +2388,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
         // TODO: What about hosts that lack a C99 library?
         return ConstantFoldFP(log10, APF, Ty);
       break;
+    case LibFunc_logb:
+    case LibFunc_logbf:
+      if (!APF.isZero() && TLI->has(Func))
+        return ConstantFoldFP(logb, APF, Ty);
+      break;
     case LibFunc_logl:
       return nullptr;
     case LibFunc_nearbyint:
diff --git a/llvm/test/Transforms/InstCombine/logb.ll b/llvm/test/Transforms/InstCombine/logb.ll
new file mode 100644
index 00000000000000..3a2d645466a4f0
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/logb.ll
@@ -0,0 +1,41 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define float @logbf_const() {
+; CHECK-LABEL: define float @logbf_const() {
+; CHECK-NEXT:    [[R:%.*]] = call float @logbf(float 7.000000e+00)
+; CHECK-NEXT:    ret float 2.000000e+00
+;
+  %r = call float @logbf(float 7.000000e+00)
+  ret float %r
+}
+
+define double @logb_const() {
+; CHECK-LABEL: define double @logb_const() {
+; CHECK-NEXT:    [[R:%.*]] = call double @logb(double -7.000000e+00)
+; CHECK-NEXT:    ret double 2.000000e+00
+;
+  %r = call double @logb(double -7.000000e+00)
+  ret double %r
+}
+
+define float @logbf_zero() {
+; CHECK-LABEL: define float @logbf_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call float @logbf(float 0.000000e+00)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @logbf(float 0.000000e+00)
+  ret float %r
+}
+
+define double @logb_zero() {
+; CHECK-LABEL: define double @logb_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call double @logb(double 0.000000e+00)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @logb(double 0.000000e+00)
+  ret double %r
+}
+
+declare float @logbf(float)
+declare double @logb(double)

>From e50012fe498ec80370fafc84d5eadd573c6d682f Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 6 Oct 2024 11:21:50 +0800
Subject: [PATCH 2/3] add neg zero test

---
 llvm/test/Transforms/InstCombine/logb.ll | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/logb.ll b/llvm/test/Transforms/InstCombine/logb.ll
index 3a2d645466a4f0..86bb535e411a07 100644
--- a/llvm/test/Transforms/InstCombine/logb.ll
+++ b/llvm/test/Transforms/InstCombine/logb.ll
@@ -37,5 +37,23 @@ define double @logb_zero() {
   ret double %r
 }
 
+define float @logbf_neg_zero() {
+; CHECK-LABEL: define float @logbf_neg_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call float @logbf(float -0.000000e+00)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @logbf(float -0.000000e+00)
+  ret float %r
+}
+
+define double @logb_neg_zero() {
+; CHECK-LABEL: define double @logb_neg_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call double @logb(double -0.000000e+00)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @logb(double -0.000000e+00)
+  ret double %r
+}
+
 declare float @logbf(float)
 declare double @logb(double)

>From 8ba1f48ead28df1260cc1039c6e010f1084bcb7c Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Tue, 8 Oct 2024 20:24:14 +0800
Subject: [PATCH 3/3] inf;nan;poison

---
 llvm/test/Transforms/InstCombine/logb.ll | 54 ++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/logb.ll b/llvm/test/Transforms/InstCombine/logb.ll
index 86bb535e411a07..f2509e956d71c1 100644
--- a/llvm/test/Transforms/InstCombine/logb.ll
+++ b/llvm/test/Transforms/InstCombine/logb.ll
@@ -55,5 +55,59 @@ define double @logb_neg_zero() {
   ret double %r
 }
 
+define float @logbf_inf() {
+; CHECK-LABEL: define float @logbf_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call float @logbf(float 0x7FF0000000000000)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @logbf(float 0x7FF0000000000000)
+  ret float %r
+}
+
+define double @logb_inf() {
+; CHECK-LABEL: define double @logb_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call double @logb(double 0x7FF0000000000000)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @logb(double 0x7FF0000000000000)
+  ret double %r
+}
+
+define float @logbf_nan() {
+; CHECK-LABEL: define float @logbf_nan() {
+; CHECK-NEXT:    [[R:%.*]] = call float @logbf(float 0x7FF8000000000000)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @logbf(float 0x7FF8000000000000)
+  ret float %r
+}
+
+define double @logb_nan() {
+; CHECK-LABEL: define double @logb_nan() {
+; CHECK-NEXT:    [[R:%.*]] = call double @logb(double 0x7FF8000000000000)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @logb(double 0x7FF8000000000000)
+  ret double %r
+}
+
+define float @logbf_poison() {
+; CHECK-LABEL: define float @logbf_poison() {
+; CHECK-NEXT:    [[R:%.*]] = call float @logbf(float poison)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @logbf(float poison)
+  ret float %r
+}
+
+define double @logb_poison() {
+; CHECK-LABEL: define double @logb_poison() {
+; CHECK-NEXT:    [[R:%.*]] = call double @logb(double poison)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @logb(double poison)
+  ret double %r
+}
+
 declare float @logbf(float)
 declare double @logb(double)



More information about the llvm-commits mailing list