LegalizeFloatTypes.cpp revision 155615d7dc4e4fbfd3b7273720a76356468edf46
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"
25using namespace llvm;
26
27/// GetFPLibCall - Return the right libcall for the given floating point type.
28static RTLIB::Libcall GetFPLibCall(EVT VT,
29                                   RTLIB::Libcall Call_F32,
30                                   RTLIB::Libcall Call_F64,
31                                   RTLIB::Libcall Call_F80,
32                                   RTLIB::Libcall Call_F128,
33                                   RTLIB::Libcall Call_PPCF128) {
34  return
35    VT == MVT::f32 ? Call_F32 :
36    VT == MVT::f64 ? Call_F64 :
37    VT == MVT::f80 ? Call_F80 :
38    VT == MVT::f128 ? Call_F128 :
39    VT == MVT::ppcf128 ? Call_PPCF128 :
40    RTLIB::UNKNOWN_LIBCALL;
41}
42
43//===----------------------------------------------------------------------===//
44//  Result Float to Integer Conversion.
45//===----------------------------------------------------------------------===//
46
47void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
48  DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
49        dbgs() << "\n");
50  SDValue R = SDValue();
51
52  switch (N->getOpcode()) {
53  default:
54#ifndef NDEBUG
55    dbgs() << "SoftenFloatResult #" << ResNo << ": ";
56    N->dump(&DAG); dbgs() << "\n";
57#endif
58    llvm_unreachable("Do not know how to soften the result of this operator!");
59
60    case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
61    case ISD::BITCAST:     R = SoftenFloatRes_BITCAST(N); break;
62    case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
63    case ISD::ConstantFP:
64      R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
65      break;
66    case ISD::EXTRACT_VECTOR_ELT:
67      R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
68    case ISD::FABS:        R = SoftenFloatRes_FABS(N); break;
69    case ISD::FADD:        R = SoftenFloatRes_FADD(N); break;
70    case ISD::FCEIL:       R = SoftenFloatRes_FCEIL(N); break;
71    case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
72    case ISD::FCOS:        R = SoftenFloatRes_FCOS(N); break;
73    case ISD::FDIV:        R = SoftenFloatRes_FDIV(N); break;
74    case ISD::FEXP:        R = SoftenFloatRes_FEXP(N); break;
75    case ISD::FEXP2:       R = SoftenFloatRes_FEXP2(N); break;
76    case ISD::FFLOOR:      R = SoftenFloatRes_FFLOOR(N); break;
77    case ISD::FLOG:        R = SoftenFloatRes_FLOG(N); break;
78    case ISD::FLOG2:       R = SoftenFloatRes_FLOG2(N); break;
79    case ISD::FLOG10:      R = SoftenFloatRes_FLOG10(N); break;
80    case ISD::FMA:         R = SoftenFloatRes_FMA(N); break;
81    case ISD::FMUL:        R = SoftenFloatRes_FMUL(N); break;
82    case ISD::FNEARBYINT:  R = SoftenFloatRes_FNEARBYINT(N); break;
83    case ISD::FNEG:        R = SoftenFloatRes_FNEG(N); break;
84    case ISD::FP_EXTEND:   R = SoftenFloatRes_FP_EXTEND(N); break;
85    case ISD::FP_ROUND:    R = SoftenFloatRes_FP_ROUND(N); break;
86    case ISD::FP16_TO_FP32:R = SoftenFloatRes_FP16_TO_FP32(N); break;
87    case ISD::FPOW:        R = SoftenFloatRes_FPOW(N); break;
88    case ISD::FPOWI:       R = SoftenFloatRes_FPOWI(N); break;
89    case ISD::FREM:        R = SoftenFloatRes_FREM(N); break;
90    case ISD::FRINT:       R = SoftenFloatRes_FRINT(N); break;
91    case ISD::FSIN:        R = SoftenFloatRes_FSIN(N); break;
92    case ISD::FSQRT:       R = SoftenFloatRes_FSQRT(N); break;
93    case ISD::FSUB:        R = SoftenFloatRes_FSUB(N); break;
94    case ISD::FTRUNC:      R = SoftenFloatRes_FTRUNC(N); break;
95    case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
96    case ISD::SELECT:      R = SoftenFloatRes_SELECT(N); break;
97    case ISD::SELECT_CC:   R = SoftenFloatRes_SELECT_CC(N); break;
98    case ISD::SINT_TO_FP:
99    case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
100    case ISD::UNDEF:       R = SoftenFloatRes_UNDEF(N); break;
101    case ISD::VAARG:       R = SoftenFloatRes_VAARG(N); break;
102  }
103
104  // If R is null, the sub-method took care of registering the result.
105  if (R.getNode())
106    SetSoftenedFloat(SDValue(N, ResNo), R);
107}
108
109SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
110  return BitConvertToInteger(N->getOperand(0));
111}
112
113SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
114                                                      unsigned ResNo) {
115  SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
116  return BitConvertToInteger(Op);
117}
118
119SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
120  // Convert the inputs to integers, and build a new pair out of them.
121  return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
122                     TLI.getTypeToTransformTo(*DAG.getContext(),
123                                              N->getValueType(0)),
124                     BitConvertToInteger(N->getOperand(0)),
125                     BitConvertToInteger(N->getOperand(1)));
126}
127
128SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
129  return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
130                         TLI.getTypeToTransformTo(*DAG.getContext(),
131                                                  N->getValueType(0)));
132}
133
134SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
135  SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
136  return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
137                     NewOp.getValueType().getVectorElementType(),
138                     NewOp, N->getOperand(1));
139}
140
141SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
142  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
143  unsigned Size = NVT.getSizeInBits();
144
145  // Mask = ~(1 << (Size-1))
146  APInt API = APInt::getAllOnesValue(Size);
147  API.clearBit(Size-1);
148  SDValue Mask = DAG.getConstant(API, NVT);
149  SDValue Op = GetSoftenedFloat(N->getOperand(0));
150  return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
151}
152
153SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
154  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
155  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
156                     GetSoftenedFloat(N->getOperand(1)) };
157  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
158                                           RTLIB::ADD_F32,
159                                           RTLIB::ADD_F64,
160                                           RTLIB::ADD_F80,
161                                           RTLIB::ADD_F128,
162                                           RTLIB::ADD_PPCF128),
163                         NVT, Ops, 2, false, SDLoc(N));
164}
165
166SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
167  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
168  SDValue Op = GetSoftenedFloat(N->getOperand(0));
169  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
170                                           RTLIB::CEIL_F32,
171                                           RTLIB::CEIL_F64,
172                                           RTLIB::CEIL_F80,
173                                           RTLIB::CEIL_F128,
174                                           RTLIB::CEIL_PPCF128),
175                         NVT, &Op, 1, false, SDLoc(N));
176}
177
178SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
179  SDValue LHS = GetSoftenedFloat(N->getOperand(0));
180  SDValue RHS = BitConvertToInteger(N->getOperand(1));
181  SDLoc dl(N);
182
183  EVT LVT = LHS.getValueType();
184  EVT RVT = RHS.getValueType();
185
186  unsigned LSize = LVT.getSizeInBits();
187  unsigned RSize = RVT.getSizeInBits();
188
189  // First get the sign bit of second operand.
190  SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, RVT),
191                                  DAG.getConstant(RSize - 1,
192                                                  TLI.getShiftAmountTy(RVT)));
193  SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
194
195  // Shift right or sign-extend it if the two operands have different types.
196  int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
197  if (SizeDiff > 0) {
198    SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
199                          DAG.getConstant(SizeDiff,
200                                 TLI.getShiftAmountTy(SignBit.getValueType())));
201    SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
202  } else if (SizeDiff < 0) {
203    SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
204    SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
205                          DAG.getConstant(-SizeDiff,
206                                 TLI.getShiftAmountTy(SignBit.getValueType())));
207  }
208
209  // Clear the sign bit of the first operand.
210  SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
211                               DAG.getConstant(LSize - 1,
212                                               TLI.getShiftAmountTy(LVT)));
213  Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
214  LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
215
216  // Or the value with the sign bit.
217  return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
218}
219
220SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
221  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
222  SDValue Op = GetSoftenedFloat(N->getOperand(0));
223  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
224                                           RTLIB::COS_F32,
225                                           RTLIB::COS_F64,
226                                           RTLIB::COS_F80,
227                                           RTLIB::COS_F128,
228                                           RTLIB::COS_PPCF128),
229                         NVT, &Op, 1, false, SDLoc(N));
230}
231
232SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
233  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
234  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
235                     GetSoftenedFloat(N->getOperand(1)) };
236  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
237                                           RTLIB::DIV_F32,
238                                           RTLIB::DIV_F64,
239                                           RTLIB::DIV_F80,
240                                           RTLIB::DIV_F128,
241                                           RTLIB::DIV_PPCF128),
242                         NVT, Ops, 2, false, SDLoc(N));
243}
244
245SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
246  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
247  SDValue Op = GetSoftenedFloat(N->getOperand(0));
248  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
249                                           RTLIB::EXP_F32,
250                                           RTLIB::EXP_F64,
251                                           RTLIB::EXP_F80,
252                                           RTLIB::EXP_F128,
253                                           RTLIB::EXP_PPCF128),
254                         NVT, &Op, 1, false, SDLoc(N));
255}
256
257SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
258  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
259  SDValue Op = GetSoftenedFloat(N->getOperand(0));
260  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
261                                           RTLIB::EXP2_F32,
262                                           RTLIB::EXP2_F64,
263                                           RTLIB::EXP2_F80,
264                                           RTLIB::EXP2_F128,
265                                           RTLIB::EXP2_PPCF128),
266                         NVT, &Op, 1, false, SDLoc(N));
267}
268
269SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
270  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
271  SDValue Op = GetSoftenedFloat(N->getOperand(0));
272  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
273                                           RTLIB::FLOOR_F32,
274                                           RTLIB::FLOOR_F64,
275                                           RTLIB::FLOOR_F80,
276                                           RTLIB::FLOOR_F128,
277                                           RTLIB::FLOOR_PPCF128),
278                         NVT, &Op, 1, false, SDLoc(N));
279}
280
281SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
282  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
283  SDValue Op = GetSoftenedFloat(N->getOperand(0));
284  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
285                                           RTLIB::LOG_F32,
286                                           RTLIB::LOG_F64,
287                                           RTLIB::LOG_F80,
288                                           RTLIB::LOG_F128,
289                                           RTLIB::LOG_PPCF128),
290                         NVT, &Op, 1, false, SDLoc(N));
291}
292
293SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
294  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
295  SDValue Op = GetSoftenedFloat(N->getOperand(0));
296  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
297                                           RTLIB::LOG2_F32,
298                                           RTLIB::LOG2_F64,
299                                           RTLIB::LOG2_F80,
300                                           RTLIB::LOG2_F128,
301                                           RTLIB::LOG2_PPCF128),
302                         NVT, &Op, 1, false, SDLoc(N));
303}
304
305SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
306  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
307  SDValue Op = GetSoftenedFloat(N->getOperand(0));
308  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
309                                           RTLIB::LOG10_F32,
310                                           RTLIB::LOG10_F64,
311                                           RTLIB::LOG10_F80,
312                                           RTLIB::LOG10_F128,
313                                           RTLIB::LOG10_PPCF128),
314                         NVT, &Op, 1, false, SDLoc(N));
315}
316
317SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
318  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
319  SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
320                     GetSoftenedFloat(N->getOperand(1)),
321                     GetSoftenedFloat(N->getOperand(2)) };
322  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
323                                           RTLIB::FMA_F32,
324                                           RTLIB::FMA_F64,
325                                           RTLIB::FMA_F80,
326                                           RTLIB::FMA_F128,
327                                           RTLIB::FMA_PPCF128),
328                         NVT, Ops, 3, false, SDLoc(N));
329}
330
331SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
332  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
333  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
334                     GetSoftenedFloat(N->getOperand(1)) };
335  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
336                                           RTLIB::MUL_F32,
337                                           RTLIB::MUL_F64,
338                                           RTLIB::MUL_F80,
339                                           RTLIB::MUL_F128,
340                                           RTLIB::MUL_PPCF128),
341                         NVT, Ops, 2, false, SDLoc(N));
342}
343
344SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
345  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
346  SDValue Op = GetSoftenedFloat(N->getOperand(0));
347  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
348                                           RTLIB::NEARBYINT_F32,
349                                           RTLIB::NEARBYINT_F64,
350                                           RTLIB::NEARBYINT_F80,
351                                           RTLIB::NEARBYINT_F128,
352                                           RTLIB::NEARBYINT_PPCF128),
353                         NVT, &Op, 1, false, SDLoc(N));
354}
355
356SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
357  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
358  // Expand Y = FNEG(X) -> Y = SUB -0.0, X
359  SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
360                     GetSoftenedFloat(N->getOperand(0)) };
361  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
362                                           RTLIB::SUB_F32,
363                                           RTLIB::SUB_F64,
364                                           RTLIB::SUB_F80,
365                                           RTLIB::SUB_F128,
366                                           RTLIB::SUB_PPCF128),
367                         NVT, Ops, 2, false, SDLoc(N));
368}
369
370SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
371  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
372  SDValue Op = N->getOperand(0);
373  RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
374  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
375  return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N));
376}
377
378// FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
379// nodes?
380SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
381  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
382  SDValue Op = N->getOperand(0);
383  return TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
384                         SDLoc(N));
385}
386
387SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
388  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
389  SDValue Op = N->getOperand(0);
390  RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
391  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
392  return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N));
393}
394
395SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
396  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
397  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
398                     GetSoftenedFloat(N->getOperand(1)) };
399  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
400                                           RTLIB::POW_F32,
401                                           RTLIB::POW_F64,
402                                           RTLIB::POW_F80,
403                                           RTLIB::POW_F128,
404                                           RTLIB::POW_PPCF128),
405                         NVT, Ops, 2, false, SDLoc(N));
406}
407
408SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
409  assert(N->getOperand(1).getValueType() == MVT::i32 &&
410         "Unsupported power type!");
411  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
412  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
413  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
414                                           RTLIB::POWI_F32,
415                                           RTLIB::POWI_F64,
416                                           RTLIB::POWI_F80,
417                                           RTLIB::POWI_F128,
418                                           RTLIB::POWI_PPCF128),
419                         NVT, Ops, 2, false, SDLoc(N));
420}
421
422SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
423  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
424  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
425                     GetSoftenedFloat(N->getOperand(1)) };
426  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
427                                           RTLIB::REM_F32,
428                                           RTLIB::REM_F64,
429                                           RTLIB::REM_F80,
430                                           RTLIB::REM_F128,
431                                           RTLIB::REM_PPCF128),
432                         NVT, Ops, 2, false, SDLoc(N));
433}
434
435SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
436  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
437  SDValue Op = GetSoftenedFloat(N->getOperand(0));
438  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
439                                           RTLIB::RINT_F32,
440                                           RTLIB::RINT_F64,
441                                           RTLIB::RINT_F80,
442                                           RTLIB::RINT_F128,
443                                           RTLIB::RINT_PPCF128),
444                         NVT, &Op, 1, false, SDLoc(N));
445}
446
447SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
448  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
449  SDValue Op = GetSoftenedFloat(N->getOperand(0));
450  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
451                                           RTLIB::SIN_F32,
452                                           RTLIB::SIN_F64,
453                                           RTLIB::SIN_F80,
454                                           RTLIB::SIN_F128,
455                                           RTLIB::SIN_PPCF128),
456                         NVT, &Op, 1, false, SDLoc(N));
457}
458
459SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
460  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
461  SDValue Op = GetSoftenedFloat(N->getOperand(0));
462  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
463                                           RTLIB::SQRT_F32,
464                                           RTLIB::SQRT_F64,
465                                           RTLIB::SQRT_F80,
466                                           RTLIB::SQRT_F128,
467                                           RTLIB::SQRT_PPCF128),
468                         NVT, &Op, 1, false, SDLoc(N));
469}
470
471SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
472  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
473  SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
474                     GetSoftenedFloat(N->getOperand(1)) };
475  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
476                                           RTLIB::SUB_F32,
477                                           RTLIB::SUB_F64,
478                                           RTLIB::SUB_F80,
479                                           RTLIB::SUB_F128,
480                                           RTLIB::SUB_PPCF128),
481                         NVT, Ops, 2, false, SDLoc(N));
482}
483
484SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
485  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
486  SDValue Op = GetSoftenedFloat(N->getOperand(0));
487  return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
488                                           RTLIB::TRUNC_F32,
489                                           RTLIB::TRUNC_F64,
490                                           RTLIB::TRUNC_F80,
491                                           RTLIB::TRUNC_F128,
492                                           RTLIB::TRUNC_PPCF128),
493                         NVT, &Op, 1, false, SDLoc(N));
494}
495
496SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
497  LoadSDNode *L = cast<LoadSDNode>(N);
498  EVT VT = N->getValueType(0);
499  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
500  SDLoc dl(N);
501
502  SDValue NewL;
503  if (L->getExtensionType() == ISD::NON_EXTLOAD) {
504    NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
505                       NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
506                       L->getPointerInfo(), NVT, L->isVolatile(),
507                       L->isNonTemporal(), false, L->getAlignment());
508    // Legalized the chain result - switch anything that used the old chain to
509    // use the new one.
510    ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
511    return NewL;
512  }
513
514  // Do a non-extending load followed by FP_EXTEND.
515  NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
516                     L->getMemoryVT(), dl, L->getChain(),
517                     L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
518                     L->getMemoryVT(), L->isVolatile(),
519                     L->isNonTemporal(), false, L->getAlignment());
520  // Legalized the chain result - switch anything that used the old chain to
521  // use the new one.
522  ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
523  return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
524}
525
526SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
527  SDValue LHS = GetSoftenedFloat(N->getOperand(1));
528  SDValue RHS = GetSoftenedFloat(N->getOperand(2));
529  return DAG.getSelect(SDLoc(N),
530                       LHS.getValueType(), N->getOperand(0), LHS, RHS);
531}
532
533SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
534  SDValue LHS = GetSoftenedFloat(N->getOperand(2));
535  SDValue RHS = GetSoftenedFloat(N->getOperand(3));
536  return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
537                     LHS.getValueType(), N->getOperand(0),
538                     N->getOperand(1), LHS, RHS, N->getOperand(4));
539}
540
541SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
542  return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
543                                               N->getValueType(0)));
544}
545
546SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
547  SDValue Chain = N->getOperand(0); // Get the chain.
548  SDValue Ptr = N->getOperand(1); // Get the pointer.
549  EVT VT = N->getValueType(0);
550  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
551  SDLoc dl(N);
552
553  SDValue NewVAARG;
554  NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
555                          N->getConstantOperandVal(3));
556
557  // Legalized the chain result - switch anything that used the old chain to
558  // use the new one.
559  ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
560  return NewVAARG;
561}
562
563SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
564  bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
565  EVT SVT = N->getOperand(0).getValueType();
566  EVT RVT = N->getValueType(0);
567  EVT NVT = EVT();
568  SDLoc dl(N);
569
570  // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
571  // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
572  // match.  Look for an appropriate libcall.
573  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
574  for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
575       t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
576    NVT = (MVT::SimpleValueType)t;
577    // The source needs to big enough to hold the operand.
578    if (NVT.bitsGE(SVT))
579      LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
580  }
581  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
582
583  // Sign/zero extend the argument if the libcall takes a larger type.
584  SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
585                           NVT, N->getOperand(0));
586  return TLI.makeLibCall(DAG, LC,
587                         TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
588                         &Op, 1, false, dl);
589}
590
591
592//===----------------------------------------------------------------------===//
593//  Operand Float to Integer Conversion..
594//===----------------------------------------------------------------------===//
595
596bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
597  DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
598        dbgs() << "\n");
599  SDValue Res = SDValue();
600
601  switch (N->getOpcode()) {
602  default:
603#ifndef NDEBUG
604    dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
605    N->dump(&DAG); dbgs() << "\n";
606#endif
607    llvm_unreachable("Do not know how to soften this operator's operand!");
608
609  case ISD::BITCAST:     Res = SoftenFloatOp_BITCAST(N); break;
610  case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
611  case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
612  case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
613  case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
614  case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
615  case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
616  case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
617  case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
618  }
619
620  // If the result is null, the sub-method took care of registering results etc.
621  if (!Res.getNode()) return false;
622
623  // If the result is N, the sub-method updated N in place.  Tell the legalizer
624  // core about this.
625  if (Res.getNode() == N)
626    return true;
627
628  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
629         "Invalid operand expansion");
630
631  ReplaceValueWith(SDValue(N, 0), Res);
632  return false;
633}
634
635SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
636  return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
637                     GetSoftenedFloat(N->getOperand(0)));
638}
639
640SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
641  EVT SVT = N->getOperand(0).getValueType();
642  EVT RVT = N->getValueType(0);
643
644  RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
645  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
646
647  SDValue Op = GetSoftenedFloat(N->getOperand(0));
648  return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N));
649}
650
651SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
652  SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
653  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
654
655  EVT VT = NewLHS.getValueType();
656  NewLHS = GetSoftenedFloat(NewLHS);
657  NewRHS = GetSoftenedFloat(NewRHS);
658  TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
659
660  // If softenSetCCOperands returned a scalar, we need to compare the result
661  // against zero to select between true and false values.
662  if (NewRHS.getNode() == 0) {
663    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
664    CCCode = ISD::SETNE;
665  }
666
667  // Update N to have the operands specified.
668  return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
669                                DAG.getCondCode(CCCode), NewLHS, NewRHS,
670                                N->getOperand(4)),
671                 0);
672}
673
674SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
675  EVT RVT = N->getValueType(0);
676  RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
677  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
678  SDValue Op = GetSoftenedFloat(N->getOperand(0));
679  return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N));
680}
681
682SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
683  EVT RVT = N->getValueType(0);
684  RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
685  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
686  SDValue Op = GetSoftenedFloat(N->getOperand(0));
687  return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N));
688}
689
690SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
691  EVT RVT = N->getValueType(0);
692  RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
693  SDValue Op = GetSoftenedFloat(N->getOperand(0));
694  return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N));
695}
696
697SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
698  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
699  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
700
701  EVT VT = NewLHS.getValueType();
702  NewLHS = GetSoftenedFloat(NewLHS);
703  NewRHS = GetSoftenedFloat(NewRHS);
704  TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
705
706  // If softenSetCCOperands returned a scalar, we need to compare the result
707  // against zero to select between true and false values.
708  if (NewRHS.getNode() == 0) {
709    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
710    CCCode = ISD::SETNE;
711  }
712
713  // Update N to have the operands specified.
714  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
715                                N->getOperand(2), N->getOperand(3),
716                                DAG.getCondCode(CCCode)),
717                 0);
718}
719
720SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
721  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
722  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
723
724  EVT VT = NewLHS.getValueType();
725  NewLHS = GetSoftenedFloat(NewLHS);
726  NewRHS = GetSoftenedFloat(NewRHS);
727  TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
728
729  // If softenSetCCOperands returned a scalar, use it.
730  if (NewRHS.getNode() == 0) {
731    assert(NewLHS.getValueType() == N->getValueType(0) &&
732           "Unexpected setcc expansion!");
733    return NewLHS;
734  }
735
736  // Otherwise, update N to have the operands specified.
737  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
738                                DAG.getCondCode(CCCode)),
739                 0);
740}
741
742SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
743  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
744  assert(OpNo == 1 && "Can only soften the stored value!");
745  StoreSDNode *ST = cast<StoreSDNode>(N);
746  SDValue Val = ST->getValue();
747  SDLoc dl(N);
748
749  if (ST->isTruncatingStore())
750    // Do an FP_ROUND followed by a non-truncating store.
751    Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
752                                          Val, DAG.getIntPtrConstant(0)));
753  else
754    Val = GetSoftenedFloat(Val);
755
756  return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
757                      ST->getPointerInfo(),
758                      ST->isVolatile(), ST->isNonTemporal(),
759                      ST->getAlignment());
760}
761
762
763//===----------------------------------------------------------------------===//
764//  Float Result Expansion
765//===----------------------------------------------------------------------===//
766
767/// ExpandFloatResult - This method is called when the specified result of the
768/// specified node is found to need expansion.  At this point, the node may also
769/// have invalid operands or may have other results that need promotion, we just
770/// know that (at least) one result needs expansion.
771void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
772  DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
773  SDValue Lo, Hi;
774  Lo = Hi = SDValue();
775
776  // See if the target wants to custom expand this node.
777  if (CustomLowerNode(N, N->getValueType(ResNo), true))
778    return;
779
780  switch (N->getOpcode()) {
781  default:
782#ifndef NDEBUG
783    dbgs() << "ExpandFloatResult #" << ResNo << ": ";
784    N->dump(&DAG); dbgs() << "\n";
785#endif
786    llvm_unreachable("Do not know how to expand the result of this operator!");
787
788  case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
789  case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
790  case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
791
792  case ISD::MERGE_VALUES:       ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
793  case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
794  case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
795  case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
796  case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
797  case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
798
799  case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
800  case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
801  case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
802  case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
803  case ISD::FCOPYSIGN:  ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
804  case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
805  case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
806  case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
807  case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
808  case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
809  case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
810  case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
811  case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
812  case ISD::FMA:        ExpandFloatRes_FMA(N, Lo, Hi); break;
813  case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
814  case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
815  case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
816  case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
817  case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
818  case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
819  case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
820  case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
821  case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
822  case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
823  case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
824  case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
825  case ISD::SINT_TO_FP:
826  case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
827  case ISD::FREM:       ExpandFloatRes_FREM(N, Lo, Hi); break;
828  }
829
830  // If Lo/Hi is null, the sub-method took care of registering results etc.
831  if (Lo.getNode())
832    SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
833}
834
835void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
836                                                 SDValue &Hi) {
837  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
838  assert(NVT.getSizeInBits() == integerPartWidth &&
839         "Do not know how to expand this float constant!");
840  APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
841  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
842                                 APInt(integerPartWidth, C.getRawData()[1])),
843                         NVT);
844  Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
845                                 APInt(integerPartWidth, C.getRawData()[0])),
846                         NVT);
847}
848
849void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
850                                           SDValue &Hi) {
851  assert(N->getValueType(0) == MVT::ppcf128 &&
852         "Logic only correct for ppcf128!");
853  SDLoc dl(N);
854  SDValue Tmp;
855  GetExpandedFloat(N->getOperand(0), Lo, Tmp);
856  Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
857  // Lo = Hi==fabs(Hi) ? Lo : -Lo;
858  Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
859                   DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
860                   ISD::SETEQ);
861}
862
863void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
864                                           SDValue &Hi) {
865  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
866                                         RTLIB::ADD_F32, RTLIB::ADD_F64,
867                                         RTLIB::ADD_F80, RTLIB::ADD_F128,
868                                         RTLIB::ADD_PPCF128),
869                            N, false);
870  GetPairElements(Call, Lo, Hi);
871}
872
873void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
874                                            SDValue &Lo, SDValue &Hi) {
875  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
876                                         RTLIB::CEIL_F32, RTLIB::CEIL_F64,
877                                         RTLIB::CEIL_F80, RTLIB::CEIL_F128,
878                                         RTLIB::CEIL_PPCF128),
879                            N, false);
880  GetPairElements(Call, Lo, Hi);
881}
882
883void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
884                                                SDValue &Lo, SDValue &Hi) {
885  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
886                                         RTLIB::COPYSIGN_F32,
887                                         RTLIB::COPYSIGN_F64,
888                                         RTLIB::COPYSIGN_F80,
889                                         RTLIB::COPYSIGN_F128,
890                                         RTLIB::COPYSIGN_PPCF128),
891                            N, false);
892  GetPairElements(Call, Lo, Hi);
893}
894
895void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
896                                           SDValue &Lo, SDValue &Hi) {
897  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
898                                         RTLIB::COS_F32, RTLIB::COS_F64,
899                                         RTLIB::COS_F80, RTLIB::COS_F128,
900                                         RTLIB::COS_PPCF128),
901                            N, false);
902  GetPairElements(Call, Lo, Hi);
903}
904
905void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
906                                           SDValue &Hi) {
907  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
908  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
909                                                   RTLIB::DIV_F32,
910                                                   RTLIB::DIV_F64,
911                                                   RTLIB::DIV_F80,
912                                                   RTLIB::DIV_F128,
913                                                   RTLIB::DIV_PPCF128),
914                                 N->getValueType(0), Ops, 2, false,
915                                 SDLoc(N));
916  GetPairElements(Call, Lo, Hi);
917}
918
919void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
920                                           SDValue &Lo, SDValue &Hi) {
921  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
922                                         RTLIB::EXP_F32, RTLIB::EXP_F64,
923                                         RTLIB::EXP_F80, RTLIB::EXP_F128,
924                                         RTLIB::EXP_PPCF128),
925                            N, false);
926  GetPairElements(Call, Lo, Hi);
927}
928
929void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
930                                            SDValue &Lo, SDValue &Hi) {
931  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
932                                         RTLIB::EXP2_F32, RTLIB::EXP2_F64,
933                                         RTLIB::EXP2_F80, RTLIB::EXP2_F128,
934                                         RTLIB::EXP2_PPCF128),
935                            N, false);
936  GetPairElements(Call, Lo, Hi);
937}
938
939void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
940                                             SDValue &Lo, SDValue &Hi) {
941  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
942                                         RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
943                                         RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
944                                         RTLIB::FLOOR_PPCF128),
945                            N, false);
946  GetPairElements(Call, Lo, Hi);
947}
948
949void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
950                                           SDValue &Lo, SDValue &Hi) {
951  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
952                                         RTLIB::LOG_F32, RTLIB::LOG_F64,
953                                         RTLIB::LOG_F80, RTLIB::LOG_F128,
954                                         RTLIB::LOG_PPCF128),
955                            N, false);
956  GetPairElements(Call, Lo, Hi);
957}
958
959void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
960                                            SDValue &Lo, SDValue &Hi) {
961  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
962                                         RTLIB::LOG2_F32, RTLIB::LOG2_F64,
963                                         RTLIB::LOG2_F80, RTLIB::LOG2_F128,
964                                         RTLIB::LOG2_PPCF128),
965                            N, false);
966  GetPairElements(Call, Lo, Hi);
967}
968
969void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
970                                             SDValue &Lo, SDValue &Hi) {
971  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
972                                         RTLIB::LOG10_F32, RTLIB::LOG10_F64,
973                                         RTLIB::LOG10_F80, RTLIB::LOG10_F128,
974                                         RTLIB::LOG10_PPCF128),
975                            N, false);
976  GetPairElements(Call, Lo, Hi);
977}
978
979void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
980                                          SDValue &Hi) {
981  SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
982  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
983                                                   RTLIB::FMA_F32,
984                                                   RTLIB::FMA_F64,
985                                                   RTLIB::FMA_F80,
986                                                   RTLIB::FMA_F128,
987                                                   RTLIB::FMA_PPCF128),
988                                 N->getValueType(0), Ops, 3, false,
989                                 SDLoc(N));
990  GetPairElements(Call, Lo, Hi);
991}
992
993void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
994                                           SDValue &Hi) {
995  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
996  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
997                                                   RTLIB::MUL_F32,
998                                                   RTLIB::MUL_F64,
999                                                   RTLIB::MUL_F80,
1000                                                   RTLIB::MUL_F128,
1001                                                   RTLIB::MUL_PPCF128),
1002                                 N->getValueType(0), Ops, 2, false,
1003                                 SDLoc(N));
1004  GetPairElements(Call, Lo, Hi);
1005}
1006
1007void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1008                                                 SDValue &Lo, SDValue &Hi) {
1009  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1010                                         RTLIB::NEARBYINT_F32,
1011                                         RTLIB::NEARBYINT_F64,
1012                                         RTLIB::NEARBYINT_F80,
1013                                         RTLIB::NEARBYINT_F128,
1014                                         RTLIB::NEARBYINT_PPCF128),
1015                            N, false);
1016  GetPairElements(Call, Lo, Hi);
1017}
1018
1019void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1020                                           SDValue &Hi) {
1021  SDLoc dl(N);
1022  GetExpandedFloat(N->getOperand(0), Lo, Hi);
1023  Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1024  Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1025}
1026
1027void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1028                                                SDValue &Hi) {
1029  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1030  Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
1031  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1032                                 APInt(NVT.getSizeInBits(), 0)), NVT);
1033}
1034
1035void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1036                                           SDValue &Lo, SDValue &Hi) {
1037  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1038                                         RTLIB::POW_F32, RTLIB::POW_F64,
1039                                         RTLIB::POW_F80, RTLIB::POW_F128,
1040                                         RTLIB::POW_PPCF128),
1041                            N, false);
1042  GetPairElements(Call, Lo, Hi);
1043}
1044
1045void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1046                                            SDValue &Lo, SDValue &Hi) {
1047  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1048                                         RTLIB::POWI_F32, RTLIB::POWI_F64,
1049                                         RTLIB::POWI_F80, RTLIB::POWI_F128,
1050                                         RTLIB::POWI_PPCF128),
1051                            N, false);
1052  GetPairElements(Call, Lo, Hi);
1053}
1054
1055void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1056                                           SDValue &Lo, SDValue &Hi) {
1057  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1058                                         RTLIB::REM_F32, RTLIB::REM_F64,
1059                                         RTLIB::REM_F80, RTLIB::REM_F128,
1060                                         RTLIB::REM_PPCF128),
1061                            N, false);
1062  GetPairElements(Call, Lo, Hi);
1063}
1064
1065void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1066                                            SDValue &Lo, SDValue &Hi) {
1067  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1068                                         RTLIB::RINT_F32, RTLIB::RINT_F64,
1069                                         RTLIB::RINT_F80, RTLIB::RINT_F128,
1070                                         RTLIB::RINT_PPCF128),
1071                            N, false);
1072  GetPairElements(Call, Lo, Hi);
1073}
1074
1075void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1076                                           SDValue &Lo, SDValue &Hi) {
1077  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1078                                         RTLIB::SIN_F32, RTLIB::SIN_F64,
1079                                         RTLIB::SIN_F80, RTLIB::SIN_F128,
1080                                         RTLIB::SIN_PPCF128),
1081                            N, false);
1082  GetPairElements(Call, Lo, Hi);
1083}
1084
1085void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1086                                            SDValue &Lo, SDValue &Hi) {
1087  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1088                                         RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1089                                         RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1090                                         RTLIB::SQRT_PPCF128),
1091                            N, false);
1092  GetPairElements(Call, Lo, Hi);
1093}
1094
1095void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1096                                           SDValue &Hi) {
1097  SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1098  SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1099                                                   RTLIB::SUB_F32,
1100                                                   RTLIB::SUB_F64,
1101                                                   RTLIB::SUB_F80,
1102                                                   RTLIB::SUB_F128,
1103                                                   RTLIB::SUB_PPCF128),
1104                                 N->getValueType(0), Ops, 2, false,
1105                                 SDLoc(N));
1106  GetPairElements(Call, Lo, Hi);
1107}
1108
1109void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1110                                             SDValue &Lo, SDValue &Hi) {
1111  SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1112                                         RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1113                                         RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1114                                         RTLIB::TRUNC_PPCF128),
1115                            N, false);
1116  GetPairElements(Call, Lo, Hi);
1117}
1118
1119void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1120                                           SDValue &Hi) {
1121  if (ISD::isNormalLoad(N)) {
1122    ExpandRes_NormalLoad(N, Lo, Hi);
1123    return;
1124  }
1125
1126  assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1127  LoadSDNode *LD = cast<LoadSDNode>(N);
1128  SDValue Chain = LD->getChain();
1129  SDValue Ptr = LD->getBasePtr();
1130  SDLoc dl(N);
1131
1132  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1133  assert(NVT.isByteSized() && "Expanded type not byte sized!");
1134  assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1135
1136  Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1137                      LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
1138                      LD->isNonTemporal(), LD->getAlignment());
1139
1140  // Remember the chain.
1141  Chain = Hi.getValue(1);
1142
1143  // The low part is zero.
1144  Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1145                                 APInt(NVT.getSizeInBits(), 0)), NVT);
1146
1147  // Modified the chain - switch anything that used the old chain to use the
1148  // new one.
1149  ReplaceValueWith(SDValue(LD, 1), Chain);
1150}
1151
1152void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1153                                                 SDValue &Hi) {
1154  assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1155  EVT VT = N->getValueType(0);
1156  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1157  SDValue Src = N->getOperand(0);
1158  EVT SrcVT = Src.getValueType();
1159  bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1160  SDLoc dl(N);
1161
1162  // First do an SINT_TO_FP, whether the original was signed or unsigned.
1163  // When promoting partial word types to i32 we must honor the signedness,
1164  // though.
1165  if (SrcVT.bitsLE(MVT::i32)) {
1166    // The integer can be represented exactly in an f64.
1167    Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1168                      MVT::i32, Src);
1169    Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1170                                   APInt(NVT.getSizeInBits(), 0)), NVT);
1171    Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1172  } else {
1173    RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1174    if (SrcVT.bitsLE(MVT::i64)) {
1175      Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1176                        MVT::i64, Src);
1177      LC = RTLIB::SINTTOFP_I64_PPCF128;
1178    } else if (SrcVT.bitsLE(MVT::i128)) {
1179      Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1180      LC = RTLIB::SINTTOFP_I128_PPCF128;
1181    }
1182    assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1183
1184    Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl);
1185    GetPairElements(Hi, Lo, Hi);
1186  }
1187
1188  if (isSigned)
1189    return;
1190
1191  // Unsigned - fix up the SINT_TO_FP value just calculated.
1192  Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1193  SrcVT = Src.getValueType();
1194
1195  // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1196  static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
1197  static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
1198  static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1199  ArrayRef<uint64_t> Parts;
1200
1201  switch (SrcVT.getSimpleVT().SimpleTy) {
1202  default:
1203    llvm_unreachable("Unsupported UINT_TO_FP!");
1204  case MVT::i32:
1205    Parts = TwoE32;
1206    break;
1207  case MVT::i64:
1208    Parts = TwoE64;
1209    break;
1210  case MVT::i128:
1211    Parts = TwoE128;
1212    break;
1213  }
1214
1215  Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1216                   DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1217                                             APInt(128, Parts)),
1218                                     MVT::ppcf128));
1219  Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
1220                       Lo, Hi, ISD::SETLT);
1221  GetPairElements(Lo, Lo, Hi);
1222}
1223
1224
1225//===----------------------------------------------------------------------===//
1226//  Float Operand Expansion
1227//===----------------------------------------------------------------------===//
1228
1229/// ExpandFloatOperand - This method is called when the specified operand of the
1230/// specified node is found to need expansion.  At this point, all of the result
1231/// types of the node are known to be legal, but other operands of the node may
1232/// need promotion or expansion as well as the specified one.
1233bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1234  DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1235  SDValue Res = SDValue();
1236
1237  // See if the target wants to custom expand this node.
1238  if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1239    return false;
1240
1241  switch (N->getOpcode()) {
1242  default:
1243#ifndef NDEBUG
1244    dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1245    N->dump(&DAG); dbgs() << "\n";
1246#endif
1247    llvm_unreachable("Do not know how to expand this operator's operand!");
1248
1249  case ISD::BITCAST:         Res = ExpandOp_BITCAST(N); break;
1250  case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
1251  case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1252
1253  case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
1254  case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
1255  case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1256  case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1257  case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
1258  case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
1259  case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1260                                                  OpNo); break;
1261  }
1262
1263  // If the result is null, the sub-method took care of registering results etc.
1264  if (!Res.getNode()) return false;
1265
1266  // If the result is N, the sub-method updated N in place.  Tell the legalizer
1267  // core about this.
1268  if (Res.getNode() == N)
1269    return true;
1270
1271  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1272         "Invalid operand expansion");
1273
1274  ReplaceValueWith(SDValue(N, 0), Res);
1275  return false;
1276}
1277
1278/// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
1279/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1280void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1281                                                SDValue &NewRHS,
1282                                                ISD::CondCode &CCCode,
1283                                                SDLoc dl) {
1284  SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1285  GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1286  GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1287
1288  assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1289
1290  // FIXME:  This generated code sucks.  We want to generate
1291  //         FCMPU crN, hi1, hi2
1292  //         BNE crN, L:
1293  //         FCMPU crN, lo1, lo2
1294  // The following can be improved, but not that much.
1295  SDValue Tmp1, Tmp2, Tmp3;
1296  Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1297                      LHSHi, RHSHi, ISD::SETOEQ);
1298  Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1299                      LHSLo, RHSLo, CCCode);
1300  Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1301  Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1302                      LHSHi, RHSHi, ISD::SETUNE);
1303  Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1304                      LHSHi, RHSHi, CCCode);
1305  Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1306  NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1307  NewRHS = SDValue();   // LHS is the result, not a compare.
1308}
1309
1310SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1311  SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1312  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1313  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1314
1315  // If ExpandSetCCOperands returned a scalar, we need to compare the result
1316  // against zero to select between true and false values.
1317  if (NewRHS.getNode() == 0) {
1318    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1319    CCCode = ISD::SETNE;
1320  }
1321
1322  // Update N to have the operands specified.
1323  return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1324                                DAG.getCondCode(CCCode), NewLHS, NewRHS,
1325                                N->getOperand(4)), 0);
1326}
1327
1328SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1329  assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1330         "Logic only correct for ppcf128!");
1331  SDValue Lo, Hi;
1332  GetExpandedFloat(N->getOperand(0), Lo, Hi);
1333  // Round it the rest of the way (e.g. to f32) if needed.
1334  return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1335                     N->getValueType(0), Hi, N->getOperand(1));
1336}
1337
1338SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1339  EVT RVT = N->getValueType(0);
1340  SDLoc dl(N);
1341
1342  // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1343  // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1344  if (RVT == MVT::i32) {
1345    assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1346           "Logic only correct for ppcf128!");
1347    SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1348                              N->getOperand(0), DAG.getValueType(MVT::f64));
1349    Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1350                      DAG.getIntPtrConstant(1));
1351    return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1352  }
1353
1354  RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1355  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1356  return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl);
1357}
1358
1359SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1360  EVT RVT = N->getValueType(0);
1361  SDLoc dl(N);
1362
1363  // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1364  // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1365  if (RVT == MVT::i32) {
1366    assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1367           "Logic only correct for ppcf128!");
1368    const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1369    APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1370    SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1371    //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1372    // FIXME: generated code sucks.
1373    return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1374                           DAG.getNode(ISD::ADD, dl, MVT::i32,
1375                                       DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1376                                                   DAG.getNode(ISD::FSUB, dl,
1377                                                               MVT::ppcf128,
1378                                                               N->getOperand(0),
1379                                                               Tmp)),
1380                                       DAG.getConstant(0x80000000, MVT::i32)),
1381                           DAG.getNode(ISD::FP_TO_SINT, dl,
1382                                       MVT::i32, N->getOperand(0)),
1383                           ISD::SETGE);
1384  }
1385
1386  RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1387  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1388  return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1389                         false, dl);
1390}
1391
1392SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1393  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1394  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1395  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1396
1397  // If ExpandSetCCOperands returned a scalar, we need to compare the result
1398  // against zero to select between true and false values.
1399  if (NewRHS.getNode() == 0) {
1400    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1401    CCCode = ISD::SETNE;
1402  }
1403
1404  // Update N to have the operands specified.
1405  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1406                                N->getOperand(2), N->getOperand(3),
1407                                DAG.getCondCode(CCCode)), 0);
1408}
1409
1410SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1411  SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1412  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1413  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1414
1415  // If ExpandSetCCOperands returned a scalar, use it.
1416  if (NewRHS.getNode() == 0) {
1417    assert(NewLHS.getValueType() == N->getValueType(0) &&
1418           "Unexpected setcc expansion!");
1419    return NewLHS;
1420  }
1421
1422  // Otherwise, update N to have the operands specified.
1423  return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1424                                DAG.getCondCode(CCCode)), 0);
1425}
1426
1427SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1428  if (ISD::isNormalStore(N))
1429    return ExpandOp_NormalStore(N, OpNo);
1430
1431  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1432  assert(OpNo == 1 && "Can only expand the stored value so far");
1433  StoreSDNode *ST = cast<StoreSDNode>(N);
1434
1435  SDValue Chain = ST->getChain();
1436  SDValue Ptr = ST->getBasePtr();
1437
1438  EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1439                                     ST->getValue().getValueType());
1440  assert(NVT.isByteSized() && "Expanded type not byte sized!");
1441  assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1442  (void)NVT;
1443
1444  SDValue Lo, Hi;
1445  GetExpandedOp(ST->getValue(), Lo, Hi);
1446
1447  return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1448                           ST->getPointerInfo(),
1449                           ST->getMemoryVT(), ST->isVolatile(),
1450                           ST->isNonTemporal(), ST->getAlignment());
1451}
1452