LLVM API Documentation

LegalizeIntegerTypes.cpp

Go to the documentation of this file.
00001 //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements integer type expansion and promotion for LegalizeTypes.
00011 // Promotion is the act of changing a computation in an illegal type into a
00012 // computation in a larger type.  For example, implementing i8 arithmetic in an
00013 // i32 register (often needed on powerpc).
00014 // Expansion is the act of changing a computation in an illegal type into a
00015 // computation in two identical registers of a smaller type.  For example,
00016 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
00017 // targets).
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #include "LegalizeTypes.h"
00022 #include "llvm/CodeGen/PseudoSourceValue.h"
00023 using namespace llvm;
00024 
00025 //===----------------------------------------------------------------------===//
00026 //  Integer Result Promotion
00027 //===----------------------------------------------------------------------===//
00028 
00029 /// PromoteIntegerResult - This method is called when a result of a node is
00030 /// found to be in need of promotion to a larger type.  At this point, the node
00031 /// may also have invalid operands or may have other results that need
00032 /// expansion, we just know that (at least) one result needs promotion.
00033 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
00034   DEBUG(cerr << "Promote integer result: "; N->dump(&DAG); cerr << "\n");
00035   SDValue Res = SDValue();
00036 
00037   // See if the target wants to custom expand this node.
00038   if (CustomLowerResults(N, N->getValueType(ResNo), true))
00039     return;
00040 
00041   switch (N->getOpcode()) {
00042   default:
00043 #ifndef NDEBUG
00044     cerr << "PromoteIntegerResult #" << ResNo << ": ";
00045     N->dump(&DAG); cerr << "\n";
00046 #endif
00047     assert(0 && "Do not know how to promote this operator!");
00048     abort();
00049   case ISD::AssertSext:  Res = PromoteIntRes_AssertSext(N); break;
00050   case ISD::AssertZext:  Res = PromoteIntRes_AssertZext(N); break;
00051   case ISD::BIT_CONVERT: Res = PromoteIntRes_BIT_CONVERT(N); break;
00052   case ISD::BSWAP:       Res = PromoteIntRes_BSWAP(N); break;
00053   case ISD::BUILD_PAIR:  Res = PromoteIntRes_BUILD_PAIR(N); break;
00054   case ISD::Constant:    Res = PromoteIntRes_Constant(N); break;
00055   case ISD::CONVERT_RNDSAT:
00056                          Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
00057   case ISD::CTLZ:        Res = PromoteIntRes_CTLZ(N); break;
00058   case ISD::CTPOP:       Res = PromoteIntRes_CTPOP(N); break;
00059   case ISD::CTTZ:        Res = PromoteIntRes_CTTZ(N); break;
00060   case ISD::EXTRACT_VECTOR_ELT:
00061                          Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
00062   case ISD::LOAD:        Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
00063   case ISD::SELECT:      Res = PromoteIntRes_SELECT(N); break;
00064   case ISD::SELECT_CC:   Res = PromoteIntRes_SELECT_CC(N); break;
00065   case ISD::SETCC:       Res = PromoteIntRes_SETCC(N); break;
00066   case ISD::SHL:         Res = PromoteIntRes_SHL(N); break;
00067   case ISD::SIGN_EXTEND_INREG:
00068                          Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
00069   case ISD::SRA:         Res = PromoteIntRes_SRA(N); break;
00070   case ISD::SRL:         Res = PromoteIntRes_SRL(N); break;
00071   case ISD::TRUNCATE:    Res = PromoteIntRes_TRUNCATE(N); break;
00072   case ISD::UNDEF:       Res = PromoteIntRes_UNDEF(N); break;
00073   case ISD::VAARG:       Res = PromoteIntRes_VAARG(N); break;
00074 
00075   case ISD::SIGN_EXTEND:
00076   case ISD::ZERO_EXTEND:
00077   case ISD::ANY_EXTEND:  Res = PromoteIntRes_INT_EXTEND(N); break;
00078 
00079   case ISD::FP_TO_SINT:
00080   case ISD::FP_TO_UINT:  Res = PromoteIntRes_FP_TO_XINT(N); break;
00081 
00082   case ISD::AND:
00083   case ISD::OR:
00084   case ISD::XOR:
00085   case ISD::ADD:
00086   case ISD::SUB:
00087   case ISD::MUL:         Res = PromoteIntRes_SimpleIntBinOp(N); break;
00088 
00089   case ISD::SDIV:
00090   case ISD::SREM:        Res = PromoteIntRes_SDIV(N); break;
00091 
00092   case ISD::UDIV:
00093   case ISD::UREM:        Res = PromoteIntRes_UDIV(N); break;
00094 
00095   case ISD::SADDO:
00096   case ISD::SSUBO:       Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
00097   case ISD::UADDO:
00098   case ISD::USUBO:       Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
00099   case ISD::SMULO:
00100   case ISD::UMULO:       Res = PromoteIntRes_XMULO(N, ResNo); break;
00101 
00102   case ISD::ATOMIC_LOAD_ADD:
00103   case ISD::ATOMIC_LOAD_SUB:
00104   case ISD::ATOMIC_LOAD_AND:
00105   case ISD::ATOMIC_LOAD_OR:
00106   case ISD::ATOMIC_LOAD_XOR:
00107   case ISD::ATOMIC_LOAD_NAND:
00108   case ISD::ATOMIC_LOAD_MIN:
00109   case ISD::ATOMIC_LOAD_MAX:
00110   case ISD::ATOMIC_LOAD_UMIN:
00111   case ISD::ATOMIC_LOAD_UMAX:
00112   case ISD::ATOMIC_SWAP:
00113     Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
00114 
00115   case ISD::ATOMIC_CMP_SWAP:
00116     Res = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
00117   }
00118 
00119   // If the result is null then the sub-method took care of registering it.
00120   if (Res.getNode())
00121     SetPromotedInteger(SDValue(N, ResNo), Res);
00122 }
00123 
00124 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
00125   // Sign-extend the new bits, and continue the assertion.
00126   SDValue Op = SExtPromotedInteger(N->getOperand(0));
00127   return DAG.getNode(ISD::AssertSext, N->getDebugLoc(),
00128                      Op.getValueType(), Op, N->getOperand(1));
00129 }
00130 
00131 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
00132   // Zero the new bits, and continue the assertion.
00133   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
00134   return DAG.getNode(ISD::AssertZext, N->getDebugLoc(),
00135                      Op.getValueType(), Op, N->getOperand(1));
00136 }
00137 
00138 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
00139   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
00140   SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(),
00141                               N->getMemoryVT(),
00142                               N->getChain(), N->getBasePtr(),
00143                               Op2, N->getSrcValue(), N->getAlignment());
00144   // Legalized the chain result - switch anything that used the old chain to
00145   // use the new one.
00146   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
00147   return Res;
00148 }
00149 
00150 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
00151   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
00152   SDValue Op3 = GetPromotedInteger(N->getOperand(3));
00153   SDValue Res = DAG.getAtomic(N->getOpcode(), N->getMemoryVT(),
00154                               N->getChain(), N->getBasePtr(),
00155                               Op2, Op3, N->getSrcValue(), N->getAlignment());
00156   // Legalized the chain result - switch anything that used the old chain to
00157   // use the new one.
00158   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
00159   return Res;
00160 }
00161 
00162 SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
00163   SDValue InOp = N->getOperand(0);
00164   MVT InVT = InOp.getValueType();
00165   MVT NInVT = TLI.getTypeToTransformTo(InVT);
00166   MVT OutVT = N->getValueType(0);
00167   MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
00168   DebugLoc dl = N->getDebugLoc();
00169 
00170   switch (getTypeAction(InVT)) {
00171   default:
00172     assert(false && "Unknown type action!");
00173     break;
00174   case Legal:
00175     break;
00176   case PromoteInteger:
00177     if (NOutVT.bitsEq(NInVT))
00178       // The input promotes to the same size.  Convert the promoted value.
00179       return DAG.getNode(ISD::BIT_CONVERT, dl,
00180                          NOutVT, GetPromotedInteger(InOp));
00181     break;
00182   case SoftenFloat:
00183     // Promote the integer operand by hand.
00184     return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
00185   case ExpandInteger:
00186   case ExpandFloat:
00187     break;
00188   case ScalarizeVector:
00189     // Convert the element to an integer and promote it by hand.
00190     return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
00191                        BitConvertToInteger(GetScalarizedVector(InOp)));
00192   case SplitVector: {
00193     // For example, i32 = BIT_CONVERT v2i16 on alpha.  Convert the split
00194     // pieces of the input into integers and reassemble in the final type.
00195     SDValue Lo, Hi;
00196     GetSplitVector(N->getOperand(0), Lo, Hi);
00197     Lo = BitConvertToInteger(Lo);
00198     Hi = BitConvertToInteger(Hi);
00199 
00200     if (TLI.isBigEndian())
00201       std::swap(Lo, Hi);
00202 
00203     InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
00204                        MVT::getIntegerVT(NOutVT.getSizeInBits()),
00205                        JoinIntegers(Lo, Hi));
00206     return DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, InOp);
00207   }
00208   case WidenVector:
00209     if (OutVT.bitsEq(NInVT))
00210       // The input is widened to the same size.  Convert to the widened value.
00211       return DAG.getNode(ISD::BIT_CONVERT, dl, OutVT, GetWidenedVector(InOp));
00212   }
00213 
00214   // Otherwise, lower the bit-convert to a store/load from the stack.
00215   // Create the stack frame object.  Make sure it is aligned for both
00216   // the source and destination types.
00217   SDValue FIPtr = DAG.CreateStackTemporary(InVT, OutVT);
00218   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
00219   const Value *SV = PseudoSourceValue::getFixedStack(FI);
00220 
00221   // Emit a store to the stack slot.
00222   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, InOp, FIPtr, SV, 0);
00223 
00224   // Result is an extending load from the stack slot.
00225   return DAG.getExtLoad(ISD::EXTLOAD, dl, NOutVT, Store, FIPtr, SV, 0, OutVT);
00226 }
00227 
00228 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
00229   SDValue Op = GetPromotedInteger(N->getOperand(0));
00230   MVT OVT = N->getValueType(0);
00231   MVT NVT = Op.getValueType();
00232   DebugLoc dl = N->getDebugLoc();
00233 
00234   unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
00235   return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
00236                      DAG.getConstant(DiffBits, TLI.getPointerTy()));
00237 }
00238 
00239 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
00240   // The pair element type may be legal, or may not promote to the same type as
00241   // the result, for example i14 = BUILD_PAIR (i7, i7).  Handle all cases.
00242   return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(),
00243                      TLI.getTypeToTransformTo(N->getValueType(0)),
00244                      JoinIntegers(N->getOperand(0), N->getOperand(1)));
00245 }
00246 
00247 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
00248   MVT VT = N->getValueType(0);
00249   // Zero extend things like i1, sign extend everything else.  It shouldn't
00250   // matter in theory which one we pick, but this tends to give better code?
00251   unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
00252   SDValue Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
00253                                SDValue(N, 0));
00254   assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
00255   return Result;
00256 }
00257 
00258 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
00259   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
00260   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
00261            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
00262            CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
00263           "can only promote integers");
00264   MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
00265   return DAG.getConvertRndSat(OutVT, N->getOperand(0),
00266                               N->getOperand(1), N->getOperand(2),
00267                               N->getOperand(3), N->getOperand(4), CvtCode);
00268 }
00269 
00270 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
00271   // Zero extend to the promoted type and do the count there.
00272   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
00273   MVT OVT = N->getValueType(0);
00274   MVT NVT = Op.getValueType();
00275   Op = DAG.getNode(ISD::CTLZ, NVT, Op);
00276   // Subtract off the extra leading bits in the bigger type.
00277   return DAG.getNode(ISD::SUB, N->getDebugLoc(), NVT, Op,
00278                      DAG.getConstant(NVT.getSizeInBits() -
00279                                      OVT.getSizeInBits(), NVT));
00280 }
00281 
00282 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
00283   // Zero extend to the promoted type and do the count there.
00284   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
00285   return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), Op.getValueType(), Op);
00286 }
00287 
00288 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
00289   SDValue Op = GetPromotedInteger(N->getOperand(0));
00290   MVT OVT = N->getValueType(0);
00291   MVT NVT = Op.getValueType();
00292   DebugLoc dl = N->getDebugLoc();
00293   // The count is the same in the promoted type except if the original
00294   // value was zero.  This can be handled by setting the bit just off
00295   // the top of the original type.
00296   APInt TopBit(NVT.getSizeInBits(), 0);
00297   TopBit.set(OVT.getSizeInBits());
00298   Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT));
00299   return DAG.getNode(ISD::CTTZ, dl, NVT, Op);
00300 }
00301 
00302 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
00303   MVT OldVT = N->getValueType(0);
00304   SDValue OldVec = N->getOperand(0);
00305   if (getTypeAction(OldVec.getValueType()) == WidenVector)
00306     OldVec = GetWidenedVector(N->getOperand(0));
00307   unsigned OldElts = OldVec.getValueType().getVectorNumElements();
00308   DebugLoc dl = N->getDebugLoc();
00309 
00310   if (OldElts == 1) {
00311     assert(!isTypeLegal(OldVec.getValueType()) &&
00312            "Legal one-element vector of a type needing promotion!");
00313     // It is tempting to follow GetScalarizedVector by a call to
00314     // GetPromotedInteger, but this would be wrong because the
00315     // scalarized value may not yet have been processed.
00316     return DAG.getNode(ISD::ANY_EXTEND, dl, TLI.getTypeToTransformTo(OldVT),
00317                        GetScalarizedVector(OldVec));
00318   }
00319 
00320   // Convert to a vector half as long with an element type of twice the width,
00321   // for example <4 x i16> -> <2 x i32>.
00322   assert(!(OldElts & 1) && "Odd length vectors not supported!");
00323   MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
00324   assert(OldVT.isSimple() && NewVT.isSimple());
00325 
00326   SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl,
00327                                  MVT::getVectorVT(NewVT, OldElts / 2),
00328                                  OldVec);
00329 
00330   // Extract the element at OldIdx / 2 from the new vector.
00331   SDValue OldIdx = N->getOperand(1);
00332   SDValue NewIdx = DAG.getNode(ISD::SRL, dl, OldIdx.getValueType(), OldIdx,
00333                                DAG.getConstant(1, TLI.getPointerTy()));
00334   SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, NewIdx);
00335 
00336   // Select the appropriate half of the element: Lo if OldIdx was even,
00337   // Hi if it was odd.
00338   SDValue Lo = Elt;
00339   SDValue Hi = DAG.getNode(ISD::SRL, dl, NewVT, Elt,
00340                            DAG.getConstant(OldVT.getSizeInBits(),
00341                                            TLI.getPointerTy()));
00342   if (TLI.isBigEndian())
00343     std::swap(Lo, Hi);
00344 
00345   // Extend to the promoted type.
00346   SDValue Odd = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, OldIdx);
00347   SDValue Res = DAG.getNode(ISD::SELECT, dl, NewVT, Odd, Hi, Lo);
00348   return DAG.getNode(ISD::ANY_EXTEND, dl, TLI.getTypeToTransformTo(OldVT), Res);
00349 }
00350 
00351 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
00352   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
00353   unsigned NewOpc = N->getOpcode();
00354   DebugLoc dl = N->getDebugLoc();
00355 
00356   // If we're promoting a UINT to a larger size, check to see if the new node
00357   // will be legal.  If it isn't, check to see if FP_TO_SINT is legal, since
00358   // we can use that instead.  This allows us to generate better code for
00359   // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
00360   // legal, such as PowerPC.
00361   if (N->getOpcode() == ISD::FP_TO_UINT &&
00362       !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
00363       TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
00364     NewOpc = ISD::FP_TO_SINT;
00365 
00366   SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
00367 
00368   // Assert that the converted value fits in the original type.  If it doesn't
00369   // (eg: because the value being converted is too big), then the result of the
00370   // original operation was undefined anyway, so the assert is still correct.
00371   return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
00372                      ISD::AssertZext : ISD::AssertSext, dl,
00373                      NVT, Res, DAG.getValueType(N->getValueType(0)));
00374 }
00375 
00376 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
00377   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
00378   DebugLoc dl = N->getDebugLoc();
00379 
00380   if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
00381     SDValue Res = GetPromotedInteger(N->getOperand(0));
00382     assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
00383 
00384     // If the result and operand types are the same after promotion, simplify
00385     // to an in-register extension.
00386     if (NVT == Res.getValueType()) {
00387       // The high bits are not guaranteed to be anything.  Insert an extend.
00388       if (N->getOpcode() == ISD::SIGN_EXTEND)
00389         return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
00390                            DAG.getValueType(N->getOperand(0).getValueType()));
00391       if (N->getOpcode() == ISD::ZERO_EXTEND)
00392         return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
00393       assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
00394       return Res;
00395     }
00396   }
00397 
00398   // Otherwise, just extend the original operand all the way to the larger type.
00399   return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
00400 }
00401 
00402 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
00403   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
00404   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
00405   ISD::LoadExtType ExtType =
00406     ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
00407   DebugLoc dl = N->getDebugLoc();
00408   SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
00409                                N->getSrcValue(), N->getSrcValueOffset(),
00410                                N->getMemoryVT(), N->isVolatile(),
00411                                N->getAlignment());
00412 
00413   // Legalized the chain result - switch anything that used the old chain to
00414   // use the new one.
00415   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
00416   return Res;
00417 }
00418 
00419 /// Promote the overflow flag of an overflowing arithmetic node.
00420 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
00421   // Simply change the return type of the boolean result.
00422   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(1));
00423   MVT ValueVTs[] = { N->getValueType(0), NVT };
00424   SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
00425   SDValue Res = DAG.getNode(N->getOpcode(), N->getDebugLoc(),
00426                             DAG.getVTList(ValueVTs, 2), Ops, 2);
00427 
00428   // Modified the sum result - switch anything that used the old sum to use
00429   // the new one.
00430   ReplaceValueWith(SDValue(N, 0), Res);
00431 
00432   return SDValue(Res.getNode(), 1);
00433 }
00434 
00435 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
00436   if (ResNo == 1)
00437     return PromoteIntRes_Overflow(N);
00438 
00439   // The operation overflowed iff the result in the larger type is not the
00440   // sign extension of its truncation to the original type.
00441   SDValue LHS = SExtPromotedInteger(N->getOperand(0));
00442   SDValue RHS = SExtPromotedInteger(N->getOperand(1));
00443   MVT OVT = N->getOperand(0).getValueType();
00444   MVT NVT = LHS.getValueType();
00445   DebugLoc dl = N->getDebugLoc();
00446 
00447   // Do the arithmetic in the larger type.
00448   unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
00449   SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
00450 
00451   // Calculate the overflow flag: sign extend the arithmetic result from
00452   // the original type.
00453   SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
00454                             DAG.getValueType(OVT));
00455   // Overflowed if and only if this is not equal to Res.
00456   Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
00457 
00458   // Use the calculated overflow everywhere.
00459   ReplaceValueWith(SDValue(N, 1), Ofl);
00460 
00461   return Res;
00462 }
00463 
00464 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
00465   // Sign extend the input.
00466   SDValue LHS = SExtPromotedInteger(N->getOperand(0));
00467   SDValue RHS = SExtPromotedInteger(N->getOperand(1));
00468   return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
00469                      LHS.getValueType(), LHS, RHS);
00470 }
00471 
00472 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
00473   SDValue LHS = GetPromotedInteger(N->getOperand(1));
00474   SDValue RHS = GetPromotedInteger(N->getOperand(2));
00475   return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
00476                      LHS.getValueType(), N->getOperand(0),LHS,RHS);
00477 }
00478 
00479 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
00480   SDValue LHS = GetPromotedInteger(N->getOperand(2));
00481   SDValue RHS = GetPromotedInteger(N->getOperand(3));
00482   return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
00483                      LHS.getValueType(), N->getOperand(0),
00484                      N->getOperand(1), LHS, RHS, N->getOperand(4));
00485 }
00486 
00487 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
00488   MVT SVT = TLI.getSetCCResultType(N->getOperand(0).getValueType());
00489   assert(isTypeLegal(SVT) && "Illegal SetCC type!");
00490   DebugLoc dl = N->getDebugLoc();
00491 
00492   // Get the SETCC result using the canonical SETCC type.
00493   SDValue SetCC = DAG.getNode(ISD::SETCC, dl, SVT, N->getOperand(0),
00494                               N->getOperand(1), N->getOperand(2));
00495 
00496   // Convert to the expected type.
00497   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
00498   assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
00499   return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
00500 }
00501 
00502 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
00503   return DAG.getNode(ISD::SHL, N->getDebugLoc(),
00504                      TLI.getTypeToTransformTo(N->getValueType(0)),
00505                      GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
00506 }
00507 
00508 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
00509   SDValue Op = GetPromotedInteger(N->getOperand(0));
00510   return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(),
00511                      Op.getValueType(), Op, N->getOperand(1));
00512 }
00513 
00514 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
00515   // The input may have strange things in the top bits of the registers, but
00516   // these operations don't care.  They may have weird bits going out, but
00517   // that too is okay if they are integer operations.
00518   SDValue LHS = GetPromotedInteger(N->getOperand(0));
00519   SDValue RHS = GetPromotedInteger(N->getOperand(1));
00520   return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
00521                     LHS.getValueType(), LHS, RHS);
00522 }
00523 
00524 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
00525   // The input value must be properly sign extended.
00526   SDValue Res = SExtPromotedInteger(N->getOperand(0));
00527   return DAG.getNode(ISD::SRA, N->getDebugLoc(),
00528                      Res.getValueType(), Res, N->getOperand(1));
00529 }
00530 
00531 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
00532   // The input value must be properly zero extended.
00533   MVT VT = N->getValueType(0);
00534   MVT NVT = TLI.getTypeToTransformTo(VT);
00535   SDValue Res = ZExtPromotedInteger(N->getOperand(0));
00536   return DAG.getNode(ISD::SRL, N->getDebugLoc(), NVT, Res, N->getOperand(1));
00537 }
00538 
00539 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
00540   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
00541   SDValue Res;
00542 
00543   switch (getTypeAction(N->getOperand(0).getValueType())) {
00544   default: assert(0 && "Unknown type action!");
00545   case Legal:
00546   case ExpandInteger:
00547     Res = N->getOperand(0);
00548     break;
00549   case PromoteInteger:
00550     Res = GetPromotedInteger(N->getOperand(0));
00551     break;
00552   }
00553 
00554   // Truncate to NVT instead of VT
00555   return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Res);
00556 }
00557 
00558 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
00559   if (ResNo == 1)
00560     return PromoteIntRes_Overflow(N);
00561 
00562   // The operation overflowed iff the result in the larger type is not the
00563   // zero extension of its truncation to the original type.
00564   SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
00565   SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
00566   MVT OVT = N->getOperand(0).getValueType();
00567   MVT NVT = LHS.getValueType();
00568   DebugLoc dl = N->getDebugLoc();
00569 
00570   // Do the arithmetic in the larger type.
00571   unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
00572   SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
00573 
00574   // Calculate the overflow flag: zero extend the arithmetic result from
00575   // the original type.
00576   SDValue Ofl = DAG.getZeroExtendInReg(Res, OVT);
00577   // Overflowed if and only if this is not equal to Res.
00578   Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
00579 
00580   // Use the calculated overflow everywhere.
00581   ReplaceValueWith(SDValue(N, 1), Ofl);
00582 
00583   return Res;
00584 }
00585 
00586 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
00587   // Zero extend the input.
00588   SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
00589   SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
00590   return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
00591                      LHS.getValueType(), LHS, RHS);
00592 }
00593 
00594 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
00595   return DAG.getNode(ISD::UNDEF, N->getDebugLoc(),
00596                      TLI.getTypeToTransformTo(N->getValueType(0)));
00597 }
00598 
00599 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
00600   SDValue Chain = N->getOperand(0); // Get the chain.
00601   SDValue Ptr = N->getOperand(1); // Get the pointer.
00602   MVT VT = N->getValueType(0);
00603   DebugLoc dl = N->getDebugLoc();
00604 
00605   MVT RegVT = TLI.getRegisterType(VT);
00606   unsigned NumRegs = TLI.getNumRegisters(VT);
00607   // The argument is passed as NumRegs registers of type RegVT.
00608 
00609   SmallVector<SDValue, 8> Parts(NumRegs);
00610   for (unsigned i = 0; i < NumRegs; ++i) {
00611     Parts[i] = DAG.getVAArg(RegVT, Chain, Ptr, N->getOperand(2));
00612     Chain = Parts[i].getValue(1);
00613   }
00614 
00615   // Handle endianness of the load.
00616   if (TLI.isBigEndian())
00617     std::reverse(Parts.begin(), Parts.end());
00618 
00619   // Assemble the parts in the promoted type.
00620   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
00621   SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
00622   for (unsigned i = 1; i < NumRegs; ++i) {
00623     SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
00624     // Shift it to the right position and "or" it in.
00625     Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
00626                        DAG.getConstant(i * RegVT.getSizeInBits(),
00627                                        TLI.getPointerTy()));
00628     Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
00629   }
00630 
00631   // Modified the chain result - switch anything that used the old chain to
00632   // use the new one.
00633   ReplaceValueWith(SDValue(N, 1), Chain);
00634 
00635   return Res;
00636 }
00637 
00638 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
00639   assert(ResNo == 1 && "Only boolean result promotion currently supported!");
00640   return PromoteIntRes_Overflow(N);
00641 }
00642 
00643 //===----------------------------------------------------------------------===//
00644 //  Integer Operand Promotion
00645 //===----------------------------------------------------------------------===//
00646 
00647 /// PromoteIntegerOperand - This method is called when the specified operand of
00648 /// the specified node is found to need promotion.  At this point, all of the
00649 /// result types of the node are known to be legal, but other operands of the
00650 /// node may need promotion or expansion as well as the specified one.
00651 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
00652   DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
00653   SDValue Res = SDValue();
00654 
00655   if (CustomLowerResults(N, N->getOperand(OpNo).getValueType(), false))
00656     return false;
00657 
00658   switch (N->getOpcode()) {
00659     default:
00660   #ifndef NDEBUG
00661     cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
00662     N->dump(&DAG); cerr << "\n";
00663   #endif
00664     assert(0 && "Do not know how to promote this operator's operand!");
00665     abort();
00666 
00667   case ISD::ANY_EXTEND:   Res = PromoteIntOp_ANY_EXTEND(N); break;
00668   case ISD::BR_CC:        Res = PromoteIntOp_BR_CC(N, OpNo); break;
00669   case ISD::BRCOND:       Res = PromoteIntOp_BRCOND(N, OpNo); break;
00670   case ISD::BUILD_PAIR:   Res = PromoteIntOp_BUILD_PAIR(N); break;
00671   case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
00672   case ISD::CONVERT_RNDSAT:
00673                           Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
00674   case ISD::INSERT_VECTOR_ELT:
00675                           Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
00676   case ISD::MEMBARRIER:   Res = PromoteIntOp_MEMBARRIER(N); break;
00677   case ISD::SELECT:       Res = PromoteIntOp_SELECT(N, OpNo); break;
00678   case ISD::SELECT_CC:    Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
00679   case ISD::SETCC:        Res = PromoteIntOp_SETCC(N, OpNo); break;
00680   case ISD::SIGN_EXTEND:  Res = PromoteIntOp_SIGN_EXTEND(N); break;
00681   case ISD::SINT_TO_FP:   Res = PromoteIntOp_SINT_TO_FP(N); break;
00682   case ISD::STORE:        Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
00683                                                    OpNo); break;
00684   case ISD::TRUNCATE:     Res = PromoteIntOp_TRUNCATE(N); break;
00685   case ISD::UINT_TO_FP:   Res = PromoteIntOp_UINT_TO_FP(N); break;
00686   case ISD::ZERO_EXTEND:  Res = PromoteIntOp_ZERO_EXTEND(N); break;
00687 
00688   case ISD::SHL:
00689   case ISD::SRA:
00690   case ISD::SRL:
00691   case ISD::ROTL:
00692   case ISD::ROTR: Res = PromoteIntOp_Shift(N); break;
00693   }
00694 
00695   // If the result is null, the sub-method took care of registering results etc.
00696   if (!Res.getNode()) return false;
00697 
00698   // If the result is N, the sub-method updated N in place.  Tell the legalizer
00699   // core about this.
00700   if (Res.getNode() == N)
00701     return true;
00702 
00703   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
00704          "Invalid operand expansion");
00705 
00706   ReplaceValueWith(SDValue(N, 0), Res);
00707   return false;
00708 }
00709 
00710 /// PromoteSetCCOperands - Promote the operands of a comparison.  This code is
00711 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
00712 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
00713                                             ISD::CondCode CCCode) {
00714   // We have to insert explicit sign or zero extends.  Note that we could
00715   // insert sign extends for ALL conditions, but zero extend is cheaper on
00716   // many machines (an AND instead of two shifts), so prefer it.
00717   switch (CCCode) {
00718   default: assert(0 && "Unknown integer comparison!");
00719   case ISD::SETEQ:
00720   case ISD::SETNE:
00721   case ISD::SETUGE:
00722   case ISD::SETUGT:
00723   case ISD::SETULE:
00724   case ISD::SETULT:
00725     // ALL of these operations will work if we either sign or zero extend
00726     // the operands (including the unsigned comparisons!).  Zero extend is
00727     // usually a simpler/cheaper operation, so prefer it.
00728     NewLHS = ZExtPromotedInteger(NewLHS);
00729     NewRHS = ZExtPromotedInteger(NewRHS);
00730     break;
00731   case ISD::SETGE:
00732   case ISD::SETGT:
00733   case ISD::SETLT:
00734   case ISD::SETLE:
00735     NewLHS = SExtPromotedInteger(NewLHS);
00736     NewRHS = SExtPromotedInteger(NewRHS);
00737     break;
00738   }
00739 }
00740 
00741 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
00742   SDValue Op = GetPromotedInteger(N->getOperand(0));
00743   return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
00744 }
00745 
00746 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
00747   assert(OpNo == 2 && "Don't know how to promote this operand!");
00748 
00749   SDValue LHS = N->getOperand(2);
00750   SDValue RHS = N->getOperand(3);
00751   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
00752 
00753   // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
00754   // legal types.
00755   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
00756                                 N->getOperand(1), LHS, RHS, N->getOperand(4));
00757 }
00758 
00759 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
00760   assert(OpNo == 1 && "only know how to promote condition");
00761 
00762   // Promote all the way up to the canonical SetCC type.
00763   MVT SVT = TLI.getSetCCResultType(MVT::Other);
00764   SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
00765 
00766   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
00767   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
00768                                 N->getOperand(2));
00769 }
00770 
00771 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
00772   // Since the result type is legal, the operands must promote to it.
00773   MVT OVT = N->getOperand(0).getValueType();
00774   SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
00775   SDValue Hi = GetPromotedInteger(N->getOperand(1));
00776   assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
00777   DebugLoc dl = N->getDebugLoc();
00778 
00779   Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
00780                    DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy()));
00781   return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
00782 }
00783 
00784 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
00785   // The vector type is legal but the element type is not.  This implies
00786   // that the vector is a power-of-two in length and that the element
00787   // type does not have a strange size (eg: it is not i1).
00788   MVT VecVT = N->getValueType(0);
00789   unsigned NumElts = VecVT.getVectorNumElements();
00790   assert(!(NumElts & 1) && "Legal vector of one illegal element?");
00791   DebugLoc dl = N->getDebugLoc();
00792 
00793   // Build a vector of half the length out of elements of twice the bitwidth.
00794   // For example <4 x i16> -> <2 x i32>.
00795   MVT OldVT = N->getOperand(0).getValueType();
00796   MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
00797   assert(OldVT.isSimple() && NewVT.isSimple());
00798 
00799   std::vector<SDValue> NewElts;
00800   NewElts.reserve(NumElts/2);
00801 
00802   for (unsigned i = 0; i < NumElts; i += 2) {
00803     // Combine two successive elements into one promoted element.
00804     SDValue Lo = N->getOperand(i);
00805     SDValue Hi = N->getOperand(i+1);
00806     if (TLI.isBigEndian())
00807       std::swap(Lo, Hi);
00808     NewElts.push_back(JoinIntegers(Lo, Hi));
00809   }
00810 
00811   SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
00812                                  MVT::getVectorVT(NewVT, NewElts.size()),
00813                                  &NewElts[0], NewElts.size());
00814 
00815   // Convert the new vector to the old vector type.
00816   return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec);
00817 }
00818 
00819 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
00820   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
00821   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
00822            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
00823            CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
00824            "can only promote integer arguments");
00825   SDValue InOp = GetPromotedInteger(N->getOperand(0));
00826   return DAG.getConvertRndSat(N->getValueType(0), InOp,
00827                               N->getOperand(1), N->getOperand(2),
00828                               N->getOperand(3), N->getOperand(4), CvtCode);
00829 }
00830 
00831 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
00832                                                          unsigned OpNo) {
00833   if (OpNo == 1) {
00834     // Promote the inserted value.  This is valid because the type does not
00835     // have to match the vector element type.
00836 
00837     // Check that any extra bits introduced will be truncated away.
00838     assert(N->getOperand(1).getValueType().getSizeInBits() >=
00839            N->getValueType(0).getVectorElementType().getSizeInBits() &&
00840            "Type of inserted value narrower than vector element type!");
00841     return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
00842                                   GetPromotedInteger(N->getOperand(1)),
00843                                   N->getOperand(2));
00844   }
00845 
00846   assert(OpNo == 2 && "Different operand and result vector types?");
00847 
00848   // Promote the index.
00849   SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
00850   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
00851                                 N->getOperand(1), Idx);
00852 }
00853 
00854 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
00855   SDValue NewOps[6];
00856   NewOps[0] = N->getOperand(0);
00857   for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
00858     SDValue Flag = GetPromotedInteger(N->getOperand(i));
00859     NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
00860   }
00861   return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
00862                                 array_lengthof(NewOps));
00863 }
00864 
00865 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
00866   assert(OpNo == 0 && "Only know how to promote condition");
00867 
00868   // Promote all the way up to the canonical SetCC type.
00869   MVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
00870   SDValue Cond = PromoteTargetBoolean(N->getOperand(0), SVT);
00871 
00872   return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
00873                                 N->getOperand(1), N->getOperand(2));
00874 }
00875 
00876 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
00877   assert(OpNo == 0 && "Don't know how to promote this operand!");
00878 
00879   SDValue LHS = N->getOperand(0);
00880   SDValue RHS = N->getOperand(1);
00881   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
00882 
00883   // The CC (#4) and the possible return values (#2 and #3) have legal types.
00884   return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
00885                                 N->getOperand(3), N->getOperand(4));
00886 }
00887 
00888 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
00889   assert(OpNo == 0 && "Don't know how to promote this operand!");
00890 
00891   SDValue LHS = N->getOperand(0);
00892   SDValue RHS = N->getOperand(1);
00893   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
00894 
00895   // The CC (#2) is always legal.
00896   return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
00897 }
00898 
00899 SDValue DAGTypeLegalizer::PromoteIntOp_Shift(SDNode *N) {
00900   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
00901                                 ZExtPromotedInteger(N->getOperand(1)));
00902 }
00903 
00904 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
00905   SDValue Op = GetPromotedInteger(N->getOperand(0));
00906   DebugLoc dl = N->getDebugLoc();
00907   Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
00908   return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
00909                      Op, DAG.getValueType(N->getOperand(0).getValueType()));
00910 }
00911 
00912 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
00913   return DAG.UpdateNodeOperands(SDValue(N, 0),
00914                                 SExtPromotedInteger(N->getOperand(0)));
00915 }
00916 
00917 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
00918   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
00919   SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
00920   int SVOffset = N->getSrcValueOffset();
00921   unsigned Alignment = N->getAlignment();
00922   bool isVolatile = N->isVolatile();
00923   DebugLoc dl = N->getDebugLoc();
00924 
00925   SDValue Val = GetPromotedInteger(N->getValue());  // Get promoted value.
00926 
00927   // Truncate the value and store the result.
00928   return DAG.getTruncStore(Ch, dl, Val, Ptr, N->getSrcValue(),
00929                            SVOffset, N->getMemoryVT(),
00930                            isVolatile, Alignment);
00931 }
00932 
00933 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
00934   SDValue Op = GetPromotedInteger(N->getOperand(0));
00935   return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), Op);
00936 }
00937 
00938 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
00939   return DAG.UpdateNodeOperands(SDValue(N, 0),
00940                                 ZExtPromotedInteger(N->getOperand(0)));
00941 }
00942 
00943 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
00944   SDValue Op = GetPromotedInteger(N->getOperand(0));
00945   Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
00946   return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
00947 }
00948 
00949 
00950 //===----------------------------------------------------------------------===//
00951 //  Integer Result Expansion
00952 //===----------------------------------------------------------------------===//
00953 
00954 /// ExpandIntegerResult - This method is called when the specified result of the
00955 /// specified node is found to need expansion.  At this point, the node may also
00956 /// have invalid operands or may have other results that need promotion, we just
00957 /// know that (at least) one result needs expansion.
00958 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
00959   DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
00960   SDValue Lo, Hi;
00961   Lo = Hi = SDValue();
00962 
00963   // See if the target wants to custom expand this node.
00964   if (CustomLowerResults(N, N->getValueType(ResNo), true))
00965     return;
00966 
00967   switch (N->getOpcode()) {
00968   default:
00969 #ifndef NDEBUG
00970     cerr << "ExpandIntegerResult #" << ResNo << ": ";
00971     N->dump(&DAG); cerr << "\n";
00972 #endif
00973     assert(0 && "Do not know how to expand the result of this operator!");
00974     abort();
00975 
00976   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
00977   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
00978   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
00979   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
00980 
00981   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
00982   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
00983   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
00984   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
00985   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
00986 
00987   case ISD::ANY_EXTEND:  ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
00988   case ISD::AssertSext:  ExpandIntRes_AssertSext(N, Lo, Hi); break;
00989   case ISD::AssertZext:  ExpandIntRes_AssertZext(N, Lo, Hi); break;
00990   case ISD::BSWAP:       ExpandIntRes_BSWAP(N, Lo, Hi); break;
00991   case ISD::Constant:    ExpandIntRes_Constant(N, Lo, Hi); break;
00992   case ISD::CTLZ:        ExpandIntRes_CTLZ(N, Lo, Hi); break;
00993   case ISD::CTPOP:       ExpandIntRes_CTPOP(N, Lo, Hi); break;
00994   case ISD::CTTZ:        ExpandIntRes_CTTZ(N, Lo, Hi); break;
00995   case ISD::FP_TO_SINT:  ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
00996   case ISD::FP_TO_UINT:  ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
00997   case ISD::LOAD:        ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
00998   case ISD::MUL:         ExpandIntRes_MUL(N, Lo, Hi); break;
00999   case ISD::SDIV:        ExpandIntRes_SDIV(N, Lo, Hi); break;
01000   case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
01001   case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
01002   case ISD::SREM:        ExpandIntRes_SREM(N, Lo, Hi); break;
01003   case ISD::TRUNCATE:    ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
01004   case ISD::UDIV:        ExpandIntRes_UDIV(N, Lo, Hi); break;
01005   case ISD::UREM:        ExpandIntRes_UREM(N, Lo, Hi); break;
01006   case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
01007 
01008   case ISD::AND:
01009   case ISD::OR:
01010   case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
01011 
01012   case ISD::ADD:
01013   case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
01014 
01015   case ISD::ADDC:
01016   case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
01017 
01018   case ISD::ADDE:
01019   case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
01020 
01021   case ISD::SHL:
01022   case ISD::SRA:
01023   case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
01024   }
01025 
01026   // If Lo/Hi is null, the sub-method took care of registering results etc.
01027   if (Lo.getNode())
01028     SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
01029 }
01030 
01031 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
01032 /// and the shift amount is a constant 'Amt'.  Expand the operation.
01033 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
01034                                              SDValue &Lo, SDValue &Hi) {
01035   DebugLoc dl = N->getDebugLoc();
01036   // Expand the incoming operand to be shifted, so that we have its parts
01037   SDValue InL, InH;
01038   GetExpandedInteger(N->getOperand(0), InL, InH);
01039 
01040   MVT NVT = InL.getValueType();
01041   unsigned VTBits = N->getValueType(0).getSizeInBits();
01042   unsigned NVTBits = NVT.getSizeInBits();
01043   MVT ShTy = N->getOperand(1).getValueType();
01044 
01045   if (N->getOpcode() == ISD::SHL) {
01046     if (Amt > VTBits) {
01047       Lo = Hi = DAG.getConstant(0, NVT);
01048     } else if (Amt > NVTBits) {
01049       Lo = DAG.getConstant(0, NVT);
01050       Hi = DAG.getNode(ISD::SHL, dl,
01051                        NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
01052     } else if (Amt == NVTBits) {
01053       Lo = DAG.getConstant(0, NVT);
01054       Hi = InL;
01055     } else if (Amt == 1 &&
01056                TLI.isOperationLegalOrCustom(ISD::ADDC,
01057                                             TLI.getTypeToExpandTo(NVT))) {
01058       // Emit this X << 1 as X+X.
01059       SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
01060       SDValue LoOps[2] = { InL, InL };
01061       Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
01062       SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
01063       Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
01064     } else {
01065       Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Amt, ShTy));
01066       Hi = DAG.getNode(ISD::OR, dl, NVT,
01067                        DAG.getNode(ISD::SHL, dl, NVT, InH,
01068                                    DAG.getConstant(Amt, ShTy)),
01069                        DAG.getNode(ISD::SRL, dl, NVT, InL,
01070                                    DAG.getConstant(NVTBits-Amt, ShTy)));
01071     }
01072     return;
01073   }
01074 
01075   if (N->getOpcode() == ISD::SRL) {
01076     if (Amt > VTBits) {
01077       Lo = DAG.getConstant(0, NVT);
01078       Hi = DAG.getConstant(0, NVT);
01079     } else if (Amt > NVTBits) {
01080       Lo = DAG.getNode(ISD::SRL, dl,
01081                        NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
01082       Hi = DAG.getConstant(0, NVT);
01083     } else if (Amt == NVTBits) {
01084       Lo = InH;
01085       Hi = DAG.getConstant(0, NVT);
01086     } else {
01087       Lo = DAG.getNode(ISD::OR, dl, NVT,
01088                        DAG.getNode(ISD::SRL, dl, NVT, InL,
01089                                    DAG.getConstant(Amt, ShTy)),
01090                        DAG.getNode(ISD::SHL, dl, NVT, InH,
01091                                    DAG.getConstant(NVTBits-Amt, ShTy)));
01092       Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
01093     }
01094     return;
01095   }
01096 
01097   assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
01098   if (Amt > VTBits) {
01099     Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
01100                           DAG.getConstant(NVTBits-1, ShTy));
01101   } else if (Amt > NVTBits) {
01102     Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
01103                      DAG.getConstant(Amt-NVTBits, ShTy));
01104     Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
01105                      DAG.getConstant(NVTBits-1, ShTy));
01106   } else if (Amt == NVTBits) {
01107     Lo = InH;
01108     Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
01109                      DAG.getConstant(NVTBits-1, ShTy));
01110   } else {
01111     Lo = DAG.getNode(ISD::OR, NVT,
01112                      DAG.getNode(ISD::SRL, dl, NVT, InL,
01113                                  DAG.getConstant(Amt, ShTy)),
01114                      DAG.getNode(ISD::SHL, dl, NVT, InH,
01115                                  DAG.getConstant(NVTBits-Amt, ShTy)));
01116     Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
01117   }
01118 }
01119 
01120 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
01121 /// this shift based on knowledge of the high bit of the shift amount.  If we
01122 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
01123 /// shift amount.
01124 bool DAGTypeLegalizer::
01125 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
01126   SDValue Amt = N->getOperand(1);
01127   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
01128   MVT ShTy = Amt.getValueType();
01129   unsigned ShBits = ShTy.getSizeInBits();
01130   unsigned NVTBits = NVT.getSizeInBits();
01131   assert(isPowerOf2_32(NVTBits) &&
01132          "Expanded integer type size not a power of two!");
01133   DebugLoc dl = N->getDebugLoc();
01134 
01135   APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
01136   APInt KnownZero, KnownOne;
01137   DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
01138 
01139   // If we don't know anything about the high bits, exit.
01140   if (((KnownZero|KnownOne) & HighBitMask) == 0)
01141     return false;
01142 
01143   // Get the incoming operand to be shifted.
01144   SDValue InL, InH;
01145   GetExpandedInteger(N->getOperand(0), InL, InH);
01146 
01147   // If we know that any of the high bits of the shift amount are one, then we
01148   // can do this as a couple of simple shifts.
01149   if (KnownOne.intersects(HighBitMask)) {
01150     // Mask out the high bit, which we know is set.
01151     Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
01152                       DAG.getConstant(~HighBitMask, ShTy));
01153 
01154     switch (N->getOpcode()) {
01155     default: assert(0 && "Unknown shift");
01156     case ISD::SHL:
01157       Lo = DAG.getConstant(0, NVT);              // Low part is zero.
01158       Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
01159       return true;
01160     case ISD::SRL:
01161       Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
01162       Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
01163       return true;
01164     case ISD::SRA:
01165       Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,       // Sign extend high part.
01166                        DAG.getConstant(NVTBits-1, ShTy));
01167       Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
01168       return true;
01169     }
01170   }
01171 
01172 #if 0
01173   // FIXME: This code is broken for shifts with a zero amount!
01174   // If we know that all of the high bits of the shift amount are zero, then we
01175   // can do this as a couple of simple shifts.
01176   if ((KnownZero & HighBitMask) == HighBitMask) {
01177     // Compute 32-amt.
01178     SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
01179                                  DAG.getConstant(NVTBits, ShTy),
01180                                  Amt);
01181     unsigned Op1, Op2;
01182     switch (N->getOpcode()) {
01183     default: assert(0 && "Unknown shift");
01184     case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
01185     case ISD::SRL:
01186     case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
01187     }
01188 
01189     Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
01190     Hi = DAG.getNode(ISD::OR, NVT,
01191                      DAG.getNode(Op1, NVT, InH, Amt),
01192                      DAG.getNode(Op2, NVT, InL, Amt2));
01193     return true;
01194   }
01195 #endif
01196 
01197   return false;
01198 }
01199 
01200 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
01201                                            SDValue &Lo, SDValue &Hi) {
01202   DebugLoc dl = N->getDebugLoc();
01203   // Expand the subcomponents.
01204   SDValue LHSL, LHSH, RHSL, RHSH;
01205   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
01206   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
01207 
01208   MVT NVT = LHSL.getValueType();
01209   SDValue LoOps[2] = { LHSL, RHSL };
01210   SDValue HiOps[3] = { LHSH, RHSH };
01211 
01212   // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
01213   // them.  TODO: Teach operation legalization how to expand unsupported
01214   // ADDC/ADDE/SUBC/SUBE.  The problem is that these operations generate
01215   // a carry of type MVT::Flag, but there doesn't seem to be any way to
01216   // generate a value of this type in the expanded code sequence.
01217   bool hasCarry =
01218     TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
01219                                    ISD::ADDC : ISD::SUBC,
01220                                  TLI.getTypeToExpandTo(NVT));
01221 
01222   if (hasCarry) {
01223     SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
01224     if (N->getOpcode() == ISD::ADD) {
01225       Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
01226       HiOps[2] = Lo.getValue(1);
01227       Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
01228     } else {
01229       Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
01230       HiOps[2] = Lo.getValue(1);
01231       Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
01232     }
01233   } else {
01234     if (N->getOpcode() == ISD::ADD) {
01235       Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
01236       Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
01237       SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[0],
01238                                   ISD::SETULT);
01239       SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
01240                                    DAG.getConstant(1, NVT),
01241                                    DAG.getConstant(0, NVT));
01242       SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[1],
01243                                   ISD::SETULT);
01244       SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
01245                                    DAG.getConstant(1, NVT), Carry1);
01246       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
01247     } else {
01248       Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
01249       Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
01250       SDValue Cmp =
01251         DAG.getSetCC(TLI.getSetCCResultType(LoOps[0].getValueType()),
01252                      LoOps[0], LoOps[1], ISD::SETULT);
01253       SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
01254                                    DAG.getConstant(1, NVT),
01255                                    DAG.getConstant(0, NVT));
01256       Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
01257     }
01258   }
01259 }
01260 
01261 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
01262                                             SDValue &Lo, SDValue &Hi) {
01263   // Expand the subcomponents.
01264   SDValue LHSL, LHSH, RHSL, RHSH;
01265   DebugLoc dl = N->getDebugLoc();
01266   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
01267   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
01268   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
01269   SDValue LoOps[2] = { LHSL, RHSL };
01270   SDValue HiOps[3] = { LHSH, RHSH };
01271 
01272   if (N->getOpcode() == ISD::ADDC) {
01273     Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
01274     HiOps[2] = Lo.getValue(1);
01275     Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
01276   } else {
01277     Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
01278     HiOps[2] = Lo.getValue(1);
01279     Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
01280   }
01281 
01282   // Legalized the flag result - switch anything that used the old flag to
01283   // use the new one.
01284   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
01285 }
01286 
01287 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
01288                                             SDValue &Lo, SDValue &Hi) {
01289   // Expand the subcomponents.
01290   SDValue LHSL, LHSH, RHSL, RHSH;
01291   DebugLoc dl = N->getDebugLoc();
01292   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
01293   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
01294   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
01295   SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
01296   SDValue HiOps[3] = { LHSH, RHSH };
01297 
01298   Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps, 3);
01299   HiOps[2] = Lo.getValue(1);
01300   Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps, 3);
01301 
01302   // Legalized the flag result - switch anything that used the old flag to
01303   // use the new one.
01304   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
01305 }
01306 
01307 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
01308                                                SDValue &Lo, SDValue &Hi) {
01309   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
01310   DebugLoc dl = N->getDebugLoc();
01311   SDValue Op = N->getOperand(0);
01312   if (Op.getValueType().bitsLE(NVT)) {
01313     // The low part is any extension of the input (which degenerates to a copy).
01314     Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
01315     Hi = DAG.getNode(ISD::UNDEF, dl, NVT);   // The high part is undefined.
01316   } else {
01317     // For example, extension of an i48 to an i64.  The operand type necessarily
01318     // promotes to the result type, so will end up being expanded too.
01319     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
01320            "Only know how to promote this result!");
01321     SDValue Res = GetPromotedInteger(Op);
01322     assert(Res.getValueType() == N->getValueType(0) &&
01323            "Operand over promoted?");
01324     // Split the promoted operand.  This will simplify when it is expanded.
01325     SplitInteger(Res, Lo, Hi);
01326   }
01327 }
01328 
01329 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
01330                                                SDValue &Lo, SDValue &Hi) {
01331   DebugLoc dl = N->getDebugLoc();
01332   GetExpandedInteger(N->getOperand(0), Lo, Hi);
01333   MVT NVT = Lo.getValueType();
01334   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
01335   unsigned NVTBits = NVT.getSizeInBits();
01336   unsigned EVTBits = EVT.getSizeInBits();
01337 
01338   if (NVTBits < EVTBits) {
01339     Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
01340                      DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
01341   } else {
01342     Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
01343     // The high part replicates the sign bit of Lo, make it explicit.
01344     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
01345                      DAG.getConstant(NVTBits-1, TLI.getPointerTy()));
01346   }
01347 }
01348 
01349 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
01350                                                SDValue &Lo, SDValue &Hi) {
01351   DebugLoc dl = N->getDebugLoc();
01352   GetExpandedInteger(N->getOperand(0), Lo, Hi);
01353   MVT NVT = Lo.getValueType();
01354   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
01355   unsigned NVTBits = NVT.getSizeInBits();
01356   unsigned EVTBits = EVT.getSizeInBits();
01357 
01358   if (NVTBits < EVTBits) {
01359     Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
01360                      DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
01361   } else {
01362     Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
01363     // The high part must be zero, make it explicit.
01364     Hi = DAG.getConstant(0, NVT);
01365   }
01366 }
01367 
01368 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
01369                                           SDValue &Lo, SDValue &Hi) {
01370   DebugLoc dl = N->getDebugLoc();
01371   GetExpandedInteger(N->getOperand(0), Hi, Lo);  // Note swapped operands.
01372   Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
01373   Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
01374 }
01375 
01376 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
01377                                              SDValue &Lo, SDValue &Hi) {
01378   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
01379   unsigned NBitWidth = NVT.getSizeInBits();
01380   const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
01381   Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
01382   Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
01383 }
01384 
01385 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
01386                                          SDValue &Lo, SDValue &Hi) {
01387   DebugLoc dl = N->getDebugLoc();
01388   // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
01389   GetExpandedInteger(N->getOperand(0), Lo, Hi);
01390   MVT NVT = Lo.getValueType();
01391 
01392   SDValue HiNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Hi,
01393                                    DAG.getConstant(0, NVT), ISD::SETNE);
01394 
01395   SDValue LoLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
01396   SDValue HiLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
01397 
01398   Lo = DAG.getNode(ISD::SELECT, dl, NVT, HiNotZero, HiLZ,
01399                    DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
01400                                DAG.getConstant(NVT.getSizeInBits(), NVT)));
01401   Hi = DAG.getConstant(0, NVT);
01402 }
01403 
01404 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
01405                                           SDValue &Lo, SDValue &Hi) {
01406   DebugLoc dl = N->getDebugLoc();
01407   // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
01408   GetExpandedInteger(N->getOperand(0), Lo, Hi);
01409   MVT NVT = Lo.getValueType();
01410   Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
01411                    DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
01412   Hi = DAG.getConstant(0, NVT);
01413 }
01414 
01415 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
01416                                          SDValue &Lo, SDValue &Hi) {
01417   DebugLoc dl = N->getDebugLoc();
01418   // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
01419   GetExpandedInteger(N->getOperand(0), Lo, Hi);
01420   MVT NVT = Lo.getValueType();
01421 
01422   SDValue LoNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo,
01423                                    DAG.getConstant(0, NVT), ISD::SETNE);
01424 
01425   SDValue LoLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
01426   SDValue HiLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
01427 
01428   Lo = DAG.getNode(ISD::SELECT, dl, NVT, LoNotZero, LoLZ,
01429                    DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
01430                                DAG.getConstant(NVT.getSizeInBits(), NVT)));
01431   Hi = DAG.getConstant(0, NVT);
01432 }
01433 
01434 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
01435                                                SDValue &Hi) {
01436   DebugLoc dl = N->getDebugLoc();
01437   MVT VT = N->getValueType(0);
01438   SDValue Op = N->getOperand(0);
01439   RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
01440   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
01441   SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*irrelevant*/, dl), Lo, Hi);
01442 }
01443 
01444 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
01445                                                SDValue &Hi) {
01446   DebugLoc dl = N->getDebugLoc();
01447   MVT VT = N->getValueType(0);
01448   SDValue Op = N->getOperand(0);
01449   RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
01450   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
01451   SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*irrelevant*/, dl), Lo, Hi);
01452 }
01453 
01454 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
01455                                          SDValue &Lo, SDValue &Hi) {
01456   if (ISD::isNormalLoad(N)) {
01457     ExpandRes_NormalLoad(N, Lo, Hi);
01458     return;
01459   }
01460 
01461   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
01462 
01463   MVT VT = N->getValueType(0);
01464   MVT NVT = TLI.getTypeToTransformTo(VT);
01465   SDValue Ch  = N->getChain();
01466   SDValue Ptr = N->getBasePtr();
01467   ISD::LoadExtType ExtType = N->getExtensionType();
01468   int SVOffset = N->getSrcValueOffset();
01469   unsigned Alignment = N->getAlignment();
01470   bool isVolatile = N->isVolatile();
01471   DebugLoc dl = N->getDebugLoc();
01472 
01473   assert(NVT.isByteSized() && "Expanded type not byte sized!");
01474 
01475   if (N->getMemoryVT().bitsLE(NVT)) {
01476     MVT EVT = N->getMemoryVT();
01477 
01478     Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
01479                         EVT, isVolatile, Alignment);
01480 
01481     // Remember the chain.
01482     Ch = Lo.getValue(1);
01483 
01484     if (ExtType == ISD::SEXTLOAD) {
01485       // The high part is obtained by SRA'ing all but one of the bits of the
01486       // lo part.
01487       unsigned LoSize = Lo.getValueType().getSizeInBits();
01488       Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
01489                        DAG.getConstant(LoSize-1, TLI.getPointerTy()));
01490     } else if (ExtType == ISD::ZEXTLOAD) {
01491       // The high part is just a zero.
01492       Hi = DAG.getConstant(0, NVT);
01493     } else {
01494       assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
01495       // The high part is undefined.
01496       Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
01497     }
01498   } else if (TLI.isLittleEndian()) {
01499     // Little-endian - low bits are at low addresses.
01500     Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getSrcValue(), SVOffset,
01501                      isVolatile, Alignment);
01502 
01503     unsigned ExcessBits =
01504       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
01505     MVT NEVT = MVT::getIntegerVT(ExcessBits);
01506 
01507     // Increment the pointer to the other half.
01508     unsigned IncrementSize = NVT.getSizeInBits()/8;
01509     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
01510                       DAG.getIntPtrConstant(IncrementSize));
01511     Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(),
01512                         SVOffset+IncrementSize, NEVT,
01513                         isVolatile, MinAlign(Alignment, IncrementSize));
01514 
01515     // Build a factor node to remember that this load is independent of the
01516     // other one.
01517     Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
01518                      Hi.getValue(1));
01519   } else {
01520     // Big-endian - high bits are at low addresses.  Favor aligned loads at
01521     // the cost of some bit-fiddling.
01522     MVT EVT = N->getMemoryVT();
01523     unsigned EBytes = EVT.getStoreSizeInBits()/8;
01524     unsigned IncrementSize = NVT.getSizeInBits()/8;
01525     unsigned ExcessBits = (EBytes - IncrementSize)*8;
01526 
01527     // Load both the high bits and maybe some of the low bits.
01528     Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
01529                         MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
01530                         isVolatile, Alignment);
01531 
01532     // Increment the pointer to the other half.
01533     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
01534                       DAG.getIntPtrConstant(IncrementSize));
01535     // Load the rest of the low bits.
01536     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr, N->getSrcValue(),
01537                         SVOffset+IncrementSize,
01538                         MVT::getIntegerVT(ExcessBits),
01539                         isVolatile, MinAlign(Alignment, IncrementSize));
01540 
01541     // Build a factor node to remember that this load is independent of the
01542     // other one.
01543     Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
01544                      Hi.getValue(1));
01545 
01546     if (ExcessBits < NVT.getSizeInBits()) {
01547       // Transfer low bits from the bottom of Hi to the top of Lo.
01548       Lo = DAG.getNode(ISD::OR, dl, NVT, Lo,
01549                        DAG.getNode(ISD::SHL, dl, NVT, Hi,
01550                                    DAG.getConstant(ExcessBits,
01551                                                    TLI.getPointerTy())));
01552       // Move high bits to the right position in Hi.
01553       Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl,
01554                        NVT, Hi,
01555                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
01556                                        TLI.getPointerTy()));
01557     }
01558   }
01559 
01560   // Legalized the chain result - switch anything that used the old chain to
01561   // use the new one.
01562   ReplaceValueWith(SDValue(N, 1), Ch);
01563 }
01564 
01565 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
01566                                             SDValue &Lo, SDValue &Hi) {
01567   DebugLoc dl = N->getDebugLoc();
01568   SDValue LL, LH, RL, RH;
01569   GetExpandedInteger(N->getOperand(0), LL, LH);
01570   GetExpandedInteger(N->getOperand(1), RL, RH);
01571   Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
01572   Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
01573 }
01574 
01575 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
01576                                         SDValue &Lo, SDValue &Hi) {
01577   MVT VT = N->getValueType(0);
01578   MVT NVT = TLI.getTypeToTransformTo(VT);
01579   DebugLoc dl = N->getDebugLoc();
01580 
01581   bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
01582   bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
01583   bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
01584   bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
01585   if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
01586     SDValue LL, LH, RL, RH;
01587     GetExpandedInteger(N->getOperand(0), LL, LH);
01588     GetExpandedInteger(N->getOperand(1), RL, RH);
01589     unsigned OuterBitSize = VT.getSizeInBits();
01590     unsigned InnerBitSize = NVT.getSizeInBits();
01591     unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
01592     unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
01593 
01594     APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
01595     if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
01596         DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
01597       // The inputs are both zero-extended.
01598       if (HasUMUL_LOHI) {
01599         // We can emit a umul_lohi.
01600         Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
01601         Hi = SDValue(Lo.getNode(), 1);
01602         return;
01603       }
01604       if (HasMULHU) {
01605         // We can emit a mulhu+mul.
01606         Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
01607         Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
01608         return;
01609       }
01610     }
01611     if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
01612       // The input values are both sign-extended.
01613       if (HasSMUL_LOHI) {
01614         // We can emit a smul_lohi.
01615         Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
01616         Hi = SDValue(Lo.getNode(), 1);
01617         return;
01618       }
01619       if (HasMULHS) {
01620         // We can emit a mulhs+mul.
01621         Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
01622         Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
01623         return;
01624       }
01625     }
01626     if (HasUMUL_LOHI) {
01627       // Lo,Hi = umul LHS, RHS.
01628       SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
01629                                        DAG.getVTList(NVT, NVT), LL, RL);
01630       Lo = UMulLOHI;
01631       Hi = UMulLOHI.getValue(1);
01632       RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
01633       LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
01634       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
01635       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
01636       return;
01637     }
01638     if (HasMULHU) {
01639       Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
01640       Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
01641       RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
01642       LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
01643       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
01644       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
01645       return;
01646     }
01647   }
01648 
01649   // If nothing else, we can make a libcall.
01650   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01651   if (VT == MVT::i16)
01652     LC = RTLIB::MUL_I16;
01653   else if (VT == MVT::i32)
01654     LC = RTLIB::MUL_I32;
01655   else if (VT == MVT::i64)
01656     LC = RTLIB::MUL_I64;
01657   else if (VT == MVT::i128)
01658     LC = RTLIB::MUL_I128;
01659   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
01660 
01661   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01662   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*irrelevant*/, dl), Lo, Hi);
01663 }
01664 
01665 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
01666                                          SDValue &Lo, SDValue &Hi) {
01667   MVT VT = N->getValueType(0);
01668   DebugLoc dl = N->getDebugLoc();
01669 
01670   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01671   if (VT == MVT::i32)
01672     LC = RTLIB::SDIV_I32;
01673   else if (VT == MVT::i64)
01674     LC = RTLIB::SDIV_I64;
01675   else if (VT == MVT::i128)
01676     LC = RTLIB::SDIV_I128;
01677   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
01678 
01679   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01680   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
01681 }
01682 
01683 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
01684                                           SDValue &Lo, SDValue &Hi) {
01685   MVT VT = N->getValueType(0);
01686   DebugLoc dl = N->getDebugLoc();
01687 
01688   // If we can emit an efficient shift operation, do so now.  Check to see if
01689   // the RHS is a constant.
01690   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
01691     return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
01692 
01693   // If we can determine that the high bit of the shift is zero or one, even if
01694   // the low bits are variable, emit this shift in an optimized form.
01695   if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
01696     return;
01697 
01698   // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
01699   unsigned PartsOpc;
01700   if (N->getOpcode() == ISD::SHL) {
01701     PartsOpc = ISD::SHL_PARTS;
01702   } else if (N->getOpcode() == ISD::SRL) {
01703     PartsOpc = ISD::SRL_PARTS;
01704   } else {
01705     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
01706     PartsOpc = ISD::SRA_PARTS;
01707   }
01708 
01709   // Next check to see if the target supports this SHL_PARTS operation or if it
01710   // will custom expand it.
01711   MVT NVT = TLI.getTypeToTransformTo(VT);
01712   TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
01713   if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
01714       Action == TargetLowering::Custom) {
01715     // Expand the subcomponents.
01716     SDValue LHSL, LHSH;
01717     GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
01718 
01719     SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
01720     MVT VT = LHSL.getValueType();
01721     Lo = DAG.getNode(PartsOpc, dl, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
01722     Hi = Lo.getValue(1);
01723     return;
01724   }
01725 
01726   // Otherwise, emit a libcall.
01727   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01728   bool isSigned;
01729   if (N->getOpcode() == ISD::SHL) {
01730     isSigned = false; /*sign irrelevant*/
01731     if (VT == MVT::i16)
01732       LC = RTLIB::SHL_I16;
01733     else if (VT == MVT::i32)
01734       LC = RTLIB::SHL_I32;
01735     else if (VT == MVT::i64)
01736       LC = RTLIB::SHL_I64;
01737     else if (VT == MVT::i128)
01738       LC = RTLIB::SHL_I128;
01739   } else if (N->getOpcode() == ISD::SRL) {
01740     isSigned = false;
01741     if (VT == MVT::i16)
01742       LC = RTLIB::SRL_I16;
01743     else if (VT == MVT::i32)
01744       LC = RTLIB::SRL_I32;
01745     else if (VT == MVT::i64)
01746       LC = RTLIB::SRL_I64;
01747     else if (VT == MVT::i128)
01748       LC = RTLIB::SRL_I128;
01749   } else {
01750     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
01751     isSigned = true;
01752     if (VT == MVT::i16)
01753       LC = RTLIB::SRA_I16;
01754     else if (VT == MVT::i32)
01755       LC = RTLIB::SRA_I32;
01756     else if (VT == MVT::i64)
01757       LC = RTLIB::SRA_I64;
01758     else if (VT == MVT::i128)
01759       LC = RTLIB::SRA_I128;
01760   }
01761   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
01762 
01763   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01764   SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned, dl), Lo, Hi);
01765 }
01766 
01767 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
01768                                                 SDValue &Lo, SDValue &Hi) {
01769   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
01770   DebugLoc dl = N->getDebugLoc();
01771   SDValue Op = N->getOperand(0);
01772   if (Op.getValueType().bitsLE(NVT)) {
01773     // The low part is sign extension of the input (degenerates to a copy).
01774     Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
01775     // The high part is obtained by SRA'ing all but one of the bits of low part.
01776     unsigned LoSize = NVT.getSizeInBits();
01777     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
01778                      DAG.getConstant(LoSize-1, TLI.getPointerTy()));
01779   } else {
01780     // For example, extension of an i48 to an i64.  The operand type necessarily
01781     // promotes to the result type, so will end up being expanded too.
01782     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
01783            "Only know how to promote this result!");
01784     SDValue Res = GetPromotedInteger(Op);
01785     assert(Res.getValueType() == N->getValueType(0) &&
01786            "Operand over promoted?");
01787     // Split the promoted operand.  This will simplify when it is expanded.
01788     SplitInteger(Res, Lo, Hi);
01789     unsigned ExcessBits =
01790       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
01791     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
01792                      DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
01793   }
01794 }
01795 
01796 void DAGTypeLegalizer::
01797 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
01798   DebugLoc dl = N->getDebugLoc();
01799   GetExpandedInteger(N->getOperand(0), Lo, Hi);
01800   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
01801 
01802   if (EVT.bitsLE(Lo.getValueType())) {
01803     // sext_inreg the low part if needed.
01804     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
01805                      N->getOperand(1));
01806 
01807     // The high part gets the sign extension from the lo-part.  This handles
01808     // things like sextinreg V:i64 from i8.
01809     Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
01810                      DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
01811                                      TLI.getPointerTy()));
01812   } else {
01813     // For example, extension of an i48 to an i64.  Leave the low part alone,
01814     // sext_inreg the high part.
01815     unsigned ExcessBits =
01816       EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
01817     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
01818                      DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
01819   }
01820 }
01821 
01822 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
01823                                          SDValue &Lo, SDValue &Hi) {
01824   MVT VT = N->getValueType(0);
01825   DebugLoc dl = N->getDebugLoc();
01826 
01827   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01828   if (VT == MVT::i32)
01829     LC = RTLIB::SREM_I32;
01830   else if (VT == MVT::i64)
01831     LC = RTLIB::SREM_I64;
01832   else if (VT == MVT::i128)
01833     LC = RTLIB::SREM_I128;
01834   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
01835 
01836   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01837   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
01838 }
01839 
01840 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
01841                                              SDValue &Lo, SDValue &Hi) {
01842   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
01843   DebugLoc dl = N->getDebugLoc();
01844   Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
01845   Hi = DAG.getNode(ISD::SRL, dl,
01846                    N->getOperand(0).getValueType(), N->getOperand(0),
01847                    DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy()));
01848   Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
01849 }
01850 
01851 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
01852                                          SDValue &Lo, SDValue &Hi) {
01853   MVT VT = N->getValueType(0);
01854   DebugLoc dl = N->getDebugLoc();
01855 
01856   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01857   if (VT == MVT::i32)
01858     LC = RTLIB::UDIV_I32;
01859   else if (VT == MVT::i64)
01860     LC = RTLIB::UDIV_I64;
01861   else if (VT == MVT::i128)
01862     LC = RTLIB::UDIV_I128;
01863   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
01864 
01865   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01866   SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
01867 }
01868 
01869 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
01870                                          SDValue &Lo, SDValue &Hi) {
01871   MVT VT = N->getValueType(0);
01872   DebugLoc dl = N->getDebugLoc();
01873 
01874   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
01875   if (VT == MVT::i32)
01876     LC = RTLIB::UREM_I32;
01877   else if (VT == MVT::i64)
01878     LC = RTLIB::UREM_I64;
01879   else if (VT == MVT::i128)
01880     LC = RTLIB::UREM_I128;
01881   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
01882 
01883   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
01884   SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
01885 }
01886 
01887 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
01888                                                 SDValue &Lo, SDValue &Hi) {
01889   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
01890   DebugLoc dl = N->getDebugLoc();
01891   SDValue Op = N->getOperand(0);
01892   if (Op.getValueType().bitsLE(NVT)) {
01893     // The low part is zero extension of the input (degenerates to a copy).
01894     Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
01895     Hi = DAG.getConstant(0, NVT);   // The high part is just a zero.
01896   } else {
01897     // For example, extension of an i48 to an i64.  The operand type necessarily
01898     // promotes to the result type, so will end up being expanded too.
01899     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
01900            "Only know how to promote this result!");
01901     SDValue Res = GetPromotedInteger(Op);
01902     assert(Res.getValueType() == N->getValueType(0) &&
01903            "Operand over promoted?");
01904     // Split the promoted operand.  This will simplify when it is expanded.
01905     SplitInteger(Res, Lo, Hi);
01906     unsigned ExcessBits =
01907       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
01908     Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
01909   }
01910 }
01911 
01912 
01913 //===----------------------------------------------------------------------===//
01914 //  Integer Operand Expansion
01915 //===----------------------------------------------------------------------===//
01916 
01917 /// ExpandIntegerOperand - This method is called when the specified operand of
01918 /// the specified node is found to need expansion.  At this point, all of the
01919 /// result types of the node are known to be legal, but other operands of the
01920 /// node may need promotion or expansion as well as the specified one.
01921 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
01922   DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
01923   SDValue Res = SDValue();
01924 
01925   if (CustomLowerResults(N, N->getOperand(OpNo).getValueType(), false))
01926     return false;
01927 
01928   switch (N->getOpcode()) {
01929   default:
01930   #ifndef NDEBUG
01931     cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
01932     N->dump(&DAG); cerr << "\n";
01933   #endif
01934     assert(0 && "Do not know how to expand this operator's operand!");
01935     abort();
01936 
01937   case ISD::BIT_CONVERT:       Res = ExpandOp_BIT_CONVERT(N); break;
01938   case ISD::BR_CC:             Res = ExpandIntOp_BR_CC(N); break;
01939   case ISD::BUILD_VECTOR:      Res = ExpandOp_BUILD_VECTOR(N); break;
01940   case ISD::EXTRACT_ELEMENT:   Res = ExpandOp_EXTRACT_ELEMENT(N); break;
01941   case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
01942   case ISD::SCALAR_TO_VECTOR:  Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
01943   case ISD::SELECT_CC:         Res = ExpandIntOp_SELECT_CC(N); break;
01944   case ISD::SETCC:             Res = ExpandIntOp_SETCC(N); break;
01945   case ISD::SINT_TO_FP:        Res = ExpandIntOp_SINT_TO_FP(N); break;
01946   case ISD::STORE:   Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
01947   case ISD::TRUNCATE:          Res = ExpandIntOp_TRUNCATE(N); break;
01948   case ISD::UINT_TO_FP:        Res = ExpandIntOp_UINT_TO_FP(N); break;
01949 
01950   case ISD::SHL:
01951   case ISD::SRA:
01952   case ISD::SRL:
01953   case ISD::ROTL:
01954   case ISD::ROTR: Res = ExpandIntOp_Shift(N); break;
01955   }
01956 
01957   // If the result is null, the sub-method took care of registering results etc.
01958   if (!Res.getNode()) return false;
01959 
01960   // If the result is N, the sub-method updated N in place.  Tell the legalizer
01961   // core about this.
01962   if (Res.getNode() == N)
01963     return true;
01964 
01965   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
01966          "Invalid operand expansion");
01967 
01968   ReplaceValueWith(SDValue(N, 0), Res);
01969   return false;
01970 }
01971 
01972 /// IntegerExpandSetCCOperands - Expand the operands of a comparison.  This code
01973 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
01974 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
01975                                                   SDValue &NewRHS,
01976                                                   ISD::CondCode &CCCode,
01977                                                   DebugLoc dl) {
01978   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
01979   GetExpandedInteger(NewLHS, LHSLo, LHSHi);
01980   GetExpandedInteger(NewRHS, RHSLo, RHSHi);
01981 
01982   MVT VT = NewLHS.getValueType();
01983 
01984   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
01985     if (RHSLo == RHSHi) {
01986       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
01987         if (RHSCST->isAllOnesValue()) {
01988           // Equality comparison to -1.
01989           NewLHS = DAG.getNode(ISD::AND, dl,
01990                                LHSLo.getValueType(), LHSLo, LHSHi);
01991           NewRHS = RHSLo;
01992           return;
01993         }
01994       }
01995     }
01996 
01997     NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
01998     NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
01999     NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
02000     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
02001     return;
02002   }
02003 
02004   // If this is a comparison of the sign bit, just look at the top part.
02005   // X > -1,  x < 0
02006   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
02007     if ((CCCode == ISD::SETLT && CST->isNullValue()) ||     // X < 0
02008         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
02009       NewLHS = LHSHi;
02010       NewRHS = RHSHi;
02011       return;
02012     }
02013 
02014   // FIXME: This generated code sucks.
02015   ISD::CondCode LowCC;
02016   switch (CCCode) {
02017   default: assert(0 && "Unknown integer setcc!");
02018   case ISD::SETLT:
02019   case ISD::SETULT: LowCC = ISD::SETULT; break;
02020   case ISD::SETGT:
02021   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
02022   case ISD::SETLE:
02023   case ISD::SETULE: LowCC = ISD::SETULE; break;
02024   case ISD::SETGE:
02025   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
02026   }
02027 
02028   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
02029   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
02030   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
02031 
02032   // NOTE: on targets without efficient SELECT of bools, we can always use
02033   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
02034   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
02035   SDValue Tmp1, Tmp2;
02036   Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
02037                            LHSLo, RHSLo, LowCC, false, DagCombineInfo, dl);
02038   if (!Tmp1.getNode())
02039     Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
02040                         LHSLo, RHSLo, LowCC);
02041   Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
02042                            LHSHi, RHSHi, CCCode, false, DagCombineInfo, dl);
02043   if (!Tmp2.getNode())
02044     Tmp2 = DAG.getNode(ISD::SETCC, dl,
02045                        TLI.getSetCCResultType(LHSHi.getValueType()),
02046                        LHSHi, RHSHi, DAG.getCondCode(CCCode));
02047 
02048   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
02049   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
02050   if ((Tmp1C && Tmp1C->isNullValue()) ||
02051       (Tmp2C && Tmp2C->isNullValue() &&
02052        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
02053         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
02054       (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
02055        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
02056         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
02057     // low part is known false, returns high part.
02058     // For LE / GE, if high part is known false, ignore the low part.
02059     // For LT / GT, if high part is known true, ignore the low part.
02060     NewLHS = Tmp2;
02061     NewRHS = SDValue();
02062     return;
02063   }
02064 
02065   NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
02066                              LHSHi, RHSHi, ISD::SETEQ, false, 
02067                              DagCombineInfo, dl);
02068   if (!NewLHS.getNode())
02069     NewLHS = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
02070                           LHSHi, RHSHi, ISD::SETEQ);
02071   NewLHS = DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
02072                        NewLHS, Tmp1, Tmp2);
02073   NewRHS = SDValue();
02074 }
02075 
02076 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
02077   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
02078   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
02079   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
02080 
02081   // If ExpandSetCCOperands returned a scalar, we need to compare the result
02082   // against zero to select between true and false values.
02083   if (NewRHS.getNode() == 0) {
02084     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
02085     CCCode = ISD::SETNE;
02086   }
02087 
02088   // Update N to have the operands specified.
02089   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
02090                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
02091                                 N->getOperand(4));
02092 }
02093 
02094 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
02095   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
02096   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
02097   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
02098 
02099   // If ExpandSetCCOperands returned a scalar, we need to compare the result
02100   // against zero to select between true and false values.
02101   if (NewRHS.getNode() == 0) {
02102     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
02103     CCCode = ISD::SETNE;
02104   }
02105 
02106   // Update N to have the operands specified.
02107   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
02108                                 N->getOperand(2), N->getOperand(3),
02109                                 DAG.getCondCode(CCCode));
02110 }
02111 
02112 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
02113   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
02114   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
02115   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
02116 
02117   // If ExpandSetCCOperands returned a scalar, use it.
02118   if (NewRHS.getNode() == 0) {
02119     assert(NewLHS.getValueType() == N->getValueType(0) &&
02120            "Unexpected setcc expansion!");
02121     return NewLHS;
02122   }
02123 
02124   // Otherwise, update N to have the operands specified.
02125   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
02126                                 DAG.getCondCode(CCCode));
02127 }
02128 
02129 SDValue DAGTypeLegalizer::ExpandIntOp_Shift(SDNode *N) {
02130   // The value being shifted is legal, but the shift amount is too big.
02131   // It follows that either the result of the shift is undefined, or the
02132   // upper half of the shift amount is zero.  Just use the lower half.
02133   SDValue Lo, Hi;
02134   GetExpandedInteger(N->getOperand(1), Lo, Hi);
02135   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Lo);
02136 }
02137 
02138 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
02139   SDValue Op = N->getOperand(0);
02140   MVT DstVT = N->getValueType(0);
02141   RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
02142   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
02143          "Don't know how to expand this SINT_TO_FP!");
02144   return MakeLibCall(LC, DstVT, &Op, 1, true, N->getDebugLoc());
02145 }
02146 
02147 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
02148   if (ISD::isNormalStore(N))
02149     return ExpandOp_NormalStore(N, OpNo);
02150 
02151   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
02152   assert(OpNo == 1 && "Can only expand the stored value so far");
02153 
02154   MVT VT = N->getOperand(1).getValueType();
02155   MVT NVT = TLI.getTypeToTransformTo(VT);
02156   SDValue Ch  = N->getChain();
02157   SDValue Ptr = N->getBasePtr();
02158   int SVOffset = N->getSrcValueOffset();
02159   unsigned Alignment = N->getAlignment();
02160   bool isVolatile = N->isVolatile();
02161   DebugLoc dl = N->getDebugLoc();
02162   SDValue Lo, Hi;
02163 
02164   assert(NVT.isByteSized() && "Expanded type not byte sized!");
02165 
02166   if (N->getMemoryVT().bitsLE(NVT)) {
02167     GetExpandedInteger(N->getValue(), Lo, Hi);
02168     return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
02169                              N->getMemoryVT(), isVolatile, Alignment);
02170   } else if (TLI.isLittleEndian()) {
02171     // Little-endian - low bits are at low addresses.
02172     GetExpandedInteger(N->getValue(), Lo, Hi);
02173 
02174     Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
02175                       isVolatile, Alignment);
02176 
02177     unsigned ExcessBits =
02178       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
02179     MVT NEVT = MVT::getIntegerVT(ExcessBits);
02180 
02181     // Increment the pointer to the other half.
02182     unsigned IncrementSize = NVT.getSizeInBits()/8;
02183     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
02184                       DAG.getIntPtrConstant(IncrementSize));
02185     Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
02186                            SVOffset+IncrementSize, NEVT,
02187                            isVolatile, MinAlign(Alignment, IncrementSize));
02188     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
02189   } else {
02190     // Big-endian - high bits are at low addresses.  Favor aligned stores at
02191     // the cost of some bit-fiddling.
02192     GetExpandedInteger(N->getValue(), Lo, Hi);
02193 
02194     MVT EVT = N->getMemoryVT();
02195     unsigned EBytes = EVT.getStoreSizeInBits()/8;
02196     unsigned IncrementSize = NVT.getSizeInBits()/8;
02197     unsigned ExcessBits = (EBytes - IncrementSize)*8;
02198     MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
02199 
02200     if (ExcessBits < NVT.getSizeInBits()) {
02201       // Transfer high bits from the top of Lo to the bottom of Hi.
02202       Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
02203                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
02204                                        TLI.getPointerTy()));
02205       Hi = DAG.getNode(ISD::OR, dl, NVT, Hi,
02206                        DAG.getNode(ISD::SRL, NVT, Lo,
02207                                    DAG.getConstant(ExcessBits,
02208                                                    TLI.getPointerTy())));
02209     }
02210 
02211     // Store both the high bits and maybe some of the low bits.
02212     Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
02213                            SVOffset, HiVT, isVolatile, Alignment);
02214 
02215     // Increment the pointer to the other half.
02216     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
02217                       DAG.getIntPtrConstant(IncrementSize));
02218     // Store the lowest ExcessBits bits in the second half.
02219     Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(),
02220                            SVOffset+IncrementSize,
02221                            MVT::getIntegerVT(ExcessBits),
02222                            isVolatile, MinAlign(Alignment, IncrementSize));
02223     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
02224   }
02225 }
02226 
02227 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
02228   SDValue InL, InH;
02229   GetExpandedInteger(N->getOperand(0), InL, InH);
02230   // Just truncate the low part of the source.
02231   return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), InL);
02232 }
02233 
02234 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
02235   SDValue Op = N->getOperand(0);
02236   MVT SrcVT = Op.getValueType();
02237   MVT DstVT = N->getValueType(0);
02238   DebugLoc dl = N->getDebugLoc();
02239 
02240   if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
02241     // Do a signed conversion then adjust the result.
02242     SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
02243     SignedConv = TLI.LowerOperation(SignedConv, DAG);
02244 
02245     // The result of the signed conversion needs adjusting if the 'sign bit' of
02246     // the incoming integer was set.  To handle this, we dynamically test to see
02247     // if it is set, and, if so, add a fudge factor.
02248 
02249     const uint64_t F32TwoE32  = 0x4F800000ULL;
02250     const uint64_t F32TwoE64  = 0x5F800000ULL;
02251     const uint64_t F32TwoE128 = 0x7F800000ULL;
02252 
02253     APInt FF(32, 0);
02254     if (SrcVT == MVT::i32)
02255       FF = APInt(32, F32TwoE32);
02256     else if (SrcVT == MVT::i64)
02257       FF = APInt(32, F32TwoE64);
02258     else if (SrcVT == MVT::i128)
02259       FF = APInt(32, F32TwoE128);
02260     else
02261       assert(false && "Unsupported UINT_TO_FP!");
02262 
02263     // Check whether the sign bit is set.
02264     SDValue Lo, Hi;
02265     GetExpandedInteger(Op, Lo, Hi);
02266     SDValue SignSet = DAG.getSetCC(dl,
02267                                    TLI.getSetCCResultType(Hi.getValueType()),
02268                                    Hi, DAG.getConstant(0, Hi.getValueType()),
02269                                    ISD::SETLT);
02270 
02271     // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
02272     SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
02273                                            TLI.getPointerTy());
02274 
02275     // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
02276     SDValue Zero = DAG.getIntPtrConstant(0);
02277     SDValue Four = DAG.getIntPtrConstant(4);
02278     if (TLI.isBigEndian()) std::swap(Zero, Four);
02279     SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
02280                                  Zero, Four);
02281     unsigned Alignment =
02282       1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
02283     FudgePtr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), FudgePtr, Offset);
02284     Alignment = std::min(Alignment, 4u);
02285 
02286     // Load the value out, extending it from f32 to the destination float type.
02287     // FIXME: Avoid the extend by constructing the right constant pool?
02288     SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
02289                                    FudgePtr, NULL, 0, MVT::f32,
02290                                    false, Alignment);
02291     return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
02292   }
02293 
02294   // Otherwise, use a libcall.
02295   RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
02296   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
02297          "Don't know how to expand this UINT_TO_FP!");
02298   return MakeLibCall(LC, DstVT, &Op, 1, true, dl);
02299 }