Home | History | Annotate | Download | only in SelectionDAG
      1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements float type expansion and softening for LegalizeTypes.
     11 // Softening is the act of turning a computation in an illegal floating point
     12 // type into a computation in an integer type of the same size; also known as
     13 // "soft float".  For example, turning f32 arithmetic into operations using i32.
     14 // The resulting integer value is the same as what you would get by performing
     15 // the floating point operation and bitcasting the result to the integer type.
     16 // Expansion is the act of changing a computation in an illegal type to be a
     17 // computation in two identical registers of a smaller type.  For example,
     18 // implementing ppcf128 arithmetic in two f64 registers.
     19 //
     20 //===----------------------------------------------------------------------===//
     21 
     22 #include "LegalizeTypes.h"
     23 #include "llvm/Support/ErrorHandling.h"
     24 #include "llvm/Support/raw_ostream.h"
     25 using namespace llvm;
     26 
     27 /// GetFPLibCall - Return the right libcall for the given floating point type.
     28 static RTLIB::Libcall GetFPLibCall(EVT VT,
     29                                    RTLIB::Libcall Call_F32,
     30                                    RTLIB::Libcall Call_F64,
     31                                    RTLIB::Libcall Call_F80,
     32                                    RTLIB::Libcall Call_PPCF128) {
     33   return
     34     VT == MVT::f32 ? Call_F32 :
     35     VT == MVT::f64 ? Call_F64 :
     36     VT == MVT::f80 ? Call_F80 :
     37     VT == MVT::ppcf128 ? Call_PPCF128 :
     38     RTLIB::UNKNOWN_LIBCALL;
     39 }
     40 
     41 //===----------------------------------------------------------------------===//
     42 //  Result Float to Integer Conversion.
     43 //===----------------------------------------------------------------------===//
     44 
     45 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
     46   DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
     47         dbgs() << "\n");
     48   SDValue R = SDValue();
     49 
     50   switch (N->getOpcode()) {
     51   default:
     52 #ifndef NDEBUG
     53     dbgs() << "SoftenFloatResult #" << ResNo << ": ";
     54     N->dump(&DAG); dbgs() << "\n";
     55 #endif
     56     llvm_unreachable("Do not know how to soften the result of this operator!");
     57 
     58     case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
     59     case ISD::BITCAST:     R = SoftenFloatRes_BITCAST(N); break;
     60     case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
     61     case ISD::ConstantFP:
     62       R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
     63       break;
     64     case ISD::EXTRACT_VECTOR_ELT:
     65       R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
     66     case ISD::FABS:        R = SoftenFloatRes_FABS(N); break;
     67     case ISD::FADD:        R = SoftenFloatRes_FADD(N); break;
     68     case ISD::FCEIL:       R = SoftenFloatRes_FCEIL(N); break;
     69     case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
     70     case ISD::FCOS:        R = SoftenFloatRes_FCOS(N); break;
     71     case ISD::FDIV:        R = SoftenFloatRes_FDIV(N); break;
     72     case ISD::FEXP:        R = SoftenFloatRes_FEXP(N); break;
     73     case ISD::FEXP2:       R = SoftenFloatRes_FEXP2(N); break;
     74     case ISD::FFLOOR:      R = SoftenFloatRes_FFLOOR(N); break;
     75     case ISD::FLOG:        R = SoftenFloatRes_FLOG(N); break;
     76     case ISD::FLOG2:       R = SoftenFloatRes_FLOG2(N); break;
     77     case ISD::FLOG10:      R = SoftenFloatRes_FLOG10(N); break;
     78     case ISD::FMA:         R = SoftenFloatRes_FMA(N); break;
     79     case ISD::FMUL:        R = SoftenFloatRes_FMUL(N); break;
     80     case ISD::FNEARBYINT:  R = SoftenFloatRes_FNEARBYINT(N); break;
     81     case ISD::FNEG:        R = SoftenFloatRes_FNEG(N); break;
     82     case ISD::FP_EXTEND:   R = SoftenFloatRes_FP_EXTEND(N); break;
     83     case ISD::FP_ROUND:    R = SoftenFloatRes_FP_ROUND(N); break;
     84     case ISD::FP16_TO_FP32:R = SoftenFloatRes_FP16_TO_FP32(N); break;
     85     case ISD::FPOW:        R = SoftenFloatRes_FPOW(N); break;
     86     case ISD::FPOWI:       R = SoftenFloatRes_FPOWI(N); break;
     87     case ISD::FREM:        R = SoftenFloatRes_FREM(N); break;
     88     case ISD::FRINT:       R = SoftenFloatRes_FRINT(N); break;
     89     case ISD::FSIN:        R = SoftenFloatRes_FSIN(N); break;
     90     case ISD::FSQRT:       R = SoftenFloatRes_FSQRT(N); break;
     91     case ISD::FSUB:        R = SoftenFloatRes_FSUB(N); break;
     92     case ISD::FTRUNC:      R = SoftenFloatRes_FTRUNC(N); break;
     93     case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
     94     case ISD::SELECT:      R = SoftenFloatRes_SELECT(N); break;
     95     case ISD::SELECT_CC:   R = SoftenFloatRes_SELECT_CC(N); break;
     96     case ISD::SINT_TO_FP:
     97     case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
     98     case ISD::UNDEF:       R = SoftenFloatRes_UNDEF(N); break;
     99     case ISD::VAARG:       R = SoftenFloatRes_VAARG(N); break;
    100   }
    101 
    102   // If R is null, the sub-method took care of registering the result.
    103   if (R.getNode())
    104     SetSoftenedFloat(SDValue(N, ResNo), R);
    105 }
    106 
    107 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
    108   return BitConvertToInteger(N->getOperand(0));
    109 }
    110 
    111 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
    112                                                       unsigned ResNo) {
    113   SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
    114   return BitConvertToInteger(Op);
    115 }
    116 
    117 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
    118   // Convert the inputs to integers, and build a new pair out of them.
    119   return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
    120                      TLI.getTypeToTransformTo(*DAG.getContext(),
    121                                               N->getValueType(0)),
    122                      BitConvertToInteger(N->getOperand(0)),
    123                      BitConvertToInteger(N->getOperand(1)));
    124 }
    125 
    126 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
    127   return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
    128                          TLI.getTypeToTransformTo(*DAG.getContext(),
    129                                                   N->getValueType(0)));
    130 }
    131 
    132 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
    133   SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
    134   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(),
    135                      NewOp.getValueType().getVectorElementType(),
    136                      NewOp, N->getOperand(1));
    137 }
    138 
    139 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
    140   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    141   unsigned Size = NVT.getSizeInBits();
    142 
    143   // Mask = ~(1 << (Size-1))
    144   APInt API = APInt::getAllOnesValue(Size);
    145   API.clearBit(Size-1);
    146   SDValue Mask = DAG.getConstant(API, NVT);
    147   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    148   return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
    149 }
    150 
    151 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
    152   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    153   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
    154                      GetSoftenedFloat(N->getOperand(1)) };
    155   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    156                                   RTLIB::ADD_F32,
    157                                   RTLIB::ADD_F64,
    158                                   RTLIB::ADD_F80,
    159                                   RTLIB::ADD_PPCF128),
    160                      NVT, Ops, 2, false, N->getDebugLoc());
    161 }
    162 
    163 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
    164   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    165   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    166   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    167                                   RTLIB::CEIL_F32,
    168                                   RTLIB::CEIL_F64,
    169                                   RTLIB::CEIL_F80,
    170                                   RTLIB::CEIL_PPCF128),
    171                      NVT, &Op, 1, false, N->getDebugLoc());
    172 }
    173 
    174 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
    175   SDValue LHS = GetSoftenedFloat(N->getOperand(0));
    176   SDValue RHS = BitConvertToInteger(N->getOperand(1));
    177   DebugLoc dl = N->getDebugLoc();
    178 
    179   EVT LVT = LHS.getValueType();
    180   EVT RVT = RHS.getValueType();
    181 
    182   unsigned LSize = LVT.getSizeInBits();
    183   unsigned RSize = RVT.getSizeInBits();
    184 
    185   // First get the sign bit of second operand.
    186   SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
    187                                   DAG.getConstant(RSize - 1,
    188                                                   TLI.getShiftAmountTy(RVT)));
    189   SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
    190 
    191   // Shift right or sign-extend it if the two operands have different types.
    192   int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
    193   if (SizeDiff > 0) {
    194     SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
    195                           DAG.getConstant(SizeDiff,
    196                                  TLI.getShiftAmountTy(SignBit.getValueType())));
    197     SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
    198   } else if (SizeDiff < 0) {
    199     SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
    200     SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
    201                           DAG.getConstant(-SizeDiff,
    202                                  TLI.getShiftAmountTy(SignBit.getValueType())));
    203   }
    204 
    205   // Clear the sign bit of the first operand.
    206   SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
    207                                DAG.getConstant(LSize - 1,
    208                                                TLI.getShiftAmountTy(LVT)));
    209   Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
    210   LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
    211 
    212   // Or the value with the sign bit.
    213   return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
    214 }
    215 
    216 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
    217   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    218   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    219   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    220                                   RTLIB::COS_F32,
    221                                   RTLIB::COS_F64,
    222                                   RTLIB::COS_F80,
    223                                   RTLIB::COS_PPCF128),
    224                      NVT, &Op, 1, false, N->getDebugLoc());
    225 }
    226 
    227 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
    228   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    229   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
    230                      GetSoftenedFloat(N->getOperand(1)) };
    231   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    232                                   RTLIB::DIV_F32,
    233                                   RTLIB::DIV_F64,
    234                                   RTLIB::DIV_F80,
    235                                   RTLIB::DIV_PPCF128),
    236                      NVT, Ops, 2, false, N->getDebugLoc());
    237 }
    238 
    239 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
    240   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    241   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    242   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    243                                   RTLIB::EXP_F32,
    244                                   RTLIB::EXP_F64,
    245                                   RTLIB::EXP_F80,
    246                                   RTLIB::EXP_PPCF128),
    247                      NVT, &Op, 1, false, N->getDebugLoc());
    248 }
    249 
    250 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
    251   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    252   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    253   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    254                                   RTLIB::EXP2_F32,
    255                                   RTLIB::EXP2_F64,
    256                                   RTLIB::EXP2_F80,
    257                                   RTLIB::EXP2_PPCF128),
    258                      NVT, &Op, 1, false, N->getDebugLoc());
    259 }
    260 
    261 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
    262   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    263   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    264   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    265                                   RTLIB::FLOOR_F32,
    266                                   RTLIB::FLOOR_F64,
    267                                   RTLIB::FLOOR_F80,
    268                                   RTLIB::FLOOR_PPCF128),
    269                      NVT, &Op, 1, false, N->getDebugLoc());
    270 }
    271 
    272 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
    273   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    274   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    275   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    276                                   RTLIB::LOG_F32,
    277                                   RTLIB::LOG_F64,
    278                                   RTLIB::LOG_F80,
    279                                   RTLIB::LOG_PPCF128),
    280                      NVT, &Op, 1, false, N->getDebugLoc());
    281 }
    282 
    283 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
    284   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    285   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    286   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    287                                   RTLIB::LOG2_F32,
    288                                   RTLIB::LOG2_F64,
    289                                   RTLIB::LOG2_F80,
    290                                   RTLIB::LOG2_PPCF128),
    291                      NVT, &Op, 1, false, N->getDebugLoc());
    292 }
    293 
    294 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
    295   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    296   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    297   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    298                                   RTLIB::LOG10_F32,
    299                                   RTLIB::LOG10_F64,
    300                                   RTLIB::LOG10_F80,
    301                                   RTLIB::LOG10_PPCF128),
    302                      NVT, &Op, 1, false, N->getDebugLoc());
    303 }
    304 
    305 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
    306   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    307   SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
    308                      GetSoftenedFloat(N->getOperand(1)),
    309                      GetSoftenedFloat(N->getOperand(2)) };
    310   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    311                                   RTLIB::FMA_F32,
    312                                   RTLIB::FMA_F64,
    313                                   RTLIB::FMA_F80,
    314                                   RTLIB::FMA_PPCF128),
    315                      NVT, Ops, 3, false, N->getDebugLoc());
    316 }
    317 
    318 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
    319   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    320   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
    321                      GetSoftenedFloat(N->getOperand(1)) };
    322   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    323                                   RTLIB::MUL_F32,
    324                                   RTLIB::MUL_F64,
    325                                   RTLIB::MUL_F80,
    326                                   RTLIB::MUL_PPCF128),
    327                      NVT, Ops, 2, false, N->getDebugLoc());
    328 }
    329 
    330 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
    331   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    332   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    333   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    334                                   RTLIB::NEARBYINT_F32,
    335                                   RTLIB::NEARBYINT_F64,
    336                                   RTLIB::NEARBYINT_F80,
    337                                   RTLIB::NEARBYINT_PPCF128),
    338                      NVT, &Op, 1, false, N->getDebugLoc());
    339 }
    340 
    341 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
    342   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    343   // Expand Y = FNEG(X) -> Y = SUB -0.0, X
    344   SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
    345                      GetSoftenedFloat(N->getOperand(0)) };
    346   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    347                                   RTLIB::SUB_F32,
    348                                   RTLIB::SUB_F64,
    349                                   RTLIB::SUB_F80,
    350                                   RTLIB::SUB_PPCF128),
    351                      NVT, Ops, 2, false, N->getDebugLoc());
    352 }
    353 
    354 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
    355   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    356   SDValue Op = N->getOperand(0);
    357   RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
    358   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
    359   return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
    360 }
    361 
    362 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
    363 // nodes?
    364 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
    365   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    366   SDValue Op = N->getOperand(0);
    367   return MakeLibCall(RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
    368                      N->getDebugLoc());
    369 }
    370 
    371 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
    372   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    373   SDValue Op = N->getOperand(0);
    374   RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
    375   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
    376   return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
    377 }
    378 
    379 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
    380   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    381   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
    382                      GetSoftenedFloat(N->getOperand(1)) };
    383   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    384                                   RTLIB::POW_F32,
    385                                   RTLIB::POW_F64,
    386                                   RTLIB::POW_F80,
    387                                   RTLIB::POW_PPCF128),
    388                      NVT, Ops, 2, false, N->getDebugLoc());
    389 }
    390 
    391 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
    392   assert(N->getOperand(1).getValueType() == MVT::i32 &&
    393          "Unsupported power type!");
    394   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    395   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
    396   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    397                                   RTLIB::POWI_F32,
    398                                   RTLIB::POWI_F64,
    399                                   RTLIB::POWI_F80,
    400                                   RTLIB::POWI_PPCF128),
    401                      NVT, Ops, 2, false, N->getDebugLoc());
    402 }
    403 
    404 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
    405   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    406   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
    407                      GetSoftenedFloat(N->getOperand(1)) };
    408   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    409                                   RTLIB::REM_F32,
    410                                   RTLIB::REM_F64,
    411                                   RTLIB::REM_F80,
    412                                   RTLIB::REM_PPCF128),
    413                      NVT, Ops, 2, false, N->getDebugLoc());
    414 }
    415 
    416 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
    417   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    418   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    419   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    420                                   RTLIB::RINT_F32,
    421                                   RTLIB::RINT_F64,
    422                                   RTLIB::RINT_F80,
    423                                   RTLIB::RINT_PPCF128),
    424                      NVT, &Op, 1, false, N->getDebugLoc());
    425 }
    426 
    427 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
    428   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    429   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    430   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    431                                   RTLIB::SIN_F32,
    432                                   RTLIB::SIN_F64,
    433                                   RTLIB::SIN_F80,
    434                                   RTLIB::SIN_PPCF128),
    435                      NVT, &Op, 1, false, N->getDebugLoc());
    436 }
    437 
    438 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
    439   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    440   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    441   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    442                                   RTLIB::SQRT_F32,
    443                                   RTLIB::SQRT_F64,
    444                                   RTLIB::SQRT_F80,
    445                                   RTLIB::SQRT_PPCF128),
    446                      NVT, &Op, 1, false, N->getDebugLoc());
    447 }
    448 
    449 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
    450   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    451   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
    452                      GetSoftenedFloat(N->getOperand(1)) };
    453   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    454                                   RTLIB::SUB_F32,
    455                                   RTLIB::SUB_F64,
    456                                   RTLIB::SUB_F80,
    457                                   RTLIB::SUB_PPCF128),
    458                      NVT, Ops, 2, false, N->getDebugLoc());
    459 }
    460 
    461 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
    462   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    463   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    464   return MakeLibCall(GetFPLibCall(N->getValueType(0),
    465                                   RTLIB::TRUNC_F32,
    466                                   RTLIB::TRUNC_F64,
    467                                   RTLIB::TRUNC_F80,
    468                                   RTLIB::TRUNC_PPCF128),
    469                      NVT, &Op, 1, false, N->getDebugLoc());
    470 }
    471 
    472 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
    473   LoadSDNode *L = cast<LoadSDNode>(N);
    474   EVT VT = N->getValueType(0);
    475   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
    476   DebugLoc dl = N->getDebugLoc();
    477 
    478   SDValue NewL;
    479   if (L->getExtensionType() == ISD::NON_EXTLOAD) {
    480     NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
    481                        NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
    482                        L->getPointerInfo(), NVT,
    483                        L->isVolatile(), L->isNonTemporal(), L->getAlignment());
    484     // Legalized the chain result - switch anything that used the old chain to
    485     // use the new one.
    486     ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
    487     return NewL;
    488   }
    489 
    490   // Do a non-extending load followed by FP_EXTEND.
    491   NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
    492                      L->getMemoryVT(), dl, L->getChain(),
    493                      L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
    494                      L->getMemoryVT(), L->isVolatile(),
    495                      L->isNonTemporal(), L->getAlignment());
    496   // Legalized the chain result - switch anything that used the old chain to
    497   // use the new one.
    498   ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
    499   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
    500 }
    501 
    502 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
    503   SDValue LHS = GetSoftenedFloat(N->getOperand(1));
    504   SDValue RHS = GetSoftenedFloat(N->getOperand(2));
    505   return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
    506                      LHS.getValueType(), N->getOperand(0),LHS,RHS);
    507 }
    508 
    509 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
    510   SDValue LHS = GetSoftenedFloat(N->getOperand(2));
    511   SDValue RHS = GetSoftenedFloat(N->getOperand(3));
    512   return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
    513                      LHS.getValueType(), N->getOperand(0),
    514                      N->getOperand(1), LHS, RHS, N->getOperand(4));
    515 }
    516 
    517 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
    518   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
    519                                                N->getValueType(0)));
    520 }
    521 
    522 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
    523   SDValue Chain = N->getOperand(0); // Get the chain.
    524   SDValue Ptr = N->getOperand(1); // Get the pointer.
    525   EVT VT = N->getValueType(0);
    526   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
    527   DebugLoc dl = N->getDebugLoc();
    528 
    529   SDValue NewVAARG;
    530   NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
    531                           N->getConstantOperandVal(3));
    532 
    533   // Legalized the chain result - switch anything that used the old chain to
    534   // use the new one.
    535   ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
    536   return NewVAARG;
    537 }
    538 
    539 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
    540   bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
    541   EVT SVT = N->getOperand(0).getValueType();
    542   EVT RVT = N->getValueType(0);
    543   EVT NVT = EVT();
    544   DebugLoc dl = N->getDebugLoc();
    545 
    546   // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
    547   // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
    548   // match.  Look for an appropriate libcall.
    549   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
    550   for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
    551        t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
    552     NVT = (MVT::SimpleValueType)t;
    553     // The source needs to big enough to hold the operand.
    554     if (NVT.bitsGE(SVT))
    555       LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
    556   }
    557   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
    558 
    559   // Sign/zero extend the argument if the libcall takes a larger type.
    560   SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
    561                            NVT, N->getOperand(0));
    562   return MakeLibCall(LC, TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
    563                      &Op, 1, false, dl);
    564 }
    565 
    566 
    567 //===----------------------------------------------------------------------===//
    568 //  Operand Float to Integer Conversion..
    569 //===----------------------------------------------------------------------===//
    570 
    571 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
    572   DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
    573         dbgs() << "\n");
    574   SDValue Res = SDValue();
    575 
    576   switch (N->getOpcode()) {
    577   default:
    578 #ifndef NDEBUG
    579     dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
    580     N->dump(&DAG); dbgs() << "\n";
    581 #endif
    582     llvm_unreachable("Do not know how to soften this operator's operand!");
    583 
    584   case ISD::BITCAST:     Res = SoftenFloatOp_BITCAST(N); break;
    585   case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
    586   case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
    587   case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
    588   case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
    589   case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
    590   case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
    591   case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
    592   case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
    593   }
    594 
    595   // If the result is null, the sub-method took care of registering results etc.
    596   if (!Res.getNode()) return false;
    597 
    598   // If the result is N, the sub-method updated N in place.  Tell the legalizer
    599   // core about this.
    600   if (Res.getNode() == N)
    601     return true;
    602 
    603   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
    604          "Invalid operand expansion");
    605 
    606   ReplaceValueWith(SDValue(N, 0), Res);
    607   return false;
    608 }
    609 
    610 /// SoftenSetCCOperands - Soften the operands of a comparison.  This code is
    611 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
    612 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
    613                                            ISD::CondCode &CCCode, DebugLoc dl) {
    614   SDValue LHSInt = GetSoftenedFloat(NewLHS);
    615   SDValue RHSInt = GetSoftenedFloat(NewRHS);
    616   EVT VT = NewLHS.getValueType();
    617 
    618   assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
    619 
    620   // Expand into one or more soft-fp libcall(s).
    621   RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
    622   switch (CCCode) {
    623   case ISD::SETEQ:
    624   case ISD::SETOEQ:
    625     LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
    626     break;
    627   case ISD::SETNE:
    628   case ISD::SETUNE:
    629     LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
    630     break;
    631   case ISD::SETGE:
    632   case ISD::SETOGE:
    633     LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
    634     break;
    635   case ISD::SETLT:
    636   case ISD::SETOLT:
    637     LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
    638     break;
    639   case ISD::SETLE:
    640   case ISD::SETOLE:
    641     LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
    642     break;
    643   case ISD::SETGT:
    644   case ISD::SETOGT:
    645     LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
    646     break;
    647   case ISD::SETUO:
    648     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
    649     break;
    650   case ISD::SETO:
    651     LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
    652     break;
    653   default:
    654     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
    655     switch (CCCode) {
    656     case ISD::SETONE:
    657       // SETONE = SETOLT | SETOGT
    658       LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
    659       // Fallthrough
    660     case ISD::SETUGT:
    661       LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
    662       break;
    663     case ISD::SETUGE:
    664       LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
    665       break;
    666     case ISD::SETULT:
    667       LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
    668       break;
    669     case ISD::SETULE:
    670       LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
    671       break;
    672     case ISD::SETUEQ:
    673       LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
    674       break;
    675     default: assert(false && "Do not know how to soften this setcc!");
    676     }
    677   }
    678 
    679   // Use the target specific return value for comparions lib calls.
    680   EVT RetVT = TLI.getCmpLibcallReturnType();
    681   SDValue Ops[2] = { LHSInt, RHSInt };
    682   NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
    683   NewRHS = DAG.getConstant(0, RetVT);
    684   CCCode = TLI.getCmpLibcallCC(LC1);
    685   if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
    686     SDValue Tmp = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT),
    687                                 NewLHS, NewRHS, DAG.getCondCode(CCCode));
    688     NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
    689     NewLHS = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT), NewLHS,
    690                          NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
    691     NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
    692     NewRHS = SDValue();
    693   }
    694 }
    695 
    696 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
    697   return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
    698                      GetSoftenedFloat(N->getOperand(0)));
    699 }
    700 
    701 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
    702   EVT SVT = N->getOperand(0).getValueType();
    703   EVT RVT = N->getValueType(0);
    704 
    705   RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
    706   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
    707 
    708   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    709   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
    710 }
    711 
    712 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
    713   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
    714   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
    715   SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
    716 
    717   // If SoftenSetCCOperands returned a scalar, we need to compare the result
    718   // against zero to select between true and false values.
    719   if (NewRHS.getNode() == 0) {
    720     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
    721     CCCode = ISD::SETNE;
    722   }
    723 
    724   // Update N to have the operands specified.
    725   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
    726                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
    727                                 N->getOperand(4)),
    728                  0);
    729 }
    730 
    731 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
    732   EVT RVT = N->getValueType(0);
    733   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
    734   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
    735   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    736   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
    737 }
    738 
    739 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
    740   EVT RVT = N->getValueType(0);
    741   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
    742   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
    743   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    744   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
    745 }
    746 
    747 SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
    748   EVT RVT = N->getValueType(0);
    749   RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
    750   SDValue Op = GetSoftenedFloat(N->getOperand(0));
    751   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
    752 }
    753 
    754 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
    755   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
    756   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
    757   SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
    758 
    759   // If SoftenSetCCOperands returned a scalar, we need to compare the result
    760   // against zero to select between true and false values.
    761   if (NewRHS.getNode() == 0) {
    762     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
    763     CCCode = ISD::SETNE;
    764   }
    765 
    766   // Update N to have the operands specified.
    767   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
    768                                 N->getOperand(2), N->getOperand(3),
    769                                 DAG.getCondCode(CCCode)),
    770                  0);
    771 }
    772 
    773 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
    774   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
    775   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
    776   SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
    777 
    778   // If SoftenSetCCOperands returned a scalar, use it.
    779   if (NewRHS.getNode() == 0) {
    780     assert(NewLHS.getValueType() == N->getValueType(0) &&
    781            "Unexpected setcc expansion!");
    782     return NewLHS;
    783   }
    784 
    785   // Otherwise, update N to have the operands specified.
    786   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
    787                                 DAG.getCondCode(CCCode)),
    788                  0);
    789 }
    790 
    791 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
    792   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
    793   assert(OpNo == 1 && "Can only soften the stored value!");
    794   StoreSDNode *ST = cast<StoreSDNode>(N);
    795   SDValue Val = ST->getValue();
    796   DebugLoc dl = N->getDebugLoc();
    797 
    798   if (ST->isTruncatingStore())
    799     // Do an FP_ROUND followed by a non-truncating store.
    800     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
    801                                           Val, DAG.getIntPtrConstant(0)));
    802   else
    803     Val = GetSoftenedFloat(Val);
    804 
    805   return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
    806                       ST->getPointerInfo(),
    807                       ST->isVolatile(), ST->isNonTemporal(),
    808                       ST->getAlignment());
    809 }
    810 
    811 
    812 //===----------------------------------------------------------------------===//
    813 //  Float Result Expansion
    814 //===----------------------------------------------------------------------===//
    815 
    816 /// ExpandFloatResult - This method is called when the specified result of the
    817 /// specified node is found to need expansion.  At this point, the node may also
    818 /// have invalid operands or may have other results that need promotion, we just
    819 /// know that (at least) one result needs expansion.
    820 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
    821   DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
    822   SDValue Lo, Hi;
    823   Lo = Hi = SDValue();
    824 
    825   // See if the target wants to custom expand this node.
    826   if (CustomLowerNode(N, N->getValueType(ResNo), true))
    827     return;
    828 
    829   switch (N->getOpcode()) {
    830   default:
    831 #ifndef NDEBUG
    832     dbgs() << "ExpandFloatResult #" << ResNo << ": ";
    833     N->dump(&DAG); dbgs() << "\n";
    834 #endif
    835     llvm_unreachable("Do not know how to expand the result of this operator!");
    836 
    837   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
    838   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
    839   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
    840 
    841   case ISD::MERGE_VALUES:       ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
    842   case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
    843   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
    844   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
    845   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
    846   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
    847 
    848   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
    849   case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
    850   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
    851   case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
    852   case ISD::FCOPYSIGN:  ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
    853   case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
    854   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
    855   case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
    856   case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
    857   case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
    858   case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
    859   case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
    860   case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
    861   case ISD::FMA:        ExpandFloatRes_FMA(N, Lo, Hi); break;
    862   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
    863   case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
    864   case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
    865   case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
    866   case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
    867   case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
    868   case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
    869   case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
    870   case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
    871   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
    872   case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
    873   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
    874   case ISD::SINT_TO_FP:
    875   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
    876   }
    877 
    878   // If Lo/Hi is null, the sub-method took care of registering results etc.
    879   if (Lo.getNode())
    880     SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
    881 }
    882 
    883 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
    884                                                  SDValue &Hi) {
    885   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    886   assert(NVT.getSizeInBits() == integerPartWidth &&
    887          "Do not know how to expand this float constant!");
    888   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
    889   Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, C.getRawData()[1])),
    890                          NVT);
    891   Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, C.getRawData()[0])),
    892                          NVT);
    893 }
    894 
    895 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
    896                                            SDValue &Hi) {
    897   assert(N->getValueType(0) == MVT::ppcf128 &&
    898          "Logic only correct for ppcf128!");
    899   DebugLoc dl = N->getDebugLoc();
    900   SDValue Tmp;
    901   GetExpandedFloat(N->getOperand(0), Lo, Tmp);
    902   Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
    903   // Lo = Hi==fabs(Hi) ? Lo : -Lo;
    904   Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
    905                    DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
    906                    DAG.getCondCode(ISD::SETEQ));
    907 }
    908 
    909 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
    910                                            SDValue &Hi) {
    911   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    912                                          RTLIB::ADD_F32, RTLIB::ADD_F64,
    913                                          RTLIB::ADD_F80, RTLIB::ADD_PPCF128),
    914                             N, false);
    915   GetPairElements(Call, Lo, Hi);
    916 }
    917 
    918 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
    919                                             SDValue &Lo, SDValue &Hi) {
    920   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    921                                          RTLIB::CEIL_F32, RTLIB::CEIL_F64,
    922                                          RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128),
    923                             N, false);
    924   GetPairElements(Call, Lo, Hi);
    925 }
    926 
    927 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
    928                                                 SDValue &Lo, SDValue &Hi) {
    929   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    930                                          RTLIB::COPYSIGN_F32,
    931                                          RTLIB::COPYSIGN_F64,
    932                                          RTLIB::COPYSIGN_F80,
    933                                          RTLIB::COPYSIGN_PPCF128),
    934                             N, false);
    935   GetPairElements(Call, Lo, Hi);
    936 }
    937 
    938 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
    939                                            SDValue &Lo, SDValue &Hi) {
    940   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    941                                          RTLIB::COS_F32, RTLIB::COS_F64,
    942                                          RTLIB::COS_F80, RTLIB::COS_PPCF128),
    943                             N, false);
    944   GetPairElements(Call, Lo, Hi);
    945 }
    946 
    947 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
    948                                            SDValue &Hi) {
    949   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
    950   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
    951                                           RTLIB::DIV_F32,
    952                                           RTLIB::DIV_F64,
    953                                           RTLIB::DIV_F80,
    954                                           RTLIB::DIV_PPCF128),
    955                              N->getValueType(0), Ops, 2, false,
    956                              N->getDebugLoc());
    957   GetPairElements(Call, Lo, Hi);
    958 }
    959 
    960 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
    961                                            SDValue &Lo, SDValue &Hi) {
    962   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    963                                          RTLIB::EXP_F32, RTLIB::EXP_F64,
    964                                          RTLIB::EXP_F80, RTLIB::EXP_PPCF128),
    965                             N, false);
    966   GetPairElements(Call, Lo, Hi);
    967 }
    968 
    969 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
    970                                             SDValue &Lo, SDValue &Hi) {
    971   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    972                                          RTLIB::EXP2_F32, RTLIB::EXP2_F64,
    973                                          RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128),
    974                             N, false);
    975   GetPairElements(Call, Lo, Hi);
    976 }
    977 
    978 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
    979                                              SDValue &Lo, SDValue &Hi) {
    980   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    981                                          RTLIB::FLOOR_F32,RTLIB::FLOOR_F64,
    982                                          RTLIB::FLOOR_F80,RTLIB::FLOOR_PPCF128),
    983                             N, false);
    984   GetPairElements(Call, Lo, Hi);
    985 }
    986 
    987 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
    988                                            SDValue &Lo, SDValue &Hi) {
    989   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    990                                          RTLIB::LOG_F32, RTLIB::LOG_F64,
    991                                          RTLIB::LOG_F80, RTLIB::LOG_PPCF128),
    992                             N, false);
    993   GetPairElements(Call, Lo, Hi);
    994 }
    995 
    996 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
    997                                             SDValue &Lo, SDValue &Hi) {
    998   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
    999                                          RTLIB::LOG2_F32, RTLIB::LOG2_F64,
   1000                                          RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128),
   1001                             N, false);
   1002   GetPairElements(Call, Lo, Hi);
   1003 }
   1004 
   1005 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
   1006                                              SDValue &Lo, SDValue &Hi) {
   1007   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1008                                          RTLIB::LOG10_F32,RTLIB::LOG10_F64,
   1009                                          RTLIB::LOG10_F80,RTLIB::LOG10_PPCF128),
   1010                             N, false);
   1011   GetPairElements(Call, Lo, Hi);
   1012 }
   1013 
   1014 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
   1015                                           SDValue &Hi) {
   1016   SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
   1017   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
   1018                                           RTLIB::FMA_F32,
   1019                                           RTLIB::FMA_F64,
   1020                                           RTLIB::FMA_F80,
   1021                                           RTLIB::FMA_PPCF128),
   1022                              N->getValueType(0), Ops, 3, false,
   1023                              N->getDebugLoc());
   1024   GetPairElements(Call, Lo, Hi);
   1025 }
   1026 
   1027 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
   1028                                            SDValue &Hi) {
   1029   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   1030   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
   1031                                           RTLIB::MUL_F32,
   1032                                           RTLIB::MUL_F64,
   1033                                           RTLIB::MUL_F80,
   1034                                           RTLIB::MUL_PPCF128),
   1035                              N->getValueType(0), Ops, 2, false,
   1036                              N->getDebugLoc());
   1037   GetPairElements(Call, Lo, Hi);
   1038 }
   1039 
   1040 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
   1041                                                  SDValue &Lo, SDValue &Hi) {
   1042   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1043                                          RTLIB::NEARBYINT_F32,
   1044                                          RTLIB::NEARBYINT_F64,
   1045                                          RTLIB::NEARBYINT_F80,
   1046                                          RTLIB::NEARBYINT_PPCF128),
   1047                             N, false);
   1048   GetPairElements(Call, Lo, Hi);
   1049 }
   1050 
   1051 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
   1052                                            SDValue &Hi) {
   1053   DebugLoc dl = N->getDebugLoc();
   1054   GetExpandedFloat(N->getOperand(0), Lo, Hi);
   1055   Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
   1056   Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
   1057 }
   1058 
   1059 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
   1060                                                 SDValue &Hi) {
   1061   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   1062   Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
   1063   Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
   1064 }
   1065 
   1066 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
   1067                                            SDValue &Lo, SDValue &Hi) {
   1068   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1069                                          RTLIB::POW_F32, RTLIB::POW_F64,
   1070                                          RTLIB::POW_F80, RTLIB::POW_PPCF128),
   1071                             N, false);
   1072   GetPairElements(Call, Lo, Hi);
   1073 }
   1074 
   1075 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
   1076                                             SDValue &Lo, SDValue &Hi) {
   1077   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1078                                          RTLIB::POWI_F32, RTLIB::POWI_F64,
   1079                                          RTLIB::POWI_F80, RTLIB::POWI_PPCF128),
   1080                             N, false);
   1081   GetPairElements(Call, Lo, Hi);
   1082 }
   1083 
   1084 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
   1085                                             SDValue &Lo, SDValue &Hi) {
   1086   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1087                                          RTLIB::RINT_F32, RTLIB::RINT_F64,
   1088                                          RTLIB::RINT_F80, RTLIB::RINT_PPCF128),
   1089                             N, false);
   1090   GetPairElements(Call, Lo, Hi);
   1091 }
   1092 
   1093 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
   1094                                            SDValue &Lo, SDValue &Hi) {
   1095   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1096                                          RTLIB::SIN_F32, RTLIB::SIN_F64,
   1097                                          RTLIB::SIN_F80, RTLIB::SIN_PPCF128),
   1098                             N, false);
   1099   GetPairElements(Call, Lo, Hi);
   1100 }
   1101 
   1102 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
   1103                                             SDValue &Lo, SDValue &Hi) {
   1104   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1105                                          RTLIB::SQRT_F32, RTLIB::SQRT_F64,
   1106                                          RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128),
   1107                             N, false);
   1108   GetPairElements(Call, Lo, Hi);
   1109 }
   1110 
   1111 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
   1112                                            SDValue &Hi) {
   1113   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   1114   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
   1115                                           RTLIB::SUB_F32,
   1116                                           RTLIB::SUB_F64,
   1117                                           RTLIB::SUB_F80,
   1118                                           RTLIB::SUB_PPCF128),
   1119                              N->getValueType(0), Ops, 2, false,
   1120                              N->getDebugLoc());
   1121   GetPairElements(Call, Lo, Hi);
   1122 }
   1123 
   1124 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
   1125                                              SDValue &Lo, SDValue &Hi) {
   1126   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
   1127                                          RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
   1128                                          RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128),
   1129                             N, false);
   1130   GetPairElements(Call, Lo, Hi);
   1131 }
   1132 
   1133 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
   1134                                            SDValue &Hi) {
   1135   if (ISD::isNormalLoad(N)) {
   1136     ExpandRes_NormalLoad(N, Lo, Hi);
   1137     return;
   1138   }
   1139 
   1140   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
   1141   LoadSDNode *LD = cast<LoadSDNode>(N);
   1142   SDValue Chain = LD->getChain();
   1143   SDValue Ptr = LD->getBasePtr();
   1144   DebugLoc dl = N->getDebugLoc();
   1145 
   1146   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
   1147   assert(NVT.isByteSized() && "Expanded type not byte sized!");
   1148   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
   1149 
   1150   Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
   1151                       LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
   1152                       LD->isNonTemporal(), LD->getAlignment());
   1153 
   1154   // Remember the chain.
   1155   Chain = Hi.getValue(1);
   1156 
   1157   // The low part is zero.
   1158   Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
   1159 
   1160   // Modified the chain - switch anything that used the old chain to use the
   1161   // new one.
   1162   ReplaceValueWith(SDValue(LD, 1), Chain);
   1163 }
   1164 
   1165 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
   1166                                                  SDValue &Hi) {
   1167   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
   1168   EVT VT = N->getValueType(0);
   1169   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
   1170   SDValue Src = N->getOperand(0);
   1171   EVT SrcVT = Src.getValueType();
   1172   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
   1173   DebugLoc dl = N->getDebugLoc();
   1174 
   1175   // First do an SINT_TO_FP, whether the original was signed or unsigned.
   1176   // When promoting partial word types to i32 we must honor the signedness,
   1177   // though.
   1178   if (SrcVT.bitsLE(MVT::i32)) {
   1179     // The integer can be represented exactly in an f64.
   1180     Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
   1181                       MVT::i32, Src);
   1182     Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
   1183     Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
   1184   } else {
   1185     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   1186     if (SrcVT.bitsLE(MVT::i64)) {
   1187       Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
   1188                         MVT::i64, Src);
   1189       LC = RTLIB::SINTTOFP_I64_PPCF128;
   1190     } else if (SrcVT.bitsLE(MVT::i128)) {
   1191       Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
   1192       LC = RTLIB::SINTTOFP_I128_PPCF128;
   1193     }
   1194     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
   1195 
   1196     Hi = MakeLibCall(LC, VT, &Src, 1, true, dl);
   1197     GetPairElements(Hi, Lo, Hi);
   1198   }
   1199 
   1200   if (isSigned)
   1201     return;
   1202 
   1203   // Unsigned - fix up the SINT_TO_FP value just calculated.
   1204   Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
   1205   SrcVT = Src.getValueType();
   1206 
   1207   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
   1208   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
   1209   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
   1210   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
   1211   ArrayRef<uint64_t> Parts;
   1212 
   1213   switch (SrcVT.getSimpleVT().SimpleTy) {
   1214   default:
   1215     assert(false && "Unsupported UINT_TO_FP!");
   1216   case MVT::i32:
   1217     Parts = TwoE32;
   1218     break;
   1219   case MVT::i64:
   1220     Parts = TwoE64;
   1221     break;
   1222   case MVT::i128:
   1223     Parts = TwoE128;
   1224     break;
   1225   }
   1226 
   1227   Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
   1228                    DAG.getConstantFP(APFloat(APInt(128, Parts)),
   1229                                      MVT::ppcf128));
   1230   Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
   1231                    Lo, Hi, DAG.getCondCode(ISD::SETLT));
   1232   GetPairElements(Lo, Lo, Hi);
   1233 }
   1234 
   1235 
   1236 //===----------------------------------------------------------------------===//
   1237 //  Float Operand Expansion
   1238 //===----------------------------------------------------------------------===//
   1239 
   1240 /// ExpandFloatOperand - This method is called when the specified operand of the
   1241 /// specified node is found to need expansion.  At this point, all of the result
   1242 /// types of the node are known to be legal, but other operands of the node may
   1243 /// need promotion or expansion as well as the specified one.
   1244 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
   1245   DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
   1246   SDValue Res = SDValue();
   1247 
   1248   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
   1249       == TargetLowering::Custom)
   1250     Res = TLI.LowerOperation(SDValue(N, 0), DAG);
   1251 
   1252   if (Res.getNode() == 0) {
   1253     switch (N->getOpcode()) {
   1254     default:
   1255   #ifndef NDEBUG
   1256       dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
   1257       N->dump(&DAG); dbgs() << "\n";
   1258   #endif
   1259       llvm_unreachable("Do not know how to expand this operator's operand!");
   1260 
   1261     case ISD::BITCAST:         Res = ExpandOp_BITCAST(N); break;
   1262     case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
   1263     case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
   1264 
   1265     case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
   1266     case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
   1267     case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
   1268     case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
   1269     case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
   1270     case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
   1271     case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
   1272                                                     OpNo); break;
   1273     }
   1274   }
   1275 
   1276   // If the result is null, the sub-method took care of registering results etc.
   1277   if (!Res.getNode()) return false;
   1278 
   1279   // If the result is N, the sub-method updated N in place.  Tell the legalizer
   1280   // core about this.
   1281   if (Res.getNode() == N)
   1282     return true;
   1283 
   1284   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
   1285          "Invalid operand expansion");
   1286 
   1287   ReplaceValueWith(SDValue(N, 0), Res);
   1288   return false;
   1289 }
   1290 
   1291 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
   1292 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
   1293 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
   1294                                                 SDValue &NewRHS,
   1295                                                 ISD::CondCode &CCCode,
   1296                                                 DebugLoc dl) {
   1297   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
   1298   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
   1299   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
   1300 
   1301   assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
   1302 
   1303   // FIXME:  This generated code sucks.  We want to generate
   1304   //         FCMPU crN, hi1, hi2
   1305   //         BNE crN, L:
   1306   //         FCMPU crN, lo1, lo2
   1307   // The following can be improved, but not that much.
   1308   SDValue Tmp1, Tmp2, Tmp3;
   1309   Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
   1310                       LHSHi, RHSHi, ISD::SETOEQ);
   1311   Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
   1312                       LHSLo, RHSLo, CCCode);
   1313   Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
   1314   Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
   1315                       LHSHi, RHSHi, ISD::SETUNE);
   1316   Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
   1317                       LHSHi, RHSHi, CCCode);
   1318   Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
   1319   NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
   1320   NewRHS = SDValue();   // LHS is the result, not a compare.
   1321 }
   1322 
   1323 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
   1324   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
   1325   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
   1326   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
   1327 
   1328   // If ExpandSetCCOperands returned a scalar, we need to compare the result
   1329   // against zero to select between true and false values.
   1330   if (NewRHS.getNode() == 0) {
   1331     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
   1332     CCCode = ISD::SETNE;
   1333   }
   1334 
   1335   // Update N to have the operands specified.
   1336   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
   1337                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
   1338                                 N->getOperand(4)), 0);
   1339 }
   1340 
   1341 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
   1342   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
   1343          "Logic only correct for ppcf128!");
   1344   SDValue Lo, Hi;
   1345   GetExpandedFloat(N->getOperand(0), Lo, Hi);
   1346   // Round it the rest of the way (e.g. to f32) if needed.
   1347   return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
   1348                      N->getValueType(0), Hi, N->getOperand(1));
   1349 }
   1350 
   1351 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
   1352   EVT RVT = N->getValueType(0);
   1353   DebugLoc dl = N->getDebugLoc();
   1354 
   1355   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
   1356   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
   1357   if (RVT == MVT::i32) {
   1358     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
   1359            "Logic only correct for ppcf128!");
   1360     SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
   1361                               N->getOperand(0), DAG.getValueType(MVT::f64));
   1362     Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
   1363                       DAG.getIntPtrConstant(1));
   1364     return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
   1365   }
   1366 
   1367   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
   1368   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
   1369   return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false, dl);
   1370 }
   1371 
   1372 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
   1373   EVT RVT = N->getValueType(0);
   1374   DebugLoc dl = N->getDebugLoc();
   1375 
   1376   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
   1377   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
   1378   if (RVT == MVT::i32) {
   1379     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
   1380            "Logic only correct for ppcf128!");
   1381     const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
   1382     APFloat APF = APFloat(APInt(128, TwoE31));
   1383     SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
   1384     //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
   1385     // FIXME: generated code sucks.
   1386     return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
   1387                        DAG.getNode(ISD::ADD, dl, MVT::i32,
   1388                                    DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
   1389                                                DAG.getNode(ISD::FSUB, dl,
   1390                                                            MVT::ppcf128,
   1391                                                            N->getOperand(0),
   1392                                                            Tmp)),
   1393                                    DAG.getConstant(0x80000000, MVT::i32)),
   1394                        DAG.getNode(ISD::FP_TO_SINT, dl,
   1395                                    MVT::i32, N->getOperand(0)),
   1396                        DAG.getCondCode(ISD::SETGE));
   1397   }
   1398 
   1399   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
   1400   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
   1401   return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false, dl);
   1402 }
   1403 
   1404 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
   1405   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
   1406   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
   1407   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
   1408 
   1409   // If ExpandSetCCOperands returned a scalar, we need to compare the result
   1410   // against zero to select between true and false values.
   1411   if (NewRHS.getNode() == 0) {
   1412     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
   1413     CCCode = ISD::SETNE;
   1414   }
   1415 
   1416   // Update N to have the operands specified.
   1417   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
   1418                                 N->getOperand(2), N->getOperand(3),
   1419                                 DAG.getCondCode(CCCode)), 0);
   1420 }
   1421 
   1422 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
   1423   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
   1424   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
   1425   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
   1426 
   1427   // If ExpandSetCCOperands returned a scalar, use it.
   1428   if (NewRHS.getNode() == 0) {
   1429     assert(NewLHS.getValueType() == N->getValueType(0) &&
   1430            "Unexpected setcc expansion!");
   1431     return NewLHS;
   1432   }
   1433 
   1434   // Otherwise, update N to have the operands specified.
   1435   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
   1436                                 DAG.getCondCode(CCCode)), 0);
   1437 }
   1438 
   1439 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
   1440   if (ISD::isNormalStore(N))
   1441     return ExpandOp_NormalStore(N, OpNo);
   1442 
   1443   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
   1444   assert(OpNo == 1 && "Can only expand the stored value so far");
   1445   StoreSDNode *ST = cast<StoreSDNode>(N);
   1446 
   1447   SDValue Chain = ST->getChain();
   1448   SDValue Ptr = ST->getBasePtr();
   1449 
   1450   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
   1451                                      ST->getValue().getValueType());
   1452   assert(NVT.isByteSized() && "Expanded type not byte sized!");
   1453   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
   1454   (void)NVT;
   1455 
   1456   SDValue Lo, Hi;
   1457   GetExpandedOp(ST->getValue(), Lo, Hi);
   1458 
   1459   return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
   1460                            ST->getPointerInfo(),
   1461                            ST->getMemoryVT(), ST->isVolatile(),
   1462                            ST->isNonTemporal(), ST->getAlignment());
   1463 }
   1464