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);
137 API.clearBit(Size-1);
138 SDValue Mask = DAG.getConstant(API, NVT);
139 SDValue Op = GetSoftenedFloat(N->getOperand(0));
140 return DAG.getNode(ISD::AND, N->getDebugLoc(), NVT, Op, Mask);
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(RVT)));
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,
188 TLI.getShiftAmountTy(SignBit.getValueType())));
189 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
190 } else if (SizeDiff < 0) {
191 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
192 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
193 DAG.getConstant(-SizeDiff,
194 TLI.getShiftAmountTy(SignBit.getValueType())));
197 // Clear the sign bit of the first operand.
198 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, LVT),
199 DAG.getConstant(LSize - 1,
200 TLI.getShiftAmountTy(LVT)));
201 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, LVT));
202 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
204 // Or the value with the sign bit.
205 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
208 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
209 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
210 SDValue Op = GetSoftenedFloat(N->getOperand(0));
211 return MakeLibCall(GetFPLibCall(N->getValueType(0),
216 NVT, &Op, 1, false, N->getDebugLoc());
219 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
220 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
221 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
222 GetSoftenedFloat(N->getOperand(1)) };
223 return MakeLibCall(GetFPLibCall(N->getValueType(0),
228 NVT, Ops, 2, false, N->getDebugLoc());
231 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
232 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
233 SDValue Op = GetSoftenedFloat(N->getOperand(0));
234 return MakeLibCall(GetFPLibCall(N->getValueType(0),
239 NVT, &Op, 1, false, N->getDebugLoc());
242 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
243 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
244 SDValue Op = GetSoftenedFloat(N->getOperand(0));
245 return MakeLibCall(GetFPLibCall(N->getValueType(0),
249 RTLIB::EXP2_PPCF128),
250 NVT, &Op, 1, false, N->getDebugLoc());
253 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
254 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
255 SDValue Op = GetSoftenedFloat(N->getOperand(0));
256 return MakeLibCall(GetFPLibCall(N->getValueType(0),
260 RTLIB::FLOOR_PPCF128),
261 NVT, &Op, 1, false, N->getDebugLoc());
264 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
265 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
266 SDValue Op = GetSoftenedFloat(N->getOperand(0));
267 return MakeLibCall(GetFPLibCall(N->getValueType(0),
272 NVT, &Op, 1, false, N->getDebugLoc());
275 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
276 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
277 SDValue Op = GetSoftenedFloat(N->getOperand(0));
278 return MakeLibCall(GetFPLibCall(N->getValueType(0),
282 RTLIB::LOG2_PPCF128),
283 NVT, &Op, 1, false, N->getDebugLoc());
286 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
287 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
288 SDValue Op = GetSoftenedFloat(N->getOperand(0));
289 return MakeLibCall(GetFPLibCall(N->getValueType(0),
293 RTLIB::LOG10_PPCF128),
294 NVT, &Op, 1, false, N->getDebugLoc());
297 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
298 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
299 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
300 GetSoftenedFloat(N->getOperand(1)) };
301 return MakeLibCall(GetFPLibCall(N->getValueType(0),
306 NVT, Ops, 2, false, N->getDebugLoc());
309 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
310 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
311 SDValue Op = GetSoftenedFloat(N->getOperand(0));
312 return MakeLibCall(GetFPLibCall(N->getValueType(0),
313 RTLIB::NEARBYINT_F32,
314 RTLIB::NEARBYINT_F64,
315 RTLIB::NEARBYINT_F80,
316 RTLIB::NEARBYINT_PPCF128),
317 NVT, &Op, 1, false, N->getDebugLoc());
320 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
321 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
322 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
323 SDValue Ops[2] = { DAG.getConstantFP(-0.0, N->getValueType(0)),
324 GetSoftenedFloat(N->getOperand(0)) };
325 return MakeLibCall(GetFPLibCall(N->getValueType(0),
330 NVT, Ops, 2, false, N->getDebugLoc());
333 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
334 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
335 SDValue Op = N->getOperand(0);
336 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
337 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
338 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
341 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
343 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP32(SDNode *N) {
344 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
345 SDValue Op = N->getOperand(0);
346 return MakeLibCall(RTLIB::FPEXT_F16_F32, NVT, &Op, 1, false,
350 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
351 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
352 SDValue Op = N->getOperand(0);
353 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
354 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
355 return MakeLibCall(LC, NVT, &Op, 1, false, N->getDebugLoc());
358 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
359 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
360 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
361 GetSoftenedFloat(N->getOperand(1)) };
362 return MakeLibCall(GetFPLibCall(N->getValueType(0),
367 NVT, Ops, 2, false, N->getDebugLoc());
370 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
371 assert(N->getOperand(1).getValueType() == MVT::i32 &&
372 "Unsupported power type!");
373 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
374 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
375 return MakeLibCall(GetFPLibCall(N->getValueType(0),
379 RTLIB::POWI_PPCF128),
380 NVT, Ops, 2, false, N->getDebugLoc());
383 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
384 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
385 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
386 GetSoftenedFloat(N->getOperand(1)) };
387 return MakeLibCall(GetFPLibCall(N->getValueType(0),
392 NVT, Ops, 2, false, N->getDebugLoc());
395 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
396 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
397 SDValue Op = GetSoftenedFloat(N->getOperand(0));
398 return MakeLibCall(GetFPLibCall(N->getValueType(0),
402 RTLIB::RINT_PPCF128),
403 NVT, &Op, 1, false, N->getDebugLoc());
406 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
407 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
408 SDValue Op = GetSoftenedFloat(N->getOperand(0));
409 return MakeLibCall(GetFPLibCall(N->getValueType(0),
414 NVT, &Op, 1, false, N->getDebugLoc());
417 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
418 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
419 SDValue Op = GetSoftenedFloat(N->getOperand(0));
420 return MakeLibCall(GetFPLibCall(N->getValueType(0),
424 RTLIB::SQRT_PPCF128),
425 NVT, &Op, 1, false, N->getDebugLoc());
428 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
429 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
430 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
431 GetSoftenedFloat(N->getOperand(1)) };
432 return MakeLibCall(GetFPLibCall(N->getValueType(0),
437 NVT, Ops, 2, false, N->getDebugLoc());
440 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
441 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
442 SDValue Op = GetSoftenedFloat(N->getOperand(0));
443 return MakeLibCall(GetFPLibCall(N->getValueType(0),
447 RTLIB::TRUNC_PPCF128),
448 NVT, &Op, 1, false, N->getDebugLoc());
451 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
452 LoadSDNode *L = cast<LoadSDNode>(N);
453 EVT VT = N->getValueType(0);
454 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
455 DebugLoc dl = N->getDebugLoc();
458 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
459 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
460 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
461 L->getPointerInfo(), NVT,
462 L->isVolatile(), L->isNonTemporal(), L->getAlignment());
463 // Legalized the chain result - switch anything that used the old chain to
465 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
469 // Do a non-extending load followed by FP_EXTEND.
470 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
471 L->getMemoryVT(), dl, L->getChain(),
472 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
473 L->getMemoryVT(), L->isVolatile(),
474 L->isNonTemporal(), L->getAlignment());
475 // Legalized the chain result - switch anything that used the old chain to
477 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
478 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
481 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
482 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
483 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
484 return DAG.getNode(ISD::SELECT, N->getDebugLoc(),
485 LHS.getValueType(), N->getOperand(0),LHS,RHS);
488 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
489 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
490 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
491 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(),
492 LHS.getValueType(), N->getOperand(0),
493 N->getOperand(1), LHS, RHS, N->getOperand(4));
496 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
497 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
498 N->getValueType(0)));
501 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
502 SDValue Chain = N->getOperand(0); // Get the chain.
503 SDValue Ptr = N->getOperand(1); // Get the pointer.
504 EVT VT = N->getValueType(0);
505 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
506 DebugLoc dl = N->getDebugLoc();
509 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
510 N->getConstantOperandVal(3));
512 // Legalized the chain result - switch anything that used the old chain to
514 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
518 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
519 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
520 EVT SVT = N->getOperand(0).getValueType();
521 EVT RVT = N->getValueType(0);
523 DebugLoc dl = N->getDebugLoc();
525 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
526 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
527 // match. Look for an appropriate libcall.
528 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
529 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
530 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
531 NVT = (MVT::SimpleValueType)t;
532 // The source needs to big enough to hold the operand.
534 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
536 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
538 // Sign/zero extend the argument if the libcall takes a larger type.
539 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
540 NVT, N->getOperand(0));
541 return MakeLibCall(LC, TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
546 //===----------------------------------------------------------------------===//
547 // Operand Float to Integer Conversion..
548 //===----------------------------------------------------------------------===//
550 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
551 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
553 SDValue Res = SDValue();
555 switch (N->getOpcode()) {
558 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
559 N->dump(&DAG); dbgs() << "\n";
561 llvm_unreachable("Do not know how to soften this operator's operand!");
563 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
564 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
565 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
566 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
567 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
568 case ISD::FP32_TO_FP16:Res = SoftenFloatOp_FP32_TO_FP16(N); break;
569 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
570 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
571 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
574 // If the result is null, the sub-method took care of registering results etc.
575 if (!Res.getNode()) return false;
577 // If the result is N, the sub-method updated N in place. Tell the legalizer
579 if (Res.getNode() == N)
582 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
583 "Invalid operand expansion");
585 ReplaceValueWith(SDValue(N, 0), Res);
589 /// SoftenSetCCOperands - Soften the operands of a comparison. This code is
590 /// shared among BR_CC, SELECT_CC, and SETCC handlers.
591 void DAGTypeLegalizer::SoftenSetCCOperands(SDValue &NewLHS, SDValue &NewRHS,
592 ISD::CondCode &CCCode, DebugLoc dl) {
593 SDValue LHSInt = GetSoftenedFloat(NewLHS);
594 SDValue RHSInt = GetSoftenedFloat(NewRHS);
595 EVT VT = NewLHS.getValueType();
597 assert((VT == MVT::f32 || VT == MVT::f64) && "Unsupported setcc type!");
599 // Expand into one or more soft-fp libcall(s).
600 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
604 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
608 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
612 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
616 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
620 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
624 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
627 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
630 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
633 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
636 // SETONE = SETOLT | SETOGT
637 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
640 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
643 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
646 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
649 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
652 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
654 default: assert(false && "Do not know how to soften this setcc!");
658 // Use the target specific return value for comparions lib calls.
659 EVT RetVT = TLI.getCmpLibcallReturnType();
660 SDValue Ops[2] = { LHSInt, RHSInt };
661 NewLHS = MakeLibCall(LC1, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
662 NewRHS = DAG.getConstant(0, RetVT);
663 CCCode = TLI.getCmpLibcallCC(LC1);
664 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
665 SDValue Tmp = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT),
666 NewLHS, NewRHS, DAG.getCondCode(CCCode));
667 NewLHS = MakeLibCall(LC2, RetVT, Ops, 2, false/*sign irrelevant*/, dl);
668 NewLHS = DAG.getNode(ISD::SETCC, dl, TLI.getSetCCResultType(RetVT), NewLHS,
669 NewRHS, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
670 NewLHS = DAG.getNode(ISD::OR, dl, Tmp.getValueType(), Tmp, NewLHS);
675 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
676 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getValueType(0),
677 GetSoftenedFloat(N->getOperand(0)));
680 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
681 EVT SVT = N->getOperand(0).getValueType();
682 EVT RVT = N->getValueType(0);
684 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, RVT);
685 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
687 SDValue Op = GetSoftenedFloat(N->getOperand(0));
688 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
691 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
692 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
693 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
694 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
696 // If SoftenSetCCOperands returned a scalar, we need to compare the result
697 // against zero to select between true and false values.
698 if (NewRHS.getNode() == 0) {
699 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
703 // Update N to have the operands specified.
704 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
705 DAG.getCondCode(CCCode), NewLHS, NewRHS,
710 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
711 EVT RVT = N->getValueType(0);
712 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
713 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
714 SDValue Op = GetSoftenedFloat(N->getOperand(0));
715 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
718 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
719 EVT RVT = N->getValueType(0);
720 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
721 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
722 SDValue Op = GetSoftenedFloat(N->getOperand(0));
723 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
726 SDValue DAGTypeLegalizer::SoftenFloatOp_FP32_TO_FP16(SDNode *N) {
727 EVT RVT = N->getValueType(0);
728 RTLIB::Libcall LC = RTLIB::FPROUND_F32_F16;
729 SDValue Op = GetSoftenedFloat(N->getOperand(0));
730 return MakeLibCall(LC, RVT, &Op, 1, false, N->getDebugLoc());
733 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
734 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
735 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
736 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
738 // If SoftenSetCCOperands returned a scalar, we need to compare the result
739 // against zero to select between true and false values.
740 if (NewRHS.getNode() == 0) {
741 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
745 // Update N to have the operands specified.
746 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
747 N->getOperand(2), N->getOperand(3),
748 DAG.getCondCode(CCCode)),
752 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
753 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
754 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
755 SoftenSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
757 // If SoftenSetCCOperands returned a scalar, use it.
758 if (NewRHS.getNode() == 0) {
759 assert(NewLHS.getValueType() == N->getValueType(0) &&
760 "Unexpected setcc expansion!");
764 // Otherwise, update N to have the operands specified.
765 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
766 DAG.getCondCode(CCCode)),
770 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
771 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
772 assert(OpNo == 1 && "Can only soften the stored value!");
773 StoreSDNode *ST = cast<StoreSDNode>(N);
774 SDValue Val = ST->getValue();
775 DebugLoc dl = N->getDebugLoc();
777 if (ST->isTruncatingStore())
778 // Do an FP_ROUND followed by a non-truncating store.
779 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
780 Val, DAG.getIntPtrConstant(0)));
782 Val = GetSoftenedFloat(Val);
784 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
785 ST->getPointerInfo(),
786 ST->isVolatile(), ST->isNonTemporal(),
791 //===----------------------------------------------------------------------===//
792 // Float Result Expansion
793 //===----------------------------------------------------------------------===//
795 /// ExpandFloatResult - This method is called when the specified result of the
796 /// specified node is found to need expansion. At this point, the node may also
797 /// have invalid operands or may have other results that need promotion, we just
798 /// know that (at least) one result needs expansion.
799 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
800 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
804 // See if the target wants to custom expand this node.
805 if (CustomLowerNode(N, N->getValueType(ResNo), true))
808 switch (N->getOpcode()) {
811 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
812 N->dump(&DAG); dbgs() << "\n";
814 llvm_unreachable("Do not know how to expand the result of this operator!");
816 case ISD::MERGE_VALUES: SplitRes_MERGE_VALUES(N, Lo, Hi); break;
817 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
818 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
819 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
821 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
822 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
823 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
824 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
825 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
827 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
828 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
829 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
830 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
831 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
832 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
833 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
834 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
835 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
836 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
837 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
838 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
839 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
840 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
841 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
842 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
843 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
844 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
845 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
846 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
847 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
848 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
849 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
850 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
851 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
852 case ISD::SINT_TO_FP:
853 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
856 // If Lo/Hi is null, the sub-method took care of registering results etc.
858 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
861 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
863 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
864 assert(NVT.getSizeInBits() == integerPartWidth &&
865 "Do not know how to expand this float constant!");
866 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
867 Lo = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
868 &C.getRawData()[1])), NVT);
869 Hi = DAG.getConstantFP(APFloat(APInt(integerPartWidth, 1,
870 &C.getRawData()[0])), NVT);
873 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
875 assert(N->getValueType(0) == MVT::ppcf128 &&
876 "Logic only correct for ppcf128!");
877 DebugLoc dl = N->getDebugLoc();
879 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
880 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
881 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
882 Lo = DAG.getNode(ISD::SELECT_CC, dl, Lo.getValueType(), Tmp, Hi, Lo,
883 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
884 DAG.getCondCode(ISD::SETEQ));
887 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
889 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
890 RTLIB::ADD_F32, RTLIB::ADD_F64,
891 RTLIB::ADD_F80, RTLIB::ADD_PPCF128),
893 GetPairElements(Call, Lo, Hi);
896 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
897 SDValue &Lo, SDValue &Hi) {
898 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
899 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
900 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128),
902 GetPairElements(Call, Lo, Hi);
905 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
906 SDValue &Lo, SDValue &Hi) {
907 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
911 RTLIB::COPYSIGN_PPCF128),
913 GetPairElements(Call, Lo, Hi);
916 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
917 SDValue &Lo, SDValue &Hi) {
918 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
919 RTLIB::COS_F32, RTLIB::COS_F64,
920 RTLIB::COS_F80, RTLIB::COS_PPCF128),
922 GetPairElements(Call, Lo, Hi);
925 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
927 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
928 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
933 N->getValueType(0), Ops, 2, false,
935 GetPairElements(Call, Lo, Hi);
938 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
939 SDValue &Lo, SDValue &Hi) {
940 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
941 RTLIB::EXP_F32, RTLIB::EXP_F64,
942 RTLIB::EXP_F80, RTLIB::EXP_PPCF128),
944 GetPairElements(Call, Lo, Hi);
947 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
948 SDValue &Lo, SDValue &Hi) {
949 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
950 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
951 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128),
953 GetPairElements(Call, Lo, Hi);
956 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
957 SDValue &Lo, SDValue &Hi) {
958 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
959 RTLIB::FLOOR_F32,RTLIB::FLOOR_F64,
960 RTLIB::FLOOR_F80,RTLIB::FLOOR_PPCF128),
962 GetPairElements(Call, Lo, Hi);
965 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
966 SDValue &Lo, SDValue &Hi) {
967 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
968 RTLIB::LOG_F32, RTLIB::LOG_F64,
969 RTLIB::LOG_F80, RTLIB::LOG_PPCF128),
971 GetPairElements(Call, Lo, Hi);
974 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
975 SDValue &Lo, SDValue &Hi) {
976 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
977 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
978 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128),
980 GetPairElements(Call, Lo, Hi);
983 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
984 SDValue &Lo, SDValue &Hi) {
985 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
986 RTLIB::LOG10_F32,RTLIB::LOG10_F64,
987 RTLIB::LOG10_F80,RTLIB::LOG10_PPCF128),
989 GetPairElements(Call, Lo, Hi);
992 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
994 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
995 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
1000 N->getValueType(0), Ops, 2, false,
1002 GetPairElements(Call, Lo, Hi);
1005 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1006 SDValue &Lo, SDValue &Hi) {
1007 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1008 RTLIB::NEARBYINT_F32,
1009 RTLIB::NEARBYINT_F64,
1010 RTLIB::NEARBYINT_F80,
1011 RTLIB::NEARBYINT_PPCF128),
1013 GetPairElements(Call, Lo, Hi);
1016 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1018 DebugLoc dl = N->getDebugLoc();
1019 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1020 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1021 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1024 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1026 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1027 Hi = DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), NVT, N->getOperand(0));
1028 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1031 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1032 SDValue &Lo, SDValue &Hi) {
1033 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1034 RTLIB::POW_F32, RTLIB::POW_F64,
1035 RTLIB::POW_F80, RTLIB::POW_PPCF128),
1037 GetPairElements(Call, Lo, Hi);
1040 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1041 SDValue &Lo, SDValue &Hi) {
1042 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1043 RTLIB::POWI_F32, RTLIB::POWI_F64,
1044 RTLIB::POWI_F80, RTLIB::POWI_PPCF128),
1046 GetPairElements(Call, Lo, Hi);
1049 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1050 SDValue &Lo, SDValue &Hi) {
1051 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1052 RTLIB::RINT_F32, RTLIB::RINT_F64,
1053 RTLIB::RINT_F80, RTLIB::RINT_PPCF128),
1055 GetPairElements(Call, Lo, Hi);
1058 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1059 SDValue &Lo, SDValue &Hi) {
1060 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1061 RTLIB::SIN_F32, RTLIB::SIN_F64,
1062 RTLIB::SIN_F80, RTLIB::SIN_PPCF128),
1064 GetPairElements(Call, Lo, Hi);
1067 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1068 SDValue &Lo, SDValue &Hi) {
1069 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1070 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1071 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128),
1073 GetPairElements(Call, Lo, Hi);
1076 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1078 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1079 SDValue Call = MakeLibCall(GetFPLibCall(N->getValueType(0),
1083 RTLIB::SUB_PPCF128),
1084 N->getValueType(0), Ops, 2, false,
1086 GetPairElements(Call, Lo, Hi);
1089 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1090 SDValue &Lo, SDValue &Hi) {
1091 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1092 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1093 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128),
1095 GetPairElements(Call, Lo, Hi);
1098 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1100 if (ISD::isNormalLoad(N)) {
1101 ExpandRes_NormalLoad(N, Lo, Hi);
1105 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1106 LoadSDNode *LD = cast<LoadSDNode>(N);
1107 SDValue Chain = LD->getChain();
1108 SDValue Ptr = LD->getBasePtr();
1109 DebugLoc dl = N->getDebugLoc();
1111 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1112 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1113 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1115 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1116 LD->getPointerInfo(), LD->getMemoryVT(), LD->isVolatile(),
1117 LD->isNonTemporal(), LD->getAlignment());
1119 // Remember the chain.
1120 Chain = Hi.getValue(1);
1122 // The low part is zero.
1123 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1125 // Modified the chain - switch anything that used the old chain to use the
1127 ReplaceValueWith(SDValue(LD, 1), Chain);
1130 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1132 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1133 EVT VT = N->getValueType(0);
1134 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1135 SDValue Src = N->getOperand(0);
1136 EVT SrcVT = Src.getValueType();
1137 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1138 DebugLoc dl = N->getDebugLoc();
1140 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1141 // When promoting partial word types to i32 we must honor the signedness,
1143 if (SrcVT.bitsLE(MVT::i32)) {
1144 // The integer can be represented exactly in an f64.
1145 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1147 Lo = DAG.getConstantFP(APFloat(APInt(NVT.getSizeInBits(), 0)), NVT);
1148 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1150 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1151 if (SrcVT.bitsLE(MVT::i64)) {
1152 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1154 LC = RTLIB::SINTTOFP_I64_PPCF128;
1155 } else if (SrcVT.bitsLE(MVT::i128)) {
1156 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1157 LC = RTLIB::SINTTOFP_I128_PPCF128;
1159 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1161 Hi = MakeLibCall(LC, VT, &Src, 1, true, dl);
1162 GetPairElements(Hi, Lo, Hi);
1168 // Unsigned - fix up the SINT_TO_FP value just calculated.
1169 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1170 SrcVT = Src.getValueType();
1172 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1173 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1174 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1175 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1176 const uint64_t *Parts = 0;
1178 switch (SrcVT.getSimpleVT().SimpleTy) {
1180 assert(false && "Unsupported UINT_TO_FP!");
1192 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1193 DAG.getConstantFP(APFloat(APInt(128, 2, Parts)),
1195 Lo = DAG.getNode(ISD::SELECT_CC, dl, VT, Src, DAG.getConstant(0, SrcVT),
1196 Lo, Hi, DAG.getCondCode(ISD::SETLT));
1197 GetPairElements(Lo, Lo, Hi);
1201 //===----------------------------------------------------------------------===//
1202 // Float Operand Expansion
1203 //===----------------------------------------------------------------------===//
1205 /// ExpandFloatOperand - This method is called when the specified operand of the
1206 /// specified node is found to need expansion. At this point, all of the result
1207 /// types of the node are known to be legal, but other operands of the node may
1208 /// need promotion or expansion as well as the specified one.
1209 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1210 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1211 SDValue Res = SDValue();
1213 if (TLI.getOperationAction(N->getOpcode(), N->getOperand(OpNo).getValueType())
1214 == TargetLowering::Custom)
1215 Res = TLI.LowerOperation(SDValue(N, 0), DAG);
1217 if (Res.getNode() == 0) {
1218 switch (N->getOpcode()) {
1221 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1222 N->dump(&DAG); dbgs() << "\n";
1224 llvm_unreachable("Do not know how to expand this operator's operand!");
1226 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1227 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1228 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1230 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1231 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1232 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1233 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1234 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1235 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1236 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1241 // If the result is null, the sub-method took care of registering results etc.
1242 if (!Res.getNode()) return false;
1244 // If the result is N, the sub-method updated N in place. Tell the legalizer
1246 if (Res.getNode() == N)
1249 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1250 "Invalid operand expansion");
1252 ReplaceValueWith(SDValue(N, 0), Res);
1256 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1257 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1258 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1260 ISD::CondCode &CCCode,
1262 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1263 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1264 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1266 EVT VT = NewLHS.getValueType();
1267 assert(VT == MVT::ppcf128 && "Unsupported setcc type!");
1269 // FIXME: This generated code sucks. We want to generate
1270 // FCMPU crN, hi1, hi2
1272 // FCMPU crN, lo1, lo2
1273 // The following can be improved, but not that much.
1274 SDValue Tmp1, Tmp2, Tmp3;
1275 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1276 LHSHi, RHSHi, ISD::SETOEQ);
1277 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSLo.getValueType()),
1278 LHSLo, RHSLo, CCCode);
1279 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1280 Tmp1 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1281 LHSHi, RHSHi, ISD::SETUNE);
1282 Tmp2 = DAG.getSetCC(dl, TLI.getSetCCResultType(LHSHi.getValueType()),
1283 LHSHi, RHSHi, CCCode);
1284 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1285 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1286 NewRHS = SDValue(); // LHS is the result, not a compare.
1289 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1290 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1291 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1292 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1294 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1295 // against zero to select between true and false values.
1296 if (NewRHS.getNode() == 0) {
1297 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1298 CCCode = ISD::SETNE;
1301 // Update N to have the operands specified.
1302 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1303 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1304 N->getOperand(4)), 0);
1307 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1308 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1309 "Logic only correct for ppcf128!");
1311 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1312 // Round it the rest of the way (e.g. to f32) if needed.
1313 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(),
1314 N->getValueType(0), Hi, N->getOperand(1));
1317 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1318 EVT RVT = N->getValueType(0);
1319 DebugLoc dl = N->getDebugLoc();
1321 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1322 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1323 if (RVT == MVT::i32) {
1324 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1325 "Logic only correct for ppcf128!");
1326 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1327 N->getOperand(0), DAG.getValueType(MVT::f64));
1328 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1329 DAG.getIntPtrConstant(1));
1330 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1333 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1334 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1335 return MakeLibCall(LC, RVT, &N->getOperand(0), 1, false, dl);
1338 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1339 EVT RVT = N->getValueType(0);
1340 DebugLoc dl = N->getDebugLoc();
1342 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1343 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1344 if (RVT == MVT::i32) {
1345 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1346 "Logic only correct for ppcf128!");
1347 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1348 APFloat APF = APFloat(APInt(128, 2, TwoE31));
1349 SDValue Tmp = DAG.getConstantFP(APF, MVT::ppcf128);
1350 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1351 // FIXME: generated code sucks.
1352 return DAG.getNode(ISD::SELECT_CC, dl, MVT::i32, N->getOperand(0), Tmp,
1353 DAG.getNode(ISD::ADD, dl, MVT::i32,
1354 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1355 DAG.getNode(ISD::FSUB, dl,
1359 DAG.getConstant(0x80000000, MVT::i32)),
1360 DAG.getNode(ISD::FP_TO_SINT, dl,
1361 MVT::i32, N->getOperand(0)),
1362 DAG.getCondCode(ISD::SETGE));
1365 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1366 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1367 return MakeLibCall(LC, N->getValueType(0), &N->getOperand(0), 1, false, dl);
1370 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1371 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1372 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1373 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1375 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1376 // against zero to select between true and false values.
1377 if (NewRHS.getNode() == 0) {
1378 NewRHS = DAG.getConstant(0, NewLHS.getValueType());
1379 CCCode = ISD::SETNE;
1382 // Update N to have the operands specified.
1383 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1384 N->getOperand(2), N->getOperand(3),
1385 DAG.getCondCode(CCCode)), 0);
1388 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1389 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1390 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1391 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, N->getDebugLoc());
1393 // If ExpandSetCCOperands returned a scalar, use it.
1394 if (NewRHS.getNode() == 0) {
1395 assert(NewLHS.getValueType() == N->getValueType(0) &&
1396 "Unexpected setcc expansion!");
1400 // Otherwise, update N to have the operands specified.
1401 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1402 DAG.getCondCode(CCCode)), 0);
1405 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1406 if (ISD::isNormalStore(N))
1407 return ExpandOp_NormalStore(N, OpNo);
1409 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1410 assert(OpNo == 1 && "Can only expand the stored value so far");
1411 StoreSDNode *ST = cast<StoreSDNode>(N);
1413 SDValue Chain = ST->getChain();
1414 SDValue Ptr = ST->getBasePtr();
1416 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1417 ST->getValue().getValueType());
1418 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1419 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1422 GetExpandedOp(ST->getValue(), Lo, Hi);
1424 return DAG.getTruncStore(Chain, N->getDebugLoc(), Hi, Ptr,
1425 ST->getPointerInfo(),
1426 ST->getMemoryVT(), ST->isVolatile(),
1427 ST->isNonTemporal(), ST->getAlignment());