[LibCallSimplifier] use instruction-level fast-math-flags to transform pow(x, 0.5...
authorSanjay Patel <spatel@rotateright.com>
Tue, 12 Jan 2016 19:06:35 +0000 (19:06 +0000)
committerSanjay Patel <spatel@rotateright.com>
Tue, 12 Jan 2016 19:06:35 +0000 (19:06 +0000)
Also, propagate the FMF to the newly created sqrt() call.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257503 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/SimplifyLibCalls.cpp
test/Transforms/InstCombine/pow-sqrt.ll

index 2551abef75b6e0b7d6d04fe08a340813d11180fc..dc07440601423073c22eaf46f02ae652a2010b75 100644 (file)
@@ -1164,9 +1164,12 @@ Value *LibCallSimplifier::optimizePow(CallInst *CI, IRBuilder<> &B) {
                       LibFunc::fabsl)) {
 
     // In -ffast-math, pow(x, 0.5) -> sqrt(x).
                       LibFunc::fabsl)) {
 
     // In -ffast-math, pow(x, 0.5) -> sqrt(x).
-    if (UnsafeFPMath)
+    if (CI->hasUnsafeAlgebra()) {
+      IRBuilder<>::FastMathFlagGuard Guard(B);
+      B.setFastMathFlags(CI->getFastMathFlags());
       return EmitUnaryFloatFnCall(Op1, TLI->getName(LibFunc::sqrt), B,
                                   Callee->getAttributes());
       return EmitUnaryFloatFnCall(Op1, TLI->getName(LibFunc::sqrt), B,
                                   Callee->getAttributes());
+    }
 
     // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
     // This is faster than calling pow, and still handles negative zero
 
     // Expand pow(x, 0.5) to (x == -infinity ? +infinity : fabs(sqrt(x))).
     // This is faster than calling pow, and still handles negative zero
index 8fc74e4a002422fce4dbe5ac1255d635cf7e123e..1e6166c5f114280668fd6f13f126b864bedfe604 100644 (file)
@@ -1,15 +1,13 @@
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
 ; RUN: opt < %s -instcombine -S | FileCheck %s
 
-define double @mypow(double %x) #0 {
-entry:
-  %pow = call double @llvm.pow.f64(double %x, double 5.000000e-01)
+define double @pow_half(double %x) {
+  %pow = call fast double @llvm.pow.f64(double %x, double 5.000000e-01)
   ret double %pow
 }
 
   ret double %pow
 }
 
-; CHECK-LABEL: define double @mypow(
-; CHECK:   %sqrt = call double @sqrt(double %x) #1
-; CHECK:   ret double %sqrt
-; CHECK: }
+; CHECK-LABEL: define double @pow_half(
+; CHECK-NEXT:  %sqrt = call fast double @sqrt(double %x)
+; CHECK-NEXT:  ret double %sqrt
 
 declare double @llvm.pow.f64(double, double)
 
 declare double @llvm.pow.f64(double, double)
-attributes #0 = { "unsafe-fp-math"="true" }
+