[llvm] 923566a - [ConstantFold] Fold `logb` and `logbf` when the input parameter is a constant value. (#111232)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 9 16:56:19 PDT 2024
Author: c8ef
Date: 2024-10-10T07:56:16+08:00
New Revision: 923566a67de39a00eb6fc5cabbad307a72aa338e
URL: https://github.com/llvm/llvm-project/commit/923566a67de39a00eb6fc5cabbad307a72aa338e
DIFF: https://github.com/llvm/llvm-project/commit/923566a67de39a00eb6fc5cabbad307a72aa338e.diff
LOG: [ConstantFold] Fold `logb` and `logbf` when the input parameter is a constant value. (#111232)
This patch adds support for constant folding for the `logb` and `logbf`
libc functions.
Added:
llvm/test/Transforms/InstCombine/logb.ll
Modified:
llvm/lib/Analysis/ConstantFolding.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index a6ef271da11f1a..102762dc7937c6 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1677,9 +1677,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':
@@ -2389,6 +2389,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..f2509e956d71c1
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/logb.ll
@@ -0,0 +1,113 @@
+; 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
+}
+
+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
+}
+
+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