[PATCH] D59500: [ConstantFolding] Fix GetConstantFoldFPValue to avoid cast overflow.

Bixia Zheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 18 10:31:16 PDT 2019


bixia created this revision.
Herald added subscribers: llvm-commits, jlebar, sanjoy.
Herald added a project: LLVM.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D59500

Files:
  lib/Analysis/ConstantFolding.cpp


Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1517,14 +1517,12 @@
 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");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59500.191122.patch
Type: text/x-patch
Size: 839 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190318/62ce4bd4/attachment.bin>


More information about the llvm-commits mailing list