From: Dale Johannesen Date: Sun, 30 Sep 2007 18:19:03 +0000 (+0000) Subject: Constant fold int-to-long-double conversions; X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=88216af3ea4bb1c68a8793ed1d3b30308b64ab0e;p=oota-llvm.git 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 2fc8b0a7b9d..7f2b6e7630a 100644 --- a/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp +++ b/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp @@ -3220,7 +3220,7 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { 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 042868d7bb1..e286eb0ec5b 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -1598,7 +1598,7 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, (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 c72663e0d4d..61be35097b9 100644 --- a/lib/ExecutionEngine/ExecutionEngine.cpp +++ b/lib/ExecutionEngine/ExecutionEngine.cpp @@ -398,7 +398,7 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 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 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 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 73ca47a9aa5..72077db3780 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -209,25 +209,17 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, return ConstantInt::get(DestTy, 0); return 0; // Other pointer types cannot be casted case Instruction::UIToFP: - if (const ConstantInt *CI = dyn_cast(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(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(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: