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