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