e8770bb23bda25d5c06f0d389f47303fd707fc19
[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::FMINNUM:     R = SoftenFloatRes_FMINNUM(N); break;
72     case ISD::FMAXNUM:     R = SoftenFloatRes_FMAXNUM(N); break;
73     case ISD::FADD:        R = SoftenFloatRes_FADD(N); break;
74     case ISD::FCEIL:       R = SoftenFloatRes_FCEIL(N); break;
75     case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
76     case ISD::FCOS:        R = SoftenFloatRes_FCOS(N); break;
77     case ISD::FDIV:        R = SoftenFloatRes_FDIV(N); break;
78     case ISD::FEXP:        R = SoftenFloatRes_FEXP(N); break;
79     case ISD::FEXP2:       R = SoftenFloatRes_FEXP2(N); break;
80     case ISD::FFLOOR:      R = SoftenFloatRes_FFLOOR(N); break;
81     case ISD::FLOG:        R = SoftenFloatRes_FLOG(N); break;
82     case ISD::FLOG2:       R = SoftenFloatRes_FLOG2(N); break;
83     case ISD::FLOG10:      R = SoftenFloatRes_FLOG10(N); break;
84     case ISD::FMA:         R = SoftenFloatRes_FMA(N); break;
85     case ISD::FMUL:        R = SoftenFloatRes_FMUL(N); break;
86     case ISD::FNEARBYINT:  R = SoftenFloatRes_FNEARBYINT(N); break;
87     case ISD::FNEG:        R = SoftenFloatRes_FNEG(N); break;
88     case ISD::FP_EXTEND:   R = SoftenFloatRes_FP_EXTEND(N); break;
89     case ISD::FP_ROUND:    R = SoftenFloatRes_FP_ROUND(N); break;
90     case ISD::FP16_TO_FP:  R = SoftenFloatRes_FP16_TO_FP(N); break;
91     case ISD::FPOW:        R = SoftenFloatRes_FPOW(N); break;
92     case ISD::FPOWI:       R = SoftenFloatRes_FPOWI(N); break;
93     case ISD::FREM:        R = SoftenFloatRes_FREM(N); break;
94     case ISD::FRINT:       R = SoftenFloatRes_FRINT(N); break;
95     case ISD::FROUND:      R = SoftenFloatRes_FROUND(N); break;
96     case ISD::FSIN:        R = SoftenFloatRes_FSIN(N); break;
97     case ISD::FSQRT:       R = SoftenFloatRes_FSQRT(N); break;
98     case ISD::FSUB:        R = SoftenFloatRes_FSUB(N); break;
99     case ISD::FTRUNC:      R = SoftenFloatRes_FTRUNC(N); break;
100     case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
101     case ISD::SELECT:      R = SoftenFloatRes_SELECT(N); break;
102     case ISD::SELECT_CC:   R = SoftenFloatRes_SELECT_CC(N); break;
103     case ISD::SINT_TO_FP:
104     case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
105     case ISD::UNDEF:       R = SoftenFloatRes_UNDEF(N); break;
106     case ISD::VAARG:       R = SoftenFloatRes_VAARG(N); break;
107   }
108
109   // If R is null, the sub-method took care of registering the result.
110   if (R.getNode())
111     SetSoftenedFloat(SDValue(N, ResNo), R);
112 }
113
114 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
115   return BitConvertToInteger(N->getOperand(0));
116 }
117
118 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
119                                                       unsigned ResNo) {
120   SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
121   return BitConvertToInteger(Op);
122 }
123
124 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
125   // Convert the inputs to integers, and build a new pair out of them.
126   return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
127                      TLI.getTypeToTransformTo(*DAG.getContext(),
128                                               N->getValueType(0)),
129                      BitConvertToInteger(N->getOperand(0)),
130                      BitConvertToInteger(N->getOperand(1)));
131 }
132
133 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
134   return DAG.getConstant(N->getValueAPF().bitcastToAPInt(), SDLoc(N),
135                          TLI.getTypeToTransformTo(*DAG.getContext(),
136                                                   N->getValueType(0)));
137 }
138
139 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
140   SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
141   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
142                      NewOp.getValueType().getVectorElementType(),
143                      NewOp, N->getOperand(1));
144 }
145
146 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
147   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
148   unsigned Size = NVT.getSizeInBits();
149
150   // Mask = ~(1 << (Size-1))
151   APInt API = APInt::getAllOnesValue(Size);
152   API.clearBit(Size - 1);
153   SDValue Mask = DAG.getConstant(API, SDLoc(N), NVT);
154   SDValue Op = GetSoftenedFloat(N->getOperand(0));
155   return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
156 }
157
158 SDValue DAGTypeLegalizer::SoftenFloatRes_FMINNUM(SDNode *N) {
159   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
160   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
161                      GetSoftenedFloat(N->getOperand(1)) };
162   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
163                                            RTLIB::FMIN_F32,
164                                            RTLIB::FMIN_F64,
165                                            RTLIB::FMIN_F80,
166                                            RTLIB::FMIN_F128,
167                                            RTLIB::FMIN_PPCF128),
168                          NVT, Ops, 2, false, SDLoc(N)).first;
169 }
170
171 SDValue DAGTypeLegalizer::SoftenFloatRes_FMAXNUM(SDNode *N) {
172   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
173   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
174                      GetSoftenedFloat(N->getOperand(1)) };
175   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
176                                            RTLIB::FMAX_F32,
177                                            RTLIB::FMAX_F64,
178                                            RTLIB::FMAX_F80,
179                                            RTLIB::FMAX_F128,
180                                            RTLIB::FMAX_PPCF128),
181                          NVT, Ops, 2, false, SDLoc(N)).first;
182 }
183
184 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
185   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
186   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
187                      GetSoftenedFloat(N->getOperand(1)) };
188   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
189                                            RTLIB::ADD_F32,
190                                            RTLIB::ADD_F64,
191                                            RTLIB::ADD_F80,
192                                            RTLIB::ADD_F128,
193                                            RTLIB::ADD_PPCF128),
194                          NVT, Ops, 2, false, SDLoc(N)).first;
195 }
196
197 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
198   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
199   SDValue Op = GetSoftenedFloat(N->getOperand(0));
200   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
201                                            RTLIB::CEIL_F32,
202                                            RTLIB::CEIL_F64,
203                                            RTLIB::CEIL_F80,
204                                            RTLIB::CEIL_F128,
205                                            RTLIB::CEIL_PPCF128),
206                          NVT, &Op, 1, false, SDLoc(N)).first;
207 }
208
209 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
210   SDValue LHS = GetSoftenedFloat(N->getOperand(0));
211   SDValue RHS = BitConvertToInteger(N->getOperand(1));
212   SDLoc dl(N);
213
214   EVT LVT = LHS.getValueType();
215   EVT RVT = RHS.getValueType();
216
217   unsigned LSize = LVT.getSizeInBits();
218   unsigned RSize = RVT.getSizeInBits();
219
220   // First get the sign bit of second operand.
221   SDValue SignBit = DAG.getNode(
222       ISD::SHL, dl, RVT, DAG.getConstant(1, dl, RVT),
223       DAG.getConstant(RSize - 1, dl,
224                       TLI.getShiftAmountTy(RVT, DAG.getDataLayout())));
225   SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
226
227   // Shift right or sign-extend it if the two operands have different types.
228   int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
229   if (SizeDiff > 0) {
230     SignBit =
231         DAG.getNode(ISD::SRL, dl, RVT, SignBit,
232                     DAG.getConstant(SizeDiff, dl,
233                                     TLI.getShiftAmountTy(SignBit.getValueType(),
234                                                          DAG.getDataLayout())));
235     SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
236   } else if (SizeDiff < 0) {
237     SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
238     SignBit =
239         DAG.getNode(ISD::SHL, dl, LVT, SignBit,
240                     DAG.getConstant(-SizeDiff, dl,
241                                     TLI.getShiftAmountTy(SignBit.getValueType(),
242                                                          DAG.getDataLayout())));
243   }
244
245   // Clear the sign bit of the first operand.
246   SDValue Mask = DAG.getNode(
247       ISD::SHL, dl, LVT, DAG.getConstant(1, dl, LVT),
248       DAG.getConstant(LSize - 1, dl,
249                       TLI.getShiftAmountTy(LVT, DAG.getDataLayout())));
250   Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, dl, LVT));
251   LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
252
253   // Or the value with the sign bit.
254   return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
255 }
256
257 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
258   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
259   SDValue Op = GetSoftenedFloat(N->getOperand(0));
260   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
261                                            RTLIB::COS_F32,
262                                            RTLIB::COS_F64,
263                                            RTLIB::COS_F80,
264                                            RTLIB::COS_F128,
265                                            RTLIB::COS_PPCF128),
266                          NVT, &Op, 1, false, SDLoc(N)).first;
267 }
268
269 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
270   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
271   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
272                      GetSoftenedFloat(N->getOperand(1)) };
273   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
274                                            RTLIB::DIV_F32,
275                                            RTLIB::DIV_F64,
276                                            RTLIB::DIV_F80,
277                                            RTLIB::DIV_F128,
278                                            RTLIB::DIV_PPCF128),
279                          NVT, Ops, 2, false, SDLoc(N)).first;
280 }
281
282 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
283   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
284   SDValue Op = GetSoftenedFloat(N->getOperand(0));
285   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
286                                            RTLIB::EXP_F32,
287                                            RTLIB::EXP_F64,
288                                            RTLIB::EXP_F80,
289                                            RTLIB::EXP_F128,
290                                            RTLIB::EXP_PPCF128),
291                          NVT, &Op, 1, false, SDLoc(N)).first;
292 }
293
294 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
295   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
296   SDValue Op = GetSoftenedFloat(N->getOperand(0));
297   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
298                                            RTLIB::EXP2_F32,
299                                            RTLIB::EXP2_F64,
300                                            RTLIB::EXP2_F80,
301                                            RTLIB::EXP2_F128,
302                                            RTLIB::EXP2_PPCF128),
303                          NVT, &Op, 1, false, SDLoc(N)).first;
304 }
305
306 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
307   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
308   SDValue Op = GetSoftenedFloat(N->getOperand(0));
309   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
310                                            RTLIB::FLOOR_F32,
311                                            RTLIB::FLOOR_F64,
312                                            RTLIB::FLOOR_F80,
313                                            RTLIB::FLOOR_F128,
314                                            RTLIB::FLOOR_PPCF128),
315                          NVT, &Op, 1, false, SDLoc(N)).first;
316 }
317
318 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
319   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
320   SDValue Op = GetSoftenedFloat(N->getOperand(0));
321   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
322                                            RTLIB::LOG_F32,
323                                            RTLIB::LOG_F64,
324                                            RTLIB::LOG_F80,
325                                            RTLIB::LOG_F128,
326                                            RTLIB::LOG_PPCF128),
327                          NVT, &Op, 1, false, SDLoc(N)).first;
328 }
329
330 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
331   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
332   SDValue Op = GetSoftenedFloat(N->getOperand(0));
333   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
334                                            RTLIB::LOG2_F32,
335                                            RTLIB::LOG2_F64,
336                                            RTLIB::LOG2_F80,
337                                            RTLIB::LOG2_F128,
338                                            RTLIB::LOG2_PPCF128),
339                          NVT, &Op, 1, false, SDLoc(N)).first;
340 }
341
342 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
343   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
344   SDValue Op = GetSoftenedFloat(N->getOperand(0));
345   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
346                                            RTLIB::LOG10_F32,
347                                            RTLIB::LOG10_F64,
348                                            RTLIB::LOG10_F80,
349                                            RTLIB::LOG10_F128,
350                                            RTLIB::LOG10_PPCF128),
351                          NVT, &Op, 1, false, SDLoc(N)).first;
352 }
353
354 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
355   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
356   SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
357                      GetSoftenedFloat(N->getOperand(1)),
358                      GetSoftenedFloat(N->getOperand(2)) };
359   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
360                                            RTLIB::FMA_F32,
361                                            RTLIB::FMA_F64,
362                                            RTLIB::FMA_F80,
363                                            RTLIB::FMA_F128,
364                                            RTLIB::FMA_PPCF128),
365                          NVT, Ops, 3, false, SDLoc(N)).first;
366 }
367
368 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
369   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
370   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
371                      GetSoftenedFloat(N->getOperand(1)) };
372   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
373                                            RTLIB::MUL_F32,
374                                            RTLIB::MUL_F64,
375                                            RTLIB::MUL_F80,
376                                            RTLIB::MUL_F128,
377                                            RTLIB::MUL_PPCF128),
378                          NVT, Ops, 2, false, SDLoc(N)).first;
379 }
380
381 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
382   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
383   SDValue Op = GetSoftenedFloat(N->getOperand(0));
384   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
385                                            RTLIB::NEARBYINT_F32,
386                                            RTLIB::NEARBYINT_F64,
387                                            RTLIB::NEARBYINT_F80,
388                                            RTLIB::NEARBYINT_F128,
389                                            RTLIB::NEARBYINT_PPCF128),
390                          NVT, &Op, 1, false, SDLoc(N)).first;
391 }
392
393 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
394   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
395   SDLoc dl(N);
396   // Expand Y = FNEG(X) -> Y = SUB -0.0, X
397   SDValue Ops[2] = { DAG.getConstantFP(-0.0, dl, N->getValueType(0)),
398                      GetSoftenedFloat(N->getOperand(0)) };
399   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
400                                            RTLIB::SUB_F32,
401                                            RTLIB::SUB_F64,
402                                            RTLIB::SUB_F80,
403                                            RTLIB::SUB_F128,
404                                            RTLIB::SUB_PPCF128),
405                          NVT, Ops, 2, false, dl).first;
406 }
407
408 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
409   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
410   SDValue Op = N->getOperand(0);
411
412   // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
413   // entirely possible for both f16 and f32 to be legal, so use the fully
414   // hard-float FP_EXTEND rather than FP16_TO_FP.
415   if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
416     Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
417     if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
418       SoftenFloatResult(Op.getNode(), 0);
419   }
420
421   RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
422   if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
423     Op = GetSoftenedFloat(Op);
424   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
425   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
426 }
427
428 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
429 // nodes?
430 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
431   EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
432   SDValue Op = N->getOperand(0);
433   SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
434                                   false, SDLoc(N)).first;
435   if (N->getValueType(0) == MVT::f32)
436     return Res32;
437
438   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
439   RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
440   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
441   return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
442 }
443
444 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
445   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
446   SDValue Op = N->getOperand(0);
447   if (N->getValueType(0) == MVT::f16) {
448     // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
449     // storage-only type get a chance to select things.
450     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
451   }
452
453   RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
454   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
455   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
456 }
457
458 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
459   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
460   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
461                      GetSoftenedFloat(N->getOperand(1)) };
462   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
463                                            RTLIB::POW_F32,
464                                            RTLIB::POW_F64,
465                                            RTLIB::POW_F80,
466                                            RTLIB::POW_F128,
467                                            RTLIB::POW_PPCF128),
468                          NVT, Ops, 2, false, SDLoc(N)).first;
469 }
470
471 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
472   assert(N->getOperand(1).getValueType() == MVT::i32 &&
473          "Unsupported power type!");
474   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
475   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
476   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
477                                            RTLIB::POWI_F32,
478                                            RTLIB::POWI_F64,
479                                            RTLIB::POWI_F80,
480                                            RTLIB::POWI_F128,
481                                            RTLIB::POWI_PPCF128),
482                          NVT, Ops, 2, false, SDLoc(N)).first;
483 }
484
485 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
486   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
487   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
488                      GetSoftenedFloat(N->getOperand(1)) };
489   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
490                                            RTLIB::REM_F32,
491                                            RTLIB::REM_F64,
492                                            RTLIB::REM_F80,
493                                            RTLIB::REM_F128,
494                                            RTLIB::REM_PPCF128),
495                          NVT, Ops, 2, false, SDLoc(N)).first;
496 }
497
498 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
499   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
500   SDValue Op = GetSoftenedFloat(N->getOperand(0));
501   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
502                                            RTLIB::RINT_F32,
503                                            RTLIB::RINT_F64,
504                                            RTLIB::RINT_F80,
505                                            RTLIB::RINT_F128,
506                                            RTLIB::RINT_PPCF128),
507                          NVT, &Op, 1, false, SDLoc(N)).first;
508 }
509
510 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
511   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
512   SDValue Op = GetSoftenedFloat(N->getOperand(0));
513   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
514                                            RTLIB::ROUND_F32,
515                                            RTLIB::ROUND_F64,
516                                            RTLIB::ROUND_F80,
517                                            RTLIB::ROUND_F128,
518                                            RTLIB::ROUND_PPCF128),
519                          NVT, &Op, 1, false, SDLoc(N)).first;
520 }
521
522 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
523   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
524   SDValue Op = GetSoftenedFloat(N->getOperand(0));
525   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
526                                            RTLIB::SIN_F32,
527                                            RTLIB::SIN_F64,
528                                            RTLIB::SIN_F80,
529                                            RTLIB::SIN_F128,
530                                            RTLIB::SIN_PPCF128),
531                          NVT, &Op, 1, false, SDLoc(N)).first;
532 }
533
534 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
535   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
536   SDValue Op = GetSoftenedFloat(N->getOperand(0));
537   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
538                                            RTLIB::SQRT_F32,
539                                            RTLIB::SQRT_F64,
540                                            RTLIB::SQRT_F80,
541                                            RTLIB::SQRT_F128,
542                                            RTLIB::SQRT_PPCF128),
543                          NVT, &Op, 1, false, SDLoc(N)).first;
544 }
545
546 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
547   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
548   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
549                      GetSoftenedFloat(N->getOperand(1)) };
550   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
551                                            RTLIB::SUB_F32,
552                                            RTLIB::SUB_F64,
553                                            RTLIB::SUB_F80,
554                                            RTLIB::SUB_F128,
555                                            RTLIB::SUB_PPCF128),
556                          NVT, Ops, 2, false, SDLoc(N)).first;
557 }
558
559 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
560   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
561   if (N->getValueType(0) == MVT::f16)
562     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
563
564   SDValue Op = GetSoftenedFloat(N->getOperand(0));
565   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
566                                            RTLIB::TRUNC_F32,
567                                            RTLIB::TRUNC_F64,
568                                            RTLIB::TRUNC_F80,
569                                            RTLIB::TRUNC_F128,
570                                            RTLIB::TRUNC_PPCF128),
571                          NVT, &Op, 1, false, SDLoc(N)).first;
572 }
573
574 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
575   LoadSDNode *L = cast<LoadSDNode>(N);
576   EVT VT = N->getValueType(0);
577   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
578   SDLoc dl(N);
579
580   SDValue NewL;
581   if (L->getExtensionType() == ISD::NON_EXTLOAD) {
582     NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
583                        NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
584                        L->getPointerInfo(), NVT, L->isVolatile(),
585                        L->isNonTemporal(), false, L->getAlignment(),
586                        L->getAAInfo());
587     // Legalized the chain result - switch anything that used the old chain to
588     // use the new one.
589     ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
590     return NewL;
591   }
592
593   // Do a non-extending load followed by FP_EXTEND.
594   NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
595                      L->getMemoryVT(), dl, L->getChain(),
596                      L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
597                      L->getMemoryVT(), L->isVolatile(),
598                      L->isNonTemporal(), false, L->getAlignment(),
599                      L->getAAInfo());
600   // Legalized the chain result - switch anything that used the old chain to
601   // use the new one.
602   ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
603   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
604 }
605
606 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
607   SDValue LHS = GetSoftenedFloat(N->getOperand(1));
608   SDValue RHS = GetSoftenedFloat(N->getOperand(2));
609   return DAG.getSelect(SDLoc(N),
610                        LHS.getValueType(), N->getOperand(0), LHS, RHS);
611 }
612
613 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
614   SDValue LHS = GetSoftenedFloat(N->getOperand(2));
615   SDValue RHS = GetSoftenedFloat(N->getOperand(3));
616   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
617                      LHS.getValueType(), N->getOperand(0),
618                      N->getOperand(1), LHS, RHS, N->getOperand(4));
619 }
620
621 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
622   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
623                                                N->getValueType(0)));
624 }
625
626 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
627   SDValue Chain = N->getOperand(0); // Get the chain.
628   SDValue Ptr = N->getOperand(1); // Get the pointer.
629   EVT VT = N->getValueType(0);
630   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
631   SDLoc dl(N);
632
633   SDValue NewVAARG;
634   NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
635                           N->getConstantOperandVal(3));
636
637   // Legalized the chain result - switch anything that used the old chain to
638   // use the new one.
639   ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
640   return NewVAARG;
641 }
642
643 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
644   bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
645   EVT SVT = N->getOperand(0).getValueType();
646   EVT RVT = N->getValueType(0);
647   EVT NVT = EVT();
648   SDLoc dl(N);
649
650   // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
651   // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
652   // match.  Look for an appropriate libcall.
653   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
654   for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
655        t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
656     NVT = (MVT::SimpleValueType)t;
657     // The source needs to big enough to hold the operand.
658     if (NVT.bitsGE(SVT))
659       LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
660   }
661   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
662
663   // Sign/zero extend the argument if the libcall takes a larger type.
664   SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
665                            NVT, N->getOperand(0));
666   return TLI.makeLibCall(DAG, LC,
667                          TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
668                          &Op, 1, Signed, dl).first;
669 }
670
671
672 //===----------------------------------------------------------------------===//
673 //  Operand Float to Integer Conversion..
674 //===----------------------------------------------------------------------===//
675
676 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
677   DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
678         dbgs() << "\n");
679   SDValue Res = SDValue();
680
681   switch (N->getOpcode()) {
682   default:
683 #ifndef NDEBUG
684     dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
685     N->dump(&DAG); dbgs() << "\n";
686 #endif
687     llvm_unreachable("Do not know how to soften this operator's operand!");
688
689   case ISD::BITCAST:     Res = SoftenFloatOp_BITCAST(N); break;
690   case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
691   case ISD::FP_EXTEND:   Res = SoftenFloatOp_FP_EXTEND(N); break;
692   case ISD::FP_TO_FP16:  // Same as FP_ROUND for softening purposes
693   case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
694   case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
695   case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
696   case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
697   case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
698   case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
699   }
700
701   // If the result is null, the sub-method took care of registering results etc.
702   if (!Res.getNode()) return false;
703
704   // If the result is N, the sub-method updated N in place.  Tell the legalizer
705   // core about this.
706   if (Res.getNode() == N)
707     return true;
708
709   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
710          "Invalid operand expansion");
711
712   ReplaceValueWith(SDValue(N, 0), Res);
713   return false;
714 }
715
716 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
717   return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
718                      GetSoftenedFloat(N->getOperand(0)));
719 }
720
721 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
722   // If we get here, the result must be legal but the source illegal.
723   EVT SVT = N->getOperand(0).getValueType();
724   EVT RVT = N->getValueType(0);
725   SDValue Op = GetSoftenedFloat(N->getOperand(0));
726
727   if (SVT == MVT::f16)
728     return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
729
730   RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
731   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
732
733   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
734 }
735
736
737 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
738   // We actually deal with the partially-softened FP_TO_FP16 node too, which
739   // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
740   assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
741
742   EVT SVT = N->getOperand(0).getValueType();
743   EVT RVT = N->getValueType(0);
744   EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
745
746   RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
747   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
748
749   SDValue Op = GetSoftenedFloat(N->getOperand(0));
750   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
751 }
752
753 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
754   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
755   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
756
757   EVT VT = NewLHS.getValueType();
758   NewLHS = GetSoftenedFloat(NewLHS);
759   NewRHS = GetSoftenedFloat(NewRHS);
760   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
761
762   // If softenSetCCOperands returned a scalar, we need to compare the result
763   // against zero to select between true and false values.
764   if (!NewRHS.getNode()) {
765     NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
766     CCCode = ISD::SETNE;
767   }
768
769   // Update N to have the operands specified.
770   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
771                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
772                                 N->getOperand(4)),
773                  0);
774 }
775
776 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
777   EVT RVT = N->getValueType(0);
778   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
779   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
780   SDValue Op = GetSoftenedFloat(N->getOperand(0));
781   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
782 }
783
784 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
785   EVT RVT = N->getValueType(0);
786   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
787   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
788   SDValue Op = GetSoftenedFloat(N->getOperand(0));
789   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
790 }
791
792 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
793   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
794   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
795
796   EVT VT = NewLHS.getValueType();
797   NewLHS = GetSoftenedFloat(NewLHS);
798   NewRHS = GetSoftenedFloat(NewRHS);
799   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
800
801   // If softenSetCCOperands returned a scalar, we need to compare the result
802   // against zero to select between true and false values.
803   if (!NewRHS.getNode()) {
804     NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
805     CCCode = ISD::SETNE;
806   }
807
808   // Update N to have the operands specified.
809   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
810                                 N->getOperand(2), N->getOperand(3),
811                                 DAG.getCondCode(CCCode)),
812                  0);
813 }
814
815 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
816   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
817   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
818
819   EVT VT = NewLHS.getValueType();
820   NewLHS = GetSoftenedFloat(NewLHS);
821   NewRHS = GetSoftenedFloat(NewRHS);
822   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
823
824   // If softenSetCCOperands returned a scalar, use it.
825   if (!NewRHS.getNode()) {
826     assert(NewLHS.getValueType() == N->getValueType(0) &&
827            "Unexpected setcc expansion!");
828     return NewLHS;
829   }
830
831   // Otherwise, update N to have the operands specified.
832   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
833                                 DAG.getCondCode(CCCode)),
834                  0);
835 }
836
837 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
838   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
839   assert(OpNo == 1 && "Can only soften the stored value!");
840   StoreSDNode *ST = cast<StoreSDNode>(N);
841   SDValue Val = ST->getValue();
842   SDLoc dl(N);
843
844   if (ST->isTruncatingStore())
845     // Do an FP_ROUND followed by a non-truncating store.
846     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
847                                           Val, DAG.getIntPtrConstant(0, dl)));
848   else
849     Val = GetSoftenedFloat(Val);
850
851   return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
852                       ST->getMemOperand());
853 }
854
855
856 //===----------------------------------------------------------------------===//
857 //  Float Result Expansion
858 //===----------------------------------------------------------------------===//
859
860 /// ExpandFloatResult - This method is called when the specified result of the
861 /// specified node is found to need expansion.  At this point, the node may also
862 /// have invalid operands or may have other results that need promotion, we just
863 /// know that (at least) one result needs expansion.
864 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
865   DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
866   SDValue Lo, Hi;
867   Lo = Hi = SDValue();
868
869   // See if the target wants to custom expand this node.
870   if (CustomLowerNode(N, N->getValueType(ResNo), true))
871     return;
872
873   switch (N->getOpcode()) {
874   default:
875 #ifndef NDEBUG
876     dbgs() << "ExpandFloatResult #" << ResNo << ": ";
877     N->dump(&DAG); dbgs() << "\n";
878 #endif
879     llvm_unreachable("Do not know how to expand the result of this operator!");
880
881   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
882   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
883   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
884
885   case ISD::MERGE_VALUES:       ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
886   case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
887   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
888   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
889   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
890   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
891
892   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
893   case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
894   case ISD::FMINNUM:    ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
895   case ISD::FMAXNUM:    ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
896   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
897   case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
898   case ISD::FCOPYSIGN:  ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
899   case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
900   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
901   case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
902   case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
903   case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
904   case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
905   case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
906   case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
907   case ISD::FMA:        ExpandFloatRes_FMA(N, Lo, Hi); break;
908   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
909   case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
910   case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
911   case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
912   case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
913   case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
914   case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
915   case ISD::FROUND:     ExpandFloatRes_FROUND(N, Lo, Hi); break;
916   case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
917   case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
918   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
919   case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
920   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
921   case ISD::SINT_TO_FP:
922   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
923   case ISD::FREM:       ExpandFloatRes_FREM(N, Lo, Hi); break;
924   }
925
926   // If Lo/Hi is null, the sub-method took care of registering results etc.
927   if (Lo.getNode())
928     SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
929 }
930
931 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
932                                                  SDValue &Hi) {
933   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
934   assert(NVT.getSizeInBits() == integerPartWidth &&
935          "Do not know how to expand this float constant!");
936   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
937   SDLoc dl(N);
938   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
939                                  APInt(integerPartWidth, C.getRawData()[1])),
940                          dl, NVT);
941   Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
942                                  APInt(integerPartWidth, C.getRawData()[0])),
943                          dl, NVT);
944 }
945
946 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
947                                            SDValue &Hi) {
948   assert(N->getValueType(0) == MVT::ppcf128 &&
949          "Logic only correct for ppcf128!");
950   SDLoc dl(N);
951   SDValue Tmp;
952   GetExpandedFloat(N->getOperand(0), Lo, Tmp);
953   Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
954   // Lo = Hi==fabs(Hi) ? Lo : -Lo;
955   Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
956                    DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
957                    ISD::SETEQ);
958 }
959
960 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
961                                               SDValue &Hi) {
962   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
963                                          RTLIB::FMIN_F32, RTLIB::FMIN_F64,
964                                          RTLIB::FMIN_F80, RTLIB::FMIN_F128,
965                                          RTLIB::FMIN_PPCF128),
966                             N, false);
967   GetPairElements(Call, Lo, Hi);
968 }
969
970 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
971                                               SDValue &Hi) {
972   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
973                                          RTLIB::FMAX_F32, RTLIB::FMAX_F64,
974                                          RTLIB::FMAX_F80, RTLIB::FMAX_F128,
975                                          RTLIB::FMAX_PPCF128),
976                             N, false);
977   GetPairElements(Call, Lo, Hi);
978 }
979
980 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
981                                            SDValue &Hi) {
982   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
983                                          RTLIB::ADD_F32, RTLIB::ADD_F64,
984                                          RTLIB::ADD_F80, RTLIB::ADD_F128,
985                                          RTLIB::ADD_PPCF128),
986                             N, false);
987   GetPairElements(Call, Lo, Hi);
988 }
989
990 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
991                                             SDValue &Lo, SDValue &Hi) {
992   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
993                                          RTLIB::CEIL_F32, RTLIB::CEIL_F64,
994                                          RTLIB::CEIL_F80, RTLIB::CEIL_F128,
995                                          RTLIB::CEIL_PPCF128),
996                             N, false);
997   GetPairElements(Call, Lo, Hi);
998 }
999
1000 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
1001                                                 SDValue &Lo, SDValue &Hi) {
1002   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1003                                          RTLIB::COPYSIGN_F32,
1004                                          RTLIB::COPYSIGN_F64,
1005                                          RTLIB::COPYSIGN_F80,
1006                                          RTLIB::COPYSIGN_F128,
1007                                          RTLIB::COPYSIGN_PPCF128),
1008                             N, false);
1009   GetPairElements(Call, Lo, Hi);
1010 }
1011
1012 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1013                                            SDValue &Lo, SDValue &Hi) {
1014   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1015                                          RTLIB::COS_F32, RTLIB::COS_F64,
1016                                          RTLIB::COS_F80, RTLIB::COS_F128,
1017                                          RTLIB::COS_PPCF128),
1018                             N, false);
1019   GetPairElements(Call, Lo, Hi);
1020 }
1021
1022 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1023                                            SDValue &Hi) {
1024   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1025   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1026                                                    RTLIB::DIV_F32,
1027                                                    RTLIB::DIV_F64,
1028                                                    RTLIB::DIV_F80,
1029                                                    RTLIB::DIV_F128,
1030                                                    RTLIB::DIV_PPCF128),
1031                                  N->getValueType(0), Ops, 2, false,
1032                                  SDLoc(N)).first;
1033   GetPairElements(Call, Lo, Hi);
1034 }
1035
1036 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1037                                            SDValue &Lo, SDValue &Hi) {
1038   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1039                                          RTLIB::EXP_F32, RTLIB::EXP_F64,
1040                                          RTLIB::EXP_F80, RTLIB::EXP_F128,
1041                                          RTLIB::EXP_PPCF128),
1042                             N, false);
1043   GetPairElements(Call, Lo, Hi);
1044 }
1045
1046 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1047                                             SDValue &Lo, SDValue &Hi) {
1048   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1049                                          RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1050                                          RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1051                                          RTLIB::EXP2_PPCF128),
1052                             N, false);
1053   GetPairElements(Call, Lo, Hi);
1054 }
1055
1056 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1057                                              SDValue &Lo, SDValue &Hi) {
1058   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1059                                          RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1060                                          RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1061                                          RTLIB::FLOOR_PPCF128),
1062                             N, false);
1063   GetPairElements(Call, Lo, Hi);
1064 }
1065
1066 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1067                                            SDValue &Lo, SDValue &Hi) {
1068   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1069                                          RTLIB::LOG_F32, RTLIB::LOG_F64,
1070                                          RTLIB::LOG_F80, RTLIB::LOG_F128,
1071                                          RTLIB::LOG_PPCF128),
1072                             N, false);
1073   GetPairElements(Call, Lo, Hi);
1074 }
1075
1076 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1077                                             SDValue &Lo, SDValue &Hi) {
1078   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1079                                          RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1080                                          RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1081                                          RTLIB::LOG2_PPCF128),
1082                             N, false);
1083   GetPairElements(Call, Lo, Hi);
1084 }
1085
1086 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1087                                              SDValue &Lo, SDValue &Hi) {
1088   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1089                                          RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1090                                          RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1091                                          RTLIB::LOG10_PPCF128),
1092                             N, false);
1093   GetPairElements(Call, Lo, Hi);
1094 }
1095
1096 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1097                                           SDValue &Hi) {
1098   SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1099   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1100                                                    RTLIB::FMA_F32,
1101                                                    RTLIB::FMA_F64,
1102                                                    RTLIB::FMA_F80,
1103                                                    RTLIB::FMA_F128,
1104                                                    RTLIB::FMA_PPCF128),
1105                                  N->getValueType(0), Ops, 3, false,
1106                                  SDLoc(N)).first;
1107   GetPairElements(Call, Lo, Hi);
1108 }
1109
1110 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1111                                            SDValue &Hi) {
1112   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1113   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1114                                                    RTLIB::MUL_F32,
1115                                                    RTLIB::MUL_F64,
1116                                                    RTLIB::MUL_F80,
1117                                                    RTLIB::MUL_F128,
1118                                                    RTLIB::MUL_PPCF128),
1119                                  N->getValueType(0), Ops, 2, false,
1120                                  SDLoc(N)).first;
1121   GetPairElements(Call, Lo, Hi);
1122 }
1123
1124 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1125                                                  SDValue &Lo, SDValue &Hi) {
1126   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1127                                          RTLIB::NEARBYINT_F32,
1128                                          RTLIB::NEARBYINT_F64,
1129                                          RTLIB::NEARBYINT_F80,
1130                                          RTLIB::NEARBYINT_F128,
1131                                          RTLIB::NEARBYINT_PPCF128),
1132                             N, false);
1133   GetPairElements(Call, Lo, Hi);
1134 }
1135
1136 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1137                                            SDValue &Hi) {
1138   SDLoc dl(N);
1139   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1140   Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1141   Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1142 }
1143
1144 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1145                                                 SDValue &Hi) {
1146   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1147   SDLoc dl(N);
1148   Hi = DAG.getNode(ISD::FP_EXTEND, dl, NVT, N->getOperand(0));
1149   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1150                                  APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1151 }
1152
1153 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1154                                            SDValue &Lo, SDValue &Hi) {
1155   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1156                                          RTLIB::POW_F32, RTLIB::POW_F64,
1157                                          RTLIB::POW_F80, RTLIB::POW_F128,
1158                                          RTLIB::POW_PPCF128),
1159                             N, false);
1160   GetPairElements(Call, Lo, Hi);
1161 }
1162
1163 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1164                                             SDValue &Lo, SDValue &Hi) {
1165   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1166                                          RTLIB::POWI_F32, RTLIB::POWI_F64,
1167                                          RTLIB::POWI_F80, RTLIB::POWI_F128,
1168                                          RTLIB::POWI_PPCF128),
1169                             N, false);
1170   GetPairElements(Call, Lo, Hi);
1171 }
1172
1173 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1174                                            SDValue &Lo, SDValue &Hi) {
1175   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1176                                          RTLIB::REM_F32, RTLIB::REM_F64,
1177                                          RTLIB::REM_F80, RTLIB::REM_F128,
1178                                          RTLIB::REM_PPCF128),
1179                             N, false);
1180   GetPairElements(Call, Lo, Hi);
1181 }
1182
1183 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1184                                             SDValue &Lo, SDValue &Hi) {
1185   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1186                                          RTLIB::RINT_F32, RTLIB::RINT_F64,
1187                                          RTLIB::RINT_F80, RTLIB::RINT_F128,
1188                                          RTLIB::RINT_PPCF128),
1189                             N, false);
1190   GetPairElements(Call, Lo, Hi);
1191 }
1192
1193 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1194                                              SDValue &Lo, SDValue &Hi) {
1195   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1196                                          RTLIB::ROUND_F32,
1197                                          RTLIB::ROUND_F64,
1198                                          RTLIB::ROUND_F80,
1199                                          RTLIB::ROUND_F128,
1200                                          RTLIB::ROUND_PPCF128),
1201                             N, false);
1202   GetPairElements(Call, Lo, Hi);
1203 }
1204
1205 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1206                                            SDValue &Lo, SDValue &Hi) {
1207   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1208                                          RTLIB::SIN_F32, RTLIB::SIN_F64,
1209                                          RTLIB::SIN_F80, RTLIB::SIN_F128,
1210                                          RTLIB::SIN_PPCF128),
1211                             N, false);
1212   GetPairElements(Call, Lo, Hi);
1213 }
1214
1215 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1216                                             SDValue &Lo, SDValue &Hi) {
1217   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1218                                          RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1219                                          RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1220                                          RTLIB::SQRT_PPCF128),
1221                             N, false);
1222   GetPairElements(Call, Lo, Hi);
1223 }
1224
1225 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1226                                            SDValue &Hi) {
1227   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1228   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1229                                                    RTLIB::SUB_F32,
1230                                                    RTLIB::SUB_F64,
1231                                                    RTLIB::SUB_F80,
1232                                                    RTLIB::SUB_F128,
1233                                                    RTLIB::SUB_PPCF128),
1234                                  N->getValueType(0), Ops, 2, false,
1235                                  SDLoc(N)).first;
1236   GetPairElements(Call, Lo, Hi);
1237 }
1238
1239 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1240                                              SDValue &Lo, SDValue &Hi) {
1241   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1242                                          RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1243                                          RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1244                                          RTLIB::TRUNC_PPCF128),
1245                             N, false);
1246   GetPairElements(Call, Lo, Hi);
1247 }
1248
1249 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1250                                            SDValue &Hi) {
1251   if (ISD::isNormalLoad(N)) {
1252     ExpandRes_NormalLoad(N, Lo, Hi);
1253     return;
1254   }
1255
1256   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1257   LoadSDNode *LD = cast<LoadSDNode>(N);
1258   SDValue Chain = LD->getChain();
1259   SDValue Ptr = LD->getBasePtr();
1260   SDLoc dl(N);
1261
1262   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1263   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1264   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1265
1266   Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1267                       LD->getMemoryVT(), LD->getMemOperand());
1268
1269   // Remember the chain.
1270   Chain = Hi.getValue(1);
1271
1272   // The low part is zero.
1273   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1274                                  APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1275
1276   // Modified the chain - switch anything that used the old chain to use the
1277   // new one.
1278   ReplaceValueWith(SDValue(LD, 1), Chain);
1279 }
1280
1281 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1282                                                  SDValue &Hi) {
1283   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1284   EVT VT = N->getValueType(0);
1285   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1286   SDValue Src = N->getOperand(0);
1287   EVT SrcVT = Src.getValueType();
1288   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1289   SDLoc dl(N);
1290
1291   // First do an SINT_TO_FP, whether the original was signed or unsigned.
1292   // When promoting partial word types to i32 we must honor the signedness,
1293   // though.
1294   if (SrcVT.bitsLE(MVT::i32)) {
1295     // The integer can be represented exactly in an f64.
1296     Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1297                       MVT::i32, Src);
1298     Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1299                                    APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1300     Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1301   } else {
1302     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1303     if (SrcVT.bitsLE(MVT::i64)) {
1304       Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1305                         MVT::i64, Src);
1306       LC = RTLIB::SINTTOFP_I64_PPCF128;
1307     } else if (SrcVT.bitsLE(MVT::i128)) {
1308       Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1309       LC = RTLIB::SINTTOFP_I128_PPCF128;
1310     }
1311     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1312
1313     Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1314     GetPairElements(Hi, Lo, Hi);
1315   }
1316
1317   if (isSigned)
1318     return;
1319
1320   // Unsigned - fix up the SINT_TO_FP value just calculated.
1321   Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1322   SrcVT = Src.getValueType();
1323
1324   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1325   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
1326   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
1327   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1328   ArrayRef<uint64_t> Parts;
1329
1330   switch (SrcVT.getSimpleVT().SimpleTy) {
1331   default:
1332     llvm_unreachable("Unsupported UINT_TO_FP!");
1333   case MVT::i32:
1334     Parts = TwoE32;
1335     break;
1336   case MVT::i64:
1337     Parts = TwoE64;
1338     break;
1339   case MVT::i128:
1340     Parts = TwoE128;
1341     break;
1342   }
1343
1344   // TODO: Are there fast-math-flags to propagate to this FADD?
1345   Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1346                    DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1347                                              APInt(128, Parts)),
1348                                      dl, MVT::ppcf128));
1349   Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, dl, SrcVT),
1350                        Lo, Hi, ISD::SETLT);
1351   GetPairElements(Lo, Lo, Hi);
1352 }
1353
1354
1355 //===----------------------------------------------------------------------===//
1356 //  Float Operand Expansion
1357 //===----------------------------------------------------------------------===//
1358
1359 /// ExpandFloatOperand - This method is called when the specified operand of the
1360 /// specified node is found to need expansion.  At this point, all of the result
1361 /// types of the node are known to be legal, but other operands of the node may
1362 /// need promotion or expansion as well as the specified one.
1363 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1364   DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1365   SDValue Res = SDValue();
1366
1367   // See if the target wants to custom expand this node.
1368   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1369     return false;
1370
1371   switch (N->getOpcode()) {
1372   default:
1373 #ifndef NDEBUG
1374     dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1375     N->dump(&DAG); dbgs() << "\n";
1376 #endif
1377     llvm_unreachable("Do not know how to expand this operator's operand!");
1378
1379   case ISD::BITCAST:         Res = ExpandOp_BITCAST(N); break;
1380   case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
1381   case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1382
1383   case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
1384   case ISD::FCOPYSIGN:  Res = ExpandFloatOp_FCOPYSIGN(N); break;
1385   case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
1386   case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1387   case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1388   case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
1389   case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
1390   case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1391                                                   OpNo); break;
1392   }
1393
1394   // If the result is null, the sub-method took care of registering results etc.
1395   if (!Res.getNode()) return false;
1396
1397   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1398   // core about this.
1399   if (Res.getNode() == N)
1400     return true;
1401
1402   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1403          "Invalid operand expansion");
1404
1405   ReplaceValueWith(SDValue(N, 0), Res);
1406   return false;
1407 }
1408
1409 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
1410 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1411 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1412                                                 SDValue &NewRHS,
1413                                                 ISD::CondCode &CCCode,
1414                                                 SDLoc dl) {
1415   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1416   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1417   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1418
1419   assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1420
1421   // FIXME:  This generated code sucks.  We want to generate
1422   //         FCMPU crN, hi1, hi2
1423   //         BNE crN, L:
1424   //         FCMPU crN, lo1, lo2
1425   // The following can be improved, but not that much.
1426   SDValue Tmp1, Tmp2, Tmp3;
1427   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1428                       LHSHi, RHSHi, ISD::SETOEQ);
1429   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1430                       LHSLo, RHSLo, CCCode);
1431   Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1432   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1433                       LHSHi, RHSHi, ISD::SETUNE);
1434   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1435                       LHSHi, RHSHi, CCCode);
1436   Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1437   NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1438   NewRHS = SDValue();   // LHS is the result, not a compare.
1439 }
1440
1441 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1442   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1443   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1444   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1445
1446   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1447   // against zero to select between true and false values.
1448   if (!NewRHS.getNode()) {
1449     NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1450     CCCode = ISD::SETNE;
1451   }
1452
1453   // Update N to have the operands specified.
1454   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1455                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1456                                 N->getOperand(4)), 0);
1457 }
1458
1459 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1460   assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1461          "Logic only correct for ppcf128!");
1462   SDValue Lo, Hi;
1463   GetExpandedFloat(N->getOperand(1), Lo, Hi);
1464   // The ppcf128 value is providing only the sign; take it from the
1465   // higher-order double (which must have the larger magnitude).
1466   return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1467                      N->getValueType(0), N->getOperand(0), Hi);
1468 }
1469
1470 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1471   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1472          "Logic only correct for ppcf128!");
1473   SDValue Lo, Hi;
1474   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1475   // Round it the rest of the way (e.g. to f32) if needed.
1476   return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1477                      N->getValueType(0), Hi, N->getOperand(1));
1478 }
1479
1480 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1481   EVT RVT = N->getValueType(0);
1482   SDLoc dl(N);
1483
1484   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1485   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1486   if (RVT == MVT::i32) {
1487     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1488            "Logic only correct for ppcf128!");
1489     SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1490                               N->getOperand(0), DAG.getValueType(MVT::f64));
1491     Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1492                       DAG.getIntPtrConstant(1, dl));
1493     return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1494   }
1495
1496   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1497   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1498   return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1499 }
1500
1501 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1502   EVT RVT = N->getValueType(0);
1503   SDLoc dl(N);
1504
1505   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1506   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1507   if (RVT == MVT::i32) {
1508     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1509            "Logic only correct for ppcf128!");
1510     const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1511     APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1512     SDValue Tmp = DAG.getConstantFP(APF, dl, MVT::ppcf128);
1513     //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1514     // FIXME: generated code sucks.
1515     // TODO: Are there fast-math-flags to propagate to this FSUB?
1516     return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1517                            DAG.getNode(ISD::ADD, dl, MVT::i32,
1518                                        DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1519                                                    DAG.getNode(ISD::FSUB, dl,
1520                                                                MVT::ppcf128,
1521                                                                N->getOperand(0),
1522                                                                Tmp)),
1523                                        DAG.getConstant(0x80000000, dl,
1524                                                        MVT::i32)),
1525                            DAG.getNode(ISD::FP_TO_SINT, dl,
1526                                        MVT::i32, N->getOperand(0)),
1527                            ISD::SETGE);
1528   }
1529
1530   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1531   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1532   return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1533                          false, dl).first;
1534 }
1535
1536 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1537   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1538   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1539   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1540
1541   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1542   // against zero to select between true and false values.
1543   if (!NewRHS.getNode()) {
1544     NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1545     CCCode = ISD::SETNE;
1546   }
1547
1548   // Update N to have the operands specified.
1549   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1550                                 N->getOperand(2), N->getOperand(3),
1551                                 DAG.getCondCode(CCCode)), 0);
1552 }
1553
1554 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1555   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1556   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1557   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1558
1559   // If ExpandSetCCOperands returned a scalar, use it.
1560   if (!NewRHS.getNode()) {
1561     assert(NewLHS.getValueType() == N->getValueType(0) &&
1562            "Unexpected setcc expansion!");
1563     return NewLHS;
1564   }
1565
1566   // Otherwise, update N to have the operands specified.
1567   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1568                                 DAG.getCondCode(CCCode)), 0);
1569 }
1570
1571 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1572   if (ISD::isNormalStore(N))
1573     return ExpandOp_NormalStore(N, OpNo);
1574
1575   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1576   assert(OpNo == 1 && "Can only expand the stored value so far");
1577   StoreSDNode *ST = cast<StoreSDNode>(N);
1578
1579   SDValue Chain = ST->getChain();
1580   SDValue Ptr = ST->getBasePtr();
1581
1582   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1583                                      ST->getValue().getValueType());
1584   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1585   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1586   (void)NVT;
1587
1588   SDValue Lo, Hi;
1589   GetExpandedOp(ST->getValue(), Lo, Hi);
1590
1591   return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1592                            ST->getMemoryVT(), ST->getMemOperand());
1593 }
1594
1595 //===----------------------------------------------------------------------===//
1596 //  Float Operand Promotion
1597 //===----------------------------------------------------------------------===//
1598 //
1599
1600 static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
1601   if (OpVT == MVT::f16) {
1602       return ISD::FP16_TO_FP;
1603   } else if (RetVT == MVT::f16) {
1604       return ISD::FP_TO_FP16;
1605   }
1606
1607   report_fatal_error("Attempt at an invalid promotion-related conversion");
1608 }
1609
1610 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1611   SDValue R = SDValue();
1612
1613   // Nodes that use a promotion-requiring floating point operand, but doesn't
1614   // produce a promotion-requiring floating point result, need to be legalized
1615   // to use the promoted float operand.  Nodes that produce at least one
1616   // promotion-requiring floating point result have their operands legalized as
1617   // a part of PromoteFloatResult.
1618   switch (N->getOpcode()) {
1619     default:
1620       llvm_unreachable("Do not know how to promote this operator's operand!");
1621
1622     case ISD::BITCAST:    R = PromoteFloatOp_BITCAST(N, OpNo); break;
1623     case ISD::FCOPYSIGN:  R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1624     case ISD::FP_TO_SINT:
1625     case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1626     case ISD::FP_EXTEND:  R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1627     case ISD::SELECT_CC:  R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1628     case ISD::SETCC:      R = PromoteFloatOp_SETCC(N, OpNo); break;
1629     case ISD::STORE:      R = PromoteFloatOp_STORE(N, OpNo); break;
1630   }
1631
1632   if (R.getNode())
1633     ReplaceValueWith(SDValue(N, 0), R);
1634   return false;
1635 }
1636
1637 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1638   SDValue Op = N->getOperand(0);
1639   EVT OpVT = Op->getValueType(0);
1640
1641   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1642   assert (IVT == N->getValueType(0) && "Bitcast to type of different size");
1643
1644   SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1645   EVT PromotedVT = Promoted->getValueType(0);
1646
1647   // Convert the promoted float value to the desired IVT.
1648   return DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N), IVT,
1649                      Promoted);
1650 }
1651
1652 // Promote Operand 1 of FCOPYSIGN.  Operand 0 ought to be handled by
1653 // PromoteFloatRes_FCOPYSIGN.
1654 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1655   assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1656   SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1657
1658   return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1659                      N->getOperand(0), Op1);
1660 }
1661
1662 // Convert the promoted float value to the desired integer type
1663 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1664   SDValue Op = GetPromotedFloat(N->getOperand(0));
1665   return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1666 }
1667
1668 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1669   SDValue Op = GetPromotedFloat(N->getOperand(0));
1670   EVT VT = N->getValueType(0);
1671
1672   // Desired VT is same as promoted type.  Use promoted float directly.
1673   if (VT == Op->getValueType(0))
1674     return Op;
1675
1676   // Else, extend the promoted float value to the desired VT.
1677   return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1678 }
1679
1680 // Promote the float operands used for comparison.  The true- and false-
1681 // operands have the same type as the result and are promoted, if needed, by
1682 // PromoteFloatRes_SELECT_CC
1683 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1684   SDValue LHS = GetPromotedFloat(N->getOperand(0));
1685   SDValue RHS = GetPromotedFloat(N->getOperand(1));
1686
1687   return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1688                      LHS, RHS, N->getOperand(2), N->getOperand(3),
1689                      N->getOperand(4));
1690 }
1691
1692 // Construct a SETCC that compares the promoted values and sets the conditional
1693 // code.
1694 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1695   EVT VT = N->getValueType(0);
1696   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1697   SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1698   SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1699   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1700
1701   return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1702
1703 }
1704
1705 // Lower the promoted Float down to the integer value of same size and construct
1706 // a STORE of the integer value.
1707 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1708   StoreSDNode *ST = cast<StoreSDNode>(N);
1709   SDValue Val = ST->getValue();
1710   SDLoc DL(N);
1711
1712   SDValue Promoted = GetPromotedFloat(Val);
1713   EVT VT = ST->getOperand(1)->getValueType(0);
1714   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1715
1716   SDValue NewVal;
1717   NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1718                        IVT, Promoted);
1719
1720   return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1721                       ST->getMemOperand());
1722 }
1723
1724 //===----------------------------------------------------------------------===//
1725 //  Float Result Promotion
1726 //===----------------------------------------------------------------------===//
1727
1728 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1729   SDValue R = SDValue();
1730
1731   switch (N->getOpcode()) {
1732     // These opcodes cannot appear if promotion of FP16 is done in the backend
1733     // instead of Clang
1734     case ISD::FP16_TO_FP:
1735     case ISD::FP_TO_FP16:
1736     default:
1737       llvm_unreachable("Do not know how to promote this operator's result!");
1738
1739     case ISD::BITCAST:    R = PromoteFloatRes_BITCAST(N); break;
1740     case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
1741     case ISD::EXTRACT_VECTOR_ELT:
1742                           R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
1743     case ISD::FCOPYSIGN:  R = PromoteFloatRes_FCOPYSIGN(N); break;
1744
1745     // Unary FP Operations
1746     case ISD::FABS:
1747     case ISD::FCEIL:
1748     case ISD::FCOS:
1749     case ISD::FEXP:
1750     case ISD::FEXP2:
1751     case ISD::FFLOOR:
1752     case ISD::FLOG:
1753     case ISD::FLOG2:
1754     case ISD::FLOG10:
1755     case ISD::FNEARBYINT:
1756     case ISD::FNEG:
1757     case ISD::FRINT:
1758     case ISD::FROUND:
1759     case ISD::FSIN:
1760     case ISD::FSQRT:
1761     case ISD::FTRUNC:     R = PromoteFloatRes_UnaryOp(N); break;
1762
1763     // Binary FP Operations
1764     case ISD::FADD:
1765     case ISD::FDIV:
1766     case ISD::FMAXNUM:
1767     case ISD::FMINNUM:
1768     case ISD::FMUL:
1769     case ISD::FPOW:
1770     case ISD::FREM:
1771     case ISD::FSUB:       R = PromoteFloatRes_BinOp(N); break;
1772
1773     case ISD::FMA:        // FMA is same as FMAD
1774     case ISD::FMAD:       R = PromoteFloatRes_FMAD(N); break;
1775
1776     case ISD::FPOWI:      R = PromoteFloatRes_FPOWI(N); break;
1777
1778     case ISD::FP_ROUND:   R = PromoteFloatRes_FP_ROUND(N); break;
1779     case ISD::LOAD:       R = PromoteFloatRes_LOAD(N); break;
1780     case ISD::SELECT:     R = PromoteFloatRes_SELECT(N); break;
1781     case ISD::SELECT_CC:  R = PromoteFloatRes_SELECT_CC(N); break;
1782
1783     case ISD::SINT_TO_FP:
1784     case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
1785     case ISD::UNDEF:      R = PromoteFloatRes_UNDEF(N); break;
1786
1787   }
1788
1789   if (R.getNode())
1790     SetPromotedFloat(SDValue(N, ResNo), R);
1791 }
1792
1793 // Bitcast from i16 to f16:  convert the i16 to a f32 value instead.
1794 // At this point, it is not possible to determine if the bitcast value is
1795 // eventually stored to memory or promoted to f32 or promoted to a floating
1796 // point at a higher precision.  Some of these cases are handled by FP_EXTEND,
1797 // STORE promotion handlers.
1798 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
1799   EVT VT = N->getValueType(0);
1800   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1801   return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT,
1802                      N->getOperand(0));
1803 }
1804
1805 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
1806   ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
1807   EVT VT = N->getValueType(0);
1808   SDLoc DL(N);
1809
1810   // Get the (bit-cast) APInt of the APFloat and build an integer constant
1811   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1812   SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(), DL,
1813                               IVT);
1814
1815   // Convert the Constant to the desired FP type
1816   // FIXME We might be able to do the conversion during compilation and get rid
1817   // of it from the object code
1818   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1819   return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, C);
1820 }
1821
1822 // If the Index operand is a constant, try to redirect the extract operation to
1823 // the correct legalized vector.  If not, bit-convert the input vector to
1824 // equivalent integer vector.  Extract the element as an (bit-cast) integer
1825 // value and convert it to the promoted type.
1826 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
1827   SDLoc DL(N);
1828
1829   // If the index is constant, try to extract the value from the legalized
1830   // vector type.
1831   if (isa<ConstantSDNode>(N->getOperand(1))) {
1832     SDValue Vec = N->getOperand(0);
1833     SDValue Idx = N->getOperand(1);
1834     EVT VecVT = Vec->getValueType(0);
1835     EVT EltVT = VecVT.getVectorElementType();
1836
1837     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1838
1839     switch (getTypeAction(VecVT)) {
1840     default: break;
1841     case TargetLowering::TypeScalarizeVector: {
1842       SDValue Res = GetScalarizedVector(N->getOperand(0));
1843       ReplaceValueWith(SDValue(N, 0), Res);
1844       return SDValue();
1845     }
1846     case TargetLowering::TypeWidenVector: {
1847       Vec = GetWidenedVector(Vec);
1848       SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
1849       ReplaceValueWith(SDValue(N, 0), Res);
1850       return SDValue();
1851     }
1852     case TargetLowering::TypeSplitVector: {
1853       SDValue Lo, Hi;
1854       GetSplitVector(Vec, Lo, Hi);
1855
1856       uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1857       SDValue Res;
1858       if (IdxVal < LoElts)
1859         Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
1860       else
1861         Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
1862                           DAG.getConstant(IdxVal - LoElts, DL,
1863                                           Idx.getValueType()));
1864       ReplaceValueWith(SDValue(N, 0), Res);
1865       return SDValue();
1866     }
1867
1868     }
1869   }
1870
1871   // Bit-convert the input vector to the equivalent integer vector
1872   SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
1873   EVT IVT = NewOp.getValueType().getVectorElementType();
1874
1875   // Extract the element as an (bit-cast) integer value
1876   SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
1877                                NewOp, N->getOperand(1));
1878
1879   // Convert the element to the desired FP type
1880   EVT VT = N->getValueType(0);
1881   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1882   return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
1883 }
1884
1885 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y.  If the result
1886 // needs promotion, so does the argument X.  Note that Y, if needed, will be
1887 // handled during operand promotion.
1888 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
1889   EVT VT = N->getValueType(0);
1890   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1891   SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1892
1893   SDValue Op1 = N->getOperand(1);
1894
1895   return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1896 }
1897
1898 // Unary operation where the result and the operand have PromoteFloat type
1899 // action.  Construct a new SDNode with the promoted float value of the old
1900 // operand.
1901 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
1902   EVT VT = N->getValueType(0);
1903   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1904   SDValue Op = GetPromotedFloat(N->getOperand(0));
1905
1906   return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
1907 }
1908
1909 // Binary operations where the result and both operands have PromoteFloat type
1910 // action.  Construct a new SDNode with the promoted float values of the old
1911 // operands.
1912 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
1913   EVT VT = N->getValueType(0);
1914   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1915   SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1916   SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1917   return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, N->getFlags());
1918 }
1919
1920 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
1921   EVT VT = N->getValueType(0);
1922   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1923   SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1924   SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1925   SDValue Op2 = GetPromotedFloat(N->getOperand(2));
1926
1927   return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
1928 }
1929
1930 // Promote the Float (first) operand and retain the Integer (second) operand
1931 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
1932   EVT VT = N->getValueType(0);
1933   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1934   SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1935   SDValue Op1 = N->getOperand(1);
1936
1937   return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1938 }
1939
1940 // Explicit operation to reduce precision.  Reduce the value to half precision
1941 // and promote it back to the legal type.
1942 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
1943   SDLoc DL(N);
1944
1945   SDValue Op = N->getOperand(0);
1946   EVT VT = N->getValueType(0);
1947   EVT OpVT = Op->getValueType(0);
1948   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1949   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1950
1951   // Round promoted float to desired precision
1952   SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
1953   // Promote it back to the legal output type
1954   return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
1955 }
1956
1957 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
1958   LoadSDNode *L = cast<LoadSDNode>(N);
1959   EVT VT = N->getValueType(0);
1960
1961   // Load the value as an integer value with the same number of bits
1962   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1963   SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
1964                    IVT, SDLoc(N), L->getChain(), L->getBasePtr(),
1965                    L->getOffset(), L->getPointerInfo(), IVT, L->isVolatile(),
1966                    L->isNonTemporal(), false, L->getAlignment(),
1967                    L->getAAInfo());
1968   // Legalize the chain result by replacing uses of the old value chain with the
1969   // new one
1970   ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
1971
1972   // Convert the integer value to the desired FP type
1973   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1974   return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
1975 }
1976
1977 // Construct a new SELECT node with the promoted true- and false- values.
1978 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
1979   SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
1980   SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
1981
1982   return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
1983                      N->getOperand(0), TrueVal, FalseVal);
1984 }
1985
1986 // Construct a new SELECT_CC node with the promoted true- and false- values.
1987 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
1988 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
1989   SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
1990   SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
1991
1992   return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1993                      N->getOperand(0), N->getOperand(1), TrueVal, FalseVal,
1994                      N->getOperand(4));
1995 }
1996
1997 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
1998 // float type.
1999 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
2000   EVT VT = N->getValueType(0);
2001   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
2002   return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, N->getOperand(0));
2003 }
2004
2005 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
2006   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2007                                                N->getValueType(0)));
2008 }
2009