Make the sqrt intrinsic return undef for a negative input.
authorSanjay Patel <spatel@rotateright.com>
Wed, 1 Oct 2014 20:36:33 +0000 (20:36 +0000)
committerSanjay Patel <spatel@rotateright.com>
Wed, 1 Oct 2014 20:36:33 +0000 (20:36 +0000)
commit0056820a482335551bf7fdbde87eeba9ed220292
tree9141c1e66a739ec01706e1548078378b69a5e175
parent03eb5f8ff6fc7a9bdeae5c5ebf7d38a47978f713
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218803 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Analysis/ConstantFolding.cpp
test/Transforms/InstCombine/constant-fold-math.ll