aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorSanjay Patel <spatel@rotateright.com>2014-10-01 20:36:33 +0000
committerSanjay Patel <spatel@rotateright.com>2014-10-01 20:36:33 +0000
commit7b2cd9ad86747970d058b72e1d0db00dbe53dc54 (patch)
treeb92a76163dce040be1810449b41fa0dd142febf7 /llvm/lib/Analysis/ConstantFolding.cpp
parentb5c9e04b51bc010dbd797d7f485844b048a3112e (diff)
downloadllvm-7b2cd9ad86747970d058b72e1d0db00dbe53dc54.zip
llvm-7b2cd9ad86747970d058b72e1d0db00dbe53dc54.tar.gz
llvm-7b2cd9ad86747970d058b72e1d0db00dbe53dc54.tar.bz2
Make the sqrt intrinsic return undef for a negative input.
As discussed here: http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140609/220598.html And again here: http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-September/077168.html The sqrt of a negative number when using the llvm intrinsic is undefined. We should return undef rather than 0.0 to match the definition in the LLVM IR lang ref. This change should not affect any code that isn't using "no-nans-fp-math"; ie, no-nans is a requirement for generating the llvm intrinsic in place of a sqrt function call. Unfortunately, the behavior introduced by this patch will not match current gcc, xlc, icc, and possibly other compilers. The current clang/llvm behavior of returning 0.0 doesn't either. We knowingly approve of this difference with the other compilers in an attempt to flag code that is invoking undefined behavior. A front-end warning should also try to convince the user that the program will fail: http://llvm.org/bugs/show_bug.cgi?id=21093 Differential Revision: http://reviews.llvm.org/D5527 llvm-svn: 218803
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 88e2558..c703491 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1520,8 +1520,14 @@ static Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID,
(Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
if (V >= -0.0)
return ConstantFoldFP(sqrt, V, Ty);
- else // Undefined
- return Constant::getNullValue(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 's':