Propagate debug loc info through prologue/epilogue.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / LegalizeFloatTypes.cpp
1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements float type expansion and softening for LegalizeTypes.
11 // Softening is the act of turning a computation in an illegal floating point
12 // type into a computation in an integer type of the same size; also known as
13 // "soft float".  For example, turning f32 arithmetic into operations using i32.
14 // The resulting integer value is the same as what you would get by performing
15 // the floating point operation and bitcasting the result to the integer type.
16 // Expansion is the act of changing a computation in an illegal type to be a
17 // computation in two identical registers of a smaller type.  For example,
18 // implementing ppcf128 arithmetic in two f64 registers.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "LegalizeTypes.h"
23 using namespace llvm;
24
25 /// GetFPLibCall - Return the right libcall for the given floating point type.
26 static RTLIB::Libcall GetFPLibCall(MVT VT,
27                                    RTLIB::Libcall Call_F32,
28                                    RTLIB::Libcall Call_F64,
29                                    RTLIB::Libcall Call_F80,
30                                    RTLIB::Libcall Call_PPCF128) {
31   return
32     VT == MVT::f32 ? Call_F32 :
33     VT == MVT::f64 ? Call_F64 :
34     VT == MVT::f80 ? Call_F80 :
35     VT == MVT::ppcf128 ? Call_PPCF128 :
36     RTLIB::UNKNOWN_LIBCALL;
37 }
38
39 //===----------------------------------------------------------------------===//
40 //  Result Float to Integer Conversion.
41 //===----------------------------------------------------------------------===//
42
43 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
44   DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
45         cerr << "\n");
46   SDValue R = SDValue();
47
48   switch (N->getOpcode()) {
49   default:
50 #ifndef NDEBUG
51     cerr << "SoftenFloatResult #" << ResNo << ": ";
52     N->dump(&DAG); cerr << "\n";
53 #endif
54     assert(0 && "Do not know how to soften the result of this operator!");
55     abort();
56
57     case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
58     case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
59     case ISD::ConstantFP:
60       R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
61       break;
62     case ISD::FABS:        R = SoftenFloatRes_FABS(N); break;
63     case ISD::FADD:        R = SoftenFloatRes_FADD(N); break;
64     case ISD::FCEIL:       R = SoftenFloatRes_FCEIL(N); break;
65     case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
66     case ISD::FCOS:        R = SoftenFloatRes_FCOS(N); break;
67     case ISD::FDIV:        R = SoftenFloatRes_FDIV(N); break;
68     case ISD::FEXP:        R = SoftenFloatRes_FEXP(N); break;
69     case ISD::FEXP2:       R = SoftenFloatRes_FEXP2(N); break;
70     case ISD::FFLOOR:      R = SoftenFloatRes_FFLOOR(N); break;
71     case ISD::FLOG:        R = SoftenFloatRes_FLOG(N); break;
72     case ISD::FLOG2:       R = SoftenFloatRes_FLOG2(N); break;
73     case ISD::FLOG10:      R = SoftenFloatRes_FLOG10(N); break;
74     case ISD::FMUL:        R = SoftenFloatRes_FMUL(N); break;
75     case ISD::FNEARBYINT:  R = SoftenFloatRes_FNEARBYINT(N); break;
76     case ISD::FNEG:        R = SoftenFloatRes_FNEG(N); break;
77     case ISD::FP_EXTEND:   R = SoftenFloatRes_FP_EXTEND(N); break;
78     case ISD::FP_ROUND:    R = SoftenFloatRes_FP_ROUND(N); break;
79     case ISD::FPOW:        R = SoftenFloatRes_FPOW(N); break;
80     case ISD::FPOWI:       R = SoftenFloatRes_FPOWI(N); break;
81     case ISD::FRINT:       R = SoftenFloatRes_FRINT(N); break;
82     case ISD::FSIN:        R = SoftenFloatRes_FSIN(N); break;
83     case ISD::FSQRT:       R = SoftenFloatRes_FSQRT(N); break;
84     case ISD::FSUB:        R = SoftenFloatRes_FSUB(N); break;
85     case ISD::FTRUNC:      R = SoftenFloatRes_FTRUNC(N); break;
86     case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
87     case ISD::SELECT:      R = SoftenFloatRes_SELECT(N); break;
88     case ISD::SELECT_CC:   R = SoftenFloatRes_SELECT_CC(N); break;
89     case ISD::SINT_TO_FP:
90     case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
91     case ISD::VAARG:       R = SoftenFloatRes_VAARG(N); break;
92   }
93
94   // If R is null, the sub-method took care of registering the result.
95   if (R.getNode())
96     SetSoftenedFloat(SDValue(N, ResNo), R);
97 }
98
99 SDValue DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
100   return BitConvertToInteger(N->getOperand(0));
101 }
102
103 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
104   // Convert the inputs to integers, and build a new pair out of them.
105   return DAG.getNode(ISD::BUILD_PAIR, N->getDebugLoc(),
106                      TLI.getTypeToTransformTo(N->getValueType(0)),
107                      BitConvertToInteger(N->getOperand(0)),
108                      BitConvertToInteger(N->getOperand(1)));
109 }
110
111 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
112   return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
113                          TLI.getTypeToTransformTo(N->getValueType(0)));
114 }
115
116 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
117   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
118   unsigned Size = NVT.getSizeInBits();
119
120   // Mask = ~(1 << (Size-1))
121   SDValue Mask = DAG.getConstant(APInt::getAllOnesValue(Size).clear(Size-1),
122                                  NVT);
123   SDValue Op = GetSoftenedFloat(N->getOperand(0));
124   return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
125 }
126
127 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
128   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
129   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
130                      GetSoftenedFloat(N->getOperand(1)) };
131   return MakeLibCall(GetFPLibCall(N->getValueType(0),
132                                   RTLIB::ADD_F32,
133                                   RTLIB::ADD_F64,
134                                   RTLIB::ADD_F80,
135                                   RTLIB::ADD_PPCF128),
136                      NVT, Ops, 2, false, N->getDebugLoc());
137 }
138
139 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
140   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
141   SDValue Op = GetSoftenedFloat(N->getOperand(0));
142   return MakeLibCall(GetFPLibCall(N->getValueType(0),
143                                   RTLIB::CEIL_F32,
144                                   RTLIB::CEIL_F64,
145                                   RTLIB::CEIL_F80,
146                                   RTLIB::CEIL_PPCF128),
147                      NVT, &Op, 1, false, N->getDebugLoc());
148 }
149
150 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
151   SDValue LHS = GetSoftenedFloat(N->getOperand(0));
152   SDValue RHS = BitConvertToInteger(N->getOperand(1));
153   DebugLoc dl = N->getDebugLoc();
154
155   MVT LVT = LHS.getValueType();
156   MVT RVT = RHS.getValueType();
157
158   unsigned LSize = LVT.getSizeInBits();
159   unsigned RSize = RVT.getSizeInBits();
160
161   // First get the sign bit of second operand.
162   SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
163                                   DAG.getConstant(RSize - 1,
164                                                   TLI.getShiftAmountTy()));
165   SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
166
167   // Shift right or sign-extend it if the two operands have different types.
168   int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
169   if (SizeDiff > 0) {
170     SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
171                           DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
172     SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
173   } else if (SizeDiff < 0) {
174     SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
175     SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
176                           DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
177   }
178
179   // Clear the sign bit of the first operand.
180   SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
181                                DAG.getConstant(LSize - 1,
182                                                TLI.getShiftAmountTy()));
183   Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
184   LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
185
186   // Or the value with the sign bit.
187   return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
188 }
189
190 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
191   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
192   SDValue Op = GetSoftenedFloat(N->getOperand(0));
193   return MakeLibCall(GetFPLibCall(N->getValueType(0),
194                                   RTLIB::COS_F32,
195                                   RTLIB::COS_F64,
196                                   RTLIB::COS_F80,
197                                   RTLIB::COS_PPCF128),
198                      NVT, &Op, 1, false, N->getDebugLoc());
199 }
200
201 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
202   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
203   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
204                      GetSoftenedFloat(N->getOperand(1)) };
205   return MakeLibCall(GetFPLibCall(N->getValueType(0),
206                                   RTLIB::DIV_F32,
207                                   RTLIB::DIV_F64,
208                                   RTLIB::DIV_F80,
209                                   RTLIB::DIV_PPCF128),
210                      NVT, Ops, 2, false, N->getDebugLoc());
211 }
212
213 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
214   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
215   SDValue Op = GetSoftenedFloat(N->getOperand(0));
216   return MakeLibCall(GetFPLibCall(N->getValueType(0),
217                                   RTLIB::EXP_F32,
218                                   RTLIB::EXP_F64,
219                                   RTLIB::EXP_F80,
220                                   RTLIB::EXP_PPCF128),
221                      NVT, &Op, 1, false, N->getDebugLoc());
222 }
223
224 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
225   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
226   SDValue Op = GetSoftenedFloat(N->getOperand(0));
227   return MakeLibCall(GetFPLibCall(N->getValueType(0),
228                                   RTLIB::EXP2_F32,
229                                   RTLIB::EXP2_F64,
230                                   RTLIB::EXP2_F80,
231                                   RTLIB::EXP2_PPCF128),
232                      NVT, &Op, 1, false, N->getDebugLoc());
233 }
234
235 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
236   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
237   SDValue Op = GetSoftenedFloat(N->getOperand(0));
238   return MakeLibCall(GetFPLibCall(N->getValueType(0),
239                                   RTLIB::FLOOR_F32,
240                                   RTLIB::FLOOR_F64,
241                                   RTLIB::FLOOR_F80,
242                                   RTLIB::FLOOR_PPCF128),
243                      NVT, &Op, 1, false, N->getDebugLoc());
244 }
245
246 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
247   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
248   SDValue Op = GetSoftenedFloat(N->getOperand(0));
249   return MakeLibCall(GetFPLibCall(N->getValueType(0),
250                                   RTLIB::LOG_F32,
251                                   RTLIB::LOG_F64,
252                                   RTLIB::LOG_F80,
253                                   RTLIB::LOG_PPCF128),
254                      NVT, &Op, 1, false, N->getDebugLoc());
255 }
256
257 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
258   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
259   SDValue Op = GetSoftenedFloat(N->getOperand(0));
260   return MakeLibCall(GetFPLibCall(N->getValueType(0),
261                                   RTLIB::LOG2_F32,
262                                   RTLIB::LOG2_F64,
263                                   RTLIB::LOG2_F80,
264                                   RTLIB::LOG2_PPCF128),
265                      NVT, &Op, 1, false, N->getDebugLoc());
266 }
267
268 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
269   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
270   SDValue Op = GetSoftenedFloat(N->getOperand(0));
271   return MakeLibCall(GetFPLibCall(N->getValueType(0),
272                                   RTLIB::LOG10_F32,
273                                   RTLIB::LOG10_F64,
274                                   RTLIB::LOG10_F80,
275                                   RTLIB::LOG10_PPCF128),
276                      NVT, &Op, 1, false, N->getDebugLoc());
277 }
278
279 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
280   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
281   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
282                      GetSoftenedFloat(N->getOperand(1)) };
283   return MakeLibCall(GetFPLibCall(N->getValueType(0),
284                                   RTLIB::MUL_F32,
285                                   RTLIB::MUL_F64,
286                                   RTLIB::MUL_F80,
287                                   RTLIB::MUL_PPCF128),
288                      NVT, Ops, 2, false, N->getDebugLoc());
289 }
290
291 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
292   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
293   SDValue Op = GetSoftenedFloat(N->getOperand(0));
294   return MakeLibCall(GetFPLibCall(N->getValueType(0),
295                                   RTLIB::NEARBYINT_F32,
296                                   RTLIB::NEARBYINT_F64,
297                                   RTLIB::NEARBYINT_F80,
298                                   RTLIB::NEARBYINT_PPCF128),
299                      NVT, &Op, 1, false, N->getDebugLoc());
300 }
301
302 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
303   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
304   // Expand Y = FNEG(X) -> Y = SUB -0.0, X
305   SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
306                      GetSoftenedFloat(N->getOperand(0)) };
307   return MakeLibCall(GetFPLibCall(N->getValueType(0),
308                                   RTLIB::SUB_F32,
309                                   RTLIB::SUB_F64,
310                                   RTLIB::SUB_F80,
311                                   RTLIB::SUB_PPCF128),
312                      NVT, Ops, 2, false, N->getDebugLoc());
313 }
314
315 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
316   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
317   SDValue Op = N->getOperand(0);
318   RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
319   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
320   return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
321 }
322
323 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
324   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
325   SDValue Op = N->getOperand(0);
326   RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
327   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
328   return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
329 }
330
331 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
332   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
333   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
334                      GetSoftenedFloat(N->getOperand(1)) };
335   return MakeLibCall(GetFPLibCall(N->getValueType(0),
336                                   RTLIB::POW_F32,
337                                   RTLIB::POW_F64,
338                                   RTLIB::POW_F80,
339                                   RTLIB::POW_PPCF128),
340                      NVT, Ops, 2, false, N->getDebugLoc());
341 }
342
343 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
344   assert(N->getOperand(1).getValueType() == MVT::i32 &&
345          "Unsupported power type!");
346   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
347   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
348   return MakeLibCall(GetFPLibCall(N->getValueType(0),
349                                   RTLIB::POWI_F32,
350                                   RTLIB::POWI_F64,
351                                   RTLIB::POWI_F80,
352                                   RTLIB::POWI_PPCF128),
353                      NVT, Ops, 2, false, N->getDebugLoc());
354 }
355
356 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
357   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
358   SDValue Op = GetSoftenedFloat(N->getOperand(0));
359   return MakeLibCall(GetFPLibCall(N->getValueType(0),
360                                   RTLIB::RINT_F32,
361                                   RTLIB::RINT_F64,
362                                   RTLIB::RINT_F80,
363                                   RTLIB::RINT_PPCF128),
364                      NVT, &Op, 1, false, N->getDebugLoc());
365 }
366
367 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
368   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
369   SDValue Op = GetSoftenedFloat(N->getOperand(0));
370   return MakeLibCall(GetFPLibCall(N->getValueType(0),
371                                   RTLIB::SIN_F32,
372                                   RTLIB::SIN_F64,
373                                   RTLIB::SIN_F80,
374                                   RTLIB::SIN_PPCF128),
375                      NVT, &Op, 1, false, N->getDebugLoc());
376 }
377
378 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
379   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
380   SDValue Op = GetSoftenedFloat(N->getOperand(0));
381   return MakeLibCall(GetFPLibCall(N->getValueType(0),
382                                   RTLIB::SQRT_F32,
383                                   RTLIB::SQRT_F64,
384                                   RTLIB::SQRT_F80,
385                                   RTLIB::SQRT_PPCF128),
386                      NVT, &Op, 1, false, N->getDebugLoc());
387 }
388
389 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
390   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
391   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
392                      GetSoftenedFloat(N->getOperand(1)) };
393   return MakeLibCall(GetFPLibCall(N->getValueType(0),
394                                   RTLIB::SUB_F32,
395                                   RTLIB::SUB_F64,
396                                   RTLIB::SUB_F80,
397                                   RTLIB::SUB_PPCF128),
398                      NVT, Ops, 2, false, N->getDebugLoc());
399 }
400
401 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
402   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
403   SDValue Op = GetSoftenedFloat(N->getOperand(0));
404   return MakeLibCall(GetFPLibCall(N->getValueType(0),
405                                   RTLIB::TRUNC_F32,
406                                   RTLIB::TRUNC_F64,
407                                   RTLIB::TRUNC_F80,
408                                   RTLIB::TRUNC_PPCF128),
409                      NVT, &Op, 1, false, N->getDebugLoc());
410 }
411
412 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
413   LoadSDNode *L = cast<LoadSDNode>(N);
414   MVT VT = N->getValueType(0);
415   MVT NVT = TLI.getTypeToTransformTo(VT);
416   DebugLoc dl = N->getDebugLoc();
417
418   SDValue NewL;
419   if (L->getExtensionType() == ISD::NON_EXTLOAD) {
420     NewL = DAG.getLoad(L->getAddressingMode(), dl, L->getExtensionType(),
421                        NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
422                        L->getSrcValue(), L->getSrcValueOffset(), NVT,
423                        L->isVolatile(), L->getAlignment());
424     // Legalized the chain result - switch anything that used the old chain to
425     // use the new one.
426     ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
427     return NewL;
428   }
429
430   // Do a non-extending load followed by FP_EXTEND.
431   NewL = DAG.getLoad(L->getAddressingMode(), dl, ISD::NON_EXTLOAD,
432                      L->getMemoryVT(), L->getChain(),
433                      L->getBasePtr(), L->getOffset(),
434                      L->getSrcValue(), L->getSrcValueOffset(),
435                      L->getMemoryVT(),
436                      L->isVolatile(), L->getAlignment());
437   // Legalized the chain result - switch anything that used the old chain to
438   // use the new one.
439   ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
440   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
441 }
442
443 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
444   SDValue LHS = GetSoftenedFloat(N->getOperand(1));
445   SDValue RHS = GetSoftenedFloat(N->getOperand(2));
446   return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
447                      LHS.getValueType(), N->getOperand(0),LHS,RHS);
448 }
449
450 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
451   SDValue LHS = GetSoftenedFloat(N->getOperand(2));
452   SDValue RHS = GetSoftenedFloat(N->getOperand(3));
453   return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
454                      LHS.getValueType(), N->getOperand(0),
455                      N->getOperand(1), LHS, RHS, N->getOperand(4));
456 }
457
458 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
459   SDValue Chain = N->getOperand(0); // Get the chain.
460   SDValue Ptr = N->getOperand(1); // Get the pointer.
461   MVT VT = N->getValueType(0);
462   MVT NVT = TLI.getTypeToTransformTo(VT);
463   DebugLoc dl = N->getDebugLoc();
464
465   SDValue NewVAARG;
466   NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2));
467   
468   // Legalized the chain result - switch anything that used the old chain to
469   // use the new one.
470   ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
471   return NewVAARG;
472 }
473
474 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
475   bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
476   MVT SVT = N->getOperand(0).getValueType();
477   MVT RVT = N->getValueType(0);
478   MVT NVT = MVT();
479   DebugLoc dl = N->getDebugLoc();
480
481   // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
482   // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
483   // match.  Look for an appropriate libcall.
484   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
485   for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
486        t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
487     NVT = (MVT::SimpleValueType)t;
488     // The source needs to big enough to hold the operand.
489     if (NVT.bitsGE(SVT))
490       LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
491   }
492   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
493
494   // Sign/zero extend the argument if the libcall takes a larger type.
495   SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
496                            NVT, N->getOperand(0));
497   return MakeLibCall(LC, TLI.getTypeToTransformTo(RVT), &Op, 1, false, dl);
498 }
499
500
501 //===----------------------------------------------------------------------===//
502 //  Operand Float to Integer Conversion..
503 //===----------------------------------------------------------------------===//
504
505 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
506   DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
507         cerr << "\n");
508   SDValue Res = SDValue();
509
510   switch (N->getOpcode()) {
511   default:
512 #ifndef NDEBUG
513     cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
514     N->dump(&DAG); cerr << "\n";
515 #endif
516     assert(0 && "Do not know how to soften this operator's operand!");
517     abort();
518
519   case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
520   case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
521   case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
522   case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
523   case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
524   case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
525   case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
526   case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
527   }
528
529   // If the result is null, the sub-method took care of registering results etc.
530   if (!Res.getNode()) return false;
531
532   // If the result is N, the sub-method updated N in place.  Tell the legalizer
533   // core about this.
534   if (Res.getNode() == N)
535     return true;
536
537   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
538          "Invalid operand expansion");
539
540   ReplaceValueWith(SDValue(N, 0), Res);
541   return false;
542 }
543
544 /// SoftenSetCCOperands - Soften the operands of a comparison.  This code is
545 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
546 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
547                                            ISD::CondCode &CCCode, DebugLoc dl) {
548   SDValue LHSInt = GetSoftenedFloat(NewLHS);
549   SDValue RHSInt = GetSoftenedFloat(NewRHS);
550   MVT VT = NewLHS.getValueType();
551
552   assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
553
554   // Expand into one or more soft-fp libcall(s).
555   RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
556   switch (CCCode) {
557   case ISD::SETEQ:
558   case ISD::SETOEQ:
559     LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
560     break;
561   case ISD::SETNE:
562   case ISD::SETUNE:
563     LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
564     break;
565   case ISD::SETGE:
566   case ISD::SETOGE:
567     LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
568     break;
569   case ISD::SETLT:
570   case ISD::SETOLT:
571     LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
572     break;
573   case ISD::SETLE:
574   case ISD::SETOLE:
575     LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
576     break;
577   case ISD::SETGT:
578   case ISD::SETOGT:
579     LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
580     break;
581   case ISD::SETUO:
582     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
583     break;
584   case ISD::SETO:
585     LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
586     break;
587   default:
588     LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
589     switch (CCCode) {
590     case ISD::SETONE:
591       // SETONE = SETOLT | SETOGT
592       LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
593       // Fallthrough
594     case ISD::SETUGT:
595       LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
596       break;
597     case ISD::SETUGE:
598       LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
599       break;
600     case ISD::SETULT:
601       LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
602       break;
603     case ISD::SETULE:
604       LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
605       break;
606     case ISD::SETUEQ:
607       LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
608       break;
609     default: assert(false && "Do not know how to soften this setcc!");
610     }
611   }
612
613   MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
614   SDValue Ops[2] = { LHSInt, RHSInt };
615   NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
616   NewRHS = DAG.getConstant(0, RetVT);
617   CCCode = TLI.getCmpLibcallCC(LC1);
618   if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
619     SDValue Tmp = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT),
620                                 NewLHS, NewRHS, DAG.getCondCode(CCCode));
621     NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
622     NewLHS = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT), NewLHS,
623                          NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
624     NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
625     NewRHS = SDValue();
626   }
627 }
628
629 SDValue DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
630   return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), N->getValueType(0),
631                      GetSoftenedFloat(N->getOperand(0)));
632 }
633
634 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
635   MVT SVT = N->getOperand(0).getValueType();
636   MVT RVT = N->getValueType(0);
637
638   RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
639   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
640
641   SDValue Op = GetSoftenedFloat(N->getOperand(0));
642   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
643 }
644
645 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
646   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
647   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
648   SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
649
650   // If SoftenSetCCOperands returned a scalar, we need to compare the result
651   // against zero to select between true and false values.
652   if (NewRHS.getNode() == 0) {
653     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
654     CCCode = ISD::SETNE;
655   }
656
657   // Update N to have the operands specified.
658   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
659                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
660                                 N->getOperand(4));
661 }
662
663 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
664   MVT RVT = N->getValueType(0);
665   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
666   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
667   SDValue Op = GetSoftenedFloat(N->getOperand(0));
668   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
669 }
670
671 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
672   MVT RVT = N->getValueType(0);
673   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
674   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
675   SDValue Op = GetSoftenedFloat(N->getOperand(0));
676   return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
677 }
678
679 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
680   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
681   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
682   SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
683
684   // If SoftenSetCCOperands returned a scalar, we need to compare the result
685   // against zero to select between true and false values.
686   if (NewRHS.getNode() == 0) {
687     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
688     CCCode = ISD::SETNE;
689   }
690
691   // Update N to have the operands specified.
692   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
693                                 N->getOperand(2), N->getOperand(3),
694                                 DAG.getCondCode(CCCode));
695 }
696
697 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
698   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
699   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
700   SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
701
702   // If SoftenSetCCOperands returned a scalar, use it.
703   if (NewRHS.getNode() == 0) {
704     assert(NewLHS.getValueType() == N->getValueType(0) &&
705            "Unexpected setcc expansion!");
706     return NewLHS;
707   }
708
709   // Otherwise, update N to have the operands specified.
710   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
711                                 DAG.getCondCode(CCCode));
712 }
713
714 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
715   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
716   assert(OpNo == 1 && "Can only soften the stored value!");
717   StoreSDNode *ST = cast<StoreSDNode>(N);
718   SDValue Val = ST->getValue();
719   DebugLoc dl = N->getDebugLoc();
720
721   if (ST->isTruncatingStore())
722     // Do an FP_ROUND followed by a non-truncating store.
723     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
724                                           Val, DAG.getIntPtrConstant(0)));
725   else
726     Val = GetSoftenedFloat(Val);
727
728   return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
729                       ST->getSrcValue(), ST->getSrcValueOffset(),
730                       ST->isVolatile(), ST->getAlignment());
731 }
732
733
734 //===----------------------------------------------------------------------===//
735 //  Float Result Expansion
736 //===----------------------------------------------------------------------===//
737
738 /// ExpandFloatResult - This method is called when the specified result of the
739 /// specified node is found to need expansion.  At this point, the node may also
740 /// have invalid operands or may have other results that need promotion, we just
741 /// know that (at least) one result needs expansion.
742 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
743   DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
744   SDValue Lo, Hi;
745   Lo = Hi = SDValue();
746
747   // See if the target wants to custom expand this node.
748   if (CustomLowerResults(N, N->getValueType(ResNo), true))
749     return;
750
751   switch (N->getOpcode()) {
752   default:
753 #ifndef NDEBUG
754     cerr << "ExpandFloatResult #" << ResNo << ": ";
755     N->dump(&DAG); cerr << "\n";
756 #endif
757     assert(0 && "Do not know how to expand the result of this operator!");
758     abort();
759
760   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
761   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
762   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
763   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
764
765   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
766   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
767   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
768   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
769   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
770
771   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
772   case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
773   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
774   case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
775   case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
776   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
777   case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
778   case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
779   case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
780   case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
781   case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
782   case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
783   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
784   case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
785   case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
786   case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
787   case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
788   case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
789   case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
790   case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
791   case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
792   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
793   case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
794   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
795   case ISD::SINT_TO_FP:
796   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
797   }
798
799   // If Lo/Hi is null, the sub-method took care of registering results etc.
800   if (Lo.getNode())
801     SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
802 }
803
804 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
805                                                  SDValue &Hi) {
806   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
807   assert(NVT.getSizeInBits() == integerPartWidth &&
808          "Do not know how to expand this float constant!");
809   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
810   Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
811                                        &C.getRawData()[1])), NVT);
812   Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
813                                        &C.getRawData()[0])), NVT);
814 }
815
816 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
817                                            SDValue &Hi) {
818   assert(N->getValueType(0) == MVT::ppcf128 &&
819          "Logic only correct for ppcf128!");
820   DebugLoc dl = N->getDebugLoc();
821   SDValue Tmp;
822   GetExpandedFloat(N->getOperand(0), Lo, Tmp);
823   Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
824   // Lo = Hi==fabs(Hi) ? Lo : -Lo;
825   Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
826                    DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
827                    DAG.getCondCode(ISD::SETEQ));
828 }
829
830 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
831                                            SDValue &Hi) {
832   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
833                                          RTLIB::ADD_F32, RTLIB::ADD_F64,
834                                          RTLIB::ADD_F80, RTLIB::ADD_PPCF128),
835                             N, false);
836   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
837          "Call lowered wrongly!");
838   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
839 }
840
841 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
842                                             SDValue &Lo, SDValue &Hi) {
843   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
844                                          RTLIB::CEIL_F32, RTLIB::CEIL_F64,
845                                          RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128),
846                             N, false);
847   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
848          "Call lowered wrongly!");
849   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
850 }
851
852 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
853                                            SDValue &Lo, SDValue &Hi) {
854   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
855                                          RTLIB::COS_F32, RTLIB::COS_F64,
856                                          RTLIB::COS_F80, RTLIB::COS_PPCF128),
857                             N, false);
858   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
859          "Call lowered wrongly!");
860   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
861 }
862
863 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
864                                            SDValue &Hi) {
865   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
866   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
867                                           RTLIB::DIV_F32,
868                                           RTLIB::DIV_F64,
869                                           RTLIB::DIV_F80,
870                                           RTLIB::DIV_PPCF128),
871                              N->getValueType(0), Ops, 2, false,
872                              N->getDebugLoc());
873   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
874          "Call lowered wrongly!");
875   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
876 }
877
878 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
879                                            SDValue &Lo, SDValue &Hi) {
880   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
881                                          RTLIB::EXP_F32, RTLIB::EXP_F64,
882                                          RTLIB::EXP_F80, RTLIB::EXP_PPCF128),
883                             N, false);
884   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
885          "Call lowered wrongly!");
886   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
887 }
888
889 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
890                                             SDValue &Lo, SDValue &Hi) {
891   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
892                                          RTLIB::EXP2_F32, RTLIB::EXP2_F64,
893                                          RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128),
894                             N, false);
895   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
896          "Call lowered wrongly!");
897   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
898 }
899
900 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
901                                              SDValue &Lo, SDValue &Hi) {
902   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
903                                          RTLIB::FLOOR_F32,RTLIB::FLOOR_F64,
904                                          RTLIB::FLOOR_F80,RTLIB::FLOOR_PPCF128),
905                             N, false);
906   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
907          "Call lowered wrongly!");
908   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
909 }
910
911 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
912                                            SDValue &Lo, SDValue &Hi) {
913   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
914                                          RTLIB::LOG_F32, RTLIB::LOG_F64,
915                                          RTLIB::LOG_F80, RTLIB::LOG_PPCF128),
916                             N, false);
917   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
918          "Call lowered wrongly!");
919   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
920 }
921
922 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
923                                             SDValue &Lo, SDValue &Hi) {
924   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
925                                          RTLIB::LOG2_F32, RTLIB::LOG2_F64,
926                                          RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128),
927                             N, false);
928   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
929          "Call lowered wrongly!");
930   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
931 }
932
933 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
934                                              SDValue &Lo, SDValue &Hi) {
935   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
936                                          RTLIB::LOG10_F32,RTLIB::LOG10_F64,
937                                          RTLIB::LOG10_F80,RTLIB::LOG10_PPCF128),
938                             N, false);
939   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
940          "Call lowered wrongly!");
941   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
942 }
943
944 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
945                                            SDValue &Hi) {
946   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
947   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
948                                           RTLIB::MUL_F32,
949                                           RTLIB::MUL_F64,
950                                           RTLIB::MUL_F80,
951                                           RTLIB::MUL_PPCF128),
952                              N->getValueType(0), Ops, 2, false,
953                              N->getDebugLoc());
954   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
955          "Call lowered wrongly!");
956   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
957 }
958
959 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
960                                                  SDValue &Lo, SDValue &Hi) {
961   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
962                                          RTLIB::NEARBYINT_F32,
963                                          RTLIB::NEARBYINT_F64,
964                                          RTLIB::NEARBYINT_F80,
965                                          RTLIB::NEARBYINT_PPCF128),
966                             N, false);
967   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
968          "Call lowered wrongly!");
969   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
970 }
971
972 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
973                                            SDValue &Hi) {
974   DebugLoc dl = N->getDebugLoc();
975   GetExpandedFloat(N->getOperand(0), Lo, Hi);
976   Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
977   Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
978 }
979
980 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
981                                                 SDValue &Hi) {
982   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
983   Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
984   Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
985 }
986
987 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
988                                            SDValue &Lo, SDValue &Hi) {
989   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
990                                          RTLIB::POW_F32, RTLIB::POW_F64,
991                                          RTLIB::POW_F80, RTLIB::POW_PPCF128),
992                             N, false);
993   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
994          "Call lowered wrongly!");
995   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
996 }
997
998 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
999                                             SDValue &Lo, SDValue &Hi) {
1000   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1001                                          RTLIB::POWI_F32, RTLIB::POWI_F64,
1002                                          RTLIB::POWI_F80, RTLIB::POWI_PPCF128),
1003                             N, false);
1004   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1005          "Call lowered wrongly!");
1006   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1007 }
1008
1009 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1010                                             SDValue &Lo, SDValue &Hi) {
1011   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1012                                          RTLIB::RINT_F32, RTLIB::RINT_F64,
1013                                          RTLIB::RINT_F80, RTLIB::RINT_PPCF128),
1014                             N, false);
1015   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1016          "Call lowered wrongly!");
1017   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1018 }
1019
1020 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1021                                            SDValue &Lo, SDValue &Hi) {
1022   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1023                                          RTLIB::SIN_F32, RTLIB::SIN_F64,
1024                                          RTLIB::SIN_F80, RTLIB::SIN_PPCF128),
1025                             N, false);
1026   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1027          "Call lowered wrongly!");
1028   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1029 }
1030
1031 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1032                                             SDValue &Lo, SDValue &Hi) {
1033   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1034                                          RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1035                                          RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128),
1036                             N, false);
1037   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1038          "Call lowered wrongly!");
1039   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1040 }
1041
1042 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1043                                            SDValue &Hi) {
1044   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1045   SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
1046                                           RTLIB::SUB_F32,
1047                                           RTLIB::SUB_F64,
1048                                           RTLIB::SUB_F80,
1049                                           RTLIB::SUB_PPCF128),
1050                              N->getValueType(0), Ops, 2, false,
1051                              N->getDebugLoc());
1052   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1053          "Call lowered wrongly!");
1054   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1055 }
1056
1057 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1058                                              SDValue &Lo, SDValue &Hi) {
1059   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1060                                          RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1061                                          RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128),
1062                             N, false);
1063   assert(Call.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1064          "Call lowered wrongly!");
1065   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
1066 }
1067
1068 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1069                                            SDValue &Hi) {
1070   if (ISD::isNormalLoad(N)) {
1071     ExpandRes_NormalLoad(N, Lo, Hi);
1072     return;
1073   }
1074
1075   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1076   LoadSDNode *LD = cast<LoadSDNode>(N);
1077   SDValue Chain = LD->getChain();
1078   SDValue Ptr = LD->getBasePtr();
1079   DebugLoc dl = N->getDebugLoc();
1080
1081   MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
1082   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1083   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1084
1085   Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1086                       LD->getSrcValue(), LD->getSrcValueOffset(),
1087                       LD->getMemoryVT(),
1088                       LD->isVolatile(), LD->getAlignment());
1089
1090   // Remember the chain.
1091   Chain = Hi.getValue(1);
1092
1093   // The low part is zero.
1094   Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1095
1096   // Modified the chain - switch anything that used the old chain to use the
1097   // new one.
1098   ReplaceValueWith(SDValue(LD, 1), Chain);
1099 }
1100
1101 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1102                                                  SDValue &Hi) {
1103   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1104   MVT VT = N->getValueType(0);
1105   MVT NVT = TLI.getTypeToTransformTo(VT);
1106   SDValue Src = N->getOperand(0);
1107   MVT SrcVT = Src.getValueType();
1108   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1109   DebugLoc dl = N->getDebugLoc();
1110
1111   // First do an SINT_TO_FP, whether the original was signed or unsigned.
1112   // When promoting partial word types to i32 we must honor the signedness,
1113   // though.
1114   if (SrcVT.bitsLE(MVT::i32)) {
1115     // The integer can be represented exactly in an f64.
1116     Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1117                       MVT::i32, Src);
1118     Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1119     Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1120   } else {
1121     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1122     if (SrcVT.bitsLE(MVT::i64)) {
1123       Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1124                         MVT::i64, Src);
1125       LC = RTLIB::SINTTOFP_I64_PPCF128;
1126     } else if (SrcVT.bitsLE(MVT::i128)) {
1127       Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1128       LC = RTLIB::SINTTOFP_I128_PPCF128;
1129     }
1130     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1131
1132     Hi = MakeLibCall(LC, VT, &Src, 1, true, dl);
1133     assert(Hi.getNode()->getOpcode() == ISD::BUILD_PAIR &&
1134            "Call lowered wrongly!");
1135     Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
1136   }
1137
1138   if (isSigned)
1139     return;
1140
1141   // Unsigned - fix up the SINT_TO_FP value just calculated.
1142   Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1143   SrcVT = Src.getValueType();
1144
1145   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1146   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
1147   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
1148   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1149   const uint64_t *Parts = 0;
1150
1151   switch (SrcVT.getSimpleVT()) {
1152   default:
1153     assert(false && "Unsupported UINT_TO_FP!");
1154   case MVT::i32:
1155     Parts = TwoE32;
1156     break;
1157   case MVT::i64:
1158     Parts = TwoE64;
1159     break;
1160   case MVT::i128:
1161     Parts = TwoE128;
1162     break;
1163   }
1164
1165   Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1166                    DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
1167                                      MVT::ppcf128));
1168   Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
1169                    Lo, Hi, DAG.getCondCode(ISD::SETLT));
1170   Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Lo, DAG.getIntPtrConstant(1));
1171   Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, NVT, Lo, DAG.getIntPtrConstant(0));
1172 }
1173
1174
1175 //===----------------------------------------------------------------------===//
1176 //  Float Operand Expansion
1177 //===----------------------------------------------------------------------===//
1178
1179 /// ExpandFloatOperand - This method is called when the specified operand of the
1180 /// specified node is found to need expansion.  At this point, all of the result
1181 /// types of the node are known to be legal, but other operands of the node may
1182 /// need promotion or expansion as well as the specified one.
1183 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1184   DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
1185   SDValue Res = SDValue();
1186
1187   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1188       == TargetLowering::Custom)
1189     Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1190
1191   if (Res.getNode() == 0) {
1192     switch (N->getOpcode()) {
1193     default:
1194   #ifndef NDEBUG
1195       cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
1196       N->dump(&DAG); cerr << "\n";
1197   #endif
1198       assert(0 && "Do not know how to expand this operator's operand!");
1199       abort();
1200
1201     case ISD::BIT_CONVERT:     Res = ExpandOp_BIT_CONVERT(N); break;
1202     case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
1203     case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1204
1205     case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
1206     case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
1207     case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1208     case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1209     case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
1210     case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
1211     case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1212                                                     OpNo); break;
1213     }
1214   }
1215
1216   // If the result is null, the sub-method took care of registering results etc.
1217   if (!Res.getNode()) return false;
1218
1219   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1220   // core about this.
1221   if (Res.getNode() == N)
1222     return true;
1223
1224   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1225          "Invalid operand expansion");
1226
1227   ReplaceValueWith(SDValue(N, 0), Res);
1228   return false;
1229 }
1230
1231 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
1232 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1233 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1234                                                 SDValue &NewRHS,
1235                                                 ISD::CondCode &CCCode,
1236                                                 DebugLoc dl) {
1237   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1238   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1239   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1240
1241   MVT VT = NewLHS.getValueType();
1242   assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1243
1244   // FIXME:  This generated code sucks.  We want to generate
1245   //         FCMPU crN, hi1, hi2
1246   //         BNE crN, L:
1247   //         FCMPU crN, lo1, lo2
1248   // The following can be improved, but not that much.
1249   SDValue Tmp1, Tmp2, Tmp3;
1250   Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1251                       LHSHi, RHSHi, ISD::SETOEQ);
1252   Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
1253                       LHSLo, RHSLo, CCCode);
1254   Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1255   Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1256                       LHSHi, RHSHi, ISD::SETUNE);
1257   Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1258                       LHSHi, RHSHi, CCCode);
1259   Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1260   NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1261   NewRHS = SDValue();   // LHS is the result, not a compare.
1262 }
1263
1264 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1265   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1266   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1267   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1268
1269   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1270   // against zero to select between true and false values.
1271   if (NewRHS.getNode() == 0) {
1272     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1273     CCCode = ISD::SETNE;
1274   }
1275
1276   // Update N to have the operands specified.
1277   return DAG.UpdateNodeOperands(SDValue(N, 0), N->getOperand(0),
1278                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1279                                 N->getOperand(4));
1280 }
1281
1282 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1283   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1284          "Logic only correct for ppcf128!");
1285   SDValue Lo, Hi;
1286   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1287   // Round it the rest of the way (e.g. to f32) if needed.
1288   return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1289                      N->getValueType(0), Hi, N->getOperand(1));
1290 }
1291
1292 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1293   MVT RVT = N->getValueType(0);
1294   DebugLoc dl = N->getDebugLoc();
1295
1296   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1297   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1298   if (RVT == MVT::i32) {
1299     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1300            "Logic only correct for ppcf128!");
1301     SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1302                               N->getOperand(0), DAG.getValueType(MVT::f64));
1303     Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1304                       DAG.getIntPtrConstant(1));
1305     return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1306   }
1307
1308   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1309   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1310   return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false, dl);
1311 }
1312
1313 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1314   MVT RVT = N->getValueType(0);
1315   DebugLoc dl = N->getDebugLoc();
1316
1317   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1318   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1319   if (RVT == MVT::i32) {
1320     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1321            "Logic only correct for ppcf128!");
1322     const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1323     APFloat APF = APFloat(APInt(128, 2, TwoE31));
1324     SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1325     //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1326     // FIXME: generated code sucks.
1327     return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1328                        DAG.getNode(ISD::ADD, dl, MVT::i32,
1329                                    DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1330                                                DAG.getNode(ISD::FSUB, dl,
1331                                                            MVT::ppcf128,
1332                                                            N->getOperand(0),
1333                                                            Tmp)),
1334                                    DAG.getConstant(0x80000000, MVT::i32)),
1335                        DAG.getNode(ISD::FP_TO_SINT, dl,
1336                                    MVT::i32, N->getOperand(0)),
1337                        DAG.getCondCode(ISD::SETGE));
1338   }
1339
1340   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1341   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1342   return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false, dl);
1343 }
1344
1345 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1346   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1347   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1348   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1349
1350   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1351   // against zero to select between true and false values.
1352   if (NewRHS.getNode() == 0) {
1353     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1354     CCCode = ISD::SETNE;
1355   }
1356
1357   // Update N to have the operands specified.
1358   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
1359                                 N->getOperand(2), N->getOperand(3),
1360                                 DAG.getCondCode(CCCode));
1361 }
1362
1363 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1364   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1365   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1366   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1367
1368   // If ExpandSetCCOperands returned a scalar, use it.
1369   if (NewRHS.getNode() == 0) {
1370     assert(NewLHS.getValueType() == N->getValueType(0) &&
1371            "Unexpected setcc expansion!");
1372     return NewLHS;
1373   }
1374
1375   // Otherwise, update N to have the operands specified.
1376   return DAG.UpdateNodeOperands(SDValue(N, 0), NewLHS, NewRHS,
1377                                 DAG.getCondCode(CCCode));
1378 }
1379
1380 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1381   if (ISD::isNormalStore(N))
1382     return ExpandOp_NormalStore(N, OpNo);
1383
1384   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1385   assert(OpNo == 1 && "Can only expand the stored value so far");
1386   StoreSDNode *ST = cast<StoreSDNode>(N);
1387
1388   SDValue Chain = ST->getChain();
1389   SDValue Ptr = ST->getBasePtr();
1390
1391   MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
1392   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1393   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1394
1395   SDValue Lo, Hi;
1396   GetExpandedOp(ST->getValue(), Lo, Hi);
1397
1398   return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
1399                            ST->getSrcValue(), ST->getSrcValueOffset(),
1400                            ST->getMemoryVT(),
1401                            ST->isVolatile(), ST->getAlignment());
1402 }