From 028084efe5a297af30f17ab0f82d3bf86199372f Mon Sep 17 00:00:00 2001 From: Dale Johannesen Date: Wed, 12 Sep 2007 03:30:33 +0000 Subject: Revise previous patch per review comments. Next round of x87 long double stuff. Getting close now, basically works. llvm-svn: 41875 --- llvm/lib/AsmParser/Lexer.l | 12 ++-- llvm/lib/AsmParser/llvmAsmParser.y | 2 +- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 1 - llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 7 +-- llvm/lib/CodeGen/AsmPrinter.cpp | 45 ++++++++++++-- llvm/lib/CodeGen/MachOWriter.cpp | 8 +-- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 10 +-- llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp | 15 ++--- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 12 ++-- llvm/lib/Target/CBackend/CBackend.cpp | 6 +- llvm/lib/Target/MSIL/MSILWriter.cpp | 8 +-- llvm/lib/VMCore/AsmWriter.cpp | 85 +++++++++++++++++--------- llvm/lib/VMCore/ConstantFold.cpp | 13 ++-- llvm/lib/VMCore/Constants.cpp | 25 ++++++-- llvm/tools/llvm2cpp/CppWriter.cpp | 4 +- 15 files changed, 163 insertions(+), 90 deletions(-) diff --git a/llvm/lib/AsmParser/Lexer.l b/llvm/lib/AsmParser/Lexer.l index 5acbbac..73e9c54 100644 --- a/llvm/lib/AsmParser/Lexer.l +++ b/llvm/lib/AsmParser/Lexer.l @@ -284,8 +284,8 @@ void { RET_TY(Type::VoidTy, VOID); } float { RET_TY(Type::FloatTy, FLOAT); } double { RET_TY(Type::DoubleTy,DOUBLE);} x86_fp80 { RET_TY(Type::X86_FP80Ty, X86_FP80);} -fp128 { RET_TY(Type::FP128Ty, FP128);} -ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);} +fp128 { RET_TY(Type::FP128Ty, FP128);} +ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);} label { RET_TY(Type::LabelTy, LABEL); } type { return TYPE; } opaque { return OPAQUE; } @@ -477,21 +477,21 @@ shufflevector { RET_TOK(OtherOpVal, ShuffleVector, SHUFFLEVECTOR); } } {FPConstant} { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; } -{HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext)); +{HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2)); return FPVAL; } {HexFP80Constant} { uint64_t Pair[2]; - HexToIntPair(yytext, Pair); + HexToIntPair(yytext+3, Pair); llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair)); return FPVAL; } {HexFP128Constant} { uint64_t Pair[2]; - HexToIntPair(yytext, Pair); + HexToIntPair(yytext+3, Pair); llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair)); return FPVAL; } {HexPPC128Constant} { uint64_t Pair[2]; - HexToIntPair(yytext, Pair); + HexToIntPair(yytext+3, Pair); llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair)); return FPVAL; } diff --git a/llvm/lib/AsmParser/llvmAsmParser.y b/llvm/lib/AsmParser/llvmAsmParser.y index c0374da..d2e5256 100644 --- a/llvm/lib/AsmParser/llvmAsmParser.y +++ b/llvm/lib/AsmParser/llvmAsmParser.y @@ -1866,7 +1866,7 @@ ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr $$ = ConstantInt::getFalse(); CHECK_FOR_ERROR } - | FPType FPVAL { // Float & Double constants + | FPType FPVAL { // Floating point constants if (!ConstantFP::isValueValidForType($1, *$2)) GEN_ERROR("Floating point constant invalid for type"); // Lexer has no type info, so builds all float and double FP constants diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 0642993..9e4a3de 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -629,7 +629,6 @@ bool BitcodeReader::ParseConstants() { V = ConstantFP::get(CurTy, APFloat(APInt(32, (uint32_t)Record[0]))); else if (CurTy == Type::DoubleTy) V = ConstantFP::get(CurTy, APFloat(APInt(64, Record[0]))); - // FIXME: Make long double constants work. BitsToDouble does not make it. else if (CurTy == Type::X86_FP80Ty) V = ConstantFP::get(CurTy, APFloat(APInt(80, 2, &Record[0]))); else if (CurTy == Type::FP128Ty) diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 78a16d8..fdaa9be5 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -526,11 +526,8 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal, } else if (const ConstantFP *CFP = dyn_cast(C)) { Code = bitc::CST_CODE_FLOAT; const Type *Ty = CFP->getType(); - if (Ty == Type::FloatTy) - Record.push_back((uint32_t)*CFP->getValueAPF().convertToAPInt(). - getRawData()); - else if (Ty == Type::DoubleTy) { - Record.push_back(*CFP->getValueAPF().convertToAPInt().getRawData()); + if (Ty == Type::FloatTy || Ty == Type::DoubleTy) { + Record.push_back(CFP->getValueAPF().convertToAPInt().getZExtValue()); } else if (Ty == Type::X86_FP80Ty) { const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData(); Record.push_back(p[0]); diff --git a/llvm/lib/CodeGen/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter.cpp index 49bcba7..b553106 100644 --- a/llvm/lib/CodeGen/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter.cpp @@ -831,7 +831,7 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) { // precision... if (CFP->getType() == Type::DoubleTy) { double Val = CFP->getValueAPF().convertToDouble(); // for comment only - uint64_t i = *CFP->getValueAPF().convertToAPInt().getRawData(); + uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue(); if (TAI->getData64bitsDirective()) O << TAI->getData64bitsDirective() << i << "\t" << TAI->getCommentString() << " double value: " << Val << "\n"; @@ -851,13 +851,50 @@ void AsmPrinter::EmitGlobalConstant(const Constant *CV) { << " double most significant word " << Val << "\n"; } return; - } else { + } else if (CFP->getType() == Type::FloatTy) { float Val = CFP->getValueAPF().convertToFloat(); // for comment only O << TAI->getData32bitsDirective() - << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData() + << CFP->getValueAPF().convertToAPInt().getZExtValue() << "\t" << TAI->getCommentString() << " float " << Val << "\n"; return; - } + } else if (CFP->getType() == Type::X86_FP80Ty) { + // all long double variants are printed as hex + const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData(); + if (TD->isBigEndian()) { + O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48) + << "\t" << TAI->getCommentString() + << " long double most significant halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32) + << "\t" << TAI->getCommentString() + << " long double next halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16) + << "\t" << TAI->getCommentString() + << " long double next halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0]) + << "\t" << TAI->getCommentString() + << " long double next halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[1]) + << "\t" << TAI->getCommentString() + << " long double least significant halfword\n"; + } else { + O << TAI->getData16bitsDirective() << uint16_t(p[1]) + << "\t" << TAI->getCommentString() + << " long double least significant halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0]) + << "\t" << TAI->getCommentString() + << " long double next halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16) + << "\t" << TAI->getCommentString() + << " long double next halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32) + << "\t" << TAI->getCommentString() + << " long double next halfword\n"; + O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48) + << "\t" << TAI->getCommentString() + << " long double most significant halfword\n"; + } + return; + } else assert(0 && "Floating point constant type not handled"); } else if (CV->getType() == Type::Int64Ty) { if (const ConstantInt *CI = dyn_cast(CV)) { uint64_t Val = CI->getZExtValue(); diff --git a/llvm/lib/CodeGen/MachOWriter.cpp b/llvm/lib/CodeGen/MachOWriter.cpp index 1c9b0fe..0c74375 100644 --- a/llvm/lib/CodeGen/MachOWriter.cpp +++ b/llvm/lib/CodeGen/MachOWriter.cpp @@ -861,8 +861,8 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, break; } case Type::FloatTyID: { - uint32_t val = (uint32_t)*cast(PC)-> - getValueAPF().convertToAPInt().getRawData(); + uint32_t val = cast(PC)->getValueAPF().convertToAPInt(). + getZExtValue(); if (TD->isBigEndian()) val = ByteSwap_32(val); ptr[0] = val; @@ -872,8 +872,8 @@ void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, break; } case Type::DoubleTyID: { - uint64_t val = *cast(PC)->getValueAPF().convertToAPInt(). - getRawData(); + uint64_t val = cast(PC)->getValueAPF().convertToAPInt(). + getZExtValue(); if (TD->isBigEndian()) val = ByteSwap_64(val); ptr[0] = val; diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 2050d23..68158be 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -3800,8 +3800,8 @@ SDOperand DAGCombiner::visitSTORE(SDNode *N) { default: assert(0 && "Unknown FP type"); case MVT::f32: if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) { - Tmp = DAG.getConstant((uint32_t)*CFP->getValueAPF(). - convertToAPInt().getRawData(), MVT::i32); + Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF(). + convertToAPInt().getZExtValue(), MVT::i32); return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(), ST->getSrcValueOffset(), ST->isVolatile(), ST->getAlignment()); @@ -3809,8 +3809,8 @@ SDOperand DAGCombiner::visitSTORE(SDNode *N) { break; case MVT::f64: if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) { - Tmp = DAG.getConstant(*CFP->getValueAPF().convertToAPInt(). - getRawData(), MVT::i64); + Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt(). + getZExtValue(), MVT::i64); return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(), ST->getSrcValueOffset(), ST->isVolatile(), ST->getAlignment()); @@ -3818,7 +3818,7 @@ SDOperand DAGCombiner::visitSTORE(SDNode *N) { // Many FP stores are not make apparent until after legalize, e.g. for // argument passing. Since this is so common, custom legalize the // 64-bit integer store into two 32-bit stores. - uint64_t Val = *CFP->getValueAPF().convertToAPInt().getRawData(); + uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue(); SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32); SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32); if (!TLI.isLittleEndian()) std::swap(Lo, Hi); diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp index d7eb85b..da55eaf 100644 --- a/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp @@ -489,11 +489,8 @@ static SDOperand ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP, ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy : Type::FloatTy, CFP->getValueAPF()); if (!UseCP) { - const APFloat& Val = LLVMC->getValueAPF(); - return isDouble - ? DAG.getConstant(*Val.convertToAPInt().getRawData(), MVT::i64) - : DAG.getConstant((uint32_t )*Val.convertToAPInt().getRawData(), - MVT::i32); + return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(), + isDouble ? MVT::i64 : MVT::i32); } if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) && @@ -1981,13 +1978,13 @@ SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) { // together. if (ConstantFPSDNode *CFP = dyn_cast(ST->getValue())) { if (CFP->getValueType(0) == MVT::f32) { - Tmp3 = DAG.getConstant((uint32_t)*CFP->getValueAPF(). - convertToAPInt().getRawData(), + Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF(). + convertToAPInt().getZExtValue(), MVT::i32); } else { assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!"); - Tmp3 = DAG.getConstant(*CFP->getValueAPF().convertToAPInt(). - getRawData(), MVT::i64); + Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt(). + getZExtValue(), MVT::i64); } Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset, isVolatile, Alignment); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp index 85a76f4..ed1777e 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp @@ -109,12 +109,12 @@ bool ISD::isBuildVectorAllOnes(const SDNode *N) { } else if (isa(NotZero)) { MVT::ValueType VT = NotZero.getValueType(); if (VT== MVT::f64) { - if (*((cast(NotZero)->getValueAPF(). - convertToAPInt().getRawData())) != (uint64_t)-1) + if (((cast(NotZero)->getValueAPF(). + convertToAPInt().getZExtValue())) != (uint64_t)-1) return false; } else { - if ((uint32_t)*cast(NotZero)-> - getValueAPF().convertToAPInt().getRawData() != + if ((uint32_t)cast(NotZero)-> + getValueAPF().convertToAPInt().getZExtValue() != (uint32_t)-1) return false; } @@ -1697,9 +1697,9 @@ SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT, } case ISD::BIT_CONVERT: if (VT == MVT::i32 && C->getValueType(0) == MVT::f32) - return getConstant((uint32_t)*V.convertToAPInt().getRawData(), VT); + return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT); else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64) - return getConstant(*V.convertToAPInt().getRawData(), VT); + return getConstant(V.convertToAPInt().getZExtValue(), VT); break; } } diff --git a/llvm/lib/Target/CBackend/CBackend.cpp b/llvm/lib/Target/CBackend/CBackend.cpp index f25b113..9fc30c4 100644 --- a/llvm/lib/Target/CBackend/CBackend.cpp +++ b/llvm/lib/Target/CBackend/CBackend.cpp @@ -1715,14 +1715,14 @@ void CWriter::printFloatingPointConstants(Function &F) { if (FPC->getType() == Type::DoubleTy) { double Val = FPC->getValueAPF().convertToDouble(); - uint64_t i = *FPC->getValueAPF().convertToAPInt().getRawData(); + uint64_t i = FPC->getValueAPF().convertToAPInt().getZExtValue(); Out << "static const ConstantDoubleTy FPConstant" << FPCounter++ << " = 0x" << std::hex << i << std::dec << "ULL; /* " << Val << " */\n"; } else if (FPC->getType() == Type::FloatTy) { float Val = FPC->getValueAPF().convertToFloat(); - uint32_t i = (uint32_t)*FPC->getValueAPF().convertToAPInt(). - getRawData(); + uint32_t i = (uint32_t)FPC->getValueAPF().convertToAPInt(). + getZExtValue(); Out << "static const ConstantFloatTy FPConstant" << FPCounter++ << " = 0x" << std::hex << i << std::dec << "U; /* " << Val << " */\n"; diff --git a/llvm/lib/Target/MSIL/MSILWriter.cpp b/llvm/lib/Target/MSIL/MSILWriter.cpp index cd67df1..5d6740b 100644 --- a/llvm/lib/Target/MSIL/MSILWriter.cpp +++ b/llvm/lib/Target/MSIL/MSILWriter.cpp @@ -428,10 +428,10 @@ void MSILWriter::printConstLoad(const Constant* C) { uint64_t X; unsigned Size; if (FP->getType()->getTypeID()==Type::FloatTyID) { - X = (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData(); + X = (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue(); Size = 4; } else { - X = *FP->getValueAPF().convertToAPInt().getRawData(); + X = FP->getValueAPF().convertToAPInt().getZExtValue(); Size = 8; } Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')'; @@ -1473,10 +1473,10 @@ void MSILWriter::printStaticConstant(const Constant* C, uint64_t& Offset) { const ConstantFP* FP = cast(C); if (Ty->getTypeID() == Type::FloatTyID) Out << "int32 (" << - (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData() << ')'; + (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue() << ')'; else Out << "int64 (" << - *FP->getValueAPF().convertToAPInt().getRawData() << ')'; + FP->getValueAPF().convertToAPInt().getZExtValue() << ')'; break; } case Type::ArrayTyID: diff --git a/llvm/lib/VMCore/AsmWriter.cpp b/llvm/lib/VMCore/AsmWriter.cpp index a7b1239..b96fbff 100644 --- a/llvm/lib/VMCore/AsmWriter.cpp +++ b/llvm/lib/VMCore/AsmWriter.cpp @@ -481,35 +481,64 @@ static void WriteConstantInt(std::ostream &Out, const Constant *CV, else Out << CI->getValue().toStringSigned(10); } else if (const ConstantFP *CFP = dyn_cast(CV)) { - // We would like to output the FP constant value in exponential notation, - // but we cannot do this if doing so will lose precision. Check here to - // make sure that we only output it in exponential format if we can parse - // the value back and get the same value. - // - bool isDouble = &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble; - double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() : - CFP->getValueAPF().convertToFloat(); - std::string StrVal = ftostr(CFP->getValueAPF()); - - // Check to make sure that the stringized number is not some string like - // "Inf" or NaN, that atof will accept, but the lexer will not. Check that - // the string matches the "[-+]?[0-9]" regex. - // - if ((StrVal[0] >= '0' && StrVal[0] <= '9') || - ((StrVal[0] == '-' || StrVal[0] == '+') && - (StrVal[1] >= '0' && StrVal[1] <= '9'))) - // Reparse stringized version! - if (atof(StrVal.c_str()) == Val) { - Out << StrVal; - return; + if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble || + &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) { + // We would like to output the FP constant value in exponential notation, + // but we cannot do this if doing so will lose precision. Check here to + // make sure that we only output it in exponential format if we can parse + // the value back and get the same value. + // + bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble; + double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() : + CFP->getValueAPF().convertToFloat(); + std::string StrVal = ftostr(CFP->getValueAPF()); + + // Check to make sure that the stringized number is not some string like + // "Inf" or NaN, that atof will accept, but the lexer will not. Check + // that the string matches the "[-+]?[0-9]" regex. + // + if ((StrVal[0] >= '0' && StrVal[0] <= '9') || + ((StrVal[0] == '-' || StrVal[0] == '+') && + (StrVal[1] >= '0' && StrVal[1] <= '9'))) { + // Reparse stringized version! + if (atof(StrVal.c_str()) == Val) { + Out << StrVal; + return; + } } - - // Otherwise we could not reparse it to exactly the same value, so we must - // output the string in hexadecimal format! - assert(sizeof(double) == sizeof(uint64_t) && - "assuming that double is 64 bits!"); - Out << "0x" << utohexstr(DoubleToBits(Val)); - + // Otherwise we could not reparse it to exactly the same value, so we must + // output the string in hexadecimal format! + assert(sizeof(double) == sizeof(uint64_t) && + "assuming that double is 64 bits!"); + Out << "0x" << utohexstr(DoubleToBits(Val)); + } else { + // Some form of long double. These appear as a magic letter identifying + // the type, then a fixed number of hex digits. + Out << "0x"; + if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended) + Out << 'K'; + else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad) + Out << 'L'; + else + assert(0 && "Unsupported floating point type"); + const uint64_t* p = CFP->getValueAPF().convertToAPInt().getRawData(); + uint64_t word = *p; + int shiftcount=60; + int width = CFP->getValueAPF().convertToAPInt().getBitWidth(); + for (int j=0; j>shiftcount) & 15; + if (nibble < 10) + Out << (unsigned char)(nibble + '0'); + else + Out << (unsigned char)(nibble - 10 + 'A'); + if (shiftcount == 0) { + word = *(++p); + shiftcount = 60; + if (width-j-4 < 64) + shiftcount = width-j-4; + } + } + } } else if (isa(CV)) { Out << "zeroinitializer"; } else if (const ConstantArray *CA = dyn_cast(CV)) { diff --git a/llvm/lib/VMCore/ConstantFold.cpp b/llvm/lib/VMCore/ConstantFold.cpp index e11b749..5c80a37 100644 --- a/llvm/lib/VMCore/ConstantFold.cpp +++ b/llvm/lib/VMCore/ConstantFold.cpp @@ -87,8 +87,8 @@ static Constant *CastConstantVector(ConstantVector *CV, if (SrcEltTy->getTypeID() == Type::DoubleTyID) { for (unsigned i = 0; i != SrcNumElts; ++i) { - uint64_t V = *cast(CV->getOperand(i))-> - getValueAPF().convertToAPInt().getRawData(); + uint64_t V = cast(CV->getOperand(i))-> + getValueAPF().convertToAPInt().getZExtValue(); Constant *C = ConstantInt::get(Type::Int64Ty, V); Result.push_back(ConstantExpr::getBitCast(C, DstEltTy )); } @@ -97,8 +97,8 @@ static Constant *CastConstantVector(ConstantVector *CV, assert(SrcEltTy->getTypeID() == Type::FloatTyID); for (unsigned i = 0; i != SrcNumElts; ++i) { - uint32_t V = (uint32_t)*cast(CV->getOperand(i))-> - getValueAPF().convertToAPInt().getRawData(); + uint32_t V = (uint32_t)cast(CV->getOperand(i))-> + getValueAPF().convertToAPInt().getZExtValue(); Constant *C = ConstantInt::get(Type::Int32Ty, V); Result.push_back(ConstantExpr::getBitCast(C, DstEltTy)); } @@ -331,9 +331,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V, return const_cast(V); if (DestTy->isFloatingPoint()) { - if (DestTy == Type::FloatTy) - return ConstantFP::get(DestTy, APFloat(CI->getValue())); - assert(DestTy == Type::DoubleTy && "Unknown FP type!"); + assert((DestTy == Type::DoubleTy || DestTy == Type::FloatTy) && + "Unknown FP type!"); return ConstantFP::get(DestTy, APFloat(CI->getValue())); } // Otherwise, can't fold this (vector?) diff --git a/llvm/lib/VMCore/Constants.cpp b/llvm/lib/VMCore/Constants.cpp index 1708e46..c546045 100644 --- a/llvm/lib/VMCore/Constants.cpp +++ b/llvm/lib/VMCore/Constants.cpp @@ -245,8 +245,14 @@ ConstantFP::ConstantFP(const Type *Ty, const APFloat& V) // temporary if (Ty==Type::FloatTy) assert(&V.getSemantics()==&APFloat::IEEEsingle); - else + else if (Ty==Type::DoubleTy) assert(&V.getSemantics()==&APFloat::IEEEdouble); + else if (Ty==Type::X86_FP80Ty) + assert(&V.getSemantics()==&APFloat::x87DoubleExtended); + else if (Ty==Type::FP128Ty) + assert(&V.getSemantics()==&APFloat::IEEEquad); + else + assert(0); } bool ConstantFP::isNullValue() const { @@ -294,8 +300,14 @@ ConstantFP *ConstantFP::get(const Type *Ty, const APFloat& V) { // temporary if (Ty==Type::FloatTy) assert(&V.getSemantics()==&APFloat::IEEEsingle); - else + else if (Ty==Type::DoubleTy) assert(&V.getSemantics()==&APFloat::IEEEdouble); + else if (Ty==Type::X86_FP80Ty) + assert(&V.getSemantics()==&APFloat::x87DoubleExtended); + else if (Ty==Type::FP128Ty) + assert(&V.getSemantics()==&APFloat::IEEEquad); + else + assert(0); DenseMapAPFloatKeyInfo::KeyTy Key(V); ConstantFP *&Slot = (*FPConstants)[Key]; @@ -713,11 +725,14 @@ bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { &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 Type::X86_FP80TyID: - case Type::PPC_FP128TyID: + return &Val2.getSemantics() == &APFloat::IEEEsingle || + &Val2.getSemantics() == &APFloat::IEEEdouble || + &Val2.getSemantics() == &APFloat::x87DoubleExtended; case Type::FP128TyID: - return true; + return &Val2.getSemantics() == &APFloat::IEEEsingle || + &Val2.getSemantics() == &APFloat::IEEEdouble || + &Val2.getSemantics() == &APFloat::IEEEquad; } } diff --git a/llvm/tools/llvm2cpp/CppWriter.cpp b/llvm/tools/llvm2cpp/CppWriter.cpp index a56c7cd..8fae51d 100644 --- a/llvm/tools/llvm2cpp/CppWriter.cpp +++ b/llvm/tools/llvm2cpp/CppWriter.cpp @@ -250,11 +250,11 @@ CppWriter::printCFP(const ConstantFP *CFP) { } else if (CFP->getType() == Type::DoubleTy) Out << "BitsToDouble(0x" << std::hex - << *CFP->getValueAPF().convertToAPInt().getRawData() + << CFP->getValueAPF().convertToAPInt().getZExtValue() << std::dec << "ULL) /* " << StrVal << " */"; else Out << "BitsToFloat(0x" << std::hex - << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData() + << (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue() << std::dec << "U) /* " << StrVal << " */"; Out << ")"; #if HAVE_PRINTF_A -- cgit v1.1