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