LLVM API Documentation

LegalizeFloatTypes.cpp
Go to the documentation of this file.
00001 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements float type expansion and softening for LegalizeTypes.
00011 // Softening is the act of turning a computation in an illegal floating point
00012 // type into a computation in an integer type of the same size; also known as
00013 // "soft float".  For example, turning f32 arithmetic into operations using i32.
00014 // The resulting integer value is the same as what you would get by performing
00015 // the floating point operation and bitcasting the result to the integer type.
00016 // Expansion is the act of changing a computation in an illegal type to be a
00017 // computation in two identical registers of a smaller type.  For example,
00018 // implementing ppcf128 arithmetic in two f64 registers.
00019 //
00020 //===----------------------------------------------------------------------===//
00021 
00022 #include "LegalizeTypes.h"
00023 #include "llvm/Support/ErrorHandling.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 using namespace llvm;
00026 
00027 #define DEBUG_TYPE "legalize-types"
00028 
00029 /// GetFPLibCall - Return the right libcall for the given floating point type.
00030 static RTLIB::Libcall GetFPLibCall(EVT VT,
00031                                    RTLIB::Libcall Call_F32,
00032                                    RTLIB::Libcall Call_F64,
00033                                    RTLIB::Libcall Call_F80,
00034                                    RTLIB::Libcall Call_F128,
00035                                    RTLIB::Libcall Call_PPCF128) {
00036   return
00037     VT == MVT::f32 ? Call_F32 :
00038     VT == MVT::f64 ? Call_F64 :
00039     VT == MVT::f80 ? Call_F80 :
00040     VT == MVT::f128 ? Call_F128 :
00041     VT == MVT::ppcf128 ? Call_PPCF128 :
00042     RTLIB::UNKNOWN_LIBCALL;
00043 }
00044 
00045 //===----------------------------------------------------------------------===//
00046 //  Result Float to Integer Conversion.
00047 //===----------------------------------------------------------------------===//
00048 
00049 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
00050   DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
00051         dbgs() << "\n");
00052   SDValue R = SDValue();
00053 
00054   switch (N->getOpcode()) {
00055   default:
00056 #ifndef NDEBUG
00057     dbgs() << "SoftenFloatResult #" << ResNo << ": ";
00058     N->dump(&DAG); dbgs() << "\n";
00059 #endif
00060     llvm_unreachable("Do not know how to soften the result of this operator!");
00061 
00062     case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
00063     case ISD::BITCAST:     R = SoftenFloatRes_BITCAST(N); break;
00064     case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
00065     case ISD::ConstantFP:
00066       R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
00067       break;
00068     case ISD::EXTRACT_VECTOR_ELT:
00069       R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
00070     case ISD::FABS:        R = SoftenFloatRes_FABS(N); break;
00071     case ISD::FADD:        R = SoftenFloatRes_FADD(N); break;
00072     case ISD::FCEIL:       R = SoftenFloatRes_FCEIL(N); break;
00073     case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
00074     case ISD::FCOS:        R = SoftenFloatRes_FCOS(N); break;
00075     case ISD::FDIV:        R = SoftenFloatRes_FDIV(N); break;
00076     case ISD::FEXP:        R = SoftenFloatRes_FEXP(N); break;
00077     case ISD::FEXP2:       R = SoftenFloatRes_FEXP2(N); break;
00078     case ISD::FFLOOR:      R = SoftenFloatRes_FFLOOR(N); break;
00079     case ISD::FLOG:        R = SoftenFloatRes_FLOG(N); break;
00080     case ISD::FLOG2:       R = SoftenFloatRes_FLOG2(N); break;
00081     case ISD::FLOG10:      R = SoftenFloatRes_FLOG10(N); break;
00082     case ISD::FMA:         R = SoftenFloatRes_FMA(N); break;
00083     case ISD::FMUL:        R = SoftenFloatRes_FMUL(N); break;
00084     case ISD::FNEARBYINT:  R = SoftenFloatRes_FNEARBYINT(N); break;
00085     case ISD::FNEG:        R = SoftenFloatRes_FNEG(N); break;
00086     case ISD::FP_EXTEND:   R = SoftenFloatRes_FP_EXTEND(N); break;
00087     case ISD::FP_ROUND:    R = SoftenFloatRes_FP_ROUND(N); break;
00088     case ISD::FP16_TO_FP:  R = SoftenFloatRes_FP16_TO_FP(N); break;
00089     case ISD::FPOW:        R = SoftenFloatRes_FPOW(N); break;
00090     case ISD::FPOWI:       R = SoftenFloatRes_FPOWI(N); break;
00091     case ISD::FREM:        R = SoftenFloatRes_FREM(N); break;
00092     case ISD::FRINT:       R = SoftenFloatRes_FRINT(N); break;
00093     case ISD::FROUND:      R = SoftenFloatRes_FROUND(N); break;
00094     case ISD::FSIN:        R = SoftenFloatRes_FSIN(N); break;
00095     case ISD::FSQRT:       R = SoftenFloatRes_FSQRT(N); break;
00096     case ISD::FSUB:        R = SoftenFloatRes_FSUB(N); break;
00097     case ISD::FTRUNC:      R = SoftenFloatRes_FTRUNC(N); break;
00098     case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
00099     case ISD::SELECT:      R = SoftenFloatRes_SELECT(N); break;
00100     case ISD::SELECT_CC:   R = SoftenFloatRes_SELECT_CC(N); break;
00101     case ISD::SINT_TO_FP:
00102     case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
00103     case ISD::UNDEF:       R = SoftenFloatRes_UNDEF(N); break;
00104     case ISD::VAARG:       R = SoftenFloatRes_VAARG(N); break;
00105   }
00106 
00107   // If R is null, the sub-method took care of registering the result.
00108   if (R.getNode())
00109     SetSoftenedFloat(SDValue(N, ResNo), R);
00110 }
00111 
00112 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
00113   return BitConvertToInteger(N->getOperand(0));
00114 }
00115 
00116 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
00117                                                       unsigned ResNo) {
00118   SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
00119   return BitConvertToInteger(Op);
00120 }
00121 
00122 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
00123   // Convert the inputs to integers, and build a new pair out of them.
00124   return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
00125                      TLI.getTypeToTransformTo(*DAG.getContext(),
00126                                               N->getValueType(0)),
00127                      BitConvertToInteger(N->getOperand(0)),
00128                      BitConvertToInteger(N->getOperand(1)));
00129 }
00130 
00131 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
00132   return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
00133                          TLI.getTypeToTransformTo(*DAG.getContext(),
00134                                                   N->getValueType(0)));
00135 }
00136 
00137 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
00138   SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
00139   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
00140                      NewOp.getValueType().getVectorElementType(),
00141                      NewOp, N->getOperand(1));
00142 }
00143 
00144 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
00145   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00146   unsigned Size = NVT.getSizeInBits();
00147 
00148   // Mask = ~(1 << (Size-1))
00149   APInt API = APInt::getAllOnesValue(Size);
00150   API.clearBit(Size-1);
00151   SDValue Mask = DAG.getConstant(API, NVT);
00152   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00153   return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
00154 }
00155 
00156 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
00157   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00158   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
00159                      GetSoftenedFloat(N->getOperand(1)) };
00160   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00161                                            RTLIB::ADD_F32,
00162                                            RTLIB::ADD_F64,
00163                                            RTLIB::ADD_F80,
00164                                            RTLIB::ADD_F128,
00165                                            RTLIB::ADD_PPCF128),
00166                          NVT, Ops, 2, false, SDLoc(N)).first;
00167 }
00168 
00169 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
00170   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00171   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00172   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00173                                            RTLIB::CEIL_F32,
00174                                            RTLIB::CEIL_F64,
00175                                            RTLIB::CEIL_F80,
00176                                            RTLIB::CEIL_F128,
00177                                            RTLIB::CEIL_PPCF128),
00178                          NVT, &Op, 1, false, SDLoc(N)).first;
00179 }
00180 
00181 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
00182   SDValue LHS = GetSoftenedFloat(N->getOperand(0));
00183   SDValue RHS = BitConvertToInteger(N->getOperand(1));
00184   SDLoc dl(N);
00185 
00186   EVT LVT = LHS.getValueType();
00187   EVT RVT = RHS.getValueType();
00188 
00189   unsigned LSize = LVT.getSizeInBits();
00190   unsigned RSize = RVT.getSizeInBits();
00191 
00192   // First get the sign bit of second operand.
00193   SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
00194                                   DAG.getConstant(RSize - 1,
00195                                                   TLI.getShiftAmountTy(RVT)));
00196   SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
00197 
00198   // Shift right or sign-extend it if the two operands have different types.
00199   int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
00200   if (SizeDiff > 0) {
00201     SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
00202                           DAG.getConstant(SizeDiff,
00203                                  TLI.getShiftAmountTy(SignBit.getValueType())));
00204     SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
00205   } else if (SizeDiff < 0) {
00206     SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
00207     SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
00208                           DAG.getConstant(-SizeDiff,
00209                                  TLI.getShiftAmountTy(SignBit.getValueType())));
00210   }
00211 
00212   // Clear the sign bit of the first operand.
00213   SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
00214                                DAG.getConstant(LSize - 1,
00215                                                TLI.getShiftAmountTy(LVT)));
00216   Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
00217   LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
00218 
00219   // Or the value with the sign bit.
00220   return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
00221 }
00222 
00223 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
00224   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00225   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00226   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00227                                            RTLIB::COS_F32,
00228                                            RTLIB::COS_F64,
00229                                            RTLIB::COS_F80,
00230                                            RTLIB::COS_F128,
00231                                            RTLIB::COS_PPCF128),
00232                          NVT, &Op, 1, false, SDLoc(N)).first;
00233 }
00234 
00235 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
00236   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00237   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
00238                      GetSoftenedFloat(N->getOperand(1)) };
00239   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00240                                            RTLIB::DIV_F32,
00241                                            RTLIB::DIV_F64,
00242                                            RTLIB::DIV_F80,
00243                                            RTLIB::DIV_F128,
00244                                            RTLIB::DIV_PPCF128),
00245                          NVT, Ops, 2, false, SDLoc(N)).first;
00246 }
00247 
00248 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
00249   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00250   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00251   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00252                                            RTLIB::EXP_F32,
00253                                            RTLIB::EXP_F64,
00254                                            RTLIB::EXP_F80,
00255                                            RTLIB::EXP_F128,
00256                                            RTLIB::EXP_PPCF128),
00257                          NVT, &Op, 1, false, SDLoc(N)).first;
00258 }
00259 
00260 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
00261   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00262   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00263   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00264                                            RTLIB::EXP2_F32,
00265                                            RTLIB::EXP2_F64,
00266                                            RTLIB::EXP2_F80,
00267                                            RTLIB::EXP2_F128,
00268                                            RTLIB::EXP2_PPCF128),
00269                          NVT, &Op, 1, false, SDLoc(N)).first;
00270 }
00271 
00272 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
00273   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00274   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00275   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00276                                            RTLIB::FLOOR_F32,
00277                                            RTLIB::FLOOR_F64,
00278                                            RTLIB::FLOOR_F80,
00279                                            RTLIB::FLOOR_F128,
00280                                            RTLIB::FLOOR_PPCF128),
00281                          NVT, &Op, 1, false, SDLoc(N)).first;
00282 }
00283 
00284 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
00285   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00286   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00287   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00288                                            RTLIB::LOG_F32,
00289                                            RTLIB::LOG_F64,
00290                                            RTLIB::LOG_F80,
00291                                            RTLIB::LOG_F128,
00292                                            RTLIB::LOG_PPCF128),
00293                          NVT, &Op, 1, false, SDLoc(N)).first;
00294 }
00295 
00296 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
00297   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00298   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00299   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00300                                            RTLIB::LOG2_F32,
00301                                            RTLIB::LOG2_F64,
00302                                            RTLIB::LOG2_F80,
00303                                            RTLIB::LOG2_F128,
00304                                            RTLIB::LOG2_PPCF128),
00305                          NVT, &Op, 1, false, SDLoc(N)).first;
00306 }
00307 
00308 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
00309   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00310   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00311   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00312                                            RTLIB::LOG10_F32,
00313                                            RTLIB::LOG10_F64,
00314                                            RTLIB::LOG10_F80,
00315                                            RTLIB::LOG10_F128,
00316                                            RTLIB::LOG10_PPCF128),
00317                          NVT, &Op, 1, false, SDLoc(N)).first;
00318 }
00319 
00320 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
00321   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00322   SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
00323                      GetSoftenedFloat(N->getOperand(1)),
00324                      GetSoftenedFloat(N->getOperand(2)) };
00325   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00326                                            RTLIB::FMA_F32,
00327                                            RTLIB::FMA_F64,
00328                                            RTLIB::FMA_F80,
00329                                            RTLIB::FMA_F128,
00330                                            RTLIB::FMA_PPCF128),
00331                          NVT, Ops, 3, false, SDLoc(N)).first;
00332 }
00333 
00334 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
00335   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00336   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
00337                      GetSoftenedFloat(N->getOperand(1)) };
00338   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00339                                            RTLIB::MUL_F32,
00340                                            RTLIB::MUL_F64,
00341                                            RTLIB::MUL_F80,
00342                                            RTLIB::MUL_F128,
00343                                            RTLIB::MUL_PPCF128),
00344                          NVT, Ops, 2, false, SDLoc(N)).first;
00345 }
00346 
00347 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
00348   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00349   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00350   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00351                                            RTLIB::NEARBYINT_F32,
00352                                            RTLIB::NEARBYINT_F64,
00353                                            RTLIB::NEARBYINT_F80,
00354                                            RTLIB::NEARBYINT_F128,
00355                                            RTLIB::NEARBYINT_PPCF128),
00356                          NVT, &Op, 1, false, SDLoc(N)).first;
00357 }
00358 
00359 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
00360   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00361   // Expand Y = FNEG(X) -> Y = SUB -0.0, X
00362   SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
00363                      GetSoftenedFloat(N->getOperand(0)) };
00364   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00365                                            RTLIB::SUB_F32,
00366                                            RTLIB::SUB_F64,
00367                                            RTLIB::SUB_F80,
00368                                            RTLIB::SUB_F128,
00369                                            RTLIB::SUB_PPCF128),
00370                          NVT, Ops, 2, false, SDLoc(N)).first;
00371 }
00372 
00373 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
00374   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00375   SDValue Op = N->getOperand(0);
00376 
00377   // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
00378   // entirely possible for both f16 and f32 to be legal, so use the fully
00379   // hard-float FP_EXTEND rather than FP16_TO_FP.
00380   if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
00381     Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
00382     if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
00383       SoftenFloatResult(Op.getNode(), 0);
00384   }
00385 
00386   RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
00387   if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
00388     Op = GetSoftenedFloat(Op);
00389   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
00390   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
00391 }
00392 
00393 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
00394 // nodes?
00395 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
00396   EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
00397   SDValue Op = N->getOperand(0);
00398   SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
00399                                   false, SDLoc(N)).first;
00400   if (N->getValueType(0) == MVT::f32)
00401     return Res32;
00402 
00403   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00404   RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
00405   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
00406   return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
00407 }
00408 
00409 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
00410   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00411   SDValue Op = N->getOperand(0);
00412   if (N->getValueType(0) == MVT::f16) {
00413     // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
00414     // storage-only type get a chance to select things.
00415     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
00416   }
00417 
00418   RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
00419   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
00420   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
00421 }
00422 
00423 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
00424   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00425   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
00426                      GetSoftenedFloat(N->getOperand(1)) };
00427   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00428                                            RTLIB::POW_F32,
00429                                            RTLIB::POW_F64,
00430                                            RTLIB::POW_F80,
00431                                            RTLIB::POW_F128,
00432                                            RTLIB::POW_PPCF128),
00433                          NVT, Ops, 2, false, SDLoc(N)).first;
00434 }
00435 
00436 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
00437   assert(N->getOperand(1).getValueType() == MVT::i32 &&
00438          "Unsupported power type!");
00439   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00440   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
00441   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00442                                            RTLIB::POWI_F32,
00443                                            RTLIB::POWI_F64,
00444                                            RTLIB::POWI_F80,
00445                                            RTLIB::POWI_F128,
00446                                            RTLIB::POWI_PPCF128),
00447                          NVT, Ops, 2, false, SDLoc(N)).first;
00448 }
00449 
00450 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
00451   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00452   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
00453                      GetSoftenedFloat(N->getOperand(1)) };
00454   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00455                                            RTLIB::REM_F32,
00456                                            RTLIB::REM_F64,
00457                                            RTLIB::REM_F80,
00458                                            RTLIB::REM_F128,
00459                                            RTLIB::REM_PPCF128),
00460                          NVT, Ops, 2, false, SDLoc(N)).first;
00461 }
00462 
00463 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
00464   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00465   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00466   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00467                                            RTLIB::RINT_F32,
00468                                            RTLIB::RINT_F64,
00469                                            RTLIB::RINT_F80,
00470                                            RTLIB::RINT_F128,
00471                                            RTLIB::RINT_PPCF128),
00472                          NVT, &Op, 1, false, SDLoc(N)).first;
00473 }
00474 
00475 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
00476   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00477   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00478   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00479                                            RTLIB::ROUND_F32,
00480                                            RTLIB::ROUND_F64,
00481                                            RTLIB::ROUND_F80,
00482                                            RTLIB::ROUND_F128,
00483                                            RTLIB::ROUND_PPCF128),
00484                          NVT, &Op, 1, false, SDLoc(N)).first;
00485 }
00486 
00487 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
00488   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00489   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00490   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00491                                            RTLIB::SIN_F32,
00492                                            RTLIB::SIN_F64,
00493                                            RTLIB::SIN_F80,
00494                                            RTLIB::SIN_F128,
00495                                            RTLIB::SIN_PPCF128),
00496                          NVT, &Op, 1, false, SDLoc(N)).first;
00497 }
00498 
00499 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
00500   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00501   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00502   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00503                                            RTLIB::SQRT_F32,
00504                                            RTLIB::SQRT_F64,
00505                                            RTLIB::SQRT_F80,
00506                                            RTLIB::SQRT_F128,
00507                                            RTLIB::SQRT_PPCF128),
00508                          NVT, &Op, 1, false, SDLoc(N)).first;
00509 }
00510 
00511 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
00512   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00513   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
00514                      GetSoftenedFloat(N->getOperand(1)) };
00515   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00516                                            RTLIB::SUB_F32,
00517                                            RTLIB::SUB_F64,
00518                                            RTLIB::SUB_F80,
00519                                            RTLIB::SUB_F128,
00520                                            RTLIB::SUB_PPCF128),
00521                          NVT, Ops, 2, false, SDLoc(N)).first;
00522 }
00523 
00524 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
00525   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00526   if (N->getValueType(0) == MVT::f16)
00527     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
00528 
00529   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00530   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00531                                            RTLIB::TRUNC_F32,
00532                                            RTLIB::TRUNC_F64,
00533                                            RTLIB::TRUNC_F80,
00534                                            RTLIB::TRUNC_F128,
00535                                            RTLIB::TRUNC_PPCF128),
00536                          NVT, &Op, 1, false, SDLoc(N)).first;
00537 }
00538 
00539 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
00540   LoadSDNode *L = cast<LoadSDNode>(N);
00541   EVT VT = N->getValueType(0);
00542   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
00543   SDLoc dl(N);
00544 
00545   SDValue NewL;
00546   if (L->getExtensionType() == ISD::NON_EXTLOAD) {
00547     NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
00548                        NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
00549                        L->getPointerInfo(), NVT, L->isVolatile(),
00550                        L->isNonTemporal(), false, L->getAlignment(),
00551                        L->getAAInfo());
00552     // Legalized the chain result - switch anything that used the old chain to
00553     // use the new one.
00554     ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
00555     return NewL;
00556   }
00557 
00558   // Do a non-extending load followed by FP_EXTEND.
00559   NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
00560                      L->getMemoryVT(), dl, L->getChain(),
00561                      L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
00562                      L->getMemoryVT(), L->isVolatile(),
00563                      L->isNonTemporal(), false, L->getAlignment(),
00564                      L->getAAInfo());
00565   // Legalized the chain result - switch anything that used the old chain to
00566   // use the new one.
00567   ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
00568   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
00569 }
00570 
00571 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
00572   SDValue LHS = GetSoftenedFloat(N->getOperand(1));
00573   SDValue RHS = GetSoftenedFloat(N->getOperand(2));
00574   return DAG.getSelect(SDLoc(N),
00575                        LHS.getValueType(), N->getOperand(0), LHS, RHS);
00576 }
00577 
00578 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
00579   SDValue LHS = GetSoftenedFloat(N->getOperand(2));
00580   SDValue RHS = GetSoftenedFloat(N->getOperand(3));
00581   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
00582                      LHS.getValueType(), N->getOperand(0),
00583                      N->getOperand(1), LHS, RHS, N->getOperand(4));
00584 }
00585 
00586 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
00587   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
00588                                                N->getValueType(0)));
00589 }
00590 
00591 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
00592   SDValue Chain = N->getOperand(0); // Get the chain.
00593   SDValue Ptr = N->getOperand(1); // Get the pointer.
00594   EVT VT = N->getValueType(0);
00595   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
00596   SDLoc dl(N);
00597 
00598   SDValue NewVAARG;
00599   NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
00600                           N->getConstantOperandVal(3));
00601 
00602   // Legalized the chain result - switch anything that used the old chain to
00603   // use the new one.
00604   ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
00605   return NewVAARG;
00606 }
00607 
00608 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
00609   bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
00610   EVT SVT = N->getOperand(0).getValueType();
00611   EVT RVT = N->getValueType(0);
00612   EVT NVT = EVT();
00613   SDLoc dl(N);
00614 
00615   // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
00616   // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
00617   // match.  Look for an appropriate libcall.
00618   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
00619   for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
00620        t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
00621     NVT = (MVT::SimpleValueType)t;
00622     // The source needs to big enough to hold the operand.
00623     if (NVT.bitsGE(SVT))
00624       LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
00625   }
00626   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
00627 
00628   // Sign/zero extend the argument if the libcall takes a larger type.
00629   SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
00630                            NVT, N->getOperand(0));
00631   return TLI.makeLibCall(DAG, LC,
00632                          TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
00633                          &Op, 1, false, dl).first;
00634 }
00635 
00636 
00637 //===----------------------------------------------------------------------===//
00638 //  Operand Float to Integer Conversion..
00639 //===----------------------------------------------------------------------===//
00640 
00641 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
00642   DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
00643         dbgs() << "\n");
00644   SDValue Res = SDValue();
00645 
00646   switch (N->getOpcode()) {
00647   default:
00648 #ifndef NDEBUG
00649     dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
00650     N->dump(&DAG); dbgs() << "\n";
00651 #endif
00652     llvm_unreachable("Do not know how to soften this operator's operand!");
00653 
00654   case ISD::BITCAST:     Res = SoftenFloatOp_BITCAST(N); break;
00655   case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
00656   case ISD::FP_EXTEND:   Res = SoftenFloatOp_FP_EXTEND(N); break;
00657   case ISD::FP_TO_FP16:  // Same as FP_ROUND for softening purposes
00658   case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
00659   case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
00660   case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
00661   case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
00662   case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
00663   case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
00664   }
00665 
00666   // If the result is null, the sub-method took care of registering results etc.
00667   if (!Res.getNode()) return false;
00668 
00669   // If the result is N, the sub-method updated N in place.  Tell the legalizer
00670   // core about this.
00671   if (Res.getNode() == N)
00672     return true;
00673 
00674   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
00675          "Invalid operand expansion");
00676 
00677   ReplaceValueWith(SDValue(N, 0), Res);
00678   return false;
00679 }
00680 
00681 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
00682   return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
00683                      GetSoftenedFloat(N->getOperand(0)));
00684 }
00685 
00686 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
00687   // If we get here, the result must be legal but the source illegal.
00688   EVT SVT = N->getOperand(0).getValueType();
00689   EVT RVT = N->getValueType(0);
00690   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00691 
00692   if (SVT == MVT::f16)
00693     return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
00694 
00695   RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
00696   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
00697 
00698   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
00699 }
00700 
00701 
00702 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
00703   // We actually deal with the partially-softened FP_TO_FP16 node too, which
00704   // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
00705   assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
00706 
00707   EVT SVT = N->getOperand(0).getValueType();
00708   EVT RVT = N->getValueType(0);
00709   EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
00710 
00711   RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
00712   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
00713 
00714   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00715   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
00716 }
00717 
00718 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
00719   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
00720   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
00721 
00722   EVT VT = NewLHS.getValueType();
00723   NewLHS = GetSoftenedFloat(NewLHS);
00724   NewRHS = GetSoftenedFloat(NewRHS);
00725   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
00726 
00727   // If softenSetCCOperands returned a scalar, we need to compare the result
00728   // against zero to select between true and false values.
00729   if (!NewRHS.getNode()) {
00730     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
00731     CCCode = ISD::SETNE;
00732   }
00733 
00734   // Update N to have the operands specified.
00735   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
00736                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
00737                                 N->getOperand(4)),
00738                  0);
00739 }
00740 
00741 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
00742   EVT RVT = N->getValueType(0);
00743   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
00744   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
00745   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00746   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
00747 }
00748 
00749 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
00750   EVT RVT = N->getValueType(0);
00751   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
00752   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
00753   SDValue Op = GetSoftenedFloat(N->getOperand(0));
00754   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
00755 }
00756 
00757 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
00758   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
00759   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
00760 
00761   EVT VT = NewLHS.getValueType();
00762   NewLHS = GetSoftenedFloat(NewLHS);
00763   NewRHS = GetSoftenedFloat(NewRHS);
00764   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
00765 
00766   // If softenSetCCOperands returned a scalar, we need to compare the result
00767   // against zero to select between true and false values.
00768   if (!NewRHS.getNode()) {
00769     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
00770     CCCode = ISD::SETNE;
00771   }
00772 
00773   // Update N to have the operands specified.
00774   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
00775                                 N->getOperand(2), N->getOperand(3),
00776                                 DAG.getCondCode(CCCode)),
00777                  0);
00778 }
00779 
00780 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
00781   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
00782   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
00783 
00784   EVT VT = NewLHS.getValueType();
00785   NewLHS = GetSoftenedFloat(NewLHS);
00786   NewRHS = GetSoftenedFloat(NewRHS);
00787   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
00788 
00789   // If softenSetCCOperands returned a scalar, use it.
00790   if (!NewRHS.getNode()) {
00791     assert(NewLHS.getValueType() == N->getValueType(0) &&
00792            "Unexpected setcc expansion!");
00793     return NewLHS;
00794   }
00795 
00796   // Otherwise, update N to have the operands specified.
00797   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
00798                                 DAG.getCondCode(CCCode)),
00799                  0);
00800 }
00801 
00802 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
00803   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
00804   assert(OpNo == 1 && "Can only soften the stored value!");
00805   StoreSDNode *ST = cast<StoreSDNode>(N);
00806   SDValue Val = ST->getValue();
00807   SDLoc dl(N);
00808 
00809   if (ST->isTruncatingStore())
00810     // Do an FP_ROUND followed by a non-truncating store.
00811     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
00812                                           Val, DAG.getIntPtrConstant(0)));
00813   else
00814     Val = GetSoftenedFloat(Val);
00815 
00816   return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
00817                       ST->getMemOperand());
00818 }
00819 
00820 
00821 //===----------------------------------------------------------------------===//
00822 //  Float Result Expansion
00823 //===----------------------------------------------------------------------===//
00824 
00825 /// ExpandFloatResult - This method is called when the specified result of the
00826 /// specified node is found to need expansion.  At this point, the node may also
00827 /// have invalid operands or may have other results that need promotion, we just
00828 /// know that (at least) one result needs expansion.
00829 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
00830   DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
00831   SDValue Lo, Hi;
00832   Lo = Hi = SDValue();
00833 
00834   // See if the target wants to custom expand this node.
00835   if (CustomLowerNode(N, N->getValueType(ResNo), true))
00836     return;
00837 
00838   switch (N->getOpcode()) {
00839   default:
00840 #ifndef NDEBUG
00841     dbgs() << "ExpandFloatResult #" << ResNo << ": ";
00842     N->dump(&DAG); dbgs() << "\n";
00843 #endif
00844     llvm_unreachable("Do not know how to expand the result of this operator!");
00845 
00846   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
00847   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
00848   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
00849 
00850   case ISD::MERGE_VALUES:       ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
00851   case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
00852   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
00853   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
00854   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
00855   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
00856 
00857   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
00858   case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
00859   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
00860   case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
00861   case ISD::FCOPYSIGN:  ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
00862   case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
00863   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
00864   case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
00865   case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
00866   case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
00867   case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
00868   case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
00869   case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
00870   case ISD::FMA:        ExpandFloatRes_FMA(N, Lo, Hi); break;
00871   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
00872   case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
00873   case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
00874   case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
00875   case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
00876   case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
00877   case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
00878   case ISD::FROUND:     ExpandFloatRes_FROUND(N, Lo, Hi); break;
00879   case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
00880   case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
00881   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
00882   case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
00883   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
00884   case ISD::SINT_TO_FP:
00885   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
00886   case ISD::FREM:       ExpandFloatRes_FREM(N, Lo, Hi); break;
00887   }
00888 
00889   // If Lo/Hi is null, the sub-method took care of registering results etc.
00890   if (Lo.getNode())
00891     SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
00892 }
00893 
00894 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
00895                                                  SDValue &Hi) {
00896   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
00897   assert(NVT.getSizeInBits() == integerPartWidth &&
00898          "Do not know how to expand this float constant!");
00899   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
00900   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
00901                                  APInt(integerPartWidth, C.getRawData()[1])),
00902                          NVT);
00903   Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
00904                                  APInt(integerPartWidth, C.getRawData()[0])),
00905                          NVT);
00906 }
00907 
00908 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
00909                                            SDValue &Hi) {
00910   assert(N->getValueType(0) == MVT::ppcf128 &&
00911          "Logic only correct for ppcf128!");
00912   SDLoc dl(N);
00913   SDValue Tmp;
00914   GetExpandedFloat(N->getOperand(0), Lo, Tmp);
00915   Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
00916   // Lo = Hi==fabs(Hi) ? Lo : -Lo;
00917   Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
00918                    DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
00919                    ISD::SETEQ);
00920 }
00921 
00922 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
00923                                            SDValue &Hi) {
00924   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
00925                                          RTLIB::ADD_F32, RTLIB::ADD_F64,
00926                                          RTLIB::ADD_F80, RTLIB::ADD_F128,
00927                                          RTLIB::ADD_PPCF128),
00928                             N, false);
00929   GetPairElements(Call, Lo, Hi);
00930 }
00931 
00932 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
00933                                             SDValue &Lo, SDValue &Hi) {
00934   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
00935                                          RTLIB::CEIL_F32, RTLIB::CEIL_F64,
00936                                          RTLIB::CEIL_F80, RTLIB::CEIL_F128,
00937                                          RTLIB::CEIL_PPCF128),
00938                             N, false);
00939   GetPairElements(Call, Lo, Hi);
00940 }
00941 
00942 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
00943                                                 SDValue &Lo, SDValue &Hi) {
00944   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
00945                                          RTLIB::COPYSIGN_F32,
00946                                          RTLIB::COPYSIGN_F64,
00947                                          RTLIB::COPYSIGN_F80,
00948                                          RTLIB::COPYSIGN_F128,
00949                                          RTLIB::COPYSIGN_PPCF128),
00950                             N, false);
00951   GetPairElements(Call, Lo, Hi);
00952 }
00953 
00954 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
00955                                            SDValue &Lo, SDValue &Hi) {
00956   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
00957                                          RTLIB::COS_F32, RTLIB::COS_F64,
00958                                          RTLIB::COS_F80, RTLIB::COS_F128,
00959                                          RTLIB::COS_PPCF128),
00960                             N, false);
00961   GetPairElements(Call, Lo, Hi);
00962 }
00963 
00964 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
00965                                            SDValue &Hi) {
00966   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
00967   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
00968                                                    RTLIB::DIV_F32,
00969                                                    RTLIB::DIV_F64,
00970                                                    RTLIB::DIV_F80,
00971                                                    RTLIB::DIV_F128,
00972                                                    RTLIB::DIV_PPCF128),
00973                                  N->getValueType(0), Ops, 2, false,
00974                                  SDLoc(N)).first;
00975   GetPairElements(Call, Lo, Hi);
00976 }
00977 
00978 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
00979                                            SDValue &Lo, SDValue &Hi) {
00980   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
00981                                          RTLIB::EXP_F32, RTLIB::EXP_F64,
00982                                          RTLIB::EXP_F80, RTLIB::EXP_F128,
00983                                          RTLIB::EXP_PPCF128),
00984                             N, false);
00985   GetPairElements(Call, Lo, Hi);
00986 }
00987 
00988 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
00989                                             SDValue &Lo, SDValue &Hi) {
00990   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
00991                                          RTLIB::EXP2_F32, RTLIB::EXP2_F64,
00992                                          RTLIB::EXP2_F80, RTLIB::EXP2_F128,
00993                                          RTLIB::EXP2_PPCF128),
00994                             N, false);
00995   GetPairElements(Call, Lo, Hi);
00996 }
00997 
00998 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
00999                                              SDValue &Lo, SDValue &Hi) {
01000   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01001                                          RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
01002                                          RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
01003                                          RTLIB::FLOOR_PPCF128),
01004                             N, false);
01005   GetPairElements(Call, Lo, Hi);
01006 }
01007 
01008 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
01009                                            SDValue &Lo, SDValue &Hi) {
01010   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01011                                          RTLIB::LOG_F32, RTLIB::LOG_F64,
01012                                          RTLIB::LOG_F80, RTLIB::LOG_F128,
01013                                          RTLIB::LOG_PPCF128),
01014                             N, false);
01015   GetPairElements(Call, Lo, Hi);
01016 }
01017 
01018 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
01019                                             SDValue &Lo, SDValue &Hi) {
01020   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01021                                          RTLIB::LOG2_F32, RTLIB::LOG2_F64,
01022                                          RTLIB::LOG2_F80, RTLIB::LOG2_F128,
01023                                          RTLIB::LOG2_PPCF128),
01024                             N, false);
01025   GetPairElements(Call, Lo, Hi);
01026 }
01027 
01028 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
01029                                              SDValue &Lo, SDValue &Hi) {
01030   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01031                                          RTLIB::LOG10_F32, RTLIB::LOG10_F64,
01032                                          RTLIB::LOG10_F80, RTLIB::LOG10_F128,
01033                                          RTLIB::LOG10_PPCF128),
01034                             N, false);
01035   GetPairElements(Call, Lo, Hi);
01036 }
01037 
01038 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
01039                                           SDValue &Hi) {
01040   SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
01041   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
01042                                                    RTLIB::FMA_F32,
01043                                                    RTLIB::FMA_F64,
01044                                                    RTLIB::FMA_F80,
01045                                                    RTLIB::FMA_F128,
01046                                                    RTLIB::FMA_PPCF128),
01047                                  N->getValueType(0), Ops, 3, false,
01048                                  SDLoc(N)).first;
01049   GetPairElements(Call, Lo, Hi);
01050 }
01051 
01052 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
01053                                            SDValue &Hi) {
01054   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01055   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
01056                                                    RTLIB::MUL_F32,
01057                                                    RTLIB::MUL_F64,
01058                                                    RTLIB::MUL_F80,
01059                                                    RTLIB::MUL_F128,
01060                                                    RTLIB::MUL_PPCF128),
01061                                  N->getValueType(0), Ops, 2, false,
01062                                  SDLoc(N)).first;
01063   GetPairElements(Call, Lo, Hi);
01064 }
01065 
01066 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
01067                                                  SDValue &Lo, SDValue &Hi) {
01068   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01069                                          RTLIB::NEARBYINT_F32,
01070                                          RTLIB::NEARBYINT_F64,
01071                                          RTLIB::NEARBYINT_F80,
01072                                          RTLIB::NEARBYINT_F128,
01073                                          RTLIB::NEARBYINT_PPCF128),
01074                             N, false);
01075   GetPairElements(Call, Lo, Hi);
01076 }
01077 
01078 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
01079                                            SDValue &Hi) {
01080   SDLoc dl(N);
01081   GetExpandedFloat(N->getOperand(0), Lo, Hi);
01082   Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
01083   Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
01084 }
01085 
01086 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
01087                                                 SDValue &Hi) {
01088   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
01089   Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
01090   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
01091                                  APInt(NVT.getSizeInBits(), 0)), NVT);
01092 }
01093 
01094 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
01095                                            SDValue &Lo, SDValue &Hi) {
01096   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01097                                          RTLIB::POW_F32, RTLIB::POW_F64,
01098                                          RTLIB::POW_F80, RTLIB::POW_F128,
01099                                          RTLIB::POW_PPCF128),
01100                             N, false);
01101   GetPairElements(Call, Lo, Hi);
01102 }
01103 
01104 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
01105                                             SDValue &Lo, SDValue &Hi) {
01106   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01107                                          RTLIB::POWI_F32, RTLIB::POWI_F64,
01108                                          RTLIB::POWI_F80, RTLIB::POWI_F128,
01109                                          RTLIB::POWI_PPCF128),
01110                             N, false);
01111   GetPairElements(Call, Lo, Hi);
01112 }
01113 
01114 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
01115                                            SDValue &Lo, SDValue &Hi) {
01116   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01117                                          RTLIB::REM_F32, RTLIB::REM_F64,
01118                                          RTLIB::REM_F80, RTLIB::REM_F128,
01119                                          RTLIB::REM_PPCF128),
01120                             N, false);
01121   GetPairElements(Call, Lo, Hi);
01122 }
01123 
01124 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
01125                                             SDValue &Lo, SDValue &Hi) {
01126   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01127                                          RTLIB::RINT_F32, RTLIB::RINT_F64,
01128                                          RTLIB::RINT_F80, RTLIB::RINT_F128,
01129                                          RTLIB::RINT_PPCF128),
01130                             N, false);
01131   GetPairElements(Call, Lo, Hi);
01132 }
01133 
01134 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
01135                                              SDValue &Lo, SDValue &Hi) {
01136   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01137                                          RTLIB::ROUND_F32,
01138                                          RTLIB::ROUND_F64,
01139                                          RTLIB::ROUND_F80,
01140                                          RTLIB::ROUND_F128,
01141                                          RTLIB::ROUND_PPCF128),
01142                             N, false);
01143   GetPairElements(Call, Lo, Hi);
01144 }
01145 
01146 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
01147                                            SDValue &Lo, SDValue &Hi) {
01148   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01149                                          RTLIB::SIN_F32, RTLIB::SIN_F64,
01150                                          RTLIB::SIN_F80, RTLIB::SIN_F128,
01151                                          RTLIB::SIN_PPCF128),
01152                             N, false);
01153   GetPairElements(Call, Lo, Hi);
01154 }
01155 
01156 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
01157                                             SDValue &Lo, SDValue &Hi) {
01158   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01159                                          RTLIB::SQRT_F32, RTLIB::SQRT_F64,
01160                                          RTLIB::SQRT_F80, RTLIB::SQRT_F128,
01161                                          RTLIB::SQRT_PPCF128),
01162                             N, false);
01163   GetPairElements(Call, Lo, Hi);
01164 }
01165 
01166 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
01167                                            SDValue &Hi) {
01168   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01169   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
01170                                                    RTLIB::SUB_F32,
01171                                                    RTLIB::SUB_F64,
01172                                                    RTLIB::SUB_F80,
01173                                                    RTLIB::SUB_F128,
01174                                                    RTLIB::SUB_PPCF128),
01175                                  N->getValueType(0), Ops, 2, false,
01176                                  SDLoc(N)).first;
01177   GetPairElements(Call, Lo, Hi);
01178 }
01179 
01180 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
01181                                              SDValue &Lo, SDValue &Hi) {
01182   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
01183                                          RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
01184                                          RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
01185                                          RTLIB::TRUNC_PPCF128),
01186                             N, false);
01187   GetPairElements(Call, Lo, Hi);
01188 }
01189 
01190 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
01191                                            SDValue &Hi) {
01192   if (ISD::isNormalLoad(N)) {
01193     ExpandRes_NormalLoad(N, Lo, Hi);
01194     return;
01195   }
01196 
01197   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
01198   LoadSDNode *LD = cast<LoadSDNode>(N);
01199   SDValue Chain = LD->getChain();
01200   SDValue Ptr = LD->getBasePtr();
01201   SDLoc dl(N);
01202 
01203   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
01204   assert(NVT.isByteSized() && "Expanded type not byte sized!");
01205   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
01206 
01207   Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
01208                       LD->getMemoryVT(), LD->getMemOperand());
01209 
01210   // Remember the chain.
01211   Chain = Hi.getValue(1);
01212 
01213   // The low part is zero.
01214   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
01215                                  APInt(NVT.getSizeInBits(), 0)), NVT);
01216 
01217   // Modified the chain - switch anything that used the old chain to use the
01218   // new one.
01219   ReplaceValueWith(SDValue(LD, 1), Chain);
01220 }
01221 
01222 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
01223                                                  SDValue &Hi) {
01224   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
01225   EVT VT = N->getValueType(0);
01226   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
01227   SDValue Src = N->getOperand(0);
01228   EVT SrcVT = Src.getValueType();
01229   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
01230   SDLoc dl(N);
01231 
01232   // First do an SINT_TO_FP, whether the original was signed or unsigned.
01233   // When promoting partial word types to i32 we must honor the signedness,
01234   // though.
01235   if (SrcVT.bitsLE(MVT::i32)) {
01236     // The integer can be represented exactly in an f64.
01237     Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
01238                       MVT::i32, Src);
01239     Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
01240                                    APInt(NVT.getSizeInBits(), 0)), NVT);
01241     Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
01242   } else {
01243     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01244     if (SrcVT.bitsLE(MVT::i64)) {
01245       Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
01246                         MVT::i64, Src);
01247       LC = RTLIB::SINTTOFP_I64_PPCF128;
01248     } else if (SrcVT.bitsLE(MVT::i128)) {
01249       Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
01250       LC = RTLIB::SINTTOFP_I128_PPCF128;
01251     }
01252     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
01253 
01254     Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
01255     GetPairElements(Hi, Lo, Hi);
01256   }
01257 
01258   if (isSigned)
01259     return;
01260 
01261   // Unsigned - fix up the SINT_TO_FP value just calculated.
01262   Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
01263   SrcVT = Src.getValueType();
01264 
01265   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
01266   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
01267   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
01268   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
01269   ArrayRef<uint64_t> Parts;
01270 
01271   switch (SrcVT.getSimpleVT().SimpleTy) {
01272   default:
01273     llvm_unreachable("Unsupported UINT_TO_FP!");
01274   case MVT::i32:
01275     Parts = TwoE32;
01276     break;
01277   case MVT::i64:
01278     Parts = TwoE64;
01279     break;
01280   case MVT::i128:
01281     Parts = TwoE128;
01282     break;
01283   }
01284 
01285   Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
01286                    DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
01287                                              APInt(128, Parts)),
01288                                      MVT::ppcf128));
01289   Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
01290                        Lo, Hi, ISD::SETLT);
01291   GetPairElements(Lo, Lo, Hi);
01292 }
01293 
01294 
01295 //===----------------------------------------------------------------------===//
01296 //  Float Operand Expansion
01297 //===----------------------------------------------------------------------===//
01298 
01299 /// ExpandFloatOperand - This method is called when the specified operand of the
01300 /// specified node is found to need expansion.  At this point, all of the result
01301 /// types of the node are known to be legal, but other operands of the node may
01302 /// need promotion or expansion as well as the specified one.
01303 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
01304   DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
01305   SDValue Res = SDValue();
01306 
01307   // See if the target wants to custom expand this node.
01308   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
01309     return false;
01310 
01311   switch (N->getOpcode()) {
01312   default:
01313 #ifndef NDEBUG
01314     dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
01315     N->dump(&DAG); dbgs() << "\n";
01316 #endif
01317     llvm_unreachable("Do not know how to expand this operator's operand!");
01318 
01319   case ISD::BITCAST:         Res = ExpandOp_BITCAST(N); break;
01320   case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
01321   case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
01322 
01323   case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
01324   case ISD::FCOPYSIGN:  Res = ExpandFloatOp_FCOPYSIGN(N); break;
01325   case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
01326   case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
01327   case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
01328   case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
01329   case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
01330   case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
01331                                                   OpNo); break;
01332   }
01333 
01334   // If the result is null, the sub-method took care of registering results etc.
01335   if (!Res.getNode()) return false;
01336 
01337   // If the result is N, the sub-method updated N in place.  Tell the legalizer
01338   // core about this.
01339   if (Res.getNode() == N)
01340     return true;
01341 
01342   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
01343          "Invalid operand expansion");
01344 
01345   ReplaceValueWith(SDValue(N, 0), Res);
01346   return false;
01347 }
01348 
01349 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
01350 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
01351 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
01352                                                 SDValue &NewRHS,
01353                                                 ISD::CondCode &CCCode,
01354                                                 SDLoc dl) {
01355   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
01356   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
01357   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
01358 
01359   assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
01360 
01361   // FIXME:  This generated code sucks.  We want to generate
01362   //         FCMPU crN, hi1, hi2
01363   //         BNE crN, L:
01364   //         FCMPU crN, lo1, lo2
01365   // The following can be improved, but not that much.
01366   SDValue Tmp1, Tmp2, Tmp3;
01367   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
01368                       LHSHi, RHSHi, ISD::SETOEQ);
01369   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
01370                       LHSLo, RHSLo, CCCode);
01371   Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
01372   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
01373                       LHSHi, RHSHi, ISD::SETUNE);
01374   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
01375                       LHSHi, RHSHi, CCCode);
01376   Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
01377   NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
01378   NewRHS = SDValue();   // LHS is the result, not a compare.
01379 }
01380 
01381 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
01382   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
01383   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
01384   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
01385 
01386   // If ExpandSetCCOperands returned a scalar, we need to compare the result
01387   // against zero to select between true and false values.
01388   if (!NewRHS.getNode()) {
01389     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
01390     CCCode = ISD::SETNE;
01391   }
01392 
01393   // Update N to have the operands specified.
01394   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
01395                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
01396                                 N->getOperand(4)), 0);
01397 }
01398 
01399 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
01400   assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
01401          "Logic only correct for ppcf128!");
01402   SDValue Lo, Hi;
01403   GetExpandedFloat(N->getOperand(1), Lo, Hi);
01404   // The ppcf128 value is providing only the sign; take it from the
01405   // higher-order double (which must have the larger magnitude).
01406   return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
01407                      N->getValueType(0), N->getOperand(0), Hi);
01408 }
01409 
01410 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
01411   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
01412          "Logic only correct for ppcf128!");
01413   SDValue Lo, Hi;
01414   GetExpandedFloat(N->getOperand(0), Lo, Hi);
01415   // Round it the rest of the way (e.g. to f32) if needed.
01416   return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
01417                      N->getValueType(0), Hi, N->getOperand(1));
01418 }
01419 
01420 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
01421   EVT RVT = N->getValueType(0);
01422   SDLoc dl(N);
01423 
01424   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
01425   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
01426   if (RVT == MVT::i32) {
01427     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
01428            "Logic only correct for ppcf128!");
01429     SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
01430                               N->getOperand(0), DAG.getValueType(MVT::f64));
01431     Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
01432                       DAG.getIntPtrConstant(1));
01433     return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
01434   }
01435 
01436   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
01437   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
01438   return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
01439 }
01440 
01441 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
01442   EVT RVT = N->getValueType(0);
01443   SDLoc dl(N);
01444 
01445   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
01446   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
01447   if (RVT == MVT::i32) {
01448     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
01449            "Logic only correct for ppcf128!");
01450     const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
01451     APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
01452     SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
01453     //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
01454     // FIXME: generated code sucks.
01455     return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
01456                            DAG.getNode(ISD::ADD, dl, MVT::i32,
01457                                        DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
01458                                                    DAG.getNode(ISD::FSUB, dl,
01459                                                                MVT::ppcf128,
01460                                                                N->getOperand(0),
01461                                                                Tmp)),
01462                                        DAG.getConstant(0x80000000, MVT::i32)),
01463                            DAG.getNode(ISD::FP_TO_SINT, dl,
01464                                        MVT::i32, N->getOperand(0)),
01465                            ISD::SETGE);
01466   }
01467 
01468   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
01469   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
01470   return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
01471                          false, dl).first;
01472 }
01473 
01474 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
01475   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
01476   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
01477   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
01478 
01479   // If ExpandSetCCOperands returned a scalar, we need to compare the result
01480   // against zero to select between true and false values.
01481   if (!NewRHS.getNode()) {
01482     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
01483     CCCode = ISD::SETNE;
01484   }
01485 
01486   // Update N to have the operands specified.
01487   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
01488                                 N->getOperand(2), N->getOperand(3),
01489                                 DAG.getCondCode(CCCode)), 0);
01490 }
01491 
01492 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
01493   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
01494   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
01495   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
01496 
01497   // If ExpandSetCCOperands returned a scalar, use it.
01498   if (!NewRHS.getNode()) {
01499     assert(NewLHS.getValueType() == N->getValueType(0) &&
01500            "Unexpected setcc expansion!");
01501     return NewLHS;
01502   }
01503 
01504   // Otherwise, update N to have the operands specified.
01505   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
01506                                 DAG.getCondCode(CCCode)), 0);
01507 }
01508 
01509 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
01510   if (ISD::isNormalStore(N))
01511     return ExpandOp_NormalStore(N, OpNo);
01512 
01513   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
01514   assert(OpNo == 1 && "Can only expand the stored value so far");
01515   StoreSDNode *ST = cast<StoreSDNode>(N);
01516 
01517   SDValue Chain = ST->getChain();
01518   SDValue Ptr = ST->getBasePtr();
01519 
01520   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
01521                                      ST->getValue().getValueType());
01522   assert(NVT.isByteSized() && "Expanded type not byte sized!");
01523   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
01524   (void)NVT;
01525 
01526   SDValue Lo, Hi;
01527   GetExpandedOp(ST->getValue(), Lo, Hi);
01528 
01529   return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
01530                            ST->getMemoryVT(), ST->getMemOperand());
01531 }