Change how FP immediates are handled.
[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     case ISD::SETCC:           Res = ExpandOperand_SETCC(N); break;
854
855     case ISD::STORE:
856       Res = ExpandOperand_STORE(cast<StoreSDNode>(N), OpNo);
857       break;
858     case ISD::MEMSET:
859     case ISD::MEMCPY:
860     case ISD::MEMMOVE:     Res = HandleMemIntrinsic(N); break;
861     }
862   }
863   
864   // If the result is null, the sub-method took care of registering results etc.
865   if (!Res.Val) return false;
866   // If the result is N, the sub-method updated N in place.  Check to see if any
867   // operands are new, and if so, mark them.
868   if (Res.Val == N) {
869     // Mark N as new and remark N and its operands.  This allows us to correctly
870     // revisit N if it needs another step of promotion and allows us to visit
871     // any new operands to N.
872     N->setNodeId(NewNode);
873     MarkNewNodes(N);
874     return true;
875   }
876
877   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
878          "Invalid operand expansion");
879   
880   ReplaceValueWith(SDOperand(N, 0), Res);
881   return false;
882 }
883
884 SDOperand DAGTypeLegalizer::ExpandOperand_TRUNCATE(SDNode *N) {
885   SDOperand InL, InH;
886   GetExpandedOp(N->getOperand(0), InL, InH);
887   // Just truncate the low part of the source.
888   return DAG.getNode(ISD::TRUNCATE, N->getValueType(0), InL);
889 }
890
891 SDOperand DAGTypeLegalizer::ExpandOperand_BIT_CONVERT(SDNode *N) {
892   return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
893 }
894
895 SDOperand DAGTypeLegalizer::ExpandOperand_SINT_TO_FP(SDOperand Source, 
896                                                      MVT::ValueType DestTy) {
897   // We know the destination is legal, but that the input needs to be expanded.
898   assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
899   
900   // Check to see if the target has a custom way to lower this.  If so, use it.
901   switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
902   default: assert(0 && "This action not implemented for this operation!");
903   case TargetLowering::Legal:
904   case TargetLowering::Expand:
905     break;   // This case is handled below.
906   case TargetLowering::Custom:
907     SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
908                                                   Source), DAG);
909     if (NV.Val) return NV;
910     break;   // The target lowered this.
911   }
912   
913   RTLIB::Libcall LC;
914   if (DestTy == MVT::f32)
915     LC = RTLIB::SINTTOFP_I64_F32;
916   else {
917     assert(DestTy == MVT::f64 && "Unknown fp value type!");
918     LC = RTLIB::SINTTOFP_I64_F64;
919   }
920   
921   assert(0 && "FIXME: no libcalls yet!");
922   abort();
923 #if 0
924   assert(TLI.getLibcallName(LC) && "Don't know how to expand this SINT_TO_FP!");
925   Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
926   SDOperand UnusedHiPart;
927   return ExpandLibCall(TLI.getLibcallName(LC), Source.Val, true, UnusedHiPart);
928 #endif
929 }
930
931 SDOperand DAGTypeLegalizer::ExpandOperand_UINT_TO_FP(SDOperand Source, 
932                                                      MVT::ValueType DestTy) {
933   // We know the destination is legal, but that the input needs to be expanded.
934   assert(getTypeAction(Source.getValueType()) == Expand &&
935          "This is not an expansion!");
936   assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
937   
938   // If this is unsigned, and not supported, first perform the conversion to
939   // signed, then adjust the result if the sign bit is set.
940   SDOperand SignedConv = ExpandOperand_SINT_TO_FP(Source, DestTy);
941
942   // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
943   // incoming integer is set.  To handle this, we dynamically test to see if
944   // it is set, and, if so, add a fudge factor.
945   SDOperand Lo, Hi;
946   GetExpandedOp(Source, Lo, Hi);
947   
948   SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
949                                    DAG.getConstant(0, Hi.getValueType()),
950                                    ISD::SETLT);
951   SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
952   SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
953                                     SignSet, Four, Zero);
954   uint64_t FF = 0x5f800000ULL;
955   if (TLI.isLittleEndian()) FF <<= 32;
956   Constant *FudgeFactor = ConstantInt::get((Type*)Type::Int64Ty, FF);
957   
958   SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
959   CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
960   SDOperand FudgeInReg;
961   if (DestTy == MVT::f32)
962     FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, NULL, 0);
963   else if (MVT::getSizeInBits(DestTy) > MVT::getSizeInBits(MVT::f32))
964     // FIXME: Avoid the extend by construction the right constantpool?
965     FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
966                                 CPIdx, NULL, 0, MVT::f32);
967   else 
968     assert(0 && "Unexpected conversion");
969   
970   return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
971 }
972
973 SDOperand DAGTypeLegalizer::ExpandOperand_EXTRACT_ELEMENT(SDNode *N) {
974   SDOperand Lo, Hi;
975   GetExpandedOp(N->getOperand(0), Lo, Hi);
976   return cast<ConstantSDNode>(N->getOperand(1))->getValue() ? Hi : Lo;
977 }
978
979 SDOperand DAGTypeLegalizer::ExpandOperand_SETCC(SDNode *N) {
980   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
981   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
982   ExpandSetCCOperands(NewLHS, NewRHS, CCCode);
983   
984   // If ExpandSetCCOperands returned a scalar, use it.
985   if (NewRHS.Val == 0) return NewLHS;
986
987   // Otherwise, update N to have the operands specified.
988   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
989                                 DAG.getCondCode(CCCode));
990 }
991
992 /// ExpandSetCCOperands - Expand the operands of a comparison.  This code is
993 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
994 void DAGTypeLegalizer::ExpandSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
995                                            ISD::CondCode &CCCode) {
996   SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
997   GetExpandedOp(NewLHS, LHSLo, LHSHi);
998   GetExpandedOp(NewRHS, RHSLo, RHSHi);
999   
1000   MVT::ValueType VT = NewLHS.getValueType();
1001   if (VT == MVT::f32 || VT == MVT::f64) {
1002     assert(0 && "FIXME: softfp not implemented yet! should be promote not exp");
1003   }
1004   
1005   if (VT == MVT::ppcf128) {
1006     // FIXME:  This generated code sucks.  We want to generate
1007     //         FCMP crN, hi1, hi2
1008     //         BNE crN, L:
1009     //         FCMP crN, lo1, lo2
1010     // The following can be improved, but not that much.
1011     SDOperand Tmp1, Tmp2, Tmp3;
1012     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1013     Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, CCCode);
1014     Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1015     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETNE);
1016     Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, CCCode);
1017     Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1018     NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1019     NewRHS = SDOperand();   // LHS is the result, not a compare.
1020     return;
1021   }
1022   
1023   
1024   if (CCCode == ISD::SETEQ || CCCode == ISD::SETNE) {
1025     if (RHSLo == RHSHi)
1026       if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1027         if (RHSCST->isAllOnesValue()) {
1028           // Equality comparison to -1.
1029           NewLHS = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
1030           NewRHS = RHSLo;
1031           return;
1032         }
1033           
1034     NewLHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1035     NewRHS = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1036     NewLHS = DAG.getNode(ISD::OR, NewLHS.getValueType(), NewLHS, NewRHS);
1037     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1038     return;
1039   }
1040   
1041   // If this is a comparison of the sign bit, just look at the top part.
1042   // X > -1,  x < 0
1043   if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(NewRHS))
1044     if ((CCCode == ISD::SETLT && CST->getValue() == 0) ||   // X < 0
1045         (CCCode == ISD::SETGT && CST->isAllOnesValue())) {  // X > -1
1046       NewLHS = LHSHi;
1047       NewRHS = RHSHi;
1048       return;
1049     }
1050       
1051   // FIXME: This generated code sucks.
1052   ISD::CondCode LowCC;
1053   switch (CCCode) {
1054   default: assert(0 && "Unknown integer setcc!");
1055   case ISD::SETLT:
1056   case ISD::SETULT: LowCC = ISD::SETULT; break;
1057   case ISD::SETGT:
1058   case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1059   case ISD::SETLE:
1060   case ISD::SETULE: LowCC = ISD::SETULE; break;
1061   case ISD::SETGE:
1062   case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1063   }
1064   
1065   // Tmp1 = lo(op1) < lo(op2)   // Always unsigned comparison
1066   // Tmp2 = hi(op1) < hi(op2)   // Signedness depends on operands
1067   // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1068   
1069   // NOTE: on targets without efficient SELECT of bools, we can always use
1070   // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
1071   TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
1072   SDOperand Tmp1, Tmp2;
1073   Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC,
1074                            false, DagCombineInfo);
1075   if (!Tmp1.Val)
1076     Tmp1 = DAG.getSetCC(TLI.getSetCCResultTy(), LHSLo, RHSLo, LowCC);
1077   Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1078                            CCCode, false, DagCombineInfo);
1079   if (!Tmp2.Val)
1080     Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(), LHSHi, RHSHi,
1081                        DAG.getCondCode(CCCode));
1082   
1083   ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.Val);
1084   ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.Val);
1085   if ((Tmp1C && Tmp1C->getValue() == 0) ||
1086       (Tmp2C && Tmp2C->getValue() == 0 &&
1087        (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
1088         CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
1089       (Tmp2C && Tmp2C->getValue() == 1 &&
1090        (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
1091         CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
1092     // low part is known false, returns high part.
1093     // For LE / GE, if high part is known false, ignore the low part.
1094     // For LT / GT, if high part is known true, ignore the low part.
1095     NewLHS = Tmp2;
1096     NewRHS = SDOperand();
1097     return;
1098   }
1099   
1100   NewLHS = TLI.SimplifySetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi,
1101                              ISD::SETEQ, false, DagCombineInfo);
1102   if (!NewLHS.Val)
1103     NewLHS = DAG.getSetCC(TLI.getSetCCResultTy(), LHSHi, RHSHi, ISD::SETEQ);
1104   NewLHS = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1105                        NewLHS, Tmp1, Tmp2);
1106   NewRHS = SDOperand();
1107 }
1108
1109 SDOperand DAGTypeLegalizer::ExpandOperand_STORE(StoreSDNode *N, unsigned OpNo) {
1110   assert(OpNo == 1 && "Can only expand the stored value so far");
1111
1112   MVT::ValueType VT = N->getOperand(1).getValueType();
1113   MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
1114   SDOperand Ch  = N->getChain();
1115   SDOperand Ptr = N->getBasePtr();
1116   int SVOffset = N->getSrcValueOffset();
1117   unsigned Alignment = N->getAlignment();
1118   bool isVolatile = N->isVolatile();
1119   SDOperand Lo, Hi;
1120
1121   assert(!(MVT::getSizeInBits(NVT) & 7) && "Expanded type not byte sized!");
1122
1123   if (!N->isTruncatingStore()) {
1124     unsigned IncrementSize = 0;
1125     GetExpandedOp(N->getValue(), Lo, Hi);
1126     IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
1127
1128     if (TLI.isBigEndian())
1129       std::swap(Lo, Hi);
1130
1131     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(),
1132                       SVOffset, isVolatile, Alignment);
1133
1134     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1135                       DAG.getIntPtrConstant(IncrementSize));
1136     assert(isTypeLegal(Ptr.getValueType()) && "Pointers must be legal!");
1137     Hi = DAG.getStore(Ch, Hi, Ptr, N->getSrcValue(), SVOffset+IncrementSize,
1138                       isVolatile, MinAlign(Alignment, IncrementSize));
1139     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1140   } else if (MVT::getSizeInBits(N->getMemoryVT()) <= MVT::getSizeInBits(NVT)) {
1141     GetExpandedOp(N->getValue(), Lo, Hi);
1142     return DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
1143                              N->getMemoryVT(), isVolatile, Alignment);
1144   } else if (TLI.isLittleEndian()) {
1145     // Little-endian - low bits are at low addresses.
1146     GetExpandedOp(N->getValue(), Lo, Hi);
1147
1148     Lo = DAG.getStore(Ch, Lo, Ptr, N->getSrcValue(), SVOffset,
1149                       isVolatile, Alignment);
1150
1151     unsigned ExcessBits =
1152       MVT::getSizeInBits(N->getMemoryVT()) - MVT::getSizeInBits(NVT);
1153     MVT::ValueType NEVT = MVT::getIntegerType(ExcessBits);
1154
1155     // Increment the pointer to the other half.
1156     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1157     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1158                       DAG.getIntPtrConstant(IncrementSize));
1159     Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
1160                            SVOffset+IncrementSize, NEVT,
1161                            isVolatile, MinAlign(Alignment, IncrementSize));
1162     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1163   } else {
1164     // Big-endian - high bits are at low addresses.  Favor aligned stores at
1165     // the cost of some bit-fiddling.
1166     GetExpandedOp(N->getValue(), Lo, Hi);
1167
1168     MVT::ValueType EVT = N->getMemoryVT();
1169     unsigned EBytes = MVT::getStoreSizeInBits(EVT)/8;
1170     unsigned IncrementSize = MVT::getSizeInBits(NVT)/8;
1171     unsigned ExcessBits = (EBytes - IncrementSize)*8;
1172     MVT::ValueType HiVT =
1173       MVT::getIntegerType(MVT::getSizeInBits(EVT)-ExcessBits);
1174
1175     if (ExcessBits < MVT::getSizeInBits(NVT)) {
1176       // Transfer high bits from the top of Lo to the bottom of Hi.
1177       Hi = DAG.getNode(ISD::SHL, NVT, Hi,
1178                        DAG.getConstant(MVT::getSizeInBits(NVT) - ExcessBits,
1179                                        TLI.getShiftAmountTy()));
1180       Hi = DAG.getNode(ISD::OR, NVT, Hi,
1181                        DAG.getNode(ISD::SRL, NVT, Lo,
1182                                    DAG.getConstant(ExcessBits,
1183                                                    TLI.getShiftAmountTy())));
1184     }
1185
1186     // Store both the high bits and maybe some of the low bits.
1187     Hi = DAG.getTruncStore(Ch, Hi, Ptr, N->getSrcValue(),
1188                            SVOffset, HiVT, isVolatile, Alignment);
1189
1190     // Increment the pointer to the other half.
1191     Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1192                       DAG.getIntPtrConstant(IncrementSize));
1193     // Store the lowest ExcessBits bits in the second half.
1194     Lo = DAG.getTruncStore(Ch, Lo, Ptr, N->getSrcValue(),
1195                            SVOffset+IncrementSize,
1196                            MVT::getIntegerType(ExcessBits),
1197                            isVolatile, MinAlign(Alignment, IncrementSize));
1198     return DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1199   }
1200 }
1201