[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
Wed Oct 9 07:56:35 PDT 2024


================
@@ -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
+}
+
----------------
c8ef wrote:

I must admit that the function body is quite large, and I have lost some details. Initially, I wrote the following code:

```c++
      if (TLI->has(Func)) {
        if (!APF.isZero())
          return ConstantFoldFP(logb, APF, Ty);
        if (Call->hasFnAttr(Attribute::ReadNone)) {
          // If x is a NaN, a NaN is returned.
          if (APF.isNaN())
            return ConstantFP::getNaN(Ty);
          // If x is zero, then a pole error occurs, and the functions return -HUGE_VAL, -HUGE_VALF, or -HUGE_VALL, respectively.
          if (APF.isZero())
            return ConstantFP::getInfinity(Ty, true);
          // If x is negative infinity or positive infinity, then positive infinity is returned.
          if (APF.isInfinity())
            return ConstantFP::getInfinity(Ty);
        }
      }
 ```
 
 But it's not working. Finally I found this:
 
 https://github.com/llvm/llvm-project/blob/8ba1f48ead28df1260cc1039c6e010f1084bcb7c/llvm/lib/Analysis/ConstantFolding.cpp#L2231-L2235 
 
If we want to fold a non-finite argument, we need to revert the code above. Or the current code is good to go?

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


More information about the llvm-commits mailing list