[PATCH] D28929: [ConstantFolding] Constant-fold llvm.sqrt(x) like other intrinsics.

Justin Lebar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 19 17:47:25 PST 2017


jlebar updated this revision to Diff 85078.
jlebar marked 3 inline comments as done.
jlebar added a comment.

Add test checking that we don't constant-fold llvm.sqrt(-2).


https://reviews.llvm.org/D28929

Files:
  llvm/lib/Analysis/ConstantFolding.cpp
  llvm/test/Transforms/InstCombine/constant-fold-math.ll


Index: llvm/test/Transforms/InstCombine/constant-fold-math.ll
===================================================================
--- llvm/test/Transforms/InstCombine/constant-fold-math.ll
+++ llvm/test/Transforms/InstCombine/constant-fold-math.ll
@@ -45,9 +45,10 @@
   ret double %x
 }
 
-; The sqrt intrinsic is undefined for negative inputs besides -0.0.
+; Currently we don't constant-fold intrinsics whose corresponding libcalls
+; raise an fp exception.
 ; CHECK-LABEL: @bad_sqrt
-; CHECK-NEXT: ret double undef
+; CHECK-NEXT: call double @llvm.sqrt.f64(double -2
 define double @bad_sqrt() {
   %x = call double @llvm.sqrt.f64(double -2.000000e+00)
   ret double %x
Index: llvm/lib/Analysis/ConstantFolding.cpp
===================================================================
--- llvm/lib/Analysis/ConstantFolding.cpp
+++ llvm/lib/Analysis/ConstantFolding.cpp
@@ -1630,6 +1630,8 @@
           return ConstantFoldFP(sin, V, Ty);
         case Intrinsic::cos:
           return ConstantFoldFP(cos, V, Ty);
+        case Intrinsic::sqrt:
+          return ConstantFoldFP(sqrt, V, Ty);
       }
 
       if (!TLI)
@@ -1683,19 +1685,6 @@
         else if ((Name == "log10" && V > 0 && TLI->has(LibFunc::log10)) ||
                  (Name == "log10f" && V > 0 && TLI->has(LibFunc::log10f)))
           return ConstantFoldFP(log10, V, Ty);
-        else if (IntrinsicID == Intrinsic::sqrt &&
-                 (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
-          if (V >= -0.0)
-            return ConstantFoldFP(sqrt, V, Ty);
-          else {
-            // Unlike the sqrt definitions in C/C++, POSIX, and IEEE-754 - which
-            // all guarantee or favor returning NaN - the square root of a
-            // negative number is not defined for the LLVM sqrt intrinsic.
-            // This is because the intrinsic should only be emitted in place of
-            // libm's sqrt function when using "no-nans-fp-math".
-            return UndefValue::get(Ty);
-          }
-        }
         break;
       case 'r':
         if ((Name == "round" && TLI->has(LibFunc::round)) ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28929.85078.patch
Type: text/x-patch
Size: 2107 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170120/e1c9f726/attachment.bin>


More information about the llvm-commits mailing list