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