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