1 //===-------- LegalizeFloatTypes.cpp - Legalization of float types --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
20 //===----------------------------------------------------------------------===//
22 #include "LegalizeTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
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) {
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;
41 //===----------------------------------------------------------------------===//
42 // Result Float to Integer Conversion.
43 //===----------------------------------------------------------------------===//
45 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
46 DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
48 SDValue R = SDValue();
50 switch (N->getOpcode()) {
53 dbgs() << "SoftenFloatResult #" << ResNo << ": ";
54 N->dump(&DAG); dbgs() << "\n";
56 llvm_unreachable("Do not know how to soften the result of this operator!");
58 case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
59 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
61 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
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;
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;
100 // If R is null, the sub-method took care of registering the result.
102 SetSoftenedFloat(SDValue(N, ResNo), R);
105 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
106 return BitConvertToInteger(N->getOperand(0));
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(),
114 BitConvertToInteger(N->getOperand(0)),
115 BitConvertToInteger(N->getOperand(1)));
118 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
119 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(),
120 TLI.getTypeToTransformTo(*DAG.getContext(),
121 N->getValueType(0)));
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));
131 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
132 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
133 unsigned Size = NVT.getSizeInBits();
135 // Mask = ~(1 << (Size-1))
136 APInt API = APInt::getAllOnesValue(Size);
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);
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),
152 NVT, Ops, 2, false, N->getDebugLoc());
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),
162 RTLIB::CEIL_PPCF128),
163 NVT, &Op, 1, false, N->getDebugLoc());
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();
171 EVT LVT = LHS.getValueType();
172 EVT RVT = RHS.getValueType();
174 unsigned LSize = LVT.getSizeInBits();
175 unsigned RSize = RVT.getSizeInBits();
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()));
181 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
183 // Shift right or sign-extend it if the two operands have different types.
184 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
186 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
187 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
188 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
189 } else if (SizeDiff < 0) {
190 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
191 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
192 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
195 // Clear the sign bit of the first operand.
196 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
197 DAG.getConstant(LSize - 1,
198 TLI.getShiftAmountTy()));
199 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
200 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
202 // Or the value with the sign bit.
203 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
206 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
207 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
208 SDValue Op = GetSoftenedFloat(N->getOperand(0));
209 return MakeLibCall(GetFPLibCall(N->getValueType(0),
214 NVT, &Op, 1, false, N->getDebugLoc());
217 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
218 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
219 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
220 GetSoftenedFloat(N->getOperand(1)) };
221 return MakeLibCall(GetFPLibCall(N->getValueType(0),
226 NVT, Ops, 2, false, N->getDebugLoc());
229 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
230 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
231 SDValue Op = GetSoftenedFloat(N->getOperand(0));
232 return MakeLibCall(GetFPLibCall(N->getValueType(0),
237 NVT, &Op, 1, false, N->getDebugLoc());
240 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
241 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
242 SDValue Op = GetSoftenedFloat(N->getOperand(0));
243 return MakeLibCall(GetFPLibCall(N->getValueType(0),
247 RTLIB::EXP2_PPCF128),
248 NVT, &Op, 1, false, N->getDebugLoc());
251 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
252 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
253 SDValue Op = GetSoftenedFloat(N->getOperand(0));
254 return MakeLibCall(GetFPLibCall(N->getValueType(0),
258 RTLIB::FLOOR_PPCF128),
259 NVT, &Op, 1, false, N->getDebugLoc());
262 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
263 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
264 SDValue Op = GetSoftenedFloat(N->getOperand(0));
265 return MakeLibCall(GetFPLibCall(N->getValueType(0),
270 NVT, &Op, 1, false, N->getDebugLoc());
273 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
274 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
275 SDValue Op = GetSoftenedFloat(N->getOperand(0));
276 return MakeLibCall(GetFPLibCall(N->getValueType(0),
280 RTLIB::LOG2_PPCF128),
281 NVT, &Op, 1, false, N->getDebugLoc());
284 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
285 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
286 SDValue Op = GetSoftenedFloat(N->getOperand(0));
287 return MakeLibCall(GetFPLibCall(N->getValueType(0),
291 RTLIB::LOG10_PPCF128),
292 NVT, &Op, 1, false, N->getDebugLoc());
295 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
296 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
297 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
298 GetSoftenedFloat(N->getOperand(1)) };
299 return MakeLibCall(GetFPLibCall(N->getValueType(0),
304 NVT, Ops, 2, false, N->getDebugLoc());
307 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
308 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
309 SDValue Op = GetSoftenedFloat(N->getOperand(0));
310 return MakeLibCall(GetFPLibCall(N->getValueType(0),
311 RTLIB::NEARBYINT_F32,
312 RTLIB::NEARBYINT_F64,
313 RTLIB::NEARBYINT_F80,
314 RTLIB::NEARBYINT_PPCF128),
315 NVT, &Op, 1, false, N->getDebugLoc());
318 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
319 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
320 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
321 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
322 GetSoftenedFloat(N->getOperand(0)) };
323 return MakeLibCall(GetFPLibCall(N->getValueType(0),
328 NVT, Ops, 2, false, N->getDebugLoc());
331 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
332 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
333 SDValue Op = N->getOperand(0);
334 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
335 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
336 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
339 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
341 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
342 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
343 SDValue Op = N->getOperand(0);
344 return MakeLibCall(RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
348 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
349 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
350 SDValue Op = N->getOperand(0);
351 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
352 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
353 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
356 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
357 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
358 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
359 GetSoftenedFloat(N->getOperand(1)) };
360 return MakeLibCall(GetFPLibCall(N->getValueType(0),
365 NVT, Ops, 2, false, N->getDebugLoc());
368 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
369 assert(N->getOperand(1).getValueType() == MVT::i32 &&
370 "Unsupported power type!");
371 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
372 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
373 return MakeLibCall(GetFPLibCall(N->getValueType(0),
377 RTLIB::POWI_PPCF128),
378 NVT, Ops, 2, false, N->getDebugLoc());
381 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
382 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
383 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
384 GetSoftenedFloat(N->getOperand(1)) };
385 return MakeLibCall(GetFPLibCall(N->getValueType(0),
390 NVT, Ops, 2, false, N->getDebugLoc());
393 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
394 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
395 SDValue Op = GetSoftenedFloat(N->getOperand(0));
396 return MakeLibCall(GetFPLibCall(N->getValueType(0),
400 RTLIB::RINT_PPCF128),
401 NVT, &Op, 1, false, N->getDebugLoc());
404 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
405 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
406 SDValue Op = GetSoftenedFloat(N->getOperand(0));
407 return MakeLibCall(GetFPLibCall(N->getValueType(0),
412 NVT, &Op, 1, false, N->getDebugLoc());
415 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
416 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
417 SDValue Op = GetSoftenedFloat(N->getOperand(0));
418 return MakeLibCall(GetFPLibCall(N->getValueType(0),
422 RTLIB::SQRT_PPCF128),
423 NVT, &Op, 1, false, N->getDebugLoc());
426 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
427 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
428 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
429 GetSoftenedFloat(N->getOperand(1)) };
430 return MakeLibCall(GetFPLibCall(N->getValueType(0),
435 NVT, Ops, 2, false, N->getDebugLoc());
438 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
439 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
440 SDValue Op = GetSoftenedFloat(N->getOperand(0));
441 return MakeLibCall(GetFPLibCall(N->getValueType(0),
445 RTLIB::TRUNC_PPCF128),
446 NVT, &Op, 1, false, N->getDebugLoc());
449 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
450 LoadSDNode *L = cast<LoadSDNode>(N);
451 EVT VT = N->getValueType(0);
452 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
453 DebugLoc dl = N->getDebugLoc();
456 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
457 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
458 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
459 L->getPointerInfo(), NVT,
460 L->isVolatile(), L->isNonTemporal(), L->getAlignment());
461 // Legalized the chain result - switch anything that used the old chain to
463 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
467 // Do a non-extending load followed by FP_EXTEND.
468 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
469 L->getMemoryVT(), dl, L->getChain(),
470 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
471 L->getMemoryVT(), L->isVolatile(),
472 L->isNonTemporal(), L->getAlignment());
473 // Legalized the chain result - switch anything that used the old chain to
475 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
476 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
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);
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));
494 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
495 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
496 N->getValueType(0)));
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();
507 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
508 N->getConstantOperandVal(3));
510 // Legalized the chain result - switch anything that used the old chain to
512 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
516 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
517 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
518 EVT SVT = N->getOperand(0).getValueType();
519 EVT RVT = N->getValueType(0);
521 DebugLoc dl = N->getDebugLoc();
523 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
524 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
525 // match. Look for an appropriate libcall.
526 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
527 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
528 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
529 NVT = (MVT::SimpleValueType)t;
530 // The source needs to big enough to hold the operand.
532 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
534 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
536 // Sign/zero extend the argument if the libcall takes a larger type.
537 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
538 NVT, N->getOperand(0));
539 return MakeLibCall(LC, TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
544 //===----------------------------------------------------------------------===//
545 // Operand Float to Integer Conversion..
546 //===----------------------------------------------------------------------===//
548 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
549 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
551 SDValue Res = SDValue();
553 switch (N->getOpcode()) {
556 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
557 N->dump(&DAG); dbgs() << "\n";
559 llvm_unreachable("Do not know how to soften this operator's operand!");
561 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
562 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
563 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
564 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
565 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
566 case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
567 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
568 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
569 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
572 // If the result is null, the sub-method took care of registering results etc.
573 if (!Res.getNode()) return false;
575 // If the result is N, the sub-method updated N in place. Tell the legalizer
577 if (Res.getNode() == N)
580 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
581 "Invalid operand expansion");
583 ReplaceValueWith(SDValue(N, 0), Res);
587 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
588 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
589 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
590 ISD::CondCode &CCCode, DebugLoc dl) {
591 SDValue LHSInt = GetSoftenedFloat(NewLHS);
592 SDValue RHSInt = GetSoftenedFloat(NewRHS);
593 EVT VT = NewLHS.getValueType();
595 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
597 // Expand into one or more soft-fp libcall(s).
598 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
602 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
606 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
610 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
614 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
618 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
622 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
625 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
628 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
631 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
634 // SETONE = SETOLT | SETOGT
635 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
638 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
641 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
644 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
647 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
650 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
652 default: assert(false && "Do not know how to soften this setcc!");
656 // Use the target specific return value for comparions lib calls.
657 EVT RetVT = TLI.getCmpLibcallReturnType();
658 SDValue Ops[2] = { LHSInt, RHSInt };
659 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
660 NewRHS = DAG.getConstant(0, RetVT);
661 CCCode = TLI.getCmpLibcallCC(LC1);
662 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
663 SDValue Tmp = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT),
664 NewLHS, NewRHS, DAG.getCondCode(CCCode));
665 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
666 NewLHS = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT), NewLHS,
667 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
668 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
673 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
674 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
675 GetSoftenedFloat(N->getOperand(0)));
678 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
679 EVT SVT = N->getOperand(0).getValueType();
680 EVT RVT = N->getValueType(0);
682 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
683 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
685 SDValue Op = GetSoftenedFloat(N->getOperand(0));
686 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
689 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
690 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
691 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
692 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
694 // If SoftenSetCCOperands returned a scalar, we need to compare the result
695 // against zero to select between true and false values.
696 if (NewRHS.getNode() == 0) {
697 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
701 // Update N to have the operands specified.
702 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
703 DAG.getCondCode(CCCode), NewLHS, NewRHS,
708 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
709 EVT RVT = N->getValueType(0);
710 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
711 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
712 SDValue Op = GetSoftenedFloat(N->getOperand(0));
713 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
716 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
717 EVT RVT = N->getValueType(0);
718 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
719 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
720 SDValue Op = GetSoftenedFloat(N->getOperand(0));
721 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
724 SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
725 EVT RVT = N->getValueType(0);
726 RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
727 SDValue Op = GetSoftenedFloat(N->getOperand(0));
728 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
731 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
732 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
733 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
734 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
736 // If SoftenSetCCOperands returned a scalar, we need to compare the result
737 // against zero to select between true and false values.
738 if (NewRHS.getNode() == 0) {
739 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
743 // Update N to have the operands specified.
744 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
745 N->getOperand(2), N->getOperand(3),
746 DAG.getCondCode(CCCode)),
750 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
751 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
752 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
753 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
755 // If SoftenSetCCOperands returned a scalar, use it.
756 if (NewRHS.getNode() == 0) {
757 assert(NewLHS.getValueType() == N->getValueType(0) &&
758 "Unexpected setcc expansion!");
762 // Otherwise, update N to have the operands specified.
763 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
764 DAG.getCondCode(CCCode)),
768 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
769 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
770 assert(OpNo == 1 && "Can only soften the stored value!");
771 StoreSDNode *ST = cast<StoreSDNode>(N);
772 SDValue Val = ST->getValue();
773 DebugLoc dl = N->getDebugLoc();
775 if (ST->isTruncatingStore())
776 // Do an FP_ROUND followed by a non-truncating store.
777 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
778 Val, DAG.getIntPtrConstant(0)));
780 Val = GetSoftenedFloat(Val);
782 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
783 ST->getPointerInfo(),
784 ST->isVolatile(), ST->isNonTemporal(),
789 //===----------------------------------------------------------------------===//
790 // Float Result Expansion
791 //===----------------------------------------------------------------------===//
793 /// ExpandFloatResult - This method is called when the specified result of the
794 /// specified node is found to need expansion. At this point, the node may also
795 /// have invalid operands or may have other results that need promotion, we just
796 /// know that (at least) one result needs expansion.
797 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
798 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
802 // See if the target wants to custom expand this node.
803 if (CustomLowerNode(N, N->getValueType(ResNo), true))
806 switch (N->getOpcode()) {
809 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
810 N->dump(&DAG); dbgs() << "\n";
812 llvm_unreachable("Do not know how to expand the result of this operator!");
814 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
815 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
816 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
817 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
819 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
820 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
821 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
822 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
823 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
825 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
826 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
827 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
828 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
829 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
830 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
831 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
832 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
833 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
834 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
835 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
836 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
837 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
838 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
839 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
840 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
841 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
842 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
843 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
844 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
845 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
846 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
847 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
848 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
849 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
850 case ISD::SINT_TO_FP:
851 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
854 // If Lo/Hi is null, the sub-method took care of registering results etc.
856 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
859 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
861 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
862 assert(NVT.getSizeInBits() == integerPartWidth &&
863 "Do not know how to expand this float constant!");
864 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
865 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
866 &C.getRawData()[1])), NVT);
867 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
868 &C.getRawData()[0])), NVT);
871 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
873 assert(N->getValueType(0) == MVT::ppcf128 &&
874 "Logic only correct for ppcf128!");
875 DebugLoc dl = N->getDebugLoc();
877 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
878 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
879 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
880 Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
881 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
882 DAG.getCondCode(ISD::SETEQ));
885 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
887 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
888 RTLIB::ADD_F32, RTLIB::ADD_F64,
889 RTLIB::ADD_F80, RTLIB::ADD_PPCF128),
891 GetPairElements(Call, Lo, Hi);
894 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
895 SDValue &Lo, SDValue &Hi) {
896 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
897 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
898 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128),
900 GetPairElements(Call, Lo, Hi);
903 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
904 SDValue &Lo, SDValue &Hi) {
905 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
909 RTLIB::COPYSIGN_PPCF128),
911 GetPairElements(Call, Lo, Hi);
914 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
915 SDValue &Lo, SDValue &Hi) {
916 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
917 RTLIB::COS_F32, RTLIB::COS_F64,
918 RTLIB::COS_F80, RTLIB::COS_PPCF128),
920 GetPairElements(Call, Lo, Hi);
923 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
925 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
926 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
931 N->getValueType(0), Ops, 2, false,
933 GetPairElements(Call, Lo, Hi);
936 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
937 SDValue &Lo, SDValue &Hi) {
938 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
939 RTLIB::EXP_F32, RTLIB::EXP_F64,
940 RTLIB::EXP_F80, RTLIB::EXP_PPCF128),
942 GetPairElements(Call, Lo, Hi);
945 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
946 SDValue &Lo, SDValue &Hi) {
947 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
948 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
949 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128),
951 GetPairElements(Call, Lo, Hi);
954 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
955 SDValue &Lo, SDValue &Hi) {
956 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
957 RTLIB::FLOOR_F32,RTLIB::FLOOR_F64,
958 RTLIB::FLOOR_F80,RTLIB::FLOOR_PPCF128),
960 GetPairElements(Call, Lo, Hi);
963 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
964 SDValue &Lo, SDValue &Hi) {
965 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
966 RTLIB::LOG_F32, RTLIB::LOG_F64,
967 RTLIB::LOG_F80, RTLIB::LOG_PPCF128),
969 GetPairElements(Call, Lo, Hi);
972 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
973 SDValue &Lo, SDValue &Hi) {
974 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
975 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
976 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128),
978 GetPairElements(Call, Lo, Hi);
981 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
982 SDValue &Lo, SDValue &Hi) {
983 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
984 RTLIB::LOG10_F32,RTLIB::LOG10_F64,
985 RTLIB::LOG10_F80,RTLIB::LOG10_PPCF128),
987 GetPairElements(Call, Lo, Hi);
990 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
992 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
993 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
998 N->getValueType(0), Ops, 2, false,
1000 GetPairElements(Call, Lo, Hi);
1003 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1004 SDValue &Lo, SDValue &Hi) {
1005 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1006 RTLIB::NEARBYINT_F32,
1007 RTLIB::NEARBYINT_F64,
1008 RTLIB::NEARBYINT_F80,
1009 RTLIB::NEARBYINT_PPCF128),
1011 GetPairElements(Call, Lo, Hi);
1014 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1016 DebugLoc dl = N->getDebugLoc();
1017 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1018 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1019 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1022 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1024 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1025 Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
1026 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1029 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1030 SDValue &Lo, SDValue &Hi) {
1031 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1032 RTLIB::POW_F32, RTLIB::POW_F64,
1033 RTLIB::POW_F80, RTLIB::POW_PPCF128),
1035 GetPairElements(Call, Lo, Hi);
1038 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1039 SDValue &Lo, SDValue &Hi) {
1040 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1041 RTLIB::POWI_F32, RTLIB::POWI_F64,
1042 RTLIB::POWI_F80, RTLIB::POWI_PPCF128),
1044 GetPairElements(Call, Lo, Hi);
1047 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1048 SDValue &Lo, SDValue &Hi) {
1049 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1050 RTLIB::RINT_F32, RTLIB::RINT_F64,
1051 RTLIB::RINT_F80, RTLIB::RINT_PPCF128),
1053 GetPairElements(Call, Lo, Hi);
1056 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1057 SDValue &Lo, SDValue &Hi) {
1058 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1059 RTLIB::SIN_F32, RTLIB::SIN_F64,
1060 RTLIB::SIN_F80, RTLIB::SIN_PPCF128),
1062 GetPairElements(Call, Lo, Hi);
1065 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1066 SDValue &Lo, SDValue &Hi) {
1067 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1068 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1069 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128),
1071 GetPairElements(Call, Lo, Hi);
1074 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1076 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1077 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
1081 RTLIB::SUB_PPCF128),
1082 N->getValueType(0), Ops, 2, false,
1084 GetPairElements(Call, Lo, Hi);
1087 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1088 SDValue &Lo, SDValue &Hi) {
1089 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1090 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1091 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128),
1093 GetPairElements(Call, Lo, Hi);
1096 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1098 if (ISD::isNormalLoad(N)) {
1099 ExpandRes_NormalLoad(N, Lo, Hi);
1103 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1104 LoadSDNode *LD = cast<LoadSDNode>(N);
1105 SDValue Chain = LD->getChain();
1106 SDValue Ptr = LD->getBasePtr();
1107 DebugLoc dl = N->getDebugLoc();
1109 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1110 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1111 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1113 Hi = DAG.getExtLoad(LD->getExtensionType(), NVT, dl, Chain, Ptr,
1114 LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
1115 LD->isNonTemporal(), LD->getAlignment());
1117 // Remember the chain.
1118 Chain = Hi.getValue(1);
1120 // The low part is zero.
1121 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1123 // Modified the chain - switch anything that used the old chain to use the
1125 ReplaceValueWith(SDValue(LD, 1), Chain);
1128 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1130 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1131 EVT VT = N->getValueType(0);
1132 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1133 SDValue Src = N->getOperand(0);
1134 EVT SrcVT = Src.getValueType();
1135 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1136 DebugLoc dl = N->getDebugLoc();
1138 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1139 // When promoting partial word types to i32 we must honor the signedness,
1141 if (SrcVT.bitsLE(MVT::i32)) {
1142 // The integer can be represented exactly in an f64.
1143 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1145 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1146 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1148 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1149 if (SrcVT.bitsLE(MVT::i64)) {
1150 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1152 LC = RTLIB::SINTTOFP_I64_PPCF128;
1153 } else if (SrcVT.bitsLE(MVT::i128)) {
1154 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1155 LC = RTLIB::SINTTOFP_I128_PPCF128;
1157 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1159 Hi = MakeLibCall(LC, VT, &Src, 1, true, dl);
1160 GetPairElements(Hi, Lo, Hi);
1166 // Unsigned - fix up the SINT_TO_FP value just calculated.
1167 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1168 SrcVT = Src.getValueType();
1170 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1171 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1172 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1173 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1174 const uint64_t *Parts = 0;
1176 switch (SrcVT.getSimpleVT().SimpleTy) {
1178 assert(false && "Unsupported UINT_TO_FP!");
1190 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1191 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
1193 Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
1194 Lo, Hi, DAG.getCondCode(ISD::SETLT));
1195 GetPairElements(Lo, Lo, Hi);
1199 //===----------------------------------------------------------------------===//
1200 // Float Operand Expansion
1201 //===----------------------------------------------------------------------===//
1203 /// ExpandFloatOperand - This method is called when the specified operand of the
1204 /// specified node is found to need expansion. At this point, all of the result
1205 /// types of the node are known to be legal, but other operands of the node may
1206 /// need promotion or expansion as well as the specified one.
1207 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1208 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1209 SDValue Res = SDValue();
1211 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1212 == TargetLowering::Custom)
1213 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1215 if (Res.getNode() == 0) {
1216 switch (N->getOpcode()) {
1219 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1220 N->dump(&DAG); dbgs() << "\n";
1222 llvm_unreachable("Do not know how to expand this operator's operand!");
1224 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1225 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1226 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1228 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1229 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1230 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1231 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1232 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1233 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1234 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1239 // If the result is null, the sub-method took care of registering results etc.
1240 if (!Res.getNode()) return false;
1242 // If the result is N, the sub-method updated N in place. Tell the legalizer
1244 if (Res.getNode() == N)
1247 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1248 "Invalid operand expansion");
1250 ReplaceValueWith(SDValue(N, 0), Res);
1254 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1255 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1256 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1258 ISD::CondCode &CCCode,
1260 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1261 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1262 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1264 EVT VT = NewLHS.getValueType();
1265 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1267 // FIXME: This generated code sucks. We want to generate
1268 // FCMPU crN, hi1, hi2
1270 // FCMPU crN, lo1, lo2
1271 // The following can be improved, but not that much.
1272 SDValue Tmp1, Tmp2, Tmp3;
1273 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1274 LHSHi, RHSHi, ISD::SETOEQ);
1275 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
1276 LHSLo, RHSLo, CCCode);
1277 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1278 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1279 LHSHi, RHSHi, ISD::SETUNE);
1280 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1281 LHSHi, RHSHi, CCCode);
1282 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1283 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1284 NewRHS = SDValue(); // LHS is the result, not a compare.
1287 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1288 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1289 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1290 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1292 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1293 // against zero to select between true and false values.
1294 if (NewRHS.getNode() == 0) {
1295 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1296 CCCode = ISD::SETNE;
1299 // Update N to have the operands specified.
1300 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1301 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1302 N->getOperand(4)), 0);
1305 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1306 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1307 "Logic only correct for ppcf128!");
1309 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1310 // Round it the rest of the way (e.g. to f32) if needed.
1311 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1312 N->getValueType(0), Hi, N->getOperand(1));
1315 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1316 EVT RVT = N->getValueType(0);
1317 DebugLoc dl = N->getDebugLoc();
1319 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1320 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1321 if (RVT == MVT::i32) {
1322 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1323 "Logic only correct for ppcf128!");
1324 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1325 N->getOperand(0), DAG.getValueType(MVT::f64));
1326 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1327 DAG.getIntPtrConstant(1));
1328 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1331 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1332 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1333 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false, dl);
1336 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1337 EVT RVT = N->getValueType(0);
1338 DebugLoc dl = N->getDebugLoc();
1340 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1341 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1342 if (RVT == MVT::i32) {
1343 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1344 "Logic only correct for ppcf128!");
1345 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1346 APFloat APF = APFloat(APInt(128, 2, TwoE31));
1347 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1348 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1349 // FIXME: generated code sucks.
1350 return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1351 DAG.getNode(ISD::ADD, dl, MVT::i32,
1352 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1353 DAG.getNode(ISD::FSUB, dl,
1357 DAG.getConstant(0x80000000, MVT::i32)),
1358 DAG.getNode(ISD::FP_TO_SINT, dl,
1359 MVT::i32, N->getOperand(0)),
1360 DAG.getCondCode(ISD::SETGE));
1363 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1364 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1365 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false, dl);
1368 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1369 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1370 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1371 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1373 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1374 // against zero to select between true and false values.
1375 if (NewRHS.getNode() == 0) {
1376 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1377 CCCode = ISD::SETNE;
1380 // Update N to have the operands specified.
1381 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1382 N->getOperand(2), N->getOperand(3),
1383 DAG.getCondCode(CCCode)), 0);
1386 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1387 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1388 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1389 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1391 // If ExpandSetCCOperands returned a scalar, use it.
1392 if (NewRHS.getNode() == 0) {
1393 assert(NewLHS.getValueType() == N->getValueType(0) &&
1394 "Unexpected setcc expansion!");
1398 // Otherwise, update N to have the operands specified.
1399 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1400 DAG.getCondCode(CCCode)), 0);
1403 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1404 if (ISD::isNormalStore(N))
1405 return ExpandOp_NormalStore(N, OpNo);
1407 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1408 assert(OpNo == 1 && "Can only expand the stored value so far");
1409 StoreSDNode *ST = cast<StoreSDNode>(N);
1411 SDValue Chain = ST->getChain();
1412 SDValue Ptr = ST->getBasePtr();
1414 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1415 ST->getValue().getValueType());
1416 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1417 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1420 GetExpandedOp(ST->getValue(), Lo, Hi);
1422 return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
1423 ST->getPointerInfo(),
1424 ST->getMemoryVT(), ST->isVolatile(),
1425 ST->isNonTemporal(), ST->getAlignment());