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