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