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