llvm.org GIT mirror llvm / 9d5f456
Revise previous patch per review comments. Next round of x87 long double stuff. Getting close now, basically works. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41875 91177308-0d34-0410-b5e6-96231b3b80d8 Dale Johannesen 12 years ago
15 changed file(s) with 163 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
283283 float { RET_TY(Type::FloatTy, FLOAT); }
284284 double { RET_TY(Type::DoubleTy,DOUBLE);}
285285 x86_fp80 { RET_TY(Type::X86_FP80Ty, X86_FP80);}
286 fp128 { RET_TY(Type::FP128Ty, FP128);}
287 ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
286 fp128 { RET_TY(Type::FP128Ty, FP128);}
287 ppc_fp128 { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
288288 label { RET_TY(Type::LabelTy, LABEL); }
289289 type { return TYPE; }
290290 opaque { return OPAQUE; }
476476 }
477477
478478 {FPConstant} { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
479 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext));
479 {HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2));
480480 return FPVAL;
481481 }
482482 {HexFP80Constant} { uint64_t Pair[2];
483 HexToIntPair(yytext, Pair);
483 HexToIntPair(yytext+3, Pair);
484484 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
485485 return FPVAL;
486486 }
487487 {HexFP128Constant} { uint64_t Pair[2];
488 HexToIntPair(yytext, Pair);
488 HexToIntPair(yytext+3, Pair);
489489 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
490490 return FPVAL;
491491 }
492492 {HexPPC128Constant} { uint64_t Pair[2];
493 HexToIntPair(yytext, Pair);
493 HexToIntPair(yytext+3, Pair);
494494 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
495495 return FPVAL;
496496 }
18651865 $$ = ConstantInt::getFalse();
18661866 CHECK_FOR_ERROR
18671867 }
1868 | FPType FPVAL { // Float & Double constants
1868 | FPType FPVAL { // Floating point constants
18691869 if (!ConstantFP::isValueValidForType($1, *$2))
18701870 GEN_ERROR("Floating point constant invalid for type");
18711871 // Lexer has no type info, so builds all float and double FP constants
628628 V = ConstantFP::get(CurTy, APFloat(APInt(32, (uint32_t)Record[0])));
629629 else if (CurTy == Type::DoubleTy)
630630 V = ConstantFP::get(CurTy, APFloat(APInt(64, Record[0])));
631 // FIXME: Make long double constants work. BitsToDouble does not make it.
632631 else if (CurTy == Type::X86_FP80Ty)
633632 V = ConstantFP::get(CurTy, APFloat(APInt(80, 2, &Record[0])));
634633 else if (CurTy == Type::FP128Ty)
525525 } else if (const ConstantFP *CFP = dyn_cast(C)) {
526526 Code = bitc::CST_CODE_FLOAT;
527527 const Type *Ty = CFP->getType();
528 if (Ty == Type::FloatTy)
529 Record.push_back((uint32_t)*CFP->getValueAPF().convertToAPInt().
530 getRawData());
531 else if (Ty == Type::DoubleTy) {
532 Record.push_back(*CFP->getValueAPF().convertToAPInt().getRawData());
528 if (Ty == Type::FloatTy || Ty == Type::DoubleTy) {
529 Record.push_back(CFP->getValueAPF().convertToAPInt().getZExtValue());
533530 } else if (Ty == Type::X86_FP80Ty) {
534531 const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
535532 Record.push_back(p[0]);
830830 // precision...
831831 if (CFP->getType() == Type::DoubleTy) {
832832 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
833 uint64_t i = *CFP->getValueAPF().convertToAPInt().getRawData();
833 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
834834 if (TAI->getData64bitsDirective())
835835 O << TAI->getData64bitsDirective() << i << "\t"
836836 << TAI->getCommentString() << " double value: " << Val << "\n";
850850 << " double most significant word " << Val << "\n";
851851 }
852852 return;
853 } else {
853 } else if (CFP->getType() == Type::FloatTy) {
854854 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
855855 O << TAI->getData32bitsDirective()
856 << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData()
856 << CFP->getValueAPF().convertToAPInt().getZExtValue()
857857 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
858858 return;
859 }
859 } else if (CFP->getType() == Type::X86_FP80Ty) {
860 // all long double variants are printed as hex
861 const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
862 if (TD->isBigEndian()) {
863 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
864 << "\t" << TAI->getCommentString()
865 << " long double most significant halfword\n";
866 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
867 << "\t" << TAI->getCommentString()
868 << " long double next halfword\n";
869 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
870 << "\t" << TAI->getCommentString()
871 << " long double next halfword\n";
872 O << TAI->getData16bitsDirective() << uint16_t(p[0])
873 << "\t" << TAI->getCommentString()
874 << " long double next halfword\n";
875 O << TAI->getData16bitsDirective() << uint16_t(p[1])
876 << "\t" << TAI->getCommentString()
877 << " long double least significant halfword\n";
878 } else {
879 O << TAI->getData16bitsDirective() << uint16_t(p[1])
880 << "\t" << TAI->getCommentString()
881 << " long double least significant halfword\n";
882 O << TAI->getData16bitsDirective() << uint16_t(p[0])
883 << "\t" << TAI->getCommentString()
884 << " long double next halfword\n";
885 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
886 << "\t" << TAI->getCommentString()
887 << " long double next halfword\n";
888 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
889 << "\t" << TAI->getCommentString()
890 << " long double next halfword\n";
891 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
892 << "\t" << TAI->getCommentString()
893 << " long double most significant halfword\n";
894 }
895 return;
896 } else assert(0 && "Floating point constant type not handled");
860897 } else if (CV->getType() == Type::Int64Ty) {
861898 if (const ConstantInt *CI = dyn_cast(CV)) {
862899 uint64_t Val = CI->getZExtValue();
860860 break;
861861 }
862862 case Type::FloatTyID: {
863 uint32_t val = (uint32_t)*cast(PC)->
864 getValueAPF().convertToAPInt().getRawData();
863 uint32_t val = cast(PC)->getValueAPF().convertToAPInt().
864 getZExtValue();
865865 if (TD->isBigEndian())
866866 val = ByteSwap_32(val);
867867 ptr[0] = val;
871871 break;
872872 }
873873 case Type::DoubleTyID: {
874 uint64_t val = *cast(PC)->getValueAPF().convertToAPInt().
875 getRawData();
874 uint64_t val = cast(PC)->getValueAPF().convertToAPInt().
875 getZExtValue();
876876 if (TD->isBigEndian())
877877 val = ByteSwap_64(val);
878878 ptr[0] = val;
37993799 default: assert(0 && "Unknown FP type");
38003800 case MVT::f32:
38013801 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
3802 Tmp = DAG.getConstant((uint32_t)*CFP->getValueAPF().
3803 convertToAPInt().getRawData(), MVT::i32);
3802 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
3803 convertToAPInt().getZExtValue(), MVT::i32);
38043804 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
38053805 ST->getSrcValueOffset(), ST->isVolatile(),
38063806 ST->getAlignment());
38083808 break;
38093809 case MVT::f64:
38103810 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
3811 Tmp = DAG.getConstant(*CFP->getValueAPF().convertToAPInt().
3812 getRawData(), MVT::i64);
3811 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
3812 getZExtValue(), MVT::i64);
38133813 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
38143814 ST->getSrcValueOffset(), ST->isVolatile(),
38153815 ST->getAlignment());
38173817 // Many FP stores are not make apparent until after legalize, e.g. for
38183818 // argument passing. Since this is so common, custom legalize the
38193819 // 64-bit integer store into two 32-bit stores.
3820 uint64_t Val = *CFP->getValueAPF().convertToAPInt().getRawData();
3820 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
38213821 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
38223822 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
38233823 if (!TLI.isLittleEndian()) std::swap(Lo, Hi);
488488 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
489489 Type::FloatTy, CFP->getValueAPF());
490490 if (!UseCP) {
491 const APFloat& Val = LLVMC->getValueAPF();
492 return isDouble
493 ? DAG.getConstant(*Val.convertToAPInt().getRawData(), MVT::i64)
494 : DAG.getConstant((uint32_t )*Val.convertToAPInt().getRawData(),
495 MVT::i32);
491 return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
492 isDouble ? MVT::i64 : MVT::i32);
496493 }
497494
498495 if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
19801977 // together.
19811978 if (ConstantFPSDNode *CFP = dyn_cast(ST->getValue())) {
19821979 if (CFP->getValueType(0) == MVT::f32) {
1983 Tmp3 = DAG.getConstant((uint32_t)*CFP->getValueAPF().
1984 convertToAPInt().getRawData(),
1980 Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
1981 convertToAPInt().getZExtValue(),
19851982 MVT::i32);
19861983 } else {
19871984 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
1988 Tmp3 = DAG.getConstant(*CFP->getValueAPF().convertToAPInt().
1989 getRawData(), MVT::i64);
1985 Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
1986 getZExtValue(), MVT::i64);
19901987 }
19911988 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
19921989 SVOffset, isVolatile, Alignment);
108108 } else if (isa(NotZero)) {
109109 MVT::ValueType VT = NotZero.getValueType();
110110 if (VT== MVT::f64) {
111 if (*((cast(NotZero)->getValueAPF().
112 convertToAPInt().getRawData())) != (uint64_t)-1)
111 if (((cast(NotZero)->getValueAPF().
112 convertToAPInt().getZExtValue())) != (uint64_t)-1)
113113 return false;
114114 } else {
115 if ((uint32_t)*cast(NotZero)->
116 getValueAPF().convertToAPInt().getRawData() !=
115 if ((uint32_t)cast(NotZero)->
116 getValueAPF().convertToAPInt().getZExtValue() !=
117117 (uint32_t)-1)
118118 return false;
119119 }
16961696 }
16971697 case ISD::BIT_CONVERT:
16981698 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
1699 return getConstant((uint32_t)*V.convertToAPInt().getRawData(), VT);
1699 return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
17001700 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
1701 return getConstant(*V.convertToAPInt().getRawData(), VT);
1701 return getConstant(V.convertToAPInt().getZExtValue(), VT);
17021702 break;
17031703 }
17041704 }
17141714
17151715 if (FPC->getType() == Type::DoubleTy) {
17161716 double Val = FPC->getValueAPF().convertToDouble();
1717 uint64_t i = *FPC->getValueAPF().convertToAPInt().getRawData();
1717 uint64_t i = FPC->getValueAPF().convertToAPInt().getZExtValue();
17181718 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
17191719 << " = 0x" << std::hex << i << std::dec
17201720 << "ULL; /* " << Val << " */\n";
17211721 } else if (FPC->getType() == Type::FloatTy) {
17221722 float Val = FPC->getValueAPF().convertToFloat();
1723 uint32_t i = (uint32_t)*FPC->getValueAPF().convertToAPInt().
1724 getRawData();
1723 uint32_t i = (uint32_t)FPC->getValueAPF().convertToAPInt().
1724 getZExtValue();
17251725 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
17261726 << " = 0x" << std::hex << i << std::dec
17271727 << "U; /* " << Val << " */\n";
427427 uint64_t X;
428428 unsigned Size;
429429 if (FP->getType()->getTypeID()==Type::FloatTyID) {
430 X = (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData();
430 X = (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue();
431431 Size = 4;
432432 } else {
433 X = *FP->getValueAPF().convertToAPInt().getRawData();
433 X = FP->getValueAPF().convertToAPInt().getZExtValue();
434434 Size = 8;
435435 }
436436 Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')';
14721472 const ConstantFP* FP = cast(C);
14731473 if (Ty->getTypeID() == Type::FloatTyID)
14741474 Out << "int32 (" <<
1475 (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData() << ')';
1475 (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue() << ')';
14761476 else
14771477 Out << "int64 (" <<
1478 *FP->getValueAPF().convertToAPInt().getRawData() << ')';
1478 FP->getValueAPF().convertToAPInt().getZExtValue() << ')';
14791479 break;
14801480 }
14811481 case Type::ArrayTyID:
480480 else
481481 Out << CI->getValue().toStringSigned(10);
482482 } else if (const ConstantFP *CFP = dyn_cast(CV)) {
483 // We would like to output the FP constant value in exponential notation,
484 // but we cannot do this if doing so will lose precision. Check here to
485 // make sure that we only output it in exponential format if we can parse
486 // the value back and get the same value.
487 //
488 bool isDouble = &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble;
489 double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() :
490 CFP->getValueAPF().convertToFloat();
491 std::string StrVal = ftostr(CFP->getValueAPF());
492
493 // Check to make sure that the stringized number is not some string like
494 // "Inf" or NaN, that atof will accept, but the lexer will not. Check that
495 // the string matches the "[-+]?[0-9]" regex.
496 //
497 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
498 ((StrVal[0] == '-' || StrVal[0] == '+') &&
499 (StrVal[1] >= '0' && StrVal[1] <= '9')))
500 // Reparse stringized version!
501 if (atof(StrVal.c_str()) == Val) {
502 Out << StrVal;
503 return;
483 if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
484 &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
485 // We would like to output the FP constant value in exponential notation,
486 // but we cannot do this if doing so will lose precision. Check here to
487 // make sure that we only output it in exponential format if we can parse
488 // the value back and get the same value.
489 //
490 bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
491 double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() :
492 CFP->getValueAPF().convertToFloat();
493 std::string StrVal = ftostr(CFP->getValueAPF());
494
495 // Check to make sure that the stringized number is not some string like
496 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
497 // that the string matches the "[-+]?[0-9]" regex.
498 //
499 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
500 ((StrVal[0] == '-' || StrVal[0] == '+') &&
501 (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
502 // Reparse stringized version!
503 if (atof(StrVal.c_str()) == Val) {
504 Out << StrVal;
505 return;
506 }
504507 }
505
506 // Otherwise we could not reparse it to exactly the same value, so we must
507 // output the string in hexadecimal format!
508 assert(sizeof(double) == sizeof(uint64_t) &&
509 "assuming that double is 64 bits!");
510 Out << "0x" << utohexstr(DoubleToBits(Val));
511
508 // Otherwise we could not reparse it to exactly the same value, so we must
509 // output the string in hexadecimal format!
510 assert(sizeof(double) == sizeof(uint64_t) &&
511 "assuming that double is 64 bits!");
512 Out << "0x" << utohexstr(DoubleToBits(Val));
513 } else {
514 // Some form of long double. These appear as a magic letter identifying
515 // the type, then a fixed number of hex digits.
516 Out << "0x";
517 if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
518 Out << 'K';
519 else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
520 Out << 'L';
521 else
522 assert(0 && "Unsupported floating point type");
523 const uint64_t* p = CFP->getValueAPF().convertToAPInt().getRawData();
524 uint64_t word = *p;
525 int shiftcount=60;
526 int width = CFP->getValueAPF().convertToAPInt().getBitWidth();
527 for (int j=0; j
528 unsigned int nibble = (word>>shiftcount) & 15;
529 if (nibble < 10)
530 Out << (unsigned char)(nibble + '0');
531 else
532 Out << (unsigned char)(nibble - 10 + 'A');
533 if (shiftcount == 0) {
534 word = *(++p);
535 shiftcount = 60;
536 if (width-j-4 < 64)
537 shiftcount = width-j-4;
538 }
539 }
540 }
512541 } else if (isa(CV)) {
513542 Out << "zeroinitializer";
514543 } else if (const ConstantArray *CA = dyn_cast(CV)) {
8686
8787 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
8888 for (unsigned i = 0; i != SrcNumElts; ++i) {
89 uint64_t V = *cast(CV->getOperand(i))->
90 getValueAPF().convertToAPInt().getRawData();
89 uint64_t V = cast(CV->getOperand(i))->
90 getValueAPF().convertToAPInt().getZExtValue();
9191 Constant *C = ConstantInt::get(Type::Int64Ty, V);
9292 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
9393 }
9696
9797 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
9898 for (unsigned i = 0; i != SrcNumElts; ++i) {
99 uint32_t V = (uint32_t)*cast(CV->getOperand(i))->
100 getValueAPF().convertToAPInt().getRawData();
99 uint32_t V = (uint32_t)cast(CV->getOperand(i))->
100 getValueAPF().convertToAPInt().getZExtValue();
101101 Constant *C = ConstantInt::get(Type::Int32Ty, V);
102102 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
103103 }
330330 return const_cast(V);
331331
332332 if (DestTy->isFloatingPoint()) {
333 if (DestTy == Type::FloatTy)
334 return ConstantFP::get(DestTy, APFloat(CI->getValue()));
335 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
333 assert((DestTy == Type::DoubleTy || DestTy == Type::FloatTy) &&
334 "Unknown FP type!");
336335 return ConstantFP::get(DestTy, APFloat(CI->getValue()));
337336 }
338337 // Otherwise, can't fold this (vector?)
244244 // temporary
245245 if (Ty==Type::FloatTy)
246246 assert(&V.getSemantics()==&APFloat::IEEEsingle);
247 else if (Ty==Type::DoubleTy)
248 assert(&V.getSemantics()==&APFloat::IEEEdouble);
249 else if (Ty==Type::X86_FP80Ty)
250 assert(&V.getSemantics()==&APFloat::x87DoubleExtended);
251 else if (Ty==Type::FP128Ty)
252 assert(&V.getSemantics()==&APFloat::IEEEquad);
247253 else
248 assert(&V.getSemantics()==&APFloat::IEEEdouble);
254 assert(0);
249255 }
250256
251257 bool ConstantFP::isNullValue() const {
293299 // temporary
294300 if (Ty==Type::FloatTy)
295301 assert(&V.getSemantics()==&APFloat::IEEEsingle);
302 else if (Ty==Type::DoubleTy)
303 assert(&V.getSemantics()==&APFloat::IEEEdouble);
304 else if (Ty==Type::X86_FP80Ty)
305 assert(&V.getSemantics()==&APFloat::x87DoubleExtended);
306 else if (Ty==Type::FP128Ty)
307 assert(&V.getSemantics()==&APFloat::IEEEquad);
296308 else
297 assert(&V.getSemantics()==&APFloat::IEEEdouble);
309 assert(0);
298310
299311 DenseMapAPFloatKeyInfo::KeyTy Key(V);
300312 ConstantFP *&Slot = (*FPConstants)[Key];
712724 &Val2.getSemantics() == &APFloat::IEEEdouble ||
713725 Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) ==
714726 APFloat::opOK;
715 // TODO: Figure out how to test if we can use a shorter type instead!
716727 case Type::X86_FP80TyID:
717 case Type::PPC_FP128TyID:
728 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
729 &Val2.getSemantics() == &APFloat::IEEEdouble ||
730 &Val2.getSemantics() == &APFloat::x87DoubleExtended;
718731 case Type::FP128TyID:
719 return true;
732 return &Val2.getSemantics() == &APFloat::IEEEsingle ||
733 &Val2.getSemantics() == &APFloat::IEEEdouble ||
734 &Val2.getSemantics() == &APFloat::IEEEquad;
720735 }
721736 }
722737
249249 }
250250 else if (CFP->getType() == Type::DoubleTy)
251251 Out << "BitsToDouble(0x" << std::hex
252 << *CFP->getValueAPF().convertToAPInt().getRawData()
252 << CFP->getValueAPF().convertToAPInt().getZExtValue()
253253 << std::dec << "ULL) /* " << StrVal << " */";
254254 else
255255 Out << "BitsToFloat(0x" << std::hex
256 << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData()
256 << (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue()
257257 << std::dec << "U) /* " << StrVal << " */";
258258 Out << ")";
259259 #if HAVE_PRINTF_A