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