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