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