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