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