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