[llvm] r334777 - Make uitofp and sitofp defined on overflow.

Eli Friedman via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 14 15:58:48 PDT 2018


Author: efriedma
Date: Thu Jun 14 15:58:48 2018
New Revision: 334777

URL: http://llvm.org/viewvc/llvm-project?rev=334777&view=rev
Log:
Make uitofp and sitofp defined on overflow.

IEEE 754 defines the expected result on overflow. As far as I know,
hardware implementations (of f16), and compiler-rt (__floatuntisf)
correctly return +-Inf on overflow. And I can't think of any useful
transform that would take advantage of overflow being undefined here.

Differential Revision: https://reviews.llvm.org/D47807


Modified:
    llvm/trunk/docs/LangRef.rst
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Transforms/ConstProp/cast.ll

Modified: llvm/trunk/docs/LangRef.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/LangRef.rst?rev=334777&r1=334776&r2=334777&view=diff
==============================================================================
--- llvm/trunk/docs/LangRef.rst (original)
+++ llvm/trunk/docs/LangRef.rst Thu Jun 14 15:58:48 2018
@@ -3288,14 +3288,12 @@ The following is the syntax for constant
     Convert an unsigned integer constant to the corresponding 
     floating-point constant. TYPE must be a scalar or vector floating-point
     type.  CST must be of scalar or vector integer type. Both CST and TYPE must
-    be scalars, or vectors of the same number of elements. If the value
-    won't fit in the floating-point type, the results are undefined.
+    be scalars, or vectors of the same number of elements.
 ``sitofp (CST to TYPE)``
     Convert a signed integer constant to the corresponding floating-point
     constant. TYPE must be a scalar or vector floating-point type.
     CST must be of scalar or vector integer type. Both CST and TYPE must
-    be scalars, or vectors of the same number of elements. If the value
-    won't fit in the floating-point type, the results are undefined.
+    be scalars, or vectors of the same number of elements.
 ``ptrtoint (CST to TYPE)``
     Perform the :ref:`ptrtoint operation <i_ptrtoint>` on constants.
 ``inttoptr (CST to TYPE)``
@@ -8851,8 +8849,9 @@ Semantics:
 
 The '``uitofp``' instruction interprets its operand as an unsigned
 integer quantity and converts it to the corresponding floating-point
-value. If the value cannot fit in the floating-point value, the results
-are undefined.
+value. If the value cannot be exactly represented, it is rounded using
+the default rounding mode.
+
 
 Example:
 """"""""
@@ -8891,9 +8890,9 @@ Semantics:
 """"""""""
 
 The '``sitofp``' instruction interprets its operand as a signed integer
-quantity and converts it to the corresponding floating-point value. If
-the value cannot fit in the floating-point value, the results are
-undefined.
+quantity and converts it to the corresponding floating-point value. If the
+value cannot be exactly represented, it is rounded using the default rounding
+mode.
 
 Example:
 """"""""

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=334777&r1=334776&r2=334777&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Thu Jun 14 15:58:48 2018
@@ -682,13 +682,8 @@ Constant *llvm::ConstantFoldCastInstruct
       const APInt &api = CI->getValue();
       APFloat apf(DestTy->getFltSemantics(),
                   APInt::getNullValue(DestTy->getPrimitiveSizeInBits()));
-      if (APFloat::opOverflow &
-          apf.convertFromAPInt(api, opc==Instruction::SIToFP,
-                              APFloat::rmNearestTiesToEven)) {
-        // Undefined behavior invoked - the destination type can't represent
-        // the input constant.
-        return UndefValue::get(DestTy);
-      }
+      apf.convertFromAPInt(api, opc==Instruction::SIToFP,
+                           APFloat::rmNearestTiesToEven);
       return ConstantFP::get(V->getContext(), apf);
     }
     return nullptr;

Modified: llvm/trunk/test/Transforms/ConstProp/cast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstProp/cast.ll?rev=334777&r1=334776&r2=334777&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/ConstProp/cast.ll (original)
+++ llvm/trunk/test/Transforms/ConstProp/cast.ll Thu Jun 14 15:58:48 2018
@@ -24,7 +24,7 @@ define i8 @overflow_fptoui() {
 
 define float @overflow_uitofp() {
 ; CHECK-LABEL: @overflow_uitofp(
-; CHECK-NEXT:    ret float undef
+; CHECK-NEXT:    ret float 0x7FF0000000000000
 ;
   %i = uitofp i130 400000000000000000000000000000000000000 to float
   ret float %i
@@ -32,7 +32,7 @@ define float @overflow_uitofp() {
 
 define float @overflow_sitofp() {
 ; CHECK-LABEL: @overflow_sitofp(
-; CHECK-NEXT:    ret float undef
+; CHECK-NEXT:    ret float 0x7FF0000000000000
 ;
   %i = sitofp i130 400000000000000000000000000000000000000 to float
   ret float %i




More information about the llvm-commits mailing list