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