summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis
diff options
context:
space:
mode:
authorBixia Zheng <bixia@google.com>2019-03-22 16:37:37 +0000
committerBixia Zheng <bixia@google.com>2019-03-22 16:37:37 +0000
commitbdf0230cffd13425a7f3492cc7c514fc60173537 (patch)
treed60d76459b69a4fbbe55c75fc0cf2a38d2fb6f11 /llvm/lib/Analysis
parent71ebc9eb0b0586c7d995f42e14c1e0180cc299b9 (diff)
downloadbcm5719-llvm-bdf0230cffd13425a7f3492cc7c514fc60173537.tar.gz
bcm5719-llvm-bdf0230cffd13425a7f3492cc7c514fc60173537.zip
[ConstantFolding] Fix GetConstantFoldFPValue to avoid cast overflow.
Summary: In C++, the behavior of casting a double value that is beyond the range of a single precision floating-point to a float value is undefined. This change replaces such a cast with APFloat::convert to convert the value, which is consistent with how we convert a double value to a half value. Reviewers: sanjoy Subscribers: lebedev.ri, sanjoy, jlebar, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D59500 llvm-svn: 356781
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp6
1 files changed, 2 insertions, 4 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 3b4e6031566..74468e8dd72 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1517,14 +1517,12 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
namespace {
Constant *GetConstantFoldFPValue(double V, Type *Ty) {
- if (Ty->isHalfTy()) {
+ if (Ty->isHalfTy() || Ty->isFloatTy()) {
APFloat APF(V);
bool unused;
- APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &unused);
+ APF.convert(Ty->getFltSemantics(), APFloat::rmNearestTiesToEven, &unused);
return ConstantFP::get(Ty->getContext(), APF);
}
- if (Ty->isFloatTy())
- return ConstantFP::get(Ty->getContext(), APFloat((float)V));
if (Ty->isDoubleTy())
return ConstantFP::get(Ty->getContext(), APFloat(V));
llvm_unreachable("Can only constant fold half/float/double");
OpenPOWER on IntegriCloud