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