Change LegalFPImmediates to use APFloat.
Add APFloat interfaces to ConstantFP, SelectionDAG.
Fix integer bit in double->APFloat conversion.
Convert LegalizeDAG to use APFloat interface in
ConstantFPSDNode uses.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41587 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
index c7803ca..265209c 100644
--- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
@@ -487,15 +487,15 @@
   MVT::ValueType VT = CFP->getValueType(0);
   bool isDouble = VT == MVT::f64;
   ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
-                                      Type::FloatTy, CFP->getValue());
+                                      Type::FloatTy, CFP->getValueAPF());
   if (!UseCP) {
-    double Val = LLVMC->getValue();
+    const APFloat& Val = LLVMC->getValueAPF();
     return isDouble
-      ? DAG.getConstant(DoubleToBits(Val), MVT::i64)
-      : DAG.getConstant(FloatToBits(Val), MVT::i32);
+      ? DAG.getConstant(DoubleToBits(Val.convertToDouble()), MVT::i64)
+      : DAG.getConstant(FloatToBits(Val.convertToFloat()), MVT::i32);
   }
 
-  if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
+  if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
       // Only do this if the target has a native EXTLOAD instruction from f32.
       TLI.isLoadXLegal(ISD::EXTLOAD, MVT::f32)) {
     LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC,Type::FloatTy));
@@ -1017,7 +1017,8 @@
 
     // If this is a legal constant, turn it into a TargetConstantFP node.
     if (isLegal) {
-      Result = DAG.getTargetConstantFP(CFP->getValue(), CFP->getValueType(0));
+      Result = DAG.getTargetConstantFP(CFP->getValueAPF(), 
+                                       CFP->getValueType(0));
       break;
     }
 
@@ -1942,10 +1943,12 @@
       // together.
       if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
         if (CFP->getValueType(0) == MVT::f32) {
-          Tmp3 = DAG.getConstant(FloatToBits(CFP->getValue()), MVT::i32);
+          Tmp3 = DAG.getConstant(FloatToBits(CFP->getValueAPF().
+                                             convertToFloat()), MVT::i32);
         } else {
           assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
-          Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValue()), MVT::i64);
+          Tmp3 = DAG.getConstant(DoubleToBits(CFP->getValueAPF().
+                                              convertToDouble()), MVT::i64);
         }
         Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
                               SVOffset, isVolatile, Alignment);
@@ -4212,7 +4215,7 @@
     for (unsigned i = 0, e = NumElems; i != e; ++i) {
       if (ConstantFPSDNode *V = 
           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
-        CV.push_back(ConstantFP::get(OpNTy, V->getValue()));
+        CV.push_back(ConstantFP::get(OpNTy, V->getValueAPF()));
       } else if (ConstantSDNode *V = 
                  dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
         CV.push_back(ConstantInt::get(OpNTy, V->getValue()));
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 055834b..d544e8e 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -52,6 +52,32 @@
   return Value.bitwiseIsEqual(V);
 }
 
+bool ConstantFPSDNode::isValueValidForType(MVT::ValueType VT, 
+                                           const APFloat& Val) {
+  // convert modifies in place, so make a copy.
+  APFloat Val2 = APFloat(Val);
+  switch (VT) {
+  default:
+    return false;         // These can't be represented as floating point!
+
+  // FIXME rounding mode needs to be more flexible
+  case MVT::f32:
+    return &Val2.getSemantics() == &APFloat::IEEEsingle ||
+           Val2.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven) == 
+              APFloat::opOK;
+  case MVT::f64:
+    return &Val2.getSemantics() == &APFloat::IEEEsingle || 
+           &Val2.getSemantics() == &APFloat::IEEEdouble ||
+           Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) == 
+             APFloat::opOK;
+  // TODO: Figure out how to test if we can use a shorter type instead!
+  case MVT::f80:
+  case MVT::f128:
+  case MVT::ppcf128:
+    return true;
+  }
+}
+
 //===----------------------------------------------------------------------===//
 //                              ISD Namespace
 //===----------------------------------------------------------------------===//
@@ -669,18 +695,20 @@
   return SDOperand(N, 0);
 }
 
-SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
+SDOperand SelectionDAG::getConstantFP(const APFloat& V, MVT::ValueType VT,
                                       bool isTarget) {
   assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
+                                
   MVT::ValueType EltVT =
     MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
-  if (EltVT == MVT::f32)
-    Val = (float)Val;  // Mask out extra precision.
+  bool isDouble = (EltVT == MVT::f64);
+  double Val = isDouble ? V.convertToDouble() : (double)V.convertToFloat();
 
   // Do the map lookup using the actual bit pattern for the floating point
   // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
   // we don't have issues with SNANs.
   unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
+  // ?? Should we store float/double/longdouble separately in ID?
   FoldingSetNodeID ID;
   AddNodeIDNode(ID, Opc, getVTList(EltVT), 0, 0);
   ID.AddDouble(Val);
@@ -704,6 +732,16 @@
   return Result;
 }
 
+SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT,
+                                      bool isTarget) {
+  MVT::ValueType EltVT =
+    MVT::isVector(VT) ? MVT::getVectorElementType(VT) : VT;
+  if (EltVT==MVT::f32)
+    return getConstantFP(APFloat((float)Val), VT, isTarget);
+  else
+    return getConstantFP(APFloat(Val), VT, isTarget);
+}
+
 SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
                                          MVT::ValueType VT, int Offset,
                                          bool isTargetGA) {