[llvm] [ConstantFold] Fold `log1p` and `log1pf` when the input parameter is a constant value. (PR #112113)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 14 07:01:31 PDT 2024


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

>From b49c6c1d100efd1df0e9e4f06e16e18b73f885c7 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 13 Oct 2024 10:49:45 +0800
Subject: [PATCH 1/4] constant folding log1p

---
 llvm/lib/Analysis/ConstantFolding.cpp     |   8 +-
 llvm/test/Transforms/InstCombine/log1p.ll | 113 ++++++++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/InstCombine/log1p.ll

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 102762dc7937c6..d7e79b6257de95 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1679,7 +1679,8 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
   case 'l':
     return Name == "log" || Name == "logf" || Name == "logl" ||
            Name == "log2" || Name == "log2f" || Name == "log10" ||
-           Name == "log10f" || Name == "logb" || Name == "logbf";
+           Name == "log10f" || Name == "logb" || Name == "logbf" ||
+           Name == "log1p" || Name == "log1pf";
   case 'n':
     return Name == "nearbyint" || Name == "nearbyintf";
   case 'p':
@@ -2394,6 +2395,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
       if (!APF.isZero() && TLI->has(Func))
         return ConstantFoldFP(logb, APF, Ty);
       break;
+    case LibFunc_log1p:
+    case LibFunc_log1pf:
+      if (APF > APFloat::getOne(APF.getSemantics(), true) && TLI->has(Func))
+        return ConstantFoldFP(log1p, APF, Ty);
+      break;
     case LibFunc_logl:
       return nullptr;
     case LibFunc_nearbyint:
diff --git a/llvm/test/Transforms/InstCombine/log1p.ll b/llvm/test/Transforms/InstCombine/log1p.ll
new file mode 100644
index 00000000000000..9e22c3f43fb7a2
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/log1p.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 @log1pf_const() {
+; CHECK-LABEL: define float @log1pf_const() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 7.000000e+00)
+; CHECK-NEXT:    ret float 0x4000A2B240000000
+;
+  %r = call float @log1pf(float 7.000000e+00)
+  ret float %r
+}
+
+define double @log1p_const() {
+; CHECK-LABEL: define double @log1p_const() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 7.000000e+00)
+; CHECK-NEXT:    ret double 0x4000A2B23F3BAB73
+;
+  %r = call double @log1p(double 7.000000e+00)
+  ret double %r
+}
+
+define float @log1pf_minus_one() {
+; CHECK-LABEL: define float @log1pf_minus_one() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -1.000000e+00)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float -1.000000e+00)
+  ret float %r
+}
+
+define double @log1p_minus_one() {
+; CHECK-LABEL: define double @log1p_minus_one() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double -1.000000e+00)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double -1.000000e+00)
+  ret double %r
+}
+
+define float @log1pf_zero() {
+; CHECK-LABEL: define float @log1pf_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0.000000e+00)
+; CHECK-NEXT:    ret float 0.000000e+00
+;
+  %r = call float @log1pf(float 0.000000e+00)
+  ret float %r
+}
+
+define double @log1p_zero() {
+; CHECK-LABEL: define double @log1p_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0.000000e+00)
+; CHECK-NEXT:    ret double 0.000000e+00
+;
+  %r = call double @log1p(double 0.000000e+00)
+  ret double %r
+}
+
+define float @log1pf_inf() {
+; CHECK-LABEL: define float @log1pf_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF0000000000000)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float 0x7FF0000000000000)
+  ret float %r
+}
+
+define double @log1p_inf() {
+; CHECK-LABEL: define double @log1p_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0x7FF0000000000000)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double 0x7FF0000000000000)
+  ret double %r
+}
+
+define float @log1pf_nan() {
+; CHECK-LABEL: define float @log1pf_nan() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF8000000000000)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float 0x7FF8000000000000)
+  ret float %r
+}
+
+define double @log1p_nan() {
+; CHECK-LABEL: define double @log1p_nan() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0x7FF8000000000000)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double 0x7FF8000000000000)
+  ret double %r
+}
+
+define float @log1pf_poison() {
+; CHECK-LABEL: define float @log1pf_poison() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float poison)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float poison)
+  ret float %r
+}
+
+define double @log1p_poison() {
+; CHECK-LABEL: define double @log1p_poison() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double poison)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double poison)
+  ret double %r
+}
+
+declare float @log1pf(float)
+declare double @log1p(double)

>From cbeb72580575e3590f6bb408eda8db3e6f431da1 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 13 Oct 2024 11:23:37 +0800
Subject: [PATCH 2/4] constant folding log1p

---
 llvm/lib/Analysis/ConstantFolding.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index d7e79b6257de95..303e1ca320e045 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2397,7 +2397,7 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
       break;
     case LibFunc_log1p:
     case LibFunc_log1pf:
-      if (APF > APFloat::getOne(APF.getSemantics(), true) && TLI->has(Func))
+      if (APF > APFloat(APF.getSemantics(), "-1") && TLI->has(Func))
         return ConstantFoldFP(log1p, APF, Ty);
       break;
     case LibFunc_logl:

>From b071121249d70eaade9d3ce6784fbe341443dc3e Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Sun, 13 Oct 2024 18:01:32 +0800
Subject: [PATCH 3/4] add test

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

diff --git a/llvm/test/Transforms/InstCombine/log1p.ll b/llvm/test/Transforms/InstCombine/log1p.ll
index 9e22c3f43fb7a2..3175c84eafcfdc 100644
--- a/llvm/test/Transforms/InstCombine/log1p.ll
+++ b/llvm/test/Transforms/InstCombine/log1p.ll
@@ -55,6 +55,60 @@ define double @log1p_zero() {
   ret double %r
 }
 
+define float @log1pf_neg_zero() {
+; CHECK-LABEL: define float @log1pf_neg_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -0.000000e+00)
+; CHECK-NEXT:    ret float -0.000000e+00
+;
+  %r = call float @log1pf(float -0.000000e+00)
+  ret float %r
+}
+
+define double @log1p_neg_zero() {
+; CHECK-LABEL: define double @log1p_neg_zero() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double -0.000000e+00)
+; CHECK-NEXT:    ret double -0.000000e+00
+;
+  %r = call double @log1p(double -0.000000e+00)
+  ret double %r
+}
+
+define float @log1pf_neg1() {
+; CHECK-LABEL: define float @log1pf_neg1() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -7.000000e+00)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float -7.000000e+00)
+  ret float %r
+}
+
+define double @log1p_neg1() {
+; CHECK-LABEL: define double @log1p_neg1() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double -7.000000e+00)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double -7.000000e+00)
+  ret double %r
+}
+
+define float @log1pf_neg2() {
+; CHECK-LABEL: define float @log1pf_neg2() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -5.000000e-01)
+; CHECK-NEXT:    ret float 0xBFE62E4300000000
+;
+  %r = call float @log1pf(float -5.000000e-01)
+  ret float %r
+}
+
+define double @log1p_neg2() {
+; CHECK-LABEL: define double @log1p_neg2() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double -5.000000e-01)
+; CHECK-NEXT:    ret double 0xBFE62E42FEFA39EF
+;
+  %r = call double @log1p(double -5.000000e-01)
+  ret double %r
+}
+
 define float @log1pf_inf() {
 ; CHECK-LABEL: define float @log1pf_inf() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF0000000000000)
@@ -73,6 +127,24 @@ define double @log1p_inf() {
   ret double %r
 }
 
+define float @log1pf_neg_inf() {
+; CHECK-LABEL: define float @log1pf_neg_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0xFFF0000000000000)
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float 0xFFF0000000000000)
+  ret float %r
+}
+
+define double @log1p_neg_inf() {
+; CHECK-LABEL: define double @log1p_neg_inf() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0xFFF0000000000000)
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double 0xFFF0000000000000)
+  ret double %r
+}
+
 define float @log1pf_nan() {
 ; CHECK-LABEL: define float @log1pf_nan() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF8000000000000)

>From 6e7496b4f53ea9a4bca4bda4d8bf20921b827e80 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Mon, 14 Oct 2024 22:01:12 +0800
Subject: [PATCH 4/4] add readnone test

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

diff --git a/llvm/test/Transforms/InstCombine/log1p.ll b/llvm/test/Transforms/InstCombine/log1p.ll
index 3175c84eafcfdc..81d3cc8a4f7ac9 100644
--- a/llvm/test/Transforms/InstCombine/log1p.ll
+++ b/llvm/test/Transforms/InstCombine/log1p.ll
@@ -37,6 +37,24 @@ define double @log1p_minus_one() {
   ret double %r
 }
 
+define float @log1pf_minus_one_memory_none() {
+; CHECK-LABEL: define float @log1pf_minus_one_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -1.000000e+00) #[[ATTR0:[0-9]+]]
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float -1.000000e+00) readnone
+  ret float %r
+}
+
+define double @log1p_minus_one_memory_none() {
+; CHECK-LABEL: define double @log1p_minus_one_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double -1.000000e+00) #[[ATTR0]]
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double -1.000000e+00) readnone
+  ret double %r
+}
+
 define float @log1pf_zero() {
 ; CHECK-LABEL: define float @log1pf_zero() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0.000000e+00)
@@ -91,6 +109,24 @@ define double @log1p_neg1() {
   ret double %r
 }
 
+define float @log1pf_neg1_memory_none() {
+; CHECK-LABEL: define float @log1pf_neg1_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -7.000000e+00) #[[ATTR0]]
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float -7.000000e+00) readnone
+  ret float %r
+}
+
+define double @log1p_neg1_memory_none() {
+; CHECK-LABEL: define double @log1p_neg1_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double -7.000000e+00) #[[ATTR0]]
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double -7.000000e+00) readnone
+  ret double %r
+}
+
 define float @log1pf_neg2() {
 ; CHECK-LABEL: define float @log1pf_neg2() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float -5.000000e-01)
@@ -127,6 +163,24 @@ define double @log1p_inf() {
   ret double %r
 }
 
+define float @log1pf_inf_memory_none() {
+; CHECK-LABEL: define float @log1pf_inf_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF0000000000000) #[[ATTR0]]
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float 0x7FF0000000000000) readnone
+  ret float %r
+}
+
+define double @log1p_inf_memory_none() {
+; CHECK-LABEL: define double @log1p_inf_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0x7FF0000000000000) #[[ATTR0]]
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double 0x7FF0000000000000) readnone
+  ret double %r
+}
+
 define float @log1pf_neg_inf() {
 ; CHECK-LABEL: define float @log1pf_neg_inf() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0xFFF0000000000000)
@@ -145,6 +199,24 @@ define double @log1p_neg_inf() {
   ret double %r
 }
 
+define float @log1pf_neg_inf_memory_none() {
+; CHECK-LABEL: define float @log1pf_neg_inf_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0xFFF0000000000000) #[[ATTR0]]
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float 0xFFF0000000000000) readnone
+  ret float %r
+}
+
+define double @log1p_neg_inf_memory_none() {
+; CHECK-LABEL: define double @log1p_neg_inf_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0xFFF0000000000000) #[[ATTR0]]
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double 0xFFF0000000000000) readnone
+  ret double %r
+}
+
 define float @log1pf_nan() {
 ; CHECK-LABEL: define float @log1pf_nan() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF8000000000000)
@@ -163,6 +235,24 @@ define double @log1p_nan() {
   ret double %r
 }
 
+define float @log1pf_nan_memory_none() {
+; CHECK-LABEL: define float @log1pf_nan_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float 0x7FF8000000000000) #[[ATTR0]]
+; CHECK-NEXT:    ret float [[R]]
+;
+  %r = call float @log1pf(float 0x7FF8000000000000) readnone
+  ret float %r
+}
+
+define double @log1p_nan_memory_none() {
+; CHECK-LABEL: define double @log1p_nan_memory_none() {
+; CHECK-NEXT:    [[R:%.*]] = call double @log1p(double 0x7FF8000000000000) #[[ATTR0]]
+; CHECK-NEXT:    ret double [[R]]
+;
+  %r = call double @log1p(double 0x7FF8000000000000) readnone
+  ret double %r
+}
+
 define float @log1pf_poison() {
 ; CHECK-LABEL: define float @log1pf_poison() {
 ; CHECK-NEXT:    [[R:%.*]] = call float @log1pf(float poison)



More information about the llvm-commits mailing list