Constant fold int-to-long-double conversions;
use APFloat for int-to-float/double; use
round-to-nearest for these (implementation-defined,
seems to match gcc).


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42484 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
index 2fc8b0a..7f2b6e7 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
@@ -3220,7 +3220,7 @@
           APFloat apf = APFloat(APInt(MVT::getSizeInBits(VT), 2, zero));
           uint64_t x = 1ULL << ShiftAmt;
           (void)apf.convertFromInteger(&x, MVT::getSizeInBits(NVT), false, 
-                                       APFloat::rmTowardZero);
+                                       APFloat::rmNearestTiesToEven);
           Tmp2 = DAG.getConstantFP(apf, VT);
           Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
                             Node->getOperand(0), Tmp2, ISD::SETLT);
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 042868d..e286eb0 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -1598,7 +1598,7 @@
       (void)apf.convertFromInteger(&Val, 
                                MVT::getSizeInBits(Operand.getValueType()), 
                                Opcode==ISD::SINT_TO_FP,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
       return getConstantFP(apf, VT);
     }
     case ISD::BIT_CONVERT:
diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp
index c72663e..61be350 100644
--- a/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -398,7 +398,7 @@
         APFloat apf = APFloat(APInt(80, 2, zero));
         (void)apf.convertFromInteger(GV.IntVal.getRawData(), 
                                GV.IntVal.getBitWidth(), false,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
@@ -414,7 +414,7 @@
         APFloat apf = APFloat(APInt(80, 2, zero));
         (void)apf.convertFromInteger(GV.IntVal.getRawData(), 
                                GV.IntVal.getBitWidth(), true,
-                               APFloat::rmTowardZero);
+                               APFloat::rmNearestTiesToEven);
         GV.IntVal = apf.convertToAPInt();
       }
       return GV;
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index 73ca47a..72077db 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -209,25 +209,17 @@
       return ConstantInt::get(DestTy, 0);
     return 0;                   // Other pointer types cannot be casted
   case Instruction::UIToFP:
-    if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
-      double d = CI->getValue().roundToDouble();
-      if (DestTy==Type::FloatTy) 
-        return ConstantFP::get(DestTy, APFloat((float)d));
-      else if (DestTy==Type::DoubleTy)
-        return ConstantFP::get(DestTy, APFloat(d));
-      else
-        return 0;     // FIXME do this for long double
-    }
-    return 0;
   case Instruction::SIToFP:
     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
-      double d = CI->getValue().signedRoundToDouble();
-      if (DestTy==Type::FloatTy)
-        return ConstantFP::get(DestTy, APFloat((float)d));
-      else if (DestTy==Type::DoubleTy)
-        return ConstantFP::get(DestTy, APFloat(d));
-      else
-        return 0;     // FIXME do this for long double
+      APInt api = CI->getValue();
+      const uint64_t zero[] = {0, 0};
+      uint32_t BitWidth = cast<IntegerType>(SrcTy)->getBitWidth();
+      APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
+                                  2, zero));
+      (void)apf.convertFromInteger(api.getRawData(), BitWidth, 
+                                   opc==Instruction::SIToFP,
+                                   APFloat::rmNearestTiesToEven);
+      return ConstantFP::get(DestTy, apf);
     }
     return 0;
   case Instruction::ZExt: