Fix PR3274: when promoting the condition of a BRCOND node,
[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).getValueType());
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   // Promote all the way up to SVT, the canonical SetCC type.
732   MVT SVT = TLI.getSetCCResultType(MVT::Other);
733   assert(isTypeLegal(SVT) && "Illegal SetCC type!");
734   assert(Cond.getValueType().bitsLE(SVT) && "Unexpected SetCC type!");
735
736   // Make sure the extra bits conform to getBooleanContents.  There are
737   // two sets of extra bits: those in Cond, which come from type promotion,
738   // and those we need to add to have the final type be SVT (for most targets
739   // this last set of bits is empty).
740   unsigned CondBits = Cond.getValueSizeInBits();
741   ISD::NodeType ExtendCode;
742   switch (TLI.getBooleanContents()) {
743   default:
744     assert(false && "Unknown BooleanContent!");
745   case TargetLowering::UndefinedBooleanContent:
746     // Extend to SVT by adding rubbish.
747     ExtendCode = ISD::ANY_EXTEND;
748     break;
749   case TargetLowering::ZeroOrOneBooleanContent:
750     ExtendCode = ISD::ZERO_EXTEND;
751     if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
752       // All extra bits need to be cleared.  Do this by zero extending the
753       // original condition value all the way to SVT.
754       Cond = N->getOperand(1);
755     break;
756   case TargetLowering::ZeroOrNegativeOneBooleanContent: {
757     ExtendCode = ISD::SIGN_EXTEND;
758     unsigned SignBits = DAG.ComputeNumSignBits(Cond);
759     if (SignBits != CondBits)
760       // All extra bits need to be sign extended.  Do this by sign extending the
761       // original condition value all the way to SVT.
762       Cond = N->getOperand(1);
763     break;
764   }
765   }
766   Cond = DAG.getNode(ExtendCode, SVT, Cond);
767
768   // The chain (Op#0) and basic block destination (Op#2) are always legal types.
769   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0), Cond,
770                                 N->getOperand(2));
771 }
772
773 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_PAIR(SDNode *N) {
774   // Since the result type is legal, the operands must promote to it.
775   MVT OVT = N->getOperand(0).getValueType();
776   SDValue Lo = ZExtPromotedInteger(N->getOperand(0));
777   SDValue Hi = GetPromotedInteger(N->getOperand(1));
778   assert(Lo.getValueType() == N->getValueType(0) && "Operand over promoted?");
779
780   Hi = DAG.getNode(ISD::SHL, N->getValueType(0), Hi,
781                    DAG.getConstant(OVT.getSizeInBits(),
782                                    TLI.getShiftAmountTy()));
783   return DAG.getNode(ISD::OR, N->getValueType(0), Lo, Hi);
784 }
785
786 SDValue DAGTypeLegalizer::PromoteIntOp_BUILD_VECTOR(SDNode *N) {
787   // The vector type is legal but the element type is not.  This implies
788   // that the vector is a power-of-two in length and that the element
789   // type does not have a strange size (eg: it is not i1).
790   MVT VecVT = N->getValueType(0);
791   unsigned NumElts = VecVT.getVectorNumElements();
792   assert(!(NumElts & 1) && "Legal vector of one illegal element?");
793
794   // Build a vector of half the length out of elements of twice the bitwidth.
795   // For example <4 x i16> -> <2 x i32>.
796   MVT OldVT = N->getOperand(0).getValueType();
797   MVT NewVT = MVT::getIntegerVT(2 * OldVT.getSizeInBits());
798   assert(OldVT.isSimple() && NewVT.isSimple());
799
800   std::vector<SDValue> NewElts;
801   NewElts.reserve(NumElts/2);
802
803   for (unsigned i = 0; i < NumElts; i += 2) {
804     // Combine two successive elements into one promoted element.
805     SDValue Lo = N->getOperand(i);
806     SDValue Hi = N->getOperand(i+1);
807     if (TLI.isBigEndian())
808       std::swap(Lo, Hi);
809     NewElts.push_back(JoinIntegers(Lo, Hi));
810   }
811
812   SDValue NewVec = DAG.getNode(ISD::BUILD_VECTOR,
813                                  MVT::getVectorVT(NewVT, NewElts.size()),
814                                  &NewElts[0], NewElts.size());
815
816   // Convert the new vector to the old vector type.
817   return DAG.getNode(ISD::BIT_CONVERT, VecVT, NewVec);
818 }
819
820 SDValue DAGTypeLegalizer::PromoteIntOp_CONVERT_RNDSAT(SDNode *N) {
821   ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(N)->getCvtCode();
822   assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
823            CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
824            CvtCode == ISD::CVT_FS || CvtCode == ISD::CVT_FU) &&
825            "can only promote integer arguments");
826   SDValue InOp = GetPromotedInteger(N->getOperand(0));
827   return DAG.getConvertRndSat(N->getValueType(0), InOp,
828                               N->getOperand(1), N->getOperand(2),
829                               N->getOperand(3), N->getOperand(4), CvtCode);
830 }
831
832 SDValue DAGTypeLegalizer::PromoteIntOp_INSERT_VECTOR_ELT(SDNode *N,
833                                                          unsigned OpNo) {
834   if (OpNo == 1) {
835     // Promote the inserted value.  This is valid because the type does not
836     // have to match the vector element type.
837
838     // Check that any extra bits introduced will be truncated away.
839     assert(N->getOperand(1).getValueType().getSizeInBits() >=
840            N->getValueType(0).getVectorElementType().getSizeInBits() &&
841            "Type of inserted value narrower than vector element type!");
842     return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
843                                   GetPromotedInteger(N->getOperand(1)),
844                                   N->getOperand(2));
845   }
846
847   assert(OpNo == 2 && "Different operand and result vector types?");
848
849   // Promote the index.
850   SDValue Idx = ZExtPromotedInteger(N->getOperand(2));
851   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
852                                 N->getOperand(1), Idx);
853 }
854
855 SDValue DAGTypeLegalizer::PromoteIntOp_MEMBARRIER(SDNode *N) {
856   SDValue NewOps[6];
857   NewOps[0] = N->getOperand(0);
858   for (unsigned i = 1; i < array_lengthof(NewOps); ++i) {
859     SDValue Flag = GetPromotedInteger(N->getOperand(i));
860     NewOps[i] = DAG.getZeroExtendInReg(Flag, MVT::i1);
861   }
862   return DAG.UpdateNodeOperands(SDValue (N, 0), NewOps,
863                                 array_lengthof(NewOps));
864 }
865
866 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT(SDNode *N, unsigned OpNo) {
867   assert(OpNo == 0 && "Only know how to promote condition");
868   SDValue Cond = GetPromotedInteger(N->getOperand(0));
869
870   // Promote all the way up to SVT, the canonical SetCC type.
871   MVT SVT = TLI.getSetCCResultType(N->getOperand(1).getValueType());
872   assert(isTypeLegal(SVT) && "Illegal SetCC type!");
873   assert(Cond.getValueType().bitsLE(SVT) && "Unexpected SetCC type!");
874
875   // Make sure the extra bits conform to getBooleanContents.  There are
876   // two sets of extra bits: those in Cond, which come from type promotion,
877   // and those we need to add to have the final type be SVT (for most targets
878   // this last set of bits is empty).
879   unsigned CondBits = Cond.getValueSizeInBits();
880   ISD::NodeType ExtendCode;
881   switch (TLI.getBooleanContents()) {
882   default:
883     assert(false && "Unknown BooleanContent!");
884   case TargetLowering::UndefinedBooleanContent:
885     // Extend to SVT by adding rubbish.
886     ExtendCode = ISD::ANY_EXTEND;
887     break;
888   case TargetLowering::ZeroOrOneBooleanContent:
889     ExtendCode = ISD::ZERO_EXTEND;
890     if (!DAG.MaskedValueIsZero(Cond,APInt::getHighBitsSet(CondBits,CondBits-1)))
891       // All extra bits need to be cleared.  Do this by zero extending the
892       // original condition value all the way to SVT.
893       Cond = N->getOperand(0);
894     break;
895   case TargetLowering::ZeroOrNegativeOneBooleanContent: {
896     ExtendCode = ISD::SIGN_EXTEND;
897     unsigned SignBits = DAG.ComputeNumSignBits(Cond);
898     if (SignBits != CondBits)
899       // All extra bits need to be sign extended.  Do this by sign extending the
900       // original condition value all the way to SVT.
901       Cond = N->getOperand(0);
902     break;
903   }
904   }
905   Cond = DAG.getNode(ExtendCode, SVT, Cond);
906
907   return DAG.UpdateNodeOperands(SDValue(N, 0), Cond,
908                                 N->getOperand(1), N->getOperand(2));
909 }
910
911 SDValue DAGTypeLegalizer::PromoteIntOp_SELECT_CC(SDNode *N, unsigned OpNo) {
912   assert(OpNo == 0 && "Don't know how to promote this operand!");
913
914   SDValue LHS = N->getOperand(0);
915   SDValue RHS = N->getOperand(1);
916   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(4))->get());
917
918   // The CC (#4) and the possible return values (#2 and #3) have legal types.
919   return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2),
920                                 N->getOperand(3), N->getOperand(4));
921 }
922
923 SDValue DAGTypeLegalizer::PromoteIntOp_SETCC(SDNode *N, unsigned OpNo) {
924   assert(OpNo == 0 && "Don't know how to promote this operand!");
925
926   SDValue LHS = N->getOperand(0);
927   SDValue RHS = N->getOperand(1);
928   PromoteSetCCOperands(LHS, RHS, cast<CondCodeSDNode>(N->getOperand(2))->get());
929
930   // The CC (#2) is always legal.
931   return DAG.UpdateNodeOperands(SDValue(N, 0), LHS, RHS, N->getOperand(2));
932 }
933
934 SDValue DAGTypeLegalizer::PromoteIntOp_SIGN_EXTEND(SDNode *N) {
935   SDValue Op = GetPromotedInteger(N->getOperand(0));
936   Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
937   return DAG.getNode(ISD::SIGN_EXTEND_INREG, Op.getValueType(),
938                      Op, DAG.getValueType(N->getOperand(0).getValueType()));
939 }
940
941 SDValue DAGTypeLegalizer::PromoteIntOp_SINT_TO_FP(SDNode *N) {
942   return DAG.UpdateNodeOperands(SDValue(N, 0),
943                                 SExtPromotedInteger(N->getOperand(0)));
944 }
945
946 SDValue DAGTypeLegalizer::PromoteIntOp_STORE(StoreSDNode *N, unsigned OpNo){
947   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
948   SDValue Ch = N->getChain(), Ptr = N->getBasePtr();
949   int SVOffset = N->getSrcValueOffset();
950   unsigned Alignment = N->getAlignment();
951   bool isVolatile = N->isVolatile();
952
953   SDValue Val = GetPromotedInteger(N->getValue());  // Get promoted value.
954
955   // Truncate the value and store the result.
956   return DAG.getTruncStore(Ch, Val, Ptr, N->getSrcValue(),
957                            SVOffset, N->getMemoryVT(),
958                            isVolatile, Alignment);
959 }
960
961 SDValue DAGTypeLegalizer::PromoteIntOp_TRUNCATE(SDNode *N) {
962   SDValue Op = GetPromotedInteger(N->getOperand(0));
963   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), Op);
964 }
965
966 SDValue DAGTypeLegalizer::PromoteIntOp_UINT_TO_FP(SDNode *N) {
967   return DAG.UpdateNodeOperands(SDValue(N, 0),
968                                 ZExtPromotedInteger(N->getOperand(0)));
969 }
970
971 SDValue DAGTypeLegalizer::PromoteIntOp_ZERO_EXTEND(SDNode *N) {
972   SDValue Op = GetPromotedInteger(N->getOperand(0));
973   Op = DAG.getNode(ISD::ANY_EXTEND, N->getValueType(0), Op);
974   return DAG.getZeroExtendInReg(Op, N->getOperand(0).getValueType());
975 }
976
977
978 //===----------------------------------------------------------------------===//
979 //  Integer Result Expansion
980 //===----------------------------------------------------------------------===//
981
982 /// ExpandIntegerResult - This method is called when the specified result of the
983 /// specified node is found to need expansion.  At this point, the node may also
984 /// have invalid operands or may have other results that need promotion, we just
985 /// know that (at least) one result needs expansion.
986 void DAGTypeLegalizer::ExpandIntegerResult(SDNode *N, unsigned ResNo) {
987   DEBUG(cerr << "Expand integer result: "; N->dump(&DAG); cerr << "\n");
988   SDValue Lo, Hi;
989   Lo = Hi = SDValue();
990
991   // See if the target wants to custom expand this node.
992   if (CustomLowerResults(N, ResNo))
993     return;
994
995   switch (N->getOpcode()) {
996   default:
997 #ifndef NDEBUG
998     cerr << "ExpandIntegerResult #" << ResNo << ": ";
999     N->dump(&DAG); cerr << "\n";
1000 #endif
1001     assert(0 && "Do not know how to expand the result of this operator!");
1002     abort();
1003
1004   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
1005   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
1006   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
1007   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
1008
1009   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
1010   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
1011   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
1012   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
1013   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
1014
1015   case ISD::ANY_EXTEND:  ExpandIntRes_ANY_EXTEND(N, Lo, Hi); break;
1016   case ISD::AssertSext:  ExpandIntRes_AssertSext(N, Lo, Hi); break;
1017   case ISD::AssertZext:  ExpandIntRes_AssertZext(N, Lo, Hi); break;
1018   case ISD::BSWAP:       ExpandIntRes_BSWAP(N, Lo, Hi); break;
1019   case ISD::Constant:    ExpandIntRes_Constant(N, Lo, Hi); break;
1020   case ISD::CTLZ:        ExpandIntRes_CTLZ(N, Lo, Hi); break;
1021   case ISD::CTPOP:       ExpandIntRes_CTPOP(N, Lo, Hi); break;
1022   case ISD::CTTZ:        ExpandIntRes_CTTZ(N, Lo, Hi); break;
1023   case ISD::FP_TO_SINT:  ExpandIntRes_FP_TO_SINT(N, Lo, Hi); break;
1024   case ISD::FP_TO_UINT:  ExpandIntRes_FP_TO_UINT(N, Lo, Hi); break;
1025   case ISD::LOAD:        ExpandIntRes_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
1026   case ISD::MUL:         ExpandIntRes_MUL(N, Lo, Hi); break;
1027   case ISD::SDIV:        ExpandIntRes_SDIV(N, Lo, Hi); break;
1028   case ISD::SIGN_EXTEND: ExpandIntRes_SIGN_EXTEND(N, Lo, Hi); break;
1029   case ISD::SIGN_EXTEND_INREG: ExpandIntRes_SIGN_EXTEND_INREG(N, Lo, Hi); break;
1030   case ISD::SREM:        ExpandIntRes_SREM(N, Lo, Hi); break;
1031   case ISD::TRUNCATE:    ExpandIntRes_TRUNCATE(N, Lo, Hi); break;
1032   case ISD::UDIV:        ExpandIntRes_UDIV(N, Lo, Hi); break;
1033   case ISD::UREM:        ExpandIntRes_UREM(N, Lo, Hi); break;
1034   case ISD::ZERO_EXTEND: ExpandIntRes_ZERO_EXTEND(N, Lo, Hi); break;
1035
1036   case ISD::AND:
1037   case ISD::OR:
1038   case ISD::XOR: ExpandIntRes_Logical(N, Lo, Hi); break;
1039
1040   case ISD::ADD:
1041   case ISD::SUB: ExpandIntRes_ADDSUB(N, Lo, Hi); break;
1042
1043   case ISD::ADDC:
1044   case ISD::SUBC: ExpandIntRes_ADDSUBC(N, Lo, Hi); break;
1045
1046   case ISD::ADDE:
1047   case ISD::SUBE: ExpandIntRes_ADDSUBE(N, Lo, Hi); break;
1048
1049   case ISD::SHL:
1050   case ISD::SRA:
1051   case ISD::SRL: ExpandIntRes_Shift(N, Lo, Hi); break;
1052   }
1053
1054   // If Lo/Hi is null, the sub-method took care of registering results etc.
1055   if (Lo.getNode())
1056     SetExpandedInteger(SDValue(N, ResNo), Lo, Hi);
1057 }
1058
1059 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
1060 /// and the shift amount is a constant 'Amt'.  Expand the operation.
1061 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt,
1062                                              SDValue &Lo, SDValue &Hi) {
1063   // Expand the incoming operand to be shifted, so that we have its parts
1064   SDValue InL, InH;
1065   GetExpandedInteger(N->getOperand(0), InL, InH);
1066
1067   MVT NVT = InL.getValueType();
1068   unsigned VTBits = N->getValueType(0).getSizeInBits();
1069   unsigned NVTBits = NVT.getSizeInBits();
1070   MVT ShTy = N->getOperand(1).getValueType();
1071
1072   if (N->getOpcode() == ISD::SHL) {
1073     if (Amt > VTBits) {
1074       Lo = Hi = DAG.getConstant(0, NVT);
1075     } else if (Amt > NVTBits) {
1076       Lo = DAG.getConstant(0, NVT);
1077       Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
1078     } else if (Amt == NVTBits) {
1079       Lo = DAG.getConstant(0, NVT);
1080       Hi = InL;
1081     } else if (Amt == 1 &&
1082                TLI.isOperationLegal(ISD::ADDC, TLI.getTypeToExpandTo(NVT))) {
1083       // Emit this X << 1 as X+X.
1084       SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1085       SDValue LoOps[2] = { InL, InL };
1086       Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1087       SDValue HiOps[3] = { InH, InH, Lo.getValue(1) };
1088       Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1089     } else {
1090       Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
1091       Hi = DAG.getNode(ISD::OR, NVT,
1092                        DAG.getNode(ISD::SHL, NVT, InH,
1093                                    DAG.getConstant(Amt, ShTy)),
1094                        DAG.getNode(ISD::SRL, NVT, InL,
1095                                    DAG.getConstant(NVTBits-Amt, ShTy)));
1096     }
1097     return;
1098   }
1099
1100   if (N->getOpcode() == ISD::SRL) {
1101     if (Amt > VTBits) {
1102       Lo = DAG.getConstant(0, NVT);
1103       Hi = DAG.getConstant(0, NVT);
1104     } else if (Amt > NVTBits) {
1105       Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
1106       Hi = DAG.getConstant(0, NVT);
1107     } else if (Amt == NVTBits) {
1108       Lo = InH;
1109       Hi = DAG.getConstant(0, NVT);
1110     } else {
1111       Lo = DAG.getNode(ISD::OR, NVT,
1112                        DAG.getNode(ISD::SRL, NVT, InL,
1113                                    DAG.getConstant(Amt, ShTy)),
1114                        DAG.getNode(ISD::SHL, NVT, InH,
1115                                    DAG.getConstant(NVTBits-Amt, ShTy)));
1116       Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
1117     }
1118     return;
1119   }
1120
1121   assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1122   if (Amt > VTBits) {
1123     Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
1124                           DAG.getConstant(NVTBits-1, ShTy));
1125   } else if (Amt > NVTBits) {
1126     Lo = DAG.getNode(ISD::SRA, NVT, InH,
1127                      DAG.getConstant(Amt-NVTBits, ShTy));
1128     Hi = DAG.getNode(ISD::SRA, NVT, InH,
1129                      DAG.getConstant(NVTBits-1, ShTy));
1130   } else if (Amt == NVTBits) {
1131     Lo = InH;
1132     Hi = DAG.getNode(ISD::SRA, NVT, InH,
1133                      DAG.getConstant(NVTBits-1, ShTy));
1134   } else {
1135     Lo = DAG.getNode(ISD::OR, NVT,
1136                      DAG.getNode(ISD::SRL, NVT, InL,
1137                                  DAG.getConstant(Amt, ShTy)),
1138                      DAG.getNode(ISD::SHL, NVT, InH,
1139                                  DAG.getConstant(NVTBits-Amt, ShTy)));
1140     Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
1141   }
1142 }
1143
1144 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
1145 /// this shift based on knowledge of the high bit of the shift amount.  If we
1146 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
1147 /// shift amount.
1148 bool DAGTypeLegalizer::
1149 ExpandShiftWithKnownAmountBit(SDNode *N, SDValue &Lo, SDValue &Hi) {
1150   SDValue Amt = N->getOperand(1);
1151   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1152   MVT ShTy = Amt.getValueType();
1153   unsigned ShBits = ShTy.getSizeInBits();
1154   unsigned NVTBits = NVT.getSizeInBits();
1155   assert(isPowerOf2_32(NVTBits) &&
1156          "Expanded integer type size not a power of two!");
1157
1158   APInt HighBitMask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
1159   APInt KnownZero, KnownOne;
1160   DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
1161
1162   // If we don't know anything about the high bits, exit.
1163   if (((KnownZero|KnownOne) & HighBitMask) == 0)
1164     return false;
1165
1166   // Get the incoming operand to be shifted.
1167   SDValue InL, InH;
1168   GetExpandedInteger(N->getOperand(0), InL, InH);
1169
1170   // If we know that any of the high bits of the shift amount are one, then we
1171   // can do this as a couple of simple shifts.
1172   if (KnownOne.intersects(HighBitMask)) {
1173     // Mask out the high bit, which we know is set.
1174     Amt = DAG.getNode(ISD::AND, ShTy, Amt,
1175                       DAG.getConstant(~HighBitMask, ShTy));
1176
1177     switch (N->getOpcode()) {
1178     default: assert(0 && "Unknown shift");
1179     case ISD::SHL:
1180       Lo = DAG.getConstant(0, NVT);              // Low part is zero.
1181       Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
1182       return true;
1183     case ISD::SRL:
1184       Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
1185       Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
1186       return true;
1187     case ISD::SRA:
1188       Hi = DAG.getNode(ISD::SRA, NVT, InH,       // Sign extend high part.
1189                        DAG.getConstant(NVTBits-1, ShTy));
1190       Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
1191       return true;
1192     }
1193   }
1194
1195 #if 0
1196   // FIXME: This code is broken for shifts with a zero amount!
1197   // If we know that all of the high bits of the shift amount are zero, then we
1198   // can do this as a couple of simple shifts.
1199   if ((KnownZero & HighBitMask) == HighBitMask) {
1200     // Compute 32-amt.
1201     SDValue Amt2 = DAG.getNode(ISD::SUB, ShTy,
1202                                  DAG.getConstant(NVTBits, ShTy),
1203                                  Amt);
1204     unsigned Op1, Op2;
1205     switch (N->getOpcode()) {
1206     default: assert(0 && "Unknown shift");
1207     case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
1208     case ISD::SRL:
1209     case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
1210     }
1211
1212     Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
1213     Hi = DAG.getNode(ISD::OR, NVT,
1214                      DAG.getNode(Op1, NVT, InH, Amt),
1215                      DAG.getNode(Op2, NVT, InL, Amt2));
1216     return true;
1217   }
1218 #endif
1219
1220   return false;
1221 }
1222
1223 void DAGTypeLegalizer::ExpandIntRes_ADDSUB(SDNode *N,
1224                                            SDValue &Lo, SDValue &Hi) {
1225   // Expand the subcomponents.
1226   SDValue LHSL, LHSH, RHSL, RHSH;
1227   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1228   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1229
1230   MVT NVT = LHSL.getValueType();
1231   SDValue LoOps[2] = { LHSL, RHSL };
1232   SDValue HiOps[3] = { LHSH, RHSH };
1233
1234   // Do not generate ADDC/ADDE or SUBC/SUBE if the target does not support
1235   // them.  TODO: Teach operation legalization how to expand unsupported
1236   // ADDC/ADDE/SUBC/SUBE.  The problem is that these operations generate
1237   // a carry of type MVT::Flag, but there doesn't seem to be any way to
1238   // generate a value of this type in the expanded code sequence.
1239   bool hasCarry =
1240     TLI.isOperationLegal(N->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC,
1241                          TLI.getTypeToExpandTo(NVT));
1242
1243   if (hasCarry) {
1244     SDVTList VTList = DAG.getVTList(NVT, MVT::Flag);
1245     if (N->getOpcode() == ISD::ADD) {
1246       Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1247       HiOps[2] = Lo.getValue(1);
1248       Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1249     } else {
1250       Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1251       HiOps[2] = Lo.getValue(1);
1252       Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1253     }
1254   } else {
1255     if (N->getOpcode() == ISD::ADD) {
1256       Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
1257       Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
1258       SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT), Lo, LoOps[0],
1259                                   ISD::SETULT);
1260       SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
1261                                    DAG.getConstant(1, NVT),
1262                                    DAG.getConstant(0, NVT));
1263       SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT), Lo, LoOps[1],
1264                                   ISD::SETULT);
1265       SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
1266                                    DAG.getConstant(1, NVT), Carry1);
1267       Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
1268     } else {
1269       Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
1270       Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
1271       SDValue Cmp =
1272         DAG.getSetCC(TLI.getSetCCResultType(LoOps[0].getValueType()),
1273                      LoOps[0], LoOps[1], ISD::SETULT);
1274       SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
1275                                    DAG.getConstant(1, NVT),
1276                                    DAG.getConstant(0, NVT));
1277       Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
1278     }
1279   }
1280 }
1281
1282 void DAGTypeLegalizer::ExpandIntRes_ADDSUBC(SDNode *N,
1283                                             SDValue &Lo, SDValue &Hi) {
1284   // Expand the subcomponents.
1285   SDValue LHSL, LHSH, RHSL, RHSH;
1286   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1287   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1288   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1289   SDValue LoOps[2] = { LHSL, RHSL };
1290   SDValue HiOps[3] = { LHSH, RHSH };
1291
1292   if (N->getOpcode() == ISD::ADDC) {
1293     Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
1294     HiOps[2] = Lo.getValue(1);
1295     Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
1296   } else {
1297     Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
1298     HiOps[2] = Lo.getValue(1);
1299     Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
1300   }
1301
1302   // Legalized the flag result - switch anything that used the old flag to
1303   // use the new one.
1304   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1305 }
1306
1307 void DAGTypeLegalizer::ExpandIntRes_ADDSUBE(SDNode *N,
1308                                             SDValue &Lo, SDValue &Hi) {
1309   // Expand the subcomponents.
1310   SDValue LHSL, LHSH, RHSL, RHSH;
1311   GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1312   GetExpandedInteger(N->getOperand(1), RHSL, RHSH);
1313   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
1314   SDValue LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
1315   SDValue HiOps[3] = { LHSH, RHSH };
1316
1317   Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
1318   HiOps[2] = Lo.getValue(1);
1319   Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
1320
1321   // Legalized the flag result - switch anything that used the old flag to
1322   // use the new one.
1323   ReplaceValueWith(SDValue(N, 1), Hi.getValue(1));
1324 }
1325
1326 void DAGTypeLegalizer::ExpandIntRes_ANY_EXTEND(SDNode *N,
1327                                                SDValue &Lo, SDValue &Hi) {
1328   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1329   SDValue Op = N->getOperand(0);
1330   if (Op.getValueType().bitsLE(NVT)) {
1331     // The low part is any extension of the input (which degenerates to a copy).
1332     Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
1333     Hi = DAG.getNode(ISD::UNDEF, NVT);   // The high part is undefined.
1334   } else {
1335     // For example, extension of an i48 to an i64.  The operand type necessarily
1336     // promotes to the result type, so will end up being expanded too.
1337     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1338            "Only know how to promote this result!");
1339     SDValue Res = GetPromotedInteger(Op);
1340     assert(Res.getValueType() == N->getValueType(0) &&
1341            "Operand over promoted?");
1342     // Split the promoted operand.  This will simplify when it is expanded.
1343     SplitInteger(Res, Lo, Hi);
1344   }
1345 }
1346
1347 void DAGTypeLegalizer::ExpandIntRes_AssertSext(SDNode *N,
1348                                                SDValue &Lo, SDValue &Hi) {
1349   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1350   MVT NVT = Lo.getValueType();
1351   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1352   unsigned NVTBits = NVT.getSizeInBits();
1353   unsigned EVTBits = EVT.getSizeInBits();
1354
1355   if (NVTBits < EVTBits) {
1356     Hi = DAG.getNode(ISD::AssertSext, NVT, Hi,
1357                      DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1358   } else {
1359     Lo = DAG.getNode(ISD::AssertSext, NVT, Lo, DAG.getValueType(EVT));
1360     // The high part replicates the sign bit of Lo, make it explicit.
1361     Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1362                      DAG.getConstant(NVTBits-1, TLI.getShiftAmountTy()));
1363   }
1364 }
1365
1366 void DAGTypeLegalizer::ExpandIntRes_AssertZext(SDNode *N,
1367                                                SDValue &Lo, SDValue &Hi) {
1368   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1369   MVT NVT = Lo.getValueType();
1370   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1371   unsigned NVTBits = NVT.getSizeInBits();
1372   unsigned EVTBits = EVT.getSizeInBits();
1373
1374   if (NVTBits < EVTBits) {
1375     Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
1376                      DAG.getValueType(MVT::getIntegerVT(EVTBits - NVTBits)));
1377   } else {
1378     Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
1379     // The high part must be zero, make it explicit.
1380     Hi = DAG.getConstant(0, NVT);
1381   }
1382 }
1383
1384 void DAGTypeLegalizer::ExpandIntRes_BSWAP(SDNode *N,
1385                                           SDValue &Lo, SDValue &Hi) {
1386   GetExpandedInteger(N->getOperand(0), Hi, Lo);  // Note swapped operands.
1387   Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
1388   Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
1389 }
1390
1391 void DAGTypeLegalizer::ExpandIntRes_Constant(SDNode *N,
1392                                              SDValue &Lo, SDValue &Hi) {
1393   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1394   unsigned NBitWidth = NVT.getSizeInBits();
1395   const APInt &Cst = cast<ConstantSDNode>(N)->getAPIntValue();
1396   Lo = DAG.getConstant(APInt(Cst).trunc(NBitWidth), NVT);
1397   Hi = DAG.getConstant(Cst.lshr(NBitWidth).trunc(NBitWidth), NVT);
1398 }
1399
1400 void DAGTypeLegalizer::ExpandIntRes_CTLZ(SDNode *N,
1401                                          SDValue &Lo, SDValue &Hi) {
1402   // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
1403   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1404   MVT NVT = Lo.getValueType();
1405
1406   SDValue HiNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), Hi,
1407                                    DAG.getConstant(0, NVT), ISD::SETNE);
1408
1409   SDValue LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
1410   SDValue HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
1411
1412   Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
1413                    DAG.getNode(ISD::ADD, NVT, LoLZ,
1414                                DAG.getConstant(NVT.getSizeInBits(), NVT)));
1415   Hi = DAG.getConstant(0, NVT);
1416 }
1417
1418 void DAGTypeLegalizer::ExpandIntRes_CTPOP(SDNode *N,
1419                                           SDValue &Lo, SDValue &Hi) {
1420   // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
1421   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1422   MVT NVT = Lo.getValueType();
1423   Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
1424                    DAG.getNode(ISD::CTPOP, NVT, Hi));
1425   Hi = DAG.getConstant(0, NVT);
1426 }
1427
1428 void DAGTypeLegalizer::ExpandIntRes_CTTZ(SDNode *N,
1429                                          SDValue &Lo, SDValue &Hi) {
1430   // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
1431   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1432   MVT NVT = Lo.getValueType();
1433
1434   SDValue LoNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), Lo,
1435                                    DAG.getConstant(0, NVT), ISD::SETNE);
1436
1437   SDValue LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
1438   SDValue HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
1439
1440   Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
1441                    DAG.getNode(ISD::ADD, NVT, HiLZ,
1442                                DAG.getConstant(NVT.getSizeInBits(), NVT)));
1443   Hi = DAG.getConstant(0, NVT);
1444 }
1445
1446 void DAGTypeLegalizer::ExpandIntRes_FP_TO_SINT(SDNode *N, SDValue &Lo,
1447                                                SDValue &Hi) {
1448   MVT VT = N->getValueType(0);
1449   SDValue Op = N->getOperand(0);
1450   RTLIB::Libcall LC = RTLIB::getFPTOSINT(Op.getValueType(), VT);
1451   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-sint conversion!");
1452   SplitInteger(MakeLibCall(LC, VT, &Op, 1, true/*sign irrelevant*/), Lo, Hi);
1453 }
1454
1455 void DAGTypeLegalizer::ExpandIntRes_FP_TO_UINT(SDNode *N, SDValue &Lo,
1456                                                SDValue &Hi) {
1457   MVT VT = N->getValueType(0);
1458   SDValue Op = N->getOperand(0);
1459   RTLIB::Libcall LC = RTLIB::getFPTOUINT(Op.getValueType(), VT);
1460   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
1461   SplitInteger(MakeLibCall(LC, VT, &Op, 1, false/*sign irrelevant*/), Lo, Hi);
1462 }
1463
1464 void DAGTypeLegalizer::ExpandIntRes_LOAD(LoadSDNode *N,
1465                                          SDValue &Lo, SDValue &Hi) {
1466   if (ISD::isNormalLoad(N)) {
1467     ExpandRes_NormalLoad(N, Lo, Hi);
1468     return;
1469   }
1470
1471   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1472
1473   MVT VT = N->getValueType(0);
1474   MVT NVT = TLI.getTypeToTransformTo(VT);
1475   SDValue Ch  = N->getChain();
1476   SDValue Ptr = N->getBasePtr();
1477   ISD::LoadExtType ExtType = N->getExtensionType();
1478   int SVOffset = N->getSrcValueOffset();
1479   unsigned Alignment = N->getAlignment();
1480   bool isVolatile = N->isVolatile();
1481
1482   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1483
1484   if (N->getMemoryVT().bitsLE(NVT)) {
1485     MVT EVT = N->getMemoryVT();
1486
1487     Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
1488                         isVolatile, Alignment);
1489
1490     // Remember the chain.
1491     Ch = Lo.getValue(1);
1492
1493     if (ExtType == ISD::SEXTLOAD) {
1494       // The high part is obtained by SRA'ing all but one of the bits of the
1495       // lo part.
1496       unsigned LoSize = Lo.getValueType().getSizeInBits();
1497       Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1498                        DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1499     } else if (ExtType == ISD::ZEXTLOAD) {
1500       // The high part is just a zero.
1501       Hi = DAG.getConstant(0, NVT);
1502     } else {
1503       assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
1504       // The high part is undefined.
1505       Hi = DAG.getNode(ISD::UNDEF, NVT);
1506     }
1507   } else if (TLI.isLittleEndian()) {
1508     // Little-endian - low bits are at low addresses.
1509     Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1510                      isVolatile, Alignment);
1511
1512     unsigned ExcessBits =
1513       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
1514     MVT NEVT = MVT::getIntegerVT(ExcessBits);
1515
1516     // Increment the pointer to the other half.
1517     unsigned IncrementSize = NVT.getSizeInBits()/8;
1518     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1519                       DAG.getIntPtrConstant(IncrementSize));
1520     Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
1521                         SVOffset+IncrementSize, NEVT,
1522                         isVolatile, MinAlign(Alignment, IncrementSize));
1523
1524     // Build a factor node to remember that this load is independent of the
1525     // other one.
1526     Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1527                      Hi.getValue(1));
1528   } else {
1529     // Big-endian - high bits are at low addresses.  Favor aligned loads at
1530     // the cost of some bit-fiddling.
1531     MVT EVT = N->getMemoryVT();
1532     unsigned EBytes = EVT.getStoreSizeInBits()/8;
1533     unsigned IncrementSize = NVT.getSizeInBits()/8;
1534     unsigned ExcessBits = (EBytes - IncrementSize)*8;
1535
1536     // Load both the high bits and maybe some of the low bits.
1537     Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
1538                         MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits),
1539                         isVolatile, Alignment);
1540
1541     // Increment the pointer to the other half.
1542     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1543                       DAG.getIntPtrConstant(IncrementSize));
1544     // Load the rest of the low bits.
1545     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
1546                         SVOffset+IncrementSize,
1547                         MVT::getIntegerVT(ExcessBits),
1548                         isVolatile, MinAlign(Alignment, IncrementSize));
1549
1550     // Build a factor node to remember that this load is independent of the
1551     // other one.
1552     Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1553                      Hi.getValue(1));
1554
1555     if (ExcessBits < NVT.getSizeInBits()) {
1556       // Transfer low bits from the bottom of Hi to the top of Lo.
1557       Lo = DAG.getNode(ISD::OR, NVT, Lo,
1558                        DAG.getNode(ISD::SHL, NVT, Hi,
1559                                    DAG.getConstant(ExcessBits,
1560                                                    TLI.getShiftAmountTy())));
1561       // Move high bits to the right position in Hi.
1562       Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
1563                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
1564                                        TLI.getShiftAmountTy()));
1565     }
1566   }
1567
1568   // Legalized the chain result - switch anything that used the old chain to
1569   // use the new one.
1570   ReplaceValueWith(SDValue(N, 1), Ch);
1571 }
1572
1573 void DAGTypeLegalizer::ExpandIntRes_Logical(SDNode *N,
1574                                             SDValue &Lo, SDValue &Hi) {
1575   SDValue LL, LH, RL, RH;
1576   GetExpandedInteger(N->getOperand(0), LL, LH);
1577   GetExpandedInteger(N->getOperand(1), RL, RH);
1578   Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
1579   Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
1580 }
1581
1582 void DAGTypeLegalizer::ExpandIntRes_MUL(SDNode *N,
1583                                         SDValue &Lo, SDValue &Hi) {
1584   MVT VT = N->getValueType(0);
1585   MVT NVT = TLI.getTypeToTransformTo(VT);
1586
1587   bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
1588   bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
1589   bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
1590   bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
1591   if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
1592     SDValue LL, LH, RL, RH;
1593     GetExpandedInteger(N->getOperand(0), LL, LH);
1594     GetExpandedInteger(N->getOperand(1), RL, RH);
1595     unsigned OuterBitSize = VT.getSizeInBits();
1596     unsigned InnerBitSize = NVT.getSizeInBits();
1597     unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
1598     unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
1599
1600     APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
1601     if (DAG.MaskedValueIsZero(N->getOperand(0), HighMask) &&
1602         DAG.MaskedValueIsZero(N->getOperand(1), HighMask)) {
1603       // The inputs are both zero-extended.
1604       if (HasUMUL_LOHI) {
1605         // We can emit a umul_lohi.
1606         Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1607         Hi = SDValue(Lo.getNode(), 1);
1608         return;
1609       }
1610       if (HasMULHU) {
1611         // We can emit a mulhu+mul.
1612         Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1613         Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1614         return;
1615       }
1616     }
1617     if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
1618       // The input values are both sign-extended.
1619       if (HasSMUL_LOHI) {
1620         // We can emit a smul_lohi.
1621         Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
1622         Hi = SDValue(Lo.getNode(), 1);
1623         return;
1624       }
1625       if (HasMULHS) {
1626         // We can emit a mulhs+mul.
1627         Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1628         Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
1629         return;
1630       }
1631     }
1632     if (HasUMUL_LOHI) {
1633       // Lo,Hi = umul LHS, RHS.
1634       SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
1635                                        DAG.getVTList(NVT, NVT), LL, RL);
1636       Lo = UMulLOHI;
1637       Hi = UMulLOHI.getValue(1);
1638       RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1639       LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1640       Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1641       Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1642       return;
1643     }
1644     if (HasMULHU) {
1645       Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
1646       Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
1647       RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
1648       LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
1649       Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
1650       Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
1651       return;
1652     }
1653   }
1654
1655   // If nothing else, we can make a libcall.
1656   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1657   if (VT == MVT::i32)
1658     LC = RTLIB::MUL_I32;
1659   else if (VT == MVT::i64)
1660     LC = RTLIB::MUL_I64;
1661   else if (VT == MVT::i128)
1662     LC = RTLIB::MUL_I128;
1663   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported MUL!");
1664
1665   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1666   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true/*sign irrelevant*/), Lo, Hi);
1667 }
1668
1669 void DAGTypeLegalizer::ExpandIntRes_SDIV(SDNode *N,
1670                                          SDValue &Lo, SDValue &Hi) {
1671   MVT VT = N->getValueType(0);
1672
1673   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1674   if (VT == MVT::i32)
1675     LC = RTLIB::SDIV_I32;
1676   else if (VT == MVT::i64)
1677     LC = RTLIB::SDIV_I64;
1678   else if (VT == MVT::i128)
1679     LC = RTLIB::SDIV_I128;
1680   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SDIV!");
1681
1682   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1683   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1684 }
1685
1686 void DAGTypeLegalizer::ExpandIntRes_Shift(SDNode *N,
1687                                           SDValue &Lo, SDValue &Hi) {
1688   MVT VT = N->getValueType(0);
1689
1690   // If we can emit an efficient shift operation, do so now.  Check to see if
1691   // the RHS is a constant.
1692   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1693     return ExpandShiftByConstant(N, CN->getZExtValue(), Lo, Hi);
1694
1695   // If we can determine that the high bit of the shift is zero or one, even if
1696   // the low bits are variable, emit this shift in an optimized form.
1697   if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
1698     return;
1699
1700   // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
1701   unsigned PartsOpc;
1702   if (N->getOpcode() == ISD::SHL) {
1703     PartsOpc = ISD::SHL_PARTS;
1704   } else if (N->getOpcode() == ISD::SRL) {
1705     PartsOpc = ISD::SRL_PARTS;
1706   } else {
1707     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1708     PartsOpc = ISD::SRA_PARTS;
1709   }
1710
1711   // Next check to see if the target supports this SHL_PARTS operation or if it
1712   // will custom expand it.
1713   MVT NVT = TLI.getTypeToTransformTo(VT);
1714   TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
1715   if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
1716       Action == TargetLowering::Custom) {
1717     // Expand the subcomponents.
1718     SDValue LHSL, LHSH;
1719     GetExpandedInteger(N->getOperand(0), LHSL, LHSH);
1720
1721     SDValue Ops[] = { LHSL, LHSH, N->getOperand(1) };
1722     MVT VT = LHSL.getValueType();
1723     Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
1724     Hi = Lo.getValue(1);
1725     return;
1726   }
1727
1728   // Otherwise, emit a libcall.
1729   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1730   bool isSigned;
1731   if (N->getOpcode() == ISD::SHL) {
1732     isSigned = false; /*sign irrelevant*/
1733     if (VT == MVT::i32)
1734       LC = RTLIB::SHL_I32;
1735     else if (VT == MVT::i64)
1736       LC = RTLIB::SHL_I64;
1737     else if (VT == MVT::i128)
1738       LC = RTLIB::SHL_I128;
1739   } else if (N->getOpcode() == ISD::SRL) {
1740     isSigned = false;
1741     if (VT == MVT::i32)
1742       LC = RTLIB::SRL_I32;
1743     else if (VT == MVT::i64)
1744       LC = RTLIB::SRL_I64;
1745     else if (VT == MVT::i128)
1746       LC = RTLIB::SRL_I128;
1747   } else {
1748     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
1749     isSigned = true;
1750     if (VT == MVT::i32)
1751       LC = RTLIB::SRA_I32;
1752     else if (VT == MVT::i64)
1753       LC = RTLIB::SRA_I64;
1754     else if (VT == MVT::i128)
1755       LC = RTLIB::SRA_I128;
1756   }
1757   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported shift!");
1758
1759   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1760   SplitInteger(MakeLibCall(LC, VT, Ops, 2, isSigned), Lo, Hi);
1761 }
1762
1763 void DAGTypeLegalizer::ExpandIntRes_SIGN_EXTEND(SDNode *N,
1764                                                 SDValue &Lo, SDValue &Hi) {
1765   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1766   SDValue Op = N->getOperand(0);
1767   if (Op.getValueType().bitsLE(NVT)) {
1768     // The low part is sign extension of the input (degenerates to a copy).
1769     Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
1770     // The high part is obtained by SRA'ing all but one of the bits of low part.
1771     unsigned LoSize = NVT.getSizeInBits();
1772     Hi = DAG.getNode(ISD::SRA, NVT, Lo,
1773                      DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
1774   } else {
1775     // For example, extension of an i48 to an i64.  The operand type necessarily
1776     // promotes to the result type, so will end up being expanded too.
1777     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1778            "Only know how to promote this result!");
1779     SDValue Res = GetPromotedInteger(Op);
1780     assert(Res.getValueType() == N->getValueType(0) &&
1781            "Operand over promoted?");
1782     // Split the promoted operand.  This will simplify when it is expanded.
1783     SplitInteger(Res, Lo, Hi);
1784     unsigned ExcessBits =
1785       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1786     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1787                      DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1788   }
1789 }
1790
1791 void DAGTypeLegalizer::
1792 ExpandIntRes_SIGN_EXTEND_INREG(SDNode *N, SDValue &Lo, SDValue &Hi) {
1793   GetExpandedInteger(N->getOperand(0), Lo, Hi);
1794   MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
1795
1796   if (EVT.bitsLE(Lo.getValueType())) {
1797     // sext_inreg the low part if needed.
1798     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
1799                      N->getOperand(1));
1800
1801     // The high part gets the sign extension from the lo-part.  This handles
1802     // things like sextinreg V:i64 from i8.
1803     Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
1804                      DAG.getConstant(Hi.getValueType().getSizeInBits()-1,
1805                                      TLI.getShiftAmountTy()));
1806   } else {
1807     // For example, extension of an i48 to an i64.  Leave the low part alone,
1808     // sext_inreg the high part.
1809     unsigned ExcessBits =
1810       EVT.getSizeInBits() - Lo.getValueType().getSizeInBits();
1811     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
1812                      DAG.getValueType(MVT::getIntegerVT(ExcessBits)));
1813   }
1814 }
1815
1816 void DAGTypeLegalizer::ExpandIntRes_SREM(SDNode *N,
1817                                          SDValue &Lo, SDValue &Hi) {
1818   MVT VT = N->getValueType(0);
1819
1820   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1821   if (VT == MVT::i32)
1822     LC = RTLIB::SREM_I32;
1823   else if (VT == MVT::i64)
1824     LC = RTLIB::SREM_I64;
1825   else if (VT == MVT::i128)
1826     LC = RTLIB::SREM_I128;
1827   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported SREM!");
1828
1829   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1830   SplitInteger(MakeLibCall(LC, VT, Ops, 2, true), Lo, Hi);
1831 }
1832
1833 void DAGTypeLegalizer::ExpandIntRes_TRUNCATE(SDNode *N,
1834                                              SDValue &Lo, SDValue &Hi) {
1835   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1836   Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
1837   Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
1838                    DAG.getConstant(NVT.getSizeInBits(),
1839                                    TLI.getShiftAmountTy()));
1840   Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
1841 }
1842
1843 void DAGTypeLegalizer::ExpandIntRes_UDIV(SDNode *N,
1844                                          SDValue &Lo, SDValue &Hi) {
1845   MVT VT = N->getValueType(0);
1846
1847   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1848   if (VT == MVT::i32)
1849     LC = RTLIB::UDIV_I32;
1850   else if (VT == MVT::i64)
1851     LC = RTLIB::UDIV_I64;
1852   else if (VT == MVT::i128)
1853     LC = RTLIB::UDIV_I128;
1854   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UDIV!");
1855
1856   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1857   SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1858 }
1859
1860 void DAGTypeLegalizer::ExpandIntRes_UREM(SDNode *N,
1861                                          SDValue &Lo, SDValue &Hi) {
1862   MVT VT = N->getValueType(0);
1863
1864   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1865   if (VT == MVT::i32)
1866     LC = RTLIB::UREM_I32;
1867   else if (VT == MVT::i64)
1868     LC = RTLIB::UREM_I64;
1869   else if (VT == MVT::i128)
1870     LC = RTLIB::UREM_I128;
1871   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported UREM!");
1872
1873   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1874   SplitInteger(MakeLibCall(LC, VT, Ops, 2, false), Lo, Hi);
1875 }
1876
1877 void DAGTypeLegalizer::ExpandIntRes_ZERO_EXTEND(SDNode *N,
1878                                                 SDValue &Lo, SDValue &Hi) {
1879   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
1880   SDValue Op = N->getOperand(0);
1881   if (Op.getValueType().bitsLE(NVT)) {
1882     // The low part is zero extension of the input (degenerates to a copy).
1883     Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
1884     Hi = DAG.getConstant(0, NVT);   // The high part is just a zero.
1885   } else {
1886     // For example, extension of an i48 to an i64.  The operand type necessarily
1887     // promotes to the result type, so will end up being expanded too.
1888     assert(getTypeAction(Op.getValueType()) == PromoteInteger &&
1889            "Only know how to promote this result!");
1890     SDValue Res = GetPromotedInteger(Op);
1891     assert(Res.getValueType() == N->getValueType(0) &&
1892            "Operand over promoted?");
1893     // Split the promoted operand.  This will simplify when it is expanded.
1894     SplitInteger(Res, Lo, Hi);
1895     unsigned ExcessBits =
1896       Op.getValueType().getSizeInBits() - NVT.getSizeInBits();
1897     Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerVT(ExcessBits));
1898   }
1899 }
1900
1901
1902 //===----------------------------------------------------------------------===//
1903 //  Integer Operand Expansion
1904 //===----------------------------------------------------------------------===//
1905
1906 /// ExpandIntegerOperand - This method is called when the specified operand of
1907 /// the specified node is found to need expansion.  At this point, all of the
1908 /// result types of the node are known to be legal, but other operands of the
1909 /// node may need promotion or expansion as well as the specified one.
1910 bool DAGTypeLegalizer::ExpandIntegerOperand(SDNode *N, unsigned OpNo) {
1911   DEBUG(cerr << "Expand integer operand: "; N->dump(&DAG); cerr << "\n");
1912   SDValue Res = SDValue();
1913
1914   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1915       == TargetLowering::Custom)
1916     Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1917
1918   if (Res.getNode() == 0) {
1919     switch (N->getOpcode()) {
1920     default:
1921   #ifndef NDEBUG
1922       cerr << "ExpandIntegerOperand Op #" << OpNo << ": ";
1923       N->dump(&DAG); cerr << "\n";
1924   #endif
1925       assert(0 && "Do not know how to expand this operator's operand!");
1926       abort();
1927
1928     case ISD::BUILD_VECTOR:      Res = ExpandOp_BUILD_VECTOR(N); break;
1929     case ISD::BIT_CONVERT:       Res = ExpandOp_BIT_CONVERT(N); break;
1930     case ISD::EXTRACT_ELEMENT:   Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1931     case ISD::INSERT_VECTOR_ELT: Res = ExpandOp_INSERT_VECTOR_ELT(N); break;
1932     case ISD::SCALAR_TO_VECTOR:  Res = ExpandOp_SCALAR_TO_VECTOR(N); break;
1933
1934     case ISD::BR_CC:      Res = ExpandIntOp_BR_CC(N); break;
1935     case ISD::SELECT_CC:  Res = ExpandIntOp_SELECT_CC(N); break;
1936     case ISD::SETCC:      Res = ExpandIntOp_SETCC(N); break;
1937     case ISD::SINT_TO_FP: Res = ExpandIntOp_SINT_TO_FP(N); break;
1938     case ISD::STORE:      Res = ExpandIntOp_STORE(cast<StoreSDNode>(N), OpNo);
1939                           break;
1940     case ISD::TRUNCATE:   Res = ExpandIntOp_TRUNCATE(N); break;
1941     case ISD::UINT_TO_FP: Res = ExpandIntOp_UINT_TO_FP(N); break;
1942     }
1943   }
1944
1945   // If the result is null, the sub-method took care of registering results etc.
1946   if (!Res.getNode()) return false;
1947
1948   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1949   // core about this.
1950   if (Res.getNode() == N)
1951     return true;
1952
1953   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1954          "Invalid operand expansion");
1955
1956   ReplaceValueWith(SDValue(N, 0), Res);
1957   return false;
1958 }
1959
1960 /// IntegerExpandSetCCOperands - Expand the operands of a comparison.  This code
1961 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1962 void DAGTypeLegalizer::IntegerExpandSetCCOperands(SDValue &NewLHS,
1963                                                   SDValue &NewRHS,
1964                                                   ISD::CondCode &CCCode) {
1965   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1966   GetExpandedInteger(NewLHS, LHSLo, LHSHi);
1967   GetExpandedInteger(NewRHS, RHSLo, RHSHi);
1968
1969   MVT VT = NewLHS.getValueType();
1970
1971   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1972     if (RHSLo == RHSHi) {
1973       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo)) {
1974         if (RHSCST->isAllOnesValue()) {
1975           // Equality comparison to -1.
1976           NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1977           NewRHS = RHSLo;
1978           return;
1979         }
1980       }
1981     }
1982
1983     NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1984     NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1985     NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1986     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1987     return;
1988   }
1989
1990   // If this is a comparison of the sign bit, just look at the top part.
1991   // X > -1,  x < 0
1992   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1993     if ((CCCode == ISD::SETLT && CST->isNullValue()) ||     // X < 0
1994         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
1995       NewLHS = LHSHi;
1996       NewRHS = RHSHi;
1997       return;
1998     }
1999
2000   // FIXME: This generated code sucks.
2001   ISD::CondCode LowCC;
2002   switch (CCCode) {
2003   default: assert(0 && "Unknown integer setcc!");
2004   case ISD::SETLT:
2005   case ISD::SETULT: LowCC = ISD::SETULT; break;
2006   case ISD::SETGT:
2007   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
2008   case ISD::SETLE:
2009   case ISD::SETULE: LowCC = ISD::SETULE; break;
2010   case ISD::SETGE:
2011   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
2012   }
2013
2014   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
2015   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
2016   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
2017
2018   // NOTE: on targets without efficient SELECT of bools, we can always use
2019   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
2020   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
2021   SDValue Tmp1, Tmp2;
2022   Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
2023                            LHSLo, RHSLo, LowCC, false, DagCombineInfo);
2024   if (!Tmp1.getNode())
2025     Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
2026                         LHSLo, RHSLo, LowCC);
2027   Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2028                            LHSHi, RHSHi, CCCode, false, DagCombineInfo);
2029   if (!Tmp2.getNode())
2030     Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi.getValueType()),
2031                        LHSHi, RHSHi, DAG.getCondCode(CCCode));
2032
2033   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
2034   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
2035   if ((Tmp1C && Tmp1C->isNullValue()) ||
2036       (Tmp2C && Tmp2C->isNullValue() &&
2037        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
2038         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
2039       (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
2040        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
2041         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
2042     // low part is known false, returns high part.
2043     // For LE / GE, if high part is known false, ignore the low part.
2044     // For LT / GT, if high part is known true, ignore the low part.
2045     NewLHS = Tmp2;
2046     NewRHS = SDValue();
2047     return;
2048   }
2049
2050   NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2051                              LHSHi, RHSHi, ISD::SETEQ, false, DagCombineInfo);
2052   if (!NewLHS.getNode())
2053     NewLHS = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
2054                           LHSHi, RHSHi, ISD::SETEQ);
2055   NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
2056                        NewLHS, Tmp1, Tmp2);
2057   NewRHS = SDValue();
2058 }
2059
2060 SDValue DAGTypeLegalizer::ExpandIntOp_BR_CC(SDNode *N) {
2061   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
2062   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
2063   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2064
2065   // If ExpandSetCCOperands returned a scalar, we need to compare the result
2066   // against zero to select between true and false values.
2067   if (NewRHS.getNode() == 0) {
2068     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2069     CCCode = ISD::SETNE;
2070   }
2071
2072   // Update N to have the operands specified.
2073   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
2074                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
2075                                 N->getOperand(4));
2076 }
2077
2078 SDValue DAGTypeLegalizer::ExpandIntOp_SELECT_CC(SDNode *N) {
2079   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2080   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
2081   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2082
2083   // If ExpandSetCCOperands returned a scalar, we need to compare the result
2084   // against zero to select between true and false values.
2085   if (NewRHS.getNode() == 0) {
2086     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
2087     CCCode = ISD::SETNE;
2088   }
2089
2090   // Update N to have the operands specified.
2091   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2092                                 N->getOperand(2), N->getOperand(3),
2093                                 DAG.getCondCode(CCCode));
2094 }
2095
2096 SDValue DAGTypeLegalizer::ExpandIntOp_SETCC(SDNode *N) {
2097   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
2098   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
2099   IntegerExpandSetCCOperands(NewLHS, NewRHS, CCCode);
2100
2101   // If ExpandSetCCOperands returned a scalar, use it.
2102   if (NewRHS.getNode() == 0) {
2103     assert(NewLHS.getValueType() == N->getValueType(0) &&
2104            "Unexpected setcc expansion!");
2105     return NewLHS;
2106   }
2107
2108   // Otherwise, update N to have the operands specified.
2109   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
2110                                 DAG.getCondCode(CCCode));
2111 }
2112
2113 SDValue DAGTypeLegalizer::ExpandIntOp_SINT_TO_FP(SDNode *N) {
2114   SDValue Op = N->getOperand(0);
2115   MVT DstVT = N->getValueType(0);
2116   RTLIB::Libcall LC = RTLIB::getSINTTOFP(Op.getValueType(), DstVT);
2117   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2118          "Don't know how to expand this SINT_TO_FP!");
2119   return MakeLibCall(LC, DstVT, &Op, 1, true);
2120 }
2121
2122 SDValue DAGTypeLegalizer::ExpandIntOp_STORE(StoreSDNode *N, unsigned OpNo) {
2123   if (ISD::isNormalStore(N))
2124     return ExpandOp_NormalStore(N, OpNo);
2125
2126   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
2127   assert(OpNo == 1 && "Can only expand the stored value so far");
2128
2129   MVT VT = N->getOperand(1).getValueType();
2130   MVT NVT = TLI.getTypeToTransformTo(VT);
2131   SDValue Ch  = N->getChain();
2132   SDValue Ptr = N->getBasePtr();
2133   int SVOffset = N->getSrcValueOffset();
2134   unsigned Alignment = N->getAlignment();
2135   bool isVolatile = N->isVolatile();
2136   SDValue Lo, Hi;
2137
2138   assert(NVT.isByteSized() && "Expanded type not byte sized!");
2139
2140   if (N->getMemoryVT().bitsLE(NVT)) {
2141     GetExpandedInteger(N->getValue(), Lo, Hi);
2142     return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2143                              N->getMemoryVT(), isVolatile, Alignment);
2144   } else if (TLI.isLittleEndian()) {
2145     // Little-endian - low bits are at low addresses.
2146     GetExpandedInteger(N->getValue(), Lo, Hi);
2147
2148     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
2149                       isVolatile, Alignment);
2150
2151     unsigned ExcessBits =
2152       N->getMemoryVT().getSizeInBits() - NVT.getSizeInBits();
2153     MVT NEVT = MVT::getIntegerVT(ExcessBits);
2154
2155     // Increment the pointer to the other half.
2156     unsigned IncrementSize = NVT.getSizeInBits()/8;
2157     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2158                       DAG.getIntPtrConstant(IncrementSize));
2159     Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2160                            SVOffset+IncrementSize, NEVT,
2161                            isVolatile, MinAlign(Alignment, IncrementSize));
2162     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2163   } else {
2164     // Big-endian - high bits are at low addresses.  Favor aligned stores at
2165     // the cost of some bit-fiddling.
2166     GetExpandedInteger(N->getValue(), Lo, Hi);
2167
2168     MVT EVT = N->getMemoryVT();
2169     unsigned EBytes = EVT.getStoreSizeInBits()/8;
2170     unsigned IncrementSize = NVT.getSizeInBits()/8;
2171     unsigned ExcessBits = (EBytes - IncrementSize)*8;
2172     MVT HiVT = MVT::getIntegerVT(EVT.getSizeInBits() - ExcessBits);
2173
2174     if (ExcessBits < NVT.getSizeInBits()) {
2175       // Transfer high bits from the top of Lo to the bottom of Hi.
2176       Hi = DAG.getNode(ISD::SHL, NVT, Hi,
2177                        DAG.getConstant(NVT.getSizeInBits() - ExcessBits,
2178                                        TLI.getShiftAmountTy()));
2179       Hi = DAG.getNode(ISD::OR, NVT, Hi,
2180                        DAG.getNode(ISD::SRL, NVT, Lo,
2181                                    DAG.getConstant(ExcessBits,
2182                                                    TLI.getShiftAmountTy())));
2183     }
2184
2185     // Store both the high bits and maybe some of the low bits.
2186     Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
2187                            SVOffset, HiVT, isVolatile, Alignment);
2188
2189     // Increment the pointer to the other half.
2190     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2191                       DAG.getIntPtrConstant(IncrementSize));
2192     // Store the lowest ExcessBits bits in the second half.
2193     Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
2194                            SVOffset+IncrementSize,
2195                            MVT::getIntegerVT(ExcessBits),
2196                            isVolatile, MinAlign(Alignment, IncrementSize));
2197     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2198   }
2199 }
2200
2201 SDValue DAGTypeLegalizer::ExpandIntOp_TRUNCATE(SDNode *N) {
2202   SDValue InL, InH;
2203   GetExpandedInteger(N->getOperand(0), InL, InH);
2204   // Just truncate the low part of the source.
2205   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
2206 }
2207
2208 SDValue DAGTypeLegalizer::ExpandIntOp_UINT_TO_FP(SDNode *N) {
2209   SDValue Op = N->getOperand(0);
2210   MVT SrcVT = Op.getValueType();
2211   MVT DstVT = N->getValueType(0);
2212
2213   if (TLI.getOperationAction(ISD::SINT_TO_FP, SrcVT) == TargetLowering::Custom){
2214     // Do a signed conversion then adjust the result.
2215     SDValue SignedConv = DAG.getNode(ISD::SINT_TO_FP, DstVT, Op);
2216     SignedConv = TLI.LowerOperation(SignedConv, DAG);
2217
2218     // The result of the signed conversion needs adjusting if the 'sign bit' of
2219     // the incoming integer was set.  To handle this, we dynamically test to see
2220     // if it is set, and, if so, add a fudge factor.
2221
2222     const uint64_t F32TwoE32  = 0x4F800000ULL;
2223     const uint64_t F32TwoE64  = 0x5F800000ULL;
2224     const uint64_t F32TwoE128 = 0x7F800000ULL;
2225
2226     APInt FF(32, 0);
2227     if (SrcVT == MVT::i32)
2228       FF = APInt(32, F32TwoE32);
2229     else if (SrcVT == MVT::i64)
2230       FF = APInt(32, F32TwoE64);
2231     else if (SrcVT == MVT::i128)
2232       FF = APInt(32, F32TwoE128);
2233     else
2234       assert(false && "Unsupported UINT_TO_FP!");
2235
2236     // Check whether the sign bit is set.
2237     SDValue Lo, Hi;
2238     GetExpandedInteger(Op, Lo, Hi);
2239     SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
2240                                    Hi, DAG.getConstant(0, Hi.getValueType()),
2241                                    ISD::SETLT);
2242
2243     // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
2244     SDValue FudgePtr = DAG.getConstantPool(ConstantInt::get(FF.zext(64)),
2245                                            TLI.getPointerTy());
2246
2247     // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
2248     SDValue Zero = DAG.getIntPtrConstant(0);
2249     SDValue Four = DAG.getIntPtrConstant(4);
2250     if (TLI.isBigEndian()) std::swap(Zero, Four);
2251     SDValue Offset = DAG.getNode(ISD::SELECT, Zero.getValueType(), SignSet,
2252                                  Zero, Four);
2253     unsigned Alignment =
2254       1 << cast<ConstantPoolSDNode>(FudgePtr)->getAlignment();
2255     FudgePtr = DAG.getNode(ISD::ADD, TLI.getPointerTy(), FudgePtr, Offset);
2256     Alignment = std::min(Alignment, 4u);
2257
2258     // Load the value out, extending it from f32 to the destination float type.
2259     // FIXME: Avoid the extend by constructing the right constant pool?
2260     SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, DstVT, DAG.getEntryNode(),
2261                                    FudgePtr, NULL, 0, MVT::f32,
2262                                    false, Alignment);
2263     return DAG.getNode(ISD::FADD, DstVT, SignedConv, Fudge);
2264   }
2265
2266   // Otherwise, use a libcall.
2267   RTLIB::Libcall LC = RTLIB::getUINTTOFP(SrcVT, DstVT);
2268   assert(LC != RTLIB::UNKNOWN_LIBCALL &&
2269          "Don't know how to expand this UINT_TO_FP!");
2270   return MakeLibCall(LC, DstVT, &Op, 1, true);
2271 }