ARM: support legalisation of "fptrunc ... to half" operations.
[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   if (N->getValueType(0) == MVT::f16) {
401     // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
402     // storage-only type get a chance to select things.
403     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
404   }
405
406   RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
407   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
408   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
409 }
410
411 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
412   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
413   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
414                      GetSoftenedFloat(N->getOperand(1)) };
415   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
416                                            RTLIB::POW_F32,
417                                            RTLIB::POW_F64,
418                                            RTLIB::POW_F80,
419                                            RTLIB::POW_F128,
420                                            RTLIB::POW_PPCF128),
421                          NVT, Ops, 2, false, SDLoc(N)).first;
422 }
423
424 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
425   assert(N->getOperand(1).getValueType() == MVT::i32 &&
426          "Unsupported power type!");
427   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
428   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
429   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
430                                            RTLIB::POWI_F32,
431                                            RTLIB::POWI_F64,
432                                            RTLIB::POWI_F80,
433                                            RTLIB::POWI_F128,
434                                            RTLIB::POWI_PPCF128),
435                          NVT, Ops, 2, false, SDLoc(N)).first;
436 }
437
438 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
439   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
440   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
441                      GetSoftenedFloat(N->getOperand(1)) };
442   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
443                                            RTLIB::REM_F32,
444                                            RTLIB::REM_F64,
445                                            RTLIB::REM_F80,
446                                            RTLIB::REM_F128,
447                                            RTLIB::REM_PPCF128),
448                          NVT, Ops, 2, false, SDLoc(N)).first;
449 }
450
451 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
452   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
453   SDValue Op = GetSoftenedFloat(N->getOperand(0));
454   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
455                                            RTLIB::RINT_F32,
456                                            RTLIB::RINT_F64,
457                                            RTLIB::RINT_F80,
458                                            RTLIB::RINT_F128,
459                                            RTLIB::RINT_PPCF128),
460                          NVT, &Op, 1, false, SDLoc(N)).first;
461 }
462
463 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
464   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
465   SDValue Op = GetSoftenedFloat(N->getOperand(0));
466   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
467                                            RTLIB::ROUND_F32,
468                                            RTLIB::ROUND_F64,
469                                            RTLIB::ROUND_F80,
470                                            RTLIB::ROUND_F128,
471                                            RTLIB::ROUND_PPCF128),
472                          NVT, &Op, 1, false, SDLoc(N)).first;
473 }
474
475 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
476   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
477   SDValue Op = GetSoftenedFloat(N->getOperand(0));
478   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
479                                            RTLIB::SIN_F32,
480                                            RTLIB::SIN_F64,
481                                            RTLIB::SIN_F80,
482                                            RTLIB::SIN_F128,
483                                            RTLIB::SIN_PPCF128),
484                          NVT, &Op, 1, false, SDLoc(N)).first;
485 }
486
487 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
488   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
489   SDValue Op = GetSoftenedFloat(N->getOperand(0));
490   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
491                                            RTLIB::SQRT_F32,
492                                            RTLIB::SQRT_F64,
493                                            RTLIB::SQRT_F80,
494                                            RTLIB::SQRT_F128,
495                                            RTLIB::SQRT_PPCF128),
496                          NVT, &Op, 1, false, SDLoc(N)).first;
497 }
498
499 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
500   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
501   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
502                      GetSoftenedFloat(N->getOperand(1)) };
503   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
504                                            RTLIB::SUB_F32,
505                                            RTLIB::SUB_F64,
506                                            RTLIB::SUB_F80,
507                                            RTLIB::SUB_F128,
508                                            RTLIB::SUB_PPCF128),
509                          NVT, Ops, 2, false, SDLoc(N)).first;
510 }
511
512 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
513   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
514   SDValue Op = GetSoftenedFloat(N->getOperand(0));
515   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
516                                            RTLIB::TRUNC_F32,
517                                            RTLIB::TRUNC_F64,
518                                            RTLIB::TRUNC_F80,
519                                            RTLIB::TRUNC_F128,
520                                            RTLIB::TRUNC_PPCF128),
521                          NVT, &Op, 1, false, SDLoc(N)).first;
522 }
523
524 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
525   LoadSDNode *L = cast<LoadSDNode>(N);
526   EVT VT = N->getValueType(0);
527   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
528   SDLoc dl(N);
529
530   SDValue NewL;
531   if (L->getExtensionType() == ISD::NON_EXTLOAD) {
532     NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
533                        NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
534                        L->getPointerInfo(), NVT, L->isVolatile(),
535                        L->isNonTemporal(), false, L->getAlignment(),
536                        L->getTBAAInfo());
537     // Legalized the chain result - switch anything that used the old chain to
538     // use the new one.
539     ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
540     return NewL;
541   }
542
543   // Do a non-extending load followed by FP_EXTEND.
544   NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
545                      L->getMemoryVT(), dl, L->getChain(),
546                      L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
547                      L->getMemoryVT(), L->isVolatile(),
548                      L->isNonTemporal(), false, L->getAlignment(),
549                      L->getTBAAInfo());
550   // Legalized the chain result - switch anything that used the old chain to
551   // use the new one.
552   ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
553   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
554 }
555
556 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
557   SDValue LHS = GetSoftenedFloat(N->getOperand(1));
558   SDValue RHS = GetSoftenedFloat(N->getOperand(2));
559   return DAG.getSelect(SDLoc(N),
560                        LHS.getValueType(), N->getOperand(0), LHS, RHS);
561 }
562
563 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
564   SDValue LHS = GetSoftenedFloat(N->getOperand(2));
565   SDValue RHS = GetSoftenedFloat(N->getOperand(3));
566   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
567                      LHS.getValueType(), N->getOperand(0),
568                      N->getOperand(1), LHS, RHS, N->getOperand(4));
569 }
570
571 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
572   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
573                                                N->getValueType(0)));
574 }
575
576 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
577   SDValue Chain = N->getOperand(0); // Get the chain.
578   SDValue Ptr = N->getOperand(1); // Get the pointer.
579   EVT VT = N->getValueType(0);
580   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
581   SDLoc dl(N);
582
583   SDValue NewVAARG;
584   NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
585                           N->getConstantOperandVal(3));
586
587   // Legalized the chain result - switch anything that used the old chain to
588   // use the new one.
589   ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
590   return NewVAARG;
591 }
592
593 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
594   bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
595   EVT SVT = N->getOperand(0).getValueType();
596   EVT RVT = N->getValueType(0);
597   EVT NVT = EVT();
598   SDLoc dl(N);
599
600   // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
601   // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
602   // match.  Look for an appropriate libcall.
603   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
604   for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
605        t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
606     NVT = (MVT::SimpleValueType)t;
607     // The source needs to big enough to hold the operand.
608     if (NVT.bitsGE(SVT))
609       LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
610   }
611   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
612
613   // Sign/zero extend the argument if the libcall takes a larger type.
614   SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
615                            NVT, N->getOperand(0));
616   return TLI.makeLibCall(DAG, LC,
617                          TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
618                          &Op, 1, false, dl).first;
619 }
620
621
622 //===----------------------------------------------------------------------===//
623 //  Operand Float to Integer Conversion..
624 //===----------------------------------------------------------------------===//
625
626 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
627   DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
628         dbgs() << "\n");
629   SDValue Res = SDValue();
630
631   switch (N->getOpcode()) {
632   default:
633 #ifndef NDEBUG
634     dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
635     N->dump(&DAG); dbgs() << "\n";
636 #endif
637     llvm_unreachable("Do not know how to soften this operator's operand!");
638
639   case ISD::BITCAST:     Res = SoftenFloatOp_BITCAST(N); break;
640   case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
641   case ISD::FP_EXTEND:   Res = SoftenFloatOp_FP_EXTEND(N); break;
642   case ISD::FP_TO_FP16:  // Same as FP_ROUND for softening purposes
643   case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
644   case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
645   case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
646   case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
647   case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
648   case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
649   }
650
651   // If the result is null, the sub-method took care of registering results etc.
652   if (!Res.getNode()) return false;
653
654   // If the result is N, the sub-method updated N in place.  Tell the legalizer
655   // core about this.
656   if (Res.getNode() == N)
657     return true;
658
659   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
660          "Invalid operand expansion");
661
662   ReplaceValueWith(SDValue(N, 0), Res);
663   return false;
664 }
665
666 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
667   return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
668                      GetSoftenedFloat(N->getOperand(0)));
669 }
670
671 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
672   // If we get here, the result must be legal but the source illegal.
673   EVT SVT = N->getOperand(0).getValueType();
674   EVT RVT = N->getValueType(0);
675   SDValue Op = GetSoftenedFloat(N->getOperand(0));
676
677   if (SVT == MVT::f16)
678     return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
679
680   RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
681   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
682
683   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
684 }
685
686
687 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
688   // We actually deal with the partially-softened FP_TO_FP16 node too, which
689   // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
690   assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
691
692   EVT SVT = N->getOperand(0).getValueType();
693   EVT RVT = N->getValueType(0);
694   EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
695
696   RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
697   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
698
699   SDValue Op = GetSoftenedFloat(N->getOperand(0));
700   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
701 }
702
703 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
704   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
705   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
706
707   EVT VT = NewLHS.getValueType();
708   NewLHS = GetSoftenedFloat(NewLHS);
709   NewRHS = GetSoftenedFloat(NewRHS);
710   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
711
712   // If softenSetCCOperands returned a scalar, we need to compare the result
713   // against zero to select between true and false values.
714   if (!NewRHS.getNode()) {
715     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
716     CCCode = ISD::SETNE;
717   }
718
719   // Update N to have the operands specified.
720   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
721                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
722                                 N->getOperand(4)),
723                  0);
724 }
725
726 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
727   EVT RVT = N->getValueType(0);
728   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
729   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
730   SDValue Op = GetSoftenedFloat(N->getOperand(0));
731   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
732 }
733
734 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
735   EVT RVT = N->getValueType(0);
736   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
737   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
738   SDValue Op = GetSoftenedFloat(N->getOperand(0));
739   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
740 }
741
742 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
743   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
744   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
745
746   EVT VT = NewLHS.getValueType();
747   NewLHS = GetSoftenedFloat(NewLHS);
748   NewRHS = GetSoftenedFloat(NewRHS);
749   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
750
751   // If softenSetCCOperands returned a scalar, we need to compare the result
752   // against zero to select between true and false values.
753   if (!NewRHS.getNode()) {
754     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
755     CCCode = ISD::SETNE;
756   }
757
758   // Update N to have the operands specified.
759   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
760                                 N->getOperand(2), N->getOperand(3),
761                                 DAG.getCondCode(CCCode)),
762                  0);
763 }
764
765 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
766   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
767   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
768
769   EVT VT = NewLHS.getValueType();
770   NewLHS = GetSoftenedFloat(NewLHS);
771   NewRHS = GetSoftenedFloat(NewRHS);
772   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
773
774   // If softenSetCCOperands returned a scalar, use it.
775   if (!NewRHS.getNode()) {
776     assert(NewLHS.getValueType() == N->getValueType(0) &&
777            "Unexpected setcc expansion!");
778     return NewLHS;
779   }
780
781   // Otherwise, update N to have the operands specified.
782   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
783                                 DAG.getCondCode(CCCode)),
784                  0);
785 }
786
787 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
788   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
789   assert(OpNo == 1 && "Can only soften the stored value!");
790   StoreSDNode *ST = cast<StoreSDNode>(N);
791   SDValue Val = ST->getValue();
792   SDLoc dl(N);
793
794   if (ST->isTruncatingStore())
795     // Do an FP_ROUND followed by a non-truncating store.
796     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
797                                           Val, DAG.getIntPtrConstant(0)));
798   else
799     Val = GetSoftenedFloat(Val);
800
801   return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
802                       ST->getMemOperand());
803 }
804
805
806 //===----------------------------------------------------------------------===//
807 //  Float Result Expansion
808 //===----------------------------------------------------------------------===//
809
810 /// ExpandFloatResult - This method is called when the specified result of the
811 /// specified node is found to need expansion.  At this point, the node may also
812 /// have invalid operands or may have other results that need promotion, we just
813 /// know that (at least) one result needs expansion.
814 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
815   DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
816   SDValue Lo, Hi;
817   Lo = Hi = SDValue();
818
819   // See if the target wants to custom expand this node.
820   if (CustomLowerNode(N, N->getValueType(ResNo), true))
821     return;
822
823   switch (N->getOpcode()) {
824   default:
825 #ifndef NDEBUG
826     dbgs() << "ExpandFloatResult #" << ResNo << ": ";
827     N->dump(&DAG); dbgs() << "\n";
828 #endif
829     llvm_unreachable("Do not know how to expand the result of this operator!");
830
831   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
832   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
833   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
834
835   case ISD::MERGE_VALUES:       ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
836   case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
837   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
838   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
839   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
840   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
841
842   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
843   case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
844   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
845   case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
846   case ISD::FCOPYSIGN:  ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
847   case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
848   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
849   case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
850   case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
851   case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
852   case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
853   case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
854   case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
855   case ISD::FMA:        ExpandFloatRes_FMA(N, Lo, Hi); break;
856   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
857   case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
858   case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
859   case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
860   case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
861   case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
862   case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
863   case ISD::FROUND:     ExpandFloatRes_FROUND(N, Lo, Hi); break;
864   case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
865   case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
866   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
867   case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
868   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
869   case ISD::SINT_TO_FP:
870   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
871   case ISD::FREM:       ExpandFloatRes_FREM(N, Lo, Hi); break;
872   }
873
874   // If Lo/Hi is null, the sub-method took care of registering results etc.
875   if (Lo.getNode())
876     SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
877 }
878
879 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
880                                                  SDValue &Hi) {
881   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
882   assert(NVT.getSizeInBits() == integerPartWidth &&
883          "Do not know how to expand this float constant!");
884   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
885   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
886                                  APInt(integerPartWidth, C.getRawData()[1])),
887                          NVT);
888   Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
889                                  APInt(integerPartWidth, C.getRawData()[0])),
890                          NVT);
891 }
892
893 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
894                                            SDValue &Hi) {
895   assert(N->getValueType(0) == MVT::ppcf128 &&
896          "Logic only correct for ppcf128!");
897   SDLoc dl(N);
898   SDValue Tmp;
899   GetExpandedFloat(N->getOperand(0), Lo, Tmp);
900   Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
901   // Lo = Hi==fabs(Hi) ? Lo : -Lo;
902   Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
903                    DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
904                    ISD::SETEQ);
905 }
906
907 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
908                                            SDValue &Hi) {
909   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
910                                          RTLIB::ADD_F32, RTLIB::ADD_F64,
911                                          RTLIB::ADD_F80, RTLIB::ADD_F128,
912                                          RTLIB::ADD_PPCF128),
913                             N, false);
914   GetPairElements(Call, Lo, Hi);
915 }
916
917 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
918                                             SDValue &Lo, SDValue &Hi) {
919   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
920                                          RTLIB::CEIL_F32, RTLIB::CEIL_F64,
921                                          RTLIB::CEIL_F80, RTLIB::CEIL_F128,
922                                          RTLIB::CEIL_PPCF128),
923                             N, false);
924   GetPairElements(Call, Lo, Hi);
925 }
926
927 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
928                                                 SDValue &Lo, SDValue &Hi) {
929   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
930                                          RTLIB::COPYSIGN_F32,
931                                          RTLIB::COPYSIGN_F64,
932                                          RTLIB::COPYSIGN_F80,
933                                          RTLIB::COPYSIGN_F128,
934                                          RTLIB::COPYSIGN_PPCF128),
935                             N, false);
936   GetPairElements(Call, Lo, Hi);
937 }
938
939 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
940                                            SDValue &Lo, SDValue &Hi) {
941   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
942                                          RTLIB::COS_F32, RTLIB::COS_F64,
943                                          RTLIB::COS_F80, RTLIB::COS_F128,
944                                          RTLIB::COS_PPCF128),
945                             N, false);
946   GetPairElements(Call, Lo, Hi);
947 }
948
949 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
950                                            SDValue &Hi) {
951   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
952   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
953                                                    RTLIB::DIV_F32,
954                                                    RTLIB::DIV_F64,
955                                                    RTLIB::DIV_F80,
956                                                    RTLIB::DIV_F128,
957                                                    RTLIB::DIV_PPCF128),
958                                  N->getValueType(0), Ops, 2, false,
959                                  SDLoc(N)).first;
960   GetPairElements(Call, Lo, Hi);
961 }
962
963 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
964                                            SDValue &Lo, SDValue &Hi) {
965   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
966                                          RTLIB::EXP_F32, RTLIB::EXP_F64,
967                                          RTLIB::EXP_F80, RTLIB::EXP_F128,
968                                          RTLIB::EXP_PPCF128),
969                             N, false);
970   GetPairElements(Call, Lo, Hi);
971 }
972
973 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
974                                             SDValue &Lo, SDValue &Hi) {
975   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
976                                          RTLIB::EXP2_F32, RTLIB::EXP2_F64,
977                                          RTLIB::EXP2_F80, RTLIB::EXP2_F128,
978                                          RTLIB::EXP2_PPCF128),
979                             N, false);
980   GetPairElements(Call, Lo, Hi);
981 }
982
983 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
984                                              SDValue &Lo, SDValue &Hi) {
985   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
986                                          RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
987                                          RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
988                                          RTLIB::FLOOR_PPCF128),
989                             N, false);
990   GetPairElements(Call, Lo, Hi);
991 }
992
993 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
994                                            SDValue &Lo, SDValue &Hi) {
995   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
996                                          RTLIB::LOG_F32, RTLIB::LOG_F64,
997                                          RTLIB::LOG_F80, RTLIB::LOG_F128,
998                                          RTLIB::LOG_PPCF128),
999                             N, false);
1000   GetPairElements(Call, Lo, Hi);
1001 }
1002
1003 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1004                                             SDValue &Lo, SDValue &Hi) {
1005   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1006                                          RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1007                                          RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1008                                          RTLIB::LOG2_PPCF128),
1009                             N, false);
1010   GetPairElements(Call, Lo, Hi);
1011 }
1012
1013 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1014                                              SDValue &Lo, SDValue &Hi) {
1015   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1016                                          RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1017                                          RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1018                                          RTLIB::LOG10_PPCF128),
1019                             N, false);
1020   GetPairElements(Call, Lo, Hi);
1021 }
1022
1023 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1024                                           SDValue &Hi) {
1025   SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1026   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1027                                                    RTLIB::FMA_F32,
1028                                                    RTLIB::FMA_F64,
1029                                                    RTLIB::FMA_F80,
1030                                                    RTLIB::FMA_F128,
1031                                                    RTLIB::FMA_PPCF128),
1032                                  N->getValueType(0), Ops, 3, false,
1033                                  SDLoc(N)).first;
1034   GetPairElements(Call, Lo, Hi);
1035 }
1036
1037 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1038                                            SDValue &Hi) {
1039   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1040   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1041                                                    RTLIB::MUL_F32,
1042                                                    RTLIB::MUL_F64,
1043                                                    RTLIB::MUL_F80,
1044                                                    RTLIB::MUL_F128,
1045                                                    RTLIB::MUL_PPCF128),
1046                                  N->getValueType(0), Ops, 2, false,
1047                                  SDLoc(N)).first;
1048   GetPairElements(Call, Lo, Hi);
1049 }
1050
1051 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1052                                                  SDValue &Lo, SDValue &Hi) {
1053   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1054                                          RTLIB::NEARBYINT_F32,
1055                                          RTLIB::NEARBYINT_F64,
1056                                          RTLIB::NEARBYINT_F80,
1057                                          RTLIB::NEARBYINT_F128,
1058                                          RTLIB::NEARBYINT_PPCF128),
1059                             N, false);
1060   GetPairElements(Call, Lo, Hi);
1061 }
1062
1063 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1064                                            SDValue &Hi) {
1065   SDLoc dl(N);
1066   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1067   Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1068   Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1069 }
1070
1071 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1072                                                 SDValue &Hi) {
1073   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1074   Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
1075   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1076                                  APInt(NVT.getSizeInBits(), 0)), NVT);
1077 }
1078
1079 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1080                                            SDValue &Lo, SDValue &Hi) {
1081   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1082                                          RTLIB::POW_F32, RTLIB::POW_F64,
1083                                          RTLIB::POW_F80, RTLIB::POW_F128,
1084                                          RTLIB::POW_PPCF128),
1085                             N, false);
1086   GetPairElements(Call, Lo, Hi);
1087 }
1088
1089 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1090                                             SDValue &Lo, SDValue &Hi) {
1091   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1092                                          RTLIB::POWI_F32, RTLIB::POWI_F64,
1093                                          RTLIB::POWI_F80, RTLIB::POWI_F128,
1094                                          RTLIB::POWI_PPCF128),
1095                             N, false);
1096   GetPairElements(Call, Lo, Hi);
1097 }
1098
1099 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1100                                            SDValue &Lo, SDValue &Hi) {
1101   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1102                                          RTLIB::REM_F32, RTLIB::REM_F64,
1103                                          RTLIB::REM_F80, RTLIB::REM_F128,
1104                                          RTLIB::REM_PPCF128),
1105                             N, false);
1106   GetPairElements(Call, Lo, Hi);
1107 }
1108
1109 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1110                                             SDValue &Lo, SDValue &Hi) {
1111   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1112                                          RTLIB::RINT_F32, RTLIB::RINT_F64,
1113                                          RTLIB::RINT_F80, RTLIB::RINT_F128,
1114                                          RTLIB::RINT_PPCF128),
1115                             N, false);
1116   GetPairElements(Call, Lo, Hi);
1117 }
1118
1119 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1120                                              SDValue &Lo, SDValue &Hi) {
1121   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1122                                          RTLIB::ROUND_F32,
1123                                          RTLIB::ROUND_F64,
1124                                          RTLIB::ROUND_F80,
1125                                          RTLIB::ROUND_F128,
1126                                          RTLIB::ROUND_PPCF128),
1127                             N, false);
1128   GetPairElements(Call, Lo, Hi);
1129 }
1130
1131 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1132                                            SDValue &Lo, SDValue &Hi) {
1133   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1134                                          RTLIB::SIN_F32, RTLIB::SIN_F64,
1135                                          RTLIB::SIN_F80, RTLIB::SIN_F128,
1136                                          RTLIB::SIN_PPCF128),
1137                             N, false);
1138   GetPairElements(Call, Lo, Hi);
1139 }
1140
1141 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1142                                             SDValue &Lo, SDValue &Hi) {
1143   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1144                                          RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1145                                          RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1146                                          RTLIB::SQRT_PPCF128),
1147                             N, false);
1148   GetPairElements(Call, Lo, Hi);
1149 }
1150
1151 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1152                                            SDValue &Hi) {
1153   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1154   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1155                                                    RTLIB::SUB_F32,
1156                                                    RTLIB::SUB_F64,
1157                                                    RTLIB::SUB_F80,
1158                                                    RTLIB::SUB_F128,
1159                                                    RTLIB::SUB_PPCF128),
1160                                  N->getValueType(0), Ops, 2, false,
1161                                  SDLoc(N)).first;
1162   GetPairElements(Call, Lo, Hi);
1163 }
1164
1165 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1166                                              SDValue &Lo, SDValue &Hi) {
1167   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1168                                          RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1169                                          RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1170                                          RTLIB::TRUNC_PPCF128),
1171                             N, false);
1172   GetPairElements(Call, Lo, Hi);
1173 }
1174
1175 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1176                                            SDValue &Hi) {
1177   if (ISD::isNormalLoad(N)) {
1178     ExpandRes_NormalLoad(N, Lo, Hi);
1179     return;
1180   }
1181
1182   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1183   LoadSDNode *LD = cast<LoadSDNode>(N);
1184   SDValue Chain = LD->getChain();
1185   SDValue Ptr = LD->getBasePtr();
1186   SDLoc dl(N);
1187
1188   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1189   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1190   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1191
1192   Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1193                       LD->getMemoryVT(), LD->getMemOperand());
1194
1195   // Remember the chain.
1196   Chain = Hi.getValue(1);
1197
1198   // The low part is zero.
1199   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1200                                  APInt(NVT.getSizeInBits(), 0)), NVT);
1201
1202   // Modified the chain - switch anything that used the old chain to use the
1203   // new one.
1204   ReplaceValueWith(SDValue(LD, 1), Chain);
1205 }
1206
1207 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1208                                                  SDValue &Hi) {
1209   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1210   EVT VT = N->getValueType(0);
1211   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1212   SDValue Src = N->getOperand(0);
1213   EVT SrcVT = Src.getValueType();
1214   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1215   SDLoc dl(N);
1216
1217   // First do an SINT_TO_FP, whether the original was signed or unsigned.
1218   // When promoting partial word types to i32 we must honor the signedness,
1219   // though.
1220   if (SrcVT.bitsLE(MVT::i32)) {
1221     // The integer can be represented exactly in an f64.
1222     Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1223                       MVT::i32, Src);
1224     Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1225                                    APInt(NVT.getSizeInBits(), 0)), NVT);
1226     Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1227   } else {
1228     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1229     if (SrcVT.bitsLE(MVT::i64)) {
1230       Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1231                         MVT::i64, Src);
1232       LC = RTLIB::SINTTOFP_I64_PPCF128;
1233     } else if (SrcVT.bitsLE(MVT::i128)) {
1234       Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1235       LC = RTLIB::SINTTOFP_I128_PPCF128;
1236     }
1237     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1238
1239     Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1240     GetPairElements(Hi, Lo, Hi);
1241   }
1242
1243   if (isSigned)
1244     return;
1245
1246   // Unsigned - fix up the SINT_TO_FP value just calculated.
1247   Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1248   SrcVT = Src.getValueType();
1249
1250   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1251   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
1252   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
1253   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1254   ArrayRef<uint64_t> Parts;
1255
1256   switch (SrcVT.getSimpleVT().SimpleTy) {
1257   default:
1258     llvm_unreachable("Unsupported UINT_TO_FP!");
1259   case MVT::i32:
1260     Parts = TwoE32;
1261     break;
1262   case MVT::i64:
1263     Parts = TwoE64;
1264     break;
1265   case MVT::i128:
1266     Parts = TwoE128;
1267     break;
1268   }
1269
1270   Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1271                    DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1272                                              APInt(128, Parts)),
1273                                      MVT::ppcf128));
1274   Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
1275                        Lo, Hi, ISD::SETLT);
1276   GetPairElements(Lo, Lo, Hi);
1277 }
1278
1279
1280 //===----------------------------------------------------------------------===//
1281 //  Float Operand Expansion
1282 //===----------------------------------------------------------------------===//
1283
1284 /// ExpandFloatOperand - This method is called when the specified operand of the
1285 /// specified node is found to need expansion.  At this point, all of the result
1286 /// types of the node are known to be legal, but other operands of the node may
1287 /// need promotion or expansion as well as the specified one.
1288 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1289   DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1290   SDValue Res = SDValue();
1291
1292   // See if the target wants to custom expand this node.
1293   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1294     return false;
1295
1296   switch (N->getOpcode()) {
1297   default:
1298 #ifndef NDEBUG
1299     dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1300     N->dump(&DAG); dbgs() << "\n";
1301 #endif
1302     llvm_unreachable("Do not know how to expand this operator's operand!");
1303
1304   case ISD::BITCAST:         Res = ExpandOp_BITCAST(N); break;
1305   case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
1306   case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1307
1308   case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
1309   case ISD::FCOPYSIGN:  Res = ExpandFloatOp_FCOPYSIGN(N); break;
1310   case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
1311   case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1312   case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1313   case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
1314   case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
1315   case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1316                                                   OpNo); break;
1317   }
1318
1319   // If the result is null, the sub-method took care of registering results etc.
1320   if (!Res.getNode()) return false;
1321
1322   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1323   // core about this.
1324   if (Res.getNode() == N)
1325     return true;
1326
1327   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1328          "Invalid operand expansion");
1329
1330   ReplaceValueWith(SDValue(N, 0), Res);
1331   return false;
1332 }
1333
1334 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
1335 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1336 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1337                                                 SDValue &NewRHS,
1338                                                 ISD::CondCode &CCCode,
1339                                                 SDLoc dl) {
1340   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1341   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1342   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1343
1344   assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1345
1346   // FIXME:  This generated code sucks.  We want to generate
1347   //         FCMPU crN, hi1, hi2
1348   //         BNE crN, L:
1349   //         FCMPU crN, lo1, lo2
1350   // The following can be improved, but not that much.
1351   SDValue Tmp1, Tmp2, Tmp3;
1352   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1353                       LHSHi, RHSHi, ISD::SETOEQ);
1354   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1355                       LHSLo, RHSLo, CCCode);
1356   Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1357   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1358                       LHSHi, RHSHi, ISD::SETUNE);
1359   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1360                       LHSHi, RHSHi, CCCode);
1361   Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1362   NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1363   NewRHS = SDValue();   // LHS is the result, not a compare.
1364 }
1365
1366 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1367   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1368   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1369   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1370
1371   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1372   // against zero to select between true and false values.
1373   if (!NewRHS.getNode()) {
1374     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1375     CCCode = ISD::SETNE;
1376   }
1377
1378   // Update N to have the operands specified.
1379   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1380                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1381                                 N->getOperand(4)), 0);
1382 }
1383
1384 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1385   assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1386          "Logic only correct for ppcf128!");
1387   SDValue Lo, Hi;
1388   GetExpandedFloat(N->getOperand(1), Lo, Hi);
1389   // The ppcf128 value is providing only the sign; take it from the
1390   // higher-order double (which must have the larger magnitude).
1391   return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1392                      N->getValueType(0), N->getOperand(0), Hi);
1393 }
1394
1395 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1396   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1397          "Logic only correct for ppcf128!");
1398   SDValue Lo, Hi;
1399   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1400   // Round it the rest of the way (e.g. to f32) if needed.
1401   return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1402                      N->getValueType(0), Hi, N->getOperand(1));
1403 }
1404
1405 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1406   EVT RVT = N->getValueType(0);
1407   SDLoc dl(N);
1408
1409   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1410   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1411   if (RVT == MVT::i32) {
1412     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1413            "Logic only correct for ppcf128!");
1414     SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1415                               N->getOperand(0), DAG.getValueType(MVT::f64));
1416     Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1417                       DAG.getIntPtrConstant(1));
1418     return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1419   }
1420
1421   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1422   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1423   return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1424 }
1425
1426 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1427   EVT RVT = N->getValueType(0);
1428   SDLoc dl(N);
1429
1430   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1431   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1432   if (RVT == MVT::i32) {
1433     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1434            "Logic only correct for ppcf128!");
1435     const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1436     APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1437     SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1438     //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1439     // FIXME: generated code sucks.
1440     return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1441                            DAG.getNode(ISD::ADD, dl, MVT::i32,
1442                                        DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1443                                                    DAG.getNode(ISD::FSUB, dl,
1444                                                                MVT::ppcf128,
1445                                                                N->getOperand(0),
1446                                                                Tmp)),
1447                                        DAG.getConstant(0x80000000, MVT::i32)),
1448                            DAG.getNode(ISD::FP_TO_SINT, dl,
1449                                        MVT::i32, N->getOperand(0)),
1450                            ISD::SETGE);
1451   }
1452
1453   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1454   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1455   return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1456                          false, dl).first;
1457 }
1458
1459 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1460   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1461   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1462   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1463
1464   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1465   // against zero to select between true and false values.
1466   if (!NewRHS.getNode()) {
1467     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1468     CCCode = ISD::SETNE;
1469   }
1470
1471   // Update N to have the operands specified.
1472   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1473                                 N->getOperand(2), N->getOperand(3),
1474                                 DAG.getCondCode(CCCode)), 0);
1475 }
1476
1477 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1478   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1479   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1480   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1481
1482   // If ExpandSetCCOperands returned a scalar, use it.
1483   if (!NewRHS.getNode()) {
1484     assert(NewLHS.getValueType() == N->getValueType(0) &&
1485            "Unexpected setcc expansion!");
1486     return NewLHS;
1487   }
1488
1489   // Otherwise, update N to have the operands specified.
1490   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1491                                 DAG.getCondCode(CCCode)), 0);
1492 }
1493
1494 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1495   if (ISD::isNormalStore(N))
1496     return ExpandOp_NormalStore(N, OpNo);
1497
1498   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1499   assert(OpNo == 1 && "Can only expand the stored value so far");
1500   StoreSDNode *ST = cast<StoreSDNode>(N);
1501
1502   SDValue Chain = ST->getChain();
1503   SDValue Ptr = ST->getBasePtr();
1504
1505   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1506                                      ST->getValue().getValueType());
1507   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1508   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1509   (void)NVT;
1510
1511   SDValue Lo, Hi;
1512   GetExpandedOp(ST->getValue(), Lo, Hi);
1513
1514   return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1515                            ST->getMemoryVT(), ST->getMemOperand());
1516 }