[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
Fri Oct 4 23:41:12 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: None (c8ef)

<details>
<summary>Changes</summary>

This patch adds support for constant folding for the `logb` and `logbf` libc functions.

---
Full diff: https://github.com/llvm/llvm-project/pull/111232.diff


2 Files Affected:

- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+8-3) 
- (added) llvm/test/Transforms/InstCombine/logb.ll (+41) 


``````````diff
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)

``````````

</details>


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


More information about the llvm-commits mailing list