aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorJustin Lebar <jlebar@google.com>2017-01-21 00:59:57 +0000
committerJustin Lebar <jlebar@google.com>2017-01-21 00:59:57 +0000
commit8b18a347b6021cf5bea7b6f7c6fd0b8c42f26adb (patch)
treef0e5a2da9b6e689d803714d386a0940fcf457691 /llvm/lib/Analysis/ConstantFolding.cpp
parentda57dbf8f7b0395a00e33889cba1564fa3d85d3b (diff)
downloadllvm-8b18a347b6021cf5bea7b6f7c6fd0b8c42f26adb.zip
llvm-8b18a347b6021cf5bea7b6f7c6fd0b8c42f26adb.tar.gz
llvm-8b18a347b6021cf5bea7b6f7c6fd0b8c42f26adb.tar.bz2
[ConstantFolding] Constant-fold llvm.sqrt(x) like other intrinsics.
Summary: Currently we return undef, but we're in the process of changing the LangRef so that llvm.sqrt behaves like the other math intrinsics, matching the return value of the standard libcall but not setting errno. This change is legal even without the LangRef change because currently calling llvm.sqrt(x) where x is negative is spec'ed to be UB. But in practice it's also safe because we're simply constant-folding fewer inputs: Inputs >= -0 get constant-folded as before, but inputs < -0 now aren't constant-folded, because ConstantFoldFP aborts if the host math function raises an fp exception. Reviewers: hfinkel, efriedma, sanjoy Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D28929 llvm-svn: 292692
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp15
1 files changed, 2 insertions, 13 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 7386727..db8ac82 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1630,6 +1630,8 @@ Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty,
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 @@ Constant *ConstantFoldScalarCall(StringRef Name, unsigned IntrinsicID, Type *Ty,
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)) ||