Home | History | Annotate | Download | only in SelectionDAG
      1 //===----- LegalizeIntegerTypes.cpp - Legalization of integer 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 integer type expansion and promotion for LegalizeTypes.
     11 // Promotion is the act of changing a computation in an illegal type into a
     12 // computation in a larger type.  For example, implementing i8 arithmetic in an
     13 // i32 register (often needed on powerpc).
     14 // Expansion is the act of changing a computation in an illegal type into a
     15 // computation in two identical registers of a smaller type.  For example,
     16 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
     17 // targets).
     18 //
     19 //===----------------------------------------------------------------------===//
     20 
     21 #include "LegalizeTypes.h"
     22 #include "llvm/IR/DerivedTypes.h"
     23 #include "llvm/Support/ErrorHandling.h"
     24 #include "llvm/Support/raw_ostream.h"
     25 using namespace llvm;
     26 
     27 #define DEBUG_TYPE "legalize-types"
     28 
     29 //===----------------------------------------------------------------------===//
     30 //  Integer Result Promotion
     31 //===----------------------------------------------------------------------===//
     32 
     33 /// PromoteIntegerResult - This method is called when a result of a node is
     34 /// found to be in need of promotion to a larger type.  At this point, the node
     35 /// may also have invalid operands or may have other results that need
     36 /// expansion, we just know that (at least) one result needs promotion.
     37 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
     38   DEBUG(dbgs() << "Promote integer result: "; N->dump(&DAG); dbgs() << "\n");
     39   SDValue Res = SDValue();
     40 
     41   // See if the target wants to custom expand this node.
     42   if (CustomLowerNode(N, N->getValueType(ResNo), true))
     43     return;
     44 
     45   switch (N->getOpcode()) {
     46   default:
     47 #ifndef NDEBUG
     48     dbgs() << "PromoteIntegerResult #" << ResNo << ": ";
     49     N->dump(&DAG); dbgs() << "\n";
     50 #endif
     51     llvm_unreachable("Do not know how to promote this operator!");
     52   case ISD::MERGE_VALUES:Res = PromoteIntRes_MERGE_VALUES(N, ResNo); break;
     53   case ISD::AssertSext:  Res = PromoteIntRes_AssertSext(N); break;
     54   case ISD::AssertZext:  Res = PromoteIntRes_AssertZext(N); break;
     55   case ISD::BITCAST:     Res = PromoteIntRes_BITCAST(N); break;
     56   case ISD::BITREVERSE:  Res = PromoteIntRes_BITREVERSE(N); break;
     57   case ISD::BSWAP:       Res = PromoteIntRes_BSWAP(N); break;
     58   case ISD::BUILD_PAIR:  Res = PromoteIntRes_BUILD_PAIR(N); break;
     59   case ISD::Constant:    Res = PromoteIntRes_Constant(N); break;
     60   case ISD::CONVERT_RNDSAT:
     61                          Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
     62   case ISD::CTLZ_ZERO_UNDEF:
     63   case ISD::CTLZ:        Res = PromoteIntRes_CTLZ(N); break;
     64   case ISD::CTPOP:       Res = PromoteIntRes_CTPOP(N); break;
     65   case ISD::CTTZ_ZERO_UNDEF:
     66   case ISD::CTTZ:        Res = PromoteIntRes_CTTZ(N); break;
     67   case ISD::EXTRACT_VECTOR_ELT:
     68                          Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
     69   case ISD::LOAD:        Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N)); break;
     70   case ISD::MLOAD:       Res = PromoteIntRes_MLOAD(cast<MaskedLoadSDNode>(N));
     71     break;
     72   case ISD::MGATHER:     Res = PromoteIntRes_MGATHER(cast<MaskedGatherSDNode>(N));
     73     break;
     74   case ISD::SELECT:      Res = PromoteIntRes_SELECT(N); break;
     75   case ISD::VSELECT:     Res = PromoteIntRes_VSELECT(N); break;
     76   case ISD::SELECT_CC:   Res = PromoteIntRes_SELECT_CC(N); break;
     77   case ISD::SETCC:       Res = PromoteIntRes_SETCC(N); break;
     78   case ISD::SMIN:
     79   case ISD::SMAX:        Res = PromoteIntRes_SExtIntBinOp(N); break;
     80   case ISD::UMIN:
     81   case ISD::UMAX:        Res = PromoteIntRes_ZExtIntBinOp(N); break;
     82 
     83   case ISD::SHL:         Res = PromoteIntRes_SHL(N); break;
     84   case ISD::SIGN_EXTEND_INREG:
     85                          Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
     86   case ISD::SRA:         Res = PromoteIntRes_SRA(N); break;
     87   case ISD::SRL:         Res = PromoteIntRes_SRL(N); break;
     88   case ISD::TRUNCATE:    Res = PromoteIntRes_TRUNCATE(N); break;
     89   case ISD::UNDEF:       Res = PromoteIntRes_UNDEF(N); break;
     90   case ISD::VAARG:       Res = PromoteIntRes_VAARG(N); break;
     91 
     92   case ISD::EXTRACT_SUBVECTOR:
     93                          Res = PromoteIntRes_EXTRACT_SUBVECTOR(N); break;
     94   case ISD::VECTOR_SHUFFLE:
     95                          Res = PromoteIntRes_VECTOR_SHUFFLE(N); break;
     96   case ISD::INSERT_VECTOR_ELT:
     97                          Res = PromoteIntRes_INSERT_VECTOR_ELT(N); break;
     98   case ISD::BUILD_VECTOR:
     99                          Res = PromoteIntRes_BUILD_VECTOR(N); break;
    100   case ISD::SCALAR_TO_VECTOR:
    101                          Res = PromoteIntRes_SCALAR_TO_VECTOR(N); break;
    102   case ISD::CONCAT_VECTORS:
    103                          Res = PromoteIntRes_CONCAT_VECTORS(N); break;
    104 
    105   case ISD::SIGN_EXTEND:
    106   case ISD::ZERO_EXTEND:
    107   case ISD::ANY_EXTEND:  Res = PromoteIntRes_INT_EXTEND(N); break;
    108 
    109   case ISD::FP_TO_SINT:
    110   case ISD::FP_TO_UINT:  Res = PromoteIntRes_FP_TO_XINT(N); break;
    111 
    112   case ISD::FP_TO_FP16:  Res = PromoteIntRes_FP_TO_FP16(N); break;
    113 
    114   case ISD::AND:
    115   case ISD::OR:
    116   case ISD::XOR:
    117   case ISD::ADD:
    118   case ISD::SUB:
    119   case ISD::MUL:         Res = PromoteIntRes_SimpleIntBinOp(N); break;
    120 
    121   case ISD::SDIV:
    122   case ISD::SREM:        Res = PromoteIntRes_SExtIntBinOp(N); break;
    123 
    124   case ISD::UDIV:
    125   case ISD::UREM:        Res = PromoteIntRes_ZExtIntBinOp(N); break;
    126 
    127   case ISD::SADDO:
    128   case ISD::SSUBO:       Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
    129   case ISD::UADDO:
    130   case ISD::USUBO:       Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
    131   case ISD::SMULO:
    132   case ISD::UMULO:       Res = PromoteIntRes_XMULO(N, ResNo); break;
    133 
    134   case ISD::ATOMIC_LOAD:
    135     Res = PromoteIntRes_Atomic0(cast<AtomicSDNode>(N)); break;
    136 
    137   case ISD::ATOMIC_LOAD_ADD:
    138   case ISD::ATOMIC_LOAD_SUB:
    139   case ISD::ATOMIC_LOAD_AND:
    140   case ISD::ATOMIC_LOAD_OR:
    141   case ISD::ATOMIC_LOAD_XOR:
    142   case ISD::ATOMIC_LOAD_NAND:
    143   case ISD::ATOMIC_LOAD_MIN:
    144   case ISD::ATOMIC_LOAD_MAX:
    145   case ISD::ATOMIC_LOAD_UMIN:
    146   case ISD::ATOMIC_LOAD_UMAX:
    147   case ISD::ATOMIC_SWAP:
    148     Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
    149 
    150   case ISD::ATOMIC_CMP_SWAP:
    151   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
    152     Res = PromoteIntRes_AtomicCmpSwap(cast<AtomicSDNode>(N), ResNo);
    153     break;
    154   }
    155 
    156   // If the result is null then the sub-method took care of registering it.
    157   if (Res.getNode())
    158     SetPromotedInteger(SDValue(N, ResNo), Res);
    159 }
    160 
    161 SDValue DAGTypeLegalizer::PromoteIntRes_MERGE_VALUES(SDNode *N,
    162                                                      unsigned ResNo) {
    163   SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
    164   return GetPromotedInteger(Op);
    165 }
    166 
    167 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
    168   // Sign-extend the new bits, and continue the assertion.
    169   SDValue Op = SExtPromotedInteger(N->getOperand(0));
    170   return DAG.getNode(ISD::AssertSext, SDLoc(N),
    171                      Op.getValueType(), Op, N->getOperand(1));
    172 }
    173 
    174 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
    175   // Zero the new bits, and continue the assertion.
    176   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
    177   return DAG.getNode(ISD::AssertZext, SDLoc(N),
    178                      Op.getValueType(), Op, N->getOperand(1));
    179 }
    180 
    181 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic0(AtomicSDNode *N) {
    182   EVT ResVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    183   SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
    184                               N->getMemoryVT(), ResVT,
    185                               N->getChain(), N->getBasePtr(),
    186                               N->getMemOperand(), N->getOrdering(),
    187                               N->getSynchScope());
    188   // Legalize the chain result - switch anything that used the old chain to
    189   // use the new one.
    190   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
    191   return Res;
    192 }
    193 
    194 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
    195   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
    196   SDValue Res = DAG.getAtomic(N->getOpcode(), SDLoc(N),
    197                               N->getMemoryVT(),
    198                               N->getChain(), N->getBasePtr(),
    199                               Op2, N->getMemOperand(), N->getOrdering(),
    200                               N->getSynchScope());
    201   // Legalize the chain result - switch anything that used the old chain to
    202   // use the new one.
    203   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
    204   return Res;
    205 }
    206 
    207 SDValue DAGTypeLegalizer::PromoteIntRes_AtomicCmpSwap(AtomicSDNode *N,
    208                                                       unsigned ResNo) {
    209   if (ResNo == 1) {
    210     assert(N->getOpcode() == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
    211     EVT SVT = getSetCCResultType(N->getOperand(2).getValueType());
    212     EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
    213 
    214     // Only use the result of getSetCCResultType if it is legal,
    215     // otherwise just use the promoted result type (NVT).
    216     if (!TLI.isTypeLegal(SVT))
    217       SVT = NVT;
    218 
    219     SDVTList VTs = DAG.getVTList(N->getValueType(0), SVT, MVT::Other);
    220     SDValue Res = DAG.getAtomicCmpSwap(
    221         ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, SDLoc(N), N->getMemoryVT(), VTs,
    222         N->getChain(), N->getBasePtr(), N->getOperand(2), N->getOperand(3),
    223         N->getMemOperand(), N->getSuccessOrdering(), N->getFailureOrdering(),
    224         N->getSynchScope());
    225     ReplaceValueWith(SDValue(N, 0), Res.getValue(0));
    226     ReplaceValueWith(SDValue(N, 2), Res.getValue(2));
    227     return Res.getValue(1);
    228   }
    229 
    230   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
    231   SDValue Op3 = GetPromotedInteger(N->getOperand(3));
    232   SDVTList VTs =
    233       DAG.getVTList(Op2.getValueType(), N->getValueType(1), MVT::Other);
    234   SDValue Res = DAG.getAtomicCmpSwap(
    235       N->getOpcode(), SDLoc(N), N->getMemoryVT(), VTs, N->getChain(),
    236       N->getBasePtr(), Op2, Op3, N->getMemOperand(), N->getSuccessOrdering(),
    237       N->getFailureOrdering(), N->getSynchScope());
    238   // Update the use to N with the newly created Res.
    239   for (unsigned i = 1, NumResults = N->getNumValues(); i < NumResults; ++i)
    240     ReplaceValueWith(SDValue(N, i), Res.getValue(i));
    241   return Res;
    242 }
    243 
    244 SDValue DAGTypeLegalizer::PromoteIntRes_BITCAST(SDNode *N) {
    245   SDValue InOp = N->getOperand(0);
    246   EVT InVT = InOp.getValueType();
    247   EVT NInVT = TLI.getTypeToTransformTo(*DAG.getContext(), InVT);
    248   EVT OutVT = N->getValueType(0);
    249   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
    250   SDLoc dl(N);
    251 
    252   switch (getTypeAction(InVT)) {
    253   case TargetLowering::TypeLegal:
    254     break;
    255   case TargetLowering::TypePromoteInteger:
    256     if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector() && !NInVT.isVector())
    257       // The input promotes to the same size.  Convert the promoted value.
    258       return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetPromotedInteger(InOp));
    259     break;
    260   case TargetLowering::TypeSoftenFloat:
    261     // Promote the integer operand by hand.
    262     return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
    263   case TargetLowering::TypePromoteFloat: {
    264     // Convert the promoted float by hand.
    265     SDValue PromotedOp = GetPromotedFloat(InOp);
    266     return DAG.getNode(ISD::FP_TO_FP16, dl, NOutVT, PromotedOp);
    267     break;
    268   }
    269   case TargetLowering::TypeExpandInteger:
    270   case TargetLowering::TypeExpandFloat:
    271     break;
    272   case TargetLowering::TypeScalarizeVector:
    273     // Convert the element to an integer and promote it by hand.
    274     if (!NOutVT.isVector())
    275       return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
    276                          BitConvertToInteger(GetScalarizedVector(InOp)));
    277     break;
    278   case TargetLowering::TypeSplitVector: {
    279     // For example, i32 = BITCAST v2i16 on alpha.  Convert the split
    280     // pieces of the input into integers and reassemble in the final type.
    281     SDValue Lo, Hi;
    282     GetSplitVector(N->getOperand(0), Lo, Hi);
    283     Lo = BitConvertToInteger(Lo);
    284     Hi = BitConvertToInteger(Hi);
    285 
    286     if (DAG.getDataLayout().isBigEndian())
    287       std::swap(Lo, Hi);
    288 
    289     InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
    290                        EVT::getIntegerVT(*DAG.getContext(),
    291                                          NOutVT.getSizeInBits()),
    292                        JoinIntegers(Lo, Hi));
    293     return DAG.getNode(ISD::BITCAST, dl, NOutVT, InOp);
    294   }
    295   case TargetLowering::TypeWidenVector:
    296     // The input is widened to the same size. Convert to the widened value.
    297     // Make sure that the outgoing value is not a vector, because this would
    298     // make us bitcast between two vectors which are legalized in different ways.
    299     if (NOutVT.bitsEq(NInVT) && !NOutVT.isVector())
    300       return DAG.getNode(ISD::BITCAST, dl, NOutVT, GetWidenedVector(InOp));
    301   }
    302 
    303   return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
    304                      CreateStackStoreLoad(InOp, OutVT));
    305 }
    306 
    307 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
    308   SDValue Op = GetPromotedInteger(N->getOperand(0));
    309   EVT OVT = N->getValueType(0);
    310   EVT NVT = Op.getValueType();
    311   SDLoc dl(N);
    312 
    313   unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
    314   return DAG.getNode(
    315       ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
    316       DAG.getConstant(DiffBits, dl,
    317                       TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
    318 }
    319 
    320 SDValue DAGTypeLegalizer::PromoteIntRes_BITREVERSE(SDNode *N) {
    321   SDValue Op = GetPromotedInteger(N->getOperand(0));
    322   EVT OVT = N->getValueType(0);
    323   EVT NVT = Op.getValueType();
    324   SDLoc dl(N);
    325 
    326   unsigned DiffBits = NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits();
    327   return DAG.getNode(
    328       ISD::SRL, dl, NVT, DAG.getNode(ISD::BITREVERSE, dl, NVT, Op),
    329       DAG.getConstant(DiffBits, dl,
    330                       TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
    331 }
    332 
    333 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
    334   // The pair element type may be legal, or may not promote to the same type as
    335   // the result, for example i14 = BUILD_PAIR (i7, i7).  Handle all cases.
    336   return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N),
    337                      TLI.getTypeToTransformTo(*DAG.getContext(),
    338                      N->getValueType(0)), JoinIntegers(N->getOperand(0),
    339                      N->getOperand(1)));
    340 }
    341 
    342 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
    343   EVT VT = N->getValueType(0);
    344   // FIXME there is no actual debug info here
    345   SDLoc dl(N);
    346   // Zero extend things like i1, sign extend everything else.  It shouldn't
    347   // matter in theory which one we pick, but this tends to give better code?
    348   unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
    349   SDValue Result = DAG.getNode(Opc, dl,
    350                                TLI.getTypeToTransformTo(*DAG.getContext(), VT),
    351                                SDValue(N, 0));
    352   assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
    353   return Result;
    354 }
    355 
    356 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
    357   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
    358   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
    359            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
    360            CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
    361           "can only promote integers");
    362   EVT OutVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    363   return DAG.getConvertRndSat(OutVT, SDLoc(N), N->getOperand(0),
    364                               N->getOperand(1), N->getOperand(2),
    365                               N->getOperand(3), N->getOperand(4), CvtCode);
    366 }
    367 
    368 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
    369   // Zero extend to the promoted type and do the count there.
    370   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
    371   SDLoc dl(N);
    372   EVT OVT = N->getValueType(0);
    373   EVT NVT = Op.getValueType();
    374   Op = DAG.getNode(N->getOpcode(), dl, NVT, Op);
    375   // Subtract off the extra leading bits in the bigger type.
    376   return DAG.getNode(
    377       ISD::SUB, dl, NVT, Op,
    378       DAG.getConstant(NVT.getScalarSizeInBits() - OVT.getScalarSizeInBits(), dl,
    379                       NVT));
    380 }
    381 
    382 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
    383   // Zero extend to the promoted type and do the count there.
    384   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
    385   return DAG.getNode(ISD::CTPOP, SDLoc(N), Op.getValueType(), Op);
    386 }
    387 
    388 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
    389   SDValue Op = GetPromotedInteger(N->getOperand(0));
    390   EVT OVT = N->getValueType(0);
    391   EVT NVT = Op.getValueType();
    392   SDLoc dl(N);
    393   if (N->getOpcode() == ISD::CTTZ) {
    394     // The count is the same in the promoted type except if the original
    395     // value was zero.  This can be handled by setting the bit just off
    396     // the top of the original type.
    397     auto TopBit = APInt::getOneBitSet(NVT.getScalarSizeInBits(),
    398                                       OVT.getScalarSizeInBits());
    399     Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, dl, NVT));
    400   }
    401   return DAG.getNode(N->getOpcode(), dl, NVT, Op);
    402 }
    403 
    404 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
    405   SDLoc dl(N);
    406   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    407   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NVT, N->getOperand(0),
    408                      N->getOperand(1));
    409 }
    410 
    411 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
    412   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    413   unsigned NewOpc = N->getOpcode();
    414   SDLoc dl(N);
    415 
    416   // If we're promoting a UINT to a larger size and the larger FP_TO_UINT is
    417   // not Legal, check to see if we can use FP_TO_SINT instead.  (If both UINT
    418   // and SINT conversions are Custom, there is no way to tell which is
    419   // preferable. We choose SINT because that's the right thing on PPC.)
    420   if (N->getOpcode() == ISD::FP_TO_UINT &&
    421       !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
    422       TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
    423     NewOpc = ISD::FP_TO_SINT;
    424 
    425   SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
    426 
    427   // Assert that the converted value fits in the original type.  If it doesn't
    428   // (eg: because the value being converted is too big), then the result of the
    429   // original operation was undefined anyway, so the assert is still correct.
    430   return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
    431                      ISD::AssertZext : ISD::AssertSext, dl, NVT, Res,
    432                      DAG.getValueType(N->getValueType(0).getScalarType()));
    433 }
    434 
    435 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_FP16(SDNode *N) {
    436   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    437   SDLoc dl(N);
    438 
    439   return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
    440 }
    441 
    442 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
    443   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    444   SDLoc dl(N);
    445 
    446   if (getTypeAction(N->getOperand(0).getValueType())
    447       == TargetLowering::TypePromoteInteger) {
    448     SDValue Res = GetPromotedInteger(N->getOperand(0));
    449     assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
    450 
    451     // If the result and operand types are the same after promotion, simplify
    452     // to an in-register extension.
    453     if (NVT == Res.getValueType()) {
    454       // The high bits are not guaranteed to be anything.  Insert an extend.
    455       if (N->getOpcode() == ISD::SIGN_EXTEND)
    456         return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
    457                            DAG.getValueType(N->getOperand(0).getValueType()));
    458       if (N->getOpcode() == ISD::ZERO_EXTEND)
    459         return DAG.getZeroExtendInReg(Res, dl,
    460                       N->getOperand(0).getValueType().getScalarType());
    461       assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
    462       return Res;
    463     }
    464   }
    465 
    466   // Otherwise, just extend the original operand all the way to the larger type.
    467   return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
    468 }
    469 
    470 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
    471   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
    472   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    473   ISD::LoadExtType ExtType =
    474     ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
    475   SDLoc dl(N);
    476   SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
    477                                N->getMemoryVT(), N->getMemOperand());
    478 
    479   // Legalize the chain result - switch anything that used the old chain to
    480   // use the new one.
    481   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
    482   return Res;
    483 }
    484 
    485 SDValue DAGTypeLegalizer::PromoteIntRes_MLOAD(MaskedLoadSDNode *N) {
    486   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    487   SDValue ExtSrc0 = GetPromotedInteger(N->getSrc0());
    488 
    489   SDLoc dl(N);
    490   SDValue Res = DAG.getMaskedLoad(NVT, dl, N->getChain(), N->getBasePtr(),
    491                                   N->getMask(), ExtSrc0, N->getMemoryVT(),
    492                                   N->getMemOperand(), ISD::SEXTLOAD);
    493   // Legalize the chain result - switch anything that used the old chain to
    494   // use the new one.
    495   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
    496   return Res;
    497 }
    498 
    499 SDValue DAGTypeLegalizer::PromoteIntRes_MGATHER(MaskedGatherSDNode *N) {
    500   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    501   SDValue ExtSrc0 = GetPromotedInteger(N->getValue());
    502   assert(NVT == ExtSrc0.getValueType() &&
    503       "Gather result type and the passThru agrument type should be the same");
    504 
    505   SDLoc dl(N);
    506   SDValue Ops[] = {N->getChain(), ExtSrc0, N->getMask(), N->getBasePtr(),
    507                    N->getIndex()};
    508   SDValue Res = DAG.getMaskedGather(DAG.getVTList(NVT, MVT::Other),
    509                                     N->getMemoryVT(), dl, Ops,
    510                                     N->getMemOperand());
    511   // Legalize the chain result - switch anything that used the old chain to
    512   // use the new one.
    513   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
    514   return Res;
    515 }
    516 
    517 /// Promote the overflow flag of an overflowing arithmetic node.
    518 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
    519   // Simply change the return type of the boolean result.
    520   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(1));
    521   EVT ValueVTs[] = { N->getValueType(0), NVT };
    522   SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
    523   SDValue Res = DAG.getNode(N->getOpcode(), SDLoc(N),
    524                             DAG.getVTList(ValueVTs), Ops);
    525 
    526   // Modified the sum result - switch anything that used the old sum to use
    527   // the new one.
    528   ReplaceValueWith(SDValue(N, 0), Res);
    529 
    530   return SDValue(Res.getNode(), 1);
    531 }
    532 
    533 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
    534   if (ResNo == 1)
    535     return PromoteIntRes_Overflow(N);
    536 
    537   // The operation overflowed iff the result in the larger type is not the
    538   // sign extension of its truncation to the original type.
    539   SDValue LHS = SExtPromotedInteger(N->getOperand(0));
    540   SDValue RHS = SExtPromotedInteger(N->getOperand(1));
    541   EVT OVT = N->getOperand(0).getValueType();
    542   EVT NVT = LHS.getValueType();
    543   SDLoc dl(N);
    544 
    545   // Do the arithmetic in the larger type.
    546   unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
    547   SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
    548 
    549   // Calculate the overflow flag: sign extend the arithmetic result from
    550   // the original type.
    551   SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
    552                             DAG.getValueType(OVT));
    553   // Overflowed if and only if this is not equal to Res.
    554   Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
    555 
    556   // Use the calculated overflow everywhere.
    557   ReplaceValueWith(SDValue(N, 1), Ofl);
    558 
    559   return Res;
    560 }
    561 
    562 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
    563   SDValue LHS = GetPromotedInteger(N->getOperand(1));
    564   SDValue RHS = GetPromotedInteger(N->getOperand(2));
    565   return DAG.getSelect(SDLoc(N),
    566                        LHS.getValueType(), N->getOperand(0), LHS, RHS);
    567 }
    568 
    569 SDValue DAGTypeLegalizer::PromoteIntRes_VSELECT(SDNode *N) {
    570   SDValue Mask = N->getOperand(0);
    571   EVT OpTy = N->getOperand(1).getValueType();
    572 
    573   // Promote all the way up to the canonical SetCC type.
    574   Mask = PromoteTargetBoolean(Mask, OpTy);
    575   SDValue LHS = GetPromotedInteger(N->getOperand(1));
    576   SDValue RHS = GetPromotedInteger(N->getOperand(2));
    577   return DAG.getNode(ISD::VSELECT, SDLoc(N),
    578                      LHS.getValueType(), Mask, LHS, RHS);
    579 }
    580 
    581 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
    582   SDValue LHS = GetPromotedInteger(N->getOperand(2));
    583   SDValue RHS = GetPromotedInteger(N->getOperand(3));
    584   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
    585                      LHS.getValueType(), N->getOperand(0),
    586                      N->getOperand(1), LHS, RHS, N->getOperand(4));
    587 }
    588 
    589 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
    590   EVT SVT = getSetCCResultType(N->getOperand(0).getValueType());
    591 
    592   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    593 
    594   // Only use the result of getSetCCResultType if it is legal,
    595   // otherwise just use the promoted result type (NVT).
    596   if (!TLI.isTypeLegal(SVT))
    597     SVT = NVT;
    598 
    599   SDLoc dl(N);
    600   assert(SVT.isVector() == N->getOperand(0).getValueType().isVector() &&
    601          "Vector compare must return a vector result!");
    602 
    603   SDValue LHS = N->getOperand(0);
    604   SDValue RHS = N->getOperand(1);
    605   if (LHS.getValueType() != RHS.getValueType()) {
    606     if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger &&
    607         !LHS.getValueType().isVector())
    608       LHS = GetPromotedInteger(LHS);
    609     if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger &&
    610         !RHS.getValueType().isVector())
    611       RHS = GetPromotedInteger(RHS);
    612   }
    613 
    614   // Get the SETCC result using the canonical SETCC type.
    615   SDValue SetCC = DAG.getNode(N->getOpcode(), dl, SVT, LHS, RHS,
    616                               N->getOperand(2));
    617 
    618   assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
    619   // Convert to the expected type.
    620   return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
    621 }
    622 
    623 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
    624   SDValue LHS = N->getOperand(0);
    625   SDValue RHS = N->getOperand(1);
    626   if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger)
    627     LHS = GetPromotedInteger(LHS);
    628   if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
    629     RHS = ZExtPromotedInteger(RHS);
    630   return DAG.getNode(ISD::SHL, SDLoc(N), LHS.getValueType(), LHS, RHS);
    631 }
    632 
    633 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
    634   SDValue Op = GetPromotedInteger(N->getOperand(0));
    635   return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N),
    636                      Op.getValueType(), Op, N->getOperand(1));
    637 }
    638 
    639 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
    640   // The input may have strange things in the top bits of the registers, but
    641   // these operations don't care.  They may have weird bits going out, but
    642   // that too is okay if they are integer operations.
    643   SDValue LHS = GetPromotedInteger(N->getOperand(0));
    644   SDValue RHS = GetPromotedInteger(N->getOperand(1));
    645   return DAG.getNode(N->getOpcode(), SDLoc(N),
    646                      LHS.getValueType(), LHS, RHS);
    647 }
    648 
    649 SDValue DAGTypeLegalizer::PromoteIntRes_SExtIntBinOp(SDNode *N) {
    650   // Sign extend the input.
    651   SDValue LHS = SExtPromotedInteger(N->getOperand(0));
    652   SDValue RHS = SExtPromotedInteger(N->getOperand(1));
    653   return DAG.getNode(N->getOpcode(), SDLoc(N),
    654                      LHS.getValueType(), LHS, RHS);
    655 }
    656 
    657 SDValue DAGTypeLegalizer::PromoteIntRes_ZExtIntBinOp(SDNode *N) {
    658   // Zero extend the input.
    659   SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
    660   SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
    661   return DAG.getNode(N->getOpcode(), SDLoc(N),
    662                      LHS.getValueType(), LHS, RHS);
    663 }
    664 
    665 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
    666   SDValue LHS = N->getOperand(0);
    667   SDValue RHS = N->getOperand(1);
    668   // The input value must be properly sign extended.
    669   if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger)
    670     LHS = SExtPromotedInteger(LHS);
    671   if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
    672     RHS = ZExtPromotedInteger(RHS);
    673   return DAG.getNode(ISD::SRA, SDLoc(N), LHS.getValueType(), LHS, RHS);
    674 }
    675 
    676 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
    677   SDValue LHS = N->getOperand(0);
    678   SDValue RHS = N->getOperand(1);
    679   // The input value must be properly zero extended.
    680   if (getTypeAction(LHS.getValueType()) == TargetLowering::TypePromoteInteger)
    681     LHS = ZExtPromotedInteger(LHS);
    682   if (getTypeAction(RHS.getValueType()) == TargetLowering::TypePromoteInteger)
    683     RHS = ZExtPromotedInteger(RHS);
    684   return DAG.getNode(ISD::SRL, SDLoc(N), LHS.getValueType(), LHS, RHS);
    685 }
    686 
    687 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
    688   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    689   SDValue Res;
    690   SDValue InOp = N->getOperand(0);
    691   SDLoc dl(N);
    692 
    693   switch (getTypeAction(InOp.getValueType())) {
    694   default: llvm_unreachable("Unknown type action!");
    695   case TargetLowering::TypeLegal:
    696   case TargetLowering::TypeExpandInteger:
    697     Res = InOp;
    698     break;
    699   case TargetLowering::TypePromoteInteger:
    700     Res = GetPromotedInteger(InOp);
    701     break;
    702   case TargetLowering::TypeSplitVector:
    703     EVT InVT = InOp.getValueType();
    704     assert(InVT.isVector() && "Cannot split scalar types");
    705     unsigned NumElts = InVT.getVectorNumElements();
    706     assert(NumElts == NVT.getVectorNumElements() &&
    707            "Dst and Src must have the same number of elements");
    708     assert(isPowerOf2_32(NumElts) &&
    709            "Promoted vector type must be a power of two");
    710 
    711     SDValue EOp1, EOp2;
    712     GetSplitVector(InOp, EOp1, EOp2);
    713 
    714     EVT HalfNVT = EVT::getVectorVT(*DAG.getContext(), NVT.getScalarType(),
    715                                    NumElts/2);
    716     EOp1 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp1);
    717     EOp2 = DAG.getNode(ISD::TRUNCATE, dl, HalfNVT, EOp2);
    718 
    719     return DAG.getNode(ISD::CONCAT_VECTORS, dl, NVT, EOp1, EOp2);
    720   }
    721 
    722   // Truncate to NVT instead of VT
    723   return DAG.getNode(ISD::TRUNCATE, dl, NVT, Res);
    724 }
    725 
    726 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
    727   if (ResNo == 1)
    728     return PromoteIntRes_Overflow(N);
    729 
    730   // The operation overflowed iff the result in the larger type is not the
    731   // zero extension of its truncation to the original type.
    732   SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
    733   SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
    734   EVT OVT = N->getOperand(0).getValueType();
    735   EVT NVT = LHS.getValueType();
    736   SDLoc dl(N);
    737 
    738   // Do the arithmetic in the larger type.
    739   unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
    740   SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
    741 
    742   // Calculate the overflow flag: zero extend the arithmetic result from
    743   // the original type.
    744   SDValue Ofl = DAG.getZeroExtendInReg(Res, dl, OVT);
    745   // Overflowed if and only if this is not equal to Res.
    746   Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
    747 
    748   // Use the calculated overflow everywhere.
    749   ReplaceValueWith(SDValue(N, 1), Ofl);
    750 
    751   return Res;
    752 }
    753 
    754 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
    755   // Promote the overflow bit trivially.
    756   if (ResNo == 1)
    757     return PromoteIntRes_Overflow(N);
    758 
    759   SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
    760   SDLoc DL(N);
    761   EVT SmallVT = LHS.getValueType();
    762 
    763   // To determine if the result overflowed in a larger type, we extend the
    764   // input to the larger type, do the multiply (checking if it overflows),
    765   // then also check the high bits of the result to see if overflow happened
    766   // there.
    767   if (N->getOpcode() == ISD::SMULO) {
    768     LHS = SExtPromotedInteger(LHS);
    769     RHS = SExtPromotedInteger(RHS);
    770   } else {
    771     LHS = ZExtPromotedInteger(LHS);
    772     RHS = ZExtPromotedInteger(RHS);
    773   }
    774   SDVTList VTs = DAG.getVTList(LHS.getValueType(), N->getValueType(1));
    775   SDValue Mul = DAG.getNode(N->getOpcode(), DL, VTs, LHS, RHS);
    776 
    777   // Overflow occurred if it occurred in the larger type, or if the high part
    778   // of the result does not zero/sign-extend the low part.  Check this second
    779   // possibility first.
    780   SDValue Overflow;
    781   if (N->getOpcode() == ISD::UMULO) {
    782     // Unsigned overflow occurred if the high part is non-zero.
    783     SDValue Hi = DAG.getNode(ISD::SRL, DL, Mul.getValueType(), Mul,
    784                              DAG.getIntPtrConstant(SmallVT.getSizeInBits(),
    785                                                    DL));
    786     Overflow = DAG.getSetCC(DL, N->getValueType(1), Hi,
    787                             DAG.getConstant(0, DL, Hi.getValueType()),
    788                             ISD::SETNE);
    789   } else {
    790     // Signed overflow occurred if the high part does not sign extend the low.
    791     SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, Mul.getValueType(),
    792                                Mul, DAG.getValueType(SmallVT));
    793     Overflow = DAG.getSetCC(DL, N->getValueType(1), SExt, Mul, ISD::SETNE);
    794   }
    795 
    796   // The only other way for overflow to occur is if the multiplication in the
    797   // larger type itself overflowed.
    798   Overflow = DAG.getNode(ISD::OR, DL, N->getValueType(1), Overflow,
    799                          SDValue(Mul.getNode(), 1));
    800 
    801   // Use the calculated overflow everywhere.
    802   ReplaceValueWith(SDValue(N, 1), Overflow);
    803   return Mul;
    804 }
    805 
    806 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
    807   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
    808                                                N->getValueType(0)));
    809 }
    810 
    811 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
    812   SDValue Chain = N->getOperand(0); // Get the chain.
    813   SDValue Ptr = N->getOperand(1); // Get the pointer.
    814   EVT VT = N->getValueType(0);
    815   SDLoc dl(N);
    816 
    817   MVT RegVT = TLI.getRegisterType(*DAG.getContext(), VT);
    818   unsigned NumRegs = TLI.getNumRegisters(*DAG.getContext(), VT);
    819   // The argument is passed as NumRegs registers of type RegVT.
    820 
    821   SmallVector<SDValue, 8> Parts(NumRegs);
    822   for (unsigned i = 0; i < NumRegs; ++i) {
    823     Parts[i] = DAG.getVAArg(RegVT, dl, Chain, Ptr, N->getOperand(2),
    824                             N->getConstantOperandVal(3));
    825     Chain = Parts[i].getValue(1);
    826   }
    827 
    828   // Handle endianness of the load.
    829   if (DAG.getDataLayout().isBigEndian())
    830     std::reverse(Parts.begin(), Parts.end());
    831 
    832   // Assemble the parts in the promoted type.
    833   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
    834   SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
    835   for (unsigned i = 1; i < NumRegs; ++i) {
    836     SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
    837     // Shift it to the right position and "or" it in.
    838     Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
    839                        DAG.getConstant(i * RegVT.getSizeInBits(), dl,
    840                                        TLI.getPointerTy(DAG.getDataLayout())));
    841     Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
    842   }
    843 
    844   // Modified the chain result - switch anything that used the old chain to
    845   // use the new one.
    846   ReplaceValueWith(SDValue(N, 1), Chain);
    847 
    848   return Res;
    849 }
    850 
    851 //===----------------------------------------------------------------------===//
    852 //  Integer Operand Promotion
    853 //===----------------------------------------------------------------------===//
    854 
    855 /// PromoteIntegerOperand - This method is called when the specified operand of
    856 /// the specified node is found to need promotion.  At this point, all of the
    857 /// result types of the node are known to be legal, but other operands of the
    858 /// node may need promotion or expansion as well as the specified one.
    859 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
    860   DEBUG(dbgs() << "Promote integer operand: "; N->dump(&DAG); dbgs() << "\n");
    861   SDValue Res = SDValue();
    862 
    863   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
    864     return false;
    865 
    866   switch (N->getOpcode()) {
    867     default:
    868   #ifndef NDEBUG
    869     dbgs() << "PromoteIntegerOperand Op #" << OpNo << ": ";
    870     N->dump(&DAG); dbgs() << "\n";
    871   #endif
    872     llvm_unreachable("Do not know how to promote this operator's operand!");
    873 
    874   case ISD::ANY_EXTEND:   Res = PromoteIntOp_ANY_EXTEND(N); break;
    875   case ISD::ATOMIC_STORE:
    876     Res = PromoteIntOp_ATOMIC_STORE(cast<AtomicSDNode>(N));
    877     break;
    878   case ISD::BITCAST:      Res = PromoteIntOp_BITCAST(N); break;
    879   case ISD::BR_CC:        Res = PromoteIntOp_BR_CC(N, OpNo); break;
    880   case ISD::BRCOND:       Res = PromoteIntOp_BRCOND(N, OpNo); break;
    881   case ISD::BUILD_PAIR:   Res = PromoteIntOp_BUILD_PAIR(N); break;
    882   case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
    883   case ISD::CONCAT_VECTORS: Res = PromoteIntOp_CONCAT_VECTORS(N); break;
    884   case ISD::EXTRACT_VECTOR_ELT: Res = PromoteIntOp_EXTRACT_VECTOR_ELT(N); break;
    885   case ISD::CONVERT_RNDSAT:
    886                           Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
    887   case ISD::INSERT_VECTOR_ELT:
    888                           Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
    889   case ISD::SCALAR_TO_VECTOR:
    890                           Res = PromoteIntOp_SCALAR_TO_VECTOR(N); break;
    891   case ISD::VSELECT:
    892   case ISD::SELECT:       Res = PromoteIntOp_SELECT(N, OpNo); break;
    893   case ISD::SELECT_CC:    Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
    894   case ISD::SETCC:        Res = PromoteIntOp_SETCC(N, OpNo); break;
    895   case ISD::SIGN_EXTEND:  Res = PromoteIntOp_SIGN_EXTEND(N); break;
    896   case ISD::SINT_TO_FP:   Res = PromoteIntOp_SINT_TO_FP(N); break;
    897   case ISD::STORE:        Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
    898                                                    OpNo); break;
    899   case ISD::MSTORE:       Res = PromoteIntOp_MSTORE(cast<MaskedStoreSDNode>(N),
    900                                                     OpNo); break;
    901   case ISD::MLOAD:        Res = PromoteIntOp_MLOAD(cast<MaskedLoadSDNode>(N),
    902                                                     OpNo); break;
    903   case ISD::MGATHER:  Res = PromoteIntOp_MGATHER(cast<MaskedGatherSDNode>(N),
    904                                                  OpNo); break;
    905   case ISD::MSCATTER: Res = PromoteIntOp_MSCATTER(cast<MaskedScatterSDNode>(N),
    906                                                   OpNo); break;
    907   case ISD::TRUNCATE:     Res = PromoteIntOp_TRUNCATE(N); break;
    908   case ISD::FP16_TO_FP:
    909   case ISD::UINT_TO_FP:   Res = PromoteIntOp_UINT_TO_FP(N); break;
    910   case ISD::ZERO_EXTEND:  Res = PromoteIntOp_ZERO_EXTEND(N); break;
    911   case ISD::EXTRACT_SUBVECTOR: Res = PromoteIntOp_EXTRACT_SUBVECTOR(N); break;
    912 
    913   case ISD::SHL:
    914   case ISD::SRA:
    915   case ISD::SRL:
    916   case ISD::ROTL:
    917   case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
    918   }
    919 
    920   // If the result is null, the sub-method took care of registering results etc.
    921   if (!Res.getNode()) return false;
    922 
    923   // If the result is N, the sub-method updated N in place.  Tell the legalizer
    924   // core about this.
    925   if (Res.getNode() == N)
    926     return true;
    927 
    928   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
    929          "Invalid operand expansion");
    930 
    931   ReplaceValueWith(SDValue(N, 0), Res);
    932   return false;
    933 }
    934 
    935 /// PromoteSetCCOperands - Promote the operands of a comparison.  This code is
    936 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
    937 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
    938                                             ISD::CondCode CCCode) {
    939   // We have to insert explicit sign or zero extends.  Note that we could
    940   // insert sign extends for ALL conditions, but zero extend is cheaper on
    941   // many machines (an AND instead of two shifts), so prefer it.
    942   switch (CCCode) {
    943   default: llvm_unreachable("Unknown integer comparison!");
    944   case ISD::SETEQ:
    945   case ISD::SETNE: {
    946     SDValue OpL = GetPromotedInteger(NewLHS);
    947     SDValue OpR = GetPromotedInteger(NewRHS);
    948 
    949     // We would prefer to promote the comparison operand with sign extension,
    950     // if we find the operand is actually to truncate an AssertSext. With this
    951     // optimization, we can avoid inserting real truncate instruction, which
    952     // is redudant eventually.
    953     if (OpL->getOpcode() == ISD::AssertSext &&
    954         cast<VTSDNode>(OpL->getOperand(1))->getVT() == NewLHS.getValueType() &&
    955         OpR->getOpcode() == ISD::AssertSext &&
    956         cast<VTSDNode>(OpR->getOperand(1))->getVT() == NewRHS.getValueType()) {
    957       NewLHS = OpL;
    958       NewRHS = OpR;
    959     } else {
    960       NewLHS = ZExtPromotedInteger(NewLHS);
    961       NewRHS = ZExtPromotedInteger(NewRHS);
    962     }
    963     break;
    964   }
    965   case ISD::SETUGE:
    966   case ISD::SETUGT:
    967   case ISD::SETULE:
    968   case ISD::SETULT:
    969     // ALL of these operations will work if we either sign or zero extend
    970     // the operands (including the unsigned comparisons!).  Zero extend is
    971     // usually a simpler/cheaper operation, so prefer it.
    972     NewLHS = ZExtPromotedInteger(NewLHS);
    973     NewRHS = ZExtPromotedInteger(NewRHS);
    974     break;
    975   case ISD::SETGE:
    976   case ISD::SETGT:
    977   case ISD::SETLT:
    978   case ISD::SETLE:
    979     NewLHS = SExtPromotedInteger(NewLHS);
    980     NewRHS = SExtPromotedInteger(NewRHS);
    981     break;
    982   }
    983 }
    984 
    985 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
    986   SDValue Op = GetPromotedInteger(N->getOperand(0));
    987   return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), N->getValueType(0), Op);
    988 }
    989 
    990 SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
    991   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
    992   return DAG.getAtomic(N->getOpcode(), SDLoc(N), N->getMemoryVT(),
    993                        N->getChain(), N->getBasePtr(), Op2, N->getMemOperand(),
    994                        N->getOrdering(), N->getSynchScope());
    995 }
    996 
    997 SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
    998   // This should only occur in unusual situations like bitcasting to an
    999   // x86_fp80, so just turn it into a store+load
   1000   return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
   1001 }
   1002 
   1003 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
   1004   assert(OpNo == 2 && "Don't know how to promote this operand!");
   1005 
   1006   SDValue LHS = N->getOperand(2);
   1007   SDValue RHS = N->getOperand(3);
   1008   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
   1009 
   1010   // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
   1011   // legal types.
   1012   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
   1013                                 N->getOperand(1), LHS, RHS, N->getOperand(4)),
   1014                  0);
   1015 }
   1016 
   1017 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
   1018   assert(OpNo == 1 && "only know how to promote condition");
   1019 
   1020   // Promote all the way up to the canonical SetCC type.
   1021   SDValue Cond = PromoteTargetBoolean(N->getOperand(1), MVT::Other);
   1022 
   1023   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
   1024   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Cond,
   1025                                         N->getOperand(2)), 0);
   1026 }
   1027 
   1028 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
   1029   // Since the result type is legal, the operands must promote to it.
   1030   EVT OVT = N->getOperand(0).getValueType();
   1031   SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
   1032   SDValue Hi = GetPromotedInteger(N->getOperand(1));
   1033   assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
   1034   SDLoc dl(N);
   1035 
   1036   Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
   1037                    DAG.getConstant(OVT.getSizeInBits(), dl,
   1038                                    TLI.getPointerTy(DAG.getDataLayout())));
   1039   return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
   1040 }
   1041 
   1042 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
   1043   // The vector type is legal but the element type is not.  This implies
   1044   // that the vector is a power-of-two in length and that the element
   1045   // type does not have a strange size (eg: it is not i1).
   1046   EVT VecVT = N->getValueType(0);
   1047   unsigned NumElts = VecVT.getVectorNumElements();
   1048   assert(!((NumElts & 1) && (!TLI.isTypeLegal(VecVT))) &&
   1049          "Legal vector of one illegal element?");
   1050 
   1051   // Promote the inserted value.  The type does not need to match the
   1052   // vector element type.  Check that any extra bits introduced will be
   1053   // truncated away.
   1054   assert(N->getOperand(0).getValueType().getSizeInBits() >=
   1055          N->getValueType(0).getVectorElementType().getSizeInBits() &&
   1056          "Type of inserted value narrower than vector element type!");
   1057 
   1058   SmallVector<SDValue, 16> NewOps;
   1059   for (unsigned i = 0; i < NumElts; ++i)
   1060     NewOps.push_back(GetPromotedInteger(N->getOperand(i)));
   1061 
   1062   return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
   1063 }
   1064 
   1065 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
   1066   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
   1067   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
   1068            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
   1069            CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
   1070            "can only promote integer arguments");
   1071   SDValue InOp = GetPromotedInteger(N->getOperand(0));
   1072   return DAG.getConvertRndSat(N->getValueType(0), SDLoc(N), InOp,
   1073                               N->getOperand(1), N->getOperand(2),
   1074                               N->getOperand(3), N->getOperand(4), CvtCode);
   1075 }
   1076 
   1077 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
   1078                                                          unsigned OpNo) {
   1079   if (OpNo == 1) {
   1080     // Promote the inserted value.  This is valid because the type does not
   1081     // have to match the vector element type.
   1082 
   1083     // Check that any extra bits introduced will be truncated away.
   1084     assert(N->getOperand(1).getValueType().getSizeInBits() >=
   1085            N->getValueType(0).getVectorElementType().getSizeInBits() &&
   1086            "Type of inserted value narrower than vector element type!");
   1087     return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
   1088                                   GetPromotedInteger(N->getOperand(1)),
   1089                                   N->getOperand(2)),
   1090                    0);
   1091   }
   1092 
   1093   assert(OpNo == 2 && "Different operand and result vector types?");
   1094 
   1095   // Promote the index.
   1096   SDValue Idx = DAG.getZExtOrTrunc(N->getOperand(2), SDLoc(N),
   1097                                    TLI.getVectorIdxTy(DAG.getDataLayout()));
   1098   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
   1099                                 N->getOperand(1), Idx), 0);
   1100 }
   1101 
   1102 SDValue DAGTypeLegalizer::PromoteIntOp_SCALAR_TO_VECTOR(SDNode *N) {
   1103   // Integer SCALAR_TO_VECTOR operands are implicitly truncated, so just promote
   1104   // the operand in place.
   1105   return SDValue(DAG.UpdateNodeOperands(N,
   1106                                 GetPromotedInteger(N->getOperand(0))), 0);
   1107 }
   1108 
   1109 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
   1110   assert(OpNo == 0 && "Only know how to promote the condition!");
   1111   SDValue Cond = N->getOperand(0);
   1112   EVT OpTy = N->getOperand(1).getValueType();
   1113 
   1114   // Promote all the way up to the canonical SetCC type.
   1115   EVT OpVT = N->getOpcode() == ISD::SELECT ? OpTy.getScalarType() : OpTy;
   1116   Cond = PromoteTargetBoolean(Cond, OpVT);
   1117 
   1118   return SDValue(DAG.UpdateNodeOperands(N, Cond, N->getOperand(1),
   1119                                         N->getOperand(2)), 0);
   1120 }
   1121 
   1122 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
   1123   assert(OpNo == 0 && "Don't know how to promote this operand!");
   1124 
   1125   SDValue LHS = N->getOperand(0);
   1126   SDValue RHS = N->getOperand(1);
   1127   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
   1128 
   1129   // The CC (#4) and the possible return values (#2 and #3) have legal types.
   1130   return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2),
   1131                                 N->getOperand(3), N->getOperand(4)), 0);
   1132 }
   1133 
   1134 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
   1135   assert(OpNo == 0 && "Don't know how to promote this operand!");
   1136 
   1137   SDValue LHS = N->getOperand(0);
   1138   SDValue RHS = N->getOperand(1);
   1139   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
   1140 
   1141   // The CC (#2) is always legal.
   1142   return SDValue(DAG.UpdateNodeOperands(N, LHS, RHS, N->getOperand(2)), 0);
   1143 }
   1144 
   1145 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
   1146   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
   1147                                 ZExtPromotedInteger(N->getOperand(1))), 0);
   1148 }
   1149 
   1150 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
   1151   SDValue Op = GetPromotedInteger(N->getOperand(0));
   1152   SDLoc dl(N);
   1153   Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
   1154   return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
   1155                      Op, DAG.getValueType(N->getOperand(0).getValueType()));
   1156 }
   1157 
   1158 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
   1159   return SDValue(DAG.UpdateNodeOperands(N,
   1160                                 SExtPromotedInteger(N->getOperand(0))), 0);
   1161 }
   1162 
   1163 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
   1164   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
   1165   SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
   1166   SDLoc dl(N);
   1167 
   1168   SDValue Val = GetPromotedInteger(N->getValue());  // Get promoted value.
   1169 
   1170   // Truncate the value and store the result.
   1171   return DAG.getTruncStore(Ch, dl, Val, Ptr,
   1172                            N->getMemoryVT(), N->getMemOperand());
   1173 }
   1174 
   1175 SDValue DAGTypeLegalizer::PromoteIntOp_MSTORE(MaskedStoreSDNode *N,
   1176                                               unsigned OpNo) {
   1177 
   1178   SDValue DataOp = N->getValue();
   1179   EVT DataVT = DataOp.getValueType();
   1180   SDValue Mask = N->getMask();
   1181   SDLoc dl(N);
   1182 
   1183   bool TruncateStore = false;
   1184   if (OpNo == 2) {
   1185     // Mask comes before the data operand. If the data operand is legal, we just
   1186     // promote the mask.
   1187     // When the data operand has illegal type, we should legalize the data
   1188     // operand first. The mask will be promoted/splitted/widened according to
   1189     // the data operand type.
   1190     if (TLI.isTypeLegal(DataVT))
   1191       Mask = PromoteTargetBoolean(Mask, DataVT);
   1192     else {
   1193       if (getTypeAction(DataVT) == TargetLowering::TypePromoteInteger)
   1194         return PromoteIntOp_MSTORE(N, 3);
   1195 
   1196       else if (getTypeAction(DataVT) == TargetLowering::TypeWidenVector)
   1197         return WidenVecOp_MSTORE(N, 3);
   1198 
   1199       else {
   1200         assert (getTypeAction(DataVT) == TargetLowering::TypeSplitVector);
   1201         return SplitVecOp_MSTORE(N, 3);
   1202       }
   1203     }
   1204   } else { // Data operand
   1205     assert(OpNo == 3 && "Unexpected operand for promotion");
   1206     DataOp = GetPromotedInteger(DataOp);
   1207     Mask = PromoteTargetBoolean(Mask, DataOp.getValueType());
   1208     TruncateStore = true;
   1209   }
   1210 
   1211   return DAG.getMaskedStore(N->getChain(), dl, DataOp, N->getBasePtr(), Mask,
   1212                             N->getMemoryVT(), N->getMemOperand(),
   1213                             TruncateStore);
   1214 }
   1215 
   1216 SDValue DAGTypeLegalizer::PromoteIntOp_MLOAD(MaskedLoadSDNode *N,
   1217                                              unsigned OpNo) {
   1218   assert(OpNo == 2 && "Only know how to promote the mask!");
   1219   EVT DataVT = N->getValueType(0);
   1220   SDValue Mask = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
   1221   SmallVector<SDValue, 4> NewOps(N->op_begin(), N->op_end());
   1222   NewOps[OpNo] = Mask;
   1223   return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
   1224 }
   1225 
   1226 SDValue DAGTypeLegalizer::PromoteIntOp_MGATHER(MaskedGatherSDNode *N,
   1227                                                unsigned OpNo) {
   1228 
   1229   SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
   1230   if (OpNo == 2) {
   1231     // The Mask
   1232     EVT DataVT = N->getValueType(0);
   1233     NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
   1234   } else
   1235     NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
   1236   return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
   1237 }
   1238 
   1239 SDValue DAGTypeLegalizer::PromoteIntOp_MSCATTER(MaskedScatterSDNode *N,
   1240                                                 unsigned OpNo) {
   1241   SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
   1242   if (OpNo == 2) {
   1243     // The Mask
   1244     EVT DataVT = N->getValue().getValueType();
   1245     NewOps[OpNo] = PromoteTargetBoolean(N->getOperand(OpNo), DataVT);
   1246   } else
   1247     NewOps[OpNo] = GetPromotedInteger(N->getOperand(OpNo));
   1248   return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
   1249 }
   1250 
   1251 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
   1252   SDValue Op = GetPromotedInteger(N->getOperand(0));
   1253   return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), Op);
   1254 }
   1255 
   1256 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
   1257   return SDValue(DAG.UpdateNodeOperands(N,
   1258                                 ZExtPromotedInteger(N->getOperand(0))), 0);
   1259 }
   1260 
   1261 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
   1262   SDLoc dl(N);
   1263   SDValue Op = GetPromotedInteger(N->getOperand(0));
   1264   Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
   1265   return DAG.getZeroExtendInReg(Op, dl,
   1266                                 N->getOperand(0).getValueType().getScalarType());
   1267 }
   1268 
   1269 
   1270 //===----------------------------------------------------------------------===//
   1271 //  Integer Result Expansion
   1272 //===----------------------------------------------------------------------===//
   1273 
   1274 /// ExpandIntegerResult - This method is called when the specified result of the
   1275 /// specified node is found to need expansion.  At this point, the node may also
   1276 /// have invalid operands or may have other results that need promotion, we just
   1277 /// know that (at least) one result needs expansion.
   1278 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
   1279   DEBUG(dbgs() << "Expand integer result: "; N->dump(&DAG); dbgs() << "\n");
   1280   SDValue Lo, Hi;
   1281   Lo = Hi = SDValue();
   1282 
   1283   // See if the target wants to custom expand this node.
   1284   if (CustomLowerNode(N, N->getValueType(ResNo), true))
   1285     return;
   1286 
   1287   switch (N->getOpcode()) {
   1288   default:
   1289 #ifndef NDEBUG
   1290     dbgs() << "ExpandIntegerResult #" << ResNo << ": ";
   1291     N->dump(&DAG); dbgs() << "\n";
   1292 #endif
   1293     llvm_unreachable("Do not know how to expand the result of this operator!");
   1294 
   1295   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
   1296   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
   1297   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
   1298   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
   1299 
   1300   case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
   1301   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
   1302   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
   1303   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
   1304   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
   1305 
   1306   case ISD::ANY_EXTEND:  ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
   1307   case ISD::AssertSext:  ExpandIntRes_AssertSext(N, Lo, Hi); break;
   1308   case ISD::AssertZext:  ExpandIntRes_AssertZext(N, Lo, Hi); break;
   1309   case ISD::BITREVERSE:  ExpandIntRes_BITREVERSE(N, Lo, Hi); break;
   1310   case ISD::BSWAP:       ExpandIntRes_BSWAP(N, Lo, Hi); break;
   1311   case ISD::Constant:    ExpandIntRes_Constant(N, Lo, Hi); break;
   1312   case ISD::CTLZ_ZERO_UNDEF:
   1313   case ISD::CTLZ:        ExpandIntRes_CTLZ(N, Lo, Hi); break;
   1314   case ISD::CTPOP:       ExpandIntRes_CTPOP(N, Lo, Hi); break;
   1315   case ISD::CTTZ_ZERO_UNDEF:
   1316   case ISD::CTTZ:        ExpandIntRes_CTTZ(N, Lo, Hi); break;
   1317   case ISD::FP_TO_SINT:  ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
   1318   case ISD::FP_TO_UINT:  ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
   1319   case ISD::LOAD:        ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
   1320   case ISD::MUL:         ExpandIntRes_MUL(N, Lo, Hi); break;
   1321   case ISD::READCYCLECOUNTER: ExpandIntRes_READCYCLECOUNTER(N, Lo, Hi); break;
   1322   case ISD::SDIV:        ExpandIntRes_SDIV(N, Lo, Hi); break;
   1323   case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
   1324   case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
   1325   case ISD::SREM:        ExpandIntRes_SREM(N, Lo, Hi); break;
   1326   case ISD::TRUNCATE:    ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
   1327   case ISD::UDIV:        ExpandIntRes_UDIV(N, Lo, Hi); break;
   1328   case ISD::UREM:        ExpandIntRes_UREM(N, Lo, Hi); break;
   1329   case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
   1330   case ISD::ATOMIC_LOAD: ExpandIntRes_ATOMIC_LOAD(N, Lo, Hi); break;
   1331 
   1332   case ISD::ATOMIC_LOAD_ADD:
   1333   case ISD::ATOMIC_LOAD_SUB:
   1334   case ISD::ATOMIC_LOAD_AND:
   1335   case ISD::ATOMIC_LOAD_OR:
   1336   case ISD::ATOMIC_LOAD_XOR:
   1337   case ISD::ATOMIC_LOAD_NAND:
   1338   case ISD::ATOMIC_LOAD_MIN:
   1339   case ISD::ATOMIC_LOAD_MAX:
   1340   case ISD::ATOMIC_LOAD_UMIN:
   1341   case ISD::ATOMIC_LOAD_UMAX:
   1342   case ISD::ATOMIC_SWAP:
   1343   case ISD::ATOMIC_CMP_SWAP: {
   1344     std::pair<SDValue, SDValue> Tmp = ExpandAtomic(N);
   1345     SplitInteger(Tmp.first, Lo, Hi);
   1346     ReplaceValueWith(SDValue(N, 1), Tmp.second);
   1347     break;
   1348   }
   1349   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
   1350     AtomicSDNode *AN = cast<AtomicSDNode>(N);
   1351     SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::Other);
   1352     SDValue Tmp = DAG.getAtomicCmpSwap(
   1353         ISD::ATOMIC_CMP_SWAP, SDLoc(N), AN->getMemoryVT(), VTs,
   1354         N->getOperand(0), N->getOperand(1), N->getOperand(2), N->getOperand(3),
   1355         AN->getMemOperand(), AN->getSuccessOrdering(), AN->getFailureOrdering(),
   1356         AN->getSynchScope());
   1357 
   1358     // Expanding to the strong ATOMIC_CMP_SWAP node means we can determine
   1359     // success simply by comparing the loaded value against the ingoing
   1360     // comparison.
   1361     SDValue Success = DAG.getSetCC(SDLoc(N), N->getValueType(1), Tmp,
   1362                                    N->getOperand(2), ISD::SETEQ);
   1363 
   1364     SplitInteger(Tmp, Lo, Hi);
   1365     ReplaceValueWith(SDValue(N, 1), Success);
   1366     ReplaceValueWith(SDValue(N, 2), Tmp.getValue(1));
   1367     break;
   1368   }
   1369 
   1370   case ISD::AND:
   1371   case ISD::OR:
   1372   case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
   1373 
   1374   case ISD::ADD:
   1375   case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
   1376 
   1377   case ISD::ADDC:
   1378   case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
   1379 
   1380   case ISD::ADDE:
   1381   case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
   1382 
   1383   case ISD::SHL:
   1384   case ISD::SRA:
   1385   case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
   1386 
   1387   case ISD::SADDO:
   1388   case ISD::SSUBO: ExpandIntRes_SADDSUBO(N, Lo, Hi); break;
   1389   case ISD::UADDO:
   1390   case ISD::USUBO: ExpandIntRes_UADDSUBO(N, Lo, Hi); break;
   1391   case ISD::UMULO:
   1392   case ISD::SMULO: ExpandIntRes_XMULO(N, Lo, Hi); break;
   1393   }
   1394 
   1395   // If Lo/Hi is null, the sub-method took care of registering results etc.
   1396   if (Lo.getNode())
   1397     SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
   1398 }
   1399 
   1400 /// Lower an atomic node to the appropriate builtin call.
   1401 std::pair <SDValue, SDValue> DAGTypeLegalizer::ExpandAtomic(SDNode *Node) {
   1402   unsigned Opc = Node->getOpcode();
   1403   MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
   1404   RTLIB::Libcall LC = RTLIB::getATOMIC(Opc, VT);
   1405   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected atomic op or value type!");
   1406 
   1407   return ExpandChainLibCall(LC, Node, false);
   1408 }
   1409 
   1410 /// N is a shift by a value that needs to be expanded,
   1411 /// and the shift amount is a constant 'Amt'.  Expand the operation.
   1412 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, const APInt &Amt,
   1413                                              SDValue &Lo, SDValue &Hi) {
   1414   SDLoc DL(N);
   1415   // Expand the incoming operand to be shifted, so that we have its parts
   1416   SDValue InL, InH;
   1417   GetExpandedInteger(N->getOperand(0), InL, InH);
   1418 
   1419   // Though Amt shouldn't usually be 0, it's possible. E.g. when legalization
   1420   // splitted a vector shift, like this: <op1, op2> SHL <0, 2>.
   1421   if (!Amt) {
   1422     Lo = InL;
   1423     Hi = InH;
   1424     return;
   1425   }
   1426 
   1427   EVT NVT = InL.getValueType();
   1428   unsigned VTBits = N->getValueType(0).getSizeInBits();
   1429   unsigned NVTBits = NVT.getSizeInBits();
   1430   EVT ShTy = N->getOperand(1).getValueType();
   1431 
   1432   if (N->getOpcode() == ISD::SHL) {
   1433     if (Amt.ugt(VTBits)) {
   1434       Lo = Hi = DAG.getConstant(0, DL, NVT);
   1435     } else if (Amt.ugt(NVTBits)) {
   1436       Lo = DAG.getConstant(0, DL, NVT);
   1437       Hi = DAG.getNode(ISD::SHL, DL,
   1438                        NVT, InL, DAG.getConstant(Amt - NVTBits, DL, ShTy));
   1439     } else if (Amt == NVTBits) {
   1440       Lo = DAG.getConstant(0, DL, NVT);
   1441       Hi = InL;
   1442     } else if (Amt == 1 &&
   1443                TLI.isOperationLegalOrCustom(ISD::ADDC,
   1444                               TLI.getTypeToExpandTo(*DAG.getContext(), NVT))) {
   1445       // Emit this X << 1 as X+X.
   1446       SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
   1447       SDValue LoOps[2] = { InL, InL };
   1448       Lo = DAG.getNode(ISD::ADDC, DL, VTList, LoOps);
   1449       SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
   1450       Hi = DAG.getNode(ISD::ADDE, DL, VTList, HiOps);
   1451     } else {
   1452       Lo = DAG.getNode(ISD::SHL, DL, NVT, InL, DAG.getConstant(Amt, DL, ShTy));
   1453       Hi = DAG.getNode(ISD::OR, DL, NVT,
   1454                        DAG.getNode(ISD::SHL, DL, NVT, InH,
   1455                                    DAG.getConstant(Amt, DL, ShTy)),
   1456                        DAG.getNode(ISD::SRL, DL, NVT, InL,
   1457                                    DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
   1458     }
   1459     return;
   1460   }
   1461 
   1462   if (N->getOpcode() == ISD::SRL) {
   1463     if (Amt.ugt(VTBits)) {
   1464       Lo = Hi = DAG.getConstant(0, DL, NVT);
   1465     } else if (Amt.ugt(NVTBits)) {
   1466       Lo = DAG.getNode(ISD::SRL, DL,
   1467                        NVT, InH, DAG.getConstant(Amt - NVTBits, DL, ShTy));
   1468       Hi = DAG.getConstant(0, DL, NVT);
   1469     } else if (Amt == NVTBits) {
   1470       Lo = InH;
   1471       Hi = DAG.getConstant(0, DL, NVT);
   1472     } else {
   1473       Lo = DAG.getNode(ISD::OR, DL, NVT,
   1474                        DAG.getNode(ISD::SRL, DL, NVT, InL,
   1475                                    DAG.getConstant(Amt, DL, ShTy)),
   1476                        DAG.getNode(ISD::SHL, DL, NVT, InH,
   1477                                    DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
   1478       Hi = DAG.getNode(ISD::SRL, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
   1479     }
   1480     return;
   1481   }
   1482 
   1483   assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
   1484   if (Amt.ugt(VTBits)) {
   1485     Hi = Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
   1486                           DAG.getConstant(NVTBits - 1, DL, ShTy));
   1487   } else if (Amt.ugt(NVTBits)) {
   1488     Lo = DAG.getNode(ISD::SRA, DL, NVT, InH,
   1489                      DAG.getConstant(Amt - NVTBits, DL, ShTy));
   1490     Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
   1491                      DAG.getConstant(NVTBits - 1, DL, ShTy));
   1492   } else if (Amt == NVTBits) {
   1493     Lo = InH;
   1494     Hi = DAG.getNode(ISD::SRA, DL, NVT, InH,
   1495                      DAG.getConstant(NVTBits - 1, DL, ShTy));
   1496   } else {
   1497     Lo = DAG.getNode(ISD::OR, DL, NVT,
   1498                      DAG.getNode(ISD::SRL, DL, NVT, InL,
   1499                                  DAG.getConstant(Amt, DL, ShTy)),
   1500                      DAG.getNode(ISD::SHL, DL, NVT, InH,
   1501                                  DAG.getConstant(-Amt + NVTBits, DL, ShTy)));
   1502     Hi = DAG.getNode(ISD::SRA, DL, NVT, InH, DAG.getConstant(Amt, DL, ShTy));
   1503   }
   1504 }
   1505 
   1506 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
   1507 /// this shift based on knowledge of the high bit of the shift amount.  If we
   1508 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
   1509 /// shift amount.
   1510 bool DAGTypeLegalizer::
   1511 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
   1512   SDValue Amt = N->getOperand(1);
   1513   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   1514   EVT ShTy = Amt.getValueType();
   1515   unsigned ShBits = ShTy.getScalarType().getSizeInBits();
   1516   unsigned NVTBits = NVT.getScalarType().getSizeInBits();
   1517   assert(isPowerOf2_32(NVTBits) &&
   1518          "Expanded integer type size not a power of two!");
   1519   SDLoc dl(N);
   1520 
   1521   APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
   1522   APInt KnownZero, KnownOne;
   1523   DAG.computeKnownBits(N->getOperand(1), KnownZero, KnownOne);
   1524 
   1525   // If we don't know anything about the high bits, exit.
   1526   if (((KnownZero|KnownOne) & HighBitMask) == 0)
   1527     return false;
   1528 
   1529   // Get the incoming operand to be shifted.
   1530   SDValue InL, InH;
   1531   GetExpandedInteger(N->getOperand(0), InL, InH);
   1532 
   1533   // If we know that any of the high bits of the shift amount are one, then we
   1534   // can do this as a couple of simple shifts.
   1535   if (KnownOne.intersects(HighBitMask)) {
   1536     // Mask out the high bit, which we know is set.
   1537     Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
   1538                       DAG.getConstant(~HighBitMask, dl, ShTy));
   1539 
   1540     switch (N->getOpcode()) {
   1541     default: llvm_unreachable("Unknown shift");
   1542     case ISD::SHL:
   1543       Lo = DAG.getConstant(0, dl, NVT);              // Low part is zero.
   1544       Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
   1545       return true;
   1546     case ISD::SRL:
   1547       Hi = DAG.getConstant(0, dl, NVT);              // Hi part is zero.
   1548       Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
   1549       return true;
   1550     case ISD::SRA:
   1551       Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,       // Sign extend high part.
   1552                        DAG.getConstant(NVTBits - 1, dl, ShTy));
   1553       Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
   1554       return true;
   1555     }
   1556   }
   1557 
   1558   // If we know that all of the high bits of the shift amount are zero, then we
   1559   // can do this as a couple of simple shifts.
   1560   if ((KnownZero & HighBitMask) == HighBitMask) {
   1561     // Calculate 31-x. 31 is used instead of 32 to avoid creating an undefined
   1562     // shift if x is zero.  We can use XOR here because x is known to be smaller
   1563     // than 32.
   1564     SDValue Amt2 = DAG.getNode(ISD::XOR, dl, ShTy, Amt,
   1565                                DAG.getConstant(NVTBits - 1, dl, ShTy));
   1566 
   1567     unsigned Op1, Op2;
   1568     switch (N->getOpcode()) {
   1569     default: llvm_unreachable("Unknown shift");
   1570     case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
   1571     case ISD::SRL:
   1572     case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
   1573     }
   1574 
   1575     // When shifting right the arithmetic for Lo and Hi is swapped.
   1576     if (N->getOpcode() != ISD::SHL)
   1577       std::swap(InL, InH);
   1578 
   1579     // Use a little trick to get the bits that move from Lo to Hi. First
   1580     // shift by one bit.
   1581     SDValue Sh1 = DAG.getNode(Op2, dl, NVT, InL, DAG.getConstant(1, dl, ShTy));
   1582     // Then compute the remaining shift with amount-1.
   1583     SDValue Sh2 = DAG.getNode(Op2, dl, NVT, Sh1, Amt2);
   1584 
   1585     Lo = DAG.getNode(N->getOpcode(), dl, NVT, InL, Amt);
   1586     Hi = DAG.getNode(ISD::OR, dl, NVT, DAG.getNode(Op1, dl, NVT, InH, Amt),Sh2);
   1587 
   1588     if (N->getOpcode() != ISD::SHL)
   1589       std::swap(Hi, Lo);
   1590     return true;
   1591   }
   1592 
   1593   return false;
   1594 }
   1595 
   1596 /// ExpandShiftWithUnknownAmountBit - Fully general expansion of integer shift
   1597 /// of any size.
   1598 bool DAGTypeLegalizer::
   1599 ExpandShiftWithUnknownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
   1600   SDValue Amt = N->getOperand(1);
   1601   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   1602   EVT ShTy = Amt.getValueType();
   1603   unsigned NVTBits = NVT.getSizeInBits();
   1604   assert(isPowerOf2_32(NVTBits) &&
   1605          "Expanded integer type size not a power of two!");
   1606   SDLoc dl(N);
   1607 
   1608   // Get the incoming operand to be shifted.
   1609   SDValue InL, InH;
   1610   GetExpandedInteger(N->getOperand(0), InL, InH);
   1611 
   1612   SDValue NVBitsNode = DAG.getConstant(NVTBits, dl, ShTy);
   1613   SDValue AmtExcess = DAG.getNode(ISD::SUB, dl, ShTy, Amt, NVBitsNode);
   1614   SDValue AmtLack = DAG.getNode(ISD::SUB, dl, ShTy, NVBitsNode, Amt);
   1615   SDValue isShort = DAG.getSetCC(dl, getSetCCResultType(ShTy),
   1616                                  Amt, NVBitsNode, ISD::SETULT);
   1617   SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(ShTy),
   1618                                 Amt, DAG.getConstant(0, dl, ShTy),
   1619                                 ISD::SETEQ);
   1620 
   1621   SDValue LoS, HiS, LoL, HiL;
   1622   switch (N->getOpcode()) {
   1623   default: llvm_unreachable("Unknown shift");
   1624   case ISD::SHL:
   1625     // Short: ShAmt < NVTBits
   1626     LoS = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt);
   1627     HiS = DAG.getNode(ISD::OR, dl, NVT,
   1628                       DAG.getNode(ISD::SHL, dl, NVT, InH, Amt),
   1629                       DAG.getNode(ISD::SRL, dl, NVT, InL, AmtLack));
   1630 
   1631     // Long: ShAmt >= NVTBits
   1632     LoL = DAG.getConstant(0, dl, NVT);                    // Lo part is zero.
   1633     HiL = DAG.getNode(ISD::SHL, dl, NVT, InL, AmtExcess); // Hi from Lo part.
   1634 
   1635     Lo = DAG.getSelect(dl, NVT, isShort, LoS, LoL);
   1636     Hi = DAG.getSelect(dl, NVT, isZero, InH,
   1637                        DAG.getSelect(dl, NVT, isShort, HiS, HiL));
   1638     return true;
   1639   case ISD::SRL:
   1640     // Short: ShAmt < NVTBits
   1641     HiS = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt);
   1642     LoS = DAG.getNode(ISD::OR, dl, NVT,
   1643                       DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
   1644     // FIXME: If Amt is zero, the following shift generates an undefined result
   1645     // on some architectures.
   1646                       DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
   1647 
   1648     // Long: ShAmt >= NVTBits
   1649     HiL = DAG.getConstant(0, dl, NVT);                    // Hi part is zero.
   1650     LoL = DAG.getNode(ISD::SRL, dl, NVT, InH, AmtExcess); // Lo from Hi part.
   1651 
   1652     Lo = DAG.getSelect(dl, NVT, isZero, InL,
   1653                        DAG.getSelect(dl, NVT, isShort, LoS, LoL));
   1654     Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
   1655     return true;
   1656   case ISD::SRA:
   1657     // Short: ShAmt < NVTBits
   1658     HiS = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt);
   1659     LoS = DAG.getNode(ISD::OR, dl, NVT,
   1660                       DAG.getNode(ISD::SRL, dl, NVT, InL, Amt),
   1661                       DAG.getNode(ISD::SHL, dl, NVT, InH, AmtLack));
   1662 
   1663     // Long: ShAmt >= NVTBits
   1664     HiL = DAG.getNode(ISD::SRA, dl, NVT, InH,             // Sign of Hi part.
   1665                       DAG.getConstant(NVTBits - 1, dl, ShTy));
   1666     LoL = DAG.getNode(ISD::SRA, dl, NVT, InH, AmtExcess); // Lo from Hi part.
   1667 
   1668     Lo = DAG.getSelect(dl, NVT, isZero, InL,
   1669                        DAG.getSelect(dl, NVT, isShort, LoS, LoL));
   1670     Hi = DAG.getSelect(dl, NVT, isShort, HiS, HiL);
   1671     return true;
   1672   }
   1673 }
   1674 
   1675 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
   1676                                            SDValue &Lo, SDValue &Hi) {
   1677   SDLoc dl(N);
   1678   // Expand the subcomponents.
   1679   SDValue LHSL, LHSH, RHSL, RHSH;
   1680   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
   1681   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
   1682 
   1683   EVT NVT = LHSL.getValueType();
   1684   SDValue LoOps[2] = { LHSL, RHSL };
   1685   SDValue HiOps[3] = { LHSH, RHSH };
   1686 
   1687   // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
   1688   // them.  TODO: Teach operation legalization how to expand unsupported
   1689   // ADDC/ADDE/SUBC/SUBE.  The problem is that these operations generate
   1690   // a carry of type MVT::Glue, but there doesn't seem to be any way to
   1691   // generate a value of this type in the expanded code sequence.
   1692   bool hasCarry =
   1693     TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
   1694                                    ISD::ADDC : ISD::SUBC,
   1695                                  TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
   1696 
   1697   if (hasCarry) {
   1698     SDVTList VTList = DAG.getVTList(NVT, MVT::Glue);
   1699     if (N->getOpcode() == ISD::ADD) {
   1700       Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
   1701       HiOps[2] = Lo.getValue(1);
   1702       Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
   1703     } else {
   1704       Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
   1705       HiOps[2] = Lo.getValue(1);
   1706       Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
   1707     }
   1708     return;
   1709   }
   1710 
   1711   bool hasOVF =
   1712     TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
   1713                                    ISD::UADDO : ISD::USUBO,
   1714                                  TLI.getTypeToExpandTo(*DAG.getContext(), NVT));
   1715   if (hasOVF) {
   1716     SDVTList VTList = DAG.getVTList(NVT, NVT);
   1717     TargetLoweringBase::BooleanContent BoolType = TLI.getBooleanContents(NVT);
   1718     int RevOpc;
   1719     if (N->getOpcode() == ISD::ADD) {
   1720       RevOpc = ISD::SUB;
   1721       Lo = DAG.getNode(ISD::UADDO, dl, VTList, LoOps);
   1722       Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
   1723     } else {
   1724       RevOpc = ISD::ADD;
   1725       Lo = DAG.getNode(ISD::USUBO, dl, VTList, LoOps);
   1726       Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
   1727     }
   1728     SDValue OVF = Lo.getValue(1);
   1729 
   1730     switch (BoolType) {
   1731     case TargetLoweringBase::UndefinedBooleanContent:
   1732       OVF = DAG.getNode(ISD::AND, dl, NVT, DAG.getConstant(1, dl, NVT), OVF);
   1733       // Fallthrough
   1734     case TargetLoweringBase::ZeroOrOneBooleanContent:
   1735       Hi = DAG.getNode(N->getOpcode(), dl, NVT, Hi, OVF);
   1736       break;
   1737     case TargetLoweringBase::ZeroOrNegativeOneBooleanContent:
   1738       Hi = DAG.getNode(RevOpc, dl, NVT, Hi, OVF);
   1739     }
   1740     return;
   1741   }
   1742 
   1743   if (N->getOpcode() == ISD::ADD) {
   1744     Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps);
   1745     Hi = DAG.getNode(ISD::ADD, dl, NVT, makeArrayRef(HiOps, 2));
   1746     SDValue Cmp1 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[0],
   1747                                 ISD::SETULT);
   1748     SDValue Carry1 = DAG.getSelect(dl, NVT, Cmp1,
   1749                                    DAG.getConstant(1, dl, NVT),
   1750                                    DAG.getConstant(0, dl, NVT));
   1751     SDValue Cmp2 = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo, LoOps[1],
   1752                                 ISD::SETULT);
   1753     SDValue Carry2 = DAG.getSelect(dl, NVT, Cmp2,
   1754                                    DAG.getConstant(1, dl, NVT), Carry1);
   1755     Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
   1756   } else {
   1757     Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps);
   1758     Hi = DAG.getNode(ISD::SUB, dl, NVT, makeArrayRef(HiOps, 2));
   1759     SDValue Cmp =
   1760       DAG.getSetCC(dl, getSetCCResultType(LoOps[0].getValueType()),
   1761                    LoOps[0], LoOps[1], ISD::SETULT);
   1762     SDValue Borrow = DAG.getSelect(dl, NVT, Cmp,
   1763                                    DAG.getConstant(1, dl, NVT),
   1764                                    DAG.getConstant(0, dl, NVT));
   1765     Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
   1766   }
   1767 }
   1768 
   1769 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
   1770                                             SDValue &Lo, SDValue &Hi) {
   1771   // Expand the subcomponents.
   1772   SDValue LHSL, LHSH, RHSL, RHSH;
   1773   SDLoc dl(N);
   1774   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
   1775   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
   1776   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
   1777   SDValue LoOps[2] = { LHSL, RHSL };
   1778   SDValue HiOps[3] = { LHSH, RHSH };
   1779 
   1780   if (N->getOpcode() == ISD::ADDC) {
   1781     Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps);
   1782     HiOps[2] = Lo.getValue(1);
   1783     Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps);
   1784   } else {
   1785     Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps);
   1786     HiOps[2] = Lo.getValue(1);
   1787     Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps);
   1788   }
   1789 
   1790   // Legalized the flag result - switch anything that used the old flag to
   1791   // use the new one.
   1792   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
   1793 }
   1794 
   1795 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
   1796                                             SDValue &Lo, SDValue &Hi) {
   1797   // Expand the subcomponents.
   1798   SDValue LHSL, LHSH, RHSL, RHSH;
   1799   SDLoc dl(N);
   1800   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
   1801   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
   1802   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Glue);
   1803   SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
   1804   SDValue HiOps[3] = { LHSH, RHSH };
   1805 
   1806   Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps);
   1807   HiOps[2] = Lo.getValue(1);
   1808   Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps);
   1809 
   1810   // Legalized the flag result - switch anything that used the old flag to
   1811   // use the new one.
   1812   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
   1813 }
   1814 
   1815 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
   1816                                                SDValue &Lo, SDValue &Hi) {
   1817   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   1818   SDLoc dl(N);
   1819   SDValue Op = N->getOperand(0);
   1820   if (Op.getValueType().bitsLE(NVT)) {
   1821     // The low part is any extension of the input (which degenerates to a copy).
   1822     Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
   1823     Hi = DAG.getUNDEF(NVT);   // The high part is undefined.
   1824   } else {
   1825     // For example, extension of an i48 to an i64.  The operand type necessarily
   1826     // promotes to the result type, so will end up being expanded too.
   1827     assert(getTypeAction(Op.getValueType()) ==
   1828            TargetLowering::TypePromoteInteger &&
   1829            "Only know how to promote this result!");
   1830     SDValue Res = GetPromotedInteger(Op);
   1831     assert(Res.getValueType() == N->getValueType(0) &&
   1832            "Operand over promoted?");
   1833     // Split the promoted operand.  This will simplify when it is expanded.
   1834     SplitInteger(Res, Lo, Hi);
   1835   }
   1836 }
   1837 
   1838 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
   1839                                                SDValue &Lo, SDValue &Hi) {
   1840   SDLoc dl(N);
   1841   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   1842   EVT NVT = Lo.getValueType();
   1843   EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
   1844   unsigned NVTBits = NVT.getSizeInBits();
   1845   unsigned EVTBits = EVT.getSizeInBits();
   1846 
   1847   if (NVTBits < EVTBits) {
   1848     Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
   1849                      DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
   1850                                                         EVTBits - NVTBits)));
   1851   } else {
   1852     Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
   1853     // The high part replicates the sign bit of Lo, make it explicit.
   1854     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
   1855                      DAG.getConstant(NVTBits - 1, dl,
   1856                                      TLI.getPointerTy(DAG.getDataLayout())));
   1857   }
   1858 }
   1859 
   1860 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
   1861                                                SDValue &Lo, SDValue &Hi) {
   1862   SDLoc dl(N);
   1863   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   1864   EVT NVT = Lo.getValueType();
   1865   EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
   1866   unsigned NVTBits = NVT.getSizeInBits();
   1867   unsigned EVTBits = EVT.getSizeInBits();
   1868 
   1869   if (NVTBits < EVTBits) {
   1870     Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
   1871                      DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
   1872                                                         EVTBits - NVTBits)));
   1873   } else {
   1874     Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
   1875     // The high part must be zero, make it explicit.
   1876     Hi = DAG.getConstant(0, dl, NVT);
   1877   }
   1878 }
   1879 
   1880 void DAGTypeLegalizer::ExpandIntRes_BITREVERSE(SDNode *N,
   1881                                                SDValue &Lo, SDValue &Hi) {
   1882   SDLoc dl(N);
   1883   GetExpandedInteger(N->getOperand(0), Hi, Lo);  // Note swapped operands.
   1884   Lo = DAG.getNode(ISD::BITREVERSE, dl, Lo.getValueType(), Lo);
   1885   Hi = DAG.getNode(ISD::BITREVERSE, dl, Hi.getValueType(), Hi);
   1886 }
   1887 
   1888 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
   1889                                           SDValue &Lo, SDValue &Hi) {
   1890   SDLoc dl(N);
   1891   GetExpandedInteger(N->getOperand(0), Hi, Lo);  // Note swapped operands.
   1892   Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
   1893   Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
   1894 }
   1895 
   1896 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
   1897                                              SDValue &Lo, SDValue &Hi) {
   1898   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   1899   unsigned NBitWidth = NVT.getSizeInBits();
   1900   auto Constant = cast<ConstantSDNode>(N);
   1901   const APInt &Cst = Constant->getAPIntValue();
   1902   bool IsTarget = Constant->isTargetOpcode();
   1903   bool IsOpaque = Constant->isOpaque();
   1904   SDLoc dl(N);
   1905   Lo = DAG.getConstant(Cst.trunc(NBitWidth), dl, NVT, IsTarget, IsOpaque);
   1906   Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), dl, NVT, IsTarget,
   1907                        IsOpaque);
   1908 }
   1909 
   1910 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
   1911                                          SDValue &Lo, SDValue &Hi) {
   1912   SDLoc dl(N);
   1913   // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
   1914   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   1915   EVT NVT = Lo.getValueType();
   1916 
   1917   SDValue HiNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Hi,
   1918                                    DAG.getConstant(0, dl, NVT), ISD::SETNE);
   1919 
   1920   SDValue LoLZ = DAG.getNode(N->getOpcode(), dl, NVT, Lo);
   1921   SDValue HiLZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, dl, NVT, Hi);
   1922 
   1923   Lo = DAG.getSelect(dl, NVT, HiNotZero, HiLZ,
   1924                      DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
   1925                                  DAG.getConstant(NVT.getSizeInBits(), dl,
   1926                                                  NVT)));
   1927   Hi = DAG.getConstant(0, dl, NVT);
   1928 }
   1929 
   1930 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
   1931                                           SDValue &Lo, SDValue &Hi) {
   1932   SDLoc dl(N);
   1933   // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
   1934   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   1935   EVT NVT = Lo.getValueType();
   1936   Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, dl, NVT, Lo),
   1937                    DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
   1938   Hi = DAG.getConstant(0, dl, NVT);
   1939 }
   1940 
   1941 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
   1942                                          SDValue &Lo, SDValue &Hi) {
   1943   SDLoc dl(N);
   1944   // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
   1945   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   1946   EVT NVT = Lo.getValueType();
   1947 
   1948   SDValue LoNotZero = DAG.getSetCC(dl, getSetCCResultType(NVT), Lo,
   1949                                    DAG.getConstant(0, dl, NVT), ISD::SETNE);
   1950 
   1951   SDValue LoLZ = DAG.getNode(ISD::CTTZ_ZERO_UNDEF, dl, NVT, Lo);
   1952   SDValue HiLZ = DAG.getNode(N->getOpcode(), dl, NVT, Hi);
   1953 
   1954   Lo = DAG.getSelect(dl, NVT, LoNotZero, LoLZ,
   1955                      DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
   1956                                  DAG.getConstant(NVT.getSizeInBits(), dl,
   1957                                                  NVT)));
   1958   Hi = DAG.getConstant(0, dl, NVT);
   1959 }
   1960 
   1961 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
   1962                                                SDValue &Hi) {
   1963   SDLoc dl(N);
   1964   EVT VT = N->getValueType(0);
   1965 
   1966   SDValue Op = N->getOperand(0);
   1967   if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
   1968     Op = GetPromotedFloat(Op);
   1969 
   1970   RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
   1971   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
   1972   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, true/*irrelevant*/, dl).first,
   1973                Lo, Hi);
   1974 }
   1975 
   1976 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
   1977                                                SDValue &Hi) {
   1978   SDLoc dl(N);
   1979   EVT VT = N->getValueType(0);
   1980 
   1981   SDValue Op = N->getOperand(0);
   1982   if (getTypeAction(Op.getValueType()) == TargetLowering::TypePromoteFloat)
   1983     Op = GetPromotedFloat(Op);
   1984 
   1985   RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
   1986   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
   1987   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Op, false/*irrelevant*/, dl).first,
   1988                Lo, Hi);
   1989 }
   1990 
   1991 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
   1992                                          SDValue &Lo, SDValue &Hi) {
   1993   if (ISD::isNormalLoad(N)) {
   1994     ExpandRes_NormalLoad(N, Lo, Hi);
   1995     return;
   1996   }
   1997 
   1998   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
   1999 
   2000   EVT VT = N->getValueType(0);
   2001   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
   2002   SDValue Ch  = N->getChain();
   2003   SDValue Ptr = N->getBasePtr();
   2004   ISD::LoadExtType ExtType = N->getExtensionType();
   2005   unsigned Alignment = N->getAlignment();
   2006   bool isVolatile = N->isVolatile();
   2007   bool isNonTemporal = N->isNonTemporal();
   2008   bool isInvariant = N->isInvariant();
   2009   AAMDNodes AAInfo = N->getAAInfo();
   2010   SDLoc dl(N);
   2011 
   2012   assert(NVT.isByteSized() && "Expanded type not byte sized!");
   2013 
   2014   if (N->getMemoryVT().bitsLE(NVT)) {
   2015     EVT MemVT = N->getMemoryVT();
   2016 
   2017     Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
   2018                         MemVT, isVolatile, isNonTemporal, isInvariant,
   2019                         Alignment, AAInfo);
   2020 
   2021     // Remember the chain.
   2022     Ch = Lo.getValue(1);
   2023 
   2024     if (ExtType == ISD::SEXTLOAD) {
   2025       // The high part is obtained by SRA'ing all but one of the bits of the
   2026       // lo part.
   2027       unsigned LoSize = Lo.getValueType().getSizeInBits();
   2028       Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
   2029                        DAG.getConstant(LoSize - 1, dl,
   2030                                        TLI.getPointerTy(DAG.getDataLayout())));
   2031     } else if (ExtType == ISD::ZEXTLOAD) {
   2032       // The high part is just a zero.
   2033       Hi = DAG.getConstant(0, dl, NVT);
   2034     } else {
   2035       assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
   2036       // The high part is undefined.
   2037       Hi = DAG.getUNDEF(NVT);
   2038     }
   2039   } else if (DAG.getDataLayout().isLittleEndian()) {
   2040     // Little-endian - low bits are at low addresses.
   2041     Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getPointerInfo(),
   2042                      isVolatile, isNonTemporal, isInvariant, Alignment,
   2043                      AAInfo);
   2044 
   2045     unsigned ExcessBits =
   2046       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
   2047     EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
   2048 
   2049     // Increment the pointer to the other half.
   2050     unsigned IncrementSize = NVT.getSizeInBits()/8;
   2051     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
   2052                       DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
   2053     Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr,
   2054                         N->getPointerInfo().getWithOffset(IncrementSize), NEVT,
   2055                         isVolatile, isNonTemporal, isInvariant,
   2056                         MinAlign(Alignment, IncrementSize), AAInfo);
   2057 
   2058     // Build a factor node to remember that this load is independent of the
   2059     // other one.
   2060     Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
   2061                      Hi.getValue(1));
   2062   } else {
   2063     // Big-endian - high bits are at low addresses.  Favor aligned loads at
   2064     // the cost of some bit-fiddling.
   2065     EVT MemVT = N->getMemoryVT();
   2066     unsigned EBytes = MemVT.getStoreSize();
   2067     unsigned IncrementSize = NVT.getSizeInBits()/8;
   2068     unsigned ExcessBits = (EBytes - IncrementSize)*8;
   2069 
   2070     // Load both the high bits and maybe some of the low bits.
   2071     Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getPointerInfo(),
   2072                         EVT::getIntegerVT(*DAG.getContext(),
   2073                                           MemVT.getSizeInBits() - ExcessBits),
   2074                         isVolatile, isNonTemporal, isInvariant, Alignment,
   2075                         AAInfo);
   2076 
   2077     // Increment the pointer to the other half.
   2078     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
   2079                       DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
   2080     // Load the rest of the low bits.
   2081     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr,
   2082                         N->getPointerInfo().getWithOffset(IncrementSize),
   2083                         EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
   2084                         isVolatile, isNonTemporal, isInvariant,
   2085                         MinAlign(Alignment, IncrementSize), AAInfo);
   2086 
   2087     // Build a factor node to remember that this load is independent of the
   2088     // other one.
   2089     Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
   2090                      Hi.getValue(1));
   2091 
   2092     if (ExcessBits < NVT.getSizeInBits()) {
   2093       // Transfer low bits from the bottom of Hi to the top of Lo.
   2094       Lo = DAG.getNode(
   2095           ISD::OR, dl, NVT, Lo,
   2096           DAG.getNode(ISD::SHL, dl, NVT, Hi,
   2097                       DAG.getConstant(ExcessBits, dl,
   2098                                       TLI.getPointerTy(DAG.getDataLayout()))));
   2099       // Move high bits to the right position in Hi.
   2100       Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, NVT,
   2101                        Hi,
   2102                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
   2103                                        TLI.getPointerTy(DAG.getDataLayout())));
   2104     }
   2105   }
   2106 
   2107   // Legalize the chain result - switch anything that used the old chain to
   2108   // use the new one.
   2109   ReplaceValueWith(SDValue(N, 1), Ch);
   2110 }
   2111 
   2112 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
   2113                                             SDValue &Lo, SDValue &Hi) {
   2114   SDLoc dl(N);
   2115   SDValue LL, LH, RL, RH;
   2116   GetExpandedInteger(N->getOperand(0), LL, LH);
   2117   GetExpandedInteger(N->getOperand(1), RL, RH);
   2118   Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
   2119   Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
   2120 }
   2121 
   2122 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
   2123                                         SDValue &Lo, SDValue &Hi) {
   2124   EVT VT = N->getValueType(0);
   2125   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
   2126   SDLoc dl(N);
   2127 
   2128   SDValue LL, LH, RL, RH;
   2129   GetExpandedInteger(N->getOperand(0), LL, LH);
   2130   GetExpandedInteger(N->getOperand(1), RL, RH);
   2131 
   2132   if (TLI.expandMUL(N, Lo, Hi, NVT, DAG, LL, LH, RL, RH))
   2133     return;
   2134 
   2135   // If nothing else, we can make a libcall.
   2136   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2137   if (VT == MVT::i16)
   2138     LC = RTLIB::MUL_I16;
   2139   else if (VT == MVT::i32)
   2140     LC = RTLIB::MUL_I32;
   2141   else if (VT == MVT::i64)
   2142     LC = RTLIB::MUL_I64;
   2143   else if (VT == MVT::i128)
   2144     LC = RTLIB::MUL_I128;
   2145   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
   2146 
   2147   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   2148   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, true/*irrelevant*/, dl).first,
   2149                Lo, Hi);
   2150 }
   2151 
   2152 void DAGTypeLegalizer::ExpandIntRes_READCYCLECOUNTER(SDNode *N, SDValue &Lo,
   2153                                                      SDValue &Hi) {
   2154   SDLoc DL(N);
   2155   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   2156   SDVTList VTs = DAG.getVTList(NVT, NVT, MVT::Other);
   2157   SDValue R = DAG.getNode(N->getOpcode(), DL, VTs, N->getOperand(0));
   2158   Lo = R.getValue(0);
   2159   Hi = R.getValue(1);
   2160   ReplaceValueWith(SDValue(N, 1), R.getValue(2));
   2161 }
   2162 
   2163 void DAGTypeLegalizer::ExpandIntRes_SADDSUBO(SDNode *Node,
   2164                                              SDValue &Lo, SDValue &Hi) {
   2165   SDValue LHS = Node->getOperand(0);
   2166   SDValue RHS = Node->getOperand(1);
   2167   SDLoc dl(Node);
   2168 
   2169   // Expand the result by simply replacing it with the equivalent
   2170   // non-overflow-checking operation.
   2171   SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
   2172                             ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
   2173                             LHS, RHS);
   2174   SplitInteger(Sum, Lo, Hi);
   2175 
   2176   // Compute the overflow.
   2177   //
   2178   //   LHSSign -> LHS >= 0
   2179   //   RHSSign -> RHS >= 0
   2180   //   SumSign -> Sum >= 0
   2181   //
   2182   //   Add:
   2183   //   Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
   2184   //   Sub:
   2185   //   Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
   2186   //
   2187   EVT OType = Node->getValueType(1);
   2188   SDValue Zero = DAG.getConstant(0, dl, LHS.getValueType());
   2189 
   2190   SDValue LHSSign = DAG.getSetCC(dl, OType, LHS, Zero, ISD::SETGE);
   2191   SDValue RHSSign = DAG.getSetCC(dl, OType, RHS, Zero, ISD::SETGE);
   2192   SDValue SignsMatch = DAG.getSetCC(dl, OType, LHSSign, RHSSign,
   2193                                     Node->getOpcode() == ISD::SADDO ?
   2194                                     ISD::SETEQ : ISD::SETNE);
   2195 
   2196   SDValue SumSign = DAG.getSetCC(dl, OType, Sum, Zero, ISD::SETGE);
   2197   SDValue SumSignNE = DAG.getSetCC(dl, OType, LHSSign, SumSign, ISD::SETNE);
   2198 
   2199   SDValue Cmp = DAG.getNode(ISD::AND, dl, OType, SignsMatch, SumSignNE);
   2200 
   2201   // Use the calculated overflow everywhere.
   2202   ReplaceValueWith(SDValue(Node, 1), Cmp);
   2203 }
   2204 
   2205 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
   2206                                          SDValue &Lo, SDValue &Hi) {
   2207   EVT VT = N->getValueType(0);
   2208   SDLoc dl(N);
   2209   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   2210 
   2211   if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
   2212     SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
   2213     SplitInteger(Res.getValue(0), Lo, Hi);
   2214     return;
   2215   }
   2216 
   2217   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2218   if (VT == MVT::i16)
   2219     LC = RTLIB::SDIV_I16;
   2220   else if (VT == MVT::i32)
   2221     LC = RTLIB::SDIV_I32;
   2222   else if (VT == MVT::i64)
   2223     LC = RTLIB::SDIV_I64;
   2224   else if (VT == MVT::i128)
   2225     LC = RTLIB::SDIV_I128;
   2226   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
   2227 
   2228   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, true, dl).first, Lo, Hi);
   2229 }
   2230 
   2231 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
   2232                                           SDValue &Lo, SDValue &Hi) {
   2233   EVT VT = N->getValueType(0);
   2234   SDLoc dl(N);
   2235 
   2236   // If we can emit an efficient shift operation, do so now.  Check to see if
   2237   // the RHS is a constant.
   2238   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
   2239     return ExpandShiftByConstant(N, CN->getAPIntValue(), Lo, Hi);
   2240 
   2241   // If we can determine that the high bit of the shift is zero or one, even if
   2242   // the low bits are variable, emit this shift in an optimized form.
   2243   if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
   2244     return;
   2245 
   2246   // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
   2247   unsigned PartsOpc;
   2248   if (N->getOpcode() == ISD::SHL) {
   2249     PartsOpc = ISD::SHL_PARTS;
   2250   } else if (N->getOpcode() == ISD::SRL) {
   2251     PartsOpc = ISD::SRL_PARTS;
   2252   } else {
   2253     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
   2254     PartsOpc = ISD::SRA_PARTS;
   2255   }
   2256 
   2257   // Next check to see if the target supports this SHL_PARTS operation or if it
   2258   // will custom expand it.
   2259   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
   2260   TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
   2261   if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
   2262       Action == TargetLowering::Custom) {
   2263     // Expand the subcomponents.
   2264     SDValue LHSL, LHSH;
   2265     GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
   2266     EVT VT = LHSL.getValueType();
   2267 
   2268     // If the shift amount operand is coming from a vector legalization it may
   2269     // have an illegal type.  Fix that first by casting the operand, otherwise
   2270     // the new SHL_PARTS operation would need further legalization.
   2271     SDValue ShiftOp = N->getOperand(1);
   2272     EVT ShiftTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
   2273     assert(ShiftTy.getScalarType().getSizeInBits() >=
   2274            Log2_32_Ceil(VT.getScalarType().getSizeInBits()) &&
   2275            "ShiftAmountTy is too small to cover the range of this type!");
   2276     if (ShiftOp.getValueType() != ShiftTy)
   2277       ShiftOp = DAG.getZExtOrTrunc(ShiftOp, dl, ShiftTy);
   2278 
   2279     SDValue Ops[] = { LHSL, LHSH, ShiftOp };
   2280     Lo = DAG.getNode(PartsOpc, dl, DAG.getVTList(VT, VT), Ops);
   2281     Hi = Lo.getValue(1);
   2282     return;
   2283   }
   2284 
   2285   // Otherwise, emit a libcall.
   2286   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2287   bool isSigned;
   2288   if (N->getOpcode() == ISD::SHL) {
   2289     isSigned = false; /*sign irrelevant*/
   2290     if (VT == MVT::i16)
   2291       LC = RTLIB::SHL_I16;
   2292     else if (VT == MVT::i32)
   2293       LC = RTLIB::SHL_I32;
   2294     else if (VT == MVT::i64)
   2295       LC = RTLIB::SHL_I64;
   2296     else if (VT == MVT::i128)
   2297       LC = RTLIB::SHL_I128;
   2298   } else if (N->getOpcode() == ISD::SRL) {
   2299     isSigned = false;
   2300     if (VT == MVT::i16)
   2301       LC = RTLIB::SRL_I16;
   2302     else if (VT == MVT::i32)
   2303       LC = RTLIB::SRL_I32;
   2304     else if (VT == MVT::i64)
   2305       LC = RTLIB::SRL_I64;
   2306     else if (VT == MVT::i128)
   2307       LC = RTLIB::SRL_I128;
   2308   } else {
   2309     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
   2310     isSigned = true;
   2311     if (VT == MVT::i16)
   2312       LC = RTLIB::SRA_I16;
   2313     else if (VT == MVT::i32)
   2314       LC = RTLIB::SRA_I32;
   2315     else if (VT == MVT::i64)
   2316       LC = RTLIB::SRA_I64;
   2317     else if (VT == MVT::i128)
   2318       LC = RTLIB::SRA_I128;
   2319   }
   2320 
   2321   if (LC != RTLIB::UNKNOWN_LIBCALL && TLI.getLibcallName(LC)) {
   2322     SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   2323     SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, isSigned, dl).first, Lo, Hi);
   2324     return;
   2325   }
   2326 
   2327   if (!ExpandShiftWithUnknownAmountBit(N, Lo, Hi))
   2328     llvm_unreachable("Unsupported shift!");
   2329 }
   2330 
   2331 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
   2332                                                 SDValue &Lo, SDValue &Hi) {
   2333   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   2334   SDLoc dl(N);
   2335   SDValue Op = N->getOperand(0);
   2336   if (Op.getValueType().bitsLE(NVT)) {
   2337     // The low part is sign extension of the input (degenerates to a copy).
   2338     Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
   2339     // The high part is obtained by SRA'ing all but one of the bits of low part.
   2340     unsigned LoSize = NVT.getSizeInBits();
   2341     Hi = DAG.getNode(
   2342         ISD::SRA, dl, NVT, Lo,
   2343         DAG.getConstant(LoSize - 1, dl, TLI.getPointerTy(DAG.getDataLayout())));
   2344   } else {
   2345     // For example, extension of an i48 to an i64.  The operand type necessarily
   2346     // promotes to the result type, so will end up being expanded too.
   2347     assert(getTypeAction(Op.getValueType()) ==
   2348            TargetLowering::TypePromoteInteger &&
   2349            "Only know how to promote this result!");
   2350     SDValue Res = GetPromotedInteger(Op);
   2351     assert(Res.getValueType() == N->getValueType(0) &&
   2352            "Operand over promoted?");
   2353     // Split the promoted operand.  This will simplify when it is expanded.
   2354     SplitInteger(Res, Lo, Hi);
   2355     unsigned ExcessBits =
   2356       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
   2357     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
   2358                      DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
   2359                                                         ExcessBits)));
   2360   }
   2361 }
   2362 
   2363 void DAGTypeLegalizer::
   2364 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
   2365   SDLoc dl(N);
   2366   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   2367   EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
   2368 
   2369   if (EVT.bitsLE(Lo.getValueType())) {
   2370     // sext_inreg the low part if needed.
   2371     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
   2372                      N->getOperand(1));
   2373 
   2374     // The high part gets the sign extension from the lo-part.  This handles
   2375     // things like sextinreg V:i64 from i8.
   2376     Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
   2377                      DAG.getConstant(Hi.getValueType().getSizeInBits() - 1, dl,
   2378                                      TLI.getPointerTy(DAG.getDataLayout())));
   2379   } else {
   2380     // For example, extension of an i48 to an i64.  Leave the low part alone,
   2381     // sext_inreg the high part.
   2382     unsigned ExcessBits =
   2383       EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
   2384     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
   2385                      DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(),
   2386                                                         ExcessBits)));
   2387   }
   2388 }
   2389 
   2390 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
   2391                                          SDValue &Lo, SDValue &Hi) {
   2392   EVT VT = N->getValueType(0);
   2393   SDLoc dl(N);
   2394   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   2395 
   2396   if (TLI.getOperationAction(ISD::SDIVREM, VT) == TargetLowering::Custom) {
   2397     SDValue Res = DAG.getNode(ISD::SDIVREM, dl, DAG.getVTList(VT, VT), Ops);
   2398     SplitInteger(Res.getValue(1), Lo, Hi);
   2399     return;
   2400   }
   2401 
   2402   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2403   if (VT == MVT::i16)
   2404     LC = RTLIB::SREM_I16;
   2405   else if (VT == MVT::i32)
   2406     LC = RTLIB::SREM_I32;
   2407   else if (VT == MVT::i64)
   2408     LC = RTLIB::SREM_I64;
   2409   else if (VT == MVT::i128)
   2410     LC = RTLIB::SREM_I128;
   2411   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
   2412 
   2413   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, true, dl).first, Lo, Hi);
   2414 }
   2415 
   2416 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
   2417                                              SDValue &Lo, SDValue &Hi) {
   2418   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   2419   SDLoc dl(N);
   2420   Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
   2421   Hi = DAG.getNode(ISD::SRL, dl, N->getOperand(0).getValueType(),
   2422                    N->getOperand(0),
   2423                    DAG.getConstant(NVT.getSizeInBits(), dl,
   2424                                    TLI.getPointerTy(DAG.getDataLayout())));
   2425   Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
   2426 }
   2427 
   2428 void DAGTypeLegalizer::ExpandIntRes_UADDSUBO(SDNode *N,
   2429                                              SDValue &Lo, SDValue &Hi) {
   2430   SDValue LHS = N->getOperand(0);
   2431   SDValue RHS = N->getOperand(1);
   2432   SDLoc dl(N);
   2433 
   2434   // Expand the result by simply replacing it with the equivalent
   2435   // non-overflow-checking operation.
   2436   SDValue Sum = DAG.getNode(N->getOpcode() == ISD::UADDO ?
   2437                             ISD::ADD : ISD::SUB, dl, LHS.getValueType(),
   2438                             LHS, RHS);
   2439   SplitInteger(Sum, Lo, Hi);
   2440 
   2441   // Calculate the overflow: addition overflows iff a + b < a, and subtraction
   2442   // overflows iff a - b > a.
   2443   SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Sum, LHS,
   2444                              N->getOpcode () == ISD::UADDO ?
   2445                              ISD::SETULT : ISD::SETUGT);
   2446 
   2447   // Use the calculated overflow everywhere.
   2448   ReplaceValueWith(SDValue(N, 1), Ofl);
   2449 }
   2450 
   2451 void DAGTypeLegalizer::ExpandIntRes_XMULO(SDNode *N,
   2452                                           SDValue &Lo, SDValue &Hi) {
   2453   EVT VT = N->getValueType(0);
   2454   SDLoc dl(N);
   2455 
   2456   // A divide for UMULO should be faster than a function call.
   2457   if (N->getOpcode() == ISD::UMULO) {
   2458     SDValue LHS = N->getOperand(0), RHS = N->getOperand(1);
   2459 
   2460     SDValue MUL = DAG.getNode(ISD::MUL, dl, LHS.getValueType(), LHS, RHS);
   2461     SplitInteger(MUL, Lo, Hi);
   2462 
   2463     // A divide for UMULO will be faster than a function call. Select to
   2464     // make sure we aren't using 0.
   2465     SDValue isZero = DAG.getSetCC(dl, getSetCCResultType(VT),
   2466                                   RHS, DAG.getConstant(0, dl, VT), ISD::SETEQ);
   2467     SDValue NotZero = DAG.getSelect(dl, VT, isZero,
   2468                                     DAG.getConstant(1, dl, VT), RHS);
   2469     SDValue DIV = DAG.getNode(ISD::UDIV, dl, VT, MUL, NotZero);
   2470     SDValue Overflow = DAG.getSetCC(dl, N->getValueType(1), DIV, LHS,
   2471                                     ISD::SETNE);
   2472     Overflow = DAG.getSelect(dl, N->getValueType(1), isZero,
   2473                              DAG.getConstant(0, dl, N->getValueType(1)),
   2474                              Overflow);
   2475     ReplaceValueWith(SDValue(N, 1), Overflow);
   2476     return;
   2477   }
   2478 
   2479   Type *RetTy = VT.getTypeForEVT(*DAG.getContext());
   2480   EVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
   2481   Type *PtrTy = PtrVT.getTypeForEVT(*DAG.getContext());
   2482 
   2483   // Replace this with a libcall that will check overflow.
   2484   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2485   if (VT == MVT::i32)
   2486     LC = RTLIB::MULO_I32;
   2487   else if (VT == MVT::i64)
   2488     LC = RTLIB::MULO_I64;
   2489   else if (VT == MVT::i128)
   2490     LC = RTLIB::MULO_I128;
   2491   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XMULO!");
   2492 
   2493   SDValue Temp = DAG.CreateStackTemporary(PtrVT);
   2494   // Temporary for the overflow value, default it to zero.
   2495   SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl,
   2496                                DAG.getConstant(0, dl, PtrVT), Temp,
   2497                                MachinePointerInfo(), false, false, 0);
   2498 
   2499   TargetLowering::ArgListTy Args;
   2500   TargetLowering::ArgListEntry Entry;
   2501   for (const SDValue &Op : N->op_values()) {
   2502     EVT ArgVT = Op.getValueType();
   2503     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
   2504     Entry.Node = Op;
   2505     Entry.Ty = ArgTy;
   2506     Entry.isSExt = true;
   2507     Entry.isZExt = false;
   2508     Args.push_back(Entry);
   2509   }
   2510 
   2511   // Also pass the address of the overflow check.
   2512   Entry.Node = Temp;
   2513   Entry.Ty = PtrTy->getPointerTo();
   2514   Entry.isSExt = true;
   2515   Entry.isZExt = false;
   2516   Args.push_back(Entry);
   2517 
   2518   SDValue Func = DAG.getExternalSymbol(TLI.getLibcallName(LC), PtrVT);
   2519 
   2520   TargetLowering::CallLoweringInfo CLI(DAG);
   2521   CLI.setDebugLoc(dl).setChain(Chain)
   2522     .setCallee(TLI.getLibcallCallingConv(LC), RetTy, Func, std::move(Args), 0)
   2523     .setSExtResult();
   2524 
   2525   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
   2526 
   2527   SplitInteger(CallInfo.first, Lo, Hi);
   2528   SDValue Temp2 = DAG.getLoad(PtrVT, dl, CallInfo.second, Temp,
   2529                               MachinePointerInfo(), false, false, false, 0);
   2530   SDValue Ofl = DAG.getSetCC(dl, N->getValueType(1), Temp2,
   2531                              DAG.getConstant(0, dl, PtrVT),
   2532                              ISD::SETNE);
   2533   // Use the overflow from the libcall everywhere.
   2534   ReplaceValueWith(SDValue(N, 1), Ofl);
   2535 }
   2536 
   2537 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
   2538                                          SDValue &Lo, SDValue &Hi) {
   2539   EVT VT = N->getValueType(0);
   2540   SDLoc dl(N);
   2541   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   2542 
   2543   if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
   2544     SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
   2545     SplitInteger(Res.getValue(0), Lo, Hi);
   2546     return;
   2547   }
   2548 
   2549   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2550   if (VT == MVT::i16)
   2551     LC = RTLIB::UDIV_I16;
   2552   else if (VT == MVT::i32)
   2553     LC = RTLIB::UDIV_I32;
   2554   else if (VT == MVT::i64)
   2555     LC = RTLIB::UDIV_I64;
   2556   else if (VT == MVT::i128)
   2557     LC = RTLIB::UDIV_I128;
   2558   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
   2559 
   2560   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, false, dl).first, Lo, Hi);
   2561 }
   2562 
   2563 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
   2564                                          SDValue &Lo, SDValue &Hi) {
   2565   EVT VT = N->getValueType(0);
   2566   SDLoc dl(N);
   2567   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
   2568 
   2569   if (TLI.getOperationAction(ISD::UDIVREM, VT) == TargetLowering::Custom) {
   2570     SDValue Res = DAG.getNode(ISD::UDIVREM, dl, DAG.getVTList(VT, VT), Ops);
   2571     SplitInteger(Res.getValue(1), Lo, Hi);
   2572     return;
   2573   }
   2574 
   2575   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
   2576   if (VT == MVT::i16)
   2577     LC = RTLIB::UREM_I16;
   2578   else if (VT == MVT::i32)
   2579     LC = RTLIB::UREM_I32;
   2580   else if (VT == MVT::i64)
   2581     LC = RTLIB::UREM_I64;
   2582   else if (VT == MVT::i128)
   2583     LC = RTLIB::UREM_I128;
   2584   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
   2585 
   2586   SplitInteger(TLI.makeLibCall(DAG, LC, VT, Ops, false, dl).first, Lo, Hi);
   2587 }
   2588 
   2589 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
   2590                                                 SDValue &Lo, SDValue &Hi) {
   2591   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
   2592   SDLoc dl(N);
   2593   SDValue Op = N->getOperand(0);
   2594   if (Op.getValueType().bitsLE(NVT)) {
   2595     // The low part is zero extension of the input (degenerates to a copy).
   2596     Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
   2597     Hi = DAG.getConstant(0, dl, NVT);   // The high part is just a zero.
   2598   } else {
   2599     // For example, extension of an i48 to an i64.  The operand type necessarily
   2600     // promotes to the result type, so will end up being expanded too.
   2601     assert(getTypeAction(Op.getValueType()) ==
   2602            TargetLowering::TypePromoteInteger &&
   2603            "Only know how to promote this result!");
   2604     SDValue Res = GetPromotedInteger(Op);
   2605     assert(Res.getValueType() == N->getValueType(0) &&
   2606            "Operand over promoted?");
   2607     // Split the promoted operand.  This will simplify when it is expanded.
   2608     SplitInteger(Res, Lo, Hi);
   2609     unsigned ExcessBits =
   2610       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
   2611     Hi = DAG.getZeroExtendInReg(Hi, dl,
   2612                                 EVT::getIntegerVT(*DAG.getContext(),
   2613                                                   ExcessBits));
   2614   }
   2615 }
   2616 
   2617 void DAGTypeLegalizer::ExpandIntRes_ATOMIC_LOAD(SDNode *N,
   2618                                                 SDValue &Lo, SDValue &Hi) {
   2619   SDLoc dl(N);
   2620   EVT VT = cast<AtomicSDNode>(N)->getMemoryVT();
   2621   SDVTList VTs = DAG.getVTList(VT, MVT::i1, MVT::Other);
   2622   SDValue Zero = DAG.getConstant(0, dl, VT);
   2623   SDValue Swap = DAG.getAtomicCmpSwap(
   2624       ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, dl,
   2625       cast<AtomicSDNode>(N)->getMemoryVT(), VTs, N->getOperand(0),
   2626       N->getOperand(1), Zero, Zero, cast<AtomicSDNode>(N)->getMemOperand(),
   2627       cast<AtomicSDNode>(N)->getOrdering(),
   2628       cast<AtomicSDNode>(N)->getOrdering(),
   2629       cast<AtomicSDNode>(N)->getSynchScope());
   2630 
   2631   ReplaceValueWith(SDValue(N, 0), Swap.getValue(0));
   2632   ReplaceValueWith(SDValue(N, 1), Swap.getValue(2));
   2633 }
   2634 
   2635 //===----------------------------------------------------------------------===//
   2636 //  Integer Operand Expansion
   2637 //===----------------------------------------------------------------------===//
   2638 
   2639 /// ExpandIntegerOperand - This method is called when the specified operand of
   2640 /// the specified node is found to need expansion.  At this point, all of the
   2641 /// result types of the node are known to be legal, but other operands of the
   2642 /// node may need promotion or expansion as well as the specified one.
   2643 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
   2644   DEBUG(dbgs() << "Expand integer operand: "; N->dump(&DAG); dbgs() << "\n");
   2645   SDValue Res = SDValue();
   2646 
   2647   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
   2648     return false;
   2649 
   2650   switch (N->getOpcode()) {
   2651   default:
   2652   #ifndef NDEBUG
   2653     dbgs() << "ExpandIntegerOperand Op #" << OpNo << ": ";
   2654     N->dump(&DAG); dbgs() << "\n";
   2655   #endif
   2656     llvm_unreachable("Do not know how to expand this operator's operand!");
   2657 
   2658   case ISD::BITCAST:           Res = ExpandOp_BITCAST(N); break;
   2659   case ISD::BR_CC:             Res = ExpandIntOp_BR_CC(N); break;
   2660   case ISD::BUILD_VECTOR:      Res = ExpandOp_BUILD_VECTOR(N); break;
   2661   case ISD::EXTRACT_ELEMENT:   Res = ExpandOp_EXTRACT_ELEMENT(N); break;
   2662   case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
   2663   case ISD::SCALAR_TO_VECTOR:  Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
   2664   case ISD::SELECT_CC:         Res = ExpandIntOp_SELECT_CC(N); break;
   2665   case ISD::SETCC:             Res = ExpandIntOp_SETCC(N); break;
   2666   case ISD::SETCCE:            Res = ExpandIntOp_SETCCE(N); break;
   2667   case ISD::SINT_TO_FP:        Res = ExpandIntOp_SINT_TO_FP(N); break;
   2668   case ISD::STORE:   Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
   2669   case ISD::TRUNCATE:          Res = ExpandIntOp_TRUNCATE(N); break;
   2670   case ISD::UINT_TO_FP:        Res = ExpandIntOp_UINT_TO_FP(N); break;
   2671 
   2672   case ISD::SHL:
   2673   case ISD::SRA:
   2674   case ISD::SRL:
   2675   case ISD::ROTL:
   2676   case ISD::ROTR:              Res = ExpandIntOp_Shift(N); break;
   2677   case ISD::RETURNADDR:
   2678   case ISD::FRAMEADDR:         Res = ExpandIntOp_RETURNADDR(N); break;
   2679 
   2680   case ISD::ATOMIC_STORE:      Res = ExpandIntOp_ATOMIC_STORE(N); break;
   2681   }
   2682 
   2683   // If the result is null, the sub-method took care of registering results etc.
   2684   if (!Res.getNode()) return false;
   2685 
   2686   // If the result is N, the sub-method updated N in place.  Tell the legalizer
   2687   // core about this.
   2688   if (Res.getNode() == N)
   2689     return true;
   2690 
   2691   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
   2692          "Invalid operand expansion");
   2693 
   2694   ReplaceValueWith(SDValue(N, 0), Res);
   2695   return false;
   2696 }
   2697 
   2698 /// IntegerExpandSetCCOperands - Expand the operands of a comparison.  This code
   2699 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
   2700 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
   2701                                                   SDValue &NewRHS,
   2702                                                   ISD::CondCode &CCCode,
   2703                                                   SDLoc dl) {
   2704   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
   2705   GetExpandedInteger(NewLHS, LHSLo, LHSHi);
   2706   GetExpandedInteger(NewRHS, RHSLo, RHSHi);
   2707 
   2708   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
   2709     if (RHSLo == RHSHi) {
   2710       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
   2711         if (RHSCST->isAllOnesValue()) {
   2712           // Equality comparison to -1.
   2713           NewLHS = DAG.getNode(ISD::AND, dl,
   2714                                LHSLo.getValueType(), LHSLo, LHSHi);
   2715           NewRHS = RHSLo;
   2716           return;
   2717         }
   2718       }
   2719     }
   2720 
   2721     NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
   2722     NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
   2723     NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
   2724     NewRHS = DAG.getConstant(0, dl, NewLHS.getValueType());
   2725     return;
   2726   }
   2727 
   2728   // If this is a comparison of the sign bit, just look at the top part.
   2729   // X > -1,  x < 0
   2730   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
   2731     if ((CCCode == ISD::SETLT && CST->isNullValue()) ||     // X < 0
   2732         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
   2733       NewLHS = LHSHi;
   2734       NewRHS = RHSHi;
   2735       return;
   2736     }
   2737 
   2738   // FIXME: This generated code sucks.
   2739   ISD::CondCode LowCC;
   2740   switch (CCCode) {
   2741   default: llvm_unreachable("Unknown integer setcc!");
   2742   case ISD::SETLT:
   2743   case ISD::SETULT: LowCC = ISD::SETULT; break;
   2744   case ISD::SETGT:
   2745   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
   2746   case ISD::SETLE:
   2747   case ISD::SETULE: LowCC = ISD::SETULE; break;
   2748   case ISD::SETGE:
   2749   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
   2750   }
   2751 
   2752   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
   2753   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
   2754   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
   2755 
   2756   // NOTE: on targets without efficient SELECT of bools, we can always use
   2757   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
   2758   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, AfterLegalizeTypes, true,
   2759                                                  nullptr);
   2760   SDValue Tmp1, Tmp2;
   2761   if (TLI.isTypeLegal(LHSLo.getValueType()) &&
   2762       TLI.isTypeLegal(RHSLo.getValueType()))
   2763     Tmp1 = TLI.SimplifySetCC(getSetCCResultType(LHSLo.getValueType()),
   2764                              LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
   2765   if (!Tmp1.getNode())
   2766     Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
   2767                         LHSLo, RHSLo, LowCC);
   2768   if (TLI.isTypeLegal(LHSHi.getValueType()) &&
   2769       TLI.isTypeLegal(RHSHi.getValueType()))
   2770     Tmp2 = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()),
   2771                              LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
   2772   if (!Tmp2.getNode())
   2773     Tmp2 = DAG.getNode(ISD::SETCC, dl,
   2774                        getSetCCResultType(LHSHi.getValueType()),
   2775                        LHSHi, RHSHi, DAG.getCondCode(CCCode));
   2776 
   2777   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
   2778   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
   2779   if ((Tmp1C && Tmp1C->isNullValue()) ||
   2780       (Tmp2C && Tmp2C->isNullValue() &&
   2781        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
   2782         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
   2783       (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
   2784        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
   2785         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
   2786     // low part is known false, returns high part.
   2787     // For LE / GE, if high part is known false, ignore the low part.
   2788     // For LT / GT, if high part is known true, ignore the low part.
   2789     NewLHS = Tmp2;
   2790     NewRHS = SDValue();
   2791     return;
   2792   }
   2793 
   2794   if (LHSHi == RHSHi) {
   2795     // Comparing the low bits is enough.
   2796     NewLHS = Tmp1;
   2797     NewRHS = SDValue();
   2798     return;
   2799   }
   2800 
   2801   // Lower with SETCCE if the target supports it.
   2802   // FIXME: Make all targets support this, then remove the other lowering.
   2803   if (TLI.getOperationAction(
   2804           ISD::SETCCE,
   2805           TLI.getTypeToExpandTo(*DAG.getContext(), LHSLo.getValueType())) ==
   2806       TargetLowering::Custom) {
   2807     // SETCCE can detect < and >= directly. For > and <=, flip operands and
   2808     // condition code.
   2809     bool FlipOperands = false;
   2810     switch (CCCode) {
   2811     case ISD::SETGT:  CCCode = ISD::SETLT;  FlipOperands = true; break;
   2812     case ISD::SETUGT: CCCode = ISD::SETULT; FlipOperands = true; break;
   2813     case ISD::SETLE:  CCCode = ISD::SETGE;  FlipOperands = true; break;
   2814     case ISD::SETULE: CCCode = ISD::SETUGE; FlipOperands = true; break;
   2815     default: break;
   2816     }
   2817     if (FlipOperands) {
   2818       std::swap(LHSLo, RHSLo);
   2819       std::swap(LHSHi, RHSHi);
   2820     }
   2821     // Perform a wide subtraction, feeding the carry from the low part into
   2822     // SETCCE. The SETCCE operation is essentially looking at the high part of
   2823     // the result of LHS - RHS. It is negative iff LHS < RHS. It is zero or
   2824     // positive iff LHS >= RHS.
   2825     SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), MVT::Glue);
   2826     SDValue LowCmp = DAG.getNode(ISD::SUBC, dl, VTList, LHSLo, RHSLo);
   2827     SDValue Res =
   2828         DAG.getNode(ISD::SETCCE, dl, getSetCCResultType(LHSLo.getValueType()),
   2829                     LHSHi, RHSHi, LowCmp.getValue(1), DAG.getCondCode(CCCode));
   2830     NewLHS = Res;
   2831     NewRHS = SDValue();
   2832     return;
   2833   }
   2834 
   2835   NewLHS = TLI.SimplifySetCC(getSetCCResultType(LHSHi.getValueType()),
   2836                              LHSHi, RHSHi, ISD::SETEQ, false,
   2837                              DagCombineInfo, dl);
   2838   if (!NewLHS.getNode())
   2839     NewLHS = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
   2840                           LHSHi, RHSHi, ISD::SETEQ);
   2841   NewLHS = DAG.getSelect(dl, Tmp1.getValueType(),
   2842                          NewLHS, Tmp1, Tmp2);
   2843   NewRHS = SDValue();
   2844 }
   2845 
   2846 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
   2847   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
   2848   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
   2849   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
   2850 
   2851   // If ExpandSetCCOperands returned a scalar, we need to compare the result
   2852   // against zero to select between true and false values.
   2853   if (!NewRHS.getNode()) {
   2854     NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
   2855     CCCode = ISD::SETNE;
   2856   }
   2857 
   2858   // Update N to have the operands specified.
   2859   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
   2860                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
   2861                                 N->getOperand(4)), 0);
   2862 }
   2863 
   2864 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
   2865   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
   2866   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
   2867   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
   2868 
   2869   // If ExpandSetCCOperands returned a scalar, we need to compare the result
   2870   // against zero to select between true and false values.
   2871   if (!NewRHS.getNode()) {
   2872     NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
   2873     CCCode = ISD::SETNE;
   2874   }
   2875 
   2876   // Update N to have the operands specified.
   2877   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
   2878                                 N->getOperand(2), N->getOperand(3),
   2879                                 DAG.getCondCode(CCCode)), 0);
   2880 }
   2881 
   2882 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
   2883   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
   2884   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
   2885   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
   2886 
   2887   // If ExpandSetCCOperands returned a scalar, use it.
   2888   if (!NewRHS.getNode()) {
   2889     assert(NewLHS.getValueType() == N->getValueType(0) &&
   2890            "Unexpected setcc expansion!");
   2891     return NewLHS;
   2892   }
   2893 
   2894   // Otherwise, update N to have the operands specified.
   2895   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
   2896                                 DAG.getCondCode(CCCode)), 0);
   2897 }
   2898 
   2899 SDValue DAGTypeLegalizer::ExpandIntOp_SETCCE(SDNode *N) {
   2900   SDValue LHS = N->getOperand(0);
   2901   SDValue RHS = N->getOperand(1);
   2902   SDValue Carry = N->getOperand(2);
   2903   SDValue Cond = N->getOperand(3);
   2904   SDLoc dl = SDLoc(N);
   2905 
   2906   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
   2907   GetExpandedInteger(LHS, LHSLo, LHSHi);
   2908   GetExpandedInteger(RHS, RHSLo, RHSHi);
   2909 
   2910   // Expand to a SUBE for the low part and a smaller SETCCE for the high.
   2911   SDVTList VTList = DAG.getVTList(LHSLo.getValueType(), MVT::Glue);
   2912   SDValue LowCmp = DAG.getNode(ISD::SUBE, dl, VTList, LHSLo, RHSLo, Carry);
   2913   return DAG.getNode(ISD::SETCCE, dl, N->getValueType(0), LHSHi, RHSHi,
   2914                      LowCmp.getValue(1), Cond);
   2915 }
   2916 
   2917 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
   2918   // The value being shifted is legal, but the shift amount is too big.
   2919   // It follows that either the result of the shift is undefined, or the
   2920   // upper half of the shift amount is zero.  Just use the lower half.
   2921   SDValue Lo, Hi;
   2922   GetExpandedInteger(N->getOperand(1), Lo, Hi);
   2923   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0), Lo), 0);
   2924 }
   2925 
   2926 SDValue DAGTypeLegalizer::ExpandIntOp_RETURNADDR(SDNode *N) {
   2927   // The argument of RETURNADDR / FRAMEADDR builtin is 32 bit contant.  This
   2928   // surely makes pretty nice problems on 8/16 bit targets. Just truncate this
   2929   // constant to valid type.
   2930   SDValue Lo, Hi;
   2931   GetExpandedInteger(N->getOperand(0), Lo, Hi);
   2932   return SDValue(DAG.UpdateNodeOperands(N, Lo), 0);
   2933 }
   2934 
   2935 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
   2936   SDValue Op = N->getOperand(0);
   2937   EVT DstVT = N->getValueType(0);
   2938   RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
   2939   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
   2940          "Don't know how to expand this SINT_TO_FP!");
   2941   return TLI.makeLibCall(DAG, LC, DstVT, Op, true, SDLoc(N)).first;
   2942 }
   2943 
   2944 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
   2945   if (ISD::isNormalStore(N))
   2946     return ExpandOp_NormalStore(N, OpNo);
   2947 
   2948   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
   2949   assert(OpNo == 1 && "Can only expand the stored value so far");
   2950 
   2951   EVT VT = N->getOperand(1).getValueType();
   2952   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
   2953   SDValue Ch  = N->getChain();
   2954   SDValue Ptr = N->getBasePtr();
   2955   unsigned Alignment = N->getAlignment();
   2956   bool isVolatile = N->isVolatile();
   2957   bool isNonTemporal = N->isNonTemporal();
   2958   AAMDNodes AAInfo = N->getAAInfo();
   2959   SDLoc dl(N);
   2960   SDValue Lo, Hi;
   2961 
   2962   assert(NVT.isByteSized() && "Expanded type not byte sized!");
   2963 
   2964   if (N->getMemoryVT().bitsLE(NVT)) {
   2965     GetExpandedInteger(N->getValue(), Lo, Hi);
   2966     return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
   2967                              N->getMemoryVT(), isVolatile, isNonTemporal,
   2968                              Alignment, AAInfo);
   2969   }
   2970 
   2971   if (DAG.getDataLayout().isLittleEndian()) {
   2972     // Little-endian - low bits are at low addresses.
   2973     GetExpandedInteger(N->getValue(), Lo, Hi);
   2974 
   2975     Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getPointerInfo(),
   2976                       isVolatile, isNonTemporal, Alignment, AAInfo);
   2977 
   2978     unsigned ExcessBits =
   2979       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
   2980     EVT NEVT = EVT::getIntegerVT(*DAG.getContext(), ExcessBits);
   2981 
   2982     // Increment the pointer to the other half.
   2983     unsigned IncrementSize = NVT.getSizeInBits()/8;
   2984     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
   2985                       DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
   2986     Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr,
   2987                            N->getPointerInfo().getWithOffset(IncrementSize),
   2988                            NEVT, isVolatile, isNonTemporal,
   2989                            MinAlign(Alignment, IncrementSize), AAInfo);
   2990     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
   2991   }
   2992 
   2993   // Big-endian - high bits are at low addresses.  Favor aligned stores at
   2994   // the cost of some bit-fiddling.
   2995   GetExpandedInteger(N->getValue(), Lo, Hi);
   2996 
   2997   EVT ExtVT = N->getMemoryVT();
   2998   unsigned EBytes = ExtVT.getStoreSize();
   2999   unsigned IncrementSize = NVT.getSizeInBits()/8;
   3000   unsigned ExcessBits = (EBytes - IncrementSize)*8;
   3001   EVT HiVT = EVT::getIntegerVT(*DAG.getContext(),
   3002                                ExtVT.getSizeInBits() - ExcessBits);
   3003 
   3004   if (ExcessBits < NVT.getSizeInBits()) {
   3005     // Transfer high bits from the top of Lo to the bottom of Hi.
   3006     Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
   3007                      DAG.getConstant(NVT.getSizeInBits() - ExcessBits, dl,
   3008                                      TLI.getPointerTy(DAG.getDataLayout())));
   3009     Hi = DAG.getNode(
   3010         ISD::OR, dl, NVT, Hi,
   3011         DAG.getNode(ISD::SRL, dl, NVT, Lo,
   3012                     DAG.getConstant(ExcessBits, dl,
   3013                                     TLI.getPointerTy(DAG.getDataLayout()))));
   3014   }
   3015 
   3016   // Store both the high bits and maybe some of the low bits.
   3017   Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getPointerInfo(),
   3018                          HiVT, isVolatile, isNonTemporal, Alignment, AAInfo);
   3019 
   3020   // Increment the pointer to the other half.
   3021   Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
   3022                     DAG.getConstant(IncrementSize, dl, Ptr.getValueType()));
   3023   // Store the lowest ExcessBits bits in the second half.
   3024   Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr,
   3025                          N->getPointerInfo().getWithOffset(IncrementSize),
   3026                          EVT::getIntegerVT(*DAG.getContext(), ExcessBits),
   3027                          isVolatile, isNonTemporal,
   3028                          MinAlign(Alignment, IncrementSize), AAInfo);
   3029   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
   3030 }
   3031 
   3032 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
   3033   SDValue InL, InH;
   3034   GetExpandedInteger(N->getOperand(0), InL, InH);
   3035   // Just truncate the low part of the source.
   3036   return DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), InL);
   3037 }
   3038 
   3039 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
   3040   SDValue Op = N->getOperand(0);
   3041   EVT SrcVT = Op.getValueType();
   3042   EVT DstVT = N->getValueType(0);
   3043   SDLoc dl(N);
   3044 
   3045   // The following optimization is valid only if every value in SrcVT (when
   3046   // treated as signed) is representable in DstVT.  Check that the mantissa
   3047   // size of DstVT is >= than the number of bits in SrcVT -1.
   3048   const fltSemantics &sem = DAG.EVTToAPFloatSemantics(DstVT);
   3049   if (APFloat::semanticsPrecision(sem) >= SrcVT.getSizeInBits()-1 &&
   3050       TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
   3051     // Do a signed conversion then adjust the result.
   3052     SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
   3053     SignedConv = TLI.LowerOperation(SignedConv, DAG);
   3054 
   3055     // The result of the signed conversion needs adjusting if the 'sign bit' of
   3056     // the incoming integer was set.  To handle this, we dynamically test to see
   3057     // if it is set, and, if so, add a fudge factor.
   3058 
   3059     const uint64_t F32TwoE32  = 0x4F800000ULL;
   3060     const uint64_t F32TwoE64  = 0x5F800000ULL;
   3061     const uint64_t F32TwoE128 = 0x7F800000ULL;
   3062 
   3063     APInt FF(32, 0);
   3064     if (SrcVT == MVT::i32)
   3065       FF = APInt(32, F32TwoE32);
   3066     else if (SrcVT == MVT::i64)
   3067       FF = APInt(32, F32TwoE64);
   3068     else if (SrcVT == MVT::i128)
   3069       FF = APInt(32, F32TwoE128);
   3070     else
   3071       llvm_unreachable("Unsupported UINT_TO_FP!");
   3072 
   3073     // Check whether the sign bit is set.
   3074     SDValue Lo, Hi;
   3075     GetExpandedInteger(Op, Lo, Hi);
   3076     SDValue SignSet = DAG.getSetCC(dl,
   3077                                    getSetCCResultType(Hi.getValueType()),
   3078                                    Hi,
   3079                                    DAG.getConstant(0, dl, Hi.getValueType()),
   3080                                    ISD::SETLT);
   3081 
   3082     // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
   3083     SDValue FudgePtr =
   3084         DAG.getConstantPool(ConstantInt::get(*DAG.getContext(), FF.zext(64)),
   3085                             TLI.getPointerTy(DAG.getDataLayout()));
   3086 
   3087     // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
   3088     SDValue Zero = DAG.getIntPtrConstant(0, dl);
   3089     SDValue Four = DAG.getIntPtrConstant(4, dl);
   3090     if (DAG.getDataLayout().isBigEndian())
   3091       std::swap(Zero, Four);
   3092     SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet,
   3093                                    Zero, Four);
   3094     unsigned Alignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
   3095     FudgePtr = DAG.getNode(ISD::ADD, dl, FudgePtr.getValueType(),
   3096                            FudgePtr, Offset);
   3097     Alignment = std::min(Alignment, 4u);
   3098 
   3099     // Load the value out, extending it from f32 to the destination float type.
   3100     // FIXME: Avoid the extend by constructing the right constant pool?
   3101     SDValue Fudge = DAG.getExtLoad(
   3102         ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(), FudgePtr,
   3103         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
   3104         false, false, false, Alignment);
   3105     return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
   3106   }
   3107 
   3108   // Otherwise, use a libcall.
   3109   RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
   3110   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
   3111          "Don't know how to expand this UINT_TO_FP!");
   3112   return TLI.makeLibCall(DAG, LC, DstVT, Op, true, dl).first;
   3113 }
   3114 
   3115 SDValue DAGTypeLegalizer::ExpandIntOp_ATOMIC_STORE(SDNode *N) {
   3116   SDLoc dl(N);
   3117   SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
   3118                                cast<AtomicSDNode>(N)->getMemoryVT(),
   3119                                N->getOperand(0),
   3120                                N->getOperand(1), N->getOperand(2),
   3121                                cast<AtomicSDNode>(N)->getMemOperand(),
   3122                                cast<AtomicSDNode>(N)->getOrdering(),
   3123                                cast<AtomicSDNode>(N)->getSynchScope());
   3124   return Swap.getValue(1);
   3125 }
   3126 
   3127 
   3128 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_SUBVECTOR(SDNode *N) {
   3129   SDValue InOp0 = N->getOperand(0);
   3130   EVT InVT = InOp0.getValueType();
   3131 
   3132   EVT OutVT = N->getValueType(0);
   3133   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
   3134   assert(NOutVT.isVector() && "This type must be promoted to a vector type");
   3135   unsigned OutNumElems = OutVT.getVectorNumElements();
   3136   EVT NOutVTElem = NOutVT.getVectorElementType();
   3137 
   3138   SDLoc dl(N);
   3139   SDValue BaseIdx = N->getOperand(1);
   3140 
   3141   SmallVector<SDValue, 8> Ops;
   3142   Ops.reserve(OutNumElems);
   3143   for (unsigned i = 0; i != OutNumElems; ++i) {
   3144 
   3145     // Extract the element from the original vector.
   3146     SDValue Index = DAG.getNode(ISD::ADD, dl, BaseIdx.getValueType(),
   3147       BaseIdx, DAG.getConstant(i, dl, BaseIdx.getValueType()));
   3148     SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
   3149       InVT.getVectorElementType(), N->getOperand(0), Index);
   3150 
   3151     SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, Ext);
   3152     // Insert the converted element to the new vector.
   3153     Ops.push_back(Op);
   3154   }
   3155 
   3156   return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops);
   3157 }
   3158 
   3159 
   3160 SDValue DAGTypeLegalizer::PromoteIntRes_VECTOR_SHUFFLE(SDNode *N) {
   3161   ShuffleVectorSDNode *SV = cast<ShuffleVectorSDNode>(N);
   3162   EVT VT = N->getValueType(0);
   3163   SDLoc dl(N);
   3164 
   3165   ArrayRef<int> NewMask = SV->getMask().slice(0, VT.getVectorNumElements());
   3166 
   3167   SDValue V0 = GetPromotedInteger(N->getOperand(0));
   3168   SDValue V1 = GetPromotedInteger(N->getOperand(1));
   3169   EVT OutVT = V0.getValueType();
   3170 
   3171   return DAG.getVectorShuffle(OutVT, dl, V0, V1, NewMask);
   3172 }
   3173 
   3174 
   3175 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_VECTOR(SDNode *N) {
   3176   EVT OutVT = N->getValueType(0);
   3177   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
   3178   assert(NOutVT.isVector() && "This type must be promoted to a vector type");
   3179   unsigned NumElems = N->getNumOperands();
   3180   EVT NOutVTElem = NOutVT.getVectorElementType();
   3181 
   3182   SDLoc dl(N);
   3183 
   3184   SmallVector<SDValue, 8> Ops;
   3185   Ops.reserve(NumElems);
   3186   for (unsigned i = 0; i != NumElems; ++i) {
   3187     SDValue Op;
   3188     // BUILD_VECTOR integer operand types are allowed to be larger than the
   3189     // result's element type. This may still be true after the promotion. For
   3190     // example, we might be promoting (<v?i1> = BV <i32>, <i32>, ...) to
   3191     // (v?i16 = BV <i32>, <i32>, ...), and we can't any_extend <i32> to <i16>.
   3192     if (N->getOperand(i).getValueType().bitsLT(NOutVTElem))
   3193       Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(i));
   3194     else
   3195       Op = N->getOperand(i);
   3196     Ops.push_back(Op);
   3197   }
   3198 
   3199   return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops);
   3200 }
   3201 
   3202 SDValue DAGTypeLegalizer::PromoteIntRes_SCALAR_TO_VECTOR(SDNode *N) {
   3203 
   3204   SDLoc dl(N);
   3205 
   3206   assert(!N->getOperand(0).getValueType().isVector() &&
   3207          "Input must be a scalar");
   3208 
   3209   EVT OutVT = N->getValueType(0);
   3210   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
   3211   assert(NOutVT.isVector() && "This type must be promoted to a vector type");
   3212   EVT NOutVTElem = NOutVT.getVectorElementType();
   3213 
   3214   SDValue Op = DAG.getNode(ISD::ANY_EXTEND, dl, NOutVTElem, N->getOperand(0));
   3215 
   3216   return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NOutVT, Op);
   3217 }
   3218 
   3219 SDValue DAGTypeLegalizer::PromoteIntRes_CONCAT_VECTORS(SDNode *N) {
   3220   SDLoc dl(N);
   3221 
   3222   EVT OutVT = N->getValueType(0);
   3223   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
   3224   assert(NOutVT.isVector() && "This type must be promoted to a vector type");
   3225 
   3226   EVT InElemTy = OutVT.getVectorElementType();
   3227   EVT OutElemTy = NOutVT.getVectorElementType();
   3228 
   3229   unsigned NumElem = N->getOperand(0).getValueType().getVectorNumElements();
   3230   unsigned NumOutElem = NOutVT.getVectorNumElements();
   3231   unsigned NumOperands = N->getNumOperands();
   3232   assert(NumElem * NumOperands == NumOutElem &&
   3233          "Unexpected number of elements");
   3234 
   3235   // Take the elements from the first vector.
   3236   SmallVector<SDValue, 8> Ops(NumOutElem);
   3237   for (unsigned i = 0; i < NumOperands; ++i) {
   3238     SDValue Op = N->getOperand(i);
   3239     for (unsigned j = 0; j < NumElem; ++j) {
   3240       SDValue Ext = DAG.getNode(
   3241           ISD::EXTRACT_VECTOR_ELT, dl, InElemTy, Op,
   3242           DAG.getConstant(j, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
   3243       Ops[i * NumElem + j] = DAG.getNode(ISD::ANY_EXTEND, dl, OutElemTy, Ext);
   3244     }
   3245   }
   3246 
   3247   return DAG.getNode(ISD::BUILD_VECTOR, dl, NOutVT, Ops);
   3248 }
   3249 
   3250 SDValue DAGTypeLegalizer::PromoteIntRes_INSERT_VECTOR_ELT(SDNode *N) {
   3251   EVT OutVT = N->getValueType(0);
   3252   EVT NOutVT = TLI.getTypeToTransformTo(*DAG.getContext(), OutVT);
   3253   assert(NOutVT.isVector() && "This type must be promoted to a vector type");
   3254 
   3255   EVT NOutVTElem = NOutVT.getVectorElementType();
   3256 
   3257   SDLoc dl(N);
   3258   SDValue V0 = GetPromotedInteger(N->getOperand(0));
   3259 
   3260   SDValue ConvElem = DAG.getNode(ISD::ANY_EXTEND, dl,
   3261     NOutVTElem, N->getOperand(1));
   3262   return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, NOutVT,
   3263     V0, ConvElem, N->getOperand(2));
   3264 }
   3265 
   3266 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_VECTOR_ELT(SDNode *N) {
   3267   SDLoc dl(N);
   3268   SDValue V0 = GetPromotedInteger(N->getOperand(0));
   3269   SDValue V1 = DAG.getZExtOrTrunc(N->getOperand(1), dl,
   3270                                   TLI.getVectorIdxTy(DAG.getDataLayout()));
   3271   SDValue Ext = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
   3272     V0->getValueType(0).getScalarType(), V0, V1);
   3273 
   3274   // EXTRACT_VECTOR_ELT can return types which are wider than the incoming
   3275   // element types. If this is the case then we need to expand the outgoing
   3276   // value and not truncate it.
   3277   return DAG.getAnyExtOrTrunc(Ext, dl, N->getValueType(0));
   3278 }
   3279 
   3280 SDValue DAGTypeLegalizer::PromoteIntOp_EXTRACT_SUBVECTOR(SDNode *N) {
   3281   SDLoc dl(N);
   3282   SDValue V0 = GetPromotedInteger(N->getOperand(0));
   3283   MVT InVT = V0.getValueType().getSimpleVT();
   3284   MVT OutVT = MVT::getVectorVT(InVT.getVectorElementType(),
   3285                                N->getValueType(0).getVectorNumElements());
   3286   SDValue Ext = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OutVT, V0, N->getOperand(1));
   3287   return DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), Ext);
   3288 }
   3289 
   3290 SDValue DAGTypeLegalizer::PromoteIntOp_CONCAT_VECTORS(SDNode *N) {
   3291   SDLoc dl(N);
   3292   unsigned NumElems = N->getNumOperands();
   3293 
   3294   EVT RetSclrTy = N->getValueType(0).getVectorElementType();
   3295 
   3296   SmallVector<SDValue, 8> NewOps;
   3297   NewOps.reserve(NumElems);
   3298 
   3299   // For each incoming vector
   3300   for (unsigned VecIdx = 0; VecIdx != NumElems; ++VecIdx) {
   3301     SDValue Incoming = GetPromotedInteger(N->getOperand(VecIdx));
   3302     EVT SclrTy = Incoming->getValueType(0).getVectorElementType();
   3303     unsigned NumElem = Incoming->getValueType(0).getVectorNumElements();
   3304 
   3305     for (unsigned i=0; i<NumElem; ++i) {
   3306       // Extract element from incoming vector
   3307       SDValue Ex = DAG.getNode(
   3308           ISD::EXTRACT_VECTOR_ELT, dl, SclrTy, Incoming,
   3309           DAG.getConstant(i, dl, TLI.getVectorIdxTy(DAG.getDataLayout())));
   3310       SDValue Tr = DAG.getNode(ISD::TRUNCATE, dl, RetSclrTy, Ex);
   3311       NewOps.push_back(Tr);
   3312     }
   3313   }
   3314 
   3315   return DAG.getNode(ISD::BUILD_VECTOR, dl,  N->getValueType(0), NewOps);
   3316 }
   3317