bcda2b8de65d616e4501f5845dedb77fb730d333
[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_FP:  R = SoftenFloatRes_FP16_TO_FP(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
377   // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
378   // entirely possible for both f16 and f32 to be legal, so use the fully
379   // hard-float FP_EXTEND rather than FP16_TO_FP.
380   if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
381     Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
382     if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
383       SoftenFloatResult(Op.getNode(), 0);
384   }
385
386   RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
387   if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
388     Op = GetSoftenedFloat(Op);
389   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
390   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
391 }
392
393 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
394 // nodes?
395 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
396   EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
397   SDValue Op = N->getOperand(0);
398   SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
399                                   false, SDLoc(N)).first;
400   if (N->getValueType(0) == MVT::f32)
401     return Res32;
402
403   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
404   RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
405   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
406   return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
407 }
408
409 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
410   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
411   SDValue Op = N->getOperand(0);
412   if (N->getValueType(0) == MVT::f16) {
413     // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
414     // storage-only type get a chance to select things.
415     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
416   }
417
418   RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
419   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
420   return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
421 }
422
423 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
424   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
425   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
426                      GetSoftenedFloat(N->getOperand(1)) };
427   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
428                                            RTLIB::POW_F32,
429                                            RTLIB::POW_F64,
430                                            RTLIB::POW_F80,
431                                            RTLIB::POW_F128,
432                                            RTLIB::POW_PPCF128),
433                          NVT, Ops, 2, false, SDLoc(N)).first;
434 }
435
436 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
437   assert(N->getOperand(1).getValueType() == MVT::i32 &&
438          "Unsupported power type!");
439   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
440   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
441   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
442                                            RTLIB::POWI_F32,
443                                            RTLIB::POWI_F64,
444                                            RTLIB::POWI_F80,
445                                            RTLIB::POWI_F128,
446                                            RTLIB::POWI_PPCF128),
447                          NVT, Ops, 2, false, SDLoc(N)).first;
448 }
449
450 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
451   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
452   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
453                      GetSoftenedFloat(N->getOperand(1)) };
454   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
455                                            RTLIB::REM_F32,
456                                            RTLIB::REM_F64,
457                                            RTLIB::REM_F80,
458                                            RTLIB::REM_F128,
459                                            RTLIB::REM_PPCF128),
460                          NVT, Ops, 2, false, SDLoc(N)).first;
461 }
462
463 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
464   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
465   SDValue Op = GetSoftenedFloat(N->getOperand(0));
466   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
467                                            RTLIB::RINT_F32,
468                                            RTLIB::RINT_F64,
469                                            RTLIB::RINT_F80,
470                                            RTLIB::RINT_F128,
471                                            RTLIB::RINT_PPCF128),
472                          NVT, &Op, 1, false, SDLoc(N)).first;
473 }
474
475 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
476   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
477   SDValue Op = GetSoftenedFloat(N->getOperand(0));
478   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
479                                            RTLIB::ROUND_F32,
480                                            RTLIB::ROUND_F64,
481                                            RTLIB::ROUND_F80,
482                                            RTLIB::ROUND_F128,
483                                            RTLIB::ROUND_PPCF128),
484                          NVT, &Op, 1, false, SDLoc(N)).first;
485 }
486
487 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
488   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
489   SDValue Op = GetSoftenedFloat(N->getOperand(0));
490   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
491                                            RTLIB::SIN_F32,
492                                            RTLIB::SIN_F64,
493                                            RTLIB::SIN_F80,
494                                            RTLIB::SIN_F128,
495                                            RTLIB::SIN_PPCF128),
496                          NVT, &Op, 1, false, SDLoc(N)).first;
497 }
498
499 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(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::SQRT_F32,
504                                            RTLIB::SQRT_F64,
505                                            RTLIB::SQRT_F80,
506                                            RTLIB::SQRT_F128,
507                                            RTLIB::SQRT_PPCF128),
508                          NVT, &Op, 1, false, SDLoc(N)).first;
509 }
510
511 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
512   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
513   SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
514                      GetSoftenedFloat(N->getOperand(1)) };
515   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
516                                            RTLIB::SUB_F32,
517                                            RTLIB::SUB_F64,
518                                            RTLIB::SUB_F80,
519                                            RTLIB::SUB_F128,
520                                            RTLIB::SUB_PPCF128),
521                          NVT, Ops, 2, false, SDLoc(N)).first;
522 }
523
524 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
525   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
526   if (N->getValueType(0) == MVT::f16)
527     return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
528
529   SDValue Op = GetSoftenedFloat(N->getOperand(0));
530   return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
531                                            RTLIB::TRUNC_F32,
532                                            RTLIB::TRUNC_F64,
533                                            RTLIB::TRUNC_F80,
534                                            RTLIB::TRUNC_F128,
535                                            RTLIB::TRUNC_PPCF128),
536                          NVT, &Op, 1, false, SDLoc(N)).first;
537 }
538
539 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
540   LoadSDNode *L = cast<LoadSDNode>(N);
541   EVT VT = N->getValueType(0);
542   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
543   SDLoc dl(N);
544
545   SDValue NewL;
546   if (L->getExtensionType() == ISD::NON_EXTLOAD) {
547     NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
548                        NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
549                        L->getPointerInfo(), NVT, L->isVolatile(),
550                        L->isNonTemporal(), false, L->getAlignment(),
551                        L->getAAInfo());
552     // Legalized the chain result - switch anything that used the old chain to
553     // use the new one.
554     ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
555     return NewL;
556   }
557
558   // Do a non-extending load followed by FP_EXTEND.
559   NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
560                      L->getMemoryVT(), dl, L->getChain(),
561                      L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
562                      L->getMemoryVT(), L->isVolatile(),
563                      L->isNonTemporal(), false, L->getAlignment(),
564                      L->getAAInfo());
565   // Legalized the chain result - switch anything that used the old chain to
566   // use the new one.
567   ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
568   return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
569 }
570
571 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
572   SDValue LHS = GetSoftenedFloat(N->getOperand(1));
573   SDValue RHS = GetSoftenedFloat(N->getOperand(2));
574   return DAG.getSelect(SDLoc(N),
575                        LHS.getValueType(), N->getOperand(0), LHS, RHS);
576 }
577
578 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
579   SDValue LHS = GetSoftenedFloat(N->getOperand(2));
580   SDValue RHS = GetSoftenedFloat(N->getOperand(3));
581   return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
582                      LHS.getValueType(), N->getOperand(0),
583                      N->getOperand(1), LHS, RHS, N->getOperand(4));
584 }
585
586 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
587   return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
588                                                N->getValueType(0)));
589 }
590
591 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
592   SDValue Chain = N->getOperand(0); // Get the chain.
593   SDValue Ptr = N->getOperand(1); // Get the pointer.
594   EVT VT = N->getValueType(0);
595   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
596   SDLoc dl(N);
597
598   SDValue NewVAARG;
599   NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
600                           N->getConstantOperandVal(3));
601
602   // Legalized the chain result - switch anything that used the old chain to
603   // use the new one.
604   ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
605   return NewVAARG;
606 }
607
608 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
609   bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
610   EVT SVT = N->getOperand(0).getValueType();
611   EVT RVT = N->getValueType(0);
612   EVT NVT = EVT();
613   SDLoc dl(N);
614
615   // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
616   // a larger type, eg: i8 -> fp.  Even if it is legal, no libcall may exactly
617   // match.  Look for an appropriate libcall.
618   RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
619   for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
620        t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
621     NVT = (MVT::SimpleValueType)t;
622     // The source needs to big enough to hold the operand.
623     if (NVT.bitsGE(SVT))
624       LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
625   }
626   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
627
628   // Sign/zero extend the argument if the libcall takes a larger type.
629   SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
630                            NVT, N->getOperand(0));
631   return TLI.makeLibCall(DAG, LC,
632                          TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
633                          &Op, 1, false, dl).first;
634 }
635
636
637 //===----------------------------------------------------------------------===//
638 //  Operand Float to Integer Conversion..
639 //===----------------------------------------------------------------------===//
640
641 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
642   DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
643         dbgs() << "\n");
644   SDValue Res = SDValue();
645
646   switch (N->getOpcode()) {
647   default:
648 #ifndef NDEBUG
649     dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
650     N->dump(&DAG); dbgs() << "\n";
651 #endif
652     llvm_unreachable("Do not know how to soften this operator's operand!");
653
654   case ISD::BITCAST:     Res = SoftenFloatOp_BITCAST(N); break;
655   case ISD::BR_CC:       Res = SoftenFloatOp_BR_CC(N); break;
656   case ISD::FP_EXTEND:   Res = SoftenFloatOp_FP_EXTEND(N); break;
657   case ISD::FP_TO_FP16:  // Same as FP_ROUND for softening purposes
658   case ISD::FP_ROUND:    Res = SoftenFloatOp_FP_ROUND(N); break;
659   case ISD::FP_TO_SINT:  Res = SoftenFloatOp_FP_TO_SINT(N); break;
660   case ISD::FP_TO_UINT:  Res = SoftenFloatOp_FP_TO_UINT(N); break;
661   case ISD::SELECT_CC:   Res = SoftenFloatOp_SELECT_CC(N); break;
662   case ISD::SETCC:       Res = SoftenFloatOp_SETCC(N); break;
663   case ISD::STORE:       Res = SoftenFloatOp_STORE(N, OpNo); break;
664   }
665
666   // If the result is null, the sub-method took care of registering results etc.
667   if (!Res.getNode()) return false;
668
669   // If the result is N, the sub-method updated N in place.  Tell the legalizer
670   // core about this.
671   if (Res.getNode() == N)
672     return true;
673
674   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
675          "Invalid operand expansion");
676
677   ReplaceValueWith(SDValue(N, 0), Res);
678   return false;
679 }
680
681 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
682   return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
683                      GetSoftenedFloat(N->getOperand(0)));
684 }
685
686 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
687   // If we get here, the result must be legal but the source illegal.
688   EVT SVT = N->getOperand(0).getValueType();
689   EVT RVT = N->getValueType(0);
690   SDValue Op = GetSoftenedFloat(N->getOperand(0));
691
692   if (SVT == MVT::f16)
693     return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
694
695   RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
696   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
697
698   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
699 }
700
701
702 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
703   // We actually deal with the partially-softened FP_TO_FP16 node too, which
704   // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
705   assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
706
707   EVT SVT = N->getOperand(0).getValueType();
708   EVT RVT = N->getValueType(0);
709   EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
710
711   RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
712   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
713
714   SDValue Op = GetSoftenedFloat(N->getOperand(0));
715   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
716 }
717
718 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
719   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
720   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
721
722   EVT VT = NewLHS.getValueType();
723   NewLHS = GetSoftenedFloat(NewLHS);
724   NewRHS = GetSoftenedFloat(NewRHS);
725   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
726
727   // If softenSetCCOperands returned a scalar, we need to compare the result
728   // against zero to select between true and false values.
729   if (!NewRHS.getNode()) {
730     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
731     CCCode = ISD::SETNE;
732   }
733
734   // Update N to have the operands specified.
735   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
736                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
737                                 N->getOperand(4)),
738                  0);
739 }
740
741 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
742   EVT RVT = N->getValueType(0);
743   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
744   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
745   SDValue Op = GetSoftenedFloat(N->getOperand(0));
746   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
747 }
748
749 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
750   EVT RVT = N->getValueType(0);
751   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
752   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
753   SDValue Op = GetSoftenedFloat(N->getOperand(0));
754   return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
755 }
756
757 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
758   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
759   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
760
761   EVT VT = NewLHS.getValueType();
762   NewLHS = GetSoftenedFloat(NewLHS);
763   NewRHS = GetSoftenedFloat(NewRHS);
764   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
765
766   // If softenSetCCOperands returned a scalar, we need to compare the result
767   // against zero to select between true and false values.
768   if (!NewRHS.getNode()) {
769     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
770     CCCode = ISD::SETNE;
771   }
772
773   // Update N to have the operands specified.
774   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
775                                 N->getOperand(2), N->getOperand(3),
776                                 DAG.getCondCode(CCCode)),
777                  0);
778 }
779
780 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
781   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
782   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
783
784   EVT VT = NewLHS.getValueType();
785   NewLHS = GetSoftenedFloat(NewLHS);
786   NewRHS = GetSoftenedFloat(NewRHS);
787   TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
788
789   // If softenSetCCOperands returned a scalar, use it.
790   if (!NewRHS.getNode()) {
791     assert(NewLHS.getValueType() == N->getValueType(0) &&
792            "Unexpected setcc expansion!");
793     return NewLHS;
794   }
795
796   // Otherwise, update N to have the operands specified.
797   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
798                                 DAG.getCondCode(CCCode)),
799                  0);
800 }
801
802 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
803   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
804   assert(OpNo == 1 && "Can only soften the stored value!");
805   StoreSDNode *ST = cast<StoreSDNode>(N);
806   SDValue Val = ST->getValue();
807   SDLoc dl(N);
808
809   if (ST->isTruncatingStore())
810     // Do an FP_ROUND followed by a non-truncating store.
811     Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
812                                           Val, DAG.getIntPtrConstant(0)));
813   else
814     Val = GetSoftenedFloat(Val);
815
816   return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
817                       ST->getMemOperand());
818 }
819
820
821 //===----------------------------------------------------------------------===//
822 //  Float Result Expansion
823 //===----------------------------------------------------------------------===//
824
825 /// ExpandFloatResult - This method is called when the specified result of the
826 /// specified node is found to need expansion.  At this point, the node may also
827 /// have invalid operands or may have other results that need promotion, we just
828 /// know that (at least) one result needs expansion.
829 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
830   DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
831   SDValue Lo, Hi;
832   Lo = Hi = SDValue();
833
834   // See if the target wants to custom expand this node.
835   if (CustomLowerNode(N, N->getValueType(ResNo), true))
836     return;
837
838   switch (N->getOpcode()) {
839   default:
840 #ifndef NDEBUG
841     dbgs() << "ExpandFloatResult #" << ResNo << ": ";
842     N->dump(&DAG); dbgs() << "\n";
843 #endif
844     llvm_unreachable("Do not know how to expand the result of this operator!");
845
846   case ISD::UNDEF:        SplitRes_UNDEF(N, Lo, Hi); break;
847   case ISD::SELECT:       SplitRes_SELECT(N, Lo, Hi); break;
848   case ISD::SELECT_CC:    SplitRes_SELECT_CC(N, Lo, Hi); break;
849
850   case ISD::MERGE_VALUES:       ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
851   case ISD::BITCAST:            ExpandRes_BITCAST(N, Lo, Hi); break;
852   case ISD::BUILD_PAIR:         ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
853   case ISD::EXTRACT_ELEMENT:    ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
854   case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
855   case ISD::VAARG:              ExpandRes_VAARG(N, Lo, Hi); break;
856
857   case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
858   case ISD::FABS:       ExpandFloatRes_FABS(N, Lo, Hi); break;
859   case ISD::FADD:       ExpandFloatRes_FADD(N, Lo, Hi); break;
860   case ISD::FCEIL:      ExpandFloatRes_FCEIL(N, Lo, Hi); break;
861   case ISD::FCOPYSIGN:  ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
862   case ISD::FCOS:       ExpandFloatRes_FCOS(N, Lo, Hi); break;
863   case ISD::FDIV:       ExpandFloatRes_FDIV(N, Lo, Hi); break;
864   case ISD::FEXP:       ExpandFloatRes_FEXP(N, Lo, Hi); break;
865   case ISD::FEXP2:      ExpandFloatRes_FEXP2(N, Lo, Hi); break;
866   case ISD::FFLOOR:     ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
867   case ISD::FLOG:       ExpandFloatRes_FLOG(N, Lo, Hi); break;
868   case ISD::FLOG2:      ExpandFloatRes_FLOG2(N, Lo, Hi); break;
869   case ISD::FLOG10:     ExpandFloatRes_FLOG10(N, Lo, Hi); break;
870   case ISD::FMA:        ExpandFloatRes_FMA(N, Lo, Hi); break;
871   case ISD::FMUL:       ExpandFloatRes_FMUL(N, Lo, Hi); break;
872   case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
873   case ISD::FNEG:       ExpandFloatRes_FNEG(N, Lo, Hi); break;
874   case ISD::FP_EXTEND:  ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
875   case ISD::FPOW:       ExpandFloatRes_FPOW(N, Lo, Hi); break;
876   case ISD::FPOWI:      ExpandFloatRes_FPOWI(N, Lo, Hi); break;
877   case ISD::FRINT:      ExpandFloatRes_FRINT(N, Lo, Hi); break;
878   case ISD::FROUND:     ExpandFloatRes_FROUND(N, Lo, Hi); break;
879   case ISD::FSIN:       ExpandFloatRes_FSIN(N, Lo, Hi); break;
880   case ISD::FSQRT:      ExpandFloatRes_FSQRT(N, Lo, Hi); break;
881   case ISD::FSUB:       ExpandFloatRes_FSUB(N, Lo, Hi); break;
882   case ISD::FTRUNC:     ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
883   case ISD::LOAD:       ExpandFloatRes_LOAD(N, Lo, Hi); break;
884   case ISD::SINT_TO_FP:
885   case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
886   case ISD::FREM:       ExpandFloatRes_FREM(N, Lo, Hi); break;
887   }
888
889   // If Lo/Hi is null, the sub-method took care of registering results etc.
890   if (Lo.getNode())
891     SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
892 }
893
894 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
895                                                  SDValue &Hi) {
896   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
897   assert(NVT.getSizeInBits() == integerPartWidth &&
898          "Do not know how to expand this float constant!");
899   APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
900   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
901                                  APInt(integerPartWidth, C.getRawData()[1])),
902                          NVT);
903   Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
904                                  APInt(integerPartWidth, C.getRawData()[0])),
905                          NVT);
906 }
907
908 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
909                                            SDValue &Hi) {
910   assert(N->getValueType(0) == MVT::ppcf128 &&
911          "Logic only correct for ppcf128!");
912   SDLoc dl(N);
913   SDValue Tmp;
914   GetExpandedFloat(N->getOperand(0), Lo, Tmp);
915   Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
916   // Lo = Hi==fabs(Hi) ? Lo : -Lo;
917   Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
918                    DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
919                    ISD::SETEQ);
920 }
921
922 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
923                                            SDValue &Hi) {
924   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
925                                          RTLIB::ADD_F32, RTLIB::ADD_F64,
926                                          RTLIB::ADD_F80, RTLIB::ADD_F128,
927                                          RTLIB::ADD_PPCF128),
928                             N, false);
929   GetPairElements(Call, Lo, Hi);
930 }
931
932 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
933                                             SDValue &Lo, SDValue &Hi) {
934   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
935                                          RTLIB::CEIL_F32, RTLIB::CEIL_F64,
936                                          RTLIB::CEIL_F80, RTLIB::CEIL_F128,
937                                          RTLIB::CEIL_PPCF128),
938                             N, false);
939   GetPairElements(Call, Lo, Hi);
940 }
941
942 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
943                                                 SDValue &Lo, SDValue &Hi) {
944   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
945                                          RTLIB::COPYSIGN_F32,
946                                          RTLIB::COPYSIGN_F64,
947                                          RTLIB::COPYSIGN_F80,
948                                          RTLIB::COPYSIGN_F128,
949                                          RTLIB::COPYSIGN_PPCF128),
950                             N, false);
951   GetPairElements(Call, Lo, Hi);
952 }
953
954 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
955                                            SDValue &Lo, SDValue &Hi) {
956   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
957                                          RTLIB::COS_F32, RTLIB::COS_F64,
958                                          RTLIB::COS_F80, RTLIB::COS_F128,
959                                          RTLIB::COS_PPCF128),
960                             N, false);
961   GetPairElements(Call, Lo, Hi);
962 }
963
964 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
965                                            SDValue &Hi) {
966   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
967   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
968                                                    RTLIB::DIV_F32,
969                                                    RTLIB::DIV_F64,
970                                                    RTLIB::DIV_F80,
971                                                    RTLIB::DIV_F128,
972                                                    RTLIB::DIV_PPCF128),
973                                  N->getValueType(0), Ops, 2, false,
974                                  SDLoc(N)).first;
975   GetPairElements(Call, Lo, Hi);
976 }
977
978 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
979                                            SDValue &Lo, SDValue &Hi) {
980   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
981                                          RTLIB::EXP_F32, RTLIB::EXP_F64,
982                                          RTLIB::EXP_F80, RTLIB::EXP_F128,
983                                          RTLIB::EXP_PPCF128),
984                             N, false);
985   GetPairElements(Call, Lo, Hi);
986 }
987
988 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
989                                             SDValue &Lo, SDValue &Hi) {
990   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
991                                          RTLIB::EXP2_F32, RTLIB::EXP2_F64,
992                                          RTLIB::EXP2_F80, RTLIB::EXP2_F128,
993                                          RTLIB::EXP2_PPCF128),
994                             N, false);
995   GetPairElements(Call, Lo, Hi);
996 }
997
998 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
999                                              SDValue &Lo, SDValue &Hi) {
1000   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1001                                          RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1002                                          RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1003                                          RTLIB::FLOOR_PPCF128),
1004                             N, false);
1005   GetPairElements(Call, Lo, Hi);
1006 }
1007
1008 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1009                                            SDValue &Lo, SDValue &Hi) {
1010   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1011                                          RTLIB::LOG_F32, RTLIB::LOG_F64,
1012                                          RTLIB::LOG_F80, RTLIB::LOG_F128,
1013                                          RTLIB::LOG_PPCF128),
1014                             N, false);
1015   GetPairElements(Call, Lo, Hi);
1016 }
1017
1018 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1019                                             SDValue &Lo, SDValue &Hi) {
1020   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1021                                          RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1022                                          RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1023                                          RTLIB::LOG2_PPCF128),
1024                             N, false);
1025   GetPairElements(Call, Lo, Hi);
1026 }
1027
1028 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1029                                              SDValue &Lo, SDValue &Hi) {
1030   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1031                                          RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1032                                          RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1033                                          RTLIB::LOG10_PPCF128),
1034                             N, false);
1035   GetPairElements(Call, Lo, Hi);
1036 }
1037
1038 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1039                                           SDValue &Hi) {
1040   SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1041   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1042                                                    RTLIB::FMA_F32,
1043                                                    RTLIB::FMA_F64,
1044                                                    RTLIB::FMA_F80,
1045                                                    RTLIB::FMA_F128,
1046                                                    RTLIB::FMA_PPCF128),
1047                                  N->getValueType(0), Ops, 3, false,
1048                                  SDLoc(N)).first;
1049   GetPairElements(Call, Lo, Hi);
1050 }
1051
1052 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1053                                            SDValue &Hi) {
1054   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1055   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1056                                                    RTLIB::MUL_F32,
1057                                                    RTLIB::MUL_F64,
1058                                                    RTLIB::MUL_F80,
1059                                                    RTLIB::MUL_F128,
1060                                                    RTLIB::MUL_PPCF128),
1061                                  N->getValueType(0), Ops, 2, false,
1062                                  SDLoc(N)).first;
1063   GetPairElements(Call, Lo, Hi);
1064 }
1065
1066 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1067                                                  SDValue &Lo, SDValue &Hi) {
1068   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1069                                          RTLIB::NEARBYINT_F32,
1070                                          RTLIB::NEARBYINT_F64,
1071                                          RTLIB::NEARBYINT_F80,
1072                                          RTLIB::NEARBYINT_F128,
1073                                          RTLIB::NEARBYINT_PPCF128),
1074                             N, false);
1075   GetPairElements(Call, Lo, Hi);
1076 }
1077
1078 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1079                                            SDValue &Hi) {
1080   SDLoc dl(N);
1081   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1082   Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1083   Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1084 }
1085
1086 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1087                                                 SDValue &Hi) {
1088   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1089   Hi = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), NVT, N->getOperand(0));
1090   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1091                                  APInt(NVT.getSizeInBits(), 0)), NVT);
1092 }
1093
1094 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1095                                            SDValue &Lo, SDValue &Hi) {
1096   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1097                                          RTLIB::POW_F32, RTLIB::POW_F64,
1098                                          RTLIB::POW_F80, RTLIB::POW_F128,
1099                                          RTLIB::POW_PPCF128),
1100                             N, false);
1101   GetPairElements(Call, Lo, Hi);
1102 }
1103
1104 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1105                                             SDValue &Lo, SDValue &Hi) {
1106   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1107                                          RTLIB::POWI_F32, RTLIB::POWI_F64,
1108                                          RTLIB::POWI_F80, RTLIB::POWI_F128,
1109                                          RTLIB::POWI_PPCF128),
1110                             N, false);
1111   GetPairElements(Call, Lo, Hi);
1112 }
1113
1114 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1115                                            SDValue &Lo, SDValue &Hi) {
1116   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1117                                          RTLIB::REM_F32, RTLIB::REM_F64,
1118                                          RTLIB::REM_F80, RTLIB::REM_F128,
1119                                          RTLIB::REM_PPCF128),
1120                             N, false);
1121   GetPairElements(Call, Lo, Hi);
1122 }
1123
1124 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1125                                             SDValue &Lo, SDValue &Hi) {
1126   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1127                                          RTLIB::RINT_F32, RTLIB::RINT_F64,
1128                                          RTLIB::RINT_F80, RTLIB::RINT_F128,
1129                                          RTLIB::RINT_PPCF128),
1130                             N, false);
1131   GetPairElements(Call, Lo, Hi);
1132 }
1133
1134 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1135                                              SDValue &Lo, SDValue &Hi) {
1136   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1137                                          RTLIB::ROUND_F32,
1138                                          RTLIB::ROUND_F64,
1139                                          RTLIB::ROUND_F80,
1140                                          RTLIB::ROUND_F128,
1141                                          RTLIB::ROUND_PPCF128),
1142                             N, false);
1143   GetPairElements(Call, Lo, Hi);
1144 }
1145
1146 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1147                                            SDValue &Lo, SDValue &Hi) {
1148   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1149                                          RTLIB::SIN_F32, RTLIB::SIN_F64,
1150                                          RTLIB::SIN_F80, RTLIB::SIN_F128,
1151                                          RTLIB::SIN_PPCF128),
1152                             N, false);
1153   GetPairElements(Call, Lo, Hi);
1154 }
1155
1156 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1157                                             SDValue &Lo, SDValue &Hi) {
1158   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1159                                          RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1160                                          RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1161                                          RTLIB::SQRT_PPCF128),
1162                             N, false);
1163   GetPairElements(Call, Lo, Hi);
1164 }
1165
1166 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1167                                            SDValue &Hi) {
1168   SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1169   SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1170                                                    RTLIB::SUB_F32,
1171                                                    RTLIB::SUB_F64,
1172                                                    RTLIB::SUB_F80,
1173                                                    RTLIB::SUB_F128,
1174                                                    RTLIB::SUB_PPCF128),
1175                                  N->getValueType(0), Ops, 2, false,
1176                                  SDLoc(N)).first;
1177   GetPairElements(Call, Lo, Hi);
1178 }
1179
1180 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1181                                              SDValue &Lo, SDValue &Hi) {
1182   SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1183                                          RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1184                                          RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1185                                          RTLIB::TRUNC_PPCF128),
1186                             N, false);
1187   GetPairElements(Call, Lo, Hi);
1188 }
1189
1190 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1191                                            SDValue &Hi) {
1192   if (ISD::isNormalLoad(N)) {
1193     ExpandRes_NormalLoad(N, Lo, Hi);
1194     return;
1195   }
1196
1197   assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1198   LoadSDNode *LD = cast<LoadSDNode>(N);
1199   SDValue Chain = LD->getChain();
1200   SDValue Ptr = LD->getBasePtr();
1201   SDLoc dl(N);
1202
1203   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1204   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1205   assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1206
1207   Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1208                       LD->getMemoryVT(), LD->getMemOperand());
1209
1210   // Remember the chain.
1211   Chain = Hi.getValue(1);
1212
1213   // The low part is zero.
1214   Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1215                                  APInt(NVT.getSizeInBits(), 0)), NVT);
1216
1217   // Modified the chain - switch anything that used the old chain to use the
1218   // new one.
1219   ReplaceValueWith(SDValue(LD, 1), Chain);
1220 }
1221
1222 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1223                                                  SDValue &Hi) {
1224   assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1225   EVT VT = N->getValueType(0);
1226   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1227   SDValue Src = N->getOperand(0);
1228   EVT SrcVT = Src.getValueType();
1229   bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1230   SDLoc dl(N);
1231
1232   // First do an SINT_TO_FP, whether the original was signed or unsigned.
1233   // When promoting partial word types to i32 we must honor the signedness,
1234   // though.
1235   if (SrcVT.bitsLE(MVT::i32)) {
1236     // The integer can be represented exactly in an f64.
1237     Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1238                       MVT::i32, Src);
1239     Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1240                                    APInt(NVT.getSizeInBits(), 0)), NVT);
1241     Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1242   } else {
1243     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1244     if (SrcVT.bitsLE(MVT::i64)) {
1245       Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1246                         MVT::i64, Src);
1247       LC = RTLIB::SINTTOFP_I64_PPCF128;
1248     } else if (SrcVT.bitsLE(MVT::i128)) {
1249       Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1250       LC = RTLIB::SINTTOFP_I128_PPCF128;
1251     }
1252     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1253
1254     Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1255     GetPairElements(Hi, Lo, Hi);
1256   }
1257
1258   if (isSigned)
1259     return;
1260
1261   // Unsigned - fix up the SINT_TO_FP value just calculated.
1262   Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1263   SrcVT = Src.getValueType();
1264
1265   // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1266   static const uint64_t TwoE32[]  = { 0x41f0000000000000LL, 0 };
1267   static const uint64_t TwoE64[]  = { 0x43f0000000000000LL, 0 };
1268   static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1269   ArrayRef<uint64_t> Parts;
1270
1271   switch (SrcVT.getSimpleVT().SimpleTy) {
1272   default:
1273     llvm_unreachable("Unsupported UINT_TO_FP!");
1274   case MVT::i32:
1275     Parts = TwoE32;
1276     break;
1277   case MVT::i64:
1278     Parts = TwoE64;
1279     break;
1280   case MVT::i128:
1281     Parts = TwoE128;
1282     break;
1283   }
1284
1285   Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1286                    DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1287                                              APInt(128, Parts)),
1288                                      MVT::ppcf128));
1289   Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, SrcVT),
1290                        Lo, Hi, ISD::SETLT);
1291   GetPairElements(Lo, Lo, Hi);
1292 }
1293
1294
1295 //===----------------------------------------------------------------------===//
1296 //  Float Operand Expansion
1297 //===----------------------------------------------------------------------===//
1298
1299 /// ExpandFloatOperand - This method is called when the specified operand of the
1300 /// specified node is found to need expansion.  At this point, all of the result
1301 /// types of the node are known to be legal, but other operands of the node may
1302 /// need promotion or expansion as well as the specified one.
1303 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1304   DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1305   SDValue Res = SDValue();
1306
1307   // See if the target wants to custom expand this node.
1308   if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1309     return false;
1310
1311   switch (N->getOpcode()) {
1312   default:
1313 #ifndef NDEBUG
1314     dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1315     N->dump(&DAG); dbgs() << "\n";
1316 #endif
1317     llvm_unreachable("Do not know how to expand this operator's operand!");
1318
1319   case ISD::BITCAST:         Res = ExpandOp_BITCAST(N); break;
1320   case ISD::BUILD_VECTOR:    Res = ExpandOp_BUILD_VECTOR(N); break;
1321   case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1322
1323   case ISD::BR_CC:      Res = ExpandFloatOp_BR_CC(N); break;
1324   case ISD::FCOPYSIGN:  Res = ExpandFloatOp_FCOPYSIGN(N); break;
1325   case ISD::FP_ROUND:   Res = ExpandFloatOp_FP_ROUND(N); break;
1326   case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1327   case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1328   case ISD::SELECT_CC:  Res = ExpandFloatOp_SELECT_CC(N); break;
1329   case ISD::SETCC:      Res = ExpandFloatOp_SETCC(N); break;
1330   case ISD::STORE:      Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1331                                                   OpNo); break;
1332   }
1333
1334   // If the result is null, the sub-method took care of registering results etc.
1335   if (!Res.getNode()) return false;
1336
1337   // If the result is N, the sub-method updated N in place.  Tell the legalizer
1338   // core about this.
1339   if (Res.getNode() == N)
1340     return true;
1341
1342   assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1343          "Invalid operand expansion");
1344
1345   ReplaceValueWith(SDValue(N, 0), Res);
1346   return false;
1347 }
1348
1349 /// FloatExpandSetCCOperands - Expand the operands of a comparison.  This code
1350 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1351 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1352                                                 SDValue &NewRHS,
1353                                                 ISD::CondCode &CCCode,
1354                                                 SDLoc dl) {
1355   SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1356   GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1357   GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1358
1359   assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1360
1361   // FIXME:  This generated code sucks.  We want to generate
1362   //         FCMPU crN, hi1, hi2
1363   //         BNE crN, L:
1364   //         FCMPU crN, lo1, lo2
1365   // The following can be improved, but not that much.
1366   SDValue Tmp1, Tmp2, Tmp3;
1367   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1368                       LHSHi, RHSHi, ISD::SETOEQ);
1369   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1370                       LHSLo, RHSLo, CCCode);
1371   Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1372   Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1373                       LHSHi, RHSHi, ISD::SETUNE);
1374   Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1375                       LHSHi, RHSHi, CCCode);
1376   Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1377   NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1378   NewRHS = SDValue();   // LHS is the result, not a compare.
1379 }
1380
1381 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1382   SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1383   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1384   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1385
1386   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1387   // against zero to select between true and false values.
1388   if (!NewRHS.getNode()) {
1389     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1390     CCCode = ISD::SETNE;
1391   }
1392
1393   // Update N to have the operands specified.
1394   return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1395                                 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1396                                 N->getOperand(4)), 0);
1397 }
1398
1399 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1400   assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1401          "Logic only correct for ppcf128!");
1402   SDValue Lo, Hi;
1403   GetExpandedFloat(N->getOperand(1), Lo, Hi);
1404   // The ppcf128 value is providing only the sign; take it from the
1405   // higher-order double (which must have the larger magnitude).
1406   return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1407                      N->getValueType(0), N->getOperand(0), Hi);
1408 }
1409
1410 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1411   assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1412          "Logic only correct for ppcf128!");
1413   SDValue Lo, Hi;
1414   GetExpandedFloat(N->getOperand(0), Lo, Hi);
1415   // Round it the rest of the way (e.g. to f32) if needed.
1416   return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1417                      N->getValueType(0), Hi, N->getOperand(1));
1418 }
1419
1420 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1421   EVT RVT = N->getValueType(0);
1422   SDLoc dl(N);
1423
1424   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1425   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1426   if (RVT == MVT::i32) {
1427     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1428            "Logic only correct for ppcf128!");
1429     SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1430                               N->getOperand(0), DAG.getValueType(MVT::f64));
1431     Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1432                       DAG.getIntPtrConstant(1));
1433     return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1434   }
1435
1436   RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1437   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1438   return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1439 }
1440
1441 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1442   EVT RVT = N->getValueType(0);
1443   SDLoc dl(N);
1444
1445   // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1446   // PPC (the libcall is not available).  FIXME: Do this in a less hacky way.
1447   if (RVT == MVT::i32) {
1448     assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1449            "Logic only correct for ppcf128!");
1450     const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1451     APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1452     SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1453     //  X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1454     // FIXME: generated code sucks.
1455     return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1456                            DAG.getNode(ISD::ADD, dl, MVT::i32,
1457                                        DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1458                                                    DAG.getNode(ISD::FSUB, dl,
1459                                                                MVT::ppcf128,
1460                                                                N->getOperand(0),
1461                                                                Tmp)),
1462                                        DAG.getConstant(0x80000000, MVT::i32)),
1463                            DAG.getNode(ISD::FP_TO_SINT, dl,
1464                                        MVT::i32, N->getOperand(0)),
1465                            ISD::SETGE);
1466   }
1467
1468   RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1469   assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1470   return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1471                          false, dl).first;
1472 }
1473
1474 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1475   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1476   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1477   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1478
1479   // If ExpandSetCCOperands returned a scalar, we need to compare the result
1480   // against zero to select between true and false values.
1481   if (!NewRHS.getNode()) {
1482     NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1483     CCCode = ISD::SETNE;
1484   }
1485
1486   // Update N to have the operands specified.
1487   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1488                                 N->getOperand(2), N->getOperand(3),
1489                                 DAG.getCondCode(CCCode)), 0);
1490 }
1491
1492 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1493   SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1494   ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1495   FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1496
1497   // If ExpandSetCCOperands returned a scalar, use it.
1498   if (!NewRHS.getNode()) {
1499     assert(NewLHS.getValueType() == N->getValueType(0) &&
1500            "Unexpected setcc expansion!");
1501     return NewLHS;
1502   }
1503
1504   // Otherwise, update N to have the operands specified.
1505   return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1506                                 DAG.getCondCode(CCCode)), 0);
1507 }
1508
1509 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1510   if (ISD::isNormalStore(N))
1511     return ExpandOp_NormalStore(N, OpNo);
1512
1513   assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1514   assert(OpNo == 1 && "Can only expand the stored value so far");
1515   StoreSDNode *ST = cast<StoreSDNode>(N);
1516
1517   SDValue Chain = ST->getChain();
1518   SDValue Ptr = ST->getBasePtr();
1519
1520   EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1521                                      ST->getValue().getValueType());
1522   assert(NVT.isByteSized() && "Expanded type not byte sized!");
1523   assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1524   (void)NVT;
1525
1526   SDValue Lo, Hi;
1527   GetExpandedOp(ST->getValue(), Lo, Hi);
1528
1529   return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1530                            ST->getMemoryVT(), ST->getMemOperand());
1531 }