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