[PATCH] D50113: [SLC] Fix shrinking of pow()

Evandro Menezes via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 3 11:04:22 PDT 2018


evandro updated this revision to Diff 159055.
evandro added a comment.

Keep the double precision `pow()` when the result is too.


https://reviews.llvm.org/D50113

Files:
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/double-float-shrink-1.ll


Index: llvm/test/Transforms/InstCombine/double-float-shrink-1.ll
===================================================================
--- llvm/test/Transforms/InstCombine/double-float-shrink-1.ll
+++ llvm/test/Transforms/InstCombine/double-float-shrink-1.ll
@@ -344,11 +344,9 @@
   ret double %call
 }
 
-; FIXME: Miscompile - we dropped the 2nd argument!
-
 define float @pow_test1(float %f, float %g)   {
 ; CHECK-LABEL: @pow_test1(
-; CHECK-NEXT:    [[POWF:%.*]] = call fast float @powf(float [[F:%.*]])
+; CHECK-NEXT:    [[POWF:%.*]] = call fast float @powf(float %f, float %g)
 ; CHECK-NEXT:    ret float [[POWF]]
 ;
   %df = fpext float %f to double
@@ -358,14 +356,10 @@
   ret float %fr
 }
 
-; TODO: This should shrink?
-
 define double @pow_test2(float %f, float %g) {
 ; CHECK-LABEL: @pow_test2(
-; CHECK-NEXT:    [[DF:%.*]] = fpext float [[F:%.*]] to double
-; CHECK-NEXT:    [[DG:%.*]] = fpext float [[G:%.*]] to double
-; CHECK-NEXT:    [[CALL:%.*]] = call fast double @pow(double [[DF]], double [[DG]])
-; CHECK-NEXT:    ret double [[CALL]]
+; CHECK:         [[POW:%.*]] = call fast double @pow(double %df, double %dg)
+; CHECK-NEXT:    ret double [[POW]]
 ;
   %df = fpext float %f to double
   %dg = fpext float %g to double
Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1150,14 +1150,16 @@
   Value *Shrunk = nullptr;
   bool Ignored;
 
-  if (UnsafeFPShrink &&
-      Name == TLI->getName(LibFunc_pow) && hasFloatVersion(Name))
-    Shrunk = optimizeUnaryDoubleFP(Pow, B, true);
-
   // Propagate the math semantics from the call to any created instructions.
   IRBuilder<>::FastMathFlagGuard Guard(B);
   B.setFastMathFlags(Pow->getFastMathFlags());
 
+  // Shrink pow() to powf() if the arguments are single precision,
+  // unless the result is expected to be double precision.
+  if (UnsafeFPShrink &&
+      Name == TLI->getName(LibFunc_pow) && hasFloatVersion(Name))
+    Shrunk = optimizeBinaryDoubleFP(Pow, B, true);
+
   // Evaluate special cases related to the base.
 
   // pow(1.0, x) -> 1.0
@@ -1252,7 +1254,7 @@
     if (!ExpoA.isInteger() ||
         ExpoA.compare
             (APFloat(ExpoA.getSemantics(), 32.0)) == APFloat::cmpGreaterThan)
-      return nullptr;
+      return Shrunk;
 
     // We will memoize intermediate products of the Addition Chain.
     Value *InnerChain[33] = {nullptr};
@@ -1270,7 +1272,7 @@
     return FMul;
   }
 
-  return nullptr;
+  return Shrunk;
 }
 
 Value *LibCallSimplifier::optimizeExp2(CallInst *CI, IRBuilder<> &B) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50113.159055.patch
Type: text/x-patch
Size: 2676 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180803/eccd919b/attachment.bin>


More information about the llvm-commits mailing list