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