[PATCH] D88066: [InstCombine] For pow(x, +/-0.5), stop falling into pow(x, 1.5), etc. case

Hubert Tong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 21:06:58 PDT 2020


hubert.reinterpretcast created this revision.
hubert.reinterpretcast added reviewers: spatel, nemanjai, daltenty.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.
hubert.reinterpretcast requested review of this revision.

The current code for handling pow(x, y) where y is an integer plus 0.5 is not explicitly guarded against attempting to transform the case where abs(y) is exactly 0.5.

The latter case is meant to be handled by `replacePowWithSqrt`. Indeed, if the pow(x, integer+0.5) case proceeds past a certain point, it will hit an assertion by attempting to form pow(x, 0) using `getPow`.

This patch adds an explicit check to prevent attempting the pow(x, integer+0.5) transformation on pow(x, +/-0.5) as suggested during the review of D87877 <https://reviews.llvm.org/D87877>. This has the effect of retaining the shrinking of `pow` to `powf` when the `sqrt` libcall cannot be formed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D88066

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


Index: llvm/test/Transforms/InstCombine/pow-4.ll
===================================================================
--- llvm/test/Transforms/InstCombine/pow-4.ll
+++ llvm/test/Transforms/InstCombine/pow-4.ll
@@ -234,6 +234,23 @@
   ret <4 x float> %1
 }
 
+; (float)pow((double)(float)x, 0.5)
+define float @shrink_pow_libcall_half(float %x) {
+; SQRT-LABEL: @shrink_pow_libcall_half(
+; SQRT-NEXT:    [[SQRTF:%.*]] = call fast float @sqrtf(float [[X:%.*]])
+; SQRT-NEXT:    [[SQRTF1:%.*]] = call fast float @sqrtf(float [[X]])
+; SQRT-NEXT:    ret float [[SQRTF1]]
+;
+; NOSQRT-LABEL: @shrink_pow_libcall_half(
+; NOSQRT-NEXT:    [[SQRTF:%.*]] = call fast float @sqrtf(float [[X:%.*]])
+; NOSQRT-NEXT:    ret float [[SQRTF]]
+;
+  %dx = fpext float %x to double
+  %call = call fast double @pow(double %dx, double 0.5)
+  %fr = fptrunc double %call to float
+  ret float %fr
+}
+
 ; Make sure that -0.0 exponent is always simplified.
 
 define double @PR43233(double %x) {
Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1722,7 +1722,8 @@
 
   // pow(x, n) -> x * x * x * ...
   const APFloat *ExpoF;
-  if (AllowApprox && match(Expo, m_APFloat(ExpoF))) {
+  if (AllowApprox && match(Expo, m_APFloat(ExpoF)) &&
+      !ExpoF->isExactlyValue(0.5) && !ExpoF->isExactlyValue(-0.5)) {
     // We limit to a max of 7 multiplications, thus the maximum exponent is 32.
     // If the exponent is an integer+0.5 we generate a call to sqrt and an
     // additional fmul.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88066.293328.patch
Type: text/x-patch
Size: 1645 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200922/62233242/attachment.bin>


More information about the llvm-commits mailing list