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