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