LegalizeFloatTypes.cpp revision 452911c468a8d3b7571a3eb9aeff37c3880adb94
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/CodeGen/PseudoSourceValue.h"
24#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26using namespace llvm;
27
28/// GetFPLibCall - Return the right libcall for the given floating point type.
29static RTLIB::Libcall GetFPLibCall(MVT VT,
30                                   RTLIB::Libcall Call_F32,
31                                   RTLIB::Libcall Call_F64,
32                                   RTLIB::Libcall Call_F80,
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::ppcf128 ? Call_PPCF128 :
39    RTLIB::UNKNOWN_LIBCALL;
40}
41
42//===----------------------------------------------------------------------===//
43//  Result Float to Integer Conversion.
44//===----------------------------------------------------------------------===//
45
46void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
47  DEBUG(cerr << "Soften float result " << ResNo << ": "; N->dump(&DAG);
48        cerr << "\n");
49  SDOperand R = SDOperand();
50
51  switch (N->getOpcode()) {
52  default:
53#ifndef NDEBUG
54    cerr << "SoftenFloatResult #" << ResNo << ": ";
55    N->dump(&DAG); cerr << "\n";
56#endif
57    assert(0 && "Do not know how to soften the result of this operator!");
58    abort();
59
60    case ISD::BIT_CONVERT: R = SoftenFloatRes_BIT_CONVERT(N); break;
61    case ISD::BUILD_PAIR:  R = SoftenFloatRes_BUILD_PAIR(N); break;
62    case ISD::ConstantFP:
63      R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
64      break;
65    case ISD::FCOPYSIGN:   R = SoftenFloatRes_FCOPYSIGN(N); break;
66    case ISD::FP_EXTEND:   R = SoftenFloatRes_FP_EXTEND(N); break;
67    case ISD::FP_ROUND:    R = SoftenFloatRes_FP_ROUND(N); break;
68    case ISD::LOAD:        R = SoftenFloatRes_LOAD(N); break;
69    case ISD::SELECT:      R = SoftenFloatRes_SELECT(N); break;
70    case ISD::SELECT_CC:   R = SoftenFloatRes_SELECT_CC(N); break;
71    case ISD::SINT_TO_FP:
72    case ISD::UINT_TO_FP:  R = SoftenFloatRes_XINT_TO_FP(N); break;
73
74    case ISD::FADD:  R = SoftenFloatRes_FADD(N); break;
75    case ISD::FMUL:  R = SoftenFloatRes_FMUL(N); break;
76    case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
77    case ISD::FSUB:  R = SoftenFloatRes_FSUB(N); break;
78  }
79
80  // If R is null, the sub-method took care of registering the result.
81  if (R.Val)
82    SetSoftenedFloat(SDOperand(N, ResNo), R);
83}
84
85SDOperand DAGTypeLegalizer::SoftenFloatRes_BIT_CONVERT(SDNode *N) {
86  return BitConvertToInteger(N->getOperand(0));
87}
88
89SDOperand DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
90  // Convert the inputs to integers, and build a new pair out of them.
91  return DAG.getNode(ISD::BUILD_PAIR,
92                     TLI.getTypeToTransformTo(N->getValueType(0)),
93                     BitConvertToInteger(N->getOperand(0)),
94                     BitConvertToInteger(N->getOperand(1)));
95}
96
97SDOperand DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
98  return DAG.getConstant(N->getValueAPF().convertToAPInt(),
99                         TLI.getTypeToTransformTo(N->getValueType(0)));
100}
101
102SDOperand DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
103  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
104  SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
105                       GetSoftenedFloat(N->getOperand(1)) };
106  return MakeLibCall(GetFPLibCall(N->getValueType(0),
107                                  RTLIB::ADD_F32,
108                                  RTLIB::ADD_F64,
109                                  RTLIB::ADD_F80,
110                                  RTLIB::ADD_PPCF128),
111                     NVT, Ops, 2, false);
112}
113
114SDOperand DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
115  SDOperand LHS = GetSoftenedFloat(N->getOperand(0));
116  SDOperand RHS = BitConvertToInteger(N->getOperand(1));
117
118  MVT LVT = LHS.getValueType();
119  MVT RVT = RHS.getValueType();
120
121  unsigned LSize = LVT.getSizeInBits();
122  unsigned RSize = RVT.getSizeInBits();
123
124  // First get the sign bit of second operand.
125  SDOperand SignBit = DAG.getNode(ISD::SHL, RVT, DAG.getConstant(1, RVT),
126                                  DAG.getConstant(RSize - 1,
127                                                  TLI.getShiftAmountTy()));
128  SignBit = DAG.getNode(ISD::AND, RVT, RHS, SignBit);
129
130  // Shift right or sign-extend it if the two operands have different types.
131  int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
132  if (SizeDiff > 0) {
133    SignBit = DAG.getNode(ISD::SRL, RVT, SignBit,
134                          DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
135    SignBit = DAG.getNode(ISD::TRUNCATE, LVT, SignBit);
136  } else if (SizeDiff < 0) {
137    SignBit = DAG.getNode(ISD::ANY_EXTEND, LVT, SignBit);
138    SignBit = DAG.getNode(ISD::SHL, LVT, SignBit,
139                          DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
140  }
141
142  // Clear the sign bit of the first operand.
143  SDOperand Mask = DAG.getNode(ISD::SHL, LVT, DAG.getConstant(1, LVT),
144                               DAG.getConstant(LSize - 1,
145                                               TLI.getShiftAmountTy()));
146  Mask = DAG.getNode(ISD::SUB, LVT, Mask, DAG.getConstant(1, LVT));
147  LHS = DAG.getNode(ISD::AND, LVT, LHS, Mask);
148
149  // Or the value with the sign bit.
150  return DAG.getNode(ISD::OR, LVT, LHS, SignBit);
151}
152
153SDOperand DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
154  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
155  SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
156                       GetSoftenedFloat(N->getOperand(1)) };
157  return MakeLibCall(GetFPLibCall(N->getValueType(0),
158                                  RTLIB::MUL_F32,
159                                  RTLIB::MUL_F64,
160                                  RTLIB::MUL_F80,
161                                  RTLIB::MUL_PPCF128),
162                     NVT, Ops, 2, false);
163}
164
165SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
166  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
167  SDOperand Op = N->getOperand(0);
168
169  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
170  switch (Op.getValueType().getSimpleVT()) {
171  default:
172    assert(false && "Unsupported FP_EXTEND!");
173  case MVT::f32:
174    switch (N->getValueType(0).getSimpleVT()) {
175    default:
176      assert(false && "Unsupported FP_EXTEND!");
177    case MVT::f64:
178      LC = RTLIB::FPEXT_F32_F64;
179    }
180  }
181
182  return MakeLibCall(LC, NVT, &Op, 1, false);
183}
184
185SDOperand DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
186  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
187  SDOperand Op = N->getOperand(0);
188
189  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
190  switch (Op.getValueType().getSimpleVT()) {
191  default:
192    assert(false && "Unsupported FP_ROUND!");
193  case MVT::f64:
194    switch (N->getValueType(0).getSimpleVT()) {
195    default:
196      assert(false && "Unsupported FP_ROUND!");
197    case MVT::f32:
198      LC = RTLIB::FPROUND_F64_F32;
199    }
200  }
201
202  return MakeLibCall(LC, NVT, &Op, 1, false);
203}
204
205SDOperand DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
206  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
207  SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
208  return MakeLibCall(GetFPLibCall(N->getValueType(0),
209                                  RTLIB::POWI_F32,
210                                  RTLIB::POWI_F64,
211                                  RTLIB::POWI_F80,
212                                  RTLIB::POWI_PPCF128),
213                     NVT, Ops, 2, false);
214}
215
216SDOperand DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
217  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
218  SDOperand Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
219                       GetSoftenedFloat(N->getOperand(1)) };
220  return MakeLibCall(GetFPLibCall(N->getValueType(0),
221                                  RTLIB::SUB_F32,
222                                  RTLIB::SUB_F64,
223                                  RTLIB::SUB_F80,
224                                  RTLIB::SUB_PPCF128),
225                     NVT, Ops, 2, false);
226}
227
228SDOperand DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
229  LoadSDNode *L = cast<LoadSDNode>(N);
230  MVT VT = N->getValueType(0);
231  MVT NVT = TLI.getTypeToTransformTo(VT);
232
233  SDOperand NewL;
234  if (L->getExtensionType() == ISD::NON_EXTLOAD) {
235    NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
236                       NVT, L->getChain(), L->getBasePtr(), L->getOffset(),
237                       L->getSrcValue(), L->getSrcValueOffset(), NVT,
238                       L->isVolatile(), L->getAlignment());
239    // Legalized the chain result - switch anything that used the old chain to
240    // use the new one.
241    ReplaceValueWith(SDOperand(N, 1), NewL.getValue(1));
242    return NewL;
243  }
244
245  // Do a non-extending load followed by FP_EXTEND.
246  NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
247                     L->getMemoryVT(), L->getChain(),
248                     L->getBasePtr(), L->getOffset(),
249                     L->getSrcValue(), L->getSrcValueOffset(),
250                     L->getMemoryVT(),
251                     L->isVolatile(), L->getAlignment());
252  // Legalized the chain result - switch anything that used the old chain to
253  // use the new one.
254  ReplaceValueWith(SDOperand(N, 1), NewL.getValue(1));
255  return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, VT, NewL));
256}
257
258SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
259  SDOperand LHS = GetSoftenedFloat(N->getOperand(1));
260  SDOperand RHS = GetSoftenedFloat(N->getOperand(2));
261  return DAG.getNode(ISD::SELECT, LHS.getValueType(), N->getOperand(0),LHS,RHS);
262}
263
264SDOperand DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
265  SDOperand LHS = GetSoftenedFloat(N->getOperand(2));
266  SDOperand RHS = GetSoftenedFloat(N->getOperand(3));
267  return DAG.getNode(ISD::SELECT_CC, LHS.getValueType(), N->getOperand(0),
268                     N->getOperand(1), LHS, RHS, N->getOperand(4));
269}
270
271SDOperand DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
272  bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
273  MVT DestVT = N->getValueType(0);
274  SDOperand Op = N->getOperand(0);
275
276  if (Op.getValueType() == MVT::i32) {
277    // simple 32-bit [signed|unsigned] integer to float/double expansion
278
279    // Get the stack frame index of a 8 byte buffer.
280    SDOperand StackSlot = DAG.CreateStackTemporary(MVT::f64);
281
282    // word offset constant for Hi/Lo address computation
283    SDOperand Offset =
284      DAG.getConstant(MVT(MVT::i32).getSizeInBits() / 8,
285                      TLI.getPointerTy());
286    // set up Hi and Lo (into buffer) address based on endian
287    SDOperand Hi = StackSlot;
288    SDOperand Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, Offset);
289    if (TLI.isLittleEndian())
290      std::swap(Hi, Lo);
291
292    // if signed map to unsigned space
293    SDOperand OpMapped;
294    if (isSigned) {
295      // constant used to invert sign bit (signed to unsigned mapping)
296      SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
297      OpMapped = DAG.getNode(ISD::XOR, MVT::i32, Op, SignBit);
298    } else {
299      OpMapped = Op;
300    }
301    // store the lo of the constructed double - based on integer input
302    SDOperand Store1 = DAG.getStore(DAG.getEntryNode(),
303                                    OpMapped, Lo, NULL, 0);
304    // initial hi portion of constructed double
305    SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
306    // store the hi of the constructed double - biased exponent
307    SDOperand Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
308    // load the constructed double
309    SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
310    // FP constant to bias correct the final result
311    SDOperand Bias = DAG.getConstantFP(isSigned ?
312                                            BitsToDouble(0x4330000080000000ULL)
313                                          : BitsToDouble(0x4330000000000000ULL),
314                                     MVT::f64);
315    // subtract the bias
316    SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
317    // final result
318    SDOperand Result;
319    // handle final rounding
320    if (DestVT == MVT::f64) {
321      // do nothing
322      Result = Sub;
323    } else if (DestVT.bitsLT(MVT::f64)) {
324      Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
325                           DAG.getIntPtrConstant(0));
326    } else if (DestVT.bitsGT(MVT::f64)) {
327      Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
328    }
329    return BitConvertToInteger(Result);
330  }
331  assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
332  SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op);
333
334  SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op), Op,
335                                   DAG.getConstant(0, Op.getValueType()),
336                                   ISD::SETLT);
337  SDOperand Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
338  SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
339                                    SignSet, Four, Zero);
340
341  // If the sign bit of the integer is set, the large number will be treated
342  // as a negative number.  To counteract this, the dynamic code adds an
343  // offset depending on the data type.
344  uint64_t FF;
345  switch (Op.getValueType().getSimpleVT()) {
346  default: assert(0 && "Unsupported integer type!");
347  case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
348  case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
349  case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
350  case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
351  }
352  if (TLI.isLittleEndian()) FF <<= 32;
353  static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
354
355  SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
356  CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
357  SDOperand FudgeInReg;
358  if (DestVT == MVT::f32)
359    FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
360                             PseudoSourceValue::getConstantPool(), 0);
361  else {
362    FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestVT,
363                                DAG.getEntryNode(), CPIdx,
364                                PseudoSourceValue::getConstantPool(), 0,
365                                MVT::f32);
366  }
367
368  return BitConvertToInteger(DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg));
369}
370
371
372//===----------------------------------------------------------------------===//
373//  Operand Float to Integer Conversion..
374//===----------------------------------------------------------------------===//
375
376bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
377  DEBUG(cerr << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
378        cerr << "\n");
379  SDOperand Res = SDOperand();
380
381  switch (N->getOpcode()) {
382  default:
383#ifndef NDEBUG
384    cerr << "SoftenFloatOperand Op #" << OpNo << ": ";
385    N->dump(&DAG); cerr << "\n";
386#endif
387    assert(0 && "Do not know how to soften this operator's operand!");
388    abort();
389
390  case ISD::BIT_CONVERT: Res = SoftenFloatOp_BIT_CONVERT(N); break;
391
392  case ISD::BR_CC:      Res = SoftenFloatOp_BR_CC(N); break;
393  case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
394  case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
395  case ISD::SELECT_CC:  Res = SoftenFloatOp_SELECT_CC(N); break;
396  case ISD::SETCC:      Res = SoftenFloatOp_SETCC(N); break;
397  case ISD::STORE:      Res = SoftenFloatOp_STORE(N, OpNo); break;
398  }
399
400  // If the result is null, the sub-method took care of registering results etc.
401  if (!Res.Val) return false;
402
403  // If the result is N, the sub-method updated N in place.  Check to see if any
404  // operands are new, and if so, mark them.
405  if (Res.Val == N) {
406    // Mark N as new and remark N and its operands.  This allows us to correctly
407    // revisit N if it needs another step of promotion and allows us to visit
408    // any new operands to N.
409    ReanalyzeNode(N);
410    return true;
411  }
412
413  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
414         "Invalid operand expansion");
415
416  ReplaceValueWith(SDOperand(N, 0), Res);
417  return false;
418}
419
420/// SoftenSetCCOperands - Soften the operands of a comparison.  This code is
421/// shared among BR_CC, SELECT_CC, and SETCC handlers.
422void DAGTypeLegalizer::SoftenSetCCOperands(SDOperand &NewLHS, SDOperand &NewRHS,
423                                           ISD::CondCode &CCCode) {
424  SDOperand LHSInt = GetSoftenedFloat(NewLHS);
425  SDOperand RHSInt = GetSoftenedFloat(NewRHS);
426  MVT VT = NewLHS.getValueType();
427
428  assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
429
430  // Expand into one or more soft-fp libcall(s).
431  RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
432  switch (CCCode) {
433  case ISD::SETEQ:
434  case ISD::SETOEQ:
435    LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
436    break;
437  case ISD::SETNE:
438  case ISD::SETUNE:
439    LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
440    break;
441  case ISD::SETGE:
442  case ISD::SETOGE:
443    LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
444    break;
445  case ISD::SETLT:
446  case ISD::SETOLT:
447    LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
448    break;
449  case ISD::SETLE:
450  case ISD::SETOLE:
451    LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
452    break;
453  case ISD::SETGT:
454  case ISD::SETOGT:
455    LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
456    break;
457  case ISD::SETUO:
458    LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
459    break;
460  case ISD::SETO:
461    LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
462    break;
463  default:
464    LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
465    switch (CCCode) {
466    case ISD::SETONE:
467      // SETONE = SETOLT | SETOGT
468      LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
469      // Fallthrough
470    case ISD::SETUGT:
471      LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
472      break;
473    case ISD::SETUGE:
474      LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
475      break;
476    case ISD::SETULT:
477      LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
478      break;
479    case ISD::SETULE:
480      LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
481      break;
482    case ISD::SETUEQ:
483      LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
484      break;
485    default: assert(false && "Do not know how to soften this setcc!");
486    }
487  }
488
489  MVT RetVT = MVT::i32; // FIXME: is this the correct return type?
490  SDOperand Ops[2] = { LHSInt, RHSInt };
491  NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/);
492  NewRHS = DAG.getConstant(0, RetVT);
493  CCCode = TLI.getCmpLibcallCC(LC1);
494  if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
495    SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
496                                NewLHS, NewRHS, DAG.getCondCode(CCCode));
497    NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/);
498    NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
499                         NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
500    NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
501    NewRHS = SDOperand();
502  }
503}
504
505SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
506  return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
507                     GetSoftenedFloat(N->getOperand(0)));
508}
509
510SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
511  SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
512  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
513  SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
514
515  // If SoftenSetCCOperands returned a scalar, we need to compare the result
516  // against zero to select between true and false values.
517  if (NewRHS.Val == 0) {
518    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
519    CCCode = ISD::SETNE;
520  }
521
522  // Update N to have the operands specified.
523  return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
524                                DAG.getCondCode(CCCode), NewLHS, NewRHS,
525                                N->getOperand(4));
526}
527
528SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
529  MVT SVT = N->getOperand(0).getValueType();
530  MVT RVT = N->getValueType(0);
531
532  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
533  switch (RVT.getSimpleVT()) {
534  case MVT::i32:
535    switch (SVT.getSimpleVT()) {
536    case MVT::f32:
537      LC = RTLIB::FPTOSINT_F32_I32;
538      break;
539    case MVT::f64:
540      LC = RTLIB::FPTOSINT_F64_I32;
541      break;
542    default:
543      break;
544    }
545    break;
546  case MVT::i64:
547    switch (SVT.getSimpleVT()) {
548    case MVT::f32:
549      LC = RTLIB::FPTOSINT_F32_I64;
550      break;
551    case MVT::f64:
552      LC = RTLIB::FPTOSINT_F64_I64;
553      break;
554    case MVT::f80:
555      LC = RTLIB::FPTOSINT_F80_I64;
556      break;
557    case MVT::ppcf128:
558      LC = RTLIB::FPTOSINT_PPCF128_I64;
559      break;
560    default:
561      break;
562    }
563    break;
564  case MVT::i128:
565    switch (SVT.getSimpleVT()) {
566    case MVT::f32:
567      LC = RTLIB::FPTOSINT_F32_I128;
568      break;
569    case MVT::f64:
570      LC = RTLIB::FPTOSINT_F64_I128;
571      break;
572    case MVT::f80:
573      LC = RTLIB::FPTOSINT_F80_I128;
574      break;
575    case MVT::ppcf128:
576      LC = RTLIB::FPTOSINT_PPCF128_I128;
577      break;
578    default:
579      break;
580    }
581    break;
582  default:
583    break;
584  }
585  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
586
587  SDOperand Op = GetSoftenedFloat(N->getOperand(0));
588  return MakeLibCall(LC, RVT, &Op, 1, false);
589}
590
591SDOperand DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
592  MVT SVT = N->getOperand(0).getValueType();
593  MVT RVT = N->getValueType(0);
594
595  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
596  switch (RVT.getSimpleVT()) {
597  case MVT::i32:
598    switch (SVT.getSimpleVT()) {
599    case MVT::f32:
600      LC = RTLIB::FPTOUINT_F32_I32;
601      break;
602    case MVT::f64:
603      LC = RTLIB::FPTOUINT_F64_I32;
604      break;
605    default:
606      break;
607    }
608    break;
609  case MVT::i64:
610    switch (SVT.getSimpleVT()) {
611    case MVT::f32:
612      LC = RTLIB::FPTOUINT_F32_I64;
613      break;
614    case MVT::f64:
615      LC = RTLIB::FPTOUINT_F64_I64;
616      break;
617    case MVT::f80:
618      LC = RTLIB::FPTOUINT_F80_I64;
619      break;
620    case MVT::ppcf128:
621      LC = RTLIB::FPTOUINT_PPCF128_I64;
622      break;
623    default:
624      break;
625    }
626    break;
627  case MVT::i128:
628    switch (SVT.getSimpleVT()) {
629    case MVT::f32:
630      LC = RTLIB::FPTOUINT_F32_I128;
631      break;
632    case MVT::f64:
633      LC = RTLIB::FPTOUINT_F64_I128;
634      break;
635    case MVT::f80:
636      LC = RTLIB::FPTOUINT_F80_I128;
637      break;
638    case MVT::ppcf128:
639      LC = RTLIB::FPTOUINT_PPCF128_I128;
640      break;
641    default:
642      break;
643    }
644    break;
645  default:
646    break;
647  }
648  assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
649
650  SDOperand Op = GetSoftenedFloat(N->getOperand(0));
651  return MakeLibCall(LC, RVT, &Op, 1, false);
652}
653
654SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
655  SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
656  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
657  SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
658
659  // If SoftenSetCCOperands returned a scalar, we need to compare the result
660  // against zero to select between true and false values.
661  if (NewRHS.Val == 0) {
662    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
663    CCCode = ISD::SETNE;
664  }
665
666  // Update N to have the operands specified.
667  return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
668                                N->getOperand(2), N->getOperand(3),
669                                DAG.getCondCode(CCCode));
670}
671
672SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
673  SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
674  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
675  SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
676
677  // If SoftenSetCCOperands returned a scalar, use it.
678  if (NewRHS.Val == 0) {
679    assert(NewLHS.getValueType() == N->getValueType(0) &&
680           "Unexpected setcc expansion!");
681    return NewLHS;
682  }
683
684  // Otherwise, update N to have the operands specified.
685  return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
686                                DAG.getCondCode(CCCode));
687}
688
689SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
690  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
691  assert(OpNo == 1 && "Can only soften the stored value!");
692  StoreSDNode *ST = cast<StoreSDNode>(N);
693  SDOperand Val = ST->getValue();
694
695  if (ST->isTruncatingStore())
696    // Do an FP_ROUND followed by a non-truncating store.
697    Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
698                                          Val, DAG.getIntPtrConstant(0)));
699  else
700    Val = GetSoftenedFloat(Val);
701
702  return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
703                      ST->getSrcValue(), ST->getSrcValueOffset(),
704                      ST->isVolatile(), ST->getAlignment());
705}
706
707
708//===----------------------------------------------------------------------===//
709//  Float Result Expansion
710//===----------------------------------------------------------------------===//
711
712/// ExpandFloatResult - This method is called when the specified result of the
713/// specified node is found to need expansion.  At this point, the node may also
714/// have invalid operands or may have other results that need promotion, we just
715/// know that (at least) one result needs expansion.
716void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
717  DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
718  SDOperand Lo, Hi;
719  Lo = Hi = SDOperand();
720
721  // See if the target wants to custom expand this node.
722  if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
723      TargetLowering::Custom) {
724    // If the target wants to, allow it to lower this itself.
725    if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
726      // Everything that once used N now uses P.  We are guaranteed that the
727      // result value types of N and the result value types of P match.
728      ReplaceNodeWith(N, P);
729      return;
730    }
731  }
732
733  switch (N->getOpcode()) {
734  default:
735#ifndef NDEBUG
736    cerr << "ExpandFloatResult #" << ResNo << ": ";
737    N->dump(&DAG); cerr << "\n";
738#endif
739    assert(0 && "Do not know how to expand the result of this operator!");
740    abort();
741
742  case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
743  case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
744  case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
745  case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
746
747  case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
748  case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
749  case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
750  case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
751
752  case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
753  case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
754  case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
755  case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
756  case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
757  case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
758  case ISD::SINT_TO_FP:
759  case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
760  }
761
762  // If Lo/Hi is null, the sub-method took care of registering results etc.
763  if (Lo.Val)
764    SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
765}
766
767void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
768                                                 SDOperand &Hi) {
769  MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
770  assert(NVT.getSizeInBits() == integerPartWidth &&
771         "Do not know how to expand this float constant!");
772  APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
773  Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
774                                       &C.getRawData()[1])), NVT);
775  Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
776                                       &C.getRawData()[0])), NVT);
777}
778
779void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
780                                           SDOperand &Hi) {
781  SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
782  SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
783                                            RTLIB::ADD_F32,
784                                            RTLIB::ADD_F64,
785                                            RTLIB::ADD_F80,
786                                            RTLIB::ADD_PPCF128),
787                               N->getValueType(0), Ops, 2,
788                               false);
789  assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
790  Lo = Call.getOperand(0); Hi = Call.getOperand(1);
791}
792
793void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
794                                           SDOperand &Hi) {
795  SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
796  SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
797                                            RTLIB::DIV_F32,
798                                            RTLIB::DIV_F64,
799                                            RTLIB::DIV_F80,
800                                            RTLIB::DIV_PPCF128),
801                               N->getValueType(0), Ops, 2,
802                               false);
803  assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
804  Lo = Call.getOperand(0); Hi = Call.getOperand(1);
805}
806
807void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
808                                           SDOperand &Hi) {
809  SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
810  SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
811                                            RTLIB::MUL_F32,
812                                            RTLIB::MUL_F64,
813                                            RTLIB::MUL_F80,
814                                            RTLIB::MUL_PPCF128),
815                               N->getValueType(0), Ops, 2,
816                               false);
817  assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
818  Lo = Call.getOperand(0); Hi = Call.getOperand(1);
819}
820
821void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
822                                           SDOperand &Hi) {
823  SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
824  SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
825                                            RTLIB::SUB_F32,
826                                            RTLIB::SUB_F64,
827                                            RTLIB::SUB_F80,
828                                            RTLIB::SUB_PPCF128),
829                               N->getValueType(0), Ops, 2,
830                               false);
831  assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
832  Lo = Call.getOperand(0); Hi = Call.getOperand(1);
833}
834
835void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
836                                           SDOperand &Hi) {
837  if (ISD::isNormalLoad(N)) {
838    ExpandRes_NormalLoad(N, Lo, Hi);
839    return;
840  }
841
842  assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
843  LoadSDNode *LD = cast<LoadSDNode>(N);
844  SDOperand Chain = LD->getChain();
845  SDOperand Ptr = LD->getBasePtr();
846
847  MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
848  assert(NVT.isByteSized() && "Expanded type not byte sized!");
849  assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
850
851  Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
852                      LD->getSrcValue(), LD->getSrcValueOffset(),
853                      LD->getMemoryVT(),
854                      LD->isVolatile(), LD->getAlignment());
855
856  // Remember the chain.
857  Chain = Lo.getValue(1);
858
859  // The high part is undefined.
860  Hi = DAG.getNode(ISD::UNDEF, NVT);
861
862  // Modified the chain - switch anything that used the old chain to use the
863  // new one.
864  ReplaceValueWith(SDOperand(LD, 1), Chain);
865}
866
867void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
868                                                 SDOperand &Hi) {
869  assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
870  MVT VT = N->getValueType(0);
871  MVT NVT = TLI.getTypeToTransformTo(VT);
872  SDOperand Src = N->getOperand(0);
873  MVT SrcVT = Src.getValueType();
874
875  // First do an SINT_TO_FP, whether the original was signed or unsigned.
876  if (SrcVT.bitsLE(MVT::i32)) {
877    // The integer can be represented exactly in an f64.
878    Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
879    Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
880    Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
881  } else {
882    RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
883    if (SrcVT.bitsLE(MVT::i64)) {
884      Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
885      LC = RTLIB::SINTTOFP_I64_PPCF128;
886    } else if (SrcVT.bitsLE(MVT::i128)) {
887      Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
888      LC = RTLIB::SINTTOFP_I128_PPCF128;
889    }
890    assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
891
892    Hi = MakeLibCall(LC, VT, &Src, 1, true);
893    assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
894    Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
895  }
896
897  if (N->getOpcode() == ISD::SINT_TO_FP)
898    return;
899
900  // Unsigned - fix up the SINT_TO_FP value just calculated.
901  Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
902  SrcVT = Src.getValueType();
903
904  // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
905  static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
906  static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
907  static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
908  const uint64_t *Parts = 0;
909
910  switch (SrcVT.getSimpleVT()) {
911  default:
912    assert(false && "Unsupported UINT_TO_FP!");
913  case MVT::i32:
914    Parts = TwoE32;
915  case MVT::i64:
916    Parts = TwoE64;
917  case MVT::i128:
918    Parts = TwoE128;
919  }
920
921  Lo = DAG.getNode(ISD::FADD, VT, Hi,
922                   DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
923                                     MVT::ppcf128));
924  Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
925                   DAG.getCondCode(ISD::SETLT));
926  Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
927                   DAG.getConstant(1, TLI.getPointerTy()));
928  Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
929                   DAG.getConstant(0, TLI.getPointerTy()));
930}
931
932
933//===----------------------------------------------------------------------===//
934//  Float Operand Expansion
935//===----------------------------------------------------------------------===//
936
937/// ExpandFloatOperand - This method is called when the specified operand of the
938/// specified node is found to need expansion.  At this point, all of the result
939/// types of the node are known to be legal, but other operands of the node may
940/// need promotion or expansion as well as the specified one.
941bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
942  DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
943  SDOperand Res = SDOperand();
944
945  if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
946      == TargetLowering::Custom)
947    Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
948
949  if (Res.Val == 0) {
950    switch (N->getOpcode()) {
951    default:
952  #ifndef NDEBUG
953      cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
954      N->dump(&DAG); cerr << "\n";
955  #endif
956      assert(0 && "Do not know how to expand this operator's operand!");
957      abort();
958
959    case ISD::BIT_CONVERT:     Res = ExpandOp_BIT_CONVERT(N); break;
960    case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
961    case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
962
963    case ISD::BR_CC:     Res = ExpandFloatOp_BR_CC(N); break;
964    case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
965    case ISD::SETCC:     Res = ExpandFloatOp_SETCC(N); break;
966
967    case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
968    case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
969    case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
970
971    case ISD::STORE:
972      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
973      break;
974    }
975  }
976
977  // If the result is null, the sub-method took care of registering results etc.
978  if (!Res.Val) return false;
979  // If the result is N, the sub-method updated N in place.  Check to see if any
980  // operands are new, and if so, mark them.
981  if (Res.Val == N) {
982    // Mark N as new and remark N and its operands.  This allows us to correctly
983    // revisit N if it needs another step of expansion and allows us to visit
984    // any new operands to N.
985    ReanalyzeNode(N);
986    return true;
987  }
988
989  assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
990         "Invalid operand expansion");
991
992  ReplaceValueWith(SDOperand(N, 0), Res);
993  return false;
994}
995
996/// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
997/// is shared among BR_CC, SELECT_CC, and SETCC handlers.
998void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
999                                                SDOperand &NewRHS,
1000                                                ISD::CondCode &CCCode) {
1001  SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1002  GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1003  GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1004
1005  MVT VT = NewLHS.getValueType();
1006  assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1007
1008  // FIXME:  This generated code sucks.  We want to generate
1009  //         FCMP crN, hi1, hi2
1010  //         BNE crN, L:
1011  //         FCMP crN, lo1, lo2
1012  // The following can be improved, but not that much.
1013  SDOperand Tmp1, Tmp2, Tmp3;
1014  Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
1015  Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
1016  Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1017  Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
1018  Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
1019  Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
1020  NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
1021  NewRHS = SDOperand();   // LHS is the result, not a compare.
1022}
1023
1024SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1025  SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1026  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1027  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1028
1029  // If ExpandSetCCOperands returned a scalar, we need to compare the result
1030  // against zero to select between true and false values.
1031  if (NewRHS.Val == 0) {
1032    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1033    CCCode = ISD::SETNE;
1034  }
1035
1036  // Update N to have the operands specified.
1037  return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
1038                                DAG.getCondCode(CCCode), NewLHS, NewRHS,
1039                                N->getOperand(4));
1040}
1041
1042SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1043  SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1044  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1045  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1046
1047  // If ExpandSetCCOperands returned a scalar, we need to compare the result
1048  // against zero to select between true and false values.
1049  if (NewRHS.Val == 0) {
1050    NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1051    CCCode = ISD::SETNE;
1052  }
1053
1054  // Update N to have the operands specified.
1055  return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1056                                N->getOperand(2), N->getOperand(3),
1057                                DAG.getCondCode(CCCode));
1058}
1059
1060SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1061  SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1062  ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1063  FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
1064
1065  // If ExpandSetCCOperands returned a scalar, use it.
1066  if (NewRHS.Val == 0) {
1067    assert(NewLHS.getValueType() == N->getValueType(0) &&
1068           "Unexpected setcc expansion!");
1069    return NewLHS;
1070  }
1071
1072  // Otherwise, update N to have the operands specified.
1073  return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
1074                                DAG.getCondCode(CCCode));
1075}
1076
1077SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1078  assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1079         "Unsupported FP_TO_UINT!");
1080
1081  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1082  switch (N->getValueType(0).getSimpleVT()) {
1083  default:
1084    assert(false && "Unsupported FP_TO_UINT!");
1085  case MVT::i32:
1086    LC = RTLIB::FPTOUINT_PPCF128_I32;
1087    break;
1088  case MVT::i64:
1089    LC = RTLIB::FPTOUINT_PPCF128_I64;
1090    break;
1091  case MVT::i128:
1092    LC = RTLIB::FPTOUINT_PPCF128_I128;
1093    break;
1094  }
1095
1096  return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
1097}
1098
1099SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1100  assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1101         "Unsupported FP_TO_SINT!");
1102
1103  RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1104  switch (N->getValueType(0).getSimpleVT()) {
1105  default:
1106    assert(false && "Unsupported FP_TO_SINT!");
1107  case MVT::i32:
1108    LC = RTLIB::FPTOSINT_PPCF128_I32;
1109  case MVT::i64:
1110    LC = RTLIB::FPTOSINT_PPCF128_I64;
1111    break;
1112  case MVT::i128:
1113    LC = RTLIB::FPTOSINT_PPCF128_I64;
1114    break;
1115  }
1116
1117  return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
1118}
1119
1120SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1121  assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1122         "Logic only correct for ppcf128!");
1123  SDOperand Lo, Hi;
1124  GetExpandedFloat(N->getOperand(0), Lo, Hi);
1125  // Round it the rest of the way (e.g. to f32) if needed.
1126  return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
1127}
1128
1129SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1130  if (ISD::isNormalStore(N))
1131    return ExpandOp_NormalStore(N, OpNo);
1132
1133  assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1134  assert(OpNo == 1 && "Can only expand the stored value so far");
1135  StoreSDNode *ST = cast<StoreSDNode>(N);
1136
1137  SDOperand Chain = ST->getChain();
1138  SDOperand Ptr = ST->getBasePtr();
1139
1140  MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
1141  assert(NVT.isByteSized() && "Expanded type not byte sized!");
1142  assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1143
1144  SDOperand Lo, Hi;
1145  GetExpandedOp(ST->getValue(), Lo, Hi);
1146
1147  return DAG.getTruncStore(Chain, Lo, Ptr,
1148                           ST->getSrcValue(), ST->getSrcValueOffset(),
1149                           ST->getMemoryVT(),
1150                           ST->isVolatile(), ST->getAlignment());
1151}
1152