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