520924d14e7839d186cd7bbe5a24f6a7b281b21d
[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   SDOperand Ops[2] = { LHSInt, RHSInt };
467   NewLHS = MakeLibCall(LC1, MVT::i32, Ops, 2, false/*sign irrelevant*/);
468   NewRHS = DAG.getConstant(0, MVT::i32);
469   CCCode = TLI.getCmpLibcallCC(LC1);
470   if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
471     SDOperand Tmp = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS),
472                                 NewLHS, NewRHS, DAG.getCondCode(CCCode));
473     NewLHS = MakeLibCall(LC2, MVT::i32, Ops, 2, false/*sign irrelevant*/);
474     NewLHS = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(NewLHS), NewLHS,
475                          NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
476     NewLHS = DAG.getNode(ISD::OR, Tmp.getValueType(), Tmp, NewLHS);
477     NewRHS = SDOperand();
478   }
479 }
480
481 SDOperand DAGTypeLegalizer::SoftenFloatOp_BIT_CONVERT(SDNode *N) {
482   return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0),
483                      GetSoftenedFloat(N->getOperand(0)));
484 }
485
486 SDOperand DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
487   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
488   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
489   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
490
491   // If SoftenSetCCOperands returned a scalar, we need to compare the result
492   // against zero to select between true and false values.
493   if (NewRHS.Val == 0) {
494     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
495     CCCode = ISD::SETNE;
496   }
497
498   // Update N to have the operands specified.
499   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
500                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
501                                 N->getOperand(4));
502 }
503
504 SDOperand DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
505   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
506   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
507   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
508
509   // If SoftenSetCCOperands returned a scalar, we need to compare the result
510   // against zero to select between true and false values.
511   if (NewRHS.Val == 0) {
512     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
513     CCCode = ISD::SETNE;
514   }
515
516   // Update N to have the operands specified.
517   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
518                                 N->getOperand(2), N->getOperand(3),
519                                 DAG.getCondCode(CCCode));
520 }
521
522 SDOperand DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
523   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
524   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
525   SoftenSetCCOperands(NewLHS, NewRHS, CCCode);
526
527   // If SoftenSetCCOperands returned a scalar, use it.
528   if (NewRHS.Val == 0) {
529     assert(NewLHS.getValueType() == N->getValueType(0) &&
530            "Unexpected setcc expansion!");
531     return NewLHS;
532   }
533
534   // Otherwise, update N to have the operands specified.
535   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
536                                 DAG.getCondCode(CCCode));
537 }
538
539 SDOperand DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
540   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
541   assert(OpNo == 1 && "Can only soften the stored value!");
542   StoreSDNode *ST = cast<StoreSDNode>(N);
543   SDOperand Val = ST->getValue();
544
545   if (ST->isTruncatingStore())
546     // Do an FP_ROUND followed by a non-truncating store.
547     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, ST->getMemoryVT(),
548                                           Val, DAG.getIntPtrConstant(0)));
549   else
550     Val = GetSoftenedFloat(Val);
551
552   return DAG.getStore(ST->getChain(), Val, ST->getBasePtr(),
553                       ST->getSrcValue(), ST->getSrcValueOffset(),
554                       ST->isVolatile(), ST->getAlignment());
555 }
556
557
558 //===----------------------------------------------------------------------===//
559 //  Float Result Expansion
560 //===----------------------------------------------------------------------===//
561
562 /// ExpandFloatResult - This method is called when the specified result of the
563 /// specified node is found to need expansion.  At this point, the node may also
564 /// have invalid operands or may have other results that need promotion, we just
565 /// know that (at least) one result needs expansion.
566 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
567   DEBUG(cerr << "Expand float result: "; N->dump(&DAG); cerr << "\n");
568   SDOperand Lo, Hi;
569   Lo = Hi = SDOperand();
570
571   // See if the target wants to custom expand this node.
572   if (TLI.getOperationAction(N->getOpcode(), N->getValueType(ResNo)) ==
573       TargetLowering::Custom) {
574     // If the target wants to, allow it to lower this itself.
575     if (SDNode *P = TLI.ReplaceNodeResults(N, DAG)) {
576       // Everything that once used N now uses P.  We are guaranteed that the
577       // result value types of N and the result value types of P match.
578       ReplaceNodeWith(N, P);
579       return;
580     }
581   }
582
583   switch (N->getOpcode()) {
584   default:
585 #ifndef NDEBUG
586     cerr << "ExpandFloatResult #" << ResNo << ": ";
587     N->dump(&DAG); cerr << "\n";
588 #endif
589     assert(0 && "Do not know how to expand the result of this operator!");
590     abort();
591
592   case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
593   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
594   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
595   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
596
597   case ISD::BIT_CONVERT:        ExpandRes_BIT_CONVERT(N, Lo, Hi); break;
598   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
599   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
600   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
601
602   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
603   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
604   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
605   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
606   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
607   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
608   case ISD::SINT_TO_FP:
609   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
610   }
611
612   // If Lo/Hi is null, the sub-method took care of registering results etc.
613   if (Lo.Val)
614     SetExpandedFloat(SDOperand(N, ResNo), Lo, Hi);
615 }
616
617 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDOperand &Lo,
618                                                  SDOperand &Hi) {
619   MVT NVT = TLI.getTypeToTransformTo(N->getValueType(0));
620   assert(NVT.getSizeInBits() == integerPartWidth &&
621          "Do not know how to expand this float constant!");
622   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().convertToAPInt();
623   Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
624                                        &C.getRawData()[1])), NVT);
625   Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
626                                        &C.getRawData()[0])), NVT);
627 }
628
629 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDOperand &Lo,
630                                            SDOperand &Hi) {
631   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
632   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
633                                             RTLIB::ADD_F32,
634                                             RTLIB::ADD_F64,
635                                             RTLIB::ADD_F80,
636                                             RTLIB::ADD_PPCF128),
637                                N->getValueType(0), Ops, 2,
638                                false);
639   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
640   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
641 }
642
643 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDOperand &Lo,
644                                            SDOperand &Hi) {
645   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
646   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
647                                             RTLIB::DIV_F32,
648                                             RTLIB::DIV_F64,
649                                             RTLIB::DIV_F80,
650                                             RTLIB::DIV_PPCF128),
651                                N->getValueType(0), Ops, 2,
652                                false);
653   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
654   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
655 }
656
657 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDOperand &Lo,
658                                            SDOperand &Hi) {
659   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
660   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
661                                             RTLIB::MUL_F32,
662                                             RTLIB::MUL_F64,
663                                             RTLIB::MUL_F80,
664                                             RTLIB::MUL_PPCF128),
665                                N->getValueType(0), Ops, 2,
666                                false);
667   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
668   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
669 }
670
671 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDOperand &Lo,
672                                            SDOperand &Hi) {
673   SDOperand Ops[2] = { N->getOperand(0), N->getOperand(1) };
674   SDOperand Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
675                                             RTLIB::SUB_F32,
676                                             RTLIB::SUB_F64,
677                                             RTLIB::SUB_F80,
678                                             RTLIB::SUB_PPCF128),
679                                N->getValueType(0), Ops, 2,
680                                false);
681   assert(Call.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
682   Lo = Call.getOperand(0); Hi = Call.getOperand(1);
683 }
684
685 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDOperand &Lo,
686                                            SDOperand &Hi) {
687   if (ISD::isNormalLoad(N)) {
688     ExpandRes_NormalLoad(N, Lo, Hi);
689     return;
690   }
691
692   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
693   LoadSDNode *LD = cast<LoadSDNode>(N);
694   SDOperand Chain = LD->getChain();
695   SDOperand Ptr = LD->getBasePtr();
696
697   MVT NVT = TLI.getTypeToTransformTo(LD->getValueType(0));
698   assert(NVT.isByteSized() && "Expanded type not byte sized!");
699   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
700
701   Lo = DAG.getExtLoad(LD->getExtensionType(), NVT, Chain, Ptr,
702                       LD->getSrcValue(), LD->getSrcValueOffset(),
703                       LD->getMemoryVT(),
704                       LD->isVolatile(), LD->getAlignment());
705
706   // Remember the chain.
707   Chain = Lo.getValue(1);
708
709   // The high part is undefined.
710   Hi = DAG.getNode(ISD::UNDEF, NVT);
711
712   // Modified the chain - switch anything that used the old chain to use the
713   // new one.
714   ReplaceValueWith(SDOperand(LD, 1), Chain);
715 }
716
717 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDOperand &Lo,
718                                                  SDOperand &Hi) {
719   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
720   MVT VT = N->getValueType(0);
721   MVT NVT = TLI.getTypeToTransformTo(VT);
722   SDOperand Src = N->getOperand(0);
723   MVT SrcVT = Src.getValueType();
724
725   // First do an SINT_TO_FP, whether the original was signed or unsigned.
726   if (SrcVT.bitsLE(MVT::i32)) {
727     // The integer can be represented exactly in an f64.
728     Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Src);
729     Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
730     Hi = DAG.getNode(ISD::SINT_TO_FP, NVT, Src);
731   } else {
732     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
733     if (SrcVT.bitsLE(MVT::i64)) {
734       Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Src);
735       LC = RTLIB::SINTTOFP_I64_PPCF128;
736     } else if (SrcVT.bitsLE(MVT::i128)) {
737       Src = DAG.getNode(ISD::SIGN_EXTEND, MVT::i128, Src);
738       LC = RTLIB::SINTTOFP_I128_PPCF128;
739     }
740     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
741
742     Hi = MakeLibCall(LC, VT, &Src, 1, true);
743     assert(Hi.Val->getOpcode() == ISD::BUILD_PAIR && "Call lowered wrongly!");
744     Lo = Hi.getOperand(0); Hi = Hi.getOperand(1);
745   }
746
747   if (N->getOpcode() == ISD::SINT_TO_FP)
748     return;
749
750   // Unsigned - fix up the SINT_TO_FP value just calculated.
751   Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
752   SrcVT = Src.getValueType();
753
754   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
755   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
756   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
757   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
758   const uint64_t *Parts = 0;
759
760   switch (SrcVT.getSimpleVT()) {
761   default:
762     assert(false && "Unsupported UINT_TO_FP!");
763   case MVT::i32:
764     Parts = TwoE32;
765   case MVT::i64:
766     Parts = TwoE64;
767   case MVT::i128:
768     Parts = TwoE128;
769   }
770
771   Lo = DAG.getNode(ISD::FADD, VT, Hi,
772                    DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
773                                      MVT::ppcf128));
774   Lo = DAG.getNode(ISD::SELECT_CC, VT, Src, DAG.getConstant(0, SrcVT), Lo, Hi,
775                    DAG.getCondCode(ISD::SETLT));
776   Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
777                    DAG.getConstant(1, TLI.getPointerTy()));
778   Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, NVT, Lo,
779                    DAG.getConstant(0, TLI.getPointerTy()));
780 }
781
782
783 //===----------------------------------------------------------------------===//
784 //  Float Operand Expansion
785 //===----------------------------------------------------------------------===//
786
787 /// ExpandFloatOperand - This method is called when the specified operand of the
788 /// specified node is found to need expansion.  At this point, all of the result
789 /// types of the node are known to be legal, but other operands of the node may
790 /// need promotion or expansion as well as the specified one.
791 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
792   DEBUG(cerr << "Expand float operand: "; N->dump(&DAG); cerr << "\n");
793   SDOperand Res = SDOperand();
794
795   if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
796       == TargetLowering::Custom)
797     Res = TLI.LowerOperation(SDOperand(N, OpNo), DAG);
798
799   if (Res.Val == 0) {
800     switch (N->getOpcode()) {
801     default:
802   #ifndef NDEBUG
803       cerr << "ExpandFloatOperand Op #" << OpNo << ": ";
804       N->dump(&DAG); cerr << "\n";
805   #endif
806       assert(0 && "Do not know how to expand this operator's operand!");
807       abort();
808
809     case ISD::BIT_CONVERT:     Res = ExpandOp_BIT_CONVERT(N); break;
810     case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
811     case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
812
813     case ISD::BR_CC:     Res = ExpandFloatOp_BR_CC(N); break;
814     case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
815     case ISD::SETCC:     Res = ExpandFloatOp_SETCC(N); break;
816
817     case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
818     case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
819     case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
820
821     case ISD::STORE:
822       Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N), OpNo);
823       break;
824     }
825   }
826
827   // If the result is null, the sub-method took care of registering results etc.
828   if (!Res.Val) return false;
829   // If the result is N, the sub-method updated N in place.  Check to see if any
830   // operands are new, and if so, mark them.
831   if (Res.Val == N) {
832     // Mark N as new and remark N and its operands.  This allows us to correctly
833     // revisit N if it needs another step of expansion and allows us to visit
834     // any new operands to N.
835     ReanalyzeNode(N);
836     return true;
837   }
838
839   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
840          "Invalid operand expansion");
841
842   ReplaceValueWith(SDOperand(N, 0), Res);
843   return false;
844 }
845
846 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
847 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
848 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDOperand &NewLHS,
849                                                 SDOperand &NewRHS,
850                                                 ISD::CondCode &CCCode) {
851   SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
852   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
853   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
854
855   MVT VT = NewLHS.getValueType();
856   assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
857
858   // FIXME:  This generated code sucks.  We want to generate
859   //         FCMP crN, hi1, hi2
860   //         BNE crN, L:
861   //         FCMP crN, lo1, lo2
862   // The following can be improved, but not that much.
863   SDOperand Tmp1, Tmp2, Tmp3;
864   Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETEQ);
865   Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
866   Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
867   Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, ISD::SETNE);
868   Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
869   Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
870   NewLHS = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
871   NewRHS = SDOperand();   // LHS is the result, not a compare.
872 }
873
874 SDOperand DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
875   SDOperand NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
876   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
877   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
878
879   // If ExpandSetCCOperands returned a scalar, we need to compare the result
880   // against zero to select between true and false values.
881   if (NewRHS.Val == 0) {
882     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
883     CCCode = ISD::SETNE;
884   }
885
886   // Update N to have the operands specified.
887   return DAG.UpdateNodeOperands(SDOperand(N, 0), N->getOperand(0),
888                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
889                                 N->getOperand(4));
890 }
891
892 SDOperand DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
893   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
894   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
895   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
896
897   // If ExpandSetCCOperands returned a scalar, we need to compare the result
898   // against zero to select between true and false values.
899   if (NewRHS.Val == 0) {
900     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
901     CCCode = ISD::SETNE;
902   }
903
904   // Update N to have the operands specified.
905   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
906                                 N->getOperand(2), N->getOperand(3),
907                                 DAG.getCondCode(CCCode));
908 }
909
910 SDOperand DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
911   SDOperand NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
912   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
913   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode);
914
915   // If ExpandSetCCOperands returned a scalar, use it.
916   if (NewRHS.Val == 0) {
917     assert(NewLHS.getValueType() == N->getValueType(0) &&
918            "Unexpected setcc expansion!");
919     return NewLHS;
920   }
921
922   // Otherwise, update N to have the operands specified.
923   return DAG.UpdateNodeOperands(SDOperand(N, 0), NewLHS, NewRHS,
924                                 DAG.getCondCode(CCCode));
925 }
926
927 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
928   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
929          "Unsupported FP_TO_UINT!");
930
931   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
932   switch (N->getValueType(0).getSimpleVT()) {
933   default:
934     assert(false && "Unsupported FP_TO_UINT!");
935   case MVT::i32:
936     LC = RTLIB::FPTOUINT_PPCF128_I32;
937     break;
938   case MVT::i64:
939     LC = RTLIB::FPTOUINT_PPCF128_I64;
940     break;
941   case MVT::i128:
942     LC = RTLIB::FPTOUINT_PPCF128_I128;
943     break;
944   }
945
946   return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
947 }
948
949 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
950   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
951          "Unsupported FP_TO_SINT!");
952
953   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
954   switch (N->getValueType(0).getSimpleVT()) {
955   default:
956     assert(false && "Unsupported FP_TO_SINT!");
957   case MVT::i32:
958     LC = RTLIB::FPTOSINT_PPCF128_I32;
959   case MVT::i64:
960     LC = RTLIB::FPTOSINT_PPCF128_I64;
961     break;
962   case MVT::i128:
963     LC = RTLIB::FPTOSINT_PPCF128_I64;
964     break;
965   }
966
967   return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false);
968 }
969
970 SDOperand DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
971   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
972          "Logic only correct for ppcf128!");
973   SDOperand Lo, Hi;
974   GetExpandedFloat(N->getOperand(0), Lo, Hi);
975   // Round it the rest of the way (e.g. to f32) if needed.
976   return DAG.getNode(ISD::FP_ROUND, N->getValueType(0), Hi, N->getOperand(1));
977 }
978
979 SDOperand DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
980   if (ISD::isNormalStore(N))
981     return ExpandOp_NormalStore(N, OpNo);
982
983   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
984   assert(OpNo == 1 && "Can only expand the stored value so far");
985   StoreSDNode *ST = cast<StoreSDNode>(N);
986
987   SDOperand Chain = ST->getChain();
988   SDOperand Ptr = ST->getBasePtr();
989
990   MVT NVT = TLI.getTypeToTransformTo(ST->getValue().getValueType());
991   assert(NVT.isByteSized() && "Expanded type not byte sized!");
992   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
993
994   SDOperand Lo, Hi;
995   GetExpandedOp(ST->getValue(), Lo, Hi);
996
997   return DAG.getTruncStore(Chain, Lo, Ptr,
998                            ST->getSrcValue(), ST->getSrcValueOffset(),
999                            ST->getMemoryVT(),
1000                            ST->isVolatile(), ST->getAlignment());
1001 }