[PATCH] D79409: [InstCombine] Remove hasNoInfs check for pow(C, y) -> exp2(log2(C)*y)

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 06:12:08 PDT 2020


foad updated this revision to Diff 262625.
foad added a comment.

Fix silly mistake in last revision.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79409/new/

https://reviews.llvm.org/D79409

Files:
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/pow-exp.ll


Index: llvm/test/Transforms/InstCombine/pow-exp.ll
===================================================================
--- llvm/test/Transforms/InstCombine/pow-exp.ll
+++ llvm/test/Transforms/InstCombine/pow-exp.ll
@@ -400,8 +400,9 @@
 
 define double @pow_ok_base_no_ninf(double %e) {
 ; CHECK-LABEL: @pow_ok_base_no_ninf(
-; CHECK-NEXT:    [[CALL:%.*]] = tail call nnan afn double @pow(double 0x3FE6666666666666, double [[E:%.*]])
-; CHECK-NEXT:    ret double [[CALL]]
+; CHECK-NEXT:    [[MUL:%.*]] = fmul nnan afn double [[E:%.*]], 0xBFE0776{{.*}}
+; CHECK-NEXT:    [[EXP2:%.*]] = call nnan afn double @exp2(double [[MUL]])
+; CHECK-NEXT:    ret double [[EXP2]]
 ;
   %call = tail call afn nnan double @pow(double 0x3FE6666666666666, double %e)
   ret double %call
Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1563,9 +1563,16 @@
     return emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10, LibFunc_exp10f,
                                 LibFunc_exp10l, B, Attrs);
 
-  // pow(n, x) -> exp2(log2(n) * x)
-  if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && Pow->hasNoInfs() &&
-      BaseF->isFiniteNonZero() && !BaseF->isNegative()) {
+  // pow(x, y) -> exp2(log2(x) * y)
+  // We can do this transformation for any positive finite n except 1.0, because
+  // pow has this behaviour:
+  //   If x is +1, the result is 1.0 (even if y is a NaN).
+  // which is hard to preserve if y might be infinite or NaN.
+  if (Pow->hasApproxFunc() && Pow->hasNoNaNs() && BaseF->isFiniteNonZero() &&
+      !BaseF->isNegative()) {
+    assert(!match(Base, m_FPOne()) &&
+           "pow(1.0, y) should have been simplified earlier!");
+
     Value *Log = nullptr;
     if (Ty->isFloatTy())
       Log = ConstantFP::get(Ty, std::log2(BaseF->convertToFloat()));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79409.262625.patch
Type: text/x-patch
Size: 1930 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200507/c6108262/attachment-0001.bin>


More information about the llvm-commits mailing list