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 #define DEBUG_TYPE "legalize-types"
29 /// GetFPLibCall - Return the right libcall for the given floating point type.
30 static RTLIB::Libcall GetFPLibCall(EVT VT,
31 RTLIB::Libcall Call_F32,
32 RTLIB::Libcall Call_F64,
33 RTLIB::Libcall Call_F80,
34 RTLIB::Libcall Call_F128,
35 RTLIB::Libcall Call_PPCF128) {
37 VT == MVT::f32 ? Call_F32 :
38 VT == MVT::f64 ? Call_F64 :
39 VT == MVT::f80 ? Call_F80 :
40 VT == MVT::f128 ? Call_F128 :
41 VT == MVT::ppcf128 ? Call_PPCF128 :
42 RTLIB::UNKNOWN_LIBCALL;
45 //===----------------------------------------------------------------------===//
46 // Result Float to Integer Conversion.
47 //===----------------------------------------------------------------------===//
49 void DAGTypeLegalizer::SoftenFloatResult(SDNode *N, unsigned ResNo) {
50 DEBUG(dbgs() << "Soften float result " << ResNo << ": "; N->dump(&DAG);
52 SDValue R = SDValue();
54 switch (N->getOpcode()) {
57 dbgs() << "SoftenFloatResult #" << ResNo << ": ";
58 N->dump(&DAG); dbgs() << "\n";
60 llvm_unreachable("Do not know how to soften the result of this operator!");
62 case ISD::MERGE_VALUES:R = SoftenFloatRes_MERGE_VALUES(N, ResNo); break;
63 case ISD::BITCAST: R = SoftenFloatRes_BITCAST(N); break;
64 case ISD::BUILD_PAIR: R = SoftenFloatRes_BUILD_PAIR(N); break;
66 R = SoftenFloatRes_ConstantFP(cast<ConstantFPSDNode>(N));
68 case ISD::EXTRACT_VECTOR_ELT:
69 R = SoftenFloatRes_EXTRACT_VECTOR_ELT(N); break;
70 case ISD::FABS: R = SoftenFloatRes_FABS(N); break;
71 case ISD::FMINNUM: R = SoftenFloatRes_FMINNUM(N); break;
72 case ISD::FMAXNUM: R = SoftenFloatRes_FMAXNUM(N); break;
73 case ISD::FADD: R = SoftenFloatRes_FADD(N); break;
74 case ISD::FCEIL: R = SoftenFloatRes_FCEIL(N); break;
75 case ISD::FCOPYSIGN: R = SoftenFloatRes_FCOPYSIGN(N); break;
76 case ISD::FCOS: R = SoftenFloatRes_FCOS(N); break;
77 case ISD::FDIV: R = SoftenFloatRes_FDIV(N); break;
78 case ISD::FEXP: R = SoftenFloatRes_FEXP(N); break;
79 case ISD::FEXP2: R = SoftenFloatRes_FEXP2(N); break;
80 case ISD::FFLOOR: R = SoftenFloatRes_FFLOOR(N); break;
81 case ISD::FLOG: R = SoftenFloatRes_FLOG(N); break;
82 case ISD::FLOG2: R = SoftenFloatRes_FLOG2(N); break;
83 case ISD::FLOG10: R = SoftenFloatRes_FLOG10(N); break;
84 case ISD::FMA: R = SoftenFloatRes_FMA(N); break;
85 case ISD::FMUL: R = SoftenFloatRes_FMUL(N); break;
86 case ISD::FNEARBYINT: R = SoftenFloatRes_FNEARBYINT(N); break;
87 case ISD::FNEG: R = SoftenFloatRes_FNEG(N); break;
88 case ISD::FP_EXTEND: R = SoftenFloatRes_FP_EXTEND(N); break;
89 case ISD::FP_ROUND: R = SoftenFloatRes_FP_ROUND(N); break;
90 case ISD::FP16_TO_FP: R = SoftenFloatRes_FP16_TO_FP(N); break;
91 case ISD::FPOW: R = SoftenFloatRes_FPOW(N); break;
92 case ISD::FPOWI: R = SoftenFloatRes_FPOWI(N); break;
93 case ISD::FREM: R = SoftenFloatRes_FREM(N); break;
94 case ISD::FRINT: R = SoftenFloatRes_FRINT(N); break;
95 case ISD::FROUND: R = SoftenFloatRes_FROUND(N); break;
96 case ISD::FSIN: R = SoftenFloatRes_FSIN(N); break;
97 case ISD::FSQRT: R = SoftenFloatRes_FSQRT(N); break;
98 case ISD::FSUB: R = SoftenFloatRes_FSUB(N); break;
99 case ISD::FTRUNC: R = SoftenFloatRes_FTRUNC(N); break;
100 case ISD::LOAD: R = SoftenFloatRes_LOAD(N); break;
101 case ISD::SELECT: R = SoftenFloatRes_SELECT(N); break;
102 case ISD::SELECT_CC: R = SoftenFloatRes_SELECT_CC(N); break;
103 case ISD::SINT_TO_FP:
104 case ISD::UINT_TO_FP: R = SoftenFloatRes_XINT_TO_FP(N); break;
105 case ISD::UNDEF: R = SoftenFloatRes_UNDEF(N); break;
106 case ISD::VAARG: R = SoftenFloatRes_VAARG(N); break;
109 // If R is null, the sub-method took care of registering the result.
111 SetSoftenedFloat(SDValue(N, ResNo), R);
114 SDValue DAGTypeLegalizer::SoftenFloatRes_BITCAST(SDNode *N) {
115 return BitConvertToInteger(N->getOperand(0));
118 SDValue DAGTypeLegalizer::SoftenFloatRes_MERGE_VALUES(SDNode *N,
120 SDValue Op = DisintegrateMERGE_VALUES(N, ResNo);
121 return BitConvertToInteger(Op);
124 SDValue DAGTypeLegalizer::SoftenFloatRes_BUILD_PAIR(SDNode *N) {
125 // Convert the inputs to integers, and build a new pair out of them.
126 return DAG.getNode(ISD::BUILD_PAIR, SDLoc(N),
127 TLI.getTypeToTransformTo(*DAG.getContext(),
129 BitConvertToInteger(N->getOperand(0)),
130 BitConvertToInteger(N->getOperand(1)));
133 SDValue DAGTypeLegalizer::SoftenFloatRes_ConstantFP(ConstantFPSDNode *N) {
134 return DAG.getConstant(N->getValueAPF().bitcastToAPInt(), SDLoc(N),
135 TLI.getTypeToTransformTo(*DAG.getContext(),
136 N->getValueType(0)));
139 SDValue DAGTypeLegalizer::SoftenFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
140 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
141 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N),
142 NewOp.getValueType().getVectorElementType(),
143 NewOp, N->getOperand(1));
146 SDValue DAGTypeLegalizer::SoftenFloatRes_FABS(SDNode *N) {
147 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
148 unsigned Size = NVT.getSizeInBits();
150 // Mask = ~(1 << (Size-1))
151 APInt API = APInt::getAllOnesValue(Size);
152 API.clearBit(Size - 1);
153 SDValue Mask = DAG.getConstant(API, SDLoc(N), NVT);
154 SDValue Op = GetSoftenedFloat(N->getOperand(0));
155 return DAG.getNode(ISD::AND, SDLoc(N), NVT, Op, Mask);
158 SDValue DAGTypeLegalizer::SoftenFloatRes_FMINNUM(SDNode *N) {
159 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
160 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
161 GetSoftenedFloat(N->getOperand(1)) };
162 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
167 RTLIB::FMIN_PPCF128),
168 NVT, Ops, 2, false, SDLoc(N)).first;
171 SDValue DAGTypeLegalizer::SoftenFloatRes_FMAXNUM(SDNode *N) {
172 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
173 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
174 GetSoftenedFloat(N->getOperand(1)) };
175 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
180 RTLIB::FMAX_PPCF128),
181 NVT, Ops, 2, false, SDLoc(N)).first;
184 SDValue DAGTypeLegalizer::SoftenFloatRes_FADD(SDNode *N) {
185 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
186 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
187 GetSoftenedFloat(N->getOperand(1)) };
188 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
194 NVT, Ops, 2, false, SDLoc(N)).first;
197 SDValue DAGTypeLegalizer::SoftenFloatRes_FCEIL(SDNode *N) {
198 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
199 SDValue Op = GetSoftenedFloat(N->getOperand(0));
200 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
205 RTLIB::CEIL_PPCF128),
206 NVT, &Op, 1, false, SDLoc(N)).first;
209 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOPYSIGN(SDNode *N) {
210 SDValue LHS = GetSoftenedFloat(N->getOperand(0));
211 SDValue RHS = BitConvertToInteger(N->getOperand(1));
214 EVT LVT = LHS.getValueType();
215 EVT RVT = RHS.getValueType();
217 unsigned LSize = LVT.getSizeInBits();
218 unsigned RSize = RVT.getSizeInBits();
220 // First get the sign bit of second operand.
221 SDValue SignBit = DAG.getNode(ISD::SHL, dl, RVT, DAG.getConstant(1, dl, RVT),
222 DAG.getConstant(RSize - 1, dl,
223 TLI.getShiftAmountTy(RVT)));
224 SignBit = DAG.getNode(ISD::AND, dl, RVT, RHS, SignBit);
226 // Shift right or sign-extend it if the two operands have different types.
227 int SizeDiff = RVT.getSizeInBits() - LVT.getSizeInBits();
229 SignBit = DAG.getNode(ISD::SRL, dl, RVT, SignBit,
230 DAG.getConstant(SizeDiff, dl,
231 TLI.getShiftAmountTy(SignBit.getValueType())));
232 SignBit = DAG.getNode(ISD::TRUNCATE, dl, LVT, SignBit);
233 } else if (SizeDiff < 0) {
234 SignBit = DAG.getNode(ISD::ANY_EXTEND, dl, LVT, SignBit);
235 SignBit = DAG.getNode(ISD::SHL, dl, LVT, SignBit,
236 DAG.getConstant(-SizeDiff, dl,
237 TLI.getShiftAmountTy(SignBit.getValueType())));
240 // Clear the sign bit of the first operand.
241 SDValue Mask = DAG.getNode(ISD::SHL, dl, LVT, DAG.getConstant(1, dl, LVT),
242 DAG.getConstant(LSize - 1, dl,
243 TLI.getShiftAmountTy(LVT)));
244 Mask = DAG.getNode(ISD::SUB, dl, LVT, Mask, DAG.getConstant(1, dl, LVT));
245 LHS = DAG.getNode(ISD::AND, dl, LVT, LHS, Mask);
247 // Or the value with the sign bit.
248 return DAG.getNode(ISD::OR, dl, LVT, LHS, SignBit);
251 SDValue DAGTypeLegalizer::SoftenFloatRes_FCOS(SDNode *N) {
252 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
253 SDValue Op = GetSoftenedFloat(N->getOperand(0));
254 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
260 NVT, &Op, 1, false, SDLoc(N)).first;
263 SDValue DAGTypeLegalizer::SoftenFloatRes_FDIV(SDNode *N) {
264 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
265 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
266 GetSoftenedFloat(N->getOperand(1)) };
267 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
273 NVT, Ops, 2, false, SDLoc(N)).first;
276 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP(SDNode *N) {
277 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
278 SDValue Op = GetSoftenedFloat(N->getOperand(0));
279 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
285 NVT, &Op, 1, false, SDLoc(N)).first;
288 SDValue DAGTypeLegalizer::SoftenFloatRes_FEXP2(SDNode *N) {
289 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
290 SDValue Op = GetSoftenedFloat(N->getOperand(0));
291 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
296 RTLIB::EXP2_PPCF128),
297 NVT, &Op, 1, false, SDLoc(N)).first;
300 SDValue DAGTypeLegalizer::SoftenFloatRes_FFLOOR(SDNode *N) {
301 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
302 SDValue Op = GetSoftenedFloat(N->getOperand(0));
303 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
308 RTLIB::FLOOR_PPCF128),
309 NVT, &Op, 1, false, SDLoc(N)).first;
312 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG(SDNode *N) {
313 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
314 SDValue Op = GetSoftenedFloat(N->getOperand(0));
315 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
321 NVT, &Op, 1, false, SDLoc(N)).first;
324 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG2(SDNode *N) {
325 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
326 SDValue Op = GetSoftenedFloat(N->getOperand(0));
327 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
332 RTLIB::LOG2_PPCF128),
333 NVT, &Op, 1, false, SDLoc(N)).first;
336 SDValue DAGTypeLegalizer::SoftenFloatRes_FLOG10(SDNode *N) {
337 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
338 SDValue Op = GetSoftenedFloat(N->getOperand(0));
339 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
344 RTLIB::LOG10_PPCF128),
345 NVT, &Op, 1, false, SDLoc(N)).first;
348 SDValue DAGTypeLegalizer::SoftenFloatRes_FMA(SDNode *N) {
349 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
350 SDValue Ops[3] = { GetSoftenedFloat(N->getOperand(0)),
351 GetSoftenedFloat(N->getOperand(1)),
352 GetSoftenedFloat(N->getOperand(2)) };
353 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
359 NVT, Ops, 3, false, SDLoc(N)).first;
362 SDValue DAGTypeLegalizer::SoftenFloatRes_FMUL(SDNode *N) {
363 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
364 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
365 GetSoftenedFloat(N->getOperand(1)) };
366 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
372 NVT, Ops, 2, false, SDLoc(N)).first;
375 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEARBYINT(SDNode *N) {
376 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
377 SDValue Op = GetSoftenedFloat(N->getOperand(0));
378 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
379 RTLIB::NEARBYINT_F32,
380 RTLIB::NEARBYINT_F64,
381 RTLIB::NEARBYINT_F80,
382 RTLIB::NEARBYINT_F128,
383 RTLIB::NEARBYINT_PPCF128),
384 NVT, &Op, 1, false, SDLoc(N)).first;
387 SDValue DAGTypeLegalizer::SoftenFloatRes_FNEG(SDNode *N) {
388 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
390 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
391 SDValue Ops[2] = { DAG.getConstantFP(-0.0, dl, N->getValueType(0)),
392 GetSoftenedFloat(N->getOperand(0)) };
393 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
399 NVT, Ops, 2, false, dl).first;
402 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_EXTEND(SDNode *N) {
403 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
404 SDValue Op = N->getOperand(0);
406 // There's only a libcall for f16 -> f32, so proceed in two stages. Also, it's
407 // entirely possible for both f16 and f32 to be legal, so use the fully
408 // hard-float FP_EXTEND rather than FP16_TO_FP.
409 if (Op.getValueType() == MVT::f16 && N->getValueType(0) != MVT::f32) {
410 Op = DAG.getNode(ISD::FP_EXTEND, SDLoc(N), MVT::f32, Op);
411 if (getTypeAction(MVT::f32) == TargetLowering::TypeSoftenFloat)
412 SoftenFloatResult(Op.getNode(), 0);
415 RTLIB::Libcall LC = RTLIB::getFPEXT(Op.getValueType(), N->getValueType(0));
416 if (getTypeAction(Op.getValueType()) == TargetLowering::TypeSoftenFloat)
417 Op = GetSoftenedFloat(Op);
418 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
419 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
422 // FIXME: Should we just use 'normal' FP_EXTEND / FP_TRUNC instead of special
424 SDValue DAGTypeLegalizer::SoftenFloatRes_FP16_TO_FP(SDNode *N) {
425 EVT MidVT = TLI.getTypeToTransformTo(*DAG.getContext(), MVT::f32);
426 SDValue Op = N->getOperand(0);
427 SDValue Res32 = TLI.makeLibCall(DAG, RTLIB::FPEXT_F16_F32, MidVT, &Op, 1,
428 false, SDLoc(N)).first;
429 if (N->getValueType(0) == MVT::f32)
432 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
433 RTLIB::Libcall LC = RTLIB::getFPEXT(MVT::f32, N->getValueType(0));
434 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
435 return TLI.makeLibCall(DAG, LC, NVT, &Res32, 1, false, SDLoc(N)).first;
438 SDValue DAGTypeLegalizer::SoftenFloatRes_FP_ROUND(SDNode *N) {
439 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
440 SDValue Op = N->getOperand(0);
441 if (N->getValueType(0) == MVT::f16) {
442 // Semi-soften first, to FP_TO_FP16, so that targets which support f16 as a
443 // storage-only type get a chance to select things.
444 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, Op);
447 RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), N->getValueType(0));
448 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
449 return TLI.makeLibCall(DAG, LC, NVT, &Op, 1, false, SDLoc(N)).first;
452 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOW(SDNode *N) {
453 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
454 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
455 GetSoftenedFloat(N->getOperand(1)) };
456 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
462 NVT, Ops, 2, false, SDLoc(N)).first;
465 SDValue DAGTypeLegalizer::SoftenFloatRes_FPOWI(SDNode *N) {
466 assert(N->getOperand(1).getValueType() == MVT::i32 &&
467 "Unsupported power type!");
468 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
469 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)), N->getOperand(1) };
470 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
475 RTLIB::POWI_PPCF128),
476 NVT, Ops, 2, false, SDLoc(N)).first;
479 SDValue DAGTypeLegalizer::SoftenFloatRes_FREM(SDNode *N) {
480 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
481 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
482 GetSoftenedFloat(N->getOperand(1)) };
483 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
489 NVT, Ops, 2, false, SDLoc(N)).first;
492 SDValue DAGTypeLegalizer::SoftenFloatRes_FRINT(SDNode *N) {
493 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
494 SDValue Op = GetSoftenedFloat(N->getOperand(0));
495 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
500 RTLIB::RINT_PPCF128),
501 NVT, &Op, 1, false, SDLoc(N)).first;
504 SDValue DAGTypeLegalizer::SoftenFloatRes_FROUND(SDNode *N) {
505 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
506 SDValue Op = GetSoftenedFloat(N->getOperand(0));
507 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
512 RTLIB::ROUND_PPCF128),
513 NVT, &Op, 1, false, SDLoc(N)).first;
516 SDValue DAGTypeLegalizer::SoftenFloatRes_FSIN(SDNode *N) {
517 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
518 SDValue Op = GetSoftenedFloat(N->getOperand(0));
519 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
525 NVT, &Op, 1, false, SDLoc(N)).first;
528 SDValue DAGTypeLegalizer::SoftenFloatRes_FSQRT(SDNode *N) {
529 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
530 SDValue Op = GetSoftenedFloat(N->getOperand(0));
531 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
536 RTLIB::SQRT_PPCF128),
537 NVT, &Op, 1, false, SDLoc(N)).first;
540 SDValue DAGTypeLegalizer::SoftenFloatRes_FSUB(SDNode *N) {
541 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
542 SDValue Ops[2] = { GetSoftenedFloat(N->getOperand(0)),
543 GetSoftenedFloat(N->getOperand(1)) };
544 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
550 NVT, Ops, 2, false, SDLoc(N)).first;
553 SDValue DAGTypeLegalizer::SoftenFloatRes_FTRUNC(SDNode *N) {
554 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
555 if (N->getValueType(0) == MVT::f16)
556 return DAG.getNode(ISD::FP_TO_FP16, SDLoc(N), NVT, N->getOperand(0));
558 SDValue Op = GetSoftenedFloat(N->getOperand(0));
559 return TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
564 RTLIB::TRUNC_PPCF128),
565 NVT, &Op, 1, false, SDLoc(N)).first;
568 SDValue DAGTypeLegalizer::SoftenFloatRes_LOAD(SDNode *N) {
569 LoadSDNode *L = cast<LoadSDNode>(N);
570 EVT VT = N->getValueType(0);
571 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
575 if (L->getExtensionType() == ISD::NON_EXTLOAD) {
576 NewL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
577 NVT, dl, L->getChain(), L->getBasePtr(), L->getOffset(),
578 L->getPointerInfo(), NVT, L->isVolatile(),
579 L->isNonTemporal(), false, L->getAlignment(),
581 // Legalized the chain result - switch anything that used the old chain to
583 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
587 // Do a non-extending load followed by FP_EXTEND.
588 NewL = DAG.getLoad(L->getAddressingMode(), ISD::NON_EXTLOAD,
589 L->getMemoryVT(), dl, L->getChain(),
590 L->getBasePtr(), L->getOffset(), L->getPointerInfo(),
591 L->getMemoryVT(), L->isVolatile(),
592 L->isNonTemporal(), false, L->getAlignment(),
594 // Legalized the chain result - switch anything that used the old chain to
596 ReplaceValueWith(SDValue(N, 1), NewL.getValue(1));
597 return BitConvertToInteger(DAG.getNode(ISD::FP_EXTEND, dl, VT, NewL));
600 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT(SDNode *N) {
601 SDValue LHS = GetSoftenedFloat(N->getOperand(1));
602 SDValue RHS = GetSoftenedFloat(N->getOperand(2));
603 return DAG.getSelect(SDLoc(N),
604 LHS.getValueType(), N->getOperand(0), LHS, RHS);
607 SDValue DAGTypeLegalizer::SoftenFloatRes_SELECT_CC(SDNode *N) {
608 SDValue LHS = GetSoftenedFloat(N->getOperand(2));
609 SDValue RHS = GetSoftenedFloat(N->getOperand(3));
610 return DAG.getNode(ISD::SELECT_CC, SDLoc(N),
611 LHS.getValueType(), N->getOperand(0),
612 N->getOperand(1), LHS, RHS, N->getOperand(4));
615 SDValue DAGTypeLegalizer::SoftenFloatRes_UNDEF(SDNode *N) {
616 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
617 N->getValueType(0)));
620 SDValue DAGTypeLegalizer::SoftenFloatRes_VAARG(SDNode *N) {
621 SDValue Chain = N->getOperand(0); // Get the chain.
622 SDValue Ptr = N->getOperand(1); // Get the pointer.
623 EVT VT = N->getValueType(0);
624 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
628 NewVAARG = DAG.getVAArg(NVT, dl, Chain, Ptr, N->getOperand(2),
629 N->getConstantOperandVal(3));
631 // Legalized the chain result - switch anything that used the old chain to
633 ReplaceValueWith(SDValue(N, 1), NewVAARG.getValue(1));
637 SDValue DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP(SDNode *N) {
638 bool Signed = N->getOpcode() == ISD::SINT_TO_FP;
639 EVT SVT = N->getOperand(0).getValueType();
640 EVT RVT = N->getValueType(0);
644 // If the input is not legal, eg: i1 -> fp, then it needs to be promoted to
645 // a larger type, eg: i8 -> fp. Even if it is legal, no libcall may exactly
646 // match. Look for an appropriate libcall.
647 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
648 for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
649 t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; ++t) {
650 NVT = (MVT::SimpleValueType)t;
651 // The source needs to big enough to hold the operand.
653 LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT):RTLIB::getUINTTOFP (NVT, RVT);
655 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
657 // Sign/zero extend the argument if the libcall takes a larger type.
658 SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
659 NVT, N->getOperand(0));
660 return TLI.makeLibCall(DAG, LC,
661 TLI.getTypeToTransformTo(*DAG.getContext(), RVT),
662 &Op, 1, Signed, dl).first;
666 //===----------------------------------------------------------------------===//
667 // Operand Float to Integer Conversion..
668 //===----------------------------------------------------------------------===//
670 bool DAGTypeLegalizer::SoftenFloatOperand(SDNode *N, unsigned OpNo) {
671 DEBUG(dbgs() << "Soften float operand " << OpNo << ": "; N->dump(&DAG);
673 SDValue Res = SDValue();
675 switch (N->getOpcode()) {
678 dbgs() << "SoftenFloatOperand Op #" << OpNo << ": ";
679 N->dump(&DAG); dbgs() << "\n";
681 llvm_unreachable("Do not know how to soften this operator's operand!");
683 case ISD::BITCAST: Res = SoftenFloatOp_BITCAST(N); break;
684 case ISD::BR_CC: Res = SoftenFloatOp_BR_CC(N); break;
685 case ISD::FP_EXTEND: Res = SoftenFloatOp_FP_EXTEND(N); break;
686 case ISD::FP_TO_FP16: // Same as FP_ROUND for softening purposes
687 case ISD::FP_ROUND: Res = SoftenFloatOp_FP_ROUND(N); break;
688 case ISD::FP_TO_SINT: Res = SoftenFloatOp_FP_TO_SINT(N); break;
689 case ISD::FP_TO_UINT: Res = SoftenFloatOp_FP_TO_UINT(N); break;
690 case ISD::SELECT_CC: Res = SoftenFloatOp_SELECT_CC(N); break;
691 case ISD::SETCC: Res = SoftenFloatOp_SETCC(N); break;
692 case ISD::STORE: Res = SoftenFloatOp_STORE(N, OpNo); break;
695 // If the result is null, the sub-method took care of registering results etc.
696 if (!Res.getNode()) return false;
698 // If the result is N, the sub-method updated N in place. Tell the legalizer
700 if (Res.getNode() == N)
703 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
704 "Invalid operand expansion");
706 ReplaceValueWith(SDValue(N, 0), Res);
710 SDValue DAGTypeLegalizer::SoftenFloatOp_BITCAST(SDNode *N) {
711 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0),
712 GetSoftenedFloat(N->getOperand(0)));
715 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_EXTEND(SDNode *N) {
716 // If we get here, the result must be legal but the source illegal.
717 EVT SVT = N->getOperand(0).getValueType();
718 EVT RVT = N->getValueType(0);
719 SDValue Op = GetSoftenedFloat(N->getOperand(0));
722 return DAG.getNode(ISD::FP16_TO_FP, SDLoc(N), RVT, Op);
724 RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, RVT);
725 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND libcall");
727 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
731 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_ROUND(SDNode *N) {
732 // We actually deal with the partially-softened FP_TO_FP16 node too, which
733 // returns an i16 so doesn't meet the constraints necessary for FP_ROUND.
734 assert(N->getOpcode() == ISD::FP_ROUND || N->getOpcode() == ISD::FP_TO_FP16);
736 EVT SVT = N->getOperand(0).getValueType();
737 EVT RVT = N->getValueType(0);
738 EVT FloatRVT = N->getOpcode() == ISD::FP_TO_FP16 ? MVT::f16 : RVT;
740 RTLIB::Libcall LC = RTLIB::getFPROUND(SVT, FloatRVT);
741 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND libcall");
743 SDValue Op = GetSoftenedFloat(N->getOperand(0));
744 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
747 SDValue DAGTypeLegalizer::SoftenFloatOp_BR_CC(SDNode *N) {
748 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
749 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
751 EVT VT = NewLHS.getValueType();
752 NewLHS = GetSoftenedFloat(NewLHS);
753 NewRHS = GetSoftenedFloat(NewRHS);
754 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
756 // If softenSetCCOperands returned a scalar, we need to compare the result
757 // against zero to select between true and false values.
758 if (!NewRHS.getNode()) {
759 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
763 // Update N to have the operands specified.
764 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
765 DAG.getCondCode(CCCode), NewLHS, NewRHS,
770 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_SINT(SDNode *N) {
771 EVT RVT = N->getValueType(0);
772 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
773 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
774 SDValue Op = GetSoftenedFloat(N->getOperand(0));
775 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
778 SDValue DAGTypeLegalizer::SoftenFloatOp_FP_TO_UINT(SDNode *N) {
779 EVT RVT = N->getValueType(0);
780 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
781 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
782 SDValue Op = GetSoftenedFloat(N->getOperand(0));
783 return TLI.makeLibCall(DAG, LC, RVT, &Op, 1, false, SDLoc(N)).first;
786 SDValue DAGTypeLegalizer::SoftenFloatOp_SELECT_CC(SDNode *N) {
787 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
788 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
790 EVT VT = NewLHS.getValueType();
791 NewLHS = GetSoftenedFloat(NewLHS);
792 NewRHS = GetSoftenedFloat(NewRHS);
793 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
795 // If softenSetCCOperands returned a scalar, we need to compare the result
796 // against zero to select between true and false values.
797 if (!NewRHS.getNode()) {
798 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
802 // Update N to have the operands specified.
803 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
804 N->getOperand(2), N->getOperand(3),
805 DAG.getCondCode(CCCode)),
809 SDValue DAGTypeLegalizer::SoftenFloatOp_SETCC(SDNode *N) {
810 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
811 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
813 EVT VT = NewLHS.getValueType();
814 NewLHS = GetSoftenedFloat(NewLHS);
815 NewRHS = GetSoftenedFloat(NewRHS);
816 TLI.softenSetCCOperands(DAG, VT, NewLHS, NewRHS, CCCode, SDLoc(N));
818 // If softenSetCCOperands returned a scalar, use it.
819 if (!NewRHS.getNode()) {
820 assert(NewLHS.getValueType() == N->getValueType(0) &&
821 "Unexpected setcc expansion!");
825 // Otherwise, update N to have the operands specified.
826 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
827 DAG.getCondCode(CCCode)),
831 SDValue DAGTypeLegalizer::SoftenFloatOp_STORE(SDNode *N, unsigned OpNo) {
832 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
833 assert(OpNo == 1 && "Can only soften the stored value!");
834 StoreSDNode *ST = cast<StoreSDNode>(N);
835 SDValue Val = ST->getValue();
838 if (ST->isTruncatingStore())
839 // Do an FP_ROUND followed by a non-truncating store.
840 Val = BitConvertToInteger(DAG.getNode(ISD::FP_ROUND, dl, ST->getMemoryVT(),
841 Val, DAG.getIntPtrConstant(0, dl)));
843 Val = GetSoftenedFloat(Val);
845 return DAG.getStore(ST->getChain(), dl, Val, ST->getBasePtr(),
846 ST->getMemOperand());
850 //===----------------------------------------------------------------------===//
851 // Float Result Expansion
852 //===----------------------------------------------------------------------===//
854 /// ExpandFloatResult - This method is called when the specified result of the
855 /// specified node is found to need expansion. At this point, the node may also
856 /// have invalid operands or may have other results that need promotion, we just
857 /// know that (at least) one result needs expansion.
858 void DAGTypeLegalizer::ExpandFloatResult(SDNode *N, unsigned ResNo) {
859 DEBUG(dbgs() << "Expand float result: "; N->dump(&DAG); dbgs() << "\n");
863 // See if the target wants to custom expand this node.
864 if (CustomLowerNode(N, N->getValueType(ResNo), true))
867 switch (N->getOpcode()) {
870 dbgs() << "ExpandFloatResult #" << ResNo << ": ";
871 N->dump(&DAG); dbgs() << "\n";
873 llvm_unreachable("Do not know how to expand the result of this operator!");
875 case ISD::UNDEF: SplitRes_UNDEF(N, Lo, Hi); break;
876 case ISD::SELECT: SplitRes_SELECT(N, Lo, Hi); break;
877 case ISD::SELECT_CC: SplitRes_SELECT_CC(N, Lo, Hi); break;
879 case ISD::MERGE_VALUES: ExpandRes_MERGE_VALUES(N, ResNo, Lo, Hi); break;
880 case ISD::BITCAST: ExpandRes_BITCAST(N, Lo, Hi); break;
881 case ISD::BUILD_PAIR: ExpandRes_BUILD_PAIR(N, Lo, Hi); break;
882 case ISD::EXTRACT_ELEMENT: ExpandRes_EXTRACT_ELEMENT(N, Lo, Hi); break;
883 case ISD::EXTRACT_VECTOR_ELT: ExpandRes_EXTRACT_VECTOR_ELT(N, Lo, Hi); break;
884 case ISD::VAARG: ExpandRes_VAARG(N, Lo, Hi); break;
886 case ISD::ConstantFP: ExpandFloatRes_ConstantFP(N, Lo, Hi); break;
887 case ISD::FABS: ExpandFloatRes_FABS(N, Lo, Hi); break;
888 case ISD::FMINNUM: ExpandFloatRes_FMINNUM(N, Lo, Hi); break;
889 case ISD::FMAXNUM: ExpandFloatRes_FMAXNUM(N, Lo, Hi); break;
890 case ISD::FADD: ExpandFloatRes_FADD(N, Lo, Hi); break;
891 case ISD::FCEIL: ExpandFloatRes_FCEIL(N, Lo, Hi); break;
892 case ISD::FCOPYSIGN: ExpandFloatRes_FCOPYSIGN(N, Lo, Hi); break;
893 case ISD::FCOS: ExpandFloatRes_FCOS(N, Lo, Hi); break;
894 case ISD::FDIV: ExpandFloatRes_FDIV(N, Lo, Hi); break;
895 case ISD::FEXP: ExpandFloatRes_FEXP(N, Lo, Hi); break;
896 case ISD::FEXP2: ExpandFloatRes_FEXP2(N, Lo, Hi); break;
897 case ISD::FFLOOR: ExpandFloatRes_FFLOOR(N, Lo, Hi); break;
898 case ISD::FLOG: ExpandFloatRes_FLOG(N, Lo, Hi); break;
899 case ISD::FLOG2: ExpandFloatRes_FLOG2(N, Lo, Hi); break;
900 case ISD::FLOG10: ExpandFloatRes_FLOG10(N, Lo, Hi); break;
901 case ISD::FMA: ExpandFloatRes_FMA(N, Lo, Hi); break;
902 case ISD::FMUL: ExpandFloatRes_FMUL(N, Lo, Hi); break;
903 case ISD::FNEARBYINT: ExpandFloatRes_FNEARBYINT(N, Lo, Hi); break;
904 case ISD::FNEG: ExpandFloatRes_FNEG(N, Lo, Hi); break;
905 case ISD::FP_EXTEND: ExpandFloatRes_FP_EXTEND(N, Lo, Hi); break;
906 case ISD::FPOW: ExpandFloatRes_FPOW(N, Lo, Hi); break;
907 case ISD::FPOWI: ExpandFloatRes_FPOWI(N, Lo, Hi); break;
908 case ISD::FRINT: ExpandFloatRes_FRINT(N, Lo, Hi); break;
909 case ISD::FROUND: ExpandFloatRes_FROUND(N, Lo, Hi); break;
910 case ISD::FSIN: ExpandFloatRes_FSIN(N, Lo, Hi); break;
911 case ISD::FSQRT: ExpandFloatRes_FSQRT(N, Lo, Hi); break;
912 case ISD::FSUB: ExpandFloatRes_FSUB(N, Lo, Hi); break;
913 case ISD::FTRUNC: ExpandFloatRes_FTRUNC(N, Lo, Hi); break;
914 case ISD::LOAD: ExpandFloatRes_LOAD(N, Lo, Hi); break;
915 case ISD::SINT_TO_FP:
916 case ISD::UINT_TO_FP: ExpandFloatRes_XINT_TO_FP(N, Lo, Hi); break;
917 case ISD::FREM: ExpandFloatRes_FREM(N, Lo, Hi); break;
920 // If Lo/Hi is null, the sub-method took care of registering results etc.
922 SetExpandedFloat(SDValue(N, ResNo), Lo, Hi);
925 void DAGTypeLegalizer::ExpandFloatRes_ConstantFP(SDNode *N, SDValue &Lo,
927 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
928 assert(NVT.getSizeInBits() == integerPartWidth &&
929 "Do not know how to expand this float constant!");
930 APInt C = cast<ConstantFPSDNode>(N)->getValueAPF().bitcastToAPInt();
932 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
933 APInt(integerPartWidth, C.getRawData()[1])),
935 Hi = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
936 APInt(integerPartWidth, C.getRawData()[0])),
940 void DAGTypeLegalizer::ExpandFloatRes_FABS(SDNode *N, SDValue &Lo,
942 assert(N->getValueType(0) == MVT::ppcf128 &&
943 "Logic only correct for ppcf128!");
946 GetExpandedFloat(N->getOperand(0), Lo, Tmp);
947 Hi = DAG.getNode(ISD::FABS, dl, Tmp.getValueType(), Tmp);
948 // Lo = Hi==fabs(Hi) ? Lo : -Lo;
949 Lo = DAG.getSelectCC(dl, Tmp, Hi, Lo,
950 DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo),
954 void DAGTypeLegalizer::ExpandFloatRes_FMINNUM(SDNode *N, SDValue &Lo,
956 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
957 RTLIB::FMIN_F32, RTLIB::FMIN_F64,
958 RTLIB::FMIN_F80, RTLIB::FMIN_F128,
959 RTLIB::FMIN_PPCF128),
961 GetPairElements(Call, Lo, Hi);
964 void DAGTypeLegalizer::ExpandFloatRes_FMAXNUM(SDNode *N, SDValue &Lo,
966 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
967 RTLIB::FMAX_F32, RTLIB::FMAX_F64,
968 RTLIB::FMAX_F80, RTLIB::FMAX_F128,
969 RTLIB::FMAX_PPCF128),
971 GetPairElements(Call, Lo, Hi);
974 void DAGTypeLegalizer::ExpandFloatRes_FADD(SDNode *N, SDValue &Lo,
976 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
977 RTLIB::ADD_F32, RTLIB::ADD_F64,
978 RTLIB::ADD_F80, RTLIB::ADD_F128,
981 GetPairElements(Call, Lo, Hi);
984 void DAGTypeLegalizer::ExpandFloatRes_FCEIL(SDNode *N,
985 SDValue &Lo, SDValue &Hi) {
986 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
987 RTLIB::CEIL_F32, RTLIB::CEIL_F64,
988 RTLIB::CEIL_F80, RTLIB::CEIL_F128,
989 RTLIB::CEIL_PPCF128),
991 GetPairElements(Call, Lo, Hi);
994 void DAGTypeLegalizer::ExpandFloatRes_FCOPYSIGN(SDNode *N,
995 SDValue &Lo, SDValue &Hi) {
996 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1000 RTLIB::COPYSIGN_F128,
1001 RTLIB::COPYSIGN_PPCF128),
1003 GetPairElements(Call, Lo, Hi);
1006 void DAGTypeLegalizer::ExpandFloatRes_FCOS(SDNode *N,
1007 SDValue &Lo, SDValue &Hi) {
1008 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1009 RTLIB::COS_F32, RTLIB::COS_F64,
1010 RTLIB::COS_F80, RTLIB::COS_F128,
1011 RTLIB::COS_PPCF128),
1013 GetPairElements(Call, Lo, Hi);
1016 void DAGTypeLegalizer::ExpandFloatRes_FDIV(SDNode *N, SDValue &Lo,
1018 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1019 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1024 RTLIB::DIV_PPCF128),
1025 N->getValueType(0), Ops, 2, false,
1027 GetPairElements(Call, Lo, Hi);
1030 void DAGTypeLegalizer::ExpandFloatRes_FEXP(SDNode *N,
1031 SDValue &Lo, SDValue &Hi) {
1032 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1033 RTLIB::EXP_F32, RTLIB::EXP_F64,
1034 RTLIB::EXP_F80, RTLIB::EXP_F128,
1035 RTLIB::EXP_PPCF128),
1037 GetPairElements(Call, Lo, Hi);
1040 void DAGTypeLegalizer::ExpandFloatRes_FEXP2(SDNode *N,
1041 SDValue &Lo, SDValue &Hi) {
1042 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1043 RTLIB::EXP2_F32, RTLIB::EXP2_F64,
1044 RTLIB::EXP2_F80, RTLIB::EXP2_F128,
1045 RTLIB::EXP2_PPCF128),
1047 GetPairElements(Call, Lo, Hi);
1050 void DAGTypeLegalizer::ExpandFloatRes_FFLOOR(SDNode *N,
1051 SDValue &Lo, SDValue &Hi) {
1052 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1053 RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
1054 RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
1055 RTLIB::FLOOR_PPCF128),
1057 GetPairElements(Call, Lo, Hi);
1060 void DAGTypeLegalizer::ExpandFloatRes_FLOG(SDNode *N,
1061 SDValue &Lo, SDValue &Hi) {
1062 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1063 RTLIB::LOG_F32, RTLIB::LOG_F64,
1064 RTLIB::LOG_F80, RTLIB::LOG_F128,
1065 RTLIB::LOG_PPCF128),
1067 GetPairElements(Call, Lo, Hi);
1070 void DAGTypeLegalizer::ExpandFloatRes_FLOG2(SDNode *N,
1071 SDValue &Lo, SDValue &Hi) {
1072 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1073 RTLIB::LOG2_F32, RTLIB::LOG2_F64,
1074 RTLIB::LOG2_F80, RTLIB::LOG2_F128,
1075 RTLIB::LOG2_PPCF128),
1077 GetPairElements(Call, Lo, Hi);
1080 void DAGTypeLegalizer::ExpandFloatRes_FLOG10(SDNode *N,
1081 SDValue &Lo, SDValue &Hi) {
1082 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1083 RTLIB::LOG10_F32, RTLIB::LOG10_F64,
1084 RTLIB::LOG10_F80, RTLIB::LOG10_F128,
1085 RTLIB::LOG10_PPCF128),
1087 GetPairElements(Call, Lo, Hi);
1090 void DAGTypeLegalizer::ExpandFloatRes_FMA(SDNode *N, SDValue &Lo,
1092 SDValue Ops[3] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) };
1093 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1098 RTLIB::FMA_PPCF128),
1099 N->getValueType(0), Ops, 3, false,
1101 GetPairElements(Call, Lo, Hi);
1104 void DAGTypeLegalizer::ExpandFloatRes_FMUL(SDNode *N, SDValue &Lo,
1106 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1107 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1112 RTLIB::MUL_PPCF128),
1113 N->getValueType(0), Ops, 2, false,
1115 GetPairElements(Call, Lo, Hi);
1118 void DAGTypeLegalizer::ExpandFloatRes_FNEARBYINT(SDNode *N,
1119 SDValue &Lo, SDValue &Hi) {
1120 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1121 RTLIB::NEARBYINT_F32,
1122 RTLIB::NEARBYINT_F64,
1123 RTLIB::NEARBYINT_F80,
1124 RTLIB::NEARBYINT_F128,
1125 RTLIB::NEARBYINT_PPCF128),
1127 GetPairElements(Call, Lo, Hi);
1130 void DAGTypeLegalizer::ExpandFloatRes_FNEG(SDNode *N, SDValue &Lo,
1133 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1134 Lo = DAG.getNode(ISD::FNEG, dl, Lo.getValueType(), Lo);
1135 Hi = DAG.getNode(ISD::FNEG, dl, Hi.getValueType(), Hi);
1138 void DAGTypeLegalizer::ExpandFloatRes_FP_EXTEND(SDNode *N, SDValue &Lo,
1140 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1142 Hi = DAG.getNode(ISD::FP_EXTEND, dl, NVT, N->getOperand(0));
1143 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1144 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1147 void DAGTypeLegalizer::ExpandFloatRes_FPOW(SDNode *N,
1148 SDValue &Lo, SDValue &Hi) {
1149 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1150 RTLIB::POW_F32, RTLIB::POW_F64,
1151 RTLIB::POW_F80, RTLIB::POW_F128,
1152 RTLIB::POW_PPCF128),
1154 GetPairElements(Call, Lo, Hi);
1157 void DAGTypeLegalizer::ExpandFloatRes_FPOWI(SDNode *N,
1158 SDValue &Lo, SDValue &Hi) {
1159 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1160 RTLIB::POWI_F32, RTLIB::POWI_F64,
1161 RTLIB::POWI_F80, RTLIB::POWI_F128,
1162 RTLIB::POWI_PPCF128),
1164 GetPairElements(Call, Lo, Hi);
1167 void DAGTypeLegalizer::ExpandFloatRes_FREM(SDNode *N,
1168 SDValue &Lo, SDValue &Hi) {
1169 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1170 RTLIB::REM_F32, RTLIB::REM_F64,
1171 RTLIB::REM_F80, RTLIB::REM_F128,
1172 RTLIB::REM_PPCF128),
1174 GetPairElements(Call, Lo, Hi);
1177 void DAGTypeLegalizer::ExpandFloatRes_FRINT(SDNode *N,
1178 SDValue &Lo, SDValue &Hi) {
1179 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1180 RTLIB::RINT_F32, RTLIB::RINT_F64,
1181 RTLIB::RINT_F80, RTLIB::RINT_F128,
1182 RTLIB::RINT_PPCF128),
1184 GetPairElements(Call, Lo, Hi);
1187 void DAGTypeLegalizer::ExpandFloatRes_FROUND(SDNode *N,
1188 SDValue &Lo, SDValue &Hi) {
1189 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1194 RTLIB::ROUND_PPCF128),
1196 GetPairElements(Call, Lo, Hi);
1199 void DAGTypeLegalizer::ExpandFloatRes_FSIN(SDNode *N,
1200 SDValue &Lo, SDValue &Hi) {
1201 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1202 RTLIB::SIN_F32, RTLIB::SIN_F64,
1203 RTLIB::SIN_F80, RTLIB::SIN_F128,
1204 RTLIB::SIN_PPCF128),
1206 GetPairElements(Call, Lo, Hi);
1209 void DAGTypeLegalizer::ExpandFloatRes_FSQRT(SDNode *N,
1210 SDValue &Lo, SDValue &Hi) {
1211 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1212 RTLIB::SQRT_F32, RTLIB::SQRT_F64,
1213 RTLIB::SQRT_F80, RTLIB::SQRT_F128,
1214 RTLIB::SQRT_PPCF128),
1216 GetPairElements(Call, Lo, Hi);
1219 void DAGTypeLegalizer::ExpandFloatRes_FSUB(SDNode *N, SDValue &Lo,
1221 SDValue Ops[2] = { N->getOperand(0), N->getOperand(1) };
1222 SDValue Call = TLI.makeLibCall(DAG, GetFPLibCall(N->getValueType(0),
1227 RTLIB::SUB_PPCF128),
1228 N->getValueType(0), Ops, 2, false,
1230 GetPairElements(Call, Lo, Hi);
1233 void DAGTypeLegalizer::ExpandFloatRes_FTRUNC(SDNode *N,
1234 SDValue &Lo, SDValue &Hi) {
1235 SDValue Call = LibCallify(GetFPLibCall(N->getValueType(0),
1236 RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
1237 RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
1238 RTLIB::TRUNC_PPCF128),
1240 GetPairElements(Call, Lo, Hi);
1243 void DAGTypeLegalizer::ExpandFloatRes_LOAD(SDNode *N, SDValue &Lo,
1245 if (ISD::isNormalLoad(N)) {
1246 ExpandRes_NormalLoad(N, Lo, Hi);
1250 assert(ISD::isUNINDEXEDLoad(N) && "Indexed load during type legalization!");
1251 LoadSDNode *LD = cast<LoadSDNode>(N);
1252 SDValue Chain = LD->getChain();
1253 SDValue Ptr = LD->getBasePtr();
1256 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), LD->getValueType(0));
1257 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1258 assert(LD->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1260 Hi = DAG.getExtLoad(LD->getExtensionType(), dl, NVT, Chain, Ptr,
1261 LD->getMemoryVT(), LD->getMemOperand());
1263 // Remember the chain.
1264 Chain = Hi.getValue(1);
1266 // The low part is zero.
1267 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1268 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1270 // Modified the chain - switch anything that used the old chain to use the
1272 ReplaceValueWith(SDValue(LD, 1), Chain);
1275 void DAGTypeLegalizer::ExpandFloatRes_XINT_TO_FP(SDNode *N, SDValue &Lo,
1277 assert(N->getValueType(0) == MVT::ppcf128 && "Unsupported XINT_TO_FP!");
1278 EVT VT = N->getValueType(0);
1279 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1280 SDValue Src = N->getOperand(0);
1281 EVT SrcVT = Src.getValueType();
1282 bool isSigned = N->getOpcode() == ISD::SINT_TO_FP;
1285 // First do an SINT_TO_FP, whether the original was signed or unsigned.
1286 // When promoting partial word types to i32 we must honor the signedness,
1288 if (SrcVT.bitsLE(MVT::i32)) {
1289 // The integer can be represented exactly in an f64.
1290 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1292 Lo = DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(NVT),
1293 APInt(NVT.getSizeInBits(), 0)), dl, NVT);
1294 Hi = DAG.getNode(ISD::SINT_TO_FP, dl, NVT, Src);
1296 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
1297 if (SrcVT.bitsLE(MVT::i64)) {
1298 Src = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
1300 LC = RTLIB::SINTTOFP_I64_PPCF128;
1301 } else if (SrcVT.bitsLE(MVT::i128)) {
1302 Src = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i128, Src);
1303 LC = RTLIB::SINTTOFP_I128_PPCF128;
1305 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported XINT_TO_FP!");
1307 Hi = TLI.makeLibCall(DAG, LC, VT, &Src, 1, true, dl).first;
1308 GetPairElements(Hi, Lo, Hi);
1314 // Unsigned - fix up the SINT_TO_FP value just calculated.
1315 Hi = DAG.getNode(ISD::BUILD_PAIR, dl, VT, Lo, Hi);
1316 SrcVT = Src.getValueType();
1318 // x>=0 ? (ppcf128)(iN)x : (ppcf128)(iN)x + 2^N; N=32,64,128.
1319 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
1320 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
1321 static const uint64_t TwoE128[] = { 0x47f0000000000000LL, 0 };
1322 ArrayRef<uint64_t> Parts;
1324 switch (SrcVT.getSimpleVT().SimpleTy) {
1326 llvm_unreachable("Unsupported UINT_TO_FP!");
1338 Lo = DAG.getNode(ISD::FADD, dl, VT, Hi,
1339 DAG.getConstantFP(APFloat(APFloat::PPCDoubleDouble,
1342 Lo = DAG.getSelectCC(dl, Src, DAG.getConstant(0, dl, SrcVT),
1343 Lo, Hi, ISD::SETLT);
1344 GetPairElements(Lo, Lo, Hi);
1348 //===----------------------------------------------------------------------===//
1349 // Float Operand Expansion
1350 //===----------------------------------------------------------------------===//
1352 /// ExpandFloatOperand - This method is called when the specified operand of the
1353 /// specified node is found to need expansion. At this point, all of the result
1354 /// types of the node are known to be legal, but other operands of the node may
1355 /// need promotion or expansion as well as the specified one.
1356 bool DAGTypeLegalizer::ExpandFloatOperand(SDNode *N, unsigned OpNo) {
1357 DEBUG(dbgs() << "Expand float operand: "; N->dump(&DAG); dbgs() << "\n");
1358 SDValue Res = SDValue();
1360 // See if the target wants to custom expand this node.
1361 if (CustomLowerNode(N, N->getOperand(OpNo).getValueType(), false))
1364 switch (N->getOpcode()) {
1367 dbgs() << "ExpandFloatOperand Op #" << OpNo << ": ";
1368 N->dump(&DAG); dbgs() << "\n";
1370 llvm_unreachable("Do not know how to expand this operator's operand!");
1372 case ISD::BITCAST: Res = ExpandOp_BITCAST(N); break;
1373 case ISD::BUILD_VECTOR: Res = ExpandOp_BUILD_VECTOR(N); break;
1374 case ISD::EXTRACT_ELEMENT: Res = ExpandOp_EXTRACT_ELEMENT(N); break;
1376 case ISD::BR_CC: Res = ExpandFloatOp_BR_CC(N); break;
1377 case ISD::FCOPYSIGN: Res = ExpandFloatOp_FCOPYSIGN(N); break;
1378 case ISD::FP_ROUND: Res = ExpandFloatOp_FP_ROUND(N); break;
1379 case ISD::FP_TO_SINT: Res = ExpandFloatOp_FP_TO_SINT(N); break;
1380 case ISD::FP_TO_UINT: Res = ExpandFloatOp_FP_TO_UINT(N); break;
1381 case ISD::SELECT_CC: Res = ExpandFloatOp_SELECT_CC(N); break;
1382 case ISD::SETCC: Res = ExpandFloatOp_SETCC(N); break;
1383 case ISD::STORE: Res = ExpandFloatOp_STORE(cast<StoreSDNode>(N),
1387 // If the result is null, the sub-method took care of registering results etc.
1388 if (!Res.getNode()) return false;
1390 // If the result is N, the sub-method updated N in place. Tell the legalizer
1392 if (Res.getNode() == N)
1395 assert(Res.getValueType() == N->getValueType(0) && N->getNumValues() == 1 &&
1396 "Invalid operand expansion");
1398 ReplaceValueWith(SDValue(N, 0), Res);
1402 /// FloatExpandSetCCOperands - Expand the operands of a comparison. This code
1403 /// is shared among BR_CC, SELECT_CC, and SETCC handlers.
1404 void DAGTypeLegalizer::FloatExpandSetCCOperands(SDValue &NewLHS,
1406 ISD::CondCode &CCCode,
1408 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
1409 GetExpandedFloat(NewLHS, LHSLo, LHSHi);
1410 GetExpandedFloat(NewRHS, RHSLo, RHSHi);
1412 assert(NewLHS.getValueType() == MVT::ppcf128 && "Unsupported setcc type!");
1414 // FIXME: This generated code sucks. We want to generate
1415 // FCMPU crN, hi1, hi2
1417 // FCMPU crN, lo1, lo2
1418 // The following can be improved, but not that much.
1419 SDValue Tmp1, Tmp2, Tmp3;
1420 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1421 LHSHi, RHSHi, ISD::SETOEQ);
1422 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSLo.getValueType()),
1423 LHSLo, RHSLo, CCCode);
1424 Tmp3 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1425 Tmp1 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1426 LHSHi, RHSHi, ISD::SETUNE);
1427 Tmp2 = DAG.getSetCC(dl, getSetCCResultType(LHSHi.getValueType()),
1428 LHSHi, RHSHi, CCCode);
1429 Tmp1 = DAG.getNode(ISD::AND, dl, Tmp1.getValueType(), Tmp1, Tmp2);
1430 NewLHS = DAG.getNode(ISD::OR, dl, Tmp1.getValueType(), Tmp1, Tmp3);
1431 NewRHS = SDValue(); // LHS is the result, not a compare.
1434 SDValue DAGTypeLegalizer::ExpandFloatOp_BR_CC(SDNode *N) {
1435 SDValue NewLHS = N->getOperand(2), NewRHS = N->getOperand(3);
1436 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(1))->get();
1437 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1439 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1440 // against zero to select between true and false values.
1441 if (!NewRHS.getNode()) {
1442 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1443 CCCode = ISD::SETNE;
1446 // Update N to have the operands specified.
1447 return SDValue(DAG.UpdateNodeOperands(N, N->getOperand(0),
1448 DAG.getCondCode(CCCode), NewLHS, NewRHS,
1449 N->getOperand(4)), 0);
1452 SDValue DAGTypeLegalizer::ExpandFloatOp_FCOPYSIGN(SDNode *N) {
1453 assert(N->getOperand(1).getValueType() == MVT::ppcf128 &&
1454 "Logic only correct for ppcf128!");
1456 GetExpandedFloat(N->getOperand(1), Lo, Hi);
1457 // The ppcf128 value is providing only the sign; take it from the
1458 // higher-order double (which must have the larger magnitude).
1459 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N),
1460 N->getValueType(0), N->getOperand(0), Hi);
1463 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_ROUND(SDNode *N) {
1464 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1465 "Logic only correct for ppcf128!");
1467 GetExpandedFloat(N->getOperand(0), Lo, Hi);
1468 // Round it the rest of the way (e.g. to f32) if needed.
1469 return DAG.getNode(ISD::FP_ROUND, SDLoc(N),
1470 N->getValueType(0), Hi, N->getOperand(1));
1473 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_SINT(SDNode *N) {
1474 EVT RVT = N->getValueType(0);
1477 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1478 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1479 if (RVT == MVT::i32) {
1480 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1481 "Logic only correct for ppcf128!");
1482 SDValue Res = DAG.getNode(ISD::FP_ROUND_INREG, dl, MVT::ppcf128,
1483 N->getOperand(0), DAG.getValueType(MVT::f64));
1484 Res = DAG.getNode(ISD::FP_ROUND, dl, MVT::f64, Res,
1485 DAG.getIntPtrConstant(1, dl));
1486 return DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Res);
1489 RTLIB::Libcall LC = RTLIB::getFPTOSINT(N->getOperand(0).getValueType(), RVT);
1490 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_SINT!");
1491 return TLI.makeLibCall(DAG, LC, RVT, &N->getOperand(0), 1, false, dl).first;
1494 SDValue DAGTypeLegalizer::ExpandFloatOp_FP_TO_UINT(SDNode *N) {
1495 EVT RVT = N->getValueType(0);
1498 // Expand ppcf128 to i32 by hand for the benefit of llvm-gcc bootstrap on
1499 // PPC (the libcall is not available). FIXME: Do this in a less hacky way.
1500 if (RVT == MVT::i32) {
1501 assert(N->getOperand(0).getValueType() == MVT::ppcf128 &&
1502 "Logic only correct for ppcf128!");
1503 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
1504 APFloat APF = APFloat(APFloat::PPCDoubleDouble, APInt(128, TwoE31));
1505 SDValue Tmp = DAG.getConstantFP(APF, dl, MVT::ppcf128);
1506 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
1507 // FIXME: generated code sucks.
1508 return DAG.getSelectCC(dl, N->getOperand(0), Tmp,
1509 DAG.getNode(ISD::ADD, dl, MVT::i32,
1510 DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32,
1511 DAG.getNode(ISD::FSUB, dl,
1515 DAG.getConstant(0x80000000, dl,
1517 DAG.getNode(ISD::FP_TO_SINT, dl,
1518 MVT::i32, N->getOperand(0)),
1522 RTLIB::Libcall LC = RTLIB::getFPTOUINT(N->getOperand(0).getValueType(), RVT);
1523 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_TO_UINT!");
1524 return TLI.makeLibCall(DAG, LC, N->getValueType(0), &N->getOperand(0), 1,
1528 SDValue DAGTypeLegalizer::ExpandFloatOp_SELECT_CC(SDNode *N) {
1529 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1530 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(4))->get();
1531 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1533 // If ExpandSetCCOperands returned a scalar, we need to compare the result
1534 // against zero to select between true and false values.
1535 if (!NewRHS.getNode()) {
1536 NewRHS = DAG.getConstant(0, SDLoc(N), NewLHS.getValueType());
1537 CCCode = ISD::SETNE;
1540 // Update N to have the operands specified.
1541 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1542 N->getOperand(2), N->getOperand(3),
1543 DAG.getCondCode(CCCode)), 0);
1546 SDValue DAGTypeLegalizer::ExpandFloatOp_SETCC(SDNode *N) {
1547 SDValue NewLHS = N->getOperand(0), NewRHS = N->getOperand(1);
1548 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1549 FloatExpandSetCCOperands(NewLHS, NewRHS, CCCode, SDLoc(N));
1551 // If ExpandSetCCOperands returned a scalar, use it.
1552 if (!NewRHS.getNode()) {
1553 assert(NewLHS.getValueType() == N->getValueType(0) &&
1554 "Unexpected setcc expansion!");
1558 // Otherwise, update N to have the operands specified.
1559 return SDValue(DAG.UpdateNodeOperands(N, NewLHS, NewRHS,
1560 DAG.getCondCode(CCCode)), 0);
1563 SDValue DAGTypeLegalizer::ExpandFloatOp_STORE(SDNode *N, unsigned OpNo) {
1564 if (ISD::isNormalStore(N))
1565 return ExpandOp_NormalStore(N, OpNo);
1567 assert(ISD::isUNINDEXEDStore(N) && "Indexed store during type legalization!");
1568 assert(OpNo == 1 && "Can only expand the stored value so far");
1569 StoreSDNode *ST = cast<StoreSDNode>(N);
1571 SDValue Chain = ST->getChain();
1572 SDValue Ptr = ST->getBasePtr();
1574 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(),
1575 ST->getValue().getValueType());
1576 assert(NVT.isByteSized() && "Expanded type not byte sized!");
1577 assert(ST->getMemoryVT().bitsLE(NVT) && "Float type not round?");
1581 GetExpandedOp(ST->getValue(), Lo, Hi);
1583 return DAG.getTruncStore(Chain, SDLoc(N), Hi, Ptr,
1584 ST->getMemoryVT(), ST->getMemOperand());
1587 //===----------------------------------------------------------------------===//
1588 // Float Operand Promotion
1589 //===----------------------------------------------------------------------===//
1592 static ISD::NodeType GetPromotionOpcode(EVT OpVT, EVT RetVT) {
1593 if (OpVT == MVT::f16) {
1594 return ISD::FP16_TO_FP;
1595 } else if (RetVT == MVT::f16) {
1596 return ISD::FP_TO_FP16;
1599 report_fatal_error("Attempt at an invalid promotion-related conversion");
1602 bool DAGTypeLegalizer::PromoteFloatOperand(SDNode *N, unsigned OpNo) {
1603 SDValue R = SDValue();
1605 // Nodes that use a promotion-requiring floating point operand, but doesn't
1606 // produce a promotion-requiring floating point result, need to be legalized
1607 // to use the promoted float operand. Nodes that produce at least one
1608 // promotion-requiring floating point result have their operands legalized as
1609 // a part of PromoteFloatResult.
1610 switch (N->getOpcode()) {
1612 llvm_unreachable("Do not know how to promote this operator's operand!");
1614 case ISD::BITCAST: R = PromoteFloatOp_BITCAST(N, OpNo); break;
1615 case ISD::FCOPYSIGN: R = PromoteFloatOp_FCOPYSIGN(N, OpNo); break;
1616 case ISD::FP_TO_SINT:
1617 case ISD::FP_TO_UINT: R = PromoteFloatOp_FP_TO_XINT(N, OpNo); break;
1618 case ISD::FP_EXTEND: R = PromoteFloatOp_FP_EXTEND(N, OpNo); break;
1619 case ISD::SELECT_CC: R = PromoteFloatOp_SELECT_CC(N, OpNo); break;
1620 case ISD::SETCC: R = PromoteFloatOp_SETCC(N, OpNo); break;
1621 case ISD::STORE: R = PromoteFloatOp_STORE(N, OpNo); break;
1625 ReplaceValueWith(SDValue(N, 0), R);
1629 SDValue DAGTypeLegalizer::PromoteFloatOp_BITCAST(SDNode *N, unsigned OpNo) {
1630 SDValue Op = N->getOperand(0);
1631 EVT OpVT = Op->getValueType(0);
1633 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), OpVT.getSizeInBits());
1634 assert (IVT == N->getValueType(0) && "Bitcast to type of different size");
1636 SDValue Promoted = GetPromotedFloat(N->getOperand(0));
1637 EVT PromotedVT = Promoted->getValueType(0);
1639 // Convert the promoted float value to the desired IVT.
1640 return DAG.getNode(GetPromotionOpcode(PromotedVT, OpVT), SDLoc(N), IVT,
1644 // Promote Operand 1 of FCOPYSIGN. Operand 0 ought to be handled by
1645 // PromoteFloatRes_FCOPYSIGN.
1646 SDValue DAGTypeLegalizer::PromoteFloatOp_FCOPYSIGN(SDNode *N, unsigned OpNo) {
1647 assert (OpNo == 1 && "Only Operand 1 must need promotion here");
1648 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1650 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
1651 N->getOperand(0), Op1);
1654 // Convert the promoted float value to the desired integer type
1655 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_TO_XINT(SDNode *N, unsigned OpNo) {
1656 SDValue Op = GetPromotedFloat(N->getOperand(0));
1657 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), Op);
1660 SDValue DAGTypeLegalizer::PromoteFloatOp_FP_EXTEND(SDNode *N, unsigned OpNo) {
1661 SDValue Op = GetPromotedFloat(N->getOperand(0));
1662 EVT VT = N->getValueType(0);
1664 // Desired VT is same as promoted type. Use promoted float directly.
1665 if (VT == Op->getValueType(0))
1668 // Else, extend the promoted float value to the desired VT.
1669 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Op);
1672 // Promote the float operands used for comparison. The true- and false-
1673 // operands have the same type as the result and are promoted, if needed, by
1674 // PromoteFloatRes_SELECT_CC
1675 SDValue DAGTypeLegalizer::PromoteFloatOp_SELECT_CC(SDNode *N, unsigned OpNo) {
1676 SDValue LHS = GetPromotedFloat(N->getOperand(0));
1677 SDValue RHS = GetPromotedFloat(N->getOperand(1));
1679 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1680 LHS, RHS, N->getOperand(2), N->getOperand(3),
1684 // Construct a SETCC that compares the promoted values and sets the conditional
1686 SDValue DAGTypeLegalizer::PromoteFloatOp_SETCC(SDNode *N, unsigned OpNo) {
1687 EVT VT = N->getValueType(0);
1688 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1689 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1690 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1691 ISD::CondCode CCCode = cast<CondCodeSDNode>(N->getOperand(2))->get();
1693 return DAG.getSetCC(SDLoc(N), NVT, Op0, Op1, CCCode);
1697 // Lower the promoted Float down to the integer value of same size and construct
1698 // a STORE of the integer value.
1699 SDValue DAGTypeLegalizer::PromoteFloatOp_STORE(SDNode *N, unsigned OpNo) {
1700 StoreSDNode *ST = cast<StoreSDNode>(N);
1701 SDValue Val = ST->getValue();
1704 SDValue Promoted = GetPromotedFloat(Val);
1705 EVT VT = ST->getOperand(1)->getValueType(0);
1706 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1709 NewVal = DAG.getNode(GetPromotionOpcode(Promoted.getValueType(), VT), DL,
1712 return DAG.getStore(ST->getChain(), DL, NewVal, ST->getBasePtr(),
1713 ST->getMemOperand());
1716 //===----------------------------------------------------------------------===//
1717 // Float Result Promotion
1718 //===----------------------------------------------------------------------===//
1720 void DAGTypeLegalizer::PromoteFloatResult(SDNode *N, unsigned ResNo) {
1721 SDValue R = SDValue();
1723 switch (N->getOpcode()) {
1724 // These opcodes cannot appear if promotion of FP16 is done in the backend
1726 case ISD::FP16_TO_FP:
1727 case ISD::FP_TO_FP16:
1729 llvm_unreachable("Do not know how to promote this operator's result!");
1731 case ISD::BITCAST: R = PromoteFloatRes_BITCAST(N); break;
1732 case ISD::ConstantFP: R = PromoteFloatRes_ConstantFP(N); break;
1733 case ISD::EXTRACT_VECTOR_ELT:
1734 R = PromoteFloatRes_EXTRACT_VECTOR_ELT(N); break;
1735 case ISD::FCOPYSIGN: R = PromoteFloatRes_FCOPYSIGN(N); break;
1737 // Unary FP Operations
1747 case ISD::FNEARBYINT:
1753 case ISD::FTRUNC: R = PromoteFloatRes_UnaryOp(N); break;
1755 // Binary FP Operations
1763 case ISD::FSUB: R = PromoteFloatRes_BinOp(N); break;
1765 case ISD::FMA: // FMA is same as FMAD
1766 case ISD::FMAD: R = PromoteFloatRes_FMAD(N); break;
1768 case ISD::FPOWI: R = PromoteFloatRes_FPOWI(N); break;
1770 case ISD::FP_ROUND: R = PromoteFloatRes_FP_ROUND(N); break;
1771 case ISD::LOAD: R = PromoteFloatRes_LOAD(N); break;
1772 case ISD::SELECT: R = PromoteFloatRes_SELECT(N); break;
1773 case ISD::SELECT_CC: R = PromoteFloatRes_SELECT_CC(N); break;
1775 case ISD::SINT_TO_FP:
1776 case ISD::UINT_TO_FP: R = PromoteFloatRes_XINT_TO_FP(N); break;
1777 case ISD::UNDEF: R = PromoteFloatRes_UNDEF(N); break;
1782 SetPromotedFloat(SDValue(N, ResNo), R);
1785 // Bitcast from i16 to f16: convert the i16 to a f32 value instead.
1786 // At this point, it is not possible to determine if the bitcast value is
1787 // eventually stored to memory or promoted to f32 or promoted to a floating
1788 // point at a higher precision. Some of these cases are handled by FP_EXTEND,
1789 // STORE promotion handlers.
1790 SDValue DAGTypeLegalizer::PromoteFloatRes_BITCAST(SDNode *N) {
1791 EVT VT = N->getValueType(0);
1792 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1793 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT,
1797 SDValue DAGTypeLegalizer::PromoteFloatRes_ConstantFP(SDNode *N) {
1798 ConstantFPSDNode *CFPNode = cast<ConstantFPSDNode>(N);
1799 EVT VT = N->getValueType(0);
1802 // Get the (bit-cast) APInt of the APFloat and build an integer constant
1803 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1804 SDValue C = DAG.getConstant(CFPNode->getValueAPF().bitcastToAPInt(), DL,
1807 // Convert the Constant to the desired FP type
1808 // FIXME We might be able to do the conversion during compilation and get rid
1809 // of it from the object code
1810 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1811 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, C);
1814 // If the Index operand is a constant, try to redirect the extract operation to
1815 // the correct legalized vector. If not, bit-convert the input vector to
1816 // equivalent integer vector. Extract the element as an (bit-cast) integer
1817 // value and convert it to the promoted type.
1818 SDValue DAGTypeLegalizer::PromoteFloatRes_EXTRACT_VECTOR_ELT(SDNode *N) {
1821 // If the index is constant, try to extract the value from the legalized
1823 if (isa<ConstantSDNode>(N->getOperand(1))) {
1824 SDValue Vec = N->getOperand(0);
1825 SDValue Idx = N->getOperand(1);
1826 EVT VecVT = Vec->getValueType(0);
1827 EVT EltVT = VecVT.getVectorElementType();
1829 uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
1831 switch (getTypeAction(VecVT)) {
1833 case TargetLowering::TypeScalarizeVector: {
1834 SDValue Res = GetScalarizedVector(N->getOperand(0));
1835 ReplaceValueWith(SDValue(N, 0), Res);
1838 case TargetLowering::TypeWidenVector: {
1839 Vec = GetWidenedVector(Vec);
1840 SDValue Res = DAG.getNode(N->getOpcode(), DL, EltVT, Vec, Idx);
1841 ReplaceValueWith(SDValue(N, 0), Res);
1844 case TargetLowering::TypeSplitVector: {
1846 GetSplitVector(Vec, Lo, Hi);
1848 uint64_t LoElts = Lo.getValueType().getVectorNumElements();
1850 if (IdxVal < LoElts)
1851 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Lo, Idx);
1853 Res = DAG.getNode(N->getOpcode(), DL, EltVT, Hi,
1854 DAG.getConstant(IdxVal - LoElts, DL,
1855 Idx.getValueType()));
1856 ReplaceValueWith(SDValue(N, 0), Res);
1863 // Bit-convert the input vector to the equivalent integer vector
1864 SDValue NewOp = BitConvertVectorToIntegerVector(N->getOperand(0));
1865 EVT IVT = NewOp.getValueType().getVectorElementType();
1867 // Extract the element as an (bit-cast) integer value
1868 SDValue NewVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IVT,
1869 NewOp, N->getOperand(1));
1871 // Convert the element to the desired FP type
1872 EVT VT = N->getValueType(0);
1873 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1874 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, NewVal);
1877 // FCOPYSIGN(X, Y) returns the value of X with the sign of Y. If the result
1878 // needs promotion, so does the argument X. Note that Y, if needed, will be
1879 // handled during operand promotion.
1880 SDValue DAGTypeLegalizer::PromoteFloatRes_FCOPYSIGN(SDNode *N) {
1881 EVT VT = N->getValueType(0);
1882 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1883 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1885 SDValue Op1 = N->getOperand(1);
1887 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1890 // Unary operation where the result and the operand have PromoteFloat type
1891 // action. Construct a new SDNode with the promoted float value of the old
1893 SDValue DAGTypeLegalizer::PromoteFloatRes_UnaryOp(SDNode *N) {
1894 EVT VT = N->getValueType(0);
1895 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1896 SDValue Op = GetPromotedFloat(N->getOperand(0));
1898 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op);
1901 // Binary operations where the result and both operands have PromoteFloat type
1902 // action. Construct a new SDNode with the promoted float values of the old
1904 SDValue DAGTypeLegalizer::PromoteFloatRes_BinOp(SDNode *N) {
1905 EVT VT = N->getValueType(0);
1906 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1907 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1908 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1910 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1913 SDValue DAGTypeLegalizer::PromoteFloatRes_FMAD(SDNode *N) {
1914 EVT VT = N->getValueType(0);
1915 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1916 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1917 SDValue Op1 = GetPromotedFloat(N->getOperand(1));
1918 SDValue Op2 = GetPromotedFloat(N->getOperand(2));
1920 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1, Op2);
1923 // Promote the Float (first) operand and retain the Integer (second) operand
1924 SDValue DAGTypeLegalizer::PromoteFloatRes_FPOWI(SDNode *N) {
1925 EVT VT = N->getValueType(0);
1926 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1927 SDValue Op0 = GetPromotedFloat(N->getOperand(0));
1928 SDValue Op1 = N->getOperand(1);
1930 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, Op0, Op1);
1933 // Explicit operation to reduce precision. Reduce the value to half precision
1934 // and promote it back to the legal type.
1935 SDValue DAGTypeLegalizer::PromoteFloatRes_FP_ROUND(SDNode *N) {
1938 SDValue Op = N->getOperand(0);
1939 EVT VT = N->getValueType(0);
1940 EVT OpVT = Op->getValueType(0);
1941 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), N->getValueType(0));
1942 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1944 // Round promoted float to desired precision
1945 SDValue Round = DAG.getNode(GetPromotionOpcode(OpVT, VT), DL, IVT, Op);
1946 // Promote it back to the legal output type
1947 return DAG.getNode(GetPromotionOpcode(VT, NVT), DL, NVT, Round);
1950 SDValue DAGTypeLegalizer::PromoteFloatRes_LOAD(SDNode *N) {
1951 LoadSDNode *L = cast<LoadSDNode>(N);
1952 EVT VT = N->getValueType(0);
1954 // Load the value as an integer value with the same number of bits
1955 EVT IVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
1956 SDValue newL = DAG.getLoad(L->getAddressingMode(), L->getExtensionType(),
1957 IVT, SDLoc(N), L->getChain(), L->getBasePtr(),
1958 L->getOffset(), L->getPointerInfo(), IVT, L->isVolatile(),
1959 L->isNonTemporal(), false, L->getAlignment(),
1961 // Legalize the chain result by replacing uses of the old value chain with the
1963 ReplaceValueWith(SDValue(N, 1), newL.getValue(1));
1965 // Convert the integer value to the desired FP type
1966 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1967 return DAG.getNode(GetPromotionOpcode(VT, NVT), SDLoc(N), NVT, newL);
1970 // Construct a new SELECT node with the promoted true- and false- values.
1971 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT(SDNode *N) {
1972 SDValue TrueVal = GetPromotedFloat(N->getOperand(1));
1973 SDValue FalseVal = GetPromotedFloat(N->getOperand(2));
1975 return DAG.getNode(ISD::SELECT, SDLoc(N), TrueVal->getValueType(0),
1976 N->getOperand(0), TrueVal, FalseVal);
1979 // Construct a new SELECT_CC node with the promoted true- and false- values.
1980 // The operands used for comparison are promoted by PromoteFloatOp_SELECT_CC.
1981 SDValue DAGTypeLegalizer::PromoteFloatRes_SELECT_CC(SDNode *N) {
1982 SDValue TrueVal = GetPromotedFloat(N->getOperand(2));
1983 SDValue FalseVal = GetPromotedFloat(N->getOperand(3));
1985 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1986 N->getOperand(0), N->getOperand(1), TrueVal, FalseVal,
1990 // Construct a SDNode that transforms the SINT or UINT operand to the promoted
1992 SDValue DAGTypeLegalizer::PromoteFloatRes_XINT_TO_FP(SDNode *N) {
1993 EVT VT = N->getValueType(0);
1994 EVT NVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT);
1995 return DAG.getNode(N->getOpcode(), SDLoc(N), NVT, N->getOperand(0));
1998 SDValue DAGTypeLegalizer::PromoteFloatRes_UNDEF(SDNode *N) {
1999 return DAG.getUNDEF(TLI.getTypeToTransformTo(*DAG.getContext(),
2000 N->getValueType(0)));