More constification of things. More comments added. No functionality
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeTypesExpand.cpp
1 //===-- LegalizeTypesExpand.cpp - Expansion for LegalizeTypes -------------===//
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 expansion support for LegalizeTypes.  Expansion is the
11 // act of changing a computation in an invalid type to be a computation in
12 // multiple registers of a smaller type.  For example, implementing i64
13 // arithmetic in two i32 registers (as is often needed on 32-bit targets, for
14 // example).
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "LegalizeTypes.h"
19 #include "llvm/Constants.h"
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //  Result Expansion
24 //===----------------------------------------------------------------------===//
25
26 /// ExpandResult - This method is called when the specified result of the
27 /// specified node is found to need expansion.  At this point, the node may also
28 /// have invalid operands or may have other results that need promotion, we just
29 /// know that (at least) one result needs expansion.
30 void DAGTypeLegalizer::ExpandResult(SDNode *N, unsigned ResNo) {
31   DEBUG(cerr << "Expand node result: "; N->dump(&DAG); cerr << "\n");
32   SDOperand Lo, Hi;
33   Lo = Hi = SDOperand();
34
35   // See if the target wants to custom expand this node.
36   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(0)) == 
37           TargetLowering::Custom) {
38     // If the target wants to, allow it to lower this itself.
39     if (SDNode *P = TLI.ExpandOperationResult(N, DAG)) {
40       // Everything that once used N now uses P.  We are guaranteed that the
41       // result value types of N and the result value types of P match.
42       ReplaceNodeWith(N, P);
43       return;
44     }
45   }
46
47   switch (N->getOpcode()) {
48   default:
49 #ifndef NDEBUG
50     cerr << "ExpandResult #" << ResNo << ": ";
51     N->dump(&DAG); cerr << "\n";
52 #endif
53     assert(0 && "Do not know how to expand the result of this operator!");
54     abort();
55       
56   case ISD::UNDEF:       ExpandResult_UNDEF(N, Lo, Hi); break;
57   case ISD::Constant:    ExpandResult_Constant(N, Lo, Hi); break;
58   case ISD::BUILD_PAIR:  ExpandResult_BUILD_PAIR(N, Lo, Hi); break;
59   case ISD::MERGE_VALUES: ExpandResult_MERGE_VALUES(N, Lo, Hi); break;
60   case ISD::ANY_EXTEND:  ExpandResult_ANY_EXTEND(N, Lo, Hi); break;
61   case ISD::ZERO_EXTEND: ExpandResult_ZERO_EXTEND(N, Lo, Hi); break;
62   case ISD::SIGN_EXTEND: ExpandResult_SIGN_EXTEND(N, Lo, Hi); break;
63   case ISD::AssertZext:  ExpandResult_AssertZext(N, Lo, Hi); break;
64   case ISD::TRUNCATE:    ExpandResult_TRUNCATE(N, Lo, Hi); break;
65   case ISD::BIT_CONVERT: ExpandResult_BIT_CONVERT(N, Lo, Hi); break;
66   case ISD::SIGN_EXTEND_INREG: ExpandResult_SIGN_EXTEND_INREG(N, Lo, Hi); break;
67   case ISD::LOAD:        ExpandResult_LOAD(cast<LoadSDNode>(N), Lo, Hi); break;
68     
69   case ISD::AND:
70   case ISD::OR:
71   case ISD::XOR:         ExpandResult_Logical(N, Lo, Hi); break;
72   case ISD::BSWAP:       ExpandResult_BSWAP(N, Lo, Hi); break;
73   case ISD::ADD:
74   case ISD::SUB:         ExpandResult_ADDSUB(N, Lo, Hi); break;
75   case ISD::ADDC:
76   case ISD::SUBC:        ExpandResult_ADDSUBC(N, Lo, Hi); break;
77   case ISD::ADDE:
78   case ISD::SUBE:        ExpandResult_ADDSUBE(N, Lo, Hi); break;
79   case ISD::SELECT:      ExpandResult_SELECT(N, Lo, Hi); break;
80   case ISD::SELECT_CC:   ExpandResult_SELECT_CC(N, Lo, Hi); break;
81   case ISD::MUL:         ExpandResult_MUL(N, Lo, Hi); break;
82   case ISD::SHL:
83   case ISD::SRA:
84   case ISD::SRL:         ExpandResult_Shift(N, Lo, Hi); break;
85
86   case ISD::CTLZ:        ExpandResult_CTLZ(N, Lo, Hi); break;
87   case ISD::CTPOP:       ExpandResult_CTPOP(N, Lo, Hi); break;
88   case ISD::CTTZ:        ExpandResult_CTTZ(N, Lo, Hi); break;
89   }
90
91   // If Lo/Hi is null, the sub-method took care of registering results etc.
92   if (Lo.Val)
93     SetExpandedOp(SDOperand(N, ResNo), Lo, Hi);
94 }
95
96 void DAGTypeLegalizer::ExpandResult_UNDEF(SDNode *N,
97                                           SDOperand &Lo, SDOperand &Hi) {
98   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
99   Lo = Hi = DAG.getNode(ISD::UNDEF, NVT);
100 }
101
102 void DAGTypeLegalizer::ExpandResult_Constant(SDNode *N,
103                                              SDOperand &Lo, SDOperand &Hi) {
104   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
105   uint64_t Cst = cast<ConstantSDNode>(N)->getValue();
106   Lo = DAG.getConstant(Cst, NVT);
107   Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
108 }
109
110 void DAGTypeLegalizer::ExpandResult_BUILD_PAIR(SDNode *N,
111                                                SDOperand &Lo, SDOperand &Hi) {
112   // Return the operands.
113   Lo = N->getOperand(0);
114   Hi = N->getOperand(1);
115 }
116
117 void DAGTypeLegalizer::ExpandResult_MERGE_VALUES(SDNode *N,
118                                                  SDOperand &Lo, SDOperand &Hi) {
119   // A MERGE_VALUES node can produce any number of values.  We know that the
120   // first illegal one needs to be expanded into Lo/Hi.
121   unsigned i;
122   
123   // The string of legal results gets turns into the input operands, which have
124   // the same type.
125   for (i = 0; isTypeLegal(N->getValueType(i)); ++i)
126     ReplaceValueWith(SDOperand(N, i), SDOperand(N->getOperand(i)));
127
128   // The first illegal result must be the one that needs to be expanded.
129   GetExpandedOp(N->getOperand(i), Lo, Hi);
130
131   // Legalize the rest of the results into the input operands whether they are
132   // legal or not.
133   unsigned e = N->getNumValues();
134   for (++i; i != e; ++i)
135     ReplaceValueWith(SDOperand(N, i), SDOperand(N->getOperand(i)));
136 }
137
138 void DAGTypeLegalizer::ExpandResult_ANY_EXTEND(SDNode *N,
139                                                SDOperand &Lo, SDOperand &Hi) {
140   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
141   SDOperand Op = N->getOperand(0);
142   if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
143     // The low part is any extension of the input (which degenerates to a copy).
144     Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Op);
145     Hi = DAG.getNode(ISD::UNDEF, NVT);   // The high part is undefined.
146   } else {
147     // For example, extension of an i48 to an i64.  The operand type necessarily
148     // promotes to the result type, so will end up being expanded too.
149     assert(getTypeAction(Op.getValueType()) == Promote &&
150            "Don't know how to expand this result!");
151     SDOperand Res = GetPromotedOp(Op);
152     assert(Res.getValueType() == N->getValueType(0) &&
153            "Operand over promoted?");
154     // Split the promoted operand.  This will simplify when it is expanded.
155     SplitOp(Res, Lo, Hi);
156   }
157 }
158
159 void DAGTypeLegalizer::ExpandResult_ZERO_EXTEND(SDNode *N,
160                                                 SDOperand &Lo, SDOperand &Hi) {
161   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
162   SDOperand Op = N->getOperand(0);
163   if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
164     // The low part is zero extension of the input (which degenerates to a copy).
165     Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, N->getOperand(0));
166     Hi = DAG.getConstant(0, NVT);   // The high part is just a zero.
167   } else {
168     // For example, extension of an i48 to an i64.  The operand type necessarily
169     // promotes to the result type, so will end up being expanded too.
170     assert(getTypeAction(Op.getValueType()) == Promote &&
171            "Don't know how to expand this result!");
172     SDOperand Res = GetPromotedOp(Op);
173     assert(Res.getValueType() == N->getValueType(0) &&
174            "Operand over promoted?");
175     // Split the promoted operand.  This will simplify when it is expanded.
176     SplitOp(Res, Lo, Hi);
177     unsigned ExcessBits =
178       MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
179     Hi = DAG.getZeroExtendInReg(Hi, MVT::getIntegerType(ExcessBits));
180   }
181 }
182
183 void DAGTypeLegalizer::ExpandResult_SIGN_EXTEND(SDNode *N,
184                                                 SDOperand &Lo, SDOperand &Hi) {
185   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
186   SDOperand Op = N->getOperand(0);
187   if (MVT::getSizeInBits(Op.getValueType()) <= MVT::getSizeInBits(NVT)) {
188     // The low part is sign extension of the input (which degenerates to a copy).
189     Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, N->getOperand(0));
190     // The high part is obtained by SRA'ing all but one of the bits of low part.
191     unsigned LoSize = MVT::getSizeInBits(NVT);
192     Hi = DAG.getNode(ISD::SRA, NVT, Lo,
193                      DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
194   } else {
195     // For example, extension of an i48 to an i64.  The operand type necessarily
196     // promotes to the result type, so will end up being expanded too.
197     assert(getTypeAction(Op.getValueType()) == Promote &&
198            "Don't know how to expand this result!");
199     SDOperand Res = GetPromotedOp(Op);
200     assert(Res.getValueType() == N->getValueType(0) &&
201            "Operand over promoted?");
202     // Split the promoted operand.  This will simplify when it is expanded.
203     SplitOp(Res, Lo, Hi);
204     unsigned ExcessBits =
205       MVT::getSizeInBits(Op.getValueType()) - MVT::getSizeInBits(NVT);
206     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
207                      DAG.getValueType(MVT::getIntegerType(ExcessBits)));
208   }
209 }
210
211 void DAGTypeLegalizer::ExpandResult_AssertZext(SDNode *N,
212                                                SDOperand &Lo, SDOperand &Hi) {
213   GetExpandedOp(N->getOperand(0), Lo, Hi);
214   MVT::ValueType NVT = Lo.getValueType();
215   MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
216   unsigned NVTBits = MVT::getSizeInBits(NVT);
217   unsigned EVTBits = MVT::getSizeInBits(EVT);
218
219   if (NVTBits < EVTBits) {
220     Hi = DAG.getNode(ISD::AssertZext, NVT, Hi,
221                      DAG.getValueType(MVT::getIntegerType(EVTBits - NVTBits)));
222   } else {
223     Lo = DAG.getNode(ISD::AssertZext, NVT, Lo, DAG.getValueType(EVT));
224     // The high part must be zero, make it explicit.
225     Hi = DAG.getConstant(0, NVT);
226   }
227 }
228
229 void DAGTypeLegalizer::ExpandResult_TRUNCATE(SDNode *N,
230                                              SDOperand &Lo, SDOperand &Hi) {
231   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
232   Lo = DAG.getNode(ISD::TRUNCATE, NVT, N->getOperand(0));
233   Hi = DAG.getNode(ISD::SRL, N->getOperand(0).getValueType(), N->getOperand(0),
234                    DAG.getConstant(MVT::getSizeInBits(NVT),
235                                    TLI.getShiftAmountTy()));
236   Hi = DAG.getNode(ISD::TRUNCATE, NVT, Hi);
237 }
238
239 void DAGTypeLegalizer::ExpandResult_BIT_CONVERT(SDNode *N,
240                                                 SDOperand &Lo, SDOperand &Hi) {
241   // Lower the bit-convert to a store/load from the stack, then expand the load.
242   SDOperand Op = CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
243   ExpandResult_LOAD(cast<LoadSDNode>(Op.Val), Lo, Hi);
244 }
245
246 void DAGTypeLegalizer::
247 ExpandResult_SIGN_EXTEND_INREG(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
248   GetExpandedOp(N->getOperand(0), Lo, Hi);
249   MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
250
251   if (MVT::getSizeInBits(EVT) <= MVT::getSizeInBits(Lo.getValueType())) {
252     // sext_inreg the low part if needed.
253     Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, Lo.getValueType(), Lo,
254                      N->getOperand(1));
255
256     // The high part gets the sign extension from the lo-part.  This handles
257     // things like sextinreg V:i64 from i8.
258     Hi = DAG.getNode(ISD::SRA, Hi.getValueType(), Lo,
259                      DAG.getConstant(MVT::getSizeInBits(Hi.getValueType())-1,
260                                      TLI.getShiftAmountTy()));
261   } else {
262     // For example, extension of an i48 to an i64.  Leave the low part alone,
263     // sext_inreg the high part.
264     unsigned ExcessBits =
265       MVT::getSizeInBits(EVT) - MVT::getSizeInBits(Lo.getValueType());
266     Hi = DAG.getNode(ISD::SIGN_EXTEND_INREG, Hi.getValueType(), Hi,
267                      DAG.getValueType(MVT::getIntegerType(ExcessBits)));
268   }
269 }
270
271 void DAGTypeLegalizer::ExpandResult_LOAD(LoadSDNode *N,
272                                          SDOperand &Lo, SDOperand &Hi) {
273   MVT::ValueType VT = N->getValueType(0);
274   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
275   SDOperand Ch  = N->getChain();    // Legalize the chain.
276   SDOperand Ptr = N->getBasePtr();  // Legalize the pointer.
277   ISD::LoadExtType ExtType = N->getExtensionType();
278   int SVOffset = N->getSrcValueOffset();
279   unsigned Alignment = N->getAlignment();
280   bool isVolatile = N->isVolatile();
281
282   assert(!(MVT::getSizeInBits(NVT) & 7) && "Expanded type not byte sized!");
283
284   if (ExtType == ISD::NON_EXTLOAD) {
285     Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
286                      isVolatile, Alignment);
287     // Increment the pointer to the other half.
288     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
289     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
290                       DAG.getIntPtrConstant(IncrementSize));
291     Hi = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
292                      isVolatile, MinAlign(Alignment, IncrementSize));
293
294     // Build a factor node to remember that this load is independent of the
295     // other one.
296     Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
297                      Hi.getValue(1));
298
299     // Handle endianness of the load.
300     if (TLI.isBigEndian())
301       std::swap(Lo, Hi);
302   } else if (MVT::getSizeInBits(N->getMemoryVT()) <= MVT::getSizeInBits(NVT)) {
303     MVT::ValueType EVT = N->getMemoryVT();
304
305     Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset, EVT,
306                         isVolatile, Alignment);
307
308     // Remember the chain.
309     Ch = Lo.getValue(1);
310
311     if (ExtType == ISD::SEXTLOAD) {
312       // The high part is obtained by SRA'ing all but one of the bits of the
313       // lo part.
314       unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
315       Hi = DAG.getNode(ISD::SRA, NVT, Lo,
316                        DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
317     } else if (ExtType == ISD::ZEXTLOAD) {
318       // The high part is just a zero.
319       Hi = DAG.getConstant(0, NVT);
320     } else {
321       assert(ExtType == ISD::EXTLOAD && "Unknown extload!");
322       // The high part is undefined.
323       Hi = DAG.getNode(ISD::UNDEF, NVT);
324     }
325   } else if (TLI.isLittleEndian()) {
326     // Little-endian - low bits are at low addresses.
327     Lo = DAG.getLoad(NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
328                      isVolatile, Alignment);
329
330     unsigned ExcessBits =
331       MVT::getSizeInBits(N->getMemoryVT()) - MVT::getSizeInBits(NVT);
332     MVT::ValueType NEVT = MVT::getIntegerType(ExcessBits);
333
334     // Increment the pointer to the other half.
335     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
336     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
337                       DAG.getIntPtrConstant(IncrementSize));
338     Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(),
339                         SVOffset+IncrementSize, NEVT,
340                         isVolatile, MinAlign(Alignment, IncrementSize));
341
342     // Build a factor node to remember that this load is independent of the
343     // other one.
344     Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
345                      Hi.getValue(1));
346   } else {
347     // Big-endian - high bits are at low addresses.  Favor aligned loads at
348     // the cost of some bit-fiddling.
349     MVT::ValueType EVT = N->getMemoryVT();
350     unsigned EBytes = MVT::getStoreSizeInBits(EVT)/8;
351     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
352     unsigned ExcessBits = (EBytes - IncrementSize)*8;
353
354     // Load both the high bits and maybe some of the low bits.
355     Hi = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, N->getSrcValue(), SVOffset,
356                         MVT::getIntegerType(MVT::getSizeInBits(EVT)-ExcessBits),
357                         isVolatile, Alignment);
358
359     // Increment the pointer to the other half.
360     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
361                       DAG.getIntPtrConstant(IncrementSize));
362     // Load the rest of the low bits.
363     Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Ch, Ptr, N->getSrcValue(),
364                         SVOffset+IncrementSize, MVT::getIntegerType(ExcessBits),
365                         isVolatile, MinAlign(Alignment, IncrementSize));
366
367     // Build a factor node to remember that this load is independent of the
368     // other one.
369     Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
370                      Hi.getValue(1));
371
372     if (ExcessBits < MVT::getSizeInBits(NVT)) {
373       // Transfer low bits from the bottom of Hi to the top of Lo.
374       Lo = DAG.getNode(ISD::OR, NVT, Lo,
375                        DAG.getNode(ISD::SHL, NVT, Hi,
376                                    DAG.getConstant(ExcessBits,
377                                                    TLI.getShiftAmountTy())));
378       // Move high bits to the right position in Hi.
379       Hi = DAG.getNode(ExtType == ISD::SEXTLOAD ? ISD::SRA : ISD::SRL, NVT, Hi,
380                        DAG.getConstant(MVT::getSizeInBits(NVT) - ExcessBits,
381                                        TLI.getShiftAmountTy()));
382     }
383   }
384
385   // Legalized the chain result - switch anything that used the old chain to
386   // use the new one.
387   ReplaceValueWith(SDOperand(N, 1), Ch);
388 }
389
390 void DAGTypeLegalizer::ExpandResult_Logical(SDNode *N,
391                                             SDOperand &Lo, SDOperand &Hi) {
392   SDOperand LL, LH, RL, RH;
393   GetExpandedOp(N->getOperand(0), LL, LH);
394   GetExpandedOp(N->getOperand(1), RL, RH);
395   Lo = DAG.getNode(N->getOpcode(), LL.getValueType(), LL, RL);
396   Hi = DAG.getNode(N->getOpcode(), LL.getValueType(), LH, RH);
397 }
398
399 void DAGTypeLegalizer::ExpandResult_BSWAP(SDNode *N,
400                                           SDOperand &Lo, SDOperand &Hi) {
401   GetExpandedOp(N->getOperand(0), Hi, Lo);  // Note swapped operands.
402   Lo = DAG.getNode(ISD::BSWAP, Lo.getValueType(), Lo);
403   Hi = DAG.getNode(ISD::BSWAP, Hi.getValueType(), Hi);
404 }
405
406 void DAGTypeLegalizer::ExpandResult_SELECT(SDNode *N,
407                                            SDOperand &Lo, SDOperand &Hi) {
408   SDOperand LL, LH, RL, RH;
409   GetExpandedOp(N->getOperand(1), LL, LH);
410   GetExpandedOp(N->getOperand(2), RL, RH);
411   Lo = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LL, RL);
412   
413   assert(N->getOperand(0).getValueType() != MVT::f32 &&
414          "FIXME: softfp shouldn't use expand!");
415   Hi = DAG.getNode(ISD::SELECT, LL.getValueType(), N->getOperand(0), LH, RH);
416 }
417
418 void DAGTypeLegalizer::ExpandResult_SELECT_CC(SDNode *N,
419                                               SDOperand &Lo, SDOperand &Hi) {
420   SDOperand LL, LH, RL, RH;
421   GetExpandedOp(N->getOperand(2), LL, LH);
422   GetExpandedOp(N->getOperand(3), RL, RH);
423   Lo = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0), 
424                    N->getOperand(1), LL, RL, N->getOperand(4));
425   
426   assert(N->getOperand(0).getValueType() != MVT::f32 &&
427          "FIXME: softfp shouldn't use expand!");
428   Hi = DAG.getNode(ISD::SELECT_CC, LL.getValueType(), N->getOperand(0), 
429                    N->getOperand(1), LH, RH, N->getOperand(4));
430 }
431
432 void DAGTypeLegalizer::ExpandResult_ADDSUB(SDNode *N,
433                                            SDOperand &Lo, SDOperand &Hi) {
434   // Expand the subcomponents.
435   SDOperand LHSL, LHSH, RHSL, RHSH;
436   GetExpandedOp(N->getOperand(0), LHSL, LHSH);
437   GetExpandedOp(N->getOperand(1), RHSL, RHSH);
438   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
439   SDOperand LoOps[2] = { LHSL, RHSL };
440   SDOperand HiOps[3] = { LHSH, RHSH };
441
442   if (N->getOpcode() == ISD::ADD) {
443     Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
444     HiOps[2] = Lo.getValue(1);
445     Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
446   } else {
447     Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
448     HiOps[2] = Lo.getValue(1);
449     Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
450   }
451 }
452
453 void DAGTypeLegalizer::ExpandResult_ADDSUBC(SDNode *N,
454                                             SDOperand &Lo, SDOperand &Hi) {
455   // Expand the subcomponents.
456   SDOperand LHSL, LHSH, RHSL, RHSH;
457   GetExpandedOp(N->getOperand(0), LHSL, LHSH);
458   GetExpandedOp(N->getOperand(1), RHSL, RHSH);
459   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
460   SDOperand LoOps[2] = { LHSL, RHSL };
461   SDOperand HiOps[3] = { LHSH, RHSH };
462
463   if (N->getOpcode() == ISD::ADDC) {
464     Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
465     HiOps[2] = Lo.getValue(1);
466     Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
467   } else {
468     Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
469     HiOps[2] = Lo.getValue(1);
470     Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
471   }
472
473   // Legalized the flag result - switch anything that used the old flag to
474   // use the new one.
475   ReplaceValueWith(SDOperand(N, 1), Hi.getValue(1));
476 }
477
478 void DAGTypeLegalizer::ExpandResult_ADDSUBE(SDNode *N,
479                                             SDOperand &Lo, SDOperand &Hi) {
480   // Expand the subcomponents.
481   SDOperand LHSL, LHSH, RHSL, RHSH;
482   GetExpandedOp(N->getOperand(0), LHSL, LHSH);
483   GetExpandedOp(N->getOperand(1), RHSL, RHSH);
484   SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
485   SDOperand LoOps[3] = { LHSL, RHSL, N->getOperand(2) };
486   SDOperand HiOps[3] = { LHSH, RHSH };
487
488   Lo = DAG.getNode(N->getOpcode(), VTList, LoOps, 3);
489   HiOps[2] = Lo.getValue(1);
490   Hi = DAG.getNode(N->getOpcode(), VTList, HiOps, 3);
491
492   // Legalized the flag result - switch anything that used the old flag to
493   // use the new one.
494   ReplaceValueWith(SDOperand(N, 1), Hi.getValue(1));
495 }
496
497 void DAGTypeLegalizer::ExpandResult_MUL(SDNode *N,
498                                         SDOperand &Lo, SDOperand &Hi) {
499   MVT::ValueType VT = N->getValueType(0);
500   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
501   
502   bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
503   bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
504   bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
505   bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
506   if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
507     SDOperand LL, LH, RL, RH;
508     GetExpandedOp(N->getOperand(0), LL, LH);
509     GetExpandedOp(N->getOperand(1), RL, RH);
510     unsigned BitSize = MVT::getSizeInBits(NVT);
511     unsigned LHSSB = DAG.ComputeNumSignBits(N->getOperand(0));
512     unsigned RHSSB = DAG.ComputeNumSignBits(N->getOperand(1));
513     
514     // FIXME: generalize this to handle other bit sizes
515     if (LHSSB == 32 && RHSSB == 32 &&
516         DAG.MaskedValueIsZero(N->getOperand(0), 0xFFFFFFFF00000000ULL) &&
517         DAG.MaskedValueIsZero(N->getOperand(1), 0xFFFFFFFF00000000ULL)) {
518       // The inputs are both zero-extended.
519       if (HasUMUL_LOHI) {
520         // We can emit a umul_lohi.
521         Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
522         Hi = SDOperand(Lo.Val, 1);
523         return;
524       }
525       if (HasMULHU) {
526         // We can emit a mulhu+mul.
527         Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
528         Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
529         return;
530       }
531     }
532     if (LHSSB > BitSize && RHSSB > BitSize) {
533       // The input values are both sign-extended.
534       if (HasSMUL_LOHI) {
535         // We can emit a smul_lohi.
536         Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
537         Hi = SDOperand(Lo.Val, 1);
538         return;
539       }
540       if (HasMULHS) {
541         // We can emit a mulhs+mul.
542         Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
543         Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
544         return;
545       }
546     }
547     if (HasUMUL_LOHI) {
548       // Lo,Hi = umul LHS, RHS.
549       SDOperand UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
550                                        DAG.getVTList(NVT, NVT), LL, RL);
551       Lo = UMulLOHI;
552       Hi = UMulLOHI.getValue(1);
553       RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
554       LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
555       Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
556       Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
557       return;
558     }
559   }
560   
561   abort();
562 #if 0 // FIXME!
563   // If nothing else, we can make a libcall.
564   Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::MUL_I64), N,
565                      false/*sign irrelevant*/, Hi);
566 #endif
567 }  
568
569
570 void DAGTypeLegalizer::ExpandResult_Shift(SDNode *N,
571                                           SDOperand &Lo, SDOperand &Hi) {
572   MVT::ValueType VT = N->getValueType(0);
573   
574   // If we can emit an efficient shift operation, do so now.  Check to see if 
575   // the RHS is a constant.
576   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N->getOperand(1)))
577     return ExpandShiftByConstant(N, CN->getValue(), Lo, Hi);
578
579   // If we can determine that the high bit of the shift is zero or one, even if
580   // the low bits are variable, emit this shift in an optimized form.
581   if (ExpandShiftWithKnownAmountBit(N, Lo, Hi))
582     return;
583   
584   // If this target supports shift_PARTS, use it.  First, map to the _PARTS opc.
585   unsigned PartsOpc;
586   if (N->getOpcode() == ISD::SHL)
587     PartsOpc = ISD::SHL_PARTS;
588   else if (N->getOpcode() == ISD::SRL)
589     PartsOpc = ISD::SRL_PARTS;
590   else {
591     assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
592     PartsOpc = ISD::SRA_PARTS;
593   }
594   
595   // Next check to see if the target supports this SHL_PARTS operation or if it
596   // will custom expand it.
597   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
598   TargetLowering::LegalizeAction Action = TLI.getOperationAction(PartsOpc, NVT);
599   if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
600       Action == TargetLowering::Custom) {
601     // Expand the subcomponents.
602     SDOperand LHSL, LHSH;
603     GetExpandedOp(N->getOperand(0), LHSL, LHSH);
604     
605     SDOperand Ops[] = { LHSL, LHSH, N->getOperand(1) };
606     MVT::ValueType VT = LHSL.getValueType();
607     Lo = DAG.getNode(PartsOpc, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
608     Hi = Lo.getValue(1);
609     return;
610   }
611   
612   abort();
613 #if 0 // FIXME!
614   // Otherwise, emit a libcall.
615   unsigned RuntimeCode = ; // SRL -> SRL_I64 etc.
616   bool Signed = ;
617   Lo = ExpandLibCall(TLI.getLibcallName(RTLIB::SRL_I64), N,
618                      false/*lshr is unsigned*/, Hi);
619 #endif
620 }  
621
622 void DAGTypeLegalizer::ExpandResult_CTLZ(SDNode *N,
623                                          SDOperand &Lo, SDOperand &Hi) {
624   // ctlz (HiLo) -> Hi != 0 ? ctlz(Hi) : (ctlz(Lo)+32)
625   GetExpandedOp(N->getOperand(0), Lo, Hi);
626   MVT::ValueType NVT = Lo.getValueType();
627
628   SDOperand HiNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
629                                      DAG.getConstant(0, NVT), ISD::SETNE);
630
631   SDOperand LoLZ = DAG.getNode(ISD::CTLZ, NVT, Lo);
632   SDOperand HiLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
633
634   Lo = DAG.getNode(ISD::SELECT, NVT, HiNotZero, HiLZ,
635                    DAG.getNode(ISD::ADD, NVT, LoLZ,
636                                DAG.getConstant(MVT::getSizeInBits(NVT), NVT)));
637   Hi = DAG.getConstant(0, NVT);
638 }
639
640 void DAGTypeLegalizer::ExpandResult_CTPOP(SDNode *N,
641                                           SDOperand &Lo, SDOperand &Hi) {
642   // ctpop(HiLo) -> ctpop(Hi)+ctpop(Lo)
643   GetExpandedOp(N->getOperand(0), Lo, Hi);
644   MVT::ValueType NVT = Lo.getValueType();
645   Lo = DAG.getNode(ISD::ADD, NVT, DAG.getNode(ISD::CTPOP, NVT, Lo),
646                    DAG.getNode(ISD::CTPOP, NVT, Hi));
647   Hi = DAG.getConstant(0, NVT);
648 }
649
650 void DAGTypeLegalizer::ExpandResult_CTTZ(SDNode *N,
651                                          SDOperand &Lo, SDOperand &Hi) {
652   // cttz (HiLo) -> Lo != 0 ? cttz(Lo) : (cttz(Hi)+32)
653   GetExpandedOp(N->getOperand(0), Lo, Hi);
654   MVT::ValueType NVT = Lo.getValueType();
655
656   SDOperand LoNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), Lo,
657                                      DAG.getConstant(0, NVT), ISD::SETNE);
658
659   SDOperand LoLZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
660   SDOperand HiLZ = DAG.getNode(ISD::CTTZ, NVT, Hi);
661
662   Lo = DAG.getNode(ISD::SELECT, NVT, LoNotZero, LoLZ,
663                    DAG.getNode(ISD::ADD, NVT, HiLZ,
664                                DAG.getConstant(MVT::getSizeInBits(NVT), NVT)));
665   Hi = DAG.getConstant(0, NVT);
666 }
667
668 /// ExpandShiftByConstant - N is a shift by a value that needs to be expanded,
669 /// and the shift amount is a constant 'Amt'.  Expand the operation.
670 void DAGTypeLegalizer::ExpandShiftByConstant(SDNode *N, unsigned Amt, 
671                                              SDOperand &Lo, SDOperand &Hi) {
672   // Expand the incoming operand to be shifted, so that we have its parts
673   SDOperand InL, InH;
674   GetExpandedOp(N->getOperand(0), InL, InH);
675   
676   MVT::ValueType NVT = InL.getValueType();
677   unsigned VTBits = MVT::getSizeInBits(N->getValueType(0));
678   unsigned NVTBits = MVT::getSizeInBits(NVT);
679   MVT::ValueType ShTy = N->getOperand(1).getValueType();
680
681   if (N->getOpcode() == ISD::SHL) {
682     if (Amt > VTBits) {
683       Lo = Hi = DAG.getConstant(0, NVT);
684     } else if (Amt > NVTBits) {
685       Lo = DAG.getConstant(0, NVT);
686       Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt-NVTBits,ShTy));
687     } else if (Amt == NVTBits) {
688       Lo = DAG.getConstant(0, NVT);
689       Hi = InL;
690     } else {
691       Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Amt, ShTy));
692       Hi = DAG.getNode(ISD::OR, NVT,
693                        DAG.getNode(ISD::SHL, NVT, InH,
694                                    DAG.getConstant(Amt, ShTy)),
695                        DAG.getNode(ISD::SRL, NVT, InL,
696                                    DAG.getConstant(NVTBits-Amt, ShTy)));
697     }
698     return;
699   }
700   
701   if (N->getOpcode() == ISD::SRL) {
702     if (Amt > VTBits) {
703       Lo = DAG.getConstant(0, NVT);
704       Hi = DAG.getConstant(0, NVT);
705     } else if (Amt > NVTBits) {
706       Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt-NVTBits,ShTy));
707       Hi = DAG.getConstant(0, NVT);
708     } else if (Amt == NVTBits) {
709       Lo = InH;
710       Hi = DAG.getConstant(0, NVT);
711     } else {
712       Lo = DAG.getNode(ISD::OR, NVT,
713                        DAG.getNode(ISD::SRL, NVT, InL,
714                                    DAG.getConstant(Amt, ShTy)),
715                        DAG.getNode(ISD::SHL, NVT, InH,
716                                    DAG.getConstant(NVTBits-Amt, ShTy)));
717       Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Amt, ShTy));
718     }
719     return;
720   }
721   
722   assert(N->getOpcode() == ISD::SRA && "Unknown shift!");
723   if (Amt > VTBits) {
724     Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
725                           DAG.getConstant(NVTBits-1, ShTy));
726   } else if (Amt > NVTBits) {
727     Lo = DAG.getNode(ISD::SRA, NVT, InH,
728                      DAG.getConstant(Amt-NVTBits, ShTy));
729     Hi = DAG.getNode(ISD::SRA, NVT, InH,
730                      DAG.getConstant(NVTBits-1, ShTy));
731   } else if (Amt == NVTBits) {
732     Lo = InH;
733     Hi = DAG.getNode(ISD::SRA, NVT, InH,
734                      DAG.getConstant(NVTBits-1, ShTy));
735   } else {
736     Lo = DAG.getNode(ISD::OR, NVT,
737                      DAG.getNode(ISD::SRL, NVT, InL,
738                                  DAG.getConstant(Amt, ShTy)),
739                      DAG.getNode(ISD::SHL, NVT, InH,
740                                  DAG.getConstant(NVTBits-Amt, ShTy)));
741     Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Amt, ShTy));
742   }
743 }
744
745 /// ExpandShiftWithKnownAmountBit - Try to determine whether we can simplify
746 /// this shift based on knowledge of the high bit of the shift amount.  If we
747 /// can tell this, we know that it is >= 32 or < 32, without knowing the actual
748 /// shift amount.
749 bool DAGTypeLegalizer::
750 ExpandShiftWithKnownAmountBit(SDNode *N, SDOperand &Lo, SDOperand &Hi) {
751   MVT::ValueType NVT = TLI.getTypeToTransformTo(N->getValueType(0));
752   unsigned NVTBits = MVT::getSizeInBits(NVT);
753   assert(!(NVTBits & (NVTBits - 1)) &&
754          "Expanded integer type size not a power of two!");
755
756   uint64_t HighBitMask = NVTBits, KnownZero, KnownOne;
757   DAG.ComputeMaskedBits(N->getOperand(1), HighBitMask, KnownZero, KnownOne);
758   
759   // If we don't know anything about the high bit, exit.
760   if (((KnownZero|KnownOne) & HighBitMask) == 0)
761     return false;
762
763   // Get the incoming operand to be shifted.
764   SDOperand InL, InH;
765   GetExpandedOp(N->getOperand(0), InL, InH);
766   SDOperand Amt = N->getOperand(1);
767
768   // If we know that the high bit of the shift amount is one, then we can do
769   // this as a couple of simple shifts.
770   if (KnownOne & HighBitMask) {
771     // Mask out the high bit, which we know is set.
772     Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
773                       DAG.getConstant(NVTBits-1, Amt.getValueType()));
774     
775     switch (N->getOpcode()) {
776     default: assert(0 && "Unknown shift");
777     case ISD::SHL:
778       Lo = DAG.getConstant(0, NVT);              // Low part is zero.
779       Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
780       return true;
781     case ISD::SRL:
782       Hi = DAG.getConstant(0, NVT);              // Hi part is zero.
783       Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
784       return true;
785     case ISD::SRA:
786       Hi = DAG.getNode(ISD::SRA, NVT, InH,       // Sign extend high part.
787                        DAG.getConstant(NVTBits-1, Amt.getValueType()));
788       Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
789       return true;
790     }
791   }
792   
793   // If we know that the high bit of the shift amount is zero, then we can do
794   // this as a couple of simple shifts.
795   assert((KnownZero & HighBitMask) && "Bad mask computation above");
796
797   // Compute 32-amt.
798   SDOperand Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
799                                DAG.getConstant(NVTBits, Amt.getValueType()),
800                                Amt);
801   unsigned Op1, Op2;
802   switch (N->getOpcode()) {
803   default: assert(0 && "Unknown shift");
804   case ISD::SHL:  Op1 = ISD::SHL; Op2 = ISD::SRL; break;
805   case ISD::SRL:
806   case ISD::SRA:  Op1 = ISD::SRL; Op2 = ISD::SHL; break;
807   }
808     
809   Lo = DAG.getNode(N->getOpcode(), NVT, InL, Amt);
810   Hi = DAG.getNode(ISD::OR, NVT,
811                    DAG.getNode(Op1, NVT, InH, Amt),
812                    DAG.getNode(Op2, NVT, InL, Amt2));
813   return true;
814 }
815
816
817 //===----------------------------------------------------------------------===//
818 //  Operand Expansion
819 //===----------------------------------------------------------------------===//
820
821 /// ExpandOperand - This method is called when the specified operand of the
822 /// specified node is found to need expansion.  At this point, all of the result
823 /// types of the node are known to be legal, but other operands of the node may
824 /// need promotion or expansion as well as the specified one.
825 bool DAGTypeLegalizer::ExpandOperand(SDNode *N, unsigned OpNo) {
826   DEBUG(cerr << "Expand node operand: "; N->dump(&DAG); cerr << "\n");
827   SDOperand Res(0, 0);
828   
829   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
830       == TargetLowering::Custom)
831     Res = TLI.LowerOperation(SDOperand(N, 0), DAG);
832   
833   if (Res.Val == 0) {
834     switch (N->getOpcode()) {
835     default:
836   #ifndef NDEBUG
837       cerr << "ExpandOperand Op #" << OpNo << ": ";
838       N->dump(&DAG); cerr << "\n";
839   #endif
840       assert(0 && "Do not know how to expand this operator's operand!");
841       abort();
842       
843     case ISD::TRUNCATE:        Res = ExpandOperand_TRUNCATE(N); break;
844     case ISD::BIT_CONVERT:     Res = ExpandOperand_BIT_CONVERT(N); break;
845
846     case ISD::SINT_TO_FP:
847       Res = ExpandOperand_SINT_TO_FP(N->getOperand(0), N->getValueType(0));
848       break;
849     case ISD::UINT_TO_FP:
850       Res = ExpandOperand_UINT_TO_FP(N->getOperand(0), N->getValueType(0)); 
851       break;
852     case ISD::EXTRACT_ELEMENT: Res = ExpandOperand_EXTRACT_ELEMENT(N); break;
853
854     case ISD::BR_CC:           Res = ExpandOperand_BR_CC(N); break;
855     case ISD::SETCC:           Res = ExpandOperand_SETCC(N); break;
856
857     case ISD::STORE:
858       Res = ExpandOperand_STORE(cast<StoreSDNode>(N), OpNo);
859       break;
860     case ISD::MEMSET:
861     case ISD::MEMCPY:
862     case ISD::MEMMOVE:     Res = HandleMemIntrinsic(N); break;
863     }
864   }
865   
866   // If the result is null, the sub-method took care of registering results etc.
867   if (!Res.Val) return false;
868   // If the result is N, the sub-method updated N in place.  Check to see if any
869   // operands are new, and if so, mark them.
870   if (Res.Val == N) {
871     // Mark N as new and remark N and its operands.  This allows us to correctly
872     // revisit N if it needs another step of promotion and allows us to visit
873     // any new operands to N.
874     N->setNodeId(NewNode);
875     MarkNewNodes(N);
876     return true;
877   }
878
879   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
880          "Invalid operand expansion");
881   
882   ReplaceValueWith(SDOperand(N, 0), Res);
883   return false;
884 }
885
886 SDOperand DAGTypeLegalizer::ExpandOperand_TRUNCATE(SDNode *N) {
887   SDOperand InL, InH;
888   GetExpandedOp(N->getOperand(0), InL, InH);
889   // Just truncate the low part of the source.
890   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
891 }
892
893 SDOperand DAGTypeLegalizer::ExpandOperand_BIT_CONVERT(SDNode *N) {
894   return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
895 }
896
897 SDOperand DAGTypeLegalizer::ExpandOperand_SINT_TO_FP(SDOperand Source, 
898                                                      MVT::ValueType DestTy) {
899   // We know the destination is legal, but that the input needs to be expanded.
900   assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
901   
902   // Check to see if the target has a custom way to lower this.  If so, use it.
903   switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
904   default: assert(0 && "This action not implemented for this operation!");
905   case TargetLowering::Legal:
906   case TargetLowering::Expand:
907     break;   // This case is handled below.
908   case TargetLowering::Custom:
909     SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
910                                                   Source), DAG);
911     if (NV.Val) return NV;
912     break;   // The target lowered this.
913   }
914   
915   RTLIB::Libcall LC;
916   if (DestTy == MVT::f32)
917     LC = RTLIB::SINTTOFP_I64_F32;
918   else {
919     assert(DestTy == MVT::f64 && "Unknown fp value type!");
920     LC = RTLIB::SINTTOFP_I64_F64;
921   }
922   
923   assert(0 && "FIXME: no libcalls yet!");
924   abort();
925 #if 0
926   assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
927   Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
928   SDOperand UnusedHiPart;
929   return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, true, UnusedHiPart);
930 #endif
931 }
932
933 SDOperand DAGTypeLegalizer::ExpandOperand_UINT_TO_FP(SDOperand Source, 
934                                                      MVT::ValueType DestTy) {
935   // We know the destination is legal, but that the input needs to be expanded.
936   assert(getTypeAction(Source.getValueType()) == Expand &&
937          "This is not an expansion!");
938   assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
939   
940   // If this is unsigned, and not supported, first perform the conversion to
941   // signed, then adjust the result if the sign bit is set.
942   SDOperand SignedConv = ExpandOperand_SINT_TO_FP(Source, DestTy);
943
944   // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
945   // incoming integer is set.  To handle this, we dynamically test to see if
946   // it is set, and, if so, add a fudge factor.
947   SDOperand Lo, Hi;
948   GetExpandedOp(Source, Lo, Hi);
949   
950   SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
951                                    DAG.getConstant(0, Hi.getValueType()),
952                                    ISD::SETLT);
953   SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
954   SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
955                                     SignSet, Four, Zero);
956   uint64_t FF = 0x5f800000ULL;
957   if (TLI.isLittleEndian()) FF <<= 32;
958   Constant *FudgeFactor = ConstantInt::get((Type*)Type::Int64Ty, FF);
959   
960   SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
961   CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
962   SDOperand FudgeInReg;
963   if (DestTy == MVT::f32)
964     FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
965   else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
966     // FIXME: Avoid the extend by construction the right constantpool?
967     FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
968                                 CPIdx, NULL, 0, MVT::f32);
969   else 
970     assert(0 && "Unexpected conversion");
971   
972   return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
973 }
974
975 SDOperand DAGTypeLegalizer::ExpandOperand_EXTRACT_ELEMENT(SDNode *N) {
976   SDOperand Lo, Hi;
977   GetExpandedOp(N->getOperand(0), Lo, Hi);
978   return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
979 }
980
981 SDOperand DAGTypeLegalizer::ExpandOperand_BR_CC(SDNode *N) {
982   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
983   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
984   ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
985
986   // If ExpandSetCCOperands returned a scalar, we need to compare the result
987   // against zero to select between true and false values.
988   if (NewRHS.Val == 0) {
989     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
990     CCCode = ISD::SETNE;
991   }
992
993   // Update N to have the operands specified.
994   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
995                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
996                                 N->getOperand(4));
997 }
998
999 SDOperand DAGTypeLegalizer::ExpandOperand_SETCC(SDNode *N) {
1000   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1001   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1002   ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1003   
1004   // If ExpandSetCCOperands returned a scalar, use it.
1005   if (NewRHS.Val == 0) return NewLHS;
1006
1007   // Otherwise, update N to have the operands specified.
1008   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1009                                 DAG.getCondCode(CCCode));
1010 }
1011
1012 /// ExpandSetCCOperands - Expand the operands of a comparison.  This code is
1013 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
1014 void DAGTypeLegalizer::ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
1015                                            ISD::CondCode &CCCode) {
1016   SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1017   GetExpandedOp(NewLHS, LHSLo, LHSHi);
1018   GetExpandedOp(NewRHS, RHSLo, RHSHi);
1019   
1020   MVT::ValueType VT = NewLHS.getValueType();
1021   if (VT == MVT::f32 || VT == MVT::f64) {
1022     assert(0 && "FIXME: softfp not implemented yet! should be promote not exp");
1023   }
1024   
1025   if (VT == MVT::ppcf128) {
1026     // FIXME:  This generated code sucks.  We want to generate
1027     //         FCMP crN, hi1, hi2
1028     //         BNE crN, L:
1029     //         FCMP crN, lo1, lo2
1030     // The following can be improved, but not that much.
1031     SDOperand Tmp1, Tmp2, Tmp3;
1032     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1033     Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
1034     Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1035     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
1036     Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
1037     Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1038     NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1039     NewRHS = SDOperand();   // LHS is the result, not a compare.
1040     return;
1041   }
1042   
1043   
1044   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1045     if (RHSLo == RHSHi)
1046       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1047         if (RHSCST->isAllOnesValue()) {
1048           // Equality comparison to -1.
1049           NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1050           NewRHS = RHSLo;
1051           return;
1052         }
1053           
1054     NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1055     NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1056     NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1057     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1058     return;
1059   }
1060   
1061   // If this is a comparison of the sign bit, just look at the top part.
1062   // X > -1,  x < 0
1063   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1064     if ((CCCode == ISD::SETLT && CST->getValue() == 0) ||   // X < 0
1065         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
1066       NewLHS = LHSHi;
1067       NewRHS = RHSHi;
1068       return;
1069     }
1070       
1071   // FIXME: This generated code sucks.
1072   ISD::CondCode LowCC;
1073   switch (CCCode) {
1074   default: assert(0 && "Unknown integer setcc!");
1075   case ISD::SETLT:
1076   case ISD::SETULT: LowCC = ISD::SETULT; break;
1077   case ISD::SETGT:
1078   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1079   case ISD::SETLE:
1080   case ISD::SETULE: LowCC = ISD::SETULE; break;
1081   case ISD::SETGE:
1082   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1083   }
1084   
1085   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
1086   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
1087   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1088   
1089   // NOTE: on targets without efficient SELECT of bools, we can always use
1090   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1091   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1092   SDOperand Tmp1, Tmp2;
1093   Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
1094                            false, DagCombineInfo);
1095   if (!Tmp1.Val)
1096     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
1097   Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1098                            CCCode, false, DagCombineInfo);
1099   if (!Tmp2.Val)
1100     Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,
1101                        DAG.getCondCode(CCCode));
1102   
1103   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
1104   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
1105   if ((Tmp1C && Tmp1C->getValue() == 0) ||
1106       (Tmp2C && Tmp2C->getValue() == 0 &&
1107        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1108         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1109       (Tmp2C && Tmp2C->getValue() == 1 &&
1110        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1111         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1112     // low part is known false, returns high part.
1113     // For LE / GE, if high part is known false, ignore the low part.
1114     // For LT / GT, if high part is known true, ignore the low part.
1115     NewLHS = Tmp2;
1116     NewRHS = SDOperand();
1117     return;
1118   }
1119   
1120   NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1121                              ISD::SETEQ, false, DagCombineInfo);
1122   if (!NewLHS.Val)
1123     NewLHS = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1124   NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1125                        NewLHS, Tmp1, Tmp2);
1126   NewRHS = SDOperand();
1127 }
1128
1129 SDOperand DAGTypeLegalizer::ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo) {
1130   assert(OpNo == 1 && "Can only expand the stored value so far");
1131
1132   MVT::ValueType VT = N->getOperand(1).getValueType();
1133   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1134   SDOperand Ch  = N->getChain();
1135   SDOperand Ptr = N->getBasePtr();
1136   int SVOffset = N->getSrcValueOffset();
1137   unsigned Alignment = N->getAlignment();
1138   bool isVolatile = N->isVolatile();
1139   SDOperand Lo, Hi;
1140
1141   assert(!(MVT::getSizeInBits(NVT) & 7) && "Expanded type not byte sized!");
1142
1143   if (!N->isTruncatingStore()) {
1144     unsigned IncrementSize = 0;
1145     GetExpandedOp(N->getValue(), Lo, Hi);
1146     IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1147
1148     if (TLI.isBigEndian())
1149       std::swap(Lo, Hi);
1150
1151     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(),
1152                       SVOffset, isVolatile, Alignment);
1153
1154     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1155                       DAG.getIntPtrConstant(IncrementSize));
1156     assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
1157     Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
1158                       isVolatile, MinAlign(Alignment, IncrementSize));
1159     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1160   } else if (MVT::getSizeInBits(N->getMemoryVT()) <= MVT::getSizeInBits(NVT)) {
1161     GetExpandedOp(N->getValue(), Lo, Hi);
1162     return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
1163                              N->getMemoryVT(), isVolatile, Alignment);
1164   } else if (TLI.isLittleEndian()) {
1165     // Little-endian - low bits are at low addresses.
1166     GetExpandedOp(N->getValue(), Lo, Hi);
1167
1168     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
1169                       isVolatile, Alignment);
1170
1171     unsigned ExcessBits =
1172       MVT::getSizeInBits(N->getMemoryVT()) - MVT::getSizeInBits(NVT);
1173     MVT::ValueType NEVT = MVT::getIntegerType(ExcessBits);
1174
1175     // Increment the pointer to the other half.
1176     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1177     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1178                       DAG.getIntPtrConstant(IncrementSize));
1179     Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
1180                            SVOffset+IncrementSize, NEVT,
1181                            isVolatile, MinAlign(Alignment, IncrementSize));
1182     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1183   } else {
1184     // Big-endian - high bits are at low addresses.  Favor aligned stores at
1185     // the cost of some bit-fiddling.
1186     GetExpandedOp(N->getValue(), Lo, Hi);
1187
1188     MVT::ValueType EVT = N->getMemoryVT();
1189     unsigned EBytes = MVT::getStoreSizeInBits(EVT)/8;
1190     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1191     unsigned ExcessBits = (EBytes - IncrementSize)*8;
1192     MVT::ValueType HiVT =
1193       MVT::getIntegerType(MVT::getSizeInBits(EVT)-ExcessBits);
1194
1195     if (ExcessBits < MVT::getSizeInBits(NVT)) {
1196       // Transfer high bits from the top of Lo to the bottom of Hi.
1197       Hi = DAG.getNode(ISD::SHL, NVT, Hi,
1198                        DAG.getConstant(MVT::getSizeInBits(NVT) - ExcessBits,
1199                                        TLI.getShiftAmountTy()));
1200       Hi = DAG.getNode(ISD::OR, NVT, Hi,
1201                        DAG.getNode(ISD::SRL, NVT, Lo,
1202                                    DAG.getConstant(ExcessBits,
1203                                                    TLI.getShiftAmountTy())));
1204     }
1205
1206     // Store both the high bits and maybe some of the low bits.
1207     Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
1208                            SVOffset, HiVT, isVolatile, Alignment);
1209
1210     // Increment the pointer to the other half.
1211     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1212                       DAG.getIntPtrConstant(IncrementSize));
1213     // Store the lowest ExcessBits bits in the second half.
1214     Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
1215                            SVOffset+IncrementSize,
1216                            MVT::getIntegerType(ExcessBits),
1217                            isVolatile, MinAlign(Alignment, IncrementSize));
1218     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1219   }
1220 }
1221