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