Fix PR3401: when using large integers, the type
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeIntegerTypes.cpp
1 //===----- LegalizeIntegerTypes.cpp - Legalization of integer types -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements integer type expansion and promotion for LegalizeTypes.
11 // Promotion is the act of changing a computation in an illegal type into a
12 // computation in a larger type.  For example, implementing i8 arithmetic in an
13 // i32 register (often needed on powerpc).
14 // Expansion is the act of changing a computation in an illegal type into a
15 // computation in two identical registers of a smaller type.  For example,
16 // implementing i64 arithmetic in two i32 registers (often needed on 32-bit
17 // targets).
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "LegalizeTypes.h"
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 //  Integer Result Promotion
26 //===----------------------------------------------------------------------===//
27
28 /// PromoteIntegerResult - This method is called when a result of a node is
29 /// found to be in need of promotion to a larger type.  At this point, the node
30 /// may also have invalid operands or may have other results that need
31 /// expansion, we just know that (at least) one result needs promotion.
32 void DAGTypeLegalizer::PromoteIntegerResult(SDNode *N, unsigned ResNo) {
33   DEBUG(cerr << "Promote integer result: "; N->dump(&DAG); cerr << "\n");
34   SDValue Res = SDValue();
35
36   // See if the target wants to custom expand this node.
37   if (CustomLowerResults(N, N->getValueType(ResNo), true))
38     return;
39
40   switch (N->getOpcode()) {
41   default:
42 #ifndef NDEBUG
43     cerr << "PromoteIntegerResult #" << ResNo << ": ";
44     N->dump(&DAG); cerr << "\n";
45 #endif
46     assert(0 && "Do not know how to promote this operator!");
47     abort();
48   case ISD::AssertSext:  Res = PromoteIntRes_AssertSext(N); break;
49   case ISD::AssertZext:  Res = PromoteIntRes_AssertZext(N); break;
50   case ISD::BIT_CONVERT: Res = PromoteIntRes_BIT_CONVERT(N); break;
51   case ISD::BSWAP:       Res = PromoteIntRes_BSWAP(N); break;
52   case ISD::BUILD_PAIR:  Res = PromoteIntRes_BUILD_PAIR(N); break;
53   case ISD::Constant:    Res = PromoteIntRes_Constant(N); break;
54   case ISD::CONVERT_RNDSAT:
55                          Res = PromoteIntRes_CONVERT_RNDSAT(N); break;
56   case ISD::CTLZ:        Res = PromoteIntRes_CTLZ(N); break;
57   case ISD::CTPOP:       Res = PromoteIntRes_CTPOP(N); break;
58   case ISD::CTTZ:        Res = PromoteIntRes_CTTZ(N); break;
59   case ISD::EXTRACT_VECTOR_ELT:
60                          Res = PromoteIntRes_EXTRACT_VECTOR_ELT(N); break;
61   case ISD::LOAD:        Res = PromoteIntRes_LOAD(cast<LoadSDNode>(N));break;
62   case ISD::SELECT:      Res = PromoteIntRes_SELECT(N); break;
63   case ISD::SELECT_CC:   Res = PromoteIntRes_SELECT_CC(N); break;
64   case ISD::SETCC:       Res = PromoteIntRes_SETCC(N); break;
65   case ISD::SHL:         Res = PromoteIntRes_SHL(N); break;
66   case ISD::SIGN_EXTEND_INREG:
67                          Res = PromoteIntRes_SIGN_EXTEND_INREG(N); break;
68   case ISD::SRA:         Res = PromoteIntRes_SRA(N); break;
69   case ISD::SRL:         Res = PromoteIntRes_SRL(N); break;
70   case ISD::TRUNCATE:    Res = PromoteIntRes_TRUNCATE(N); break;
71   case ISD::UNDEF:       Res = PromoteIntRes_UNDEF(N); break;
72   case ISD::VAARG:       Res = PromoteIntRes_VAARG(N); break;
73
74   case ISD::SIGN_EXTEND:
75   case ISD::ZERO_EXTEND:
76   case ISD::ANY_EXTEND:  Res = PromoteIntRes_INT_EXTEND(N); break;
77
78   case ISD::FP_TO_SINT:
79   case ISD::FP_TO_UINT:  Res = PromoteIntRes_FP_TO_XINT(N); break;
80
81   case ISD::AND:
82   case ISD::OR:
83   case ISD::XOR:
84   case ISD::ADD:
85   case ISD::SUB:
86   case ISD::MUL:         Res = PromoteIntRes_SimpleIntBinOp(N); break;
87
88   case ISD::SDIV:
89   case ISD::SREM:        Res = PromoteIntRes_SDIV(N); break;
90
91   case ISD::UDIV:
92   case ISD::UREM:        Res = PromoteIntRes_UDIV(N); break;
93
94   case ISD::SADDO:
95   case ISD::SSUBO:       Res = PromoteIntRes_SADDSUBO(N, ResNo); break;
96   case ISD::UADDO:
97   case ISD::USUBO:       Res = PromoteIntRes_UADDSUBO(N, ResNo); break;
98   case ISD::SMULO:
99   case ISD::UMULO:       Res = PromoteIntRes_XMULO(N, ResNo); break;
100
101   case ISD::ATOMIC_LOAD_ADD:
102   case ISD::ATOMIC_LOAD_SUB:
103   case ISD::ATOMIC_LOAD_AND:
104   case ISD::ATOMIC_LOAD_OR:
105   case ISD::ATOMIC_LOAD_XOR:
106   case ISD::ATOMIC_LOAD_NAND:
107   case ISD::ATOMIC_LOAD_MIN:
108   case ISD::ATOMIC_LOAD_MAX:
109   case ISD::ATOMIC_LOAD_UMIN:
110   case ISD::ATOMIC_LOAD_UMAX:
111   case ISD::ATOMIC_SWAP:
112     Res = PromoteIntRes_Atomic1(cast<AtomicSDNode>(N)); break;
113
114   case ISD::ATOMIC_CMP_SWAP:
115     Res = PromoteIntRes_Atomic2(cast<AtomicSDNode>(N)); break;
116   }
117
118   // If the result is null then the sub-method took care of registering it.
119   if (Res.getNode())
120     SetPromotedInteger(SDValue(N, ResNo), Res);
121 }
122
123 SDValue DAGTypeLegalizer::PromoteIntRes_AssertSext(SDNode *N) {
124   // Sign-extend the new bits, and continue the assertion.
125   SDValue Op = SExtPromotedInteger(N->getOperand(0));
126   return DAG.getNode(ISD::AssertSext, N->getDebugLoc(),
127                      Op.getValueType(), Op, N->getOperand(1));
128 }
129
130 SDValue DAGTypeLegalizer::PromoteIntRes_AssertZext(SDNode *N) {
131   // Zero the new bits, and continue the assertion.
132   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
133   return DAG.getNode(ISD::AssertZext, N->getDebugLoc(),
134                      Op.getValueType(), Op, N->getOperand(1));
135 }
136
137 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic1(AtomicSDNode *N) {
138   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
139   SDValue Res = DAG.getAtomic(N->getOpcode(), N->getDebugLoc(), 
140                               N->getMemoryVT(),
141                               N->getChain(), N->getBasePtr(),
142                               Op2, N->getSrcValue(), N->getAlignment());
143   // Legalized the chain result - switch anything that used the old chain to
144   // use the new one.
145   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
146   return Res;
147 }
148
149 SDValue DAGTypeLegalizer::PromoteIntRes_Atomic2(AtomicSDNode *N) {
150   SDValue Op2 = GetPromotedInteger(N->getOperand(2));
151   SDValue Op3 = GetPromotedInteger(N->getOperand(3));
152   SDValue Res = DAG.getAtomic(N->getOpcode(), N->getMemoryVT(),
153                               N->getChain(), N->getBasePtr(),
154                               Op2, Op3, N->getSrcValue(), N->getAlignment());
155   // Legalized the chain result - switch anything that used the old chain to
156   // use the new one.
157   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
158   return Res;
159 }
160
161 SDValue DAGTypeLegalizer::PromoteIntRes_BIT_CONVERT(SDNode *N) {
162   SDValue InOp = N->getOperand(0);
163   MVT InVT = InOp.getValueType();
164   MVT NInVT = TLI.getTypeToTransformTo(InVT);
165   MVT OutVT = N->getValueType(0);
166   MVT NOutVT = TLI.getTypeToTransformTo(OutVT);
167   DebugLoc dl = N->getDebugLoc();
168
169   switch (getTypeAction(InVT)) {
170   default:
171     assert(false && "Unknown type action!");
172     break;
173   case Legal:
174     break;
175   case PromoteInteger:
176     if (NOutVT.bitsEq(NInVT))
177       // The input promotes to the same size.  Convert the promoted value.
178       return DAG.getNode(ISD::BIT_CONVERT, dl, 
179                          NOutVT, GetPromotedInteger(InOp));
180     break;
181   case SoftenFloat:
182     // Promote the integer operand by hand.
183     return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT, GetSoftenedFloat(InOp));
184   case ExpandInteger:
185   case ExpandFloat:
186     break;
187   case ScalarizeVector:
188     // Convert the element to an integer and promote it by hand.
189     return DAG.getNode(ISD::ANY_EXTEND, dl, NOutVT,
190                        BitConvertToInteger(GetScalarizedVector(InOp)));
191   case SplitVector: {
192     // For example, i32 = BIT_CONVERT v2i16 on alpha.  Convert the split
193     // pieces of the input into integers and reassemble in the final type.
194     SDValue Lo, Hi;
195     GetSplitVector(N->getOperand(0), Lo, Hi);
196     Lo = BitConvertToInteger(Lo);
197     Hi = BitConvertToInteger(Hi);
198
199     if (TLI.isBigEndian())
200       std::swap(Lo, Hi);
201
202     InOp = DAG.getNode(ISD::ANY_EXTEND, dl,
203                        MVT::getIntegerVT(NOutVT.getSizeInBits()),
204                        JoinIntegers(Lo, Hi));
205     return DAG.getNode(ISD::BIT_CONVERT, dl, NOutVT, InOp);
206   }
207   case WidenVector:
208     if (OutVT.bitsEq(NInVT))
209       // The input is widened to the same size.  Convert to the widened value.
210       return DAG.getNode(ISD::BIT_CONVERT, dl, OutVT, GetWidenedVector(InOp));
211   }
212
213   // Otherwise, lower the bit-convert to a store/load from the stack.
214   // Create the stack frame object.  Make sure it is aligned for both
215   // the source and destination types.
216   SDValue FIPtr = DAG.CreateStackTemporary(InVT, OutVT);
217
218   // Emit a store to the stack slot.
219   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, InOp, FIPtr, NULL, 0);
220
221   // Result is an extending load from the stack slot.
222   return DAG.getExtLoad(ISD::EXTLOAD, dl, NOutVT, Store, FIPtr, NULL, 0, OutVT);
223 }
224
225 SDValue DAGTypeLegalizer::PromoteIntRes_BSWAP(SDNode *N) {
226   SDValue Op = GetPromotedInteger(N->getOperand(0));
227   MVT OVT = N->getValueType(0);
228   MVT NVT = Op.getValueType();
229   DebugLoc dl = N->getDebugLoc();
230
231   unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
232   return DAG.getNode(ISD::SRL, dl, NVT, DAG.getNode(ISD::BSWAP, dl, NVT, Op),
233                      DAG.getConstant(DiffBits, TLI.getPointerTy()));
234 }
235
236 SDValue DAGTypeLegalizer::PromoteIntRes_BUILD_PAIR(SDNode *N) {
237   // The pair element type may be legal, or may not promote to the same type as
238   // the result, for example i14 = BUILD_PAIR (i7, i7).  Handle all cases.
239   return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(),
240                      TLI.getTypeToTransformTo(N->getValueType(0)),
241                      JoinIntegers(N->getOperand(0), N->getOperand(1)));
242 }
243
244 SDValue DAGTypeLegalizer::PromoteIntRes_Constant(SDNode *N) {
245   MVT VT = N->getValueType(0);
246   // Zero extend things like i1, sign extend everything else.  It shouldn't
247   // matter in theory which one we pick, but this tends to give better code?
248   unsigned Opc = VT.isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
249   SDValue Result = DAG.getNode(Opc, TLI.getTypeToTransformTo(VT),
250                                SDValue(N, 0));
251   assert(isa<ConstantSDNode>(Result) && "Didn't constant fold ext?");
252   return Result;
253 }
254
255 SDValue DAGTypeLegalizer::PromoteIntRes_CONVERT_RNDSAT(SDNode *N) {
256   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
257   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
258            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
259            CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
260           "can only promote integers");
261   MVT OutVT = TLI.getTypeToTransformTo(N->getValueType(0));
262   return DAG.getConvertRndSat(OutVT, N->getOperand(0),
263                               N->getOperand(1), N->getOperand(2),
264                               N->getOperand(3), N->getOperand(4), CvtCode);
265 }
266
267 SDValue DAGTypeLegalizer::PromoteIntRes_CTLZ(SDNode *N) {
268   // Zero extend to the promoted type and do the count there.
269   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
270   MVT OVT = N->getValueType(0);
271   MVT NVT = Op.getValueType();
272   Op = DAG.getNode(ISD::CTLZ, NVT, Op);
273   // Subtract off the extra leading bits in the bigger type.
274   return DAG.getNode(ISD::SUB, N->getDebugLoc(), NVT, Op,
275                      DAG.getConstant(NVT.getSizeInBits() -
276                                      OVT.getSizeInBits(), NVT));
277 }
278
279 SDValue DAGTypeLegalizer::PromoteIntRes_CTPOP(SDNode *N) {
280   // Zero extend to the promoted type and do the count there.
281   SDValue Op = ZExtPromotedInteger(N->getOperand(0));
282   return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), Op.getValueType(), Op);
283 }
284
285 SDValue DAGTypeLegalizer::PromoteIntRes_CTTZ(SDNode *N) {
286   SDValue Op = GetPromotedInteger(N->getOperand(0));
287   MVT OVT = N->getValueType(0);
288   MVT NVT = Op.getValueType();
289   DebugLoc dl = N->getDebugLoc();
290   // The count is the same in the promoted type except if the original
291   // value was zero.  This can be handled by setting the bit just off
292   // the top of the original type.
293   APInt TopBit(NVT.getSizeInBits(), 0);
294   TopBit.set(OVT.getSizeInBits());
295   Op = DAG.getNode(ISD::OR, dl, NVT, Op, DAG.getConstant(TopBit, NVT));
296   return DAG.getNode(ISD::CTTZ, dl, NVT, Op);
297 }
298
299 SDValue DAGTypeLegalizer::PromoteIntRes_EXTRACT_VECTOR_ELT(SDNode *N) {
300   MVT OldVT = N->getValueType(0);
301   SDValue OldVec = N->getOperand(0);
302   if (getTypeAction(OldVec.getValueType()) == WidenVector)
303     OldVec = GetWidenedVector(N->getOperand(0));
304   unsigned OldElts = OldVec.getValueType().getVectorNumElements();
305   DebugLoc dl = N->getDebugLoc();
306
307   if (OldElts == 1) {
308     assert(!isTypeLegal(OldVec.getValueType()) &&
309            "Legal one-element vector of a type needing promotion!");
310     // It is tempting to follow GetScalarizedVector by a call to
311     // GetPromotedInteger, but this would be wrong because the
312     // scalarized value may not yet have been processed.
313     return DAG.getNode(ISD::ANY_EXTEND, dl, TLI.getTypeToTransformTo(OldVT),
314                        GetScalarizedVector(OldVec));
315   }
316
317   // Convert to a vector half as long with an element type of twice the width,
318   // for example <4 x i16> -> <2 x i32>.
319   assert(!(OldElts & 1) && "Odd length vectors not supported!");
320   MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
321   assert(OldVT.isSimple() && NewVT.isSimple());
322
323   SDValue NewVec = DAG.getNode(ISD::BIT_CONVERT, dl,
324                                  MVT::getVectorVT(NewVT, OldElts / 2),
325                                  OldVec);
326
327   // Extract the element at OldIdx / 2 from the new vector.
328   SDValue OldIdx = N->getOperand(1);
329   SDValue NewIdx = DAG.getNode(ISD::SRL, dl, OldIdx.getValueType(), OldIdx,
330                                DAG.getConstant(1, TLI.getPointerTy()));
331   SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, NewVT, NewVec, NewIdx);
332
333   // Select the appropriate half of the element: Lo if OldIdx was even,
334   // Hi if it was odd.
335   SDValue Lo = Elt;
336   SDValue Hi = DAG.getNode(ISD::SRL, dl, NewVT, Elt,
337                            DAG.getConstant(OldVT.getSizeInBits(),
338                                            TLI.getPointerTy()));
339   if (TLI.isBigEndian())
340     std::swap(Lo, Hi);
341
342   // Extend to the promoted type.
343   SDValue Odd = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, OldIdx);
344   SDValue Res = DAG.getNode(ISD::SELECT, dl, NewVT, Odd, Hi, Lo);
345   return DAG.getNode(ISD::ANY_EXTEND, dl, TLI.getTypeToTransformTo(OldVT), Res);
346 }
347
348 SDValue DAGTypeLegalizer::PromoteIntRes_FP_TO_XINT(SDNode *N) {
349   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
350   unsigned NewOpc = N->getOpcode();
351   DebugLoc dl = N->getDebugLoc();
352
353   // If we're promoting a UINT to a larger size, check to see if the new node
354   // will be legal.  If it isn't, check to see if FP_TO_SINT is legal, since
355   // we can use that instead.  This allows us to generate better code for
356   // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
357   // legal, such as PowerPC.
358   if (N->getOpcode() == ISD::FP_TO_UINT &&
359       !TLI.isOperationLegalOrCustom(ISD::FP_TO_UINT, NVT) &&
360       TLI.isOperationLegalOrCustom(ISD::FP_TO_SINT, NVT))
361     NewOpc = ISD::FP_TO_SINT;
362
363   SDValue Res = DAG.getNode(NewOpc, dl, NVT, N->getOperand(0));
364
365   // Assert that the converted value fits in the original type.  If it doesn't
366   // (eg: because the value being converted is too big), then the result of the
367   // original operation was undefined anyway, so the assert is still correct.
368   return DAG.getNode(N->getOpcode() == ISD::FP_TO_UINT ?
369                      ISD::AssertZext : ISD::AssertSext, dl,
370                      NVT, Res, DAG.getValueType(N->getValueType(0)));
371 }
372
373 SDValue DAGTypeLegalizer::PromoteIntRes_INT_EXTEND(SDNode *N) {
374   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
375   DebugLoc dl = N->getDebugLoc();
376
377   if (getTypeAction(N->getOperand(0).getValueType()) == PromoteInteger) {
378     SDValue Res = GetPromotedInteger(N->getOperand(0));
379     assert(Res.getValueType().bitsLE(NVT) && "Extension doesn't make sense!");
380
381     // If the result and operand types are the same after promotion, simplify
382     // to an in-register extension.
383     if (NVT == Res.getValueType()) {
384       // The high bits are not guaranteed to be anything.  Insert an extend.
385       if (N->getOpcode() == ISD::SIGN_EXTEND)
386         return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
387                            DAG.getValueType(N->getOperand(0).getValueType()));
388       if (N->getOpcode() == ISD::ZERO_EXTEND)
389         return DAG.getZeroExtendInReg(Res, N->getOperand(0).getValueType());
390       assert(N->getOpcode() == ISD::ANY_EXTEND && "Unknown integer extension!");
391       return Res;
392     }
393   }
394
395   // Otherwise, just extend the original operand all the way to the larger type.
396   return DAG.getNode(N->getOpcode(), dl, NVT, N->getOperand(0));
397 }
398
399 SDValue DAGTypeLegalizer::PromoteIntRes_LOAD(LoadSDNode *N) {
400   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
401   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
402   ISD::LoadExtType ExtType =
403     ISD::isNON_EXTLoad(N) ? ISD::EXTLOAD : N->getExtensionType();
404   DebugLoc dl = N->getDebugLoc();
405   SDValue Res = DAG.getExtLoad(ExtType, dl, NVT, N->getChain(), N->getBasePtr(),
406                                N->getSrcValue(), N->getSrcValueOffset(),
407                                N->getMemoryVT(), N->isVolatile(),
408                                N->getAlignment());
409
410   // Legalized the chain result - switch anything that used the old chain to
411   // use the new one.
412   ReplaceValueWith(SDValue(N, 1), Res.getValue(1));
413   return Res;
414 }
415
416 /// Promote the overflow flag of an overflowing arithmetic node.
417 SDValue DAGTypeLegalizer::PromoteIntRes_Overflow(SDNode *N) {
418   // Simply change the return type of the boolean result.
419   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(1));
420   MVT ValueVTs[] = { N->getValueType(0), NVT };
421   SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
422   SDValue Res = DAG.getNode(N->getOpcode(), N->getDebugLoc(),
423                             DAG.getVTList(ValueVTs, 2), Ops, 2);
424
425   // Modified the sum result - switch anything that used the old sum to use
426   // the new one.
427   ReplaceValueWith(SDValue(N, 0), Res);
428
429   return SDValue(Res.getNode(), 1);
430 }
431
432 SDValue DAGTypeLegalizer::PromoteIntRes_SADDSUBO(SDNode *N, unsigned ResNo) {
433   if (ResNo == 1)
434     return PromoteIntRes_Overflow(N);
435
436   // The operation overflowed iff the result in the larger type is not the
437   // sign extension of its truncation to the original type.
438   SDValue LHS = SExtPromotedInteger(N->getOperand(0));
439   SDValue RHS = SExtPromotedInteger(N->getOperand(1));
440   MVT OVT = N->getOperand(0).getValueType();
441   MVT NVT = LHS.getValueType();
442   DebugLoc dl = N->getDebugLoc();
443
444   // Do the arithmetic in the larger type.
445   unsigned Opcode = N->getOpcode() == ISD::SADDO ? ISD::ADD : ISD::SUB;
446   SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
447
448   // Calculate the overflow flag: sign extend the arithmetic result from
449   // the original type.
450   SDValue Ofl = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NVT, Res,
451                             DAG.getValueType(OVT));
452   // Overflowed if and only if this is not equal to Res.
453   Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
454
455   // Use the calculated overflow everywhere.
456   ReplaceValueWith(SDValue(N, 1), Ofl);
457
458   return Res;
459 }
460
461 SDValue DAGTypeLegalizer::PromoteIntRes_SDIV(SDNode *N) {
462   // Sign extend the input.
463   SDValue LHS = SExtPromotedInteger(N->getOperand(0));
464   SDValue RHS = SExtPromotedInteger(N->getOperand(1));
465   return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
466                      LHS.getValueType(), LHS, RHS);
467 }
468
469 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT(SDNode *N) {
470   SDValue LHS = GetPromotedInteger(N->getOperand(1));
471   SDValue RHS = GetPromotedInteger(N->getOperand(2));
472   return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
473                      LHS.getValueType(), N->getOperand(0),LHS,RHS);
474 }
475
476 SDValue DAGTypeLegalizer::PromoteIntRes_SELECT_CC(SDNode *N) {
477   SDValue LHS = GetPromotedInteger(N->getOperand(2));
478   SDValue RHS = GetPromotedInteger(N->getOperand(3));
479   return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
480                      LHS.getValueType(), N->getOperand(0),
481                      N->getOperand(1), LHS, RHS, N->getOperand(4));
482 }
483
484 SDValue DAGTypeLegalizer::PromoteIntRes_SETCC(SDNode *N) {
485   MVT SVT = TLI.getSetCCResultType(N->getOperand(0).getValueType());
486   assert(isTypeLegal(SVT) && "Illegal SetCC type!");
487   DebugLoc dl = N->getDebugLoc();
488
489   // Get the SETCC result using the canonical SETCC type.
490   SDValue SetCC = DAG.getNode(ISD::SETCC, dl, SVT, N->getOperand(0),
491                               N->getOperand(1), N->getOperand(2));
492
493   // Convert to the expected type.
494   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
495   assert(NVT.bitsLE(SVT) && "Integer type overpromoted?");
496   return DAG.getNode(ISD::TRUNCATE, dl, NVT, SetCC);
497 }
498
499 SDValue DAGTypeLegalizer::PromoteIntRes_SHL(SDNode *N) {
500   return DAG.getNode(ISD::SHL, N->getDebugLoc(),
501                      TLI.getTypeToTransformTo(N->getValueType(0)),
502                      GetPromotedInteger(N->getOperand(0)), N->getOperand(1));
503 }
504
505 SDValue DAGTypeLegalizer::PromoteIntRes_SIGN_EXTEND_INREG(SDNode *N) {
506   SDValue Op = GetPromotedInteger(N->getOperand(0));
507   return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(),
508                      Op.getValueType(), Op, N->getOperand(1));
509 }
510
511 SDValue DAGTypeLegalizer::PromoteIntRes_SimpleIntBinOp(SDNode *N) {
512   // The input may have strange things in the top bits of the registers, but
513   // these operations don't care.  They may have weird bits going out, but
514   // that too is okay if they are integer operations.
515   SDValue LHS = GetPromotedInteger(N->getOperand(0));
516   SDValue RHS = GetPromotedInteger(N->getOperand(1));
517   return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
518                     LHS.getValueType(), LHS, RHS);
519 }
520
521 SDValue DAGTypeLegalizer::PromoteIntRes_SRA(SDNode *N) {
522   // The input value must be properly sign extended.
523   SDValue Res = SExtPromotedInteger(N->getOperand(0));
524   return DAG.getNode(ISD::SRA, N->getDebugLoc(),
525                      Res.getValueType(), Res, N->getOperand(1));
526 }
527
528 SDValue DAGTypeLegalizer::PromoteIntRes_SRL(SDNode *N) {
529   // The input value must be properly zero extended.
530   MVT VT = N->getValueType(0);
531   MVT NVT = TLI.getTypeToTransformTo(VT);
532   SDValue Res = ZExtPromotedInteger(N->getOperand(0));
533   return DAG.getNode(ISD::SRL, N->getDebugLoc(), NVT, Res, N->getOperand(1));
534 }
535
536 SDValue DAGTypeLegalizer::PromoteIntRes_TRUNCATE(SDNode *N) {
537   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
538   SDValue Res;
539
540   switch (getTypeAction(N->getOperand(0).getValueType())) {
541   default: assert(0 && "Unknown type action!");
542   case Legal:
543   case ExpandInteger:
544     Res = N->getOperand(0);
545     break;
546   case PromoteInteger:
547     Res = GetPromotedInteger(N->getOperand(0));
548     break;
549   }
550
551   // Truncate to NVT instead of VT
552   return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Res);
553 }
554
555 SDValue DAGTypeLegalizer::PromoteIntRes_UADDSUBO(SDNode *N, unsigned ResNo) {
556   if (ResNo == 1)
557     return PromoteIntRes_Overflow(N);
558
559   // The operation overflowed iff the result in the larger type is not the
560   // zero extension of its truncation to the original type.
561   SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
562   SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
563   MVT OVT = N->getOperand(0).getValueType();
564   MVT NVT = LHS.getValueType();
565   DebugLoc dl = N->getDebugLoc();
566
567   // Do the arithmetic in the larger type.
568   unsigned Opcode = N->getOpcode() == ISD::UADDO ? ISD::ADD : ISD::SUB;
569   SDValue Res = DAG.getNode(Opcode, dl, NVT, LHS, RHS);
570
571   // Calculate the overflow flag: zero extend the arithmetic result from
572   // the original type.
573   SDValue Ofl = DAG.getZeroExtendInReg(Res, OVT);
574   // Overflowed if and only if this is not equal to Res.
575   Ofl = DAG.getSetCC(dl, N->getValueType(1), Ofl, Res, ISD::SETNE);
576
577   // Use the calculated overflow everywhere.
578   ReplaceValueWith(SDValue(N, 1), Ofl);
579
580   return Res;
581 }
582
583 SDValue DAGTypeLegalizer::PromoteIntRes_UDIV(SDNode *N) {
584   // Zero extend the input.
585   SDValue LHS = ZExtPromotedInteger(N->getOperand(0));
586   SDValue RHS = ZExtPromotedInteger(N->getOperand(1));
587   return DAG.getNode(N->getOpcode(), N->getDebugLoc(),
588                      LHS.getValueType(), LHS, RHS);
589 }
590
591 SDValue DAGTypeLegalizer::PromoteIntRes_UNDEF(SDNode *N) {
592   return DAG.getNode(ISD::UNDEF, N->getDebugLoc(),
593                      TLI.getTypeToTransformTo(N->getValueType(0)));
594 }
595
596 SDValue DAGTypeLegalizer::PromoteIntRes_VAARG(SDNode *N) {
597   SDValue Chain = N->getOperand(0); // Get the chain.
598   SDValue Ptr = N->getOperand(1); // Get the pointer.
599   MVT VT = N->getValueType(0);
600   DebugLoc dl = N->getDebugLoc();
601
602   MVT RegVT = TLI.getRegisterType(VT);
603   unsigned NumRegs = TLI.getNumRegisters(VT);
604   // The argument is passed as NumRegs registers of type RegVT.
605
606   SmallVector<SDValue, 8> Parts(NumRegs);
607   for (unsigned i = 0; i < NumRegs; ++i) {
608     Parts[i] = DAG.getVAArg(RegVT, Chain, Ptr, N->getOperand(2));
609     Chain = Parts[i].getValue(1);
610   }
611
612   // Handle endianness of the load.
613   if (TLI.isBigEndian())
614     std::reverse(Parts.begin(), Parts.end());
615
616   // Assemble the parts in the promoted type.
617   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
618   SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[0]);
619   for (unsigned i = 1; i < NumRegs; ++i) {
620     SDValue Part = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Parts[i]);
621     // Shift it to the right position and "or" it in.
622     Part = DAG.getNode(ISD::SHL, dl, NVT, Part,
623                        DAG.getConstant(i * RegVT.getSizeInBits(),
624                                        TLI.getPointerTy()));
625     Res = DAG.getNode(ISD::OR, dl, NVT, Res, Part);
626   }
627
628   // Modified the chain result - switch anything that used the old chain to
629   // use the new one.
630   ReplaceValueWith(SDValue(N, 1), Chain);
631
632   return Res;
633 }
634
635 SDValue DAGTypeLegalizer::PromoteIntRes_XMULO(SDNode *N, unsigned ResNo) {
636   assert(ResNo == 1 && "Only boolean result promotion currently supported!");
637   return PromoteIntRes_Overflow(N);
638 }
639
640 //===----------------------------------------------------------------------===//
641 //  Integer Operand Promotion
642 //===----------------------------------------------------------------------===//
643
644 /// PromoteIntegerOperand - This method is called when the specified operand of
645 /// the specified node is found to need promotion.  At this point, all of the
646 /// result types of the node are known to be legal, but other operands of the
647 /// node may need promotion or expansion as well as the specified one.
648 bool DAGTypeLegalizer::PromoteIntegerOperand(SDNode *N, unsigned OpNo) {
649   DEBUG(cerr << "Promote integer operand: "; N->dump(&DAG); cerr << "\n");
650   SDValue Res = SDValue();
651
652   if (CustomLowerResults(N, N->getOperand(OpNo).getValueType(), false))
653     return false;
654
655   switch (N->getOpcode()) {
656     default:
657   #ifndef NDEBUG
658     cerr << "PromoteIntegerOperand Op #" << OpNo << ": ";
659     N->dump(&DAG); cerr << "\n";
660   #endif
661     assert(0 && "Do not know how to promote this operator's operand!");
662     abort();
663
664   case ISD::ANY_EXTEND:   Res = PromoteIntOp_ANY_EXTEND(N); break;
665   case ISD::BR_CC:        Res = PromoteIntOp_BR_CC(N, OpNo); break;
666   case ISD::BRCOND:       Res = PromoteIntOp_BRCOND(N, OpNo); break;
667   case ISD::BUILD_PAIR:   Res = PromoteIntOp_BUILD_PAIR(N); break;
668   case ISD::BUILD_VECTOR: Res = PromoteIntOp_BUILD_VECTOR(N); break;
669   case ISD::CONVERT_RNDSAT:
670                           Res = PromoteIntOp_CONVERT_RNDSAT(N); break;
671   case ISD::INSERT_VECTOR_ELT:
672                           Res = PromoteIntOp_INSERT_VECTOR_ELT(N, OpNo);break;
673   case ISD::MEMBARRIER:   Res = PromoteIntOp_MEMBARRIER(N); break;
674   case ISD::SELECT:       Res = PromoteIntOp_SELECT(N, OpNo); break;
675   case ISD::SELECT_CC:    Res = PromoteIntOp_SELECT_CC(N, OpNo); break;
676   case ISD::SETCC:        Res = PromoteIntOp_SETCC(N, OpNo); break;
677   case ISD::SIGN_EXTEND:  Res = PromoteIntOp_SIGN_EXTEND(N); break;
678   case ISD::SINT_TO_FP:   Res = PromoteIntOp_SINT_TO_FP(N); break;
679   case ISD::STORE:        Res = PromoteIntOp_STORE(cast<StoreSDNode>(N),
680                                                    OpNo); break;
681   case ISD::TRUNCATE:     Res = PromoteIntOp_TRUNCATE(N); break;
682   case ISD::UINT_TO_FP:   Res = PromoteIntOp_UINT_TO_FP(N); break;
683   case ISD::ZERO_EXTEND:  Res = PromoteIntOp_ZERO_EXTEND(N); break;
684   }
685
686   // If the result is null, the sub-method took care of registering results etc.
687   if (!Res.getNode()) return false;
688
689   // If the result is N, the sub-method updated N in place.  Tell the legalizer
690   // core about this.
691   if (Res.getNode() == N)
692     return true;
693
694   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
695          "Invalid operand expansion");
696
697   ReplaceValueWith(SDValue(N, 0), Res);
698   return false;
699 }
700
701 /// PromoteSetCCOperands - Promote the operands of a comparison.  This code is
702 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
703 void DAGTypeLegalizer::PromoteSetCCOperands(SDValue &NewLHS,SDValue &NewRHS,
704                                             ISD::CondCode CCCode) {
705   // We have to insert explicit sign or zero extends.  Note that we could
706   // insert sign extends for ALL conditions, but zero extend is cheaper on
707   // many machines (an AND instead of two shifts), so prefer it.
708   switch (CCCode) {
709   default: assert(0 && "Unknown integer comparison!");
710   case ISD::SETEQ:
711   case ISD::SETNE:
712   case ISD::SETUGE:
713   case ISD::SETUGT:
714   case ISD::SETULE:
715   case ISD::SETULT:
716     // ALL of these operations will work if we either sign or zero extend
717     // the operands (including the unsigned comparisons!).  Zero extend is
718     // usually a simpler/cheaper operation, so prefer it.
719     NewLHS = ZExtPromotedInteger(NewLHS);
720     NewRHS = ZExtPromotedInteger(NewRHS);
721     break;
722   case ISD::SETGE:
723   case ISD::SETGT:
724   case ISD::SETLT:
725   case ISD::SETLE:
726     NewLHS = SExtPromotedInteger(NewLHS);
727     NewRHS = SExtPromotedInteger(NewRHS);
728     break;
729   }
730 }
731
732 SDValue DAGTypeLegalizer::PromoteIntOp_ANY_EXTEND(SDNode *N) {
733   SDValue Op = GetPromotedInteger(N->getOperand(0));
734   return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
735 }
736
737 SDValue DAGTypeLegalizer::PromoteIntOp_BR_CC(SDNode *N, unsigned OpNo) {
738   assert(OpNo == 2 && "Don't know how to promote this operand!");
739
740   SDValue LHS = N->getOperand(2);
741   SDValue RHS = N->getOperand(3);
742   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(1))->get());
743
744   // The chain (Op#0), CC (#1) and basic block destination (Op#4) are always
745   // legal types.
746   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
747                                 N->getOperand(1), LHS, RHS, N->getOperand(4));
748 }
749
750 SDValue DAGTypeLegalizer::PromoteIntOp_BRCOND(SDNode *N, unsigned OpNo) {
751   assert(OpNo == 1 && "only know how to promote condition");
752
753   // Promote all the way up to the canonical SetCC type.
754   MVT SVT = TLI.getSetCCResultType(MVT::Other);
755   SDValue Cond = PromoteTargetBoolean(N->getOperand(1), SVT);
756
757   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
758   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
759                                 N->getOperand(2));
760 }
761
762 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
763   // Since the result type is legal, the operands must promote to it.
764   MVT OVT = N->getOperand(0).getValueType();
765   SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
766   SDValue Hi = GetPromotedInteger(N->getOperand(1));
767   assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
768   DebugLoc dl = N->getDebugLoc();
769
770   Hi = DAG.getNode(ISD::SHL, dl, N->getValueType(0), Hi,
771                    DAG.getConstant(OVT.getSizeInBits(), TLI.getPointerTy()));
772   return DAG.getNode(ISD::OR, dl, N->getValueType(0), Lo, Hi);
773 }
774
775 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
776   // The vector type is legal but the element type is not.  This implies
777   // that the vector is a power-of-two in length and that the element
778   // type does not have a strange size (eg: it is not i1).
779   MVT VecVT = N->getValueType(0);
780   unsigned NumElts = VecVT.getVectorNumElements();
781   assert(!(NumElts & 1) && "Legal vector of one illegal element?");
782   DebugLoc dl = N->getDebugLoc();
783
784   // Build a vector of half the length out of elements of twice the bitwidth.
785   // For example <4 x i16> -> <2 x i32>.
786   MVT OldVT = N->getOperand(0).getValueType();
787   MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
788   assert(OldVT.isSimple() && NewVT.isSimple());
789
790   std::vector<SDValue> NewElts;
791   NewElts.reserve(NumElts/2);
792
793   for (unsigned i = 0; i < NumElts; i += 2) {
794     // Combine two successive elements into one promoted element.
795     SDValue Lo = N->getOperand(i);
796     SDValue Hi = N->getOperand(i+1);
797     if (TLI.isBigEndian())
798       std::swap(Lo, Hi);
799     NewElts.push_back(JoinIntegers(Lo, Hi));
800   }
801
802   SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR, dl,
803                                  MVT::getVectorVT(NewVT, NewElts.size()),
804                                  &NewElts[0], NewElts.size());
805
806   // Convert the new vector to the old vector type.
807   return DAG.getNode(ISD::BIT_CONVERT, dl, VecVT, NewVec);
808 }
809
810 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
811   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
812   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
813            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
814            CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
815            "can only promote integer arguments");
816   SDValue InOp = GetPromotedInteger(N->getOperand(0));
817   return DAG.getConvertRndSat(N->getValueType(0), InOp,
818                               N->getOperand(1), N->getOperand(2),
819                               N->getOperand(3), N->getOperand(4), CvtCode);
820 }
821
822 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
823                                                          unsigned OpNo) {
824   if (OpNo == 1) {
825     // Promote the inserted value.  This is valid because the type does not
826     // have to match the vector element type.
827
828     // Check that any extra bits introduced will be truncated away.
829     assert(N->getOperand(1).getValueType().getSizeInBits() >=
830            N->getValueType(0).getVectorElementType().getSizeInBits() &&
831            "Type of inserted value narrower than vector element type!");
832     return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
833                                   GetPromotedInteger(N->getOperand(1)),
834                                   N->getOperand(2));
835   }
836
837   assert(OpNo == 2 && "Different operand and result vector types?");
838
839   // Promote the index.
840   SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
841   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
842                                 N->getOperand(1), Idx);
843 }
844
845 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
846   SDValue NewOps[6];
847   NewOps[0] = N->getOperand(0);
848   for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
849     SDValue Flag = GetPromotedInteger(N->getOperand(i));
850     NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
851   }
852   return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
853                                 array_lengthof(NewOps));
854 }
855
856 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
857   assert(OpNo == 0 && "Only know how to promote condition");
858
859   // Promote all the way up to the canonical SetCC type.
860   MVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
861   SDValue Cond = PromoteTargetBoolean(N->getOperand(0), SVT);
862
863   return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
864                                 N->getOperand(1), N->getOperand(2));
865 }
866
867 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
868   assert(OpNo == 0 && "Don't know how to promote this operand!");
869
870   SDValue LHS = N->getOperand(0);
871   SDValue RHS = N->getOperand(1);
872   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
873
874   // The CC (#4) and the possible return values (#2 and #3) have legal types.
875   return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
876                                 N->getOperand(3), N->getOperand(4));
877 }
878
879 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
880   assert(OpNo == 0 && "Don't know how to promote this operand!");
881
882   SDValue LHS = N->getOperand(0);
883   SDValue RHS = N->getOperand(1);
884   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
885
886   // The CC (#2) is always legal.
887   return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
888 }
889
890 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
891   SDValue Op = GetPromotedInteger(N->getOperand(0));
892   DebugLoc dl = N->getDebugLoc();
893   Op = DAG.getNode(ISD::ANY_EXTEND, dl, N->getValueType(0), Op);
894   return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Op.getValueType(),
895                      Op, DAG.getValueType(N->getOperand(0).getValueType()));
896 }
897
898 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
899   return DAG.UpdateNodeOperands(SDValue(N, 0),
900                                 SExtPromotedInteger(N->getOperand(0)));
901 }
902
903 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
904   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
905   SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
906   int SVOffset = N->getSrcValueOffset();
907   unsigned Alignment = N->getAlignment();
908   bool isVolatile = N->isVolatile();
909   DebugLoc dl = N->getDebugLoc();
910
911   SDValue Val = GetPromotedInteger(N->getValue());  // Get promoted value.
912
913   // Truncate the value and store the result.
914   return DAG.getTruncStore(Ch, dl, Val, Ptr, N->getSrcValue(),
915                            SVOffset, N->getMemoryVT(),
916                            isVolatile, Alignment);
917 }
918
919 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
920   SDValue Op = GetPromotedInteger(N->getOperand(0));
921   return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), Op);
922 }
923
924 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
925   return DAG.UpdateNodeOperands(SDValue(N, 0),
926                                 ZExtPromotedInteger(N->getOperand(0)));
927 }
928
929 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
930   SDValue Op = GetPromotedInteger(N->getOperand(0));
931   Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), N->getValueType(0), Op);
932   return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
933 }
934
935
936 //===----------------------------------------------------------------------===//
937 //  Integer Result Expansion
938 //===----------------------------------------------------------------------===//
939
940 /// ExpandIntegerResult - This method is called when the specified result of the
941 /// specified node is found to need expansion.  At this point, the node may also
942 /// have invalid operands or may have other results that need promotion, we just
943 /// know that (at least) one result needs expansion.
944 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
945   DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
946   SDValue Lo, Hi;
947   Lo = Hi = SDValue();
948
949   // See if the target wants to custom expand this node.
950   if (CustomLowerResults(N, N->getValueType(ResNo), true))
951     return;
952
953   switch (N->getOpcode()) {
954   default:
955 #ifndef NDEBUG
956     cerr << "ExpandIntegerResult #" << ResNo << ": ";
957     N->dump(&DAG); cerr << "\n";
958 #endif
959     assert(0 && "Do not know how to expand the result of this operator!");
960     abort();
961
962   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
963   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
964   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
965   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
966
967   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
968   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
969   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
970   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
971   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
972
973   case ISD::ANY_EXTEND:  ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
974   case ISD::AssertSext:  ExpandIntRes_AssertSext(N, Lo, Hi); break;
975   case ISD::AssertZext:  ExpandIntRes_AssertZext(N, Lo, Hi); break;
976   case ISD::BSWAP:       ExpandIntRes_BSWAP(N, Lo, Hi); break;
977   case ISD::Constant:    ExpandIntRes_Constant(N, Lo, Hi); break;
978   case ISD::CTLZ:        ExpandIntRes_CTLZ(N, Lo, Hi); break;
979   case ISD::CTPOP:       ExpandIntRes_CTPOP(N, Lo, Hi); break;
980   case ISD::CTTZ:        ExpandIntRes_CTTZ(N, Lo, Hi); break;
981   case ISD::FP_TO_SINT:  ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
982   case ISD::FP_TO_UINT:  ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
983   case ISD::LOAD:        ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
984   case ISD::MUL:         ExpandIntRes_MUL(N, Lo, Hi); break;
985   case ISD::SDIV:        ExpandIntRes_SDIV(N, Lo, Hi); break;
986   case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
987   case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
988   case ISD::SREM:        ExpandIntRes_SREM(N, Lo, Hi); break;
989   case ISD::TRUNCATE:    ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
990   case ISD::UDIV:        ExpandIntRes_UDIV(N, Lo, Hi); break;
991   case ISD::UREM:        ExpandIntRes_UREM(N, Lo, Hi); break;
992   case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
993
994   case ISD::AND:
995   case ISD::OR:
996   case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
997
998   case ISD::ADD:
999   case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1000
1001   case ISD::ADDC:
1002   case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1003
1004   case ISD::ADDE:
1005   case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1006
1007   case ISD::SHL:
1008   case ISD::SRA:
1009   case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1010   }
1011
1012   // If Lo/Hi is null, the sub-method took care of registering results etc.
1013   if (Lo.getNode())
1014     SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1015 }
1016
1017 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1018 /// and the shift amount is a constant 'Amt'.  Expand the operation.
1019 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1020                                              SDValue &Lo, SDValue &Hi) {
1021   DebugLoc dl = N->getDebugLoc();
1022   // Expand the incoming operand to be shifted, so that we have its parts
1023   SDValue InL, InH;
1024   GetExpandedInteger(N->getOperand(0), InL, InH);
1025
1026   MVT NVT = InL.getValueType();
1027   unsigned VTBits = N->getValueType(0).getSizeInBits();
1028   unsigned NVTBits = NVT.getSizeInBits();
1029   MVT ShTy = N->getOperand(1).getValueType();
1030
1031   if (N->getOpcode() == ISD::SHL) {
1032     if (Amt > VTBits) {
1033       Lo = Hi = DAG.getConstant(0, NVT);
1034     } else if (Amt > NVTBits) {
1035       Lo = DAG.getConstant(0, NVT);
1036       Hi = DAG.getNode(ISD::SHL, dl, 
1037                        NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1038     } else if (Amt == NVTBits) {
1039       Lo = DAG.getConstant(0, NVT);
1040       Hi = InL;
1041     } else if (Amt == 1 &&
1042                TLI.isOperationLegalOrCustom(ISD::ADDC,
1043                                             TLI.getTypeToExpandTo(NVT))) {
1044       // Emit this X << 1 as X+X.
1045       SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1046       SDValue LoOps[2] = { InL, InL };
1047       Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1048       SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1049       Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1050     } else {
1051       Lo = DAG.getNode(ISD::SHL, dl, NVT, InL, DAG.getConstant(Amt, ShTy));
1052       Hi = DAG.getNode(ISD::OR, dl, NVT,
1053                        DAG.getNode(ISD::SHL, dl, NVT, InH,
1054                                    DAG.getConstant(Amt, ShTy)),
1055                        DAG.getNode(ISD::SRL, dl, NVT, InL,
1056                                    DAG.getConstant(NVTBits-Amt, ShTy)));
1057     }
1058     return;
1059   }
1060
1061   if (N->getOpcode() == ISD::SRL) {
1062     if (Amt > VTBits) {
1063       Lo = DAG.getConstant(0, NVT);
1064       Hi = DAG.getConstant(0, NVT);
1065     } else if (Amt > NVTBits) {
1066       Lo = DAG.getNode(ISD::SRL, dl, 
1067                        NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1068       Hi = DAG.getConstant(0, NVT);
1069     } else if (Amt == NVTBits) {
1070       Lo = InH;
1071       Hi = DAG.getConstant(0, NVT);
1072     } else {
1073       Lo = DAG.getNode(ISD::OR, dl, NVT,
1074                        DAG.getNode(ISD::SRL, dl, NVT, InL,
1075                                    DAG.getConstant(Amt, ShTy)),
1076                        DAG.getNode(ISD::SHL, dl, NVT, InH,
1077                                    DAG.getConstant(NVTBits-Amt, ShTy)));
1078       Hi = DAG.getNode(ISD::SRL, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
1079     }
1080     return;
1081   }
1082
1083   assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1084   if (Amt > VTBits) {
1085     Hi = Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
1086                           DAG.getConstant(NVTBits-1, ShTy));
1087   } else if (Amt > NVTBits) {
1088     Lo = DAG.getNode(ISD::SRA, dl, NVT, InH,
1089                      DAG.getConstant(Amt-NVTBits, ShTy));
1090     Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
1091                      DAG.getConstant(NVTBits-1, ShTy));
1092   } else if (Amt == NVTBits) {
1093     Lo = InH;
1094     Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,
1095                      DAG.getConstant(NVTBits-1, ShTy));
1096   } else {
1097     Lo = DAG.getNode(ISD::OR, NVT,
1098                      DAG.getNode(ISD::SRL, dl, NVT, InL,
1099                                  DAG.getConstant(Amt, ShTy)),
1100                      DAG.getNode(ISD::SHL, dl, NVT, InH,
1101                                  DAG.getConstant(NVTBits-Amt, ShTy)));
1102     Hi = DAG.getNode(ISD::SRA, dl, NVT, InH, DAG.getConstant(Amt, ShTy));
1103   }
1104 }
1105
1106 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1107 /// this shift based on knowledge of the high bit of the shift amount.  If we
1108 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1109 /// shift amount.
1110 bool DAGTypeLegalizer::
1111 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1112   SDValue Amt = N->getOperand(1);
1113   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1114   MVT ShTy = Amt.getValueType();
1115   unsigned ShBits = ShTy.getSizeInBits();
1116   unsigned NVTBits = NVT.getSizeInBits();
1117   assert(isPowerOf2_32(NVTBits) &&
1118          "Expanded integer type size not a power of two!");
1119   DebugLoc dl = N->getDebugLoc();
1120
1121   APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1122   APInt KnownZero, KnownOne;
1123   DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1124
1125   // If we don't know anything about the high bits, exit.
1126   if (((KnownZero|KnownOne) & HighBitMask) == 0)
1127     return false;
1128
1129   // Get the incoming operand to be shifted.
1130   SDValue InL, InH;
1131   GetExpandedInteger(N->getOperand(0), InL, InH);
1132
1133   // If we know that any of the high bits of the shift amount are one, then we
1134   // can do this as a couple of simple shifts.
1135   if (KnownOne.intersects(HighBitMask)) {
1136     // Mask out the high bit, which we know is set.
1137     Amt = DAG.getNode(ISD::AND, dl, ShTy, Amt,
1138                       DAG.getConstant(~HighBitMask, ShTy));
1139
1140     switch (N->getOpcode()) {
1141     default: assert(0 && "Unknown shift");
1142     case ISD::SHL:
1143       Lo = DAG.getConstant(0, NVT);              // Low part is zero.
1144       Hi = DAG.getNode(ISD::SHL, dl, NVT, InL, Amt); // High part from Lo part.
1145       return true;
1146     case ISD::SRL:
1147       Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
1148       Lo = DAG.getNode(ISD::SRL, dl, NVT, InH, Amt); // Lo part from Hi part.
1149       return true;
1150     case ISD::SRA:
1151       Hi = DAG.getNode(ISD::SRA, dl, NVT, InH,       // Sign extend high part.
1152                        DAG.getConstant(NVTBits-1, ShTy));
1153       Lo = DAG.getNode(ISD::SRA, dl, NVT, InH, Amt); // Lo part from Hi part.
1154       return true;
1155     }
1156   }
1157
1158 #if 0
1159   // FIXME: This code is broken for shifts with a zero amount!
1160   // If we know that all of the high bits of the shift amount are zero, then we
1161   // can do this as a couple of simple shifts.
1162   if ((KnownZero & HighBitMask) == HighBitMask) {
1163     // Compute 32-amt.
1164     SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1165                                  DAG.getConstant(NVTBits, ShTy),
1166                                  Amt);
1167     unsigned Op1, Op2;
1168     switch (N->getOpcode()) {
1169     default: assert(0 && "Unknown shift");
1170     case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1171     case ISD::SRL:
1172     case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1173     }
1174
1175     Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1176     Hi = DAG.getNode(ISD::OR, NVT,
1177                      DAG.getNode(Op1, NVT, InH, Amt),
1178                      DAG.getNode(Op2, NVT, InL, Amt2));
1179     return true;
1180   }
1181 #endif
1182
1183   return false;
1184 }
1185
1186 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1187                                            SDValue &Lo, SDValue &Hi) {
1188   DebugLoc dl = N->getDebugLoc();
1189   // Expand the subcomponents.
1190   SDValue LHSL, LHSH, RHSL, RHSH;
1191   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1192   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1193
1194   MVT NVT = LHSL.getValueType();
1195   SDValue LoOps[2] = { LHSL, RHSL };
1196   SDValue HiOps[3] = { LHSH, RHSH };
1197
1198   // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1199   // them.  TODO: Teach operation legalization how to expand unsupported
1200   // ADDC/ADDE/SUBC/SUBE.  The problem is that these operations generate
1201   // a carry of type MVT::Flag, but there doesn't seem to be any way to
1202   // generate a value of this type in the expanded code sequence.
1203   bool hasCarry =
1204     TLI.isOperationLegalOrCustom(N->getOpcode() == ISD::ADD ?
1205                                    ISD::ADDC : ISD::SUBC,
1206                                  TLI.getTypeToExpandTo(NVT));
1207
1208   if (hasCarry) {
1209     SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1210     if (N->getOpcode() == ISD::ADD) {
1211       Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1212       HiOps[2] = Lo.getValue(1);
1213       Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1214     } else {
1215       Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
1216       HiOps[2] = Lo.getValue(1);
1217       Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1218     }
1219   } else {
1220     if (N->getOpcode() == ISD::ADD) {
1221       Lo = DAG.getNode(ISD::ADD, dl, NVT, LoOps, 2);
1222       Hi = DAG.getNode(ISD::ADD, dl, NVT, HiOps, 2);
1223       SDValue Cmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[0],
1224                                   ISD::SETULT);
1225       SDValue Carry1 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp1,
1226                                    DAG.getConstant(1, NVT),
1227                                    DAG.getConstant(0, NVT));
1228       SDValue Cmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo, LoOps[1],
1229                                   ISD::SETULT);
1230       SDValue Carry2 = DAG.getNode(ISD::SELECT, dl, NVT, Cmp2,
1231                                    DAG.getConstant(1, NVT), Carry1);
1232       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, Carry2);
1233     } else {
1234       Lo = DAG.getNode(ISD::SUB, dl, NVT, LoOps, 2);
1235       Hi = DAG.getNode(ISD::SUB, dl, NVT, HiOps, 2);
1236       SDValue Cmp =
1237         DAG.getSetCC(TLI.getSetCCResultType(LoOps[0].getValueType()),
1238                      LoOps[0], LoOps[1], ISD::SETULT);
1239       SDValue Borrow = DAG.getNode(ISD::SELECT, dl, NVT, Cmp,
1240                                    DAG.getConstant(1, NVT),
1241                                    DAG.getConstant(0, NVT));
1242       Hi = DAG.getNode(ISD::SUB, dl, NVT, Hi, Borrow);
1243     }
1244   }
1245 }
1246
1247 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1248                                             SDValue &Lo, SDValue &Hi) {
1249   // Expand the subcomponents.
1250   SDValue LHSL, LHSH, RHSL, RHSH;
1251   DebugLoc dl = N->getDebugLoc();
1252   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1253   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1254   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1255   SDValue LoOps[2] = { LHSL, RHSL };
1256   SDValue HiOps[3] = { LHSH, RHSH };
1257
1258   if (N->getOpcode() == ISD::ADDC) {
1259     Lo = DAG.getNode(ISD::ADDC, dl, VTList, LoOps, 2);
1260     HiOps[2] = Lo.getValue(1);
1261     Hi = DAG.getNode(ISD::ADDE, dl, VTList, HiOps, 3);
1262   } else {
1263     Lo = DAG.getNode(ISD::SUBC, dl, VTList, LoOps, 2);
1264     HiOps[2] = Lo.getValue(1);
1265     Hi = DAG.getNode(ISD::SUBE, dl, VTList, HiOps, 3);
1266   }
1267
1268   // Legalized the flag result - switch anything that used the old flag to
1269   // use the new one.
1270   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1271 }
1272
1273 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1274                                             SDValue &Lo, SDValue &Hi) {
1275   // Expand the subcomponents.
1276   SDValue LHSL, LHSH, RHSL, RHSH;
1277   DebugLoc dl = N->getDebugLoc();
1278   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1279   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1280   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1281   SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1282   SDValue HiOps[3] = { LHSH, RHSH };
1283
1284   Lo = DAG.getNode(N->getOpcode(), dl, VTList, LoOps, 3);
1285   HiOps[2] = Lo.getValue(1);
1286   Hi = DAG.getNode(N->getOpcode(), dl, VTList, HiOps, 3);
1287
1288   // Legalized the flag result - switch anything that used the old flag to
1289   // use the new one.
1290   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1291 }
1292
1293 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1294                                                SDValue &Lo, SDValue &Hi) {
1295   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1296   DebugLoc dl = N->getDebugLoc();
1297   SDValue Op = N->getOperand(0);
1298   if (Op.getValueType().bitsLE(NVT)) {
1299     // The low part is any extension of the input (which degenerates to a copy).
1300     Lo = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Op);
1301     Hi = DAG.getNode(ISD::UNDEF, dl, NVT);   // The high part is undefined.
1302   } else {
1303     // For example, extension of an i48 to an i64.  The operand type necessarily
1304     // promotes to the result type, so will end up being expanded too.
1305     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1306            "Only know how to promote this result!");
1307     SDValue Res = GetPromotedInteger(Op);
1308     assert(Res.getValueType() == N->getValueType(0) &&
1309            "Operand over promoted?");
1310     // Split the promoted operand.  This will simplify when it is expanded.
1311     SplitInteger(Res, Lo, Hi);
1312   }
1313 }
1314
1315 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1316                                                SDValue &Lo, SDValue &Hi) {
1317   DebugLoc dl = N->getDebugLoc();
1318   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1319   MVT NVT = Lo.getValueType();
1320   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1321   unsigned NVTBits = NVT.getSizeInBits();
1322   unsigned EVTBits = EVT.getSizeInBits();
1323
1324   if (NVTBits < EVTBits) {
1325     Hi = DAG.getNode(ISD::AssertSext, dl, NVT, Hi,
1326                      DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1327   } else {
1328     Lo = DAG.getNode(ISD::AssertSext, dl, NVT, Lo, DAG.getValueType(EVT));
1329     // The high part replicates the sign bit of Lo, make it explicit.
1330     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1331                      DAG.getConstant(NVTBits-1, TLI.getPointerTy()));
1332   }
1333 }
1334
1335 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1336                                                SDValue &Lo, SDValue &Hi) {
1337   DebugLoc dl = N->getDebugLoc();
1338   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1339   MVT NVT = Lo.getValueType();
1340   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1341   unsigned NVTBits = NVT.getSizeInBits();
1342   unsigned EVTBits = EVT.getSizeInBits();
1343
1344   if (NVTBits < EVTBits) {
1345     Hi = DAG.getNode(ISD::AssertZext, dl, NVT, Hi,
1346                      DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1347   } else {
1348     Lo = DAG.getNode(ISD::AssertZext, dl, NVT, Lo, DAG.getValueType(EVT));
1349     // The high part must be zero, make it explicit.
1350     Hi = DAG.getConstant(0, NVT);
1351   }
1352 }
1353
1354 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1355                                           SDValue &Lo, SDValue &Hi) {
1356   DebugLoc dl = N->getDebugLoc();
1357   GetExpandedInteger(N->getOperand(0), Hi, Lo);  // Note swapped operands.
1358   Lo = DAG.getNode(ISD::BSWAP, dl, Lo.getValueType(), Lo);
1359   Hi = DAG.getNode(ISD::BSWAP, dl, Hi.getValueType(), Hi);
1360 }
1361
1362 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1363                                              SDValue &Lo, SDValue &Hi) {
1364   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1365   unsigned NBitWidth = NVT.getSizeInBits();
1366   const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1367   Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1368   Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1369 }
1370
1371 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1372                                          SDValue &Lo, SDValue &Hi) {
1373   DebugLoc dl = N->getDebugLoc();
1374   // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1375   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1376   MVT NVT = Lo.getValueType();
1377
1378   SDValue HiNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Hi,
1379                                    DAG.getConstant(0, NVT), ISD::SETNE);
1380
1381   SDValue LoLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Lo);
1382   SDValue HiLZ = DAG.getNode(ISD::CTLZ, dl, NVT, Hi);
1383
1384   Lo = DAG.getNode(ISD::SELECT, dl, NVT, HiNotZero, HiLZ,
1385                    DAG.getNode(ISD::ADD, dl, NVT, LoLZ,
1386                                DAG.getConstant(NVT.getSizeInBits(), NVT)));
1387   Hi = DAG.getConstant(0, NVT);
1388 }
1389
1390 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1391                                           SDValue &Lo, SDValue &Hi) {
1392   DebugLoc dl = N->getDebugLoc();
1393   // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1394   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1395   MVT NVT = Lo.getValueType();
1396   Lo = DAG.getNode(ISD::ADD, dl, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
1397                    DAG.getNode(ISD::CTPOP, dl, NVT, Hi));
1398   Hi = DAG.getConstant(0, NVT);
1399 }
1400
1401 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1402                                          SDValue &Lo, SDValue &Hi) {
1403   DebugLoc dl = N->getDebugLoc();
1404   // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1405   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1406   MVT NVT = Lo.getValueType();
1407
1408   SDValue LoNotZero = DAG.getSetCC(dl, TLI.getSetCCResultType(NVT), Lo,
1409                                    DAG.getConstant(0, NVT), ISD::SETNE);
1410
1411   SDValue LoLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Lo);
1412   SDValue HiLZ = DAG.getNode(ISD::CTTZ, dl, NVT, Hi);
1413
1414   Lo = DAG.getNode(ISD::SELECT, dl, NVT, LoNotZero, LoLZ,
1415                    DAG.getNode(ISD::ADD, dl, NVT, HiLZ,
1416                                DAG.getConstant(NVT.getSizeInBits(), NVT)));
1417   Hi = DAG.getConstant(0, NVT);
1418 }
1419
1420 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1421                                                SDValue &Hi) {
1422   DebugLoc dl = N->getDebugLoc();
1423   MVT VT = N->getValueType(0);
1424   SDValue Op = N->getOperand(0);
1425   RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1426   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1427   SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*irrelevant*/, dl), Lo, Hi);
1428 }
1429
1430 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1431                                                SDValue &Hi) {
1432   DebugLoc dl = N->getDebugLoc();
1433   MVT VT = N->getValueType(0);
1434   SDValue Op = N->getOperand(0);
1435   RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1436   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1437   SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*irrelevant*/, dl), Lo, Hi);
1438 }
1439
1440 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1441                                          SDValue &Lo, SDValue &Hi) {
1442   if (ISD::isNormalLoad(N)) {
1443     ExpandRes_NormalLoad(N, Lo, Hi);
1444     return;
1445   }
1446
1447   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1448
1449   MVT VT = N->getValueType(0);
1450   MVT NVT = TLI.getTypeToTransformTo(VT);
1451   SDValue Ch  = N->getChain();
1452   SDValue Ptr = N->getBasePtr();
1453   ISD::LoadExtType ExtType = N->getExtensionType();
1454   int SVOffset = N->getSrcValueOffset();
1455   unsigned Alignment = N->getAlignment();
1456   bool isVolatile = N->isVolatile();
1457   DebugLoc dl = N->getDebugLoc();
1458
1459   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1460
1461   if (N->getMemoryVT().bitsLE(NVT)) {
1462     MVT EVT = N->getMemoryVT();
1463
1464     Lo = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1465                         EVT, isVolatile, Alignment);
1466
1467     // Remember the chain.
1468     Ch = Lo.getValue(1);
1469
1470     if (ExtType == ISD::SEXTLOAD) {
1471       // The high part is obtained by SRA'ing all but one of the bits of the
1472       // lo part.
1473       unsigned LoSize = Lo.getValueType().getSizeInBits();
1474       Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1475                        DAG.getConstant(LoSize-1, TLI.getPointerTy()));
1476     } else if (ExtType == ISD::ZEXTLOAD) {
1477       // The high part is just a zero.
1478       Hi = DAG.getConstant(0, NVT);
1479     } else {
1480       assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1481       // The high part is undefined.
1482       Hi = DAG.getNode(ISD::UNDEF, dl, NVT);
1483     }
1484   } else if (TLI.isLittleEndian()) {
1485     // Little-endian - low bits are at low addresses.
1486     Lo = DAG.getLoad(NVT, dl, Ch, Ptr, N->getSrcValue(), SVOffset,
1487                      isVolatile, Alignment);
1488
1489     unsigned ExcessBits =
1490       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1491     MVT NEVT = MVT::getIntegerVT(ExcessBits);
1492
1493     // Increment the pointer to the other half.
1494     unsigned IncrementSize = NVT.getSizeInBits()/8;
1495     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1496                       DAG.getIntPtrConstant(IncrementSize));
1497     Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(),
1498                         SVOffset+IncrementSize, NEVT,
1499                         isVolatile, MinAlign(Alignment, IncrementSize));
1500
1501     // Build a factor node to remember that this load is independent of the
1502     // other one.
1503     Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1504                      Hi.getValue(1));
1505   } else {
1506     // Big-endian - high bits are at low addresses.  Favor aligned loads at
1507     // the cost of some bit-fiddling.
1508     MVT EVT = N->getMemoryVT();
1509     unsigned EBytes = EVT.getStoreSizeInBits()/8;
1510     unsigned IncrementSize = NVT.getSizeInBits()/8;
1511     unsigned ExcessBits = (EBytes - IncrementSize)*8;
1512
1513     // Load both the high bits and maybe some of the low bits.
1514     Hi = DAG.getExtLoad(ExtType, dl, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1515                         MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1516                         isVolatile, Alignment);
1517
1518     // Increment the pointer to the other half.
1519     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
1520                       DAG.getIntPtrConstant(IncrementSize));
1521     // Load the rest of the low bits.
1522     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, NVT, Ch, Ptr, N->getSrcValue(),
1523                         SVOffset+IncrementSize,
1524                         MVT::getIntegerVT(ExcessBits),
1525                         isVolatile, MinAlign(Alignment, IncrementSize));
1526
1527     // Build a factor node to remember that this load is independent of the
1528     // other one.
1529     Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
1530                      Hi.getValue(1));
1531
1532     if (ExcessBits < NVT.getSizeInBits()) {
1533       // Transfer low bits from the bottom of Hi to the top of Lo.
1534       Lo = DAG.getNode(ISD::OR, dl, NVT, Lo,
1535                        DAG.getNode(ISD::SHL, dl, NVT, Hi,
1536                                    DAG.getConstant(ExcessBits,
1537                                                    TLI.getPointerTy())));
1538       // Move high bits to the right position in Hi.
1539       Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, dl, 
1540                        NVT, Hi,
1541                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1542                                        TLI.getPointerTy()));
1543     }
1544   }
1545
1546   // Legalized the chain result - switch anything that used the old chain to
1547   // use the new one.
1548   ReplaceValueWith(SDValue(N, 1), Ch);
1549 }
1550
1551 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1552                                             SDValue &Lo, SDValue &Hi) {
1553   DebugLoc dl = N->getDebugLoc();
1554   SDValue LL, LH, RL, RH;
1555   GetExpandedInteger(N->getOperand(0), LL, LH);
1556   GetExpandedInteger(N->getOperand(1), RL, RH);
1557   Lo = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LL, RL);
1558   Hi = DAG.getNode(N->getOpcode(), dl, LL.getValueType(), LH, RH);
1559 }
1560
1561 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1562                                         SDValue &Lo, SDValue &Hi) {
1563   MVT VT = N->getValueType(0);
1564   MVT NVT = TLI.getTypeToTransformTo(VT);
1565   DebugLoc dl = N->getDebugLoc();
1566
1567   bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, NVT);
1568   bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, NVT);
1569   bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, NVT);
1570   bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, NVT);
1571   if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1572     SDValue LL, LH, RL, RH;
1573     GetExpandedInteger(N->getOperand(0), LL, LH);
1574     GetExpandedInteger(N->getOperand(1), RL, RH);
1575     unsigned OuterBitSize = VT.getSizeInBits();
1576     unsigned InnerBitSize = NVT.getSizeInBits();
1577     unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1578     unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1579
1580     APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1581     if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1582         DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1583       // The inputs are both zero-extended.
1584       if (HasUMUL_LOHI) {
1585         // We can emit a umul_lohi.
1586         Lo = DAG.getNode(ISD::UMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1587         Hi = SDValue(Lo.getNode(), 1);
1588         return;
1589       }
1590       if (HasMULHU) {
1591         // We can emit a mulhu+mul.
1592         Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1593         Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1594         return;
1595       }
1596     }
1597     if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1598       // The input values are both sign-extended.
1599       if (HasSMUL_LOHI) {
1600         // We can emit a smul_lohi.
1601         Lo = DAG.getNode(ISD::SMUL_LOHI, dl, DAG.getVTList(NVT, NVT), LL, RL);
1602         Hi = SDValue(Lo.getNode(), 1);
1603         return;
1604       }
1605       if (HasMULHS) {
1606         // We can emit a mulhs+mul.
1607         Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1608         Hi = DAG.getNode(ISD::MULHS, dl, NVT, LL, RL);
1609         return;
1610       }
1611     }
1612     if (HasUMUL_LOHI) {
1613       // Lo,Hi = umul LHS, RHS.
1614       SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI, dl,
1615                                        DAG.getVTList(NVT, NVT), LL, RL);
1616       Lo = UMulLOHI;
1617       Hi = UMulLOHI.getValue(1);
1618       RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1619       LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1620       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1621       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1622       return;
1623     }
1624     if (HasMULHU) {
1625       Lo = DAG.getNode(ISD::MUL, dl, NVT, LL, RL);
1626       Hi = DAG.getNode(ISD::MULHU, dl, NVT, LL, RL);
1627       RH = DAG.getNode(ISD::MUL, dl, NVT, LL, RH);
1628       LH = DAG.getNode(ISD::MUL, dl, NVT, LH, RL);
1629       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, RH);
1630       Hi = DAG.getNode(ISD::ADD, dl, NVT, Hi, LH);
1631       return;
1632     }
1633   }
1634
1635   // If nothing else, we can make a libcall.
1636   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1637   if (VT == MVT::i16)
1638     LC = RTLIB::MUL_I16;
1639   else if (VT == MVT::i32)
1640     LC = RTLIB::MUL_I32;
1641   else if (VT == MVT::i64)
1642     LC = RTLIB::MUL_I64;
1643   else if (VT == MVT::i128)
1644     LC = RTLIB::MUL_I128;
1645   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1646
1647   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1648   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*irrelevant*/, dl), Lo, Hi);
1649 }
1650
1651 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1652                                          SDValue &Lo, SDValue &Hi) {
1653   MVT VT = N->getValueType(0);
1654   DebugLoc dl = N->getDebugLoc();
1655
1656   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1657   if (VT == MVT::i32)
1658     LC = RTLIB::SDIV_I32;
1659   else if (VT == MVT::i64)
1660     LC = RTLIB::SDIV_I64;
1661   else if (VT == MVT::i128)
1662     LC = RTLIB::SDIV_I128;
1663   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1664
1665   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1666   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
1667 }
1668
1669 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1670                                           SDValue &Lo, SDValue &Hi) {
1671   MVT VT = N->getValueType(0);
1672   DebugLoc dl = N->getDebugLoc();
1673
1674   // If we can emit an efficient shift operation, do so now.  Check to see if
1675   // the RHS is a constant.
1676   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1677     return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1678
1679   // If we can determine that the high bit of the shift is zero or one, even if
1680   // the low bits are variable, emit this shift in an optimized form.
1681   if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1682     return;
1683
1684   // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
1685   unsigned PartsOpc;
1686   if (N->getOpcode() == ISD::SHL) {
1687     PartsOpc = ISD::SHL_PARTS;
1688   } else if (N->getOpcode() == ISD::SRL) {
1689     PartsOpc = ISD::SRL_PARTS;
1690   } else {
1691     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1692     PartsOpc = ISD::SRA_PARTS;
1693   }
1694
1695   // Next check to see if the target supports this SHL_PARTS operation or if it
1696   // will custom expand it.
1697   MVT NVT = TLI.getTypeToTransformTo(VT);
1698   TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1699   if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1700       Action == TargetLowering::Custom) {
1701     // Expand the subcomponents.
1702     SDValue LHSL, LHSH;
1703     GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1704
1705     SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1706     MVT VT = LHSL.getValueType();
1707     Lo = DAG.getNode(PartsOpc, dl, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1708     Hi = Lo.getValue(1);
1709     return;
1710   }
1711
1712   // Otherwise, emit a libcall.
1713   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1714   bool isSigned;
1715   if (N->getOpcode() == ISD::SHL) {
1716     isSigned = false; /*sign irrelevant*/
1717     if (VT == MVT::i16)
1718       LC = RTLIB::SHL_I16;
1719     else if (VT == MVT::i32)
1720       LC = RTLIB::SHL_I32;
1721     else if (VT == MVT::i64)
1722       LC = RTLIB::SHL_I64;
1723     else if (VT == MVT::i128)
1724       LC = RTLIB::SHL_I128;
1725   } else if (N->getOpcode() == ISD::SRL) {
1726     isSigned = false;
1727     if (VT == MVT::i16)
1728       LC = RTLIB::SRL_I16;
1729     else if (VT == MVT::i32)
1730       LC = RTLIB::SRL_I32;
1731     else if (VT == MVT::i64)
1732       LC = RTLIB::SRL_I64;
1733     else if (VT == MVT::i128)
1734       LC = RTLIB::SRL_I128;
1735   } else {
1736     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1737     isSigned = true;
1738     if (VT == MVT::i16)
1739       LC = RTLIB::SRA_I16;
1740     else if (VT == MVT::i32)
1741       LC = RTLIB::SRA_I32;
1742     else if (VT == MVT::i64)
1743       LC = RTLIB::SRA_I64;
1744     else if (VT == MVT::i128)
1745       LC = RTLIB::SRA_I128;
1746   }
1747   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
1748
1749   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1750   SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned, dl), Lo, Hi);
1751 }
1752
1753 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1754                                                 SDValue &Lo, SDValue &Hi) {
1755   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1756   DebugLoc dl = N->getDebugLoc();
1757   SDValue Op = N->getOperand(0);
1758   if (Op.getValueType().bitsLE(NVT)) {
1759     // The low part is sign extension of the input (degenerates to a copy).
1760     Lo = DAG.getNode(ISD::SIGN_EXTEND, dl, NVT, N->getOperand(0));
1761     // The high part is obtained by SRA'ing all but one of the bits of low part.
1762     unsigned LoSize = NVT.getSizeInBits();
1763     Hi = DAG.getNode(ISD::SRA, dl, NVT, Lo,
1764                      DAG.getConstant(LoSize-1, TLI.getPointerTy()));
1765   } else {
1766     // For example, extension of an i48 to an i64.  The operand type necessarily
1767     // promotes to the result type, so will end up being expanded too.
1768     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1769            "Only know how to promote this result!");
1770     SDValue Res = GetPromotedInteger(Op);
1771     assert(Res.getValueType() == N->getValueType(0) &&
1772            "Operand over promoted?");
1773     // Split the promoted operand.  This will simplify when it is expanded.
1774     SplitInteger(Res, Lo, Hi);
1775     unsigned ExcessBits =
1776       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1777     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
1778                      DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1779   }
1780 }
1781
1782 void DAGTypeLegalizer::
1783 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1784   DebugLoc dl = N->getDebugLoc();
1785   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1786   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1787
1788   if (EVT.bitsLE(Lo.getValueType())) {
1789     // sext_inreg the low part if needed.
1790     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Lo.getValueType(), Lo,
1791                      N->getOperand(1));
1792
1793     // The high part gets the sign extension from the lo-part.  This handles
1794     // things like sextinreg V:i64 from i8.
1795     Hi = DAG.getNode(ISD::SRA, dl, Hi.getValueType(), Lo,
1796                      DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
1797                                      TLI.getPointerTy()));
1798   } else {
1799     // For example, extension of an i48 to an i64.  Leave the low part alone,
1800     // sext_inreg the high part.
1801     unsigned ExcessBits =
1802       EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1803     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, Hi.getValueType(), Hi,
1804                      DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1805   }
1806 }
1807
1808 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1809                                          SDValue &Lo, SDValue &Hi) {
1810   MVT VT = N->getValueType(0);
1811   DebugLoc dl = N->getDebugLoc();
1812
1813   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1814   if (VT == MVT::i32)
1815     LC = RTLIB::SREM_I32;
1816   else if (VT == MVT::i64)
1817     LC = RTLIB::SREM_I64;
1818   else if (VT == MVT::i128)
1819     LC = RTLIB::SREM_I128;
1820   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1821
1822   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1823   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true, dl), Lo, Hi);
1824 }
1825
1826 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1827                                              SDValue &Lo, SDValue &Hi) {
1828   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1829   DebugLoc dl = N->getDebugLoc();
1830   Lo = DAG.getNode(ISD::TRUNCATE, dl, NVT, N->getOperand(0));
1831   Hi = DAG.getNode(ISD::SRL, dl,
1832                    N->getOperand(0).getValueType(), N->getOperand(0),
1833                    DAG.getConstant(NVT.getSizeInBits(), TLI.getPointerTy()));
1834   Hi = DAG.getNode(ISD::TRUNCATE, dl, NVT, Hi);
1835 }
1836
1837 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1838                                          SDValue &Lo, SDValue &Hi) {
1839   MVT VT = N->getValueType(0);
1840   DebugLoc dl = N->getDebugLoc();
1841
1842   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1843   if (VT == MVT::i32)
1844     LC = RTLIB::UDIV_I32;
1845   else if (VT == MVT::i64)
1846     LC = RTLIB::UDIV_I64;
1847   else if (VT == MVT::i128)
1848     LC = RTLIB::UDIV_I128;
1849   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1850
1851   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1852   SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
1853 }
1854
1855 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1856                                          SDValue &Lo, SDValue &Hi) {
1857   MVT VT = N->getValueType(0);
1858   DebugLoc dl = N->getDebugLoc();
1859
1860   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1861   if (VT == MVT::i32)
1862     LC = RTLIB::UREM_I32;
1863   else if (VT == MVT::i64)
1864     LC = RTLIB::UREM_I64;
1865   else if (VT == MVT::i128)
1866     LC = RTLIB::UREM_I128;
1867   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1868
1869   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1870   SplitInteger(MakeLibCall(LC, VT, Ops, 2, false, dl), Lo, Hi);
1871 }
1872
1873 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1874                                                 SDValue &Lo, SDValue &Hi) {
1875   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1876   DebugLoc dl = N->getDebugLoc();
1877   SDValue Op = N->getOperand(0);
1878   if (Op.getValueType().bitsLE(NVT)) {
1879     // The low part is zero extension of the input (degenerates to a copy).
1880     Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N->getOperand(0));
1881     Hi = DAG.getConstant(0, NVT);   // The high part is just a zero.
1882   } else {
1883     // For example, extension of an i48 to an i64.  The operand type necessarily
1884     // promotes to the result type, so will end up being expanded too.
1885     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1886            "Only know how to promote this result!");
1887     SDValue Res = GetPromotedInteger(Op);
1888     assert(Res.getValueType() == N->getValueType(0) &&
1889            "Operand over promoted?");
1890     // Split the promoted operand.  This will simplify when it is expanded.
1891     SplitInteger(Res, Lo, Hi);
1892     unsigned ExcessBits =
1893       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1894     Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
1895   }
1896 }
1897
1898
1899 //===----------------------------------------------------------------------===//
1900 //  Integer Operand Expansion
1901 //===----------------------------------------------------------------------===//
1902
1903 /// ExpandIntegerOperand - This method is called when the specified operand of
1904 /// the specified node is found to need expansion.  At this point, all of the
1905 /// result types of the node are known to be legal, but other operands of the
1906 /// node may need promotion or expansion as well as the specified one.
1907 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
1908   DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1909   SDValue Res = SDValue();
1910
1911   if (CustomLowerResults(N, N->getOperand(OpNo).getValueType(), false))
1912     return false;
1913
1914   switch (N->getOpcode()) {
1915   default:
1916   #ifndef NDEBUG
1917     cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1918     N->dump(&DAG); cerr << "\n";
1919   #endif
1920     assert(0 && "Do not know how to expand this operator's operand!");
1921     abort();
1922
1923   case ISD::BIT_CONVERT:       Res = ExpandOp_BIT_CONVERT(N); break;
1924   case ISD::BR_CC:             Res = ExpandIntOp_BR_CC(N); break;
1925   case ISD::BUILD_VECTOR:      Res = ExpandOp_BUILD_VECTOR(N); break;
1926   case ISD::EXTRACT_ELEMENT:   Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1927   case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
1928   case ISD::SCALAR_TO_VECTOR:  Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
1929   case ISD::SELECT_CC:         Res = ExpandIntOp_SELECT_CC(N); break;
1930   case ISD::SETCC:             Res = ExpandIntOp_SETCC(N); break;
1931   case ISD::SINT_TO_FP:        Res = ExpandIntOp_SINT_TO_FP(N); break;
1932   case ISD::STORE:   Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo); break;
1933   case ISD::TRUNCATE:          Res = ExpandIntOp_TRUNCATE(N); break;
1934   case ISD::UINT_TO_FP:        Res = ExpandIntOp_UINT_TO_FP(N); break;
1935   }
1936
1937   // If the result is null, the sub-method took care of registering results etc.
1938   if (!Res.getNode()) return false;
1939
1940   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1941   // core about this.
1942   if (Res.getNode() == N)
1943     return true;
1944
1945   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1946          "Invalid operand expansion");
1947
1948   ReplaceValueWith(SDValue(N, 0), Res);
1949   return false;
1950 }
1951
1952 /// IntegerExpandSetCCOperands - Expand the operands of a comparison.  This code
1953 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1954 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
1955                                                   SDValue &NewRHS,
1956                                                   ISD::CondCode &CCCode,
1957                                                   DebugLoc dl) {
1958   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1959   GetExpandedInteger(NewLHS, LHSLo, LHSHi);
1960   GetExpandedInteger(NewRHS, RHSLo, RHSHi);
1961
1962   MVT VT = NewLHS.getValueType();
1963
1964   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1965     if (RHSLo == RHSHi) {
1966       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
1967         if (RHSCST->isAllOnesValue()) {
1968           // Equality comparison to -1.
1969           NewLHS = DAG.getNode(ISD::AND, dl, 
1970                                LHSLo.getValueType(), LHSLo, LHSHi);
1971           NewRHS = RHSLo;
1972           return;
1973         }
1974       }
1975     }
1976
1977     NewLHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSLo, RHSLo);
1978     NewRHS = DAG.getNode(ISD::XOR, dl, LHSLo.getValueType(), LHSHi, RHSHi);
1979     NewLHS = DAG.getNode(ISD::OR, dl, NewLHS.getValueType(), NewLHS, NewRHS);
1980     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1981     return;
1982   }
1983
1984   // If this is a comparison of the sign bit, just look at the top part.
1985   // X > -1,  x < 0
1986   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1987     if ((CCCode == ISD::SETLT && CST->isNullValue()) ||     // X < 0
1988         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
1989       NewLHS = LHSHi;
1990       NewRHS = RHSHi;
1991       return;
1992     }
1993
1994   // FIXME: This generated code sucks.
1995   ISD::CondCode LowCC;
1996   switch (CCCode) {
1997   default: assert(0 && "Unknown integer setcc!");
1998   case ISD::SETLT:
1999   case ISD::SETULT: LowCC = ISD::SETULT; break;
2000   case ISD::SETGT:
2001   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2002   case ISD::SETLE:
2003   case ISD::SETULE: LowCC = ISD::SETULE; break;
2004   case ISD::SETGE:
2005   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2006   }
2007
2008   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
2009   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
2010   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2011
2012   // NOTE: on targets without efficient SELECT of bools, we can always use
2013   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2014   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
2015   SDValue Tmp1, Tmp2;
2016   Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
2017                            LHSLo, RHSLo, LowCC, false, DagCombineInfo);
2018   if (!Tmp1.getNode())
2019     Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
2020                         LHSLo, RHSLo, LowCC);
2021   Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2022                            LHSHi, RHSHi, CCCode, false, DagCombineInfo);
2023   if (!Tmp2.getNode())
2024     Tmp2 = DAG.getNode(ISD::SETCC, dl,
2025                        TLI.getSetCCResultType(LHSHi.getValueType()),
2026                        LHSHi, RHSHi, DAG.getCondCode(CCCode));
2027
2028   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2029   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2030   if ((Tmp1C && Tmp1C->isNullValue()) ||
2031       (Tmp2C && Tmp2C->isNullValue() &&
2032        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2033         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2034       (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2035        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2036         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2037     // low part is known false, returns high part.
2038     // For LE / GE, if high part is known false, ignore the low part.
2039     // For LT / GT, if high part is known true, ignore the low part.
2040     NewLHS = Tmp2;
2041     NewRHS = SDValue();
2042     return;
2043   }
2044
2045   NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2046                              LHSHi, RHSHi, ISD::SETEQ, false, DagCombineInfo);
2047   if (!NewLHS.getNode())
2048     NewLHS = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
2049                           LHSHi, RHSHi, ISD::SETEQ);
2050   NewLHS = DAG.getNode(ISD::SELECT, dl, Tmp1.getValueType(),
2051                        NewLHS, Tmp1, Tmp2);
2052   NewRHS = SDValue();
2053 }
2054
2055 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2056   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2057   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2058   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
2059
2060   // If ExpandSetCCOperands returned a scalar, we need to compare the result
2061   // against zero to select between true and false values.
2062   if (NewRHS.getNode() == 0) {
2063     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2064     CCCode = ISD::SETNE;
2065   }
2066
2067   // Update N to have the operands specified.
2068   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
2069                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2070                                 N->getOperand(4));
2071 }
2072
2073 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2074   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2075   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2076   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
2077
2078   // If ExpandSetCCOperands returned a scalar, we need to compare the result
2079   // against zero to select between true and false values.
2080   if (NewRHS.getNode() == 0) {
2081     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2082     CCCode = ISD::SETNE;
2083   }
2084
2085   // Update N to have the operands specified.
2086   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2087                                 N->getOperand(2), N->getOperand(3),
2088                                 DAG.getCondCode(CCCode));
2089 }
2090
2091 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2092   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2093   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2094   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
2095
2096   // If ExpandSetCCOperands returned a scalar, use it.
2097   if (NewRHS.getNode() == 0) {
2098     assert(NewLHS.getValueType() == N->getValueType(0) &&
2099            "Unexpected setcc expansion!");
2100     return NewLHS;
2101   }
2102
2103   // Otherwise, update N to have the operands specified.
2104   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2105                                 DAG.getCondCode(CCCode));
2106 }
2107
2108 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2109   SDValue Op = N->getOperand(0);
2110   MVT DstVT = N->getValueType(0);
2111   RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2112   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2113          "Don't know how to expand this SINT_TO_FP!");
2114   return MakeLibCall(LC, DstVT, &Op, 1, true, N->getDebugLoc());
2115 }
2116
2117 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2118   if (ISD::isNormalStore(N))
2119     return ExpandOp_NormalStore(N, OpNo);
2120
2121   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2122   assert(OpNo == 1 && "Can only expand the stored value so far");
2123
2124   MVT VT = N->getOperand(1).getValueType();
2125   MVT NVT = TLI.getTypeToTransformTo(VT);
2126   SDValue Ch  = N->getChain();
2127   SDValue Ptr = N->getBasePtr();
2128   int SVOffset = N->getSrcValueOffset();
2129   unsigned Alignment = N->getAlignment();
2130   bool isVolatile = N->isVolatile();
2131   DebugLoc dl = N->getDebugLoc();
2132   SDValue Lo, Hi;
2133
2134   assert(NVT.isByteSized() && "Expanded type not byte sized!");
2135
2136   if (N->getMemoryVT().bitsLE(NVT)) {
2137     GetExpandedInteger(N->getValue(), Lo, Hi);
2138     return DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
2139                              N->getMemoryVT(), isVolatile, Alignment);
2140   } else if (TLI.isLittleEndian()) {
2141     // Little-endian - low bits are at low addresses.
2142     GetExpandedInteger(N->getValue(), Lo, Hi);
2143
2144     Lo = DAG.getStore(Ch, dl, Lo, Ptr, N->getSrcValue(), SVOffset,
2145                       isVolatile, Alignment);
2146
2147     unsigned ExcessBits =
2148       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2149     MVT NEVT = MVT::getIntegerVT(ExcessBits);
2150
2151     // Increment the pointer to the other half.
2152     unsigned IncrementSize = NVT.getSizeInBits()/8;
2153     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2154                       DAG.getIntPtrConstant(IncrementSize));
2155     Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
2156                            SVOffset+IncrementSize, NEVT,
2157                            isVolatile, MinAlign(Alignment, IncrementSize));
2158     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2159   } else {
2160     // Big-endian - high bits are at low addresses.  Favor aligned stores at
2161     // the cost of some bit-fiddling.
2162     GetExpandedInteger(N->getValue(), Lo, Hi);
2163
2164     MVT EVT = N->getMemoryVT();
2165     unsigned EBytes = EVT.getStoreSizeInBits()/8;
2166     unsigned IncrementSize = NVT.getSizeInBits()/8;
2167     unsigned ExcessBits = (EBytes - IncrementSize)*8;
2168     MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2169
2170     if (ExcessBits < NVT.getSizeInBits()) {
2171       // Transfer high bits from the top of Lo to the bottom of Hi.
2172       Hi = DAG.getNode(ISD::SHL, dl, NVT, Hi,
2173                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2174                                        TLI.getPointerTy()));
2175       Hi = DAG.getNode(ISD::OR, dl, NVT, Hi,
2176                        DAG.getNode(ISD::SRL, NVT, Lo,
2177                                    DAG.getConstant(ExcessBits,
2178                                                    TLI.getPointerTy())));
2179     }
2180
2181     // Store both the high bits and maybe some of the low bits.
2182     Hi = DAG.getTruncStore(Ch, dl, Hi, Ptr, N->getSrcValue(),
2183                            SVOffset, HiVT, isVolatile, Alignment);
2184
2185     // Increment the pointer to the other half.
2186     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
2187                       DAG.getIntPtrConstant(IncrementSize));
2188     // Store the lowest ExcessBits bits in the second half.
2189     Lo = DAG.getTruncStore(Ch, dl, Lo, Ptr, N->getSrcValue(),
2190                            SVOffset+IncrementSize,
2191                            MVT::getIntegerVT(ExcessBits),
2192                            isVolatile, MinAlign(Alignment, IncrementSize));
2193     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
2194   }
2195 }
2196
2197 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2198   SDValue InL, InH;
2199   GetExpandedInteger(N->getOperand(0), InL, InH);
2200   // Just truncate the low part of the source.
2201   return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), N->getValueType(0), InL);
2202 }
2203
2204 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2205   SDValue Op = N->getOperand(0);
2206   MVT SrcVT = Op.getValueType();
2207   MVT DstVT = N->getValueType(0);
2208   DebugLoc dl = N->getDebugLoc();
2209
2210   if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2211     // Do a signed conversion then adjust the result.
2212     SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Op);
2213     SignedConv = TLI.LowerOperation(SignedConv, DAG);
2214
2215     // The result of the signed conversion needs adjusting if the 'sign bit' of
2216     // the incoming integer was set.  To handle this, we dynamically test to see
2217     // if it is set, and, if so, add a fudge factor.
2218
2219     const uint64_t F32TwoE32  = 0x4F800000ULL;
2220     const uint64_t F32TwoE64  = 0x5F800000ULL;
2221     const uint64_t F32TwoE128 = 0x7F800000ULL;
2222
2223     APInt FF(32, 0);
2224     if (SrcVT == MVT::i32)
2225       FF = APInt(32, F32TwoE32);
2226     else if (SrcVT == MVT::i64)
2227       FF = APInt(32, F32TwoE64);
2228     else if (SrcVT == MVT::i128)
2229       FF = APInt(32, F32TwoE128);
2230     else
2231       assert(false && "Unsupported UINT_TO_FP!");
2232
2233     // Check whether the sign bit is set.
2234     SDValue Lo, Hi;
2235     GetExpandedInteger(Op, Lo, Hi);
2236     SDValue SignSet = DAG.getSetCC(dl, 
2237                                    TLI.getSetCCResultType(Hi.getValueType()),
2238                                    Hi, DAG.getConstant(0, Hi.getValueType()),
2239                                    ISD::SETLT);
2240
2241     // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2242     SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
2243                                            TLI.getPointerTy());
2244
2245     // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2246     SDValue Zero = DAG.getIntPtrConstant(0);
2247     SDValue Four = DAG.getIntPtrConstant(4);
2248     if (TLI.isBigEndian()) std::swap(Zero, Four);
2249     SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
2250                                  Zero, Four);
2251     unsigned Alignment =
2252       1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2253     FudgePtr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), FudgePtr, Offset);
2254     Alignment = std::min(Alignment, 4u);
2255
2256     // Load the value out, extending it from f32 to the destination float type.
2257     // FIXME: Avoid the extend by constructing the right constant pool?
2258     SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, DstVT, DAG.getEntryNode(),
2259                                    FudgePtr, NULL, 0, MVT::f32,
2260                                    false, Alignment);
2261     return DAG.getNode(ISD::FADD, dl, DstVT, SignedConv, Fudge);
2262   }
2263
2264   // Otherwise, use a libcall.
2265   RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2266   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2267          "Don't know how to expand this UINT_TO_FP!");
2268   return MakeLibCall(LC, DstVT, &Op, 1, true, dl);
2269 }