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